bcoin-org / bcoin

Javascript bitcoin library for node.js and browsers
https://bcoin.io
Other
3k stars 813 forks source link

Can bip45 paths be derived using bcoin's hd lib? #153

Closed ghost closed 7 years ago

ghost commented 7 years ago

I read at the wallet docs that bip45 is not supported but:

Would this code work and derive a correct bip45 shared xpub and sub-branches if I use some brute-forced path?

console.log('============= COSIGNER 1: ================================');

// OFFLINE DATA
var mne1 = bcoin.hd.Mnemonic({
    language: 'english',
    passphrase: 'satoshi'
});
console.log('---MNEMONIC----------\n' + JSON.stringify(mne1));
var seed1 = bcoin.crypto.pbkdf2(new Buffer(mne1.phrase), 'mnemonic' + 'foo', 2048, 64, 'sha512').toString('hex');
var master1 = new bcoin.hd.fromSeed(new Buffer(seed1, 'hex'));
var shkey1 = master1.derivePath('m/45\''); // shared with cosigners
var kr1 = bcoin.keyring(shkey1.derivePath('m/1/0/0')); // first receiving address for second cosigner

console.log('--- KEY DETAILS ---\n - master: ' + master1.xprivkey() + '\n - online shared branch: m/45\'/*\n - shared xpub: ' + shkey1.xpubkey() + '\n - seed: ' + seed1 + '\n - derived (m/45\'/1/0/0): ' + kr1.publicKey.toString("hex"));

var xpub = shkey1.xpubkey();
var masterpub = new bcoin.hd.fromBase58(xpub);
var rec1 = masterpub.derivePath('m/1/0/0');
var rec1kr = bcoin.keyring(rec1);

// CHECK
assert.equal(rec1kr.getAddress('base58'), kr1.getAddress('base58'));
chjj commented 7 years ago

bip45 is supported by the HD module, just not the wallet. There's even helper functions for it.

var key = HD.generate();
var purposeKey = key.derivePurpose45();

Also, you can do:

key = HD.PrivateKey.fromMnemonic(mnemonic);
// or
key = HD.PrivateKey.fromSeed(mnemonic.toSeed());

Instead of calling out to pbkdf2.

ghost commented 7 years ago

excellent.