Skip to content

Commit 360b250

Browse files
committed
Adds some examples the library needed for a long time. Still more to come
1 parent 2079a37 commit 360b250

File tree

7 files changed

+153
-72
lines changed

7 files changed

+153
-72
lines changed

bitcoin_tools/utxo_dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_min_input_size(out, height, count_p2sh=False):
7777
scriptSig = 1 + (req_sigs * 72) # OP_0 (1 byte) + 72 bytes per sig (PUSH sig (1 byte) + sig (71 bytes))
7878
scriptSig_len = int(ceil(scriptSig / float(256)))
7979
else:
80-
# All other types (including non standard P2MS, OP_Return and non-standard outs)
80+
# All other types (non-standard outs)
8181
scriptSig = -fixed_size - 1 # Those scripts are marked with length -1 and skipped in dust calculation.
8282
scriptSig_len = 0
8383

example.py

Lines changed: 0 additions & 71 deletions
This file was deleted.

examples/__init__.py

Whitespace-only changes.

examples/advanced_raw_tx_creation.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from bitcoin_tools.keys import load_keys
2+
from bitcoin_tools.transaction import TX
3+
4+
#################################################
5+
# Advanced Raw transaction building #
6+
# P2MS -> P2MS #
7+
#################################################
8+
# ---------------------------------------------------------------------------------------------------------------------
9+
# The following piece of code serves as an example of how to build a P2MS transaction. Funds will be redeemed from a
10+
# Pay-to-multisig address, and m-out of-n signatures will be required from different keys.
11+
# - The library store keys using a Bitcoin address as an identifier. If you need more information about generating and
12+
# storing / loading keys, refer to key_management.py / basic_raw_tx_creation.py examples.
13+
# - First of all we will select an unspent transaction output (UTXO) of type P2MS, by choosing the proper prev_tx_id and
14+
# index.
15+
# - Then we should define the amount to be transferred and the fees.
16+
# - Now we are ready to build the transaction. We can use the input/output constructor to do so.
17+
# - Finally, we should sign the transaction using all m-out of-n required private keys. Notice that the order of the in
18+
# which the keys are provided must match with the order in which the public keys where defined in the previous tx output
19+
# script.
20+
# - Finally we can serialize the transaction and display it to check that all worked!
21+
# ---------------------------------------------------------------------------------------------------------------------
22+
23+
# Reference to the previous transaction output that will be used to redeem and spend the funds, consisting on an id and
24+
# an output index.
25+
prev_tx_id = "adcd6d269d5e0713fa9650099e9ab54ebf845a0d95f3740b44361bdb287959a7"
26+
prev_out_index = 0
27+
28+
# Amount to be spent, in Satoshis, and the fee to be deduced (should be calculated).
29+
value = 6163910
30+
fee = 230 * 240
31+
32+
# Destination Bitcoin address where the value in bitcoins will be sent and locked until the owner redeems it.
33+
destination_btc_addr = "mwryy9YdVezq2Wo1DukA5ADhrNemqCKTmy"
34+
35+
# First, we build our transaction from io (input/output) using the previous transaction references, the value, and the
36+
# destination.
37+
tx = TX.build_from_io(prev_tx_id, prev_out_index, value - fee, destination_btc_addr)
38+
# Finally, the transaction is signed using the private key associated with the Bitcoin address from each input.
39+
# Input 0 will be signed, since we have only created one.
40+
tx.sign(sk, 0)
41+
42+
# Once created we can display the serialized transaction. Transaction is now ready to be broadcast.
43+
print "hex: " + tx.serialize()
44+
45+
# Finally, we can analyze each field of the transaction.
46+
tx.display()

examples/basic_raw_tx_creation.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from bitcoin_tools.keys import load_keys
2+
from bitcoin_tools.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 = "mwryy9YdVezq2Wo1DukA5ADhrNemqCKTmy"
14+
sk, pk = load_keys(btc_addr)
15+
16+
#################################################
17+
# Basic Raw transaction building #
18+
# P2PKH -> P2PKH #
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 new P2PKH output.
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 address.
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 = "7767a9eb2c8adda3ffce86c06689007a903b6f7e78dbc049ef0dbaf9eeebe075"
36+
prev_out_index = 0
37+
38+
# Amount to be spent, in Satoshis, and the fee to be deduced (should be calculated).
39+
value = 6163910
40+
fee = 230 * 240
41+
42+
# Destination Bitcoin address where the value in bitcoins will be sent and locked until the owner redeems it.
43+
destination_btc_addr = "mwryy9YdVezq2Wo1DukA5ADhrNemqCKTmy"
44+
45+
# First, we build our transaction from io (input/output) using the previous transaction references, the value, and the
46+
# destination.
47+
tx = TX.build_from_io(prev_tx_id, prev_out_index, value - fee, 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()

examples/key_management.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from bitcoin_tools.keys import generate_keys, store_keys
2+
from bitcoin_tools.wallet import generate_wif, generate_btc_addr
3+
4+
#################################################
5+
# Key management and Bitcoin address generation #
6+
#################################################
7+
# ---------------------------------------------------------------------------------------------------------------------
8+
# The following piece of code generates fresh keys and Bitcoin address.
9+
# - Both mainnet and testnet addresses can be generated. Tesnet are generated by default.
10+
# - Keys are stored in the folder defined in conf.py.
11+
# - WIF can be stored as a qr image or text. Image is set by default.
12+
# ---------------------------------------------------------------------------------------------------------------------
13+
14+
# First of all the ECDSA keys are generated.
15+
sk, pk = generate_keys()
16+
# Then, the Bitcoin address is derived from the public key created above.
17+
btc_addr = generate_btc_addr(pk)
18+
# Both the public and private key are stored in disk in pem format. The Bitcoin address is used as an identifier in the
19+
# name of the folder that contains both keys.
20+
store_keys(sk.to_pem(), pk.to_pem(), btc_addr)
21+
# Finally, the private key is encoded as WIF and also stored in disk, ready to be imported in a wallet.
22+
generate_wif(btc_addr, sk)
23+
24+
print "Keys for address " + btc_addr + " properly generated and stored."
25+
26+
27+

examples/tx_analysis.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from bitcoin_tools.transaction import TX
2+
3+
#################################################
4+
# Hex transaction analysis #
5+
#################################################
6+
7+
# ---------------------------------------------------------------------------------------------------------------------
8+
# The following piece of code parses a serialized transaction (hex encoded) transaction and displays all the information
9+
# related to it.
10+
# - Leftmost displayed transaction shows data as should be interpreted (human-readable), while rightmost
11+
# (surrounded by parenthesis) shows it as it is in the serialize transaction (can be used to identify it inside the
12+
# transaction)
13+
# - You should change the hex_tx for the one you'd like to deserialize. Serialized transaction can be obtain though
14+
# block explorers such as blockchyper or blockr.io, or by building a transaction using some of the library tools.
15+
# ---------------------------------------------------------------------------------------------------------------------
16+
17+
# First a transaction object is created (through the deserialize constructor) by deserializing the hex transaction we
18+
# have selected.
19+
hex_tx = "01000000013ca58d2f6fac36602d831ee0cf2bc80031c7472e80a322b57f614c5ce9142b71000000006b483045022100f0331d85cb7f7ec1bedc41f50c695d654489458e88aec0076fbad5d8aeda1673022009e8ca2dda1d6a16bfd7133b0008720145dacccb35c0d5c9fc567e52f26ca5f7012103a164209a7c23227fcd6a71c51efc5b6eb25407f4faf06890f57908425255e42bffffffff0241a20000000000001976a914e44839239ab36f5bc67b2079de00ecf587233ebe88ac74630000000000001976a914dc7016484646168d99e49f907c86c271299441c088ac00000000"
20+
tx = TX.deserialize(hex_tx)
21+
22+
# Then, the transaction can be displayed using the display method to analyze how it's been constructed.
23+
tx.display()

0 commit comments

Comments
 (0)