OSDN Git Service

update error code
[bytom/Bytom-JS-SDK.git] / src / utils / key / createKey.js
1 const bip39 = require('bip39');
2 const utils = require('./utils');
3 import Error from '../error'
4
5 const WORDLISTS = {
6     en: require('bip39/src/wordlists/english.json'),
7     zh: require('bip39/src/wordlists/chinese_simplified.json')
8 };
9
10 const keystore  = require('./keystore');
11
12 const EntropyLength = 128;
13 const LightScryptN = 1 << 12;
14 const LightScryptP = 6;
15
16 function createkey({
17     alias,
18     password,
19     mnemonic,
20     language
21 })  {
22     if(!language){
23         language = 'en';
24     }
25
26     if (mnemonic && mnemonic.length > 0 ){
27         return importKeyFromMnemonic(alias,password, mnemonic, language);
28     }
29
30     let obj = createNewKey(alias, password, language);
31     return {alias: obj.alias, xpub: obj.xPub.toString('hex'), keystore: obj.keystore, mnemonic:obj.mnemonic};
32 }
33
34 function isValidMnemonic(mnemonic, language){
35     // checksum length = entropy length /32
36     // mnemonic length = (entropy length + checksum length)/11
37     let mnemArray = mnemonic.trim().split(' ');
38     if (mnemArray.length != ((EntropyLength+EntropyLength/32)/11 )){
39         throw new Error('mnemonic length error', 'BTM3005');
40     }
41
42     // Pre validate that the mnemonic is well formed and only contains words that
43     // are present in the word list
44     if (!bip39.validateMnemonic(mnemonic,  WORDLISTS[language])) {
45         throw new Error('mnemonic is invalid', 'BTM3006');
46     }
47 }
48
49 function importKeyFromMnemonic(alias, password, mnemonic, language) {
50     isValidMnemonic(mnemonic, language)
51
52     const result = createKeyFromMnemonic(alias, password, mnemonic);
53     result.xpub = result.xPub;
54     delete result['xPub'];
55
56     return result;
57 }
58
59 function  createKeyFromMnemonic(alias,password, mnemonic) {
60     // Generate a Bip32 HD wallet for the mnemonic and a user supplied password
61     let seed = bip39.mnemonicToSeedSync(mnemonic, '');
62     let {xprv, xpub} = utils.newXKeys(seed);
63     let key = {
64         keyType: 'bytom_kd',
65         xPub:    xpub,
66         xPrv:    xprv.xprv,
67         alias
68     };
69     let _keystore = keystore.encryptKey( key, password, LightScryptN, LightScryptP);
70
71     return {xPub: xpub.toString('hex'), alias, keystore: _keystore};
72 }
73
74 function createNewKey(alias, password, language) {
75     let normalizedAlias = alias.trim().toLowerCase();
76     return createChainKDKey(normalizedAlias, password, language);
77 }
78
79 function createChainKDKey(alias,password, language){
80     // Generate a mnemonic for memorization or user-friendly seeds
81     let mnemonic = bip39.generateMnemonic(EntropyLength,undefined, WORDLISTS[language]);
82
83     let object = createKeyFromMnemonic(alias, password, mnemonic);
84     object.mnemonic = mnemonic;
85
86     return object;
87 }
88
89 export {
90     createkey,
91     importKeyFromMnemonic,
92     createNewKey,
93     isValidMnemonic
94 };