|
| 1 | +<?php |
| 2 | +require_once 'Blockchain/Backend/core/transactions/TxIn.php'; |
| 3 | +require_once 'Blockchain/Backend/core/transactions/TxOut.php'; |
| 4 | +require_once 'Blockchain/Backend/core/transactions/Tx.php'; |
| 5 | +require_once 'Blockchain/Backend/core/Script.php'; |
| 6 | +require_once 'Blockchain/Backend/util/util.php'; |
| 7 | +require_once 'Blockchain/Backend/API/Client_calls/PluginHelperAPI.php'; |
| 8 | + |
| 9 | +const ZERO_HASH = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 10 | +const REWARD = 50; |
| 11 | +const PRIVATE_KEY = "56114968769095885066321288702375272595970830268400415922098497799492460020984"; |
| 12 | +const MINER_ADDRESS = "1K3if2mFojLAWVtdD1eeYYKNVCwghpBvgb"; |
| 13 | + |
| 14 | +class Coinbase { |
| 15 | +public function __construct($blockHeight) { |
| 16 | +$this->blockHeightIntLittleEndian = intToLittleEndian($blockHeight, bytesNeeded($blockHeight)); |
| 17 | +} |
| 18 | + |
| 19 | +public function coinbaseTransaction() { |
| 20 | +$prevTx = hex2bin(ZERO_HASH); |
| 21 | +$prevIndex = 0xFFFFFFFF; |
| 22 | + |
| 23 | +$txIns = []; |
| 24 | +$txIns[] = new TxIn($prevTx, $prevIndex); |
| 25 | +$txIns[0]->scriptSig->cmds[] = $this->blockHeightIntLittleEndian; |
| 26 | + |
| 27 | +$txOuts = []; |
| 28 | +$targetAmount = REWARD * 100000000; |
| 29 | +$hexValue = $this->decodeBase58API(MINER_ADDRESS); |
| 30 | +$targetH160 = $hexValue; |
| 31 | +$targetScript = Script::p2pkhScript($targetH160); |
| 32 | +$txOuts[] = new TxOut($targetAmount, $targetScript); |
| 33 | + |
| 34 | +return new Tx(1, $txIns, $txOuts, 0); |
| 35 | +} |
| 36 | + |
| 37 | +public function decodeBase58API($value) |
| 38 | +{ |
| 39 | +$address = PluginHelperAPI::$clientAddress; |
| 40 | +$url = $address."get_decode_base58"; |
| 41 | +$ch = curl_init($url); |
| 42 | +$data = json_encode(array( |
| 43 | +"value" => $value |
| 44 | +)); |
| 45 | +$val = PluginHelperAPI::curlSkeletonIfDataSend($ch, "POST", $data); |
| 46 | +$data = json_decode($val['data'], true); |
| 47 | +return $data['byte_data']; |
| 48 | +} |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +//$address = PluginHelperAPI::$clientAddress; |
| 53 | +//$url = $address."get_decode_base58"; |
| 54 | +//$ch = curl_init($url); |
| 55 | +//$data = json_encode(array( |
| 56 | +// "value" => "1K3if2mFojLAWVtdD1eeYYKNVCwghpBvgb" |
| 57 | +//)); |
| 58 | +//$val = PluginHelperAPI::curlSkeletonIfDataSend($ch, "POST", $data); |
| 59 | +//$data = json_decode($val['data'], true); |
| 60 | +//echo "\n\n"; |
| 61 | +//print_r($data['byte_data']); |
| 62 | +//function decodeBase58API($value) |
| 63 | +//{ |
| 64 | +// $address = PluginHelperAPI::$clientAddress; |
| 65 | +// $url = $address . "get_decode_base58"; |
| 66 | +// $ch = curl_init($url); |
| 67 | +// $data = json_encode(array( |
| 68 | +// "value" => $value |
| 69 | +// )); |
| 70 | +// $val = PluginHelperAPI::curlSkeletonIfDataSend($ch, "POST", $data); |
| 71 | +// $data = json_decode($val['data'], true); |
| 72 | +// return $data['byte_data']; |
| 73 | +//} |
| 74 | +// |
| 75 | +//$data = decodeBase58API("1K3if2mFojLAWVtdD1eeYYKNVCwghpBvgb"); |
| 76 | +//echo "\n\n"; |
| 77 | +//print_r($data); |
| 78 | +//echo "\n\n"; |
| 79 | +// |
| 80 | +// |
| 81 | +//$binaryData = hex2bin($data); |
| 82 | +//print_r($binaryData); |
| 83 | +//echo "\n\n"; |
| 84 | +// |
| 85 | +//// Perform operations on the binary data (if needed) |
| 86 | +// |
| 87 | +//// Convert the binary data back to a hexadecimal string |
| 88 | +//$resultHexadecimal = bin2hex($binaryData); |
| 89 | +// |
| 90 | +//// Output the result |
| 91 | +//echo $resultHexadecimal; |
| 92 | + |
| 93 | +////$hexString = $data; |
| 94 | +////$byteString = 'b"' . implode('\x', str_split($hexString, 2)) . '"'; |
| 95 | +//$byteString = hex2bin($data); |
| 96 | +//$formattedBinary = 'b"'; |
| 97 | +//foreach (str_split($byteString) as $byte) { |
| 98 | +// $formattedBinary .= '\x' . bin2hex($byte); |
| 99 | +//} |
| 100 | +//$formattedBinary .= '",'; |
| 101 | +// |
| 102 | +//echo $formattedBinary; |
| 103 | +//echo "\n\n"; |
| 104 | +// |
| 105 | +//$hexString = ''; |
| 106 | +//$matches = []; |
| 107 | +//if (preg_match('/b"(.+)",/', $formattedBinary, $matches)) { |
| 108 | +// $hexBytes = explode('\x', $matches[1]); |
| 109 | +// foreach ($hexBytes as $hexByte) { |
| 110 | +// $hexString .= chr(hexdec($hexByte)); |
| 111 | +// } |
| 112 | +// $hexString = bin2hex($hexString); |
| 113 | +// echo $hexString; |
| 114 | +//} else { |
| 115 | +// echo "Invalid format."; |
| 116 | +//} |
| 117 | +//echo "\n\n"; |
| 118 | +// |
| 119 | +//$hexString = ''; |
| 120 | +//$matches = []; |
| 121 | +// |
| 122 | +//if (preg_match('/b"(.+)",/', $formattedBinary, $matches)) { |
| 123 | +// $hexBytes = explode('\x', $matches[1]); |
| 124 | +// foreach ($hexBytes as $hexByte) { |
| 125 | +// $hexString .= bin2hex(hex2bin($hexByte)); |
| 126 | +// } |
| 127 | +// echo $hexString; |
| 128 | +//} else { |
| 129 | +// echo "Invalid format."; |
| 130 | +//} |
0 commit comments