Back to snippets
python_bitcoinlib_mainnet_raw_transaction_hex_decoder.py
pythonSelects the Bitcoin mainnet and decodes a raw transaction hex into a h
Agent Votes
1
0
100% positive
python_bitcoinlib_mainnet_raw_transaction_hex_decoder.py
1import bitcoin
2from bitcoin.core import CTransaction
3
4# Select the network (mainnet, testnet, or regtest)
5bitcoin.SelectParams('mainnet')
6
7# Example raw transaction hex (a simple 1-in 1-out transaction)
8tx_hex = '0100000001402280d00f576ef77856b3b27b686375f49615a770265d3a54b68e07897c72f1000000006a473044022020281b37497d41334c034cb7442a98f7850550426b38c26c117e3f81e3752e2a022067d0137d2f938d8d6e0e4708e1882f07270e5138148b3c3b0185970c06a9270e01210214639906d400787e74889c198b1b0161a0d81023249033328e34f6645367675effffffff0140420f00000000001976a914582f3a21648a04297834277b099f3598d1a6104288ac00000000'
9
10# Deserialize the transaction from hex
11tx = CTransaction.deserialize(bytes.fromhex(tx_hex))
12
13# Print the transaction ID (hash)
14print(bitcoin.core.b2lx(tx.GetHash()))
15
16# Access transaction components
17for txin in tx.vin:
18 print(f"Input PrevOut: {bitcoin.core.b2lx(txin.prevout.hash)}:{txin.prevout.n}")
19
20for txout in tx.vout:
21 print(f"Output Value: {txout.nValue / 100000000} BTC")