Esempio n. 1
0
 public function testChildKeyDerivationOne()
 {
     $test_vectors = [0 => ['master' => '000102030405060708090a0b0c0d0e0f', 'ckd' => [0 => ['child' => "0'", 'priv' => 'xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7', 'pub' => 'xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw'], 1 => ['child' => '1', 'priv' => 'xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs', 'pub' => 'xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ'], 2 => ['child' => "2'", 'priv' => 'xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM', 'pub' => 'xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5'], 3 => ['child' => '2', 'priv' => 'xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334', 'pub' => 'xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV'], 4 => ['child' => '1000000000', 'priv' => 'xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76', 'pub' => 'xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy']]], 1 => ['master' => 'fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542', 'ckd' => [0 => ['child' => "0", 'priv' => 'xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt', 'pub' => 'xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH'], 1 => ['child' => "2147483647'", 'priv' => 'xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9', 'pub' => 'xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a'], 2 => ['child' => "1", 'priv' => 'xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef', 'pub' => 'xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon'], 3 => ['child' => "2147483646'", 'priv' => 'xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc', 'pub' => 'xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL'], 4 => ['child' => '2', 'priv' => 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j', 'pub' => 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt']]]];
     foreach ($test_vectors as $test => $vector) {
         $master = BIP32::master_key($vector['master']);
         $key = $master;
         foreach ($vector['ckd'] as $test_array) {
             $this->setup();
             $key = $this->bip32->build_key($key, $test_array['child']);
             $this->assertEquals($key[0], $test_array['priv']);
             $pub = $this->bip32->extended_private_to_public($key);
             $this->assertEquals($pub[0], $test_array['pub']);
             $this->tearDown();
         }
     }
 }
 /**
  * create a new key;
  *  1) a BIP39 mnemonic
  *  2) a seed from that mnemonic with the password
  *  3) a private key from that seed
  *
  * @param string    $passphrase             the password to use in the BIP39 creation of the seed
  * @param string    $forceEntropy           forced entropy instead of random entropy for testing purposes
  * @return array
  */
 protected function generateNewSeed($passphrase = "", $forceEntropy = null)
 {
     // generate master seed, retry if the generated private key isn't valid (FALSE is returned)
     do {
         $mnemonic = $this->generateNewMnemonic($forceEntropy);
         $seed = BIP39::mnemonicToSeedHex($mnemonic, $passphrase);
         $key = BIP32::master_key($seed, $this->network, $this->testnet);
     } while (!$key);
     return [$mnemonic, $seed, $key];
 }
 /**
  * @param                                $primaryMnemonic
  * @param                                $primaryPassphrase
  * @param                                $backupMnemonic
  * @param array                          $blocktrailPublicKeys
  * @param BlockchainDataServiceInterface $bitcoinClient
  * @param string                         $network
  * @param bool                           $testnet
  * @throws \Exception
  */
 public function __construct($primaryMnemonic, $primaryPassphrase, $backupMnemonic, array $blocktrailPublicKeys, BlockchainDataServiceInterface $bitcoinClient, $network = 'btc', $testnet = false)
 {
     // normalize network and set bitcoinlib to the right magic-bytes
     list($this->network, $this->testnet) = $this->normalizeNetwork($network, $testnet);
     BitcoinLib::setMagicByteDefaults($this->network . ($this->testnet ? '-testnet' : ''));
     //create BIP32 keys for the Blocktrail public keys
     foreach ($blocktrailPublicKeys as $blocktrailKey) {
         $this->blocktrailPublicKeys[$blocktrailKey['keyIndex']] = BIP32Key::create($blocktrailKey['pubkey'], $blocktrailKey['path']);
     }
     //set the unspent output finder, using the given bitcoin data service provider
     $this->bitcoinClient = $bitcoinClient;
     $this->utxoFinder = new UnspentOutputFinder($this->bitcoinClient);
     // cleanup copy paste errors from mnemonics
     $primaryMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($primaryMnemonic))));
     $backupMnemonic = str_replace("  ", " ", str_replace("\r\n", " ", str_replace("\n", " ", trim($backupMnemonic))));
     // convert the primary and backup mnemonics to seeds (using BIP39), then create private keys (using BIP32)
     $primarySeed = BIP39::mnemonicToSeedHex($primaryMnemonic, $primaryPassphrase);
     $backupSeed = BIP39::mnemonicToSeedHex($backupMnemonic, "");
     $this->primaryPrivateKey = BIP32Key::create(BIP32::master_key($primarySeed, $this->network, $this->testnet));
     $this->backupPrivateKey = BIP32Key::create(BIP32::master_key($backupSeed, $this->network, $this->testnet));
 }
Esempio n. 4
0
 public function testNewBlankWithoutMnemonicsWallet()
 {
     $client = $this->setupBlocktrailSDK();
     $identifier = $this->getRandomTestIdentifier();
     $primaryPrivateKey = BIP32::master_key(BIP39::mnemonicToSeedHex(BIP39::entropyToMnemonic(BIP39::generateEntropy(512)), "password"), 'bitcoin', true);
     $backupPublicKey = BIP32::extended_private_to_public(BIP32::master_key(BIP39::mnemonicToSeedHex(BIP39::entropyToMnemonic(BIP39::generateEntropy(512)), "password"), 'bitcoin', true));
     /**
      * @var $wallet \Blocktrail\SDK\Wallet
      */
     $e = null;
     try {
         $wallet = $client->initWallet(["identifier" => $identifier]);
     } catch (ObjectNotFound $e) {
         list($wallet, $primaryMnemonic, $backupMnemonic, $blocktrailPublicKeys) = $client->createNewWallet(["identifier" => $identifier, "primary_private_key" => $primaryPrivateKey, "backup_public_key" => $backupPublicKey, "key_index" => 9999]);
     }
     $this->assertTrue(!!$e, "New wallet with ID [{$identifier}] already exists...");
     $wallet = $client->initWallet(["identifier" => $identifier, "primary_private_key" => $primaryPrivateKey]);
     $this->wallets[] = $wallet;
     // store for cleanup
     $this->assertEquals(0, $wallet->getBalance()[0]);
     $e = null;
     try {
         $wallet->pay(["2N6Fg6T74Fcv1JQ8FkPJMs8mYmbm9kitTxy" => BlocktrailSDK::toSatoshi(0.001)]);
     } catch (\Exception $e) {
     }
     $this->assertTrue(!!$e, "Wallet without balance is able to pay...");
 }
<?php

use BitWasp\BitcoinLib\BIP32;
use BitWasp\BitcoinLib\BitcoinLib;
use BitWasp\BitcoinLib\RawTransaction;
require_once __DIR__ . '/../vendor/autoload.php';
// Fixed seed and derivation to test with
$seed = '41414141414141414141414141414141414141';
$def = "0'/0";
// Create master key from seed
$master = BIP32::master_key($seed);
echo "\nMaster key\n m           : {$master[0]} \n";
// Create derived key from master key + derivation
$key = BIP32::build_key($master, $def);
// Display private extended key and the address that's derived from it.
echo "Generated key: note that all depth=1 keys are hardened. \n {$key[1]}        : {$key[0]}\n";
echo "             : " . BIP32::key_to_address($key[0]) . "\n";
// Convert the extended private key to the public key, and display the address that's derived from it.
$pub = BIP32::extended_private_to_public($key);
echo "Public key\n {$pub[1]}        : {$pub[0]}\n";
echo "             : " . BIP32::key_to_address($pub[0]) . "\n";
/////////////////////////////
// We're gonna spent the first txout from this tx:
//  https://www.blocktrail.com/BTC/tx/4a2231e13182cdb64fa2f9aae38fca46549891e9dc15e8aaf484d82fc6e0a1d8
// Set up inputs here
$inputs = array(array('txid' => '4a2231e13182cdb64fa2f9aae38fca46549891e9dc15e8aaf484d82fc6e0a1d8', 'vout' => 0));
// Set up outputs here
$outputs = array('1KuE17Fbcdsn3Ns5T9Wzi1epurRnKC9qVr' => BitcoinLib::toSatoshi(0.0004));
////////////////////////////
// Parameters for signing.
// Create JSON inputs parameter
Esempio n. 6
0
<?php

use BitWasp\BitcoinLib\BIP32;
require_once __DIR__ . '/../vendor/autoload.php';
$master = BIP32::master_key(bin2hex(mcrypt_create_iv(64, \MCRYPT_DEV_URANDOM)));
// Load a 128 bit key, and convert this to extended key format.
//$master = BIP32::master_key('41414141414141414141414141414141414141');
$def = "0'/0";
echo "\nMaster key\n m           : {$master[0]} \n";
$key = BIP32::build_key($master, $def);
// Define what derivation you wish to calculate.
// Display private extended key and the address that's derived from it.
echo "Generated key: note that all depth=1 keys are hardened. \n {$key[1]}        : {$key[0]}\n";
echo "             : " . BIP32::key_to_address($key[0]) . "\n";
// Convert the extended private key to the public key, and display the
// address that's derived from it.
$pub = BIP32::extended_private_to_public($key);
echo "Public key\n {$pub[1]}        : {$pub[0]}\n";
echo "             : " . BIP32::key_to_address($pub[0]) . "\n";
//$nextpub = BIP32::build_key($pub, '0');
//echo  "Child key\n";
//echo " {$nextpub[1]}      : {$nextpub[0]}\n";
Esempio n. 7
0
<?php

use BitWasp\BitcoinLib\BIP32;
require_once __DIR__ . '/../vendor/autoload.php';
// Load a 128 bit key, and convert this to extended key format.
$master = BIP32::master_key('41414141414141414141414141414141414141');
$def = "0'";
echo "\nMaster key\n m           : {$master[0]} \n";
// Define what derivation you wish to calculate.
$key = BIP32::build_key($master, $def);
// Build the extended key
// Display private extended key and the address that's derived from it.
echo "Generated key: note that all depth=1 keys are hardened. \n {$key[1]}        : {$key[0]}\n";
echo "             : " . BIP32::key_to_address($key[0]) . "\n";
// Convert the extended private key to the public key, and display the
// address that's derived from it.
$pub = BIP32::extended_private_to_public($key);
echo "Public key\n {$pub[1]}        : {$pub[0]}\n";
echo "             : " . BIP32::key_to_address($pub[0]) . "\n";
$nextpub = BIP32::build_key($pub, '0');
echo "Child key\n";
echo " {$nextpub[1]}      : {$nextpub[0]}\n";
Esempio n. 8
0
 /**
  * unlock wallet so it can be used for payments
  *
  * @param          $options ['primary_private_key' => key] OR ['passphrase' => pass]
  * @param callable $fn
  * @return bool
  * @throws \Exception
  */
 public function unlock($options, callable $fn = null)
 {
     // explode the wallet data
     $password = isset($options['passphrase']) ? $options['passphrase'] : (isset($options['password']) ? $options['password'] : null);
     $primaryMnemonic = $this->primaryMnemonic;
     $primaryPrivateKey = isset($options['primary_private_key']) ? $options['primary_private_key'] : null;
     if ($primaryMnemonic && $primaryPrivateKey) {
         throw new \InvalidArgumentException("Can't specify Primary Mnemonic and Primary PrivateKey");
     }
     if (!$primaryMnemonic && !$primaryPrivateKey) {
         throw new \InvalidArgumentException("Can't init wallet with Primary Mnemonic or Primary PrivateKey");
     }
     if ($primaryMnemonic && !$password) {
         throw new \InvalidArgumentException("Can't init wallet with Primary Mnemonic without a passphrase");
     }
     if ($primaryPrivateKey) {
         if (is_string($primaryPrivateKey)) {
             $primaryPrivateKey = [$primaryPrivateKey, "m"];
         }
     } else {
         // convert the mnemonic to a seed using BIP39 standard
         $primarySeed = BIP39::mnemonicToSeedHex($primaryMnemonic, $password);
         // create BIP32 private key from the seed
         $primaryPrivateKey = BIP32::master_key($primarySeed, $this->network, $this->testnet);
     }
     $this->primaryPrivateKey = BIP32Key::create($primaryPrivateKey);
     // create checksum (address) of the primary privatekey to compare to the stored checksum
     $checksum = BIP32::key_to_address($primaryPrivateKey[0]);
     if ($checksum != $this->checksum) {
         throw new \Exception("Checksum [{$checksum}] does not match [{$this->checksum}], most likely due to incorrect password");
     }
     $this->locked = false;
     // if the response suggests we should upgrade to a different blocktrail cosigning key then we should
     if (isset($data['upgrade_key_index'])) {
         $this->upgradeKeyIndex($data['upgrade_key_index']);
     }
     if ($fn) {
         $fn($this);
         $this->lock();
     }
 }
<?php

use BitWasp\BitcoinLib\BIP32;
use BitWasp\BitcoinLib\RawTransaction;
require_once __DIR__ . '/../vendor/autoload.php';
echo "Lets start off by generating a wallet for each of the 'users'.\n";
echo "This will be stored on their machine.\n";
$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";
for ($i = 0; $i < 3; $i++) {
    $bip32key[0] = BIP32::build_key($pub[0], "0/{$i}");
    $bip32key[1] = BIP32::build_key($pub[1], "0/{$i}");
$key = BIP32::build_key($key, "2'");
echo "    ext priv:\n    " . $key[0] . "\n";
$public = BIP32::extended_private_to_public($key);
echo "    ext pub: \n    " . $public[0] . "\n";
echo "Chain m/0h/1/2h/2\n";
$key = BIP32::build_key($key, "2");
echo "    ext priv:\n    " . $key[0] . "\n";
$public = BIP32::extended_private_to_public($key);
echo "    ext pub: \n    " . $public[0] . "\n";
echo "Chain m/0h/1/2h/2/1000000000\n";
$key = BIP32::build_key($key, "1000000000");
echo "    ext priv:\n    " . $key[0] . "\n";
$public = BIP32::extended_private_to_public($key);
echo "    ext pub: \n    " . $public[0] . "\n\n\n\n";
echo "test two\n";
$master = BIP32::master_key('fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542');
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 "Chain m/0\n";
$key = BIP32::build_key($master, '0');
echo "    ext priv:\n    " . $key[0] . "\n";
$public = BIP32::extended_private_to_public($key);
echo "    ext pub: \n    " . $public[0] . "\n";
echo "Chain m/0/2147483647'\n";
$key = BIP32::build_key($key, "2147483647'");
echo "    ext priv:\n    " . $key[0] . "\n";
$public = BIP32::extended_private_to_public($key);
echo "    ext pub: \n    " . $public[0] . "\n";
echo "Chain m/0/2147483647'/1\n";
Esempio n. 11
0
 public function testGenerate()
 {
     for ($i = 0; $i < 100; $i++) {
         $entropy = BIP39::generateEntropy(128);
         $this->assertTrue(!!$entropy);
         $mnemonic = BIP39::entropyToMnemonic($entropy);
         $this->assertTrue(!!$entropy);
         $entropy2 = BIP39::mnemonicToEntropy($mnemonic);
         $this->assertTrue(!!$entropy2);
         $this->assertEquals($entropy, $entropy2);
         $bip32 = BIP32::master_key(BIP39::mnemonicToSeedHex($mnemonic, 'PASSWORD'));
         $this->assertTrue(!!$bip32);
     }
     for ($i = 0; $i < 100; $i++) {
         $entropy = BIP39::generateEntropy(256);
         $this->assertTrue(!!$entropy);
         $mnemonic = BIP39::entropyToMnemonic($entropy);
         $this->assertTrue(!!$entropy);
         $entropy2 = BIP39::mnemonicToEntropy($mnemonic);
         $this->assertTrue(!!$entropy2);
         $this->assertEquals($entropy, $entropy2);
         $bip32 = BIP32::master_key(BIP39::mnemonicToSeedHex($mnemonic, 'PASSWORD'));
         $this->assertTrue(!!$bip32);
     }
     for ($i = 0; $i < 100; $i++) {
         $entropy = BIP39::generateEntropy(512);
         $this->assertTrue(!!$entropy);
         $mnemonic = BIP39::entropyToMnemonic($entropy);
         $this->assertTrue(!!$entropy);
         $entropy2 = BIP39::mnemonicToEntropy($mnemonic);
         $this->assertTrue(!!$entropy2);
         $this->assertEquals($entropy, $entropy2);
         $bip32 = BIP32::master_key(BIP39::mnemonicToSeedHex($mnemonic, 'PASSWORD'));
         $this->assertTrue(!!$bip32);
     }
 }