OSDN Git Service

merge dashboard into electron
[bytom/bytom-electron.git] / src / features / mockhsm / actions.js
1 import { baseListActions, baseCreateActions } from 'features/shared/actions'
2 import { chainClient } from 'utility/environment'
3 import {push} from 'react-router-redux'
4
5 const type = 'key'
6 const clientApi = () => chainClient().mockHsm.keys
7
8 const list = baseListActions(type, {
9   className: 'Key',
10   clientApi,
11 })
12 const create = baseCreateActions(type, {
13   className: 'Key',
14   clientApi,
15 })
16
17 const resetPassword = {
18   submitResetForm: (params) => {
19     let promise = Promise.resolve()
20     return (dispatch)  => {
21       return promise.then(() => clientApi().resetPassword(params).then((resp) => {
22         if(resp.data.changed){
23           dispatch({ type: `RESET_PASSWORD_${type.toUpperCase()}`, resp })
24           dispatch(push({
25             pathname: `/${type}s/${params.xpub}`,
26             state: {
27               preserveFlash: true
28             }
29           }))
30         }else{
31           let msg = 'Unable to reset the key password.'
32           throw new Error(msg)
33         }
34       }))
35     }
36   }
37 }
38
39 const checkPassword = (data) => (dispatch) => {
40   return clientApi().checkPassword(data)
41     .then((resp) => {
42       if(resp.status === 'fail'){
43         throw new Error(resp.msg)
44       }else if(!resp.data.checkResult){
45         throw new Error('Your Password is wrong')
46       }
47       dispatch({ type: 'KEY_PASSWORD_SUCCESS'})
48     })
49 }
50
51 const createExport =  (arg, fileName) => (dispatch) => {
52   clientApi().export(arg).then(resp => {
53     if(resp.status == 'success'){
54       const privateKey = resp.data.privateKey
55
56       var element = document.createElement('a')
57       element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
58       element.setAttribute('download', fileName)
59       element.style.display = 'none'
60       document.body.appendChild(element)
61       element.click()
62
63       document.body.removeChild(element)
64     }else if(resp.status == 'fail'){
65       dispatch({ type: 'ERROR', payload: {message: resp.msg} })
66     }
67   })
68 }
69
70 export default {
71   ...create,
72   ...list,
73   ...resetPassword,
74   checkPassword,
75   createExport
76 }