Takes a byte stream and parses the transaction at the start return a Tx object
(cls, s, testnet=False)
| 103 | |
| 104 | @classmethod |
| 105 | def parse(cls, s, testnet=False): |
| 106 | '''Takes a byte stream and parses the transaction at the start |
| 107 | return a Tx object |
| 108 | ''' |
| 109 | # s.read(n) will return n bytes |
| 110 | # version is an integer in 4 bytes, little-endian |
| 111 | version = little_endian_to_int(s.read(4)) |
| 112 | # num_inputs is a varint, use read_varint(s) |
| 113 | num_inputs = read_varint(s) |
| 114 | # parse num_inputs number of TxIns |
| 115 | inputs = [] |
| 116 | for _ in range(num_inputs): |
| 117 | inputs.append(TxIn.parse(s)) |
| 118 | # num_outputs is a varint, use read_varint(s) |
| 119 | num_outputs = read_varint(s) |
| 120 | # parse num_outputs number of TxOuts |
| 121 | outputs = [] |
| 122 | for _ in range(num_outputs): |
| 123 | outputs.append(TxOut.parse(s)) |
| 124 | # locktime is an integer in 4 bytes, little-endian |
| 125 | locktime = little_endian_to_int(s.read(4)) |
| 126 | # return an instance of the class (see __init__ for args) |
| 127 | return cls(version, inputs, outputs, locktime, testnet=testnet) |
| 128 | |
| 129 | def serialize(self): |
| 130 | '''Returns the byte serialization of the transaction''' |