Author

Topic: Trouble importing keys (Read 1359 times)

sr. member
Activity: 412
Merit: 287
June 18, 2014, 02:16:43 PM
#4
Bitcoind doesn't support BIP32 keys yet. The reason bip32 keys look unfamiliar, is they serialize other stuff besides the private key. Chain code, address number, depth (how many parents to the master key), and the fingerprint of the parent..

So, from the private key above, using this library, you need to 'BIP32::import()' the extended key in order to access the 'key' parameter. This is the hex private key, so you need to call BitcoinLib::private_key_to_WIF($imported_bip32key['key'], '6f', TRUE) - 6f for testnet; TRUE to return a WIF for a compressed pubkey address.

You can import WIF keys into bitcoind then!

Code:
$bitcoinprivkey "tprv8dbLDXsfGU1PG9aHtCZKGybntES6SdhEwLWpsKCrsCieLX5x7nGGvwwaqTnyW7R8VyockuvAoFhseNM6NcUDgWoqQdHWAqt3ynRG5GwAYBZ";
$label'test import key';

$decoded_key BIP32::import($bitcoinprivkey);
$private_key_hex $decoded_key['key']; // hexadecimal
$wif BitcoinLib::private_key_to_WIF(); // base58 wallet import format
?>

Get importprivkey echo $bc->importprivkey ($bitcoinprivkey$label); ?>

If you do:

print_r(BIP32::import('tprv8baY42qkWkxUTXPRrkfNx1iEg3GxVJBeGRrbFWorwjao6a4KFtycUcY2Rjp5tXvGeQwvXH3s7KA HCKkueb8bjPnHFGZEfc3eYc6v437192G'));

You'll see all the data which is compacted into BIP32 keys, making them longer, but assisting with the deterministic algorithm used to generate them.
full member
Activity: 178
Merit: 100
June 17, 2014, 04:36:01 PM
#3
I see what you mean. I did dumpprivkey from the CL and get totally different results than what the BIP#@LIB.php is giving me.
Code:
bitcoind dumpprivkey mvth9xbuTtRVkzwziMET5EaXYFK3TbTE9A
cNVXuCivrzYDBstqzWuGJHzVjZuB56jMNxfrhmaYLsvF38CLyh2i

So I took the key that dumppriv key gave me and replaced it in the above code but it gave the same exact error (Invalid private key encoding). I realize I would be "importing" a key that was already there but I would expect a different error message?

So I have more than one problem - the BIP32 functions aren't giving me valid keys and, even if they were, it looks like my import code is wrong too.

Here is the code giving me the master key:

Code:
use BitWasp\BitcoinLib\BIP32;

require_once(__DIR__. '/bitcoin-lib-php-master/vendor/autoload.php');

// Load a 128 bit key, and convert this to extended key format.
$master = BIP32::master_key('741F3C7FDC93D');
$def = "0'";

echo "
\nMaster key\n m           : {$master[0]} \n";
Gives me this  "key" - tprv8dbLDXsfGU1PG9aHtCZKGybntES6SdhEwLWpsKCrsCieLX5x7nGGvwwaqTnyW7R8VyockuvAoFh seNM6NcUDgWoqQdHWAqt3ynRG5GwAYBZ

formed the same as the first one I tried -
legendary
Activity: 938
Merit: 1001
June 17, 2014, 03:49:50 PM
#2
er.. that key doesn't look valid
full member
Activity: 178
Merit: 100
June 17, 2014, 03:46:10 PM
#1
I  "built" keys and addresses using Bip32 in PHP and am now trying to import them into bitcoind.

I'm getting the following error:
Code:
in account callss func $bitcoinprivkey = tprv8baY42qkWkxUTXPRrkfNx1iEg3GxVJBeGRrbFWorwjao6a4KFtycUcY2Rjp5tXvGeQwvXH3s7KAHCKkueb8bjPnHFGZEfc3eYc6v437192G Fatal error: Uncaught exception 'Exception' with message 'Request error: Array ( [code] => -5 [message] => Invalid private key encoding ) ' in /var/www/includes/jsonRPCClient.class.php:186 Stack trace: #0 /var/www/includes/BitcoinAccount.class.php(115): jsonRPCClient->__call('importprivkey', Array) #1 /var/www/includes/BitcoinAccount.class.php(115): jsonRPCClient->importprivkey('tprv8baY42qkWkx...', 'test import key', true) #2 /var/www/test_bitcoin_rpc.php(46): BitcoinAccount->importprivkey('tprv8baY42qkWkx...', 'test import key') #3 {main} thrown in /var/www/includes/jsonRPCClient.class.php on line 186


And here is the code from var/www/includes/BitcoinAccount.class.php(115):

Code:
public function importprivkey ($bitcoinprivkey, $label)
{
echo 'in account callss func $bitcoinprivkey = ', $bitcoinprivkey;
  return $this->rpc->importprivkey ($bitcoinprivkey, $label, true);
 }


And here is where I am calling the function:

Code:
$bitcoinprivkey = "tprv8dbLDXsfGU1PG9aHtCZKGybntES6SdhEwLWpsKCrsCieLX5x7nGGvwwaqTnyW7R8VyockuvAoFhseNM6NcUDgWoqQdHWAqt3ynRG5GwAYBZ";
$label= 'test import key';
Get importprivkey echo $bc->importprivkey ($bitcoinprivkey$label); ?>



It sounds like I have to use some encoding function on the key but which one and how?

Thanks

[/code]
Jump to: