OSDN Git Service

add importing private key
authorYongfeng LI <wliyongfeng@gmail.com>
Thu, 18 Jan 2018 10:08:16 +0000 (18:08 +0800)
committerYongfeng LI <wliyongfeng@gmail.com>
Thu, 18 Jan 2018 10:08:16 +0000 (18:08 +0800)
src/features/mockhsm/components/New.jsx
src/sdk/api/mockHsmKeys.js

index 58d7006..3f84f1f 100644 (file)
@@ -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 {
         <FormSection title='Key Information'>
           <TextField title='Alias' placeholder='Alias' fieldProps={alias} autoFocus={true} />
           {/*<TextField title='Password' placeholder='Password' fieldProps={password} autoFocus={false} type={'password'} />*/}
+          <div>
+            <input type='checkbox' id='private_key_file_input'
+                   checked={this.state.import}
+                   onChange={this.handleCheckedChange.bind(this)}/>
+            <label htmlFor='private_key_file_input'
+                   style={{'marginLeft': '5px', 'userSelect': 'none'}}>
+              import from file
+            </label>
+          </div>
+          {
+            this.state.checked &&
+              <TextField title='Account alias' placeholder='Account alias' fieldProps={accountAlias}></TextField>
+          }
+          {
+            this.state.checked &&
+            <input type='file' style={{'display': 'flex', 'alignItems': 'center', 'fontSize': '12px'}}
+                   onChange={this.handleFileChange.bind(this)}/>
+          }
         </FormSection>
       </FormContainer>
     )
   }
 }
 
-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
     }
index 69374a5..a0e7b00 100644 (file)
@@ -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
       )
     },