1 import {createkey, isValidMnemonic} from '../utils/key/createKey';
2 import { decryptKey} from '../utils/key/keystore';
3 import { camelize } from '../utils/utils';
4 import CryptoJS from 'crypto-js';
5 import {signMessage as signMJs} from '../utils/transaction/signMessage';
6 import Error from '../utils/error';
15 * @param {String} alias - User specified, unique identifier.
16 * @param {String} password - User specified, key password.
18 keysSDK.prototype.createKey = function(alias, password) {
19 var normalizedAlias = alias.toLowerCase().trim();
22 data.alias = normalizedAlias;
23 data.password = password;
24 const res = createkey(data);
26 res.vault = this.encryptMnemonic(res.mnemonic, password, res.keystore);
34 * @param {String} alias - User specified, unique identifier.
35 * @param {String} password - User specified, key password.
37 keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) {
38 var normalizedAlias = alias.toLowerCase().trim();
41 data.alias = normalizedAlias;
42 data.password = password;
43 data.mnemonic = mnemonic;
45 const res = createkey(data);
47 res.vault = this.encryptMnemonic(mnemonic, password, res.keystore);
54 * @param {String} alias - User specified, unique identifier.
55 * @param {String} password - User specified, key password.
57 keysSDK.prototype.restoreFromKeystore = function( password, keystore) {
59 const result = decryptKey(keystore, password);
60 result.xpub = result.xPub.toString('hex');
61 delete result['xPub'];
70 * @param {String} alias - User specified, unique identifier.
71 * @param {String} password - User specified, key password.
73 keysSDK.prototype.encryptMnemonic = function(mnemonic, password, keystore) {
75 const result = decryptKey(keystore, password);
76 const xprv = result.xPrv.toString('hex');
78 const ciphertext = CryptoJS.AES.encrypt(mnemonic, xprv);
80 return ciphertext.toString();
86 * @param {String} alias - User specified, unique identifier.
87 * @param {String} password - User specified, key password.
89 keysSDK.prototype.decryptMnemonic = function(ciphertext, password, keystore) {
91 const result = decryptKey(keystore, password);
92 const xprv = result.xPrv.toString('hex');
95 const bytes = CryptoJS.AES.decrypt(ciphertext, xprv);
96 const plaintext = bytes.toString(CryptoJS.enc.Utf8);
106 * @param {String} keystore - User specified, unique identifier.
108 keysSDK.prototype.isValidKeystore = function( keystore ) {
110 const walletImage = camelize(JSON.parse(keystore));
113 if(walletImage.keyImages && walletImage.keyImages.xkeys ){
114 keys = walletImage.keyImages.xkeys;
117 // match older version of backups keystore files
118 else if(walletImage['accounts-server']){
119 keys = walletImage.keys.map(keyItem => JSON.parse( keyItem.key ) );
126 throw new Error('do not support multiple keystore imported.', 'BTM3004');
128 else if(keys.length === 1){
139 * @param {String} alias - User specified, unique identifier.
140 * @param {String} password - User specified, key password.
142 keysSDK.prototype.isValidMnemonic = function(mnemonic) {
144 return isValidMnemonic(mnemonic);
150 * @param {String} alias - User specified, unique identifier.
151 * @param {String} password - User specified, key password.
153 keysSDK.prototype.verifyPassword = function(keystore, password) {
155 decryptKey(keystore, password);
165 * @param {String} message - message.
166 * @param {String} password - password.
167 * @param {Object} address - address.
169 keysSDK.prototype.signMessageJs = function(message, password, keystore) {
170 return signMJs(message, password, keystore);
173 keysSDK.prototype.signMessageJsPromise = function(message, password, keystore) {
174 let retPromise = new Promise((resolve, reject) => {
176 let result = this.signMessageJs(message, password, keystore);
187 export default keysSDK;