diff --git a/index.js b/index.js index 2091fdb..2fd7c03 100644 --- a/index.js +++ b/index.js @@ -7,9 +7,9 @@ const uuid = require('uuid') const keccak = require('keccak') const eccrypto = require('eccrypto') // 用于加解密。不知道怎么用 crypto 本身的加解密。 const keyman = require('js-crypto-key-utils') // 转换原始密钥和 PER/DER 格式。 -const Secword = require('bitcore-mnemonic') // https://bitcore.io/api/mnemonic/ https://github.com/bitpay/bitcore-mnemonic -// const bip39 = require('bip39') // https://github.com/bitcoinjs/bip39 // 有更多语言,但不方便选择语言,也不能使用 pass -// const HDKey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或者用 bitcore-mnemonic 或者 ethers 里的相同功能 +// const BitcoreMnemonic = require('bitcore-mnemonic') // https://bitcore.io/api/mnemonic/ https://github.com/bitpay/bitcore-mnemonic // 打包成 app 里常有问题,试图访问 window 变量,无法生成 secword +const bip39 = require('bip39') // https://github.com/bitcoinjs/bip39 // 有更多语言,但不方便选择语言,也不能使用 pass +const HDKey = require('hdkey') // https://github.com/cryptocoinjs/hdkey // 或者用 bitcore-mnemonic 或者 ethers 里的相同功能 // const bitcorelib = require('bitcore-lib') // const secp256k1 = require('secp256k1') @@ -67,8 +67,15 @@ module.exports = { } , isSecword(secword){ + //// for bitcore-mnemonic. 注意,bitcore-mnemonic 对少于12词的会抛出异常,很蠢。 + // if (typeof secword==='string' && 12===secword.split(/ +/).length) + // return BitcoreMnemonic.isValid(secword) + // else + // return false + + //// for bip39. 注意,bip39对当前defaultWordlist之外其他语言的合法 mnemonic 也返回 false。所以不能直接 bip39.validateMnemonic(secword) if (typeof secword==='string' && 12===secword.split(/ +/).length) - return Secword.isValid(secword) + return true else return false } @@ -203,7 +210,7 @@ module.exports = { // 据测试, Purpose和CoinType都可以任意其他值,不必要如规范所示;' 引号可有可无,导致的密钥不一样; // Account 最大为 0x7FFFFFFF, Change/Index 最大均为 0xFFFFFFFF(=4294967295) // 但可以不断延伸下去:/xxx/xxx/xxx/xxx/... - if (Secword.isValid(secword)){ + if (this.isSecword(secword)){ option=option||{} option.coin=my.COIN_LIST.indexOf(option.coin)>=0?option.coin:my.COIN @@ -219,8 +226,8 @@ module.exports = { } }else { // 用 bip39 算法从 secword 到种子,再用 bip32 算法从种子到根私钥。这是比特币、以太坊的标准方式,结果一致。 -// let hdmaster=HDKey.fromMasterSeed(new Buffer(this.secword2seed(secword, option.pass), 'hex')) // 和 new Secword(secword).toHDPrivateKey 求出的公私钥一样! - let hdmaster=new Secword(secword).toHDPrivateKey(option.pass) // 和 ethers.HDNode.fromMnemonic(secword)的公私钥一样。而 ethers.HDNode.fromMnemonic(secword).derivePath("m/44'/60'/0'/0/0")的公私钥===ethers.Wallet.fromMnemonic(secword [,"m/44'/60'/0'/0/0"]) + let hdmaster=HDKey.fromMasterSeed(Buffer.from(this.secword2seed(secword, option.pass), 'hex')) // 和 new BitcoreMnemonic(secword).toHDPrivateKey 求出的公私钥一样! +// let hdmaster=new BitcoreMnemonic(secword).toHDPrivateKey(option.pass) // 和 ethers.HDNode.fromMnemonic(secword)的公私钥一样。而 ethers.HDNode.fromMnemonic(secword).derivePath("m/44'/60'/0'/0/0")的公私钥===ethers.Wallet.fromMnemonic(secword [,"m/44'/60'/0'/0/0"]) let key=hdmaster if (option.path==='master'){ key=hdmaster @@ -408,16 +415,25 @@ module.exports = { } , secword2seed(secword, pass) { // 遵循bip39的算法。和 ether.HDNode.mnemonic2Seed 结果一样,是64字节的种子。 - if (Secword.isValid(secword)) { // bip39.validateMnemonic(secword)) { - return new Secword(secword).toSeed(pass).toString('hex') // 结果一致于 bip39.mnemonicToSeedHex(secword) 或 ethers.HDNode.mnemonic2Seed(secword) + if (this.isSecword(secword)) { // bip39.validateMnemonic(secword)) { + return bip39.mnemonicToSeedSync(secword, pass).toString('hex') // 结果一致于 new BitcoreMnemonic(secword).toSeed(pass).toString('hex') 或 ethers.HDNode.mnemonic2Seed(secword) +// return new BitcoreMnemonic(secword).toSeed(pass).toString('hex') } return null } , - randomSecword(lang='ENGLISH'){ // Object.keys(Secword.Words) => [ 'CHINESE', 'ENGLISH', 'FRENCH', 'ITALIAN', 'JAPANESE', 'KOREAN', 'SPANISH' ] - let language = { zhCN: 'CHINESE', enUS: 'ENGLISH', frFR: 'FRENCH', itIT: 'ITALIAN', jaJP: 'JAPANESE', koKR: 'KOREAN', esES: 'SPANISH' }[lang] - || (Secword.Words.hasOwnProperty(lang.toUpperCase()) ? lang.toUpperCase() : 'ENGLISH') - return new Secword(Secword.Words[language]).phrase + randomSecword(lang='english'){ // Object.keys(BitcoreMnemonic.Words) => [ 'CHINESE', 'ENGLISH', 'FRENCH', 'ITALIAN', 'JAPANESE', 'KOREAN', 'SPANISH' ] + //// for BitcoreMnemonic + // let language = { zhCN: 'CHINESE', enUS: 'ENGLISH', frFR: 'FRENCH', itIT: 'ITALIAN', jaJP: 'JAPANESE', koKR: 'KOREAN', esES: 'SPANISH' }[lang] + // || (BitcoreMnemonic.Words.hasOwnProperty(lang.toUpperCase()) ? lang.toUpperCase() : 'ENGLISH') + // return new BitcoreMnemonic(BitcoreMnemonic.Words[language]).phrase + + // for bip39 + const langMap = { zhCN: 'chinese_simplified', zhTW: 'chinese_traditional', enUS: 'english', frFR: 'french', itIT: 'italian', jaJP: 'japanese', koKR: 'korean', esES: 'spanish' } + let language = langMap[lang] + || (typeof(lang)==='string' && Object.values(langMap).indexOf(lang.toLowerCase()>=0) ? lang.toLowerCase() : 'english') + bip39.setDefaultWordlist(language) + return bip39.generateMnemonic() } , randomSeckey(option){ // todo: 使用 crypto.randomBytes(size) diff --git a/package.json b/package.json index 640955b..68903b4 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,10 @@ "private": true, "dependencies": { "big-integer": "^1.6.48", - "bitcore-mnemonic": "^8.16.0", - "bs58check": "^2.1.1", + "bip39": "^3.0.2", + "bs58check": "^2.1.2", "eccrypto": "^1.1.3", + "hdkey": "^1.1.1", "js-crypto-key-utils": "^0.7.3", "keccak": "^2.1.0", "tweetnacl": "^1.0.3",