Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2. // block 100,004
  3. $txids = array(
  4.     'AB7ED423933FE5413DC51B1041A58FD8AF0CD70491B1CE607CB41DDDCE74A92B',
  5.     'C763E59A79C9F1E626DDF1C3E9F20F234959C457FF82918F7B24E9D18A12DB99',
  6.     '80EA3E647AEEF92973F5414E0CAA1721C7B42345B99ED161DD505ACC41F5516B',
  7.     '1B72EEFD70CE0A362EC0CB48E2213274DF3C55F9DABD5806CDC087A335498CD3',
  8.     '23E13370F6D59E2D7C7A9CA604B872312DE34A387BD7DECA2C8F4486F7E66173',
  9.     '149A098D6261B7F9359A572D797C4A41B62378836A14093912618B15644BA402',
  10. );
  11.  
  12.  
  13. // ------------
  14. // PREPARE DATA
  15. // ------------
  16.  
  17. // Convert txids in to big endian (BE), because that's the format they need to be hashed in to get the merkle root.
  18. foreach ($txids as $txid) {
  19.     $txidsBE[] = implode('', array_reverse(str_split($txid, 2)));
  20. }
  21.  
  22. // Now convert each of these txids in to binary, because the hash function wants the binary value, not the hex.
  23. foreach ($txidsBE as $txidBE) {
  24.     $txidsBEbinary[] = hex2bin($txidBE);
  25. }
  26.  
  27.  
  28. // -----------------------------
  29. // RECURSIVE FUNCTION STARTS NOW
  30. // -----------------------------
  31. // Now the TXIDs are ready. So run through the array, concatenating each pair and hashing them.
  32.  
  33. function merkleroot($txids) {
  34.        
  35.     while (count($txids) > 0) {
  36.  
  37.         if (count($txids) >= 2) {
  38.             // Get first two
  39.             $pair_first = $txids[0];
  40.             $pair_second = $txids[1];
  41.  
  42.             // Hash them
  43.             $pair = $pair_first.$pair_second;
  44.             $pairhashes[] = hash('sha256', $pair);
  45.  
  46.             // Remove those two from the array
  47.             unset($txids[0]);
  48.             unset($txids[1]);
  49.             // Re-set the indexes (the above just nullifies the values) and make a new array without the original first two slots.
  50.             $txids = array_values($txids);
  51.         }
  52.  
  53.         if (count($txids) == 1) {
  54.         // Get the first one twice
  55.             $pair_first = $txids[0];
  56.             $pair_second = $txids[0];
  57.  
  58.             // Hash it with itself
  59.             $pair = $pair_first.$pair_second;
  60.             $pairhashes[] = hash('sha256', $pair);
  61.  
  62.             // Remove it from the array
  63.             unset($txids[0]);
  64.             // Re-set the indexes (the above just nullifies the values) and make a new array without the original first two slots.
  65.             $txids = array_values($txids);
  66.         }
  67.  
  68.     }
  69.  
  70.     return $pairhashes;
  71.  
  72. }
  73.  
  74.  
  75. // Test
  76. print_r($txids);
  77. print_r(merkleroot($txidsBE));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement