OSDN Git Service

update create key
authorZhiting Lin <zlin035@uottawa.ca>
Fri, 31 Jul 2020 10:26:18 +0000 (18:26 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Fri, 31 Jul 2020 10:26:18 +0000 (18:26 +0800)
src/sdk/accounts.js
src/sdk/keys.js
src/sdk/wallet.js
src/utils/key/createKey.js

index ef7d2c4..38aee4e 100644 (file)
@@ -94,6 +94,25 @@ accountsSDK.prototype.createAccountReceiverUseServer = function(guid, label) {
 
 /**
  * Create a wallet using a public key. Each wallet is identified by a guid. (by server)
+ *
+ * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints
+ * @param {String} rootXPub
+ * @param {String} alias alias for the account
+ * @param {String} label alias for the first address
+ * @returns {Promise}
+ */
+accountsSDK.prototype.createNewAccount = function(rootXPub, label) {
+    let net = this.bytom.net;
+    let pm = {pubkey: rootXPub};
+    if (label) {
+        pm.label = label;
+    }
+    return this.http.request('account/create', pm, net);
+};
+
+
+/**
+ * Create a wallet using a public key. Each wallet is identified by a guid. (by server)
  * 
  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#endpoints
  * @param {String} rootXPub
index dc68e6b..8c250dc 100644 (file)
@@ -1,6 +1,6 @@
 import { createKey ,resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func';
 import {getDB} from '../db/db';
-import {createkey} from '../utils/key/createKey';
+import {createkey, isValidMnemonic} from '../utils/key/createKey';
 import {encryptKey, decryptKey} from '../utils/key/keystore';
 import { restoreFromKeyStore } from '../utils/account';
 
@@ -145,11 +145,52 @@ keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) {
 
     const res = createkey(data);
 
-    const xpub = res.xpub;
+    return res;
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.restoreFromKeystore = function( password, keystore) {
 
-    //Todo: /account/wallets api find if xpub exist in the blockcenter, yes restore, otherwise create new account
+    const walletImage = JSON.parse(keystore);
 
-    return res;
+    let keys, key;
+    if(walletImage.key_images && walletImage.key_images.xkeys ){
+        keys = walletImage.key_images.xkeys;
+    }
+
+    // match older version of backups keystore files
+    else if(walletImage.keys>0){
+        keys = walletImage.keys.map(keyItem => JSON.parse( keyItem.key ) );
+    }else{
+        key  = walletImage;
+    }
+
+    if(keys.length>1){
+        throw 'do not support multiple keystore imported.';
+    }
+    else if(keys.length === 1){
+        key = keys[0];
+    }
+
+    const result = decryptKey(key, password);
+
+    return result;
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.isValidMnemonic = function(mnemonic) {
+
+    return isValidMnemonic(mnemonic);
 };
 
 /**
index 661569b..9fa14f7 100644 (file)
@@ -1,5 +1,6 @@
 import {initDB, getDB} from '../db/db';
 import { restoreFromKeyStore } from '../utils/account';
+import accountsSDK from './accounts';
 
 function walletSDK(bytom) {
     this.http = bytom.serverHttp;
@@ -52,6 +53,12 @@ walletSDK.prototype.backup = function() {
     return retPromise;
 };
 
+walletSDK.prototype.list = function(pubkey) {
+    let net = this.bytom.net;
+    let pm = {pubkey};
+    return this.http.request('account/wallets', pm, net);
+};
+
 /**
  * Restore wallet.
  *
index 07f0c3e..d2526a4 100644 (file)
@@ -30,7 +30,7 @@ function createkey({
     return {alias: obj.alias, xpub: obj.xPub.toString('hex'), keystore: obj.keystore, mnemonic:obj.mnemonic};
 }
 
-function importKeyFromMnemonic(alias, password, mnemonic, language) {
+function isValidMnemonic(mnemonic, language){
     // checksum length = entropy length /32
     // mnemonic length = (entropy length + checksum length)/11
     let mnemArray = mnemonic.trim().split(' ');
@@ -43,6 +43,10 @@ function importKeyFromMnemonic(alias, password, mnemonic, language) {
     if (!bip39.validateMnemonic(mnemonic,  WORDLISTS[language])) {
         throw 'mnemonic is invalid';
     }
+}
+
+function importKeyFromMnemonic(alias, password, mnemonic, language) {
+    isValidMnemonic(mnemonic, language)
 
     return createKeyFromMnemonic(alias, password, mnemonic);
 }
@@ -72,7 +76,7 @@ function createChainKDKey(alias,password, language){
     let mnemonic = bip39.generateMnemonic(EntropyLength,undefined, WORDLISTS[language]);
 
     let object = createKeyFromMnemonic(alias, password, mnemonic);
-    object.mnemonic = mnemonic
+    object.mnemonic = mnemonic;
 
     return object;
 }
@@ -80,5 +84,6 @@ function createChainKDKey(alias,password, language){
 export {
     createkey,
     importKeyFromMnemonic,
-    createNewKey
+    createNewKey,
+    isValidMnemonic
 };