From 67b37212bba74d9ee066200a9196d3e3bff40bb5 Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Thu, 18 Jan 2018 18:08:16 +0800 Subject: [PATCH] add importing private key --- src/features/mockhsm/components/New.jsx | 57 ++++++++++++++++++++++++++++++--- src/sdk/api/mockHsmKeys.js | 5 ++- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/features/mockhsm/components/New.jsx b/src/features/mockhsm/components/New.jsx index 58d7006..3f84f1f 100644 --- a/src/features/mockhsm/components/New.jsx +++ b/src/features/mockhsm/components/New.jsx @@ -7,18 +7,46 @@ class New extends React.Component { super(props) this.submitWithErrors = this.submitWithErrors.bind(this) + this.state = { + checked: false, + key: null + } } submitWithErrors(data) { return new Promise((resolve, reject) => { - this.props.submitForm(data) - .catch((err) => reject({_error: err})) + this.props.submitForm(Object.assign({}, data, { + xprv: this.state.key, + index: 5000 + })).catch((err) => reject({_error: err})) }) } + handleCheckedChange(e) { + this.setState({ + checked: e.target.checked + }) + } + + handleFileChange(event) { + const files = event.target.files + if (files.length <= 0) { + this.setState({key: null}) + return + } + + const fileReader = new FileReader() + fileReader.onload = fileLoadedEvent => { + this.setState({ + key: fileLoadedEvent.target.result + }) + } + fileReader.readAsText(files[0], 'UTF-8') + } + render() { const { - fields: { alias, password }, + fields: { alias, password, accountAlias }, error, handleSubmit, submitting @@ -34,13 +62,31 @@ class New extends React.Component { {/**/} +
+ + +
+ { + this.state.checked && + + } + { + this.state.checked && + + }
) } } -const fields = [ 'alias', 'password' ] +const fields = [ 'alias', 'password', 'accountAlias' ] export default BaseNew.connect( BaseNew.mapStateToProps('key'), BaseNew.mapDispatchToProps('key'), @@ -53,6 +99,9 @@ export default BaseNew.connect( if (!values.alias) { errors.alias = 'Key alias is required' } + if (!values.accountAlias) { + errors.accountAlias = 'Account alias is required' + } return errors } diff --git a/src/sdk/api/mockHsmKeys.js b/src/sdk/api/mockHsmKeys.js index 69374a5..a0e7b00 100644 --- a/src/sdk/api/mockHsmKeys.js +++ b/src/sdk/api/mockHsmKeys.js @@ -5,8 +5,11 @@ const mockHsmKeysAPI = (client) => { return { create: (params, cb) => { let body = Object.assign({ clientToken: uuid.v4() }, params, {password: '123456'}) + const uri = body.xprv ? '/import-private-key' : '/create-key' + + debugger return shared.tryCallback( - client.request('/create-key', body).then(data => data), + client.request(uri, body).then(data => data), cb ) }, -- 2.11.0