def sig_hash(self, input_index, redeem_script=None): s = int_to_little_endian(self.version, 4) s += encode_varint(len(self.tx_ins)) for i, tx_in in enumerate(self.tx_ins): if i == input_index: if redeem_script: script_sig = redeem_script else: script_sig = tx_in.script_pubkey(self.testnet) else: script_sig = None s += TxIn( prev_tx=tx_in.prev_tx, prev_index=tx_in.prev_index, script_sig=script_sig, sequence=tx_in.sequence, ).serialize() s += encode_varint(len(self.tx_outs)) for tx_out in self.tx_outs: s += tx_out.serialize() s += int_to_little_endian(self.locktime, 4) s += int_to_little_endian(SIGHASH_ALL, 4) h256 = hash256(s) return int.from_bytes(h256, "big")
def serialize(self): result = int_to_little_endian(self.version, 4) result += int_to_little_endian(self.services, 8) result += int_to_little_endian(self.timestamp, 8) result += int_to_little_endian(self.receiver_services, 8) result += b"\x00" * 10 + b"\xff\xff" + self.receiver_ip result += self.receiver_port.to_bytes(2, "big") result += int_to_little_endian(self.sender_services, 8) result += b"\x00" * 10 + b"\xff\xff" + self.sender_ip result += self.sender_port.to_bytes(2, "big") result += self.nonce result += encode_varint(len(self.user_agent)) result += self.user_agent result += int_to_little_endian(self.latest_block, 4) if self.relay: result += b"\x01" else: result += b"\x00" return result
def sig_hash_bip143(self, input_index, redeem_script=None, witness_script=None): tx_in = self.tx_ins[input_index] s = int_to_little_endian(self.version, 4) s += self.hash_prevouts() + self.hash_sequence() s += tx_in.prev_tx[::-1] + int_to_little_endian(tx_in.prev_index, 4) if witness_script: script_code = witness_script.serialize() elif redeem_script: script_code = p2pkh_script(redeem_script.cmds[1]).serialize() else: script_code = p2pkh_script( tx_in.script_pubkey(self.testnet).cmds[1] ).serialize() s += script_code s += int_to_little_endian(tx_in.value(), 8) s += int_to_little_endian(tx_in.sequence, 4) s += self.hash_outputs() s += int_to_little_endian(self.locktime, 4) s += int_to_little_endian(SIGHASH_ALL, 4) return int.from_bytes(hash256(s), "big")
def raw_serialize(self): result = b"" for cmd in self.cmds: if type(cmd) == int: result += int_to_little_endian(cmd, 1) else: length = len(cmd) if length < 75: result += int_to_little_endian(length, 1) # OP_PUSHDATA1 elif length > 75 and length < 0x100: result += int_to_little_endian(76, 1) result += int_to_little_endian(length, 1) # OP_PUSHDATA2 elif length >= 0x100 and length <= 520: result += int_to_little_endian(77, 1) result += int_to_little_endian(length, 2) else: raise ValueError("too long an cmd") result += cmd return result
def serialize(self): result = int_to_little_endian(self.version, 4) result += encode_varint(self.num_hashes) result += self.start_block[::-1] result += self.end_block[::-1] return result
def serialize(self): result = self.prev_tx[::-1] result += int_to_little_endian(self.prev_index, 4) result += self.script_sig.serialize() result += int_to_little_endian(self.sequence, 4) return result
def serialize(self): result = int_to_little_endian(self.amount, 8) result += self.script_pubkey.serialize() return result
def serialize(self): result = encode_varint(len(self.data)) for data_type, identifier in self.data: result += int_to_little_endian(data_type, 4) result += identifier[::-1] return result