|
1 | 1 | <?php
|
2 |
| - |
| 2 | +require_once 'Blockchain/Backend/util/util.php'; |
3 | 3 | class Tx
|
4 | 4 | {
|
5 | 5 | public $locktime;
|
@@ -14,15 +14,52 @@ public function __construct($version, $txIns, $txOuts, $locktime) {
|
14 | 14 | $this->locktime = $locktime;
|
15 | 15 | }
|
16 | 16 |
|
| 17 | +/** |
| 18 | +* @throws Exception |
| 19 | +*/ |
17 | 20 | public function serialize() {
|
18 |
| -$result = intToLittleEndian($this->version, 4); |
19 |
| -$result .= count($this->txIns); |
| 21 | +// Initialize an empty result string |
| 22 | +$result = ""; |
| 23 | + |
| 24 | +// Serialize the version as a little-endian 4-byte integer |
| 25 | +$result .= intToLittleEndian($this->version, 4); |
| 26 | + |
| 27 | +// Serialize the number of transaction inputs as a variable-length integer |
| 28 | +$result .= encode_varint(count($this->tx_ins)); |
| 29 | + |
| 30 | +// Serialize each transaction input |
| 31 | +foreach ($this->tx_ins as $tx_in) { |
| 32 | +$result .= $tx_in->serialize(); |
| 33 | +} |
| 34 | + |
| 35 | +// Serialize the number of transaction outputs as a variable-length integer |
| 36 | +$result .= encode_varint(count($this->tx_outs)); |
20 | 37 |
|
21 |
| -// Add serialization logic here |
| 38 | +// Serialize each transaction output |
| 39 | +foreach ($this->tx_outs as $tx_out) { |
| 40 | +$result .= $tx_out->serialize(); |
| 41 | +} |
| 42 | + |
| 43 | +// Serialize the locktime as a little-endian 4-byte integer |
| 44 | +$result .= intToLittleEndian($this->locktime, 4); |
22 | 45 |
|
| 46 | +// Return the serialized result |
23 | 47 | return $result;
|
24 | 48 | }
|
25 | 49 |
|
| 50 | +public function id() { |
| 51 | +// Human-readable Tx id |
| 52 | +return $this->hash()->hex(); |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | +* @throws Exception |
| 57 | +*/ |
| 58 | +public function hash() { |
| 59 | +// Binary Hash of serialization |
| 60 | +return strrev(hash256($this->serialize())); |
| 61 | +} |
| 62 | + |
26 | 63 | public function isCoinbase() {
|
27 | 64 | if (count($this->txIns) !== 1) {
|
28 | 65 | return false;
|
|
0 commit comments