transactions – OP_CHECKLOCKTIMEVERIFY Utilizing Python on Bitcoin Mainnet

0
54


I’m attempting to create a uncooked bitcoin transaction in hexadecimal kind utilizing python and I am having hassle efficiently broadcasting a transaction that makes use of OP_CHECKLOCKTIMEVERIFY (that’s, fund-freezing script). Right here is the python code.

utxo = [{'txid':'a75d4f4e399167a5ef3eefff5d2dba8b6e6c5ac2e2c1c522132fb44c9515395e', 'vout': 0}]
priv_key_hex3 = "????????"
address4 = "3ExeUN35zc6gaHgKAptC3nxxgbn239nZNz"

model = struct.pack("<L", 2).hex()
marker = "00"
flag = "01"
sequence = struct.pack("<L", int("fe000001", base=16)).hex() 

input_count = struct.pack('<B', 1).hex()
prev_txid1 = bytes.fromhex(utxo[0]['txid'])[::-1].hex()
input_index1 = struct.pack("<L", utxo[0]['vout']).hex()
input_script1 = bitcoin.address_to_script(bitcoin.privkey_to_address(
    bitcoin.encode_privkey(bitcoin.decode_privkey(priv_key_hex3, 'hex_compressed'), 'wif_compressed')))
input_script_length1 = hex(len(bytes.fromhex(input_script1)))[2:]

output_count = struct.pack('<B', 1).hex()
balance1 = struct.pack("<Q", 145000).hex()
value1 = struct.pack("<Q", 130000).hex()
output_hash160_1 = base58.b58decode(address4)[1:21].hex()
# <expiry time> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
# 04 <expiry time> b17576a914 <pubKeyHash> 88ac
expiry_time = struct.pack("<L", int(time.time() + 1800)).hex() # half-hour lock
output_freezetime_script1 = "04{}b17576a914{}88ac".format(expiry_time, output_hash160_1)
output_script_length1 = hex(len(bytes.fromhex(output_freezetime_script1)))[2:]

hashPrevOuts = hashlib.sha256(hashlib.sha256(bytes.fromhex(prev_txid1 + input_index1)).digest()).hexdigest()
hashSequence = hashlib.sha256(hashlib.sha256(bytes.fromhex(sequence)).digest()).hexdigest()
hashOutputs = hashlib.sha256(hashlib.sha256(bytes.fromhex(value1 +
    output_script_length1 + output_freezetime_script1)).digest()).hexdigest()

locktime = struct.pack("<L", int(time.time() + 1800)).hex()
sighash_code = struct.pack("<L", 1).hex()

hash_preimage1 = model + hashPrevOuts + hashSequence + prev_txid1 + input_index1 + 
    input_script_length1 + input_script1 + balance1 + sequence + hashOutputs + locktime + sighash_code

signing_key1 = ecdsa.SigningKey.from_string(bytes.fromhex(priv_key_hex3), curve=ecdsa.SECP256k1)
sighash1 = hashlib.sha256(hashlib.sha256(bytes.fromhex(hash_preimage1)).digest()).digest()
signature1 = signing_key1.sign_digest(sighash1, sigencode=ecdsa.util.sigencode_der_canonize)

compressed_pub_key1 = bitcoin.encode_pubkey(
    bitcoin.fast_multiply(bitcoin.G, bitcoin.decode_privkey(priv_key_hex3, 'hex')), 'hex_compressed')
witness1 = "02" + hex(len(signature1 + bytes([1])))[2:] + signature1.hex() + "01" + 
    hex(len(bytes.fromhex(compressed_pub_key1)))[2:] + compressed_pub_key1

payload = model + marker + flag + input_count + 
          prev_txid1 + input_index1 + "1716" + redeem_script(priv_key_hex3) + sequence + 
          output_count + value1 + output_script_length1 + output_freezetime_script1 + 
          witness1 + locktime

I broadcasted my uncooked tx and acquired a message, saying that the “transaction was rejected as a result of community guidelines.nnscriptpubkey”. I double checked the opposite elements and my code efficiently labored once I used the usual output script. I learn some articles and conscious of the circumstances and restrictions(nSequence and nLocktime) when utilizing this opcode however I am unable to actually inform precisely which half I’m flawed. Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here