Skip to content

Commit

Permalink
Updates examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
sr-gi committed Oct 2, 2017
1 parent e1f5aac commit 2df6415
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 326 deletions.
22 changes: 0 additions & 22 deletions bitcoin_tools/constants.py

This file was deleted.

Empty file removed bitcoin_tools/utxo/__init__.py
Empty file.
46 changes: 0 additions & 46 deletions bitcoin_tools/utxo/ldb_analysis.py

This file was deleted.

33 changes: 0 additions & 33 deletions bitcoin_tools/utxo/ldb_parser.py

This file was deleted.

202 changes: 0 additions & 202 deletions bitcoin_tools/utxo/utxo_dump.py

This file was deleted.

57 changes: 38 additions & 19 deletions examples/advanced_raw_tx_creation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from bitcoin_tools.core.keys import load_keys, serialize_pk
from bitcoin_tools.core.transaction import TX
from json import load

#################################################
# Advanced Raw transaction building #
Expand All @@ -9,37 +11,54 @@
# Pay-to-multisig address, and m-out of-n signatures will be required from different keys.
# - The library store keys using a Bitcoin address as an identifier. If you need more information about generating and
# storing / loading keys, refer to key_management.py / basic_raw_tx_creation.py examples.
# - First of all we will select an unspent transaction output (UTXO) of type P2MS, by choosing the proper prev_tx_id and
# index.
# - Then we should define the amount to be transferred and the fees.
# - Now we are ready to build the transaction. We can use the input/output constructor to do so.
# - First of all we will select an unspent transaction output (UTXO) of type P2MS. This time, we will load it from a
# file.
# - Then the fees will be set.
# - Once all the data is defined, we will be ready to build the transaction. We can use the input/output constructor to
# do so.
# - Finally, we should sign the transaction using all m-out of-n required private keys. Notice that the order of the in
# which the keys are provided must match with the order in which the public keys where defined in the previous tx output
# script.
# - Finally we can serialize the transaction and display it to check that all worked!
# - Finally we wil serialize the transaction and display it to check that all worked!
# ---------------------------------------------------------------------------------------------------------------------

# Reference to the previous transaction output that will be used to redeem and spend the funds, consisting on an id and
# an output index.
prev_tx_id = "adcd6d269d5e0713fa9650099e9ab54ebf845a0d95f3740b44361bdb287959a7"
prev_out_index = 0
# Loads the UTXO data from a json file. You can create your own file based on the provided example with UTXOs from keys
# own. It won't work if you don't update it.
utxo = load(open('example_utxos/P2MS_utxo.json', 'r'))

# Amount to be spent, in Satoshis, and the fee to be deduced (should be calculated).
value = 6163910
# Get the previous transaction id and index, as well as the source bitcoin address.
prev_tx_id = str(utxo.get('tx_id'))
prev_out_index = utxo.get('index')

# Load the keys from the loaded addresses (you should have them!). Notice that since we are going to create a P2MS tx
# from a P2MS UTXO, several keys will be required.
source_btc_addrs = utxo.get('btc_addr')
keys = map(load_keys, source_btc_addrs)
sks = [k[0] for k in keys]
pks = [k[1] for k in keys]

# Amount to be spent, in Satoshi, and the fee to be deduced (should be calculated).
value = utxo.get('value')
fee = 230 * 240

# Destination Bitcoin address where the value in bitcoins will be sent and locked until the owner redeems it.
destination_btc_addr = "mwryy9YdVezq2Wo1DukA5ADhrNemqCKTmy"
# Now we can build the transaction from inputs/outputs.
# We will create a 2-3 P2MS from a 1-3 P2MS, so we need to include 1 signature to redeem the previous utxo, and include
# a 2-3 script to create the new one.

# To create the destination script, we need to include first the number of required signatures (2), and then, the
# all the public keys.
destination = [2, serialize_pk(pks[0]), serialize_pk(pks[1]), serialize_pk(pks[2])]
# Using map will also do the trick.
# destination = [2] + map(serialize_pk, pks)

# First, we build our transaction from io (input/output) using the previous transaction references, the value, and the
# destination.
tx = TX.build_from_io(prev_tx_id, prev_out_index, value - fee, destination_btc_addr)
# Finally, the transaction is signed using the private key associated with the Bitcoin address from each input.
# Input 0 will be signed, since we have only created one.
tx.sign(sk, 0)
# Now we can create the transaction. Since we owe all the keys, we can choose any one of the tree to sign it.
tx = TX.build_from_io(prev_tx_id, prev_out_index, value - fee, destination)
tx.sign(sks[0], 0)

# Once created we can display the serialized transaction. Transaction is now ready to be broadcast.
print "hex: " + tx.serialize()

# Finally, we can analyze each field of the transaction.
tx.display()


Loading

0 comments on commit 2df6415

Please sign in to comment.