OSDN Git Service

update error code
[bytom/Bytom-JS-SDK.git] / src / sdk / keys.js
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';
7
8
9 function keysSDK() {
10 }
11
12 /**
13  * Create a new key.
14  *
15  * @param {String} alias - User specified, unique identifier.
16  * @param {String} password - User specified, key password.
17  */
18 keysSDK.prototype.createKey = function(alias, password) {
19     var normalizedAlias = alias.toLowerCase().trim();
20
21     let data = {};
22     data.alias = normalizedAlias;
23     data.password = password;
24     const res = createkey(data);
25
26     res.vault = this.encryptMnemonic(res.mnemonic, password, res.keystore);
27
28     return res;
29 };
30
31 /**
32  * Create a new key.
33  *
34  * @param {String} alias - User specified, unique identifier.
35  * @param {String} password - User specified, key password.
36  */
37 keysSDK.prototype.restoreFromMnemonic = function(alias, password, mnemonic) {
38     var normalizedAlias = alias.toLowerCase().trim();
39
40     let data = {};
41     data.alias = normalizedAlias;
42     data.password = password;
43     data.mnemonic = mnemonic;
44
45     const res = createkey(data);
46
47     res.vault = this.encryptMnemonic(mnemonic, password, res.keystore);
48     return res;
49 };
50
51 /**
52  * Create a new key.
53  *
54  * @param {String} alias - User specified, unique identifier.
55  * @param {String} password - User specified, key password.
56  */
57 keysSDK.prototype.restoreFromKeystore = function( password, keystore) {
58
59     const result = decryptKey(keystore, password);
60     result.xpub = result.xPub.toString('hex');
61     delete result['xPub'];
62
63     return result;
64 };
65
66
67 /**
68  * Create a new key.
69  *
70  * @param {String} alias - User specified, unique identifier.
71  * @param {String} password - User specified, key password.
72  */
73 keysSDK.prototype.encryptMnemonic = function(mnemonic, password, keystore) {
74
75     const result = decryptKey(keystore, password);
76     const xprv = result.xPrv.toString('hex');
77
78     const ciphertext = CryptoJS.AES.encrypt(mnemonic, xprv);
79
80     return ciphertext.toString();
81 };
82
83 /**
84  * Create a new key.
85  *
86  * @param {String} alias - User specified, unique identifier.
87  * @param {String} password - User specified, key password.
88  */
89 keysSDK.prototype.decryptMnemonic = function(ciphertext, password, keystore) {
90
91     const result = decryptKey(keystore, password);
92     const xprv = result.xPrv.toString('hex');
93
94
95     const bytes = CryptoJS.AES.decrypt(ciphertext, xprv);
96     const plaintext = bytes.toString(CryptoJS.enc.Utf8);
97
98     return plaintext;
99 };
100
101
102
103 /**
104  * Create a new key.
105  *
106  * @param {String} keystore - User specified, unique identifier.
107  */
108 keysSDK.prototype.isValidKeystore = function(  keystore ) {
109
110     const walletImage = camelize(JSON.parse(keystore));
111
112     let keys, key;
113     if(walletImage.keyImages && walletImage.keyImages.xkeys ){
114         keys = walletImage.keyImages.xkeys;
115     }
116
117     // match older version of backups keystore files
118     else if(walletImage['accounts-server']){
119         keys = walletImage.keys.map(keyItem => JSON.parse( keyItem.key ) );
120     }else{
121         key  = walletImage;
122     }
123
124     if(keys){
125         if(keys.length>1){
126             throw new Error('do not support multiple keystore imported.', 'BTM3004');
127         }
128         else if(keys.length === 1){
129             key = keys[0];
130         }
131     }
132
133     return key;
134 };
135
136 /**
137  * Create a new key.
138  *
139  * @param {String} alias - User specified, unique identifier.
140  * @param {String} password - User specified, key password.
141  */
142 keysSDK.prototype.isValidMnemonic = function(mnemonic) {
143
144     return isValidMnemonic(mnemonic);
145 };
146
147 /**
148  * Create a new key.
149  *
150  * @param {String} alias - User specified, unique identifier.
151  * @param {String} password - User specified, key password.
152  */
153 keysSDK.prototype.verifyPassword = function(keystore, password) {
154     try{
155         decryptKey(keystore, password);
156     }catch (e){
157         return false;
158     }
159     return true;
160 };
161
162 /**
163  * Sign Message.
164  *
165  * @param {String} message - message.
166  * @param {String} password - password.
167  * @param {Object} address - address.
168  */
169 keysSDK.prototype.signMessageJs = function(message, password, keystore) {
170     return signMJs(message, password, keystore);
171 };
172
173 keysSDK.prototype.signMessageJsPromise = function(message, password, keystore) {
174     let retPromise = new Promise((resolve, reject) => {
175         try{
176             let result = this.signMessageJs(message, password, keystore);
177             resolve(result);
178         }
179         catch(error) {
180             reject(error);
181         }
182     });
183
184     return retPromise;
185 };
186
187 export default keysSDK;