Sun. Jun 23rd, 2024

[ad_1]

I made a transaction that can be spent by anyone and now I wanna spend it and bring it back to my address. The problem in it doesn’t have enough bitcoin for transaction fee so I’m using another input for that. This 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(amount, scriptPubKey):
    return CMutableTxOut(amount*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.sign(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': 'application/x-www-form-urlencoded'}
    return requests.post(
        'https://api.blockcypher.com/v1/btc/test3/txs/push',
        headers=headers,
        data="{"tx": "%s"}" % raw_transaction,
    )

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

my_private_key1 = bitcoin.wallet.CBitcoinSecret(pv_key1)
my_private_key2 = bitcoin.wallet.CBitcoinSecret(pv_key2)
my_public_key1 = my_private_key1.pub
my_public_key2 = my_private_key2.pub
my_address1 = bitcoin.wallet.P2PKHBitcoinAddress.from_pubkey(my_public_key1)
my_address2 = bitcoin.wallet.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 problem is I get scriptPubKey returned false for the second input and I don’t know why.

[ad_2]

Source link

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *