-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, isValidMnemonic} from '../utils/key/createKey';
+import {encryptKey, decryptKey} from '../utils/key/keystore';
+import { restoreFromKeyStore } from '../utils/account';
+import { camelize } from '../utils/utils';
+import CryptoJS from 'crypto-js';
function keysSDK() {
let data = {};
data.alias = normalizedAlias;
- data.auth = password;
- return createKey(data).then((res) => {
- let jsonData = JSON.parse(res.data);
- let dbData = {
- key:res.data,
- xpub:jsonData.xpub,
- alias:alias,
- };
- return dbData;
- }).catch(error => {
- throw(error);
- });
+ data.password = password;
+ const res = createkey(data);
+
+ res.vault = this.encryptMnemonic(res.mnemonic, password, res.keystore);
+
+ 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);
+
+ res.vault = this.encryptMnemonic(mnemonic, password, res.keystore);
+ 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) {
+
+ const result = decryptKey(keystore, password);
+ result.xpub = result.xPub.toString('hex');
+ delete result['xPub'];
+
+ return result;
+};
+
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.encryptMnemonic = function(mnemonic, password, keystore) {
+
+ const result = decryptKey(keystore, password);
+ const xprv = result.xPrv.toString('hex');
+
+ const ciphertext = CryptoJS.AES.encrypt(mnemonic, xprv);
+
+ return ciphertext.toString();
+};
+
+/**
+ * Create a new key.
+ *
+ * @param {String} alias - User specified, unique identifier.
+ * @param {String} password - User specified, key password.
+ */
+keysSDK.prototype.decryptMnemonic = function(ciphertext, password, keystore) {
+
+ const result = decryptKey(keystore, password);
+ const xprv = result.xPrv.toString('hex');
+
+
+ const bytes = CryptoJS.AES.decrypt(ciphertext, xprv);
+ const plaintext = bytes.toString(CryptoJS.enc.Utf8);
+
+ return plaintext;
+};
+
+
+
+/**
+ * Create a new key.
+ *
+ * @param {String} keystore - User specified, unique identifier.
+ */
+keysSDK.prototype.isValidKeystore = function( keystore ) {
+
+ const walletImage = camelize(JSON.parse(keystore));
+
+ let keys, key;
+ if(walletImage.keyImages && walletImage.keyImages.xkeys ){
+ keys = walletImage.keyImages.xkeys;
+ }
+
+ // match older version of backups keystore files
+ else if(walletImage['accounts-server']){
+ keys = walletImage.keys.map(keyItem => JSON.parse( keyItem.key ) );
+ }else{
+ key = walletImage;
+ }
+
+ if(keys){
+ if(keys.length>1){
+ throw 'do not support multiple keystore imported.';
+ }
+ else if(keys.length === 1){
+ key = keys[0];
+ }
+ }
+
+ return key;
+};
+
+/**
+ * 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);
};
/**
* @param {String} password - password.
* @param {Object} address - address.
*/
-keysSDK.prototype.signMessage = function(message, password, address) {
- let retPromise = new Promise((resolve, reject) => {
- getDB().then(db => {
- let getRequest = db.transaction(['accounts-server'], 'readonly')
- .objectStore('accounts-server')
- .getAll();
-
- getRequest.onsuccess = function (e) {
- 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;
- let keyObject = db.transaction(['keys'], 'readonly')
- .objectStore('keys')
- .index('xpub')
- .get(rootXpub);
+keysSDK.prototype.signMessage = function(message, password, keystore) {
+ let data = {};
+ data.message = message;
+ data.password = password;
+ data.key = keystore;
+ return signMessage(data).then((res) => {
+ let jsonData = JSON.parse(res.data);
+ return (jsonData);
+ }).catch(error => {
+ throw (error);
+ });
- keyObject.onsuccess = function (e) {
- if (!e.target.result) {
- reject(new Error('not found xpub'));
- return;
- }
- let data = {};
- data.message = message;
- data.password = password;
- data.key = e.target.result.key;
- signMessage(data).then((res) => {
- let jsonData = JSON.parse(res.data);
- resolve(jsonData);
- }).catch(error => {
- reject(error);
- });
- };
- keyObject.onerror = function () {
- reject(getRequest.error);
- };
- };
- getRequest.onerror = function () {
- reject(getRequest.error);
- };
- }).catch(error => {
- reject(error);
- });
- });
- return retPromise;
};
export default keysSDK;
\ No newline at end of file