• R/O
  • HTTP
  • SSH
  • HTTPS

Bytom-JS-SDK: Commit

It is a project for Bytom Chrome extension JS SDK https://bytom.github.io/Bytom-JS-SDK


Commit MetaInfo

Revisione2655076ba594759771d7e58498fce665696c8c3 (tree)
Time2020-08-25 16:56:48
AuthorZhiting Lin <zlin035@uott...>
CommiterZhiting Lin

Log Message

update key relavant logic

Change Summary

Incremental Difference

--- a/package-lock.json
+++ b/package-lock.json
@@ -407,6 +407,11 @@
407407 "randomfill": "^1.0.3"
408408 }
409409 },
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+ },
410415 "debug": {
411416 "version": "3.1.0",
412417 "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
--- a/package.json
+++ b/package.json
@@ -18,6 +18,7 @@
1818 "bn.js": "^5.1.1",
1919 "create-hmac": "^1.1.7",
2020 "crypto-browserify": "^3.12.0",
21+ "crypto-js": "^4.0.0",
2122 "elliptic": "^6.5.2",
2223 "eslint": "^5.6.1",
2324 "js-sha3": "^0.8.0",
--- a/src/sdk/accounts.js
+++ b/src/sdk/accounts.js
@@ -14,7 +14,8 @@ function accountsSDK(bytom){
1414 * @returns {Promise} List of Accounts
1515 */
1616 accountsSDK.prototype.listAccountUseServer = function() {
17- let net = this.bytom.net;
17+ // let net = 'mainnet';
18+ let net = 'testnet';
1819 let retPromise = new Promise((resolve, reject) => {
1920 getDB().then(db => {
2021 let transaction = db.transaction(['accounts-server'], 'readonly');
--- a/src/sdk/keys.js
+++ b/src/sdk/keys.js
@@ -4,6 +4,7 @@ import {createkey, isValidMnemonic} from '../utils/key/createKey';
44 import {encryptKey, decryptKey} from '../utils/key/keystore';
55 import { restoreFromKeyStore } from '../utils/account';
66 import { camelize } from '../utils/utils';
7+import CryptoJS from 'crypto-js';
78
89
910 function keysSDK() {
@@ -127,6 +128,9 @@ keysSDK.prototype.createKey = function(alias, password) {
127128 data.alias = normalizedAlias;
128129 data.password = password;
129130 const res = createkey(data);
131+
132+ res.vault = this.encryptMnemonic(res.mnemonic, password, res.keystore);
133+
130134 return res;
131135 };
132136
@@ -145,6 +149,8 @@ keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) {
145149 data.mnemonic = mnemonic;
146150
147151 const res = createkey(data);
152+
153+ res.vault = this.encryptMnemonic(mnemonic, password, res.keystore);
148154 return res;
149155 };
150156
@@ -163,6 +169,43 @@ keysSDK.prototype.restoreFromKeystore = function( password, keystore) {
163169 return result;
164170 };
165171
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+
166209 /**
167210 * Create a new key.
168211 *
@@ -287,55 +330,19 @@ keysSDK.prototype.createPubkey = function(xpub) {
287330 * @param {String} password - password.
288331 * @param {Object} address - address.
289332 */
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+ });
309344
310- keyObject.onsuccess = function (e) {
311- if (!e.target.result) {
312- reject(new Error('not found xpub'));
313- return;
314- }
315345
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;
339346 };
340347
341348 export default keysSDK;
\ No newline at end of file
--- a/src/sdk/transaction.js
+++ b/src/sdk/transaction.js
@@ -73,7 +73,7 @@ transactionSDK.prototype.submitPayment = function(address, raw_transaction, sign
7373 * @param {Number} confirmations - transaction confirmations
7474 * @returns {Promise}
7575 */
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) {
7777 let net = this.bytom.net;
7878 let pm = {
7979 asset: asset,
@@ -89,9 +89,6 @@ transactionSDK.prototype.buildPayment = function(address, to, asset, amount, fee
8989 if (forbidChainTx) {
9090 pm.forbid_chain_tx = forbidChainTx;
9191 }
92- if (fee) {
93- pm.fee = fee;
94- }
9592 if (confirmations) {
9693 pm.confirmations = confirmations;
9794 }
@@ -152,6 +149,13 @@ transactionSDK.prototype.buildVote = function(address, vote, amount, confirmatio
152149 return this.http.request(`merchant/build-vote?address=${address}`, pm, net);
153150 };
154151
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+
155159 /**
156160 * Build a Veto transaction.
157161 *
Show on old repository browser