Advertisement
maskekar

BIP-32/44

Aug 5th, 2018
1,271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.62 KB | None | 0 0
  1. <html>
  2. <?php
  3. require_once('vendor/autoload.php');
  4. use BitWasp\BitcoinLib\BitcoinLib;
  5. use BitcoinPHP\BitcoinECDSA\BitcoinECDSA;
  6. use Mdanter\Ecc\EccFactory;
  7. $bitcoinECDSA = new BitcoinECDSA();
  8.  
  9. // Start BIP 39 Seed
  10.  
  11. $password = "symptom display melt eight where universe prison vacuum convince garbage glance fork";
  12. $iterations = 2048;
  13. $salt = "mnemonic";
  14. $hash = hash_pbkdf2("sha512", $password, $salt, $iterations);
  15. echo $hash."<br>";
  16.  
  17. // End BIP 39 Seed
  18.  
  19. // Start BIP 32 Root Key (m)
  20.  
  21. $hashdata = pack("H*" , $hash);
  22. $chaindata = "Bitcoin seed";
  23. $hash_m = hash_hmac("sha512" , $hashdata, $chaindata);
  24. $hash_m_l = substr($hash_m, 0, 64);
  25. $hash_m_r = substr($hash_m, 64, 64);
  26.  
  27. $data_m = array(
  28.             'network' => '0488ade4',
  29.             'depth' => '00',
  30.             'fingerprint' => '00000000',
  31.             'i' => '00000000',
  32.             'chain_code' => $hash_m_r,
  33.             'key' => '00'.$hash_m_l,
  34.         );
  35.  
  36. $xprivkey_m = implode($data_m);
  37. $checksum_m = hash('sha256', pack("H*", hash('sha256', pack("H*", $xprivkey_m))));
  38. $checksum_m = substr($checksum_m, 0, 8);
  39. $xprivkey_m = $xprivkey_m . $checksum_m;
  40. $base58 = new StephenHill\Base58();
  41. echo $base58->encode(pack("H*", $xprivkey_m))."<br>";
  42.  
  43. // End BIP 32 Root Key (m)
  44.  
  45. // Start m/44' derivation
  46.  
  47. $hashdata = pack("H*", "00" . $hash_m_l . "8000002C");
  48. $chaindata = pack("H*", $hash_m_r);
  49. $hash_m_44 = hash_hmac("sha512" , $hashdata, $chaindata);
  50. $hash_m_44_l = substr($hash_m_44, 0, 64);
  51. $hash_m_44_r = substr($hash_m_44, 64, 64);
  52.  
  53. $bitcoinECDSA->setPrivateKey($hash_m_l);
  54. $finger_m = $bitcoinECDSA->getPubKey();
  55. $finger_m = hash('ripemd160', pack("H*", hash('sha256', pack("H*", $finger_m))));
  56. $finger_m = substr($finger_m, 0, 8);
  57.  
  58. $math = EccFactory::getAdapter();
  59. $g = EccFactory::getSecgCurves($math)->generator256k1();
  60. $n = $g->getOrder();
  61. $Il_dec = $math->hexDec($hash_m_44_l);
  62. $private_key_dec = $math->hexDec($hash_m_l);
  63. $key_dec = $math->mod($math->add($Il_dec, $private_key_dec), $n);
  64. $keym44 = str_pad(BitcoinLib::hex_encode($key_dec), 64, '0', STR_PAD_LEFT);
  65.  
  66. $data_m_44 = array(
  67.             'network' => '0488ade4',
  68.             'depth' => '01',
  69.             'fingerprint' => $finger_m,
  70.             'i' => '8000002C',
  71.             'chain_code' => $hash_m_44_r,
  72.             'key' => '00'.$keym44,
  73.         );
  74.  
  75. $xprivkey_m_44 = implode($data_m_44);
  76. $checksum_m_44 = hash('sha256', pack("H*", hash('sha256', pack("H*", $xprivkey_m_44))));
  77. $checksum_m_44 = substr($checksum_m_44, 0, 8);
  78. $xprivkey_m_44 = $xprivkey_m_44 . $checksum_m_44;
  79. $base58 = new StephenHill\Base58();
  80. echo $base58->encode(pack("H*", $xprivkey_m_44))."<br>";
  81.  
  82. // End m/44' derivation
  83.  
  84. // Start m/44'/0' derivation
  85.  
  86. $hashdata = pack("H*", "00" . $keym44 . "80000000");
  87. $chaindata = pack("H*", $hash_m_44_r);
  88. $hash_m_44_0 = hash_hmac("sha512" , $hashdata, $chaindata);
  89. $hash_m_44_0_l = substr($hash_m_44_0, 0, 64);
  90. $hash_m_44_0_r = substr($hash_m_44_0, 64, 64);
  91.  
  92. $bitcoinECDSA->setPrivateKey($keym44);
  93. $finger_m_44 = $bitcoinECDSA->getPubKey();
  94. $finger_m_44 = hash('ripemd160', pack("H*", hash('sha256', pack("H*", $finger_m_44))));
  95. $finger_m_44 = substr($finger_m_44, 0, 8);
  96.  
  97. $math = EccFactory::getAdapter();
  98. $g = EccFactory::getSecgCurves($math)->generator256k1();
  99. $n = $g->getOrder();
  100. $Il_dec = $math->hexDec($hash_m_44_0_l);
  101. $private_key_dec = $math->hexDec($keym44);
  102. $key_dec = $math->mod($math->add($Il_dec, $private_key_dec), $n);
  103. $keym440 = str_pad(BitcoinLib::hex_encode($key_dec), 64, '0', STR_PAD_LEFT);
  104.  
  105. $data_m_44_0 = array(
  106.             'network' => '0488ade4',
  107.             'depth' => '02',
  108.             'fingerprint' => $finger_m_44,
  109.             'i' => '80000000',
  110.             'chain_code' => $hash_m_44_0_r,
  111.             'key' => '00'.$keym440,
  112.         );
  113.  
  114. $xprivkey_m_44_0 = implode($data_m_44_0);
  115. $checksum_m_44_0 = hash('sha256', pack("H*", hash('sha256', pack("H*", $xprivkey_m_44_0))));
  116. $checksum_m_44_0 = substr($checksum_m_44_0, 0, 8);
  117. $xprivkey_m_44_0 = $xprivkey_m_44_0 . $checksum_m_44_0;
  118. $base58 = new StephenHill\Base58();
  119. echo $base58->encode(pack("H*", $xprivkey_m_44_0))."<br>";
  120.  
  121. // End m/44'/0' derivation
  122.  
  123. // Start m/44'/0'/0' derivation
  124.  
  125. $hashdata = pack("H*", "00" . $keym440 . "80000000");
  126. $chaindata = pack("H*", $hash_m_44_0_r);
  127. $hash_m_44_0_0 = hash_hmac("sha512" , $hashdata, $chaindata);
  128. $hash_m_44_0_0_l = substr($hash_m_44_0_0, 0, 64);
  129. $hash_m_44_0_0_r = substr($hash_m_44_0_0, 64, 64);
  130.  
  131. $bitcoinECDSA->setPrivateKey($keym440);
  132. $finger_m_44_0 = $bitcoinECDSA->getPubKey();
  133. $finger_m_44_0 = hash('ripemd160', pack("H*", hash('sha256', pack("H*", $finger_m_44_0))));
  134. $finger_m_44_0 = substr($finger_m_44_0, 0, 8);
  135.  
  136. $math = EccFactory::getAdapter();
  137. $g = EccFactory::getSecgCurves($math)->generator256k1();
  138. $n = $g->getOrder();
  139. $Il_dec = $math->hexDec($hash_m_44_0_0_l);
  140. $private_key_dec = $math->hexDec($keym440);
  141. $key_dec = $math->mod($math->add($Il_dec, $private_key_dec), $n);
  142. $keym4400 = str_pad(BitcoinLib::hex_encode($key_dec), 64, '0', STR_PAD_LEFT);
  143.  
  144. $data_m_44_0_0 = array(
  145.             'network' => '0488ade4',
  146.             'depth' => '03',
  147.             'fingerprint' => $finger_m_44_0,
  148.             'i' => '80000000',
  149.             'chain_code' => $hash_m_44_0_0_r,
  150.             'key' => '00'.$keym4400,
  151.         );
  152.  
  153. $xprivkey_m_44_0_0 = implode($data_m_44_0_0);
  154. $checksum_m_44_0_0 = hash('sha256', pack("H*", hash('sha256', pack("H*", $xprivkey_m_44_0_0))));
  155. $checksum_m_44_0_0 = substr($checksum_m_44_0_0, 0, 8);
  156. $xprivkey_m_44_0_0 = $xprivkey_m_44_0_0 . $checksum_m_44_0_0;
  157. $base58 = new StephenHill\Base58();
  158. echo $base58->encode(pack("H*", $xprivkey_m_44_0_0))."<br>";
  159.  
  160. // End m/44'/0'/0' derivation
  161.  
  162. ?>
  163. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement