0

I have the following code that successfully created a bitcoin transaction with an OP_RETURN message:

from bitcoinutils.utils import to_satoshis
from bitcoinutils.setup import setup
from bitcoinutils.transactions import Transaction, TxInput, TxOutput
from bitcoinutils.script import Script
from bitcoinutils.keys import PrivateKey as utilPrivKey
from bitcoinutils.constants import SIGHASH_ALL, SIGHASH_ANYONECANPAY

setup('testnet')

# private key for tb1qqh5eczga9ltll4crtyvr4epxcssyufuvlhwdxv
priv = utilPrivKey.from_wif('SENDING ADDRESS PRIVATE KEY')

pub = priv.get_public_key().to_hex()
addrs_for_script = priv.get_public_key().get_address()

# private key for tb1qpeervdxrt2np74xhm4q4jjp76ey5f9fh9tf58f
recPrivKey = utilPrivKey.from_wif('RECEIVING ADDRESS PRIVATE KEY')

# This UTXO has 0.00032557 btc
txin1 = TxInput("947145005bac29a9966403e986b8a36cb225cba504bffbb3f6c1da7dba7197be", 1)

# This UTXO has 0.00006859 btc
txin2 = TxInput("aea283b193f2179cb6a5c2bcea4b9ad7158960696ebfb5c15d22a0411f25dafe", 0)

# the script code required for signing for p2wpkh is the same as p2pkh
script_code = Script(['OP_DUP', 'OP_HASH160', addrs_for_script.to_hash160(),
                        'OP_EQUALVERIFY', 'OP_CHECKSIG'])

addr = recPrivKey.get_public_key().get_segwit_address()
addr_non_seg = recPrivKey.get_public_key().get_address()

# This is random text designed to be hashed 8hwi8a8x3lja1ukbu ---> 69eb032167c25784108b9783c9d59ce23608d00238eb9e3c03c73cdd79f74528
msg1 = "69eb032167c25784108b9783c9d59ce23608d00238eb9e3c03c73cdd79f74528"

op_return_script = ["OP_RETURN"]
op_return_script.append(msg1)
op_return_script = Script(op_return_script)
print(op_return_script)
print(addr.to_script_pub_key())

txoutOpR = TxOutput(0, op_return_script)

# remaining 0.00005 is tx fees
txout = TxOutput(to_satoshis(0.00034416), addr.to_script_pub_key())

# create transaction from inputs/outputs -- default locktime is used
tx = Transaction([txin1, txin2], [txoutOpR, txout], has_segwit=True)

txsign1 = priv.sign_segwit_input(tx, 0, script_code, to_satoshis(0.00032557), SIGHASH_ALL | SIGHASH_ANYONECANPAY)
tx.witnesses = [ Script([txsign1, pub]) ]

txsign2 = priv.sign_segwit_input(tx, 1, script_code, to_satoshis(0.00006859), SIGHASH_ALL)
tx.witnesses.append( Script([txsign2, pub]) )

signed_tx = tx.serialize()
print("raw tx below this line")
print(signed_tx)
print("raw tx above this line")

I used the above code to make this transaction:
02000000000102be9771ba7ddac1f6b3fbbf04a5cb25b26ca3b886e9036496a929ac5b004571940100000000fffffffffeda251f41a0225dc1b5bf6e69608915d79a4beabcc2a5b69c17f293b183a2ae0000000000ffffffff020000000000000000226a2069eb032167c25784108b9783c9d59ce23608d00238eb9e3c03c73cdd79f7452870860000000000001600140e723634c35aa61f54d7dd4159483ed649449537024730440220188a85f11816410f8ea51a933dbf9e38b318203602a42019837534789cd3915a02200351fa3e40ac446959d295f4efcbd2f76f0546b4be8c2066e5ffac9a3688e519812102cc30cb24a244910be0a343ee2cd7cee2e79558dbc8873404f6122d40a70cf71f0247304402205cf55018d39e3377ab843ec53be895934f59520e8268bbbdc18328a6d1d55c590220322de21da63e32fc2160db526979f7799d44c190c9810e0a20cd89e70cc61bb6012102cc30cb24a244910be0a343ee2cd7cee2e79558dbc8873404f6122d40a70cf71f00000000

This is the above transaction after it was broadcasted https://blockstream.info/testnet/tx/a86f0187be2eca4590a456e2423a2f5286b269c3b777846092ceb2fc44270aad.

How would I optimize this transaction creation script? I am mostly looking for whether or not some of the parameters are unecessary, like 'OP_DUP', 'OP_HASH160', 'OP_EQUALVERIFY', 'OP_CHECKSIG', SIGHASH_ALL, or SIGHASH_ANYONECANPAY?

1 Answer 1

0

This is the most optimized version of my script that I'm aware of:

from bitcoinutils.utils import to_satoshis
from bitcoinutils.setup import setup
from bitcoinutils.transactions import Transaction, TxInput, TxOutput
from bitcoinutils.script import Script
from bitcoinutils.keys import PrivateKey as utilPrivKey
from bitcoinutils.constants import SIGHASH_ALL

setup('testnet')

# private key for tb1qpeervdxrt2np74xhm4q4jjp76ey5f9fh9tf58f
sender_priv_key = utilPrivKey.from_wif('PRIVATE KEY FOR THE SENDING ADDRESS')
sender_pub_key = sender_priv_key.get_public_key().to_hex()
addrs_for_script = sender_priv_key.get_public_key().get_address()

# private key for tb1qj57tu40e9da959w4lj9s3njwteq2z89uy7kenh
receiver_priv_key = utilPrivKey.from_wif('PRIVATE KEY FOR THE RECEIVING ADDRESS')
receiver_addr = receiver_priv_key.get_public_key().get_segwit_address()

txin1 = TxInput("a86f0187be2eca4590a456e2423a2f5286b269c3b777846092ceb2fc44270aad", 1)
txin2 = TxInput("9019818039f181ed00046d682411719050a4b2dc1d1eb6aaa2d781df863522fc", 0)

script_code = Script(['OP_DUP', 'OP_HASH160', addrs_for_script.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG'])
msg1 = "69eb032167c25784108b9783c9d59ce23608d00238eb9e3c03c73cdd79f74528"
op_return_script = Script(["OP_RETURN", msg1])

txoutOpR = TxOutput(0, op_return_script)

# 0.00005 left for transaction fee
txout = TxOutput(to_satoshis(0.00036275), receiver_addr.to_script_pub_key())

tx = Transaction([txin1, txin2], [txoutOpR, txout], has_segwit=True)
txsign1 = sender_priv_key.sign_segwit_input(tx, 0, script_code, to_satoshis(0.00034416), SIGHASH_ALL)
tx.witnesses = [Script([txsign1, sender_pub_key])]
txsign2 = sender_priv_key.sign_segwit_input(tx, 1, script_code, to_satoshis(0.00006859), SIGHASH_ALL)
tx.witnesses.append(Script([txsign2, sender_pub_key]))

signed_tx = tx.serialize()
print("raw tx below this line")
print(signed_tx)
print("raw tx above this line")

This code worked, it produced this tx which was accepted by the blockchain:

02000000000102ad0a2744fcb2ce92608477b7c369b286522f3a42e256a49045ca2ebe87016fa80100000000fffffffffc223586df81d7a2aab61e1ddcb2a45090711124686d0400ed81f139808119900000000000ffffffff020000000000000000226a2069eb032167c25784108b9783c9d59ce23608d00238eb9e3c03c73cdd79f74528b38d000000000000160014953cbe55f92b7a5a15d5fc8b08ce4e5e40a11cbc0247304402203fb058c0afbf8d44158f89e95df13d434398d3a447978249d3860199e167fd62022077aed81b7e33726e2ace77318670d62eb74bf131cacbbf379a6b5925bc2935a0012102ad3b5a101ab6fed444f7b76f49b6f668f126a165aeb70162c16560f05cd53c3402473044022051b927dd9111894b6e440bd032d78584010f804f3d63f2a956145dc801e0c60e0220552bb84b5ed35e2ac0860805ca3e4819456ab707c973561e4e76bd04fcf48bd4012102ad3b5a101ab6fed444f7b76f49b6f668f126a165aeb70162c16560f05cd53c3400000000```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.