The Cryptonomics™
  • Home
  • Blockchain
  • Bitcoin
  • Ethereum
  • NFTS
  • Altcoin
  • Mining
  • Consulting
Reading: testnet – Python code to create a two-input transaction in take a look at web
Share
Please enter CoinGecko Free Api Key to get this plugin works.
The Cryptonomics™The Cryptonomics™
Font ResizerAa
Search
  • Home
  • Blockchain
  • Bitcoin
  • Ethereum
  • NFTS
  • Altcoin
  • Mining
  • Consulting
Follow US
  • About Us
  • Advertising Solutions
  • Privacy
  • Terms
  • Advertise
Copyright © MetaMedia™ Capital Inc, All right reserved
The Cryptonomics™ > Bitcoin > testnet – Python code to create a two-input transaction in take a look at web
Bitcoin

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

admin
Last updated: May 29, 2024 9:06 am
admin Published May 29, 2024
Share
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 convey it again to my handle. The issue in it would not have sufficient bitcoin for transaction charge 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': 'software/x-www-form-urlencoded'}
    return requests.submit(
        'https://api.blockcypher.com/v1/btc/test3/txs/push',
        headers=headers,
        information="{"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.

You Might Also Like

Ethereum Reclaims NFT Market Dominance – Rises +30% In Gross sales

Agora Raises $50 Million to Speed up Growth of Its Stablecoin Initiative

Robinhood Below Florida Probe For Alleged Misleading Crypto Promotion

HBAR is obtainable for buying and selling!

Pudgy Penguins Set To Launch Pudgy Occasion NFT Sport In August

Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Torque Drift 2 devs stop after Web3 pivot; sport NFTs should not securities: Web3 Gamer Torque Drift 2 devs stop after Web3 pivot; sport NFTs should not securities: Web3 Gamer
Next Article What Is Grounding And Hallucination In AI? What Is Grounding And Hallucination In AI?
Leave a comment

Leave a Reply Cancel reply

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

Subscribe to our newslettern

Get Newest Articles Instantly!

- Advertisement -
Ad imageAd image
Popular News
Ethereum units sights on transformative zkEVM integration
Ethereum units sights on transformative zkEVM integration
The journey to a mature asset administration system
The journey to a mature asset administration system
High 3 Meme Coin Gems Price Shopping for Earlier than Could 2024 – PEPE, WIF, and DOGEVERSE
High 3 Meme Coin Gems Price Shopping for Earlier than Could 2024 – PEPE, WIF, and DOGEVERSE

Follow Us on Socials

We use social media to react to breaking news, update supporters and share information

Facebook Instagram Linkedin Pinterest Tiktok Twitter Youtube
The Cryptonomics™

Cryptonomics Magazine is your premier digital source for blockchain insights, offering cutting-edge research, news, interviews, and ICO updates for everyone from entrepreneurs to institutions. We drive blockchain knowledge and growth.

Subscribe to our newsletter

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

Ethereum units sights on transformative zkEVM integration
July 11, 2025
Altcoins roar as Bitcoin hits new all-time highs, Cardano and XRP surge over 10%
July 11, 2025
Did A Crypto Market Tremendous Cycle Begin? Right here’s How To Know
July 11, 2025
BIT Mining’s strategic leap into Solana propels inventory to 3-year excessive
July 11, 2025
Dogecoin (DOGE) Rockets to $0.20 — Can It Go Even Larger?
July 11, 2025
Copyright © The Cryptonomics™ , All right reserved
  • About Us
  • Advertising Solutions
  • Privacy
  • Terms
  • Advertise
Join Us!

Subscribe & Stay Ahead of the Curve with Cryptonomics !

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?