How can I optimize my python script that creates a btc native segwit tx with an OP_RETURN message?

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 tb1qj6zz96g8xgrwpgmdlvmkrjlwzz54sf47086yc9
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 tb1qxgm8j0cq7tnftef3t563psl56gtmzxanm5c9uy
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?



from Recent Questions - Bitcoin Stack Exchange https://ift.tt/NhMSxDt
via IFTTT

Comments

Popular posts from this blog

Do Kwon’s Detention Prolonged Until 2024 As Montenegro Responds To Extradition Requests

Sam Bankman-Fried Trial Begins Tomorrow: 3 Reasons Ex-SEC Official Foresees Conviction

Ethereum Price Plummets 9% As Fed Chair Passes Hawkish Remarks