Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // block 100,004
- $txids = array(
- 'AB7ED423933FE5413DC51B1041A58FD8AF0CD70491B1CE607CB41DDDCE74A92B',
- 'C763E59A79C9F1E626DDF1C3E9F20F234959C457FF82918F7B24E9D18A12DB99',
- '80EA3E647AEEF92973F5414E0CAA1721C7B42345B99ED161DD505ACC41F5516B',
- '1B72EEFD70CE0A362EC0CB48E2213274DF3C55F9DABD5806CDC087A335498CD3',
- '23E13370F6D59E2D7C7A9CA604B872312DE34A387BD7DECA2C8F4486F7E66173',
- '149A098D6261B7F9359A572D797C4A41B62378836A14093912618B15644BA402',
- );
- // ------------
- // PREPARE DATA
- // ------------
- // Convert txids in to big endian (BE), because that's the format they need to be hashed in to get the merkle root.
- foreach ($txids as $txid) {
- $txidsBE[] = implode('', array_reverse(str_split($txid, 2)));
- }
- // Now convert each of these txids in to binary, because the hash function wants the binary value, not the hex.
- foreach ($txidsBE as $txidBE) {
- $txidsBEbinary[] = hex2bin($txidBE);
- }
- // -----------------------------
- // RECURSIVE FUNCTION STARTS NOW
- // -----------------------------
- // Now the TXIDs are ready. So run through the array, concatenating each pair and hashing them.
- function merkleroot($txids) {
- while (count($txids) > 0) {
- if (count($txids) >= 2) {
- // Get first two
- $pair_first = $txids[0];
- $pair_second = $txids[1];
- // Hash them
- $pair = $pair_first.$pair_second;
- $pairhashes[] = hash('sha256', $pair);
- // Remove those two from the array
- unset($txids[0]);
- unset($txids[1]);
- // Re-set the indexes (the above just nullifies the values) and make a new array without the original first two slots.
- $txids = array_values($txids);
- }
- if (count($txids) == 1) {
- // Get the first one twice
- $pair_first = $txids[0];
- $pair_second = $txids[0];
- // Hash it with itself
- $pair = $pair_first.$pair_second;
- $pairhashes[] = hash('sha256', $pair);
- // Remove it from the array
- unset($txids[0]);
- // Re-set the indexes (the above just nullifies the values) and make a new array without the original first two slots.
- $txids = array_values($txids);
- }
- }
- return $pairhashes;
- }
- // Test
- print_r($txids);
- print_r(merkleroot($txidsBE));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement