It is a project for Bytom Chrome extension JS SDK https://bytom.github.io/Bytom-JS-SDK
リビジョン | e2655076ba594759771d7e58498fce665696c8c3 (tree) |
---|---|
日時 | 2020-08-25 16:56:48 |
作者 | Zhiting Lin <zlin035@uott...> |
コミッター | Zhiting Lin |
update key relavant logic
@@ -407,6 +407,11 @@ | ||
407 | 407 | "randomfill": "^1.0.3" |
408 | 408 | } |
409 | 409 | }, |
410 | + "crypto-js": { | |
411 | + "version": "4.0.0", | |
412 | + "resolved": "https://registry.npm.taobao.org/crypto-js/download/crypto-js-4.0.0.tgz?cache=0&sync_timestamp=1581509247325&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcrypto-js%2Fdownload%2Fcrypto-js-4.0.0.tgz", | |
413 | + "integrity": "sha1-KQSrJnep0EKFai6i74DekuSjbcw=" | |
414 | + }, | |
410 | 415 | "debug": { |
411 | 416 | "version": "3.1.0", |
412 | 417 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", |
@@ -18,6 +18,7 @@ | ||
18 | 18 | "bn.js": "^5.1.1", |
19 | 19 | "create-hmac": "^1.1.7", |
20 | 20 | "crypto-browserify": "^3.12.0", |
21 | + "crypto-js": "^4.0.0", | |
21 | 22 | "elliptic": "^6.5.2", |
22 | 23 | "eslint": "^5.6.1", |
23 | 24 | "js-sha3": "^0.8.0", |
@@ -14,7 +14,8 @@ function accountsSDK(bytom){ | ||
14 | 14 | * @returns {Promise} List of Accounts |
15 | 15 | */ |
16 | 16 | accountsSDK.prototype.listAccountUseServer = function() { |
17 | - let net = this.bytom.net; | |
17 | + // let net = 'mainnet'; | |
18 | + let net = 'testnet'; | |
18 | 19 | let retPromise = new Promise((resolve, reject) => { |
19 | 20 | getDB().then(db => { |
20 | 21 | let transaction = db.transaction(['accounts-server'], 'readonly'); |
@@ -4,6 +4,7 @@ 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 | import { camelize } from '../utils/utils'; |
7 | +import CryptoJS from 'crypto-js'; | |
7 | 8 | |
8 | 9 | |
9 | 10 | function keysSDK() { |
@@ -127,6 +128,9 @@ keysSDK.prototype.createKey = function(alias, password) { | ||
127 | 128 | data.alias = normalizedAlias; |
128 | 129 | data.password = password; |
129 | 130 | const res = createkey(data); |
131 | + | |
132 | + res.vault = this.encryptMnemonic(res.mnemonic, password, res.keystore); | |
133 | + | |
130 | 134 | return res; |
131 | 135 | }; |
132 | 136 |
@@ -145,6 +149,8 @@ keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) { | ||
145 | 149 | data.mnemonic = mnemonic; |
146 | 150 | |
147 | 151 | const res = createkey(data); |
152 | + | |
153 | + res.vault = this.encryptMnemonic(mnemonic, password, res.keystore); | |
148 | 154 | return res; |
149 | 155 | }; |
150 | 156 |
@@ -163,6 +169,43 @@ keysSDK.prototype.restoreFromKeystore = function( password, keystore) { | ||
163 | 169 | return result; |
164 | 170 | }; |
165 | 171 | |
172 | + | |
173 | +/** | |
174 | + * Create a new key. | |
175 | + * | |
176 | + * @param {String} alias - User specified, unique identifier. | |
177 | + * @param {String} password - User specified, key password. | |
178 | + */ | |
179 | +keysSDK.prototype.encryptMnemonic = function(mnemonic, password, keystore) { | |
180 | + | |
181 | + const result = decryptKey(keystore, password); | |
182 | + const xprv = result.xPrv.toString('hex'); | |
183 | + | |
184 | + const ciphertext = CryptoJS.AES.encrypt(mnemonic, xprv); | |
185 | + | |
186 | + return ciphertext.toString(); | |
187 | +}; | |
188 | + | |
189 | +/** | |
190 | + * Create a new key. | |
191 | + * | |
192 | + * @param {String} alias - User specified, unique identifier. | |
193 | + * @param {String} password - User specified, key password. | |
194 | + */ | |
195 | +keysSDK.prototype.decryptMnemonic = function(ciphertext, password, keystore) { | |
196 | + | |
197 | + const result = decryptKey(keystore, password); | |
198 | + const xprv = result.xPrv.toString('hex'); | |
199 | + | |
200 | + | |
201 | + const bytes = CryptoJS.AES.decrypt(ciphertext, xprv); | |
202 | + const plaintext = bytes.toString(CryptoJS.enc.Utf8); | |
203 | + | |
204 | + return plaintext; | |
205 | +}; | |
206 | + | |
207 | + | |
208 | + | |
166 | 209 | /** |
167 | 210 | * Create a new key. |
168 | 211 | * |
@@ -287,55 +330,19 @@ keysSDK.prototype.createPubkey = function(xpub) { | ||
287 | 330 | * @param {String} password - password. |
288 | 331 | * @param {Object} address - address. |
289 | 332 | */ |
290 | -keysSDK.prototype.signMessage = function(message, password, address) { | |
291 | - let retPromise = new Promise((resolve, reject) => { | |
292 | - getDB().then(db => { | |
293 | - let getRequest = db.transaction(['accounts-server'], 'readonly') | |
294 | - .objectStore('accounts-server') | |
295 | - .getAll(); | |
296 | - | |
297 | - getRequest.onsuccess = function (e) { | |
298 | - const result = getRequest.result.filter(obj => (obj.address === address || obj.vpAddress === address)); | |
299 | - if (result.length === 0) { | |
300 | - reject(new Error('not found address')); | |
301 | - return; | |
302 | - } | |
303 | - | |
304 | - const rootXpub = result[0].rootXPub; | |
305 | - let keyObject = db.transaction(['keys'], 'readonly') | |
306 | - .objectStore('keys') | |
307 | - .index('xpub') | |
308 | - .get(rootXpub); | |
333 | +keysSDK.prototype.signMessage = function(message, password, keystore) { | |
334 | + let data = {}; | |
335 | + data.message = message; | |
336 | + data.password = password; | |
337 | + data.key = keystore; | |
338 | + return signMessage(data).then((res) => { | |
339 | + let jsonData = JSON.parse(res.data); | |
340 | + return (jsonData); | |
341 | + }).catch(error => { | |
342 | + throw (error); | |
343 | + }); | |
309 | 344 | |
310 | - keyObject.onsuccess = function (e) { | |
311 | - if (!e.target.result) { | |
312 | - reject(new Error('not found xpub')); | |
313 | - return; | |
314 | - } | |
315 | 345 | |
316 | - let data = {}; | |
317 | - data.message = message; | |
318 | - data.password = password; | |
319 | - data.key = e.target.result.key; | |
320 | - signMessage(data).then((res) => { | |
321 | - let jsonData = JSON.parse(res.data); | |
322 | - resolve(jsonData); | |
323 | - }).catch(error => { | |
324 | - reject(error); | |
325 | - }); | |
326 | - }; | |
327 | - keyObject.onerror = function () { | |
328 | - reject(getRequest.error); | |
329 | - }; | |
330 | - }; | |
331 | - getRequest.onerror = function () { | |
332 | - reject(getRequest.error); | |
333 | - }; | |
334 | - }).catch(error => { | |
335 | - reject(error); | |
336 | - }); | |
337 | - }); | |
338 | - return retPromise; | |
339 | 346 | }; |
340 | 347 | |
341 | 348 | export default keysSDK; |
\ No newline at end of file |
@@ -73,7 +73,7 @@ transactionSDK.prototype.submitPayment = function(address, raw_transaction, sign | ||
73 | 73 | * @param {Number} confirmations - transaction confirmations |
74 | 74 | * @returns {Promise} |
75 | 75 | */ |
76 | -transactionSDK.prototype.buildPayment = function(address, to, asset, amount, fee, confirmations, memo, forbidChainTx) { | |
76 | +transactionSDK.prototype.buildPayment = function(address, to, asset, amount, confirmations, memo, forbidChainTx) { | |
77 | 77 | let net = this.bytom.net; |
78 | 78 | let pm = { |
79 | 79 | asset: asset, |
@@ -89,9 +89,6 @@ transactionSDK.prototype.buildPayment = function(address, to, asset, amount, fee | ||
89 | 89 | if (forbidChainTx) { |
90 | 90 | pm.forbid_chain_tx = forbidChainTx; |
91 | 91 | } |
92 | - if (fee) { | |
93 | - pm.fee = fee; | |
94 | - } | |
95 | 92 | if (confirmations) { |
96 | 93 | pm.confirmations = confirmations; |
97 | 94 | } |
@@ -152,6 +149,13 @@ transactionSDK.prototype.buildVote = function(address, vote, amount, confirmatio | ||
152 | 149 | return this.http.request(`merchant/build-vote?address=${address}`, pm, net); |
153 | 150 | }; |
154 | 151 | |
152 | +transactionSDK.prototype.estimateFee = function(address, asset_amounts, confirmations=1) { | |
153 | + let net = this.bytom.net; | |
154 | + let pm = {asset_amounts, confirmations}; | |
155 | + | |
156 | + return this.http.request(`merchant/estimate-tx-fee?address=${address}`, pm, net); | |
157 | +}; | |
158 | + | |
155 | 159 | /** |
156 | 160 | * Build a Veto transaction. |
157 | 161 | * |