I need a sample program to create and sign a SegWit P2PKH testnet Bitcoin transaction on an offline computer, where I only need to fill in my private key, addresses, and other details. Thank you. #477
-
|
I am using the Python bitcoinlib library to create an offline Native SegWit (P2WPKH) transaction. The code I’m using is as follows: from bitcoinlib.transactions import Transaction def create_native_segwit_offline_transaction(prev_txid, prev_output_index, prev_output_value, from_privkey_wif, to_address, send_value, network='testnet'): tx.add_input(prev_txid, prev_output_index, keys=[from_privkey_wif], value=prev_output_value, witness_type='segwit') tx.add_output(send_value, to_address) tx.sign() return tx.as_hex() prev_txid = 'b759ffc1c3b85814ecb8f9333b6dc9329673db1cd02397612824a795a0022d18' raw_tx_hex = create_native_segwit_offline_transaction(prev_txid, prev_output_index, prev_output_value, from_privkey_wif, to_address, send_value) The transaction raw hex generated by this code is: However, after broadcasting it to a testnet node, I received an error from the node: Problem: I have confirmed the following: The prev_txid and prev_output_index correspond to a valid UTXO that exists and is unspent on the blockchain. The prev_output_value matches the value of that UTXO. The private key is in correct WIF format, and it corresponds to the address that holds the UTXO. The transaction format (hex) appears valid and follows the Native SegWit specification. My Question: What could be the possible cause of this error? Is there anything missing or incorrect in my code? 🔍Additional Info: I have used the native bitcoinlib methods to construct the transaction. I have included the UTXO value and specified witness_type='segwit'. However, the add_input() function in bitcoinlib does not seem to accept script_pub_key as a parameter. So my questions are: Do I need to provide the previous output’s scriptPubKey in another way? Or should I use a different tool to verify whether the signature in the transaction is valid? Any guidance would be greatly appreciated — thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
The previous transaction (b759ffc1c3b85814ecb8f9333b6dc9329673db1cd02397612824a795a0022d18) does not seem to exists, so it gives an error that the utxo cannot be found (bad-txns-inputs-missingorspent) |
Beta Was this translation helpful? Give feedback.
-
|
After you made changes to a transaction, for example with add_input or add_output, you need to update it. You could use the sign_and_update() method. With tx.verify() the transaction will be verified, I don't think this step is required, but it good to check before you try to push the transaction to the network. Also in your code example the 'testnet4' was not passed to the method, so I think the default 'testnet' was used. With the code below I got it working |
Beta Was this translation helpful? Give feedback.

After you made changes to a transaction, for example with add_input or add_output, you need to update it.
You could use the sign_and_update() method. With tx.verify() the transaction will be verified, I don't think this step is required, but it good to check before you try to push the transaction to the network.
Also in your code example the 'testnet4' was not passed to the method, so I think the default 'testnet' was used.
With the code below I got it working