Skip to content

Instantly share code, notes, and snippets.

@dizda

dizda/bip32.php Secret

Created October 1, 2014 21:33
Show Gist options
  • Save dizda/15c3672d0df4b0d30f88 to your computer and use it in GitHub Desktop.
Save dizda/15c3672d0df4b0d30f88 to your computer and use it in GitHub Desktop.
<?php
$wallet[0] = BIP32::master_key('b861e093a58718e145b9791af35fb111');
$wallet[1] = BIP32::master_key('b861e093a58718e145b9791af35fb222');
$wallet[2] = BIP32::master_key('b861e093a58718e145b9791af35fb333');
print_r($wallet);
echo "Now we will generate a m/0' extended key. These will yield a private key\n";
$user[0] = BIP32::build_key($wallet[0][0], "3'");
$user[1] = BIP32::build_key($wallet[1][0], "23'");
$user[2] = BIP32::build_key($wallet[2][0], "9'");
print_r($user);
// As the previous is a private key, we should convert to the corresponding
// public key: M/0'
echo "As the previous is a private key, we should convert it to the corresponding\n";
echo "public key: M/0' \n";
$pub[0] = BIP32::extended_private_to_public($user[0]);
$pub[1] = BIP32::extended_private_to_public($user[1]);
$pub[2] = BIP32::extended_private_to_public($user[2]);
print_r($pub);
echo "This is the key you will ask your users for. For repeated transactions\n";
echo "BIP32 allows you to deterministically generate public keys, meaning less\n";
echo "effort for everyone involved\n\n";
echo "Now we can generate many multisignature addresses from what we have here: \n";
$multisig = null;
for($i = 0; $i < 1; $i++) {
// create derived public keys
$bip32key[0] = BIP32::build_key($pub[0], "0/{$i}");
$bip32key[1] = BIP32::build_key($pub[1], "0/{$i}");
$bip32key[2] = BIP32::build_key($pub[2], "0/{$i}");
// convert to proper public keys format
$pubkey[0] = BIP32::extract_public_key($bip32key[0]);
$pubkey[1] = BIP32::extract_public_key($bip32key[1]);
$pubkey[2] = BIP32::extract_public_key($bip32key[2]);
$output->writeln('<info>Public keys:</info>');
print_r($pubkey);
$multisig = RawTransaction::create_multisig(2, $pubkey);
print_r($multisig);
}
$inputs = [
[
'txid' => '2013db04623fa4dfd6b9cfec6cfd71dc23d7f94e612563578b7df6cfa4a3a3f7',
'vout' => 0,
'scriptPubKey' => 'a91471535703e6553bbe62343a13785eb9573cc41fc387',
'redeemScript' => '522103b8c960a5342172c109512b7a2f9f6a06736aaf2ca5a7577e708a4a8984e2b2102103ee743226b7e9a9b691fef59ed10b6288093fdd031a78ac44d827846437b9ee892102b45219b966fcf4fee51ca83181535c4b6d7fff28b77e7ffbaf3f7d7a6cb788c353ae'
]
];
$outputs = [
'1Ms3F5X7wGgKHw88fNWjEE32tRDK1q56KU' => '0.0002'
];
$rawTransaction = RawTransaction::create($inputs, $outputs);
$output->writeln(sprintf('Transaction unsigned: <info>%s</info>', $rawTransaction));
$privateWIF = [];
foreach ($user as $index => $u) {
// get details of private key
$keyDetails = BIP32::import($u[0]);
// then convert it to WIF format
$prvKey = BitcoinLib::private_key_to_WIF($keyDetails['key'], false, $keyDetails['version']);
if (!BitcoinLib::validate_WIF($prvKey)) { throw new \Exception("Failed to validate WIF", 1); }
$privateWIF []= $prvKey;
$output->writeln(sprintf('Private key WIF format #%d: <info>%s</info>', $index, $prvKey));
}
echo json_encode($inputs).PHP_EOL;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment