OSDN Git Service

Add the recovery api for the restore wallet by seed.
[bytom/bytom-electron.git] / src / features / initialization / actions.js
1 import { chainClient } from 'utility/environment'
2 import {push} from 'react-router-redux'
3
4 const registerKey = (data) => {
5   return (dispatch) => {
6     if (typeof data.keyAlias == 'string')  data.keyAlias = data.keyAlias.trim()
7
8     const keyData = {
9       'alias': data.keyAlias,
10       'password': data.password
11     }
12
13     return chainClient().mockHsm.keys.create(keyData)
14       .then((resp) => {
15         if (resp.status === 'fail') {
16           throw resp
17         }
18
19         if (typeof data.accountAlias == 'string')  data.accountAlias = data.accountAlias.trim()
20         const accountData = {
21           'root_xpubs':[resp.data.xpub],
22           'quorum':1,
23           'alias': data.accountAlias}
24
25         dispatch({type: 'INIT_ACCOUNT', data: resp.data.mnemonic})
26
27         chainClient().accounts.create(accountData)
28           .then((resp) => {
29             if (resp.status === 'fail') {
30               throw resp
31             }
32
33             if(resp.status === 'success') {
34               dispatch(push('/initialization/mnemonic'))
35             }
36           })
37           .catch((err) => {
38             throw err
39           })
40       })
41       .catch((err) => {
42         throw err
43       })
44   }
45 }
46
47 const restoreKeystore = (data, success) => {
48   return (dispatch) => {
49     const file = data.file
50
51     return new Promise(function(resolve, reject){
52       const fileReader = new FileReader()
53
54       fileReader.onload = function(e) {
55         const result = JSON.parse(e.target.result)
56         return chainClient().backUp.restore(result)
57           .then(resp => {
58             if (resp.status === 'fail') {
59               throw resp
60             }
61             resolve()
62             dispatch(success)
63           })
64           .catch((err) => {
65             reject(err) })
66       }
67
68       fileReader.readAsText(file, 'UTF-8')
69       fileReader.onerror = function(error) { reject(error) }
70     })
71   }
72 }
73
74 const restoreMnemonic = (data, success) => {
75   return (dispatch) => {
76     if (typeof data.keyAlias == 'string')  data.keyAlias = data.keyAlias.trim()
77     if (typeof data.mnemonic == 'string') data.mnemonic = data.mnemonic.trim()
78
79     const keyData = {
80       'alias': data.keyAlias,
81       'password': data.password,
82       'mnemonic': data.mnemonic
83     }
84
85     return chainClient().mockHsm.keys.create(keyData)
86       .then((resp) => {
87         if (resp.status === 'fail') {
88           throw resp
89         }else{
90           return chainClient().backUp.recovery({
91             xpub: resp.data.xpub
92           })
93             .then((resp) => {
94               if (resp.status === 'fail') {
95                 throw resp
96               }
97
98               dispatch(success)
99             })
100             .catch((err) => {
101               throw err
102             })
103         }
104       })
105       .catch((err) => {
106         throw err
107       })
108   }
109 }
110
111 const initSucceeded = () => (dispatch) => {
112   dispatch({type: 'CREATE_REGISTER_ACCOUNT'})
113   dispatch(push({
114     pathname: '/transactions',
115     state: {
116       preserveFlash: true
117     }
118   }))
119 }
120
121 let actions = {
122   initSucceeded,
123   registerKey,
124   restoreKeystore,
125   restoreMnemonic
126 }
127
128 export default actions