• 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

Revisionec08d8ca5b213deaba11cbd02d406add43201060 (tree)
Time2020-07-31 19:26:18
AuthorZhiting Lin <zlin035@uott...>
CommiterZhiting Lin

Log Message

update create key

Change Summary

Incremental Difference

--- a/src/sdk/accounts.js
+++ b/src/sdk/accounts.js
@@ -94,6 +94,25 @@ accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) {
9494
9595 /**
9696 * 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)
97116 *
98117 * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints
99118 * @param {String} rootXPub
--- a/src/sdk/keys.js
+++ b/src/sdk/keys.js
@@ -1,6 +1,6 @@
11 import { createKey ,resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func';
22 import {getDB} from '../db/db';
3-import {createkey} from '../utils/key/createKey';
3+import {createkey, isValidMnemonic} from '../utils/key/createKey';
44 import {encryptKey, decryptKey} from '../utils/key/keystore';
55 import { restoreFromKeyStore } from '../utils/account';
66
@@ -145,11 +145,52 @@ keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) {
145145
146146 const res = createkey(data);
147147
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) {
149158
150- //Todo: /account/wallets api find if xpub exist in the blockcenter, yes restore, otherwise create new account
159+ const walletImage = JSON.parse(keystore);
151160
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);
153194 };
154195
155196 /**
--- a/src/sdk/wallet.js
+++ b/src/sdk/wallet.js
@@ -1,5 +1,6 @@
11 import {initDB, getDB} from '../db/db';
22 import { restoreFromKeyStore } from '../utils/account';
3+import accountsSDK from './accounts';
34
45 function walletSDK(bytom) {
56 this.http = bytom.serverHttp;
@@ -52,6 +53,12 @@ walletSDK.prototype.backup = function() {
5253 return retPromise;
5354 };
5455
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+
5562 /**
5663 * Restore wallet.
5764 *
--- a/src/utils/key/createKey.js
+++ b/src/utils/key/createKey.js
@@ -30,7 +30,7 @@ function createkey({
3030 return {alias: obj.alias, xpub: obj.xPub.toString('hex'), keystore: obj.keystore, mnemonic:obj.mnemonic};
3131 }
3232
33-function importKeyFromMnemonic(alias, password, mnemonic, language) {
33+function isValidMnemonic(mnemonic, language){
3434 // checksum length = entropy length /32
3535 // mnemonic length = (entropy length + checksum length)/11
3636 let mnemArray = mnemonic.trim().split(' ');
@@ -43,6 +43,10 @@ function importKeyFromMnemonic(alias, password, mnemonic, language) {
4343 if (!bip39.validateMnemonic(mnemonic, WORDLISTS[language])) {
4444 throw 'mnemonic is invalid';
4545 }
46+}
47+
48+function importKeyFromMnemonic(alias, password, mnemonic, language) {
49+ isValidMnemonic(mnemonic, language)
4650
4751 return createKeyFromMnemonic(alias, password, mnemonic);
4852 }
@@ -72,7 +76,7 @@ function createChainKDKey(alias,password, language){
7276 let mnemonic = bip39.generateMnemonic(EntropyLength,undefined, WORDLISTS[language]);
7377
7478 let object = createKeyFromMnemonic(alias, password, mnemonic);
75- object.mnemonic = mnemonic
79+ object.mnemonic = mnemonic;
7680
7781 return object;
7882 }
@@ -80,5 +84,6 @@ function createChainKDKey(alias,password, language){
8084 export {
8185 createkey,
8286 importKeyFromMnemonic,
83- createNewKey
87+ createNewKey,
88+ isValidMnemonic
8489 };
Show on old repository browser