OSDN Git Service

update and test the key logic
[bytom/Bytom-JS-SDK.git] / src / sdk / keys.js
index e5ec7ee..dc68e6b 100644 (file)
@@ -1,5 +1,8 @@
-import {createKey, resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func';
+import { createKey ,resetKeyPassword, createPubkey, signMessage, signTransaction} from '../wasm/func';
 import {getDB} from '../db/db';
+import {createkey} from '../utils/key/createKey';
+import {encryptKey, decryptKey} from '../utils/key/keystore';
+import { restoreFromKeyStore } from '../utils/account';
 
 
 function keysSDK() {
@@ -112,6 +115,45 @@ keysSDK.prototype.list = function() {
 
 /**
  * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.createKey = function(alias, password) {
+    var normalizedAlias = alias.toLowerCase().trim();
+
+    let data = {};
+    data.alias = normalizedAlias;
+    data.password = password;
+    const res = createkey(data);
+    return res;
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) {
+    var normalizedAlias = alias.toLowerCase().trim();
+
+    let data = {};
+    data.alias = normalizedAlias;
+    data.password = password;
+    data.mnemonic = mnemonic;
+
+    const res = createkey(data);
+
+    const xpub = res.xpub;
+
+    //Todo: /account/wallets api find if xpub exist in the blockcenter, yes restore, otherwise create new account
+
+    return res;
+};
+
+/**
+ * Create a new key.
  * 
  * @param {String} alias - User specified, unique identifier.
  * @param {String} password - User specified, key password.
@@ -195,16 +237,16 @@ keysSDK.prototype.signMessage = function(message, password, address) {
         getDB().then(db => {
             let getRequest = db.transaction(['accounts-server'], 'readonly')
                 .objectStore('accounts-server')
-                .getAll()
+                .getAll();
 
             getRequest.onsuccess = function (e) {
-                const result = getRequest.result.filter(obj => obj.address === address)
+                const result = getRequest.result.filter(obj => (obj.address === address || obj.vpAddress === address));
                 if (result.length === 0) {
                     reject(new Error('not found address'));
                     return;
                 }
 
-                const rootXpub = result[0].rootXPub
+                const rootXpub = result[0].rootXPub;
                 let keyObject = db.transaction(['keys'], 'readonly')
                     .objectStore('keys')
                     .index('xpub')