def parse_script(self, unlock_script, lock_script , txid) -> bool: # print(f'@@@@@@@@@@@@@@@@{unlock_script}\n{lock_script}') self.tx_hash = txid size, padd = var_int.varint_to_int(unlock_script) # print (unlock_script[padd:size*2 + padd]) sig = bytearray.fromhex(unlock_script[padd:size*2 + padd])[:-1] unlock_script = unlock_script[size*2 + padd:] size, padd = var_int.varint_to_int(unlock_script) # print (unlock_script[padd:size*2 + padd]) pubKey = bytearray.fromhex(unlock_script[padd:size*2 + padd]) self.stack.append(sig) self.stack.append(pubKey) skip = 0 i = len(lock_script) while i > 0: if len(lock_script) == 0: break if skip: self.stack.append(bytearray.fromhex(lock_script[:skip*2])) lock_script = lock_script[(skip * 2 - 2):] i-= (skip * 2 - 2) skip = 0 elif lock_script[0:2] in self.func: if not self.func[lock_script[0:2]](): return False else: skip = int(lock_script[0:2],16) lock_script = lock_script[2:] i -= 1 return True if self.stack[-1] == b'\x01' else False
def deserialize_output(s): padd = 0 r_o = Output() r_o.value = int.from_bytes(bytearray.fromhex(s[0:16]), byteorder='little') #? value padd += 16 s = s[16:] r_o.script_pub_key_size, v_int = var_int.varint_to_int(s) #? script pub_key size padd += v_int s = s[v_int:] r_o.script_pub_key = s[0:r_o.script_pub_key_size * 2] #? script pub_key padd += r_o.script_pub_key_size * 2 a_len , v_int = var_int.varint_to_int(r_o.script_pub_key[5:7]) r_o.recipient_address = r_o.script_pub_key[5 + v_int: a_len] #? p2pkh getting addres from return (r_o, padd)
def deserialize(s) -> Transaction: tx = Transaction() tx.version = int.from_bytes(bytearray.fromhex(s[0:8]), byteorder = "little") s = s[8:] tx.input_count, v_int = var_int.varint_to_int(s) s = s[v_int:] for i in range(0,tx.input_count): inp , padd = Deserializer.deserialize_input(s) tx.inputs.append(inp) s = s[padd:] tx.output_count, v_int = var_int.varint_to_int(s) s = s[v_int:] for i in range (0, tx.output_count): otp, padd = Deserializer.deserialize_output(s) tx.outputs.append(otp) s = s[padd:] return tx
def recipient_address(self): if self.script_pub_key == "": return "" a_len, v_int = var_int.varint_to_int(self.script_pub_key[4:]) hex160 = bytearray.fromhex( self.script_pub_key[(4 + v_int):(a_len * 2 + (4 + v_int))]) return wallet.covert_to_address(hex160=hex160, net=0x6f) #! net identificator
def deserialize_output(s): 'return output and it`s n chars' padd = 0 r_o = Output() r_o.value = int.from_bytes(bytearray.fromhex(s[0:16]), byteorder='little') #? value padd += 16 s = s[16:] r_o.script_pub_key_size, v_int = var_int.varint_to_int( s) #? script pub_key size padd += v_int s = s[v_int:] r_o.script_pub_key = s[0:r_o.script_pub_key_size * 2] #? script pub_key padd += r_o.script_pub_key_size * 2 return (r_o, padd)
def deserialize_input(s): padd = 0 r_i = Input() r_i.txid = (bytearray.fromhex(s[0:64]))[::-1] #?txid padd += 64 s = s[64:] r_i.vout = int.from_bytes(bytearray.fromhex(s[0:8]), byteorder='little') #?vout padd += 8 s = s[8:] r_i.scriptsigsize, v_int = var_int.varint_to_int(s) #?sript sig size padd += v_int s = s[v_int:] r_i.scriptsig = s[0:r_i.scriptsigsize * 2] #? script sig padd += r_i.scriptsigsize * 2 s = s[r_i.scriptsigsize * 2:] r_i.sequence = bytearray.fromhex(s[0:8])[::-1] #? sequence padd += 8 return (r_i, padd)