def from_bytes(b): """ Creates a BlockHeader object from a serialized bytestream. This function "eats" the bytestream and returns the remainder of the stream after deserializing the fields of the BlockHeader. Args: b (bytes): bytes beginning with the (4-byte) version. Returns: bh, b (tuple): A tuple containing two elements - a BlockHeader object and the remainder of the bytestream after deserialization. """ version, b = unpack_u32(b) prev_block_hash, b = Hash(b[0:32]), b[32:] merkle_root_hash, b = Hash(b[0:32]), b[32:] time, b = unpack_u32(b) bits, b = unpack_u32(b) nonce, b = unpack_u32(b) return ( BlockHeader(version, prev_block_hash, merkle_root_hash, time, bits, nonce), b )
def __init__(self, height, version, prev_block_hash, time, bits, nonce, txns): self.block_header = BlockHeader(version, prev_block_hash, Hash(bytes(32)), # Fake merkle_root for now time, bits, nonce) # Fake nonce also self.height = height self.txns = txns self.merkle_tree = None self.invalidate()
def __init__(self, height, version, prev_block_hash, time, bits, merkle_edge, cb_txn=None): self.block_header = BlockHeader(version, prev_block_hash, Hash(bytes(32)), # Fake merkle_root for now time, bits, 0) # Fake nonce also self.height = height self.merkle_edge = merkle_edge if cb_txn is not None: self.coinbase_transaction = cb_txn else: self._cb_txn = None
def from_bytes(b): """ Deserializes a byte stream into a TransactionInput. Args: b (bytes): byte stream starting with the outpoint. Returns: tuple: First element of the tuple is the TransactionInput object and the second is the remaining byte stream. """ outpoint = b[0:32] outpoint_index, b1 = unpack_u32(b[32:]) script, b1 = Script.from_bytes(b1) sequence_num, b1 = unpack_u32(b1) return (TransactionInput(Hash(outpoint), outpoint_index, script, sequence_num), b1)
class CoinbaseInput(TransactionInput): """ See https://bitcoin.org/en/developer-reference#coinbase Args: height (uint): The height of the block coinbase is part of will go into. Not required for version 1 blocks. raw_script (bytes): the bytes of the coinbase script. For block_version > 1 the height portion should NOT be included in this script. sequence (int): Unless you are Satoshi with a version 1 block, the default is fine. If you are Satoshi, send me some of your private keys and set this to 0. block_version (int): The version of the block this coinbase is a part of or will go into. If raw_script already contains the height of the block, this must be 1. """ NULL_OUTPOINT = Hash(bytes(32)) MAX_INT = 0xffffffff def __init__(self, height, raw_script, sequence=MAX_INT, block_version=3): self.height = height if block_version == 1: scr = raw_script else: scr = Script.build_push_int(self.height) + raw_script # Coinbase scripts are basically whatever, so we don't # try to create a script object from them. super().__init__(self.NULL_OUTPOINT, self.MAX_INT, scr, sequence) def get_addresses(self, testnet=False): """ Returns all addresses associated with the script in this input. Args: testnet (bool): True if the transaction is a testnet transaction. Returns: list (str): A list of all addresses found in the script. """ return [] def __str__(self): """ Returns a human readable formatting of this input. Returns: s (str): A string containing the human readable input. """ return ("CoinbaseInput(" + "Outpoint: %s " % (self.outpoint) + "Outpoint Index: 0x%08x " % (self.outpoint_index) + "Script: %s " % (bytes_to_str(self.script)) + "Sequence: 0x%08x)" % (self.sequence_num)) def __bytes__(self): """ Serializes the object into a byte stream. Returns: b (bytes): byte stream containing the serialized coinbase input. """ return (bytes(self.outpoint) + pack_u32(self.outpoint_index) + pack_var_str(self.script) + pack_u32(self.sequence_num))