def parse_from_hex(self, raw_hex): offset = 4 self._version = decode_uint32(raw_hex[:4]) input_cnt, varint_size = decode_varint(raw_hex[offset:]) self.input_cnt = input_cnt offset += varint_size self._inputs = [] for i in range(input_cnt): tx_input = TransactionInput().parse_from_hex(raw_hex[offset:]) print tx_input.__repr__() offset += tx_input.size self._inputs.append(input) output_cnt, varint_size = decode_varint(raw_hex[offset:]) offset += varint_size self.output_cnt = output_cnt self._outputs = [] for i in range(output_cnt): tx_output = TransactionOutput().parse_from_hex(raw_hex[offset:]) print tx_output.__repr__() offset += tx_output.size self._outputs.append(tx_output) self.size = offset + 4 self.hash = format_hash(double_sha256(raw_hex[:self.size])) return self
def __init__(self, raw_hex): self._hash = None self._txid = None self.inputs = None self.outputs = None self._version = None self._locktime = None self.n_inputs = 0 self.n_outputs = 0 self.is_segwit = False offset = 4 # adds basic support for segwit transactions # - https://bitcoincore.org/en/segwit_wallet_dev/ # - https://en.bitcoin.it/wiki/Protocol_documentation#BlockTransactions if b'\x00\x01' == raw_hex[offset:offset + 2]: self.is_segwit = True offset += 2 self.n_inputs, varint_size = decode_varint(raw_hex[offset:]) offset += varint_size self.inputs = [] for i in range(self.n_inputs): input = Input.from_hex(raw_hex[offset:]) offset += input.size self.inputs.append(input) self.n_outputs, varint_size = decode_varint(raw_hex[offset:]) offset += varint_size self.outputs = [] for i in range(self.n_outputs): output = Output.from_hex(raw_hex[offset:]) offset += output.size self.outputs.append(output) if self.is_segwit: self._offset_before_tx_witnesses = offset for inp in self.inputs: tx_witnesses_n, varint_size = decode_varint(raw_hex[offset:]) offset += varint_size for j in range(tx_witnesses_n): component_length, varint_size = decode_varint( raw_hex[offset:]) offset += varint_size witness = raw_hex[offset:offset + component_length] inp.add_witness(witness) offset += component_length self.size = offset + 4 self.hex = raw_hex[:self.size]
def parse_transactions(self, raw_hex): tx_cnt, offset = decode_varint(raw_hex) self._tx_cnt = tx_cnt for i in range(tx_cnt): transaction = Transaction().parse_from_hex(raw_hex[offset:]) print transaction.__repr__() offset += transaction.size self._tx_list.append(transaction)
def parse_from_hex(self, raw_hex): script_length, varint_size = decode_varint(raw_hex[8:]) script_start = 8 + varint_size self.size = script_start + script_length self._value = decode_uint64(raw_hex[:8]) self._script = raw_hex[script_start:script_start + script_length] # self.type = self.get_type() return self
def n_transactions(self): """Return the number of transactions contained in this block, it is faster to use this than to use len(block.transactions) as there's no need to parse all transactions to get this information """ if self._n_transactions is None: self._n_transactions = decode_varint(self.hex[80:])[0] return self._n_transactions
def __init__(self, raw_hex): self._value = None self._script = None self._addresses = None script_length, varint_size = decode_varint(raw_hex[8:]) script_start = 8 + varint_size self._script_hex = raw_hex[script_start:script_start + script_length] self.size = script_start + script_length self._value_hex = raw_hex[:8]
def parse_from_hex(self, raw_hex): script_length, varint_length = decode_varint(raw_hex[36:]) script_start = 36 + varint_length self.size = script_start + script_length + 4 real_hex = raw_hex[:self.size] self._previous_transaction_hash = format_hash(real_hex[:32]) self._previous_transaction_index = decode_uint32(real_hex[32:36]) self._script = Script(real_hex[script_start:(script_start + script_length)]) self._sequence_number = decode_uint32(real_hex[(self.size - 4):self.size]) return self
def __init__(self, raw_hex): self._transaction_hash = None self._transaction_index = None self._script = None self._sequence_number = None self._witnesses = [] self._script_length, varint_length = decode_varint(raw_hex[36:]) self._script_start = 36 + varint_length self.size = self._script_start + self._script_length + 4 self.hex = raw_hex[:self.size]
def get_block_transactions(raw_hex): """Given the raw hexadecimal representation of a block, yields the block's transactions """ # Skipping the header transaction_data = raw_hex[80:] # Decoding the number of transactions, offset is the size of # the varint (1 to 9 bytes) n_transactions, offset = decode_varint(transaction_data) for i in range(n_transactions): transaction = Transaction.from_hex(transaction_data[offset:]) yield transaction # Skipping to the next transaction offset += transaction.size