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

Example of getting the WIF of a BIP32 extended private key, for import using bitcoind's "importprivkey" command. #1

Closed
ap-m opened this issue May 12, 2014 · 2 comments

Comments

@ap-m
Copy link

ap-m commented May 12, 2014

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!

@afk11
Copy link
Member

afk11 commented May 17, 2014

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.

@afk11
Copy link
Member

afk11 commented May 21, 2014

Now possible by specifying an argument:

public static function private_key_to_WIF($privKey, $compressed = FALSE, $address_version)

@afk11 afk11 closed this as completed May 21, 2014
afk11 pushed a commit that referenced this issue Mar 9, 2015
where applicable change the name of functions / vars to be txid or hash
@coingeek coingeek mentioned this issue Mar 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants