符合标准的EOS助记词生成私钥
最近要对接HD钱包,测试得到EOS的
eosjs-ecc 不符合标准的BIP44
m/44'/194'/0'/0/0
修正方法为
const hdkey = require('hdkey')
const wif = require('wif')
const ecc = require('eosjs-ecc')
const bip39 = require('bip39')
const mnemonic = 'real flame win provide layer trigger soda erode upset rate beef wrist fame design merit'
const seed = bip39.mnemonicToSeedSync(mnemonic)
const master = hdkey.fromMasterSeed(Buffer(seed, 'hex'))
const node = master.derive("m/44'/194'/0'/0/0")
console.log("publicKey: "+ecc.PublicKey(node._publicKey).toString())
console.log("privateKey: "+wif.encode(128, node._privateKey, false))
根据测试例子,修改eosjs-ecc支持扩展方法
package.json 增加新依赖
npm i bip39
npm i hdkey
npm i wif
修改api_common.js 增加bip44参数
默认false兼容之前版本
seedPrivate: (seed, bip44 = false) => PrivateKey.fromSeed(seed, bip44).toString(),
修改 key_private.js
增加新依赖
const hdkey = require('hdkey')
const WIFReturn = require('wif')
const bip39 = require('bip39')
修改fromSeed支持新实现
PrivateKey.fromSeed = function(seed, bip44) { // generate_private_key
if (!(typeof seed === 'string')) {
throw new Error('seed must be of type string');
}
if(bip44) {
const seedString = bip39.mnemonicToSeedSync(seed)
const master = hdkey.fromMasterSeed(Buffer(seedString, 'hex'))
const node = master.derive("m/44'/194'/0'/0/0")
return WIFReturn.encode(128, node._privateKey, false)
}
return PrivateKey.fromBuffer(hash.sha256(seed));
}
测试
测试助记词
real flame win provide layer trigger soda erode upset rate beef wrist fame design merit
原版eosjs-ecc
<script src="./dist-web/eosjs-ecc.js"></script>
<script>
(async () => {
let wif = eosjs_ecc.seedPrivate('real flame win provide layer trigger soda erode upset rate beef wrist fame design merit');
console.log(wif + '\n');
let pubKey = eosjs_ecc.privateToPublic(wif);
console.log(pubKey + '\n');
})();
</script>
生成结果为
5JX94izH6NMZkGqq9VvrmSTWux28HKRyns5mBwujzB9p48XSgNQ
EOS6j4E5ksFkDBAP32XXYseTaUkGqBKqPzYjcwUVeBV4JY8UbS1N5
使用修改后得标准BIP44
<script src="./dist-web/eosjs-ecc.js"></script>
<script>
(async () => {
let wif = eosjs_ecc.seedPrivate('real flame win provide layer trigger soda erode upset rate beef wrist fame design merit',true);
console.log(wif + '\n');
let pubKey = eosjs_ecc.privateToPublic(wif);
console.log(pubKey + '\n');
})();
</script>
结果为
5KX4T16FtxG9LvRJukA31TP9BKq3jYve3xQ3Px3ui8mzuJ7nUYE
EOS61oRAVkx1rqPM8mEsBZxPAFAa9Nm6kLa7mQs6mRKTsRTFQaad7
参考
https://github.com/satoshilabs/slips/blob/master/slip-0044.md
https://iancoleman.io/bip39/
备注
eosjs编译时需要先
npm run build
不能直接
npm run build_browser
如果测试自定义得bip44中得 coin type,可以在https://iancoleman.io/bip39/ 中得Derivation Path 选项中,选择BIP32,并去掉最后一层,例如,自定义coin type为9527
m/44'/9527'/0'/0
当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »