def from_bytes(b): """ Deserializes a byte stream into a Transaction. Args: b (bytes): byte stream starting with the version. Returns: tuple: First element of the tuple is the Transaction, second is the remainder of the byte stream. """ # First 4 bytes are version version = struct.unpack('<I', b[:4])[0] b1 = b[4:] # Work on inputs num_inputs, b1 = unpack_compact_int(b1) inputs = [] for i in range(num_inputs): inp, b1 = TransactionInput.from_bytes(b1) inputs.append(inp) # Work on outputs num_outputs, b1 = unpack_compact_int(b1) outputs = [] for o in range(num_outputs): out, b1 = TransactionOutput.from_bytes(b1) outputs.append(out) # Lock time lock_time = struct.unpack('<I', b1[:4])[0] return (Transaction(version, inputs, outputs, lock_time), b1[4:])
def from_bytes(b): """ Deserializes a byte stream into a TransactionOutput object. Args: b (bytes): byte-stream beginning with the value. Returns: tuple: First element of the tuple is a TransactionOutput, the second is the remainder of the byte stream. """ value, b0 = unpack_u64(b) script_len, b0 = unpack_compact_int(b0) return (TransactionOutput(value, Script(b0[:script_len])), b0[script_len:])
def from_bytes(b): """ Creates a Block from a serialized byte stream. Args: b (bytes): The byte stream, starting with the block version. Returns: block, b (tuple): A tuple. The first item is the deserialized block and the second is the remainder of the byte stream. """ bh, b = BlockHeader.from_bytes(b) num_txns, b = unpack_compact_int(b) txns = [] for i in range(num_txns): t, b = Transaction.from_bytes(b) txns.append(t) return Block.from_blockheader(bh, txns), b