Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: redPanda on April 11, 2016, 07:19:05 PM



Title: BIP 32 - Find xprv from seed and passphrase (bitcore-mnemonic)
Post by: redPanda on April 11, 2016, 07:19:05 PM
from BIP 47:  https://github.com/trezor/python-mnemonic/blob/master/vectors.json
Code:
passphrase = "TREZOR";
entropy = "00000000000000000000000000000000";
code    = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
seed    = "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04";
xprv    = "xprv9s21ZrQH143K3h3fDYiay8mocZ3afhfULfb5GX8kCBdno77K4HiA15Tg23wpbeF1pLfs1c5SPmYHrEpTuuRhxMwvKDwqdKiGJS9XFKzUsAF";
from entropy and passphrase, I can find code, seed and xprv with bitcore-mnemonic (node.js):
Code:
var Mnemonic = require('bitcore-mnemonic');
mnemonic   = Mnemonic._entropy2mnemonic(new Buffer(entropy, 'hex'), Mnemonic.Words.ENGLISH);
customCode = new Mnemonic(mnemonic);
theSeed    = customCode.toSeed(passphrase);
xprv       = customCode.toHDPrivateKey(passphrase);

Now I would like to use "seed" in BIP 32 with passphrase and find xprv (and mnemonic?) with bitcore-mnemonic.
Is there a way to do that ?


Title: Re: BIP 32 - Find xprv from seed and passphrase (bitcore-mnemonic)
Post by: johoe on April 11, 2016, 07:58:33 PM
Look for bip 39.  This is from mnemonic words and passphrase to seed.

EDIT: I think I understand, you want to go the other direction?  It's not feasible to find mnemonic words and/or passphrase from an xprv key or seed. PBKDF2 is not invertible.

From seed to xprv is easy, just the last line of your code.  Well, not really but look at the function:

Code:
Mnemonic.prototype.toHDPrivateKey = function(passphrase, network) {
  var seed = this.toSeed(passphrase);
  return bitcore.HDPrivateKey.fromSeed(seed, network);
};

So the last line of that function gives you the xprv from seed.


Title: Re: BIP 32 - Find xprv from seed and passphrase (bitcore-mnemonic)
Post by: redPanda on April 12, 2016, 01:29:16 PM
You're right: I want to do exactly this: given seed and passphrase: get xpriv

from BIP32: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
Code:
Master key generation:
Generate a seed byte sequence S of a chosen length (between 128 and 512 bits; 256 bits is advised) from a (P)RNG.
Calculate I = HMAC-SHA512(Key = "Bitcoin seed", Data = S)
Split I into two 32-byte sequences, I_L and I_R.
Use parse256(I_L) as master secret key, and I_R as master chain code.
xpriv is essentially a serialization of the master secret key (I_L) and the master chain code (I_R).

So the function you gave me seems the right one BUT I have 2 problems:

1) if I do in node.js

var Mnemonic = require('bitcore-mnemonic');
var bitcore = require('bitcore');

I have the following error:
Code:
Error: More than one instance of bitcore found with versions: v0.13.4 and v0.13.14.
Please make sure to require bitcore and check that submodules do not also include their own bitcore dependency.
2) I don't understand the keyword "prototype" so I can't try your function.
Can you please indicate me how can I can use this function in my code


Title: Re: BIP 32 - Find xprv from seed and passphrase (bitcore-mnemonic)
Post by: johoe on April 12, 2016, 02:50:20 PM
Something like this should work:

Code:
var bitcore = require('bitcore-lib');
var xprv = bitcore.HDPrivateKey.fromSeed(seed);

I'm not familiar enough with javascript to know how to fix the bitcore version errors.