|
| 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() |
0 commit comments