Friday, November 22, 2024

testnet – Python code to create a two-input transaction in take a look at web

I made a transaction that may be spent by anybody and now I wanna spend it and produce it again to my deal with. The issue in it does not have sufficient bitcoin for transaction payment so I am utilizing one other enter for that. That is my code:

def P2PKH_scriptPubKey(key):
    return [OP_DUP, OP_HASH160, Hash160(key), OP_EQUALVERIFY, OP_CHECKSIG]

def P2PKH_scriptSig(txin, txout, txin_scriptPubKey, private_key):
    signature = create_OP_CHECKSIG_signature(txin, txout, txin_scriptPubKey, private_key)
    public_key = private_key.pub
    return [signature, public_key]

def get_txout_scriptPubKeys(amount_to_send1, amount_to_send2):
    txout1_scriptPubKey = [OP_TRUE]
    txout2_scriptPubKey = [OP_FALSE]
    txout1 = create_txout(amount_to_send1, txout1_scriptPubKey)
    txout2 = create_txout(amount_to_send2, txout2_scriptPubKey)
    return txout1, txout2

def send_from_custom_transaction(amount_to_send, txid_to_spend, utxo_index, txin_scriptPubKey, txin_scriptSig, txout_scriptPubKey):
    txout = create_txout(amount_to_send, txout_scriptPubKey)
    txin = create_txin(txid_to_spend, utxo_index)
    new_tx = create_signed_transaction(txin, txout, txin_scriptPubKey, txin_scriptSig)
    return broadcast_transaction(new_tx)


def create_txin(txid, utxo_index):
    return CMutableTxIn(COutPoint(lx(txid), utxo_index))


def create_txout(quantity, scriptPubKey):
    return CMutableTxOut(quantity*COIN, CScript(scriptPubKey))


def create_OP_CHECKSIG_signature(txin, txout, txin_scriptPubKey, seckey):
    tx = CMutableTransaction([txin], txout)
    sighash = SignatureHash(CScript(txin_scriptPubKey), tx,
                            0, SIGHASH_ALL)
    sig = seckey.signal(sighash) + bytes([SIGHASH_ALL])
    return sig


def create_signed_transaction(txins, txouts, txin_scriptPubKey, txin_scriptSigs):
    tx = CMutableTransaction(txins, txouts)
    for i, txin in enumerate(txins):
        txin.scriptSig = CScript(txin_scriptSigs[i])
        VerifyScript(txin.scriptSig, CScript(txin_scriptPubKey[i]), tx, i, (SCRIPT_VERIFY_P2SH,))
    return tx


def broadcast_transaction(tx):
    raw_transaction = b2x(tx.serialize())
    headers = {'content-type': 'utility/x-www-form-urlencoded'}
    return requests.submit(
        'https://api.blockcypher.com/v1/btc/test3/txs/push',
        headers=headers,
        knowledge="{"tx": "%s"}" % raw_transaction,
    )

pv_key1 = "..."
pv_key2 = "..."  

my_private_key1 = bitcoin.pockets.CBitcoinSecret(pv_key1)
my_private_key2 = bitcoin.pockets.CBitcoinSecret(pv_key2)
my_public_key1 = my_private_key1.pub
my_public_key2 = my_private_key2.pub
my_address1 = bitcoin.pockets.P2PKHBitcoinAddress.from_pubkey(my_public_key1)
my_address2 = bitcoin.pockets.P2PKHBitcoinAddress.from_pubkey(my_public_key2)



def send_from_P2PKH_transaction(amount_to_send, txid_to_spend1, utxo_index1, txid_to_spend2, utxo_index2):
    txout_scriptPubKey = P2PKH_scriptPubKey(my_public_key2)
    txout = create_txout(amount_to_send, txout_scriptPubKey)
    
    txin1 = create_txin(txid_to_spend1, utxo_index1)
    txin2 = create_txin(txid_to_spend2, utxo_index2)
    
    txin_scriptPubKey1 = [OP_TRUE]
    txin_scriptPubKey2 = P2PKH_scriptPubKey(my_public_key2)
    
    txin1_scriptSig = []
    txin2_scriptSig = P2PKH_scriptSig(txin2, [txout], txin_scriptPubKey2, my_private_key2)
    
    new_tx = create_signed_transaction([txin2, txin1], [txout], [txin_scriptPubKey2, txin_scriptPubKey1], [txin2_scriptSig, txin1_scriptSig])
    return broadcast_transaction(new_tx)

all_money = 0.00015883 + 0.000001 
amount_to_send = 0.00000003
txid_to_spend1 = 'c21f572546f11ab142b3099db329c352937449c1757238c4461b6b13de927c7c'
utxo_index1 = 0
txid_to_spend2 = '38610341422c10bb433046e7bb200e4015af178d4031ab4be192c3a93a3c8363'  
utxo_index2 = 0

response = send_from_P2PKH_transaction(amount_to_send, txid_to_spend1, utxo_index1, txid_to_spend2, utxo_index2)


The issue is I get scriptPubKey returned false for the second enter and I do not know why.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles