Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's default derive path for `getnewaddress`? #13302

Closed
nerddan opened this issue May 22, 2018 · 10 comments

Comments

@nerddan
Copy link

@nerddan nerddan commented May 22, 2018

From a same private key I got different derived address from bitcoin-cli and my own code. I am wondering what mistakes I made.

I created a HD wallet (under BIP49) and generated a derived address from this piece of code,

const btc = require('bitcoinjs-lib');
const btc_prv = btc.HDNode.fromSeedBuffer(seed).derivePath("m/49'/0'/0'");
const btc_pub = btc_prv.neutered();
const btc_prv0 = btc_prv.derivePath("0/0");
const btc_pub0 = btc_pub.derivePath("0/0");
const btc_add0 = btc.address.fromOutputScript(btc.script.scriptHash.output.encode(btc.crypto.hash160(btc.script.witnessPubKeyHash.output.encode(btc.crypto.hash160(btc_pub0.getPublicKeyBuffer())))));
console.log("private WIF  : "+btc_prv.keyPair.toWIF());
console.log("public key   : "+btc_pub.getPublicKeyBuffer().toString('hex'));
console.log("private WIF 0: "+btc_prv0.keyPair.toWIF());
console.log("public key  0: "+btc_pub0.getPublicKeyBuffer().toString('hex'));
console.log("address     0: "+btc_add0);

and got this output.

private WIF  : L4R...
public key   : 036124c83be4781cf7294a4d200c49d8161535f1fb5b4bdfef07d282d3219d81b4
private WIF 0: Kxf...
public key  0: 0259dcde34aa3cc5c4949bfdd8a35d189db40db3706b82f0879ba52b69be28f2f9
address     0: 36H1KVwD1BuLPFaNGDDnAwLP8vPNTak71R

Importing this private key into bitcoin-cli and generating derived key from

bitcoin-cli --version
bitcoin-cli importprivkey L4R... "" false
bitcoin-cli getnewaddress

, yields

Bitcoin Core RPC client version v0.16.0.0-g4b4d7eb255
38jrZ3ojUPvxbhG8pzqVSNae1MgmVVsMHd

. Why did I get different addresses? What is the algorithm behind getnewaddress? Can I generate address from custom derive path?

@nerddan nerddan changed the title Whats default derive path for `getnewaddress`? What's default derive path for `getnewaddress`? May 22, 2018
@MarcoFalke

This comment has been minimized.

Copy link
Member

@MarcoFalke MarcoFalke commented May 22, 2018

sethdseed will be in the next release of Bitcoin Core 0.17.0

@achow101

This comment has been minimized.

Copy link
Member

@achow101 achow101 commented May 22, 2018

You are using importprivkey entirely incorrectly. importprivkey does not set an HD seed for keys to be derived from. Yet you are trying to import the private key that you use as the HD seed. importprivkey directly imports a private key to be used, no derivation happens with the key and the only transformation is to generate its address. Furthermore, importprivkey does not effect getnewaddress at all. You will not get imported private keys in getnewaddress and importing a private key does not change how new addresses are created.

To see the address that you want, you have to import its private key, not the private key of its parents. So you should have imported btc_prv0. You can then see that it imported successfully by using getaddressesbyaccount (or getaddressesbylabel if using the master branch).

@nerddan

This comment has been minimized.

Copy link
Author

@nerddan nerddan commented May 23, 2018

@achow101 @ldenman thank you so much for replying! Is BIP32, BIP44, or BIP49 supported by bitcoin-cli?

@achow101

This comment has been minimized.

Copy link
Member

@achow101 achow101 commented May 23, 2018

Bitcoin Core supports BIP 32. Keys are derived with the derivation path m/0'/0'/i'

@nerddan nerddan closed this May 23, 2018
@nerddan

This comment has been minimized.

Copy link
Author

@nerddan nerddan commented May 23, 2018

@achow101 do you mean getnewaddress derives sub-keys with path m/0'/0'/i' and generates segwit addresses with BIP142? Where can I find corresponding official document?

@achow101

This comment has been minimized.

Copy link
Member

@achow101 achow101 commented May 23, 2018

do you mean getnewaddress derives sub-keys with path m/0'/0'/i'

Yes.

generates segwit addresses with BIP142?

No. Segwit addresses use BIP 173. BIP 142 is superceded by BIP 173.

Where can I find corresponding official document?

There is none.

@nerddan

This comment has been minimized.

Copy link
Author

@nerddan nerddan commented May 23, 2018

@achow101
BIP173 addresses start with bc1, while getnewaddress generates addresses start with 3.

@sipa

This comment has been minimized.

Copy link
Member

@sipa sipa commented May 23, 2018

Please take this discussion to https://bitcoin.stackexchange.com or some other forum. This is not an issue with Bitcoin Core's code.

@nerddan

This comment has been minimized.

Copy link
Author

@nerddan nerddan commented May 24, 2018

@sipa
I had no idea about that! Where is the source code of bitcoin-cli?

@sipa

This comment has been minimized.

Copy link
Member

@sipa sipa commented May 24, 2018

@nerddan You're on the repository with that source code. Now, please, take this question elsewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.