Example of getting the WIF of a BIP32 extended private key, for import using bitcoind's "importprivkey" command.
ap-m opened this issue · 2 comments
Hello,
I've been experimenting with getting the WIF encoding of a BIP32 extended private key, so I can import it into the bitcoind wallet using the "importprivkey" command (while using the Bitcoin testnet).
I simplified the existing test_bip32.php script, and had it emit the address and WIF of the master private key using BitcoinLib::private_key_to_WIF():
<?php
require_once(dirname(__FILE__).'/../BitcoinLib.php');
require_once(dirname(__FILE__).'/..//BIP32.php');
$master = BIP32::master_key('fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542', 'bitcoin', true);
echo "Chain m\n";
echo " ext priv:\n ".$master[0]."\n";
$public = BIP32::extended_private_to_public($master);
echo " ext pub:\n ".$public[0]."\n";
echo " address (priv):\n ".BIP32::key_to_address($master[0])."\n";
echo " address (pub):\n ".BIP32::key_to_address($public[0])."\n";
echo " ext wif:\n ".BitcoinLib::private_key_to_WIF(BIP32::import($master[0])['key'], '6f')."\n";
Running the script prints:
Chain m
ext priv:
tprv8ZgxMBicQKsPdqC56nGKYsarqYsgrSm33vCswnuMLFCk3gP7DFW5nPFExzSe7FGAzkbAFrxtXoQEe8vaX471tU3dsUUC7PNpYLGuzb2agmj
ext pub:
tpubD6NzVbkrYhZ4XJDrzRvuxHEyQaPd1mwwdDofEJwekX18tAdsqeKfxss79AJzg1431FybXg5rfpTrJF4iAhyR7RubberdzEQXiRmXGADH2eA
address (priv):
mxkmFi1a9MhPPcKGWqHnY6A8NayFMKSKvr
address (pub):
mxkmFi1a9MhPPcKGWqHnY6A8NayFMKSKvr
ext wif:
929xKZ1UYiiw7iHXQeWL9PVA6YDRkesRnMjaMxL3FkMR4M7Lr3h
I then tried importing the private key using bitcoind's "importprivkey" command:
$ bitcoind -testnet importprivkey 929xKZ1UYiiw7iHXQeWL9PVA6YDRkesRnMjaMxL3FkMR4M7Lr3h 20140512 true
It appeared to complete okay, with somewhat of a delay due to the rescan.
Then I tried to dump that imported key using the "dumpprivkey" command, but it gave an error like below:
$ bitcoind -testnet dumpprivkey mxkmFi1a9MhPPcKGWqHnY6A8NayFMKSKvr
error: {"code":-4,"message":"Private key for address mxkmFi1a9MhPPcKGWqHnY6A8NayFMKSKvr is not known"}
I'm new to Bitcoin and this library, so I'm not sure if this is a bug, or if it's just me generating the address or WIF incorrectly.
If this isn't a bug, would it be possible to provide some sample code showing how to get the address and WIF properly?
This was with commit 9eab00f, under OS X 10.9.2, with bitcoind v0.9.99.0-b733288-beta (built from source).
Thanks!
You're very close; BIP32's specification says all public keys are in compressed format, meaning that's what is hashed. To create the WIF that indicates you want to use the compressed address, put '01' at the end! That's it; the WIF this yields is the right address as per brainwallet.
BitcoinLib::private_key_to_WIF(BIP32::import($master[0])['key'].'01', '6f')."\n";
I'll add an argument to that function and mention it around the documentation to clear this up.
Now possible by specifying an argument:
bitcoin-lib-php/src/BitcoinLib.php
Line 351 in 20daa7a