Skip to content

Commit 57ef95f

Browse files
author
Cris Perez
committed
New example: a transaction redeeming a P2PKH output and creating two new P2PKH outputs
1 parent 25f94dd commit 57ef95f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from bitcoin_tools.core.keys import load_keys
2+
from bitcoin_tools.core.transaction import TX
3+
4+
#################################################
5+
# Key loading #
6+
#################################################
7+
# ---------------------------------------------------------------------------------------------------------------------
8+
# The following piece of code loads an already generated ECDSA key pair from disk (check key_management.py if you
9+
# haven't generated a key pair yet).
10+
# - You should replace the Bitcoin address for the one that matches yours.
11+
# ---------------------------------------------------------------------------------------------------------------------
12+
13+
btc_addr = "mqrCarJrAvXrSQXpupd9i52hgYkaPVdyck"
14+
sk, pk = load_keys(btc_addr)
15+
16+
#################################################
17+
# Not that basic Raw transaction building #
18+
# P2PKH -> P2PKH, P2PKH (2 outputs) #
19+
#################################################
20+
# ---------------------------------------------------------------------------------------------------------------------
21+
# The following piece of code serves as an example of how to build a P2PKH transaction. Funds will be redeemed from the
22+
# already loaded Bitcoin address (Notice that, in order to work, there should be funds hold by the address).
23+
# - You will build a transaction that spends from a P2PKH output and generates a two new P2PKH outputs.
24+
# - You should change prev_tx_id, prev_out_index and value for the ones who match with an unspent transaction output
25+
# from your recently generated address.
26+
# - Choose a fee big enough to pay for the transaction inclusion into a block. You can use https://bitcoinfees.21.co/ to
27+
# figure out the current fee-per-byte ratio.
28+
# - Choose the transaction destination addresses.
29+
# - Build the transaction using the basic constructor.
30+
# - Sign and broadcast the transaction.
31+
# ---------------------------------------------------------------------------------------------------------------------
32+
33+
# Reference to the previous transaction output that will be used to redeem and spend the funds, consisting on an id and
34+
# an output index.
35+
prev_tx_id = "131b785c8afb42844fbc4d93566afa34b6ee457687033f818d6a301416994397"
36+
prev_out_index = 0
37+
38+
# Amount to be spent, in Satoshis, and the fee to be deduced (should be calculated).
39+
fee = 230 * 240
40+
value = [100000000, 66614329 - fee]
41+
42+
# Destination Bitcoin addresses where the values in bitcoins will be sent and locked until the owner(s) redeems them.
43+
destination_btc_addr = ["miWdbNn9zDLnKpcNuCLdfRiJx59c93bT8t", "mmp3aVcmdM9PKDj1FQZtBqK9nBnx1eNhPf"]
44+
45+
# First, we build our transaction from io (input/output) using the previous transaction references, the values, and the
46+
# destinations.
47+
tx = TX.build_from_io(prev_tx_id, prev_out_index, value, destination_btc_addr)
48+
# Finally, the transaction is signed using the private key associated with the Bitcoin address from each input.
49+
# Input 0 will be signed, since we have only created one.
50+
tx.sign(sk, 0)
51+
52+
# Once created we can display the serialized transaction. Transaction is now ready to be broadcast.
53+
print "hex: " + tx.serialize()
54+
55+
# Finally, we can analyze each field of the transaction.
56+
tx.display()

0 commit comments

Comments
 (0)