It is a project for Bytom Chrome extension JS SDK https://bytom.github.io/Bytom-JS-SDK
Revision | ec08d8ca5b213deaba11cbd02d406add43201060 (tree) |
---|---|
Time | 2020-07-31 19:26:18 |
Author | Zhiting Lin <zlin035@uott...> |
Commiter | Zhiting Lin |
update create key
@@ -94,6 +94,25 @@ accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) { | ||
94 | 94 | |
95 | 95 | /** |
96 | 96 | * Create a wallet using a public key. Each wallet is identified by a guid. (by server) |
97 | + * | |
98 | + * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints | |
99 | + * @param {String} rootXPub | |
100 | + * @param {String} alias alias for the account | |
101 | + * @param {String} label alias for the first address | |
102 | + * @returns {Promise} | |
103 | + */ | |
104 | +accountsSDK.prototype.createNewAccount = function(rootXPub, label) { | |
105 | + let net = this.bytom.net; | |
106 | + let pm = {pubkey: rootXPub}; | |
107 | + if (label) { | |
108 | + pm.label = label; | |
109 | + } | |
110 | + return this.http.request('account/create', pm, net); | |
111 | +}; | |
112 | + | |
113 | + | |
114 | +/** | |
115 | + * Create a wallet using a public key. Each wallet is identified by a guid. (by server) | |
97 | 116 | * |
98 | 117 | * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints |
99 | 118 | * @param {String} rootXPub |
@@ -1,6 +1,6 @@ | ||
1 | 1 | import { createKey ,resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func'; |
2 | 2 | import {getDB} from '../db/db'; |
3 | -import {createkey} from '../utils/key/createKey'; | |
3 | +import {createkey, isValidMnemonic} from '../utils/key/createKey'; | |
4 | 4 | import {encryptKey, decryptKey} from '../utils/key/keystore'; |
5 | 5 | import { restoreFromKeyStore } from '../utils/account'; |
6 | 6 |
@@ -145,11 +145,52 @@ keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) { | ||
145 | 145 | |
146 | 146 | const res = createkey(data); |
147 | 147 | |
148 | - const xpub = res.xpub; | |
148 | + return res; | |
149 | +}; | |
150 | + | |
151 | +/** | |
152 | + * Create a new key. | |
153 | + * | |
154 | + * @param {String} alias - User specified, unique identifier. | |
155 | + * @param {String} password - User specified, key password. | |
156 | + */ | |
157 | +keysSDK.prototype.restoreFromKeystore = function( password, keystore) { | |
149 | 158 | |
150 | - //Todo: /account/wallets api find if xpub exist in the blockcenter, yes restore, otherwise create new account | |
159 | + const walletImage = JSON.parse(keystore); | |
151 | 160 | |
152 | - return res; | |
161 | + let keys, key; | |
162 | + if(walletImage.key_images && walletImage.key_images.xkeys ){ | |
163 | + keys = walletImage.key_images.xkeys; | |
164 | + } | |
165 | + | |
166 | + // match older version of backups keystore files | |
167 | + else if(walletImage.keys>0){ | |
168 | + keys = walletImage.keys.map(keyItem => JSON.parse( keyItem.key ) ); | |
169 | + }else{ | |
170 | + key = walletImage; | |
171 | + } | |
172 | + | |
173 | + if(keys.length>1){ | |
174 | + throw 'do not support multiple keystore imported.'; | |
175 | + } | |
176 | + else if(keys.length === 1){ | |
177 | + key = keys[0]; | |
178 | + } | |
179 | + | |
180 | + const result = decryptKey(key, password); | |
181 | + | |
182 | + return result; | |
183 | +}; | |
184 | + | |
185 | +/** | |
186 | + * Create a new key. | |
187 | + * | |
188 | + * @param {String} alias - User specified, unique identifier. | |
189 | + * @param {String} password - User specified, key password. | |
190 | + */ | |
191 | +keysSDK.prototype.isValidMnemonic = function(mnemonic) { | |
192 | + | |
193 | + return isValidMnemonic(mnemonic); | |
153 | 194 | }; |
154 | 195 | |
155 | 196 | /** |
@@ -1,5 +1,6 @@ | ||
1 | 1 | import {initDB, getDB} from '../db/db'; |
2 | 2 | import { restoreFromKeyStore } from '../utils/account'; |
3 | +import accountsSDK from './accounts'; | |
3 | 4 | |
4 | 5 | function walletSDK(bytom) { |
5 | 6 | this.http = bytom.serverHttp; |
@@ -52,6 +53,12 @@ walletSDK.prototype.backup = function() { | ||
52 | 53 | return retPromise; |
53 | 54 | }; |
54 | 55 | |
56 | +walletSDK.prototype.list = function(pubkey) { | |
57 | + let net = this.bytom.net; | |
58 | + let pm = {pubkey}; | |
59 | + return this.http.request('account/wallets', pm, net); | |
60 | +}; | |
61 | + | |
55 | 62 | /** |
56 | 63 | * Restore wallet. |
57 | 64 | * |
@@ -30,7 +30,7 @@ function createkey({ | ||
30 | 30 | return {alias: obj.alias, xpub: obj.xPub.toString('hex'), keystore: obj.keystore, mnemonic:obj.mnemonic}; |
31 | 31 | } |
32 | 32 | |
33 | -function importKeyFromMnemonic(alias, password, mnemonic, language) { | |
33 | +function isValidMnemonic(mnemonic, language){ | |
34 | 34 | // checksum length = entropy length /32 |
35 | 35 | // mnemonic length = (entropy length + checksum length)/11 |
36 | 36 | let mnemArray = mnemonic.trim().split(' '); |
@@ -43,6 +43,10 @@ function importKeyFromMnemonic(alias, password, mnemonic, language) { | ||
43 | 43 | if (!bip39.validateMnemonic(mnemonic, WORDLISTS[language])) { |
44 | 44 | throw 'mnemonic is invalid'; |
45 | 45 | } |
46 | +} | |
47 | + | |
48 | +function importKeyFromMnemonic(alias, password, mnemonic, language) { | |
49 | + isValidMnemonic(mnemonic, language) | |
46 | 50 | |
47 | 51 | return createKeyFromMnemonic(alias, password, mnemonic); |
48 | 52 | } |
@@ -72,7 +76,7 @@ function createChainKDKey(alias,password, language){ | ||
72 | 76 | let mnemonic = bip39.generateMnemonic(EntropyLength,undefined, WORDLISTS[language]); |
73 | 77 | |
74 | 78 | let object = createKeyFromMnemonic(alias, password, mnemonic); |
75 | - object.mnemonic = mnemonic | |
79 | + object.mnemonic = mnemonic; | |
76 | 80 | |
77 | 81 | return object; |
78 | 82 | } |
@@ -80,5 +84,6 @@ function createChainKDKey(alias,password, language){ | ||
80 | 84 | export { |
81 | 85 | createkey, |
82 | 86 | importKeyFromMnemonic, |
83 | - createNewKey | |
87 | + createNewKey, | |
88 | + isValidMnemonic | |
84 | 89 | }; |