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

0
76


I’ve the next code that efficiently 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')

# personal 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()

# personal 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 similar 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()

# That is random textual content 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 charges
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("uncooked tx under this line")
print(signed_tx)
print("uncooked tx above this line")

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

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

How would I optimize this transaction creation script? I’m principally on the lookout for whether or not or not a number of the parameters are unecessary, like ‘OP_DUP’, ‘OP_HASH160’, ‘OP_EQUALVERIFY’, ‘OP_CHECKSIG’, SIGHASH_ALL, or SIGHASH_ANYONECANPAY?

LEAVE A REPLY

Please enter your comment!
Please enter your name here