def serialize_input(self, script): # Prev hash and index s = self.serialize_outpoint() # Script length, script, sequence s += int_to_hex(len(script) / 2) s += script s += write_uint32(self.in_sequence).encode('hex') return s
def serialize(self, estimate_size=False, witness=True): version = write_uint32(self.tx_ver).encode('hex') locktime = write_uint32(self.locktime).encode('hex') # inputs = self.inputs() input_list = self.input_list() # outputs = self.outputs() output_list = self.output_list() txins = int_to_hex(len(input_list)) + ''.join( txin.serialize_input(txin.input_script(estimate_size)) for txin in input_list) txouts = int_to_hex(len(output_list)) + ''.join(o.serialize_output() for o in output_list) if witness and self.is_segwit(): marker = '00' flag = '01' witness = ''.join(x.serialize_witness() for x in input_list) return version + marker + flag + txins + txouts + witness + locktime else: return version + txins + txouts + locktime
def serialize(self, estimate_size=False, witness=True): """ raw to struct info :return: """ version = write_uint32(self.tx_ver).encode('hex') locktime = write_uint32(self.tx_locktime).encode('hex') input_list = self._input_list output_list = self._output_list txins = int_to_hex(len(input_list)) + ''.join( txin.serialize_input() for txin in input_list) txouts = int_to_hex(len(output_list)) + ''.join(o.serialize_output() for o in output_list) if witness and self.is_segwit(): marker = '00' flag = '01' witness = ''.join(x.serialize_witness() for x in input_list) return version + marker + flag + txins + txouts + witness + locktime else: return version + txins + txouts + locktime
def serialize_input_preimage(self, i): if i == self.in_sn: script = Script().get_script_pubkey(self.in_address) else: script = '' # Prev hash and index s = self.serialize_outpoint() # Script length, script, sequence s += int_to_hex(len(script) / 2) s += script s += write_uint32(self.in_sequence).encode('hex') return s
def serialize_input(self): script = self.in_signature # if script_type == 1: pubkeys, x_pubkeys = self.get_sorted_pubkeys() script = Script().input_script(self.in_dict, pubkeys, x_pubkeys) # Prev hash and index s = self.serialize_outpoint() # Script length, script, sequence s += int_to_hex(len(script) / 2) s += script s += write_uint32(self.in_sequence).encode('hex') return s
def serialize_preimage(self, i): """ get to be sign content :param i: :return: """ version = write_uint32(self.tx_ver).encode('hex') hash_type = write_uint32(1).encode('hex') locktime = write_uint32(self.tx_locktime).encode('hex') input_list = self._input_list output_list = self._output_list txin = input_list[i] if txin.is_segwit_input(): hash_prevouts = double_sha256( ''.join(txin.serialize_outpoint() for txin in input_list).decode('hex')).encode( 'hex') hash_sequence = double_sha256( ''.join(write_uint32(txin.in_sequence) for txin in input_list).decode( 'hex')).encode('hex') hash_outputs = double_sha256( ''.join(o.serialize_output() for o in output_list).decode('hex')).encode('hex') outpoint = txin.serialize_outpoint() pubkey = txin.in_dict['pubkeys'][0] pkh = hash_160(pubkey.decode('hex')).encode('hex') # redeem_script = '00' + push_script(pkh) script_code = push_script('76a9' + push_script(pkh) + '88ac') # script_hash = hash_160(redeem_script.decode('hex')).encode('hex') # script_pub_key = 'a9' + push_script(script_hash) + '87' amount = write_uint64(txin.in_value) sequence = write_uint32(txin.in_sequence) preimage = version + hash_prevouts + hash_sequence + outpoint + script_code + amount + sequence + hash_outputs + locktime + hash_type else: txins = int_to_hex(len(input_list)) + ''.join( txin.serialize_input_preimage(k) for k, txin in enumerate(input_list)) txouts = int_to_hex(len(output_list)) + ''.join( o.serialize_output() for o in output_list) preimage = version + txins + txouts + locktime + hash_type return preimage
def serialize_output(self): s = write_uint64(self.out_value).encode('hex') script = self.pay_script_from_address() s += int_to_hex(len(script) / 2) s += script return s
def serialize_witness(self): pubkeys, sig_list = self.get_siglist() n = len(pubkeys) + len(sig_list) return int_to_hex(n) + ''.join(push_script(x) for x in sig_list) + ''.join( push_script(x) for x in pubkeys)