-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (40 loc) · 1.74 KB
/
main.py
File metadata and controls
50 lines (40 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Bitcoin Transaction Signing Script (P2PKH)
# Author: YOUR_NAME
# License: MIT
# -------------------------------------------
# This script demonstrates how to construct and sign a Bitcoin transaction
# using a private key (WIF format) and raw transaction components.
from bitcoinlib.keys import Key
from bitcoinlib.transactions import Transaction
from bitcoinlib.services.services import Service
# Set the network: 'bitcoin' for mainnet, 'testnet' for testnet usage
network = 'testnet' # Change to 'bitcoin' for mainnet
# Private key in Wallet Import Format (WIF)
private_key_wif = 'cTxXXXXXXX_REPLACE_WITH_YOUR_TESTNET_WIF' # Example testnet WIF
# Previous UTXO details
prev_txid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
vout_index = 0
input_amount_satoshi = 100000 # 0.001 BTC
# Destination details
destination_address = 'tb1qxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Testnet address
send_amount_satoshi = 90000
fee_satoshi = input_amount_satoshi - send_amount_satoshi
# Build key from WIF
key = Key(import_key=private_key_wif, network=network)
# Create transaction
tx = Transaction(network=network)
tx.add_input(prev_txid, vout_index, input_amount_satoshi, address=key.address)
tx.add_output(send_amount_satoshi, destination_address)
# Sign transaction
tx.sign(keys=[key])
# Print the signed transaction
raw_signed_tx = tx.as_hex()
print("\n🔐 Raw Signed Transaction:")
print(raw_signed_tx)
# (Optional) Broadcast the transaction
broadcast = False # Set to True if you want to broadcast
if broadcast:
service = Service(network=network)
tx_id = service.sendrawtransaction(raw_signed_tx)
print("\n✅ Transaction successfully broadcasted!")
print("TXID:", tx_id)