edgecase
Author: StJohn Piano
Published: 2019-07-02
Datafeed Article 115
This article has been digitally signed by Edgecase Datafeed.
This article has been digitally signed by its author.
14556 words - 5174 lines - 130 pages





GOAL



Assemble a set of standard Bitcoin transactions, including the private keys, for use in future tests of Bitcoin software. These transactions must have already been added to the Bitcoin blockchain.




CONTENTS



- Goal
- Contents
- Summary
- Notes
- Bitcoin Transaction Test Set
- Project Log






SUMMARY





Actions:
- Read through previous articles.
- Copied excerpts in which standard Bitcoin transactions were constructed.
- Extracted the inputs and the final signed forms of these transactions.
- Re-constructed the transactions from the inputs.
- Confirmed that the new transactions are identical to the original ones.
- Confirmed that the new transactions are identical to the ones on the Bitcoin blockchain.

Summaries of the transactions can be found in the Bitcoin Transaction Test Set section.

Transaction List:
- Transaction 1: 1 input, 1 output
- Transaction 2: 1 input, 1 output
- Transaction 3: 1 input, 2 outputs
- Transaction 4: 1 input, 2 outputs
- Transaction 5: 2 inputs, 1 output

To construct the transactions, I followed the recipe in this article:
Recipe for creating and signing a standard Bitcoin transaction
but I used the set of assets downloaded from
Creating a Bitcoin transaction with two outputs.




Notes section:

The Notes section contains the following parts:

- Terms and acryonyms
- Services used during this project
- Adjustment to transaction_2.py
- Constructing standard Bitcoin transactions








NOTES





Parts


- Terms and acryonyms
- Services used during this project
- Adjustment to transaction_2.py
- Constructing standard Bitcoin transactions




Terms and acryonyms


- tx = transaction
- txid = transaction id




Services used during this project


A service for looking up the raw hex form of a transaction:
http://blockchain.info/tx/[txid]?format=hex
Example:
http://blockchain.info/tx/745e224ccba0a033c55ea80523f207da18b903418ac1f5d293eed62c19e0334d?format=hex




Adjustment to transaction_2.py


In the output of create_transaction_2.py, a description of each input is included. This description is generated in the Input class in transaction_2.py.

Example:

Input 1:
Input: {40 bytes unsigned, 66 bytes signable, 179 bytes signed}

[...]

- [destination, goes into scriptSig] public_key_hex: 043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9 (65 bytes)


The word "destination" should be "source". The public_key_hex value corresponds to the address from which bitcoin is being sent.




Constructing standard Bitcoin transactions


My working definition of a standard transaction:
- It has at least one input and at least one output.
- All input and output addresses are Pay-To-Public-Key-Hash (P2PKH).
- All input scriptSigs contain uncompressed public keys.


From these articles:
- Recipe for creating and signing a standard Bitcoin transaction
- Creating a Bitcoin transaction with two outputs
- Creating a Bitcoin transaction with two inputs
I know that the following pieces of information are the necessary input and output components for a standard Bitcoin transaction:

1) For each input:
- The txid of the previous transaction that transferred the unspent output to the input address. When its byte order is reversed, it becomes the "previous_output_hash".
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
- The private key (in hex bytes) of the input address.
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
- A 32-byte random value (1 per input). The transaction will be signed once using each input and each signature requires 32 bytes of entropy.

2) For each output:
- The output address.
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".

3) For the whole transaction:
- The fee (in satoshi). The fee must be an integer e.g.
"225"
.










BITCOIN TRANSACTION TEST SET





Transaction list


- Transaction 1: 1 input, 1 output
- Transaction 2: 1 input, 1 output
- Transaction 3: 1 input, 2 outputs
- Transaction 4: 1 input, 2 outputs
- Transaction 5: 2 inputs, 1 output




Transaction 1


Source: Creating and signing a standard raw Bitcoin transaction
Transaction name in source: tx1


Input data:

Input 1:
- The txid of the previous transaction that transferred the unspent output to the input address.
8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero.
9
- The private key (in hex bytes) of the input address:
0000000000000000000000007468655f6c6962726172795f6f665f626162656c
- The bitcoin amount (or satoshi amount) contained within the unspent output (the "input amount").
0.002 bitcoin
- A 32-byte random value. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
72616e646f6d5f627974655f737472696e675f61786178617861735f6d6c6f

Output 1:
- The output address.
12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
199775 satoshi

The fee:
- The fee is an integer (e.g. "225") number of satoshi.
225 satoshi



Output data: tx (signed)
0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000



Mined txid (txid of tx in the Bitcoin blockchain):
4bdd5f653d0100ea4735953e8e22e92321af9074926b3bd33c279c289f2d975a





Transaction 2


Source: Creating and signing a standard raw Bitcoin transaction: Iteration #2
Transaction name in source: tx1


Input data:

Input 1:
- The txid of the previous transaction that transferred the unspent output to the input address.
e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero.
8
- The private key (in hex bytes) of the input address:
00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965
- The bitcoin amount (or satoshi amount) contained within the unspent output (the "input amount").
0.00242000 bitcoin
- A 32-byte random value. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
00000000005468652046616365206f6620476f64206468636d726c636874646a

Output 1:
- The output address.
13xPBB175FtPbPQ84iB8KuawaVy3mHrady
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
0.002 bitcoin

The fee:
- The fee is an integer (e.g. "225") number of satoshi.
223 satoshi


Note: The actual amount sent to the output 1 address in the transaction is 241777 satoshi.


Output data: tx (signed)
01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000



Mined txid (txid of tx in the Bitcoin blockchain):
745e224ccba0a033c55ea80523f207da18b903418ac1f5d293eed62c19e0334d





Transaction 3


Source: Creating a Bitcoin transaction with two outputs
Transactions: tx1


Input data:

Input 1:
- The txid of the previous transaction that transferred the unspent output to the input address.
f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero.
12
- The private key (in hex bytes) of the input address:
7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5
- The bitcoin amount (or satoshi amount) contained within the unspent output (the "input amount").
0.006 bitcoin (600000 satoshi)
- A 32-byte random value. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
257c2ff4ac1606d5e42fe152c2624cffac2aa58cdfe8578a69b12beefbce68b9

Output 1:
- The output address.
1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
0.003 bitcoin

Output 2:
- The output address.
1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
0.00000001 bitcoin

The fee:
- The fee is an integer (e.g. "225") number of satoshi.
774 satoshi


Note: The actual amount sent to the output 2 address in the transaction is 299226 satoshi.


Output data: tx (signed)
01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000



Mined txid (txid of tx in the Bitcoin blockchain):
db2c3d84708cd9d0e40ae1754021f9146a0d6ab555fc0e1d547d7876c0c092f4





Transaction 4


Source: Creating a Bitcoin transaction with two inputs
Transaction name in source: tx1


Input data:

Input 1:
- The txid of the previous transaction that transferred the unspent output to the input address.
1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero.
3
- The private key (in hex bytes) of the input address:
7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8
- The bitcoin amount (or satoshi amount) contained within the unspent output (the "input amount").
0.004 bitcoin (400000 satoshi)
- A 32-byte random value. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
84425ee562f20d7493420c97eea153d37891eb63ce51fa24f51bfc7646353d32

Output 1:
- The output address.
1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
0.002 bitcoin (200000 satoshi)

Output 2:
- The output address.
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
0.002 bitcoin (200000 satoshi)

The fee:
- The fee is an integer (e.g. "225") number of satoshi.
386 satoshi


Note: The actual amount sent to the output 2 address in the transaction is 199614 satoshi.


Output data: tx (signed)
0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000



Mined txid (txid of tx in the Bitcoin blockchain):
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826





Transaction 5


Source: Creating a Bitcoin transaction with two inputs
Transaction name in source: tx2


Input data:

Input 1:
- The txid of the previous transaction that transferred the unspent output to the input address.
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero.
0
- The private key (in hex bytes) of the input address:
cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f
- The bitcoin amount (or satoshi amount) contained within the unspent output (the "input amount").
0.002 bitcoin (200000 satoshi)
- A 32-byte random value. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
5ab5bbe6fdc861a461a6721b2953d32be23c233e5ad5dc08d7c13daa95adbc17

Input 2:
- The txid of the previous transaction that transferred the unspent output to the input address.
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero.
1
- The private key (in hex bytes) of the input address:
7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af
- The bitcoin amount (or satoshi amount) contained within the unspent output (the "input amount").
0.00199614 bitcoin (199614 satoshi)
- A 32-byte random value. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
75fc65686a47326556735be91491a06f380a9db682fe9d50f82ad8d8d8849c2f

Output 1:
- The output address.
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s
- The bitcoin amount (or satoshi amount) that will be transferred to the output address (the "output amount").
0.00399614 bitcoin (399614 satoshi)

The fee:
- The fee is an integer (e.g. "225") number of satoshi.
443 satoshi


Note: The actual amount sent to the output address in the transaction is 399171 satoshi.


Output data: tx (signed)
01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000



Mined txid (txid of tx in the Bitcoin blockchain):
92616e432f7807bf6d93252fcdca7efb139e56f7aa34835b50670ebc6a4b5649

















PROJECT LOG





System details:
- Name: Shovel
- Specifications: HP 6005 Pro SFF. 3 GHz x86_64 processor (AMD II x4 B95 Quad Core), 4 GB RAM, 1 TB hard drive. Running CentOS 7.6.1810 (Core).
- More information: New computer: Shovel
- Installed items: GCC 4.8.5, Make 3.82, Vim 7.4, Python 2.7.5, Python 3.3.2, Gnome 3.28.2, gedit 3.28.1, GPG 1.4.10.




My working definition of a standard transaction:
- It has at least one input and at least one output.
- All input and output addresses are Pay-To-Public-Key-Hash (P2PKH).
- All input scriptSigs contain uncompressed public keys.




From these articles:
- Recipe for creating and signing a standard Bitcoin transaction
- Creating a Bitcoin transaction with two outputs
- Creating a Bitcoin transaction with two inputs
I know that the following pieces of information are the necessary input and output components for a standard Bitcoin transaction:

1) For each input:
- The txid of the previous transaction that transferred the unspent output to the input address.
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
- The private key (in hex bytes) of the input address.
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.

2) For each output:
- The output address.
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".

3) For the whole transaction:
- The fee (in satoshi) or fee_rate (in satoshi / byte). The fee must be an integer e.g.
"225"
. The fee_rate must be an integer e.g.
"1"
or a float e.g.
"1.2"
.



For testing purposes, better to avoid floats. I'll use "fee" but not "fee rate".




I'll gather the standard transactions that I have already constructed and published.





Source: Creating and signing a standard raw Bitcoin transaction
Transactions: tx1


Excerpts:


Source address:

aineko:work stjohnpiano$ python generate_bitcoin_address.py


- INPUT: private_key_bytes: the_library_of_babel
- private_key_hex: 7468655f6c6962726172795f6f665f626162656c
- 32-byte private_key_hex: 0000000000000000000000007468655f6c6962726172795f6f665f626162656c
- private key lies within input domain of secp256k1 elliptic curve.
- private_key_WIF: 5HpHagT65TZzG1PH3CdWbVZG75U59A8FphH7EtKwbFCZf3p6bjg
- public_key_hex: 04e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76d
- Bitcoin address: 1CTumCMjzBfccCJBTkHoPQmAwEqU9Uj2sQ



Target address:

aineko:work stjohnpiano$ python generate_bitcoin_address.py


- INPUT: private_key_bytes: the_eye_of_argon
- private_key_hex: 7468655f6579655f6f665f6172676f6e
- 32-byte private_key_hex: 000000000000000000000000000000007468655f6579655f6f665f6172676f6e
- private key lies within input domain of secp256k1 elliptic curve.
- private_key_WIF: 5HpHagT65TZzG1PH3CSu63k9qfR2Vx3KfTXx3ojN7NvuJvT5uDe
- public_key_hex: 040567b4b55f25ddaff277931f00f2ccc131a3f342eb08563ee61b993b5f321c39b796a3c641185a58f1a7012a5d7469cb78728f9312dba33788a4ac98285f806a
- Bitcoin address: 12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f



Source address:
1CTumCMjzBfccCJBTkHoPQmAwEqU9Uj2sQ

Target address:
12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f


[...]


Let tx0 be the transaction that moved bitcoin from my LocalBitcoins account to the source address.

Let tx1 be the transaction that moved bitcoin from the source address to the target address.


[...]


For tx1:
- Transaction fee was 225 satoshis.
- Transaction size was 223 bytes.


[...]


txid0 (little-endian, used in block explorers):
8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce


txid0 (big-endian form, produced by the double SHA256 hash of the signed transaction):
ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284



txid1 (big-endian form, produced by the double SHA256 hash of the signed transaction):
5a972d9f289c273cd33b6b927490af2123e9228e3e953547ea00013d655fdd4b


txid1 (little-endian, used in block explorers):
4bdd5f653d0100ea4735953e8e22e92321af9074926b3bd33c279c289f2d975a



[...]


The little-endian forms of the txids can be used to look up the transactions on a block explorer web service.


[...]


tx1:
live.blockcypher.com/btc/tx/4bdd5f653d0100ea4735953e8e22e92321af9074926b3bd33c279c289f2d975a


[...]


tx1 (signed):

Expected format (raw hex bytes):
- version: 4 bytes (little-endian) = 01 00 00 00
- input_count: (var_int) = 01
- inputs:
-- previous_output_hash: 32 bytes (big-endian) = ce b4 93 94 1b f4 46 71 99 4f 85 3f e8 58 5f 33 00 70 95 66 40 bd 30 fe 88 5d 42 91 83 e4 82 84
-- previous_output_index: 4 bytes (little-endian) = 09 00 00 00
-- script_length: (var_int)
-- scriptSig = [see below]
-- sequence: 4 bytes (little-endian) = ff ff ff ff
- output_count: (var_int) = 01
- outputs:
-- value: 8 bytes (little-endian) = 5f 0c 03 00 00 00 00 00
-- script_length: (var_int)
-- scriptPubKey = [see below]
- block lock time: 4 bytes = 00 00 00 00



scriptSig:
- PUSHDATA: 47
- [derived property] PUSHDATA decimal value: 71
- signature_data: 30 44 02 20 6a 2e ea 0c 90 8e fb d7 80 a3 4c 48 b4 8d 80 94 6a 6e 74 0c 27 24 68 42 7b ad a3 ba 97 73 d3 9a 02 20 76 01 70 28 c4 38 60 20 b1 9b 7d 1f 16 40 55 8c d9 02 ae ff bf bd 85 f4 63 7a 8b e0 e9 2e e1 de 01
- PUSHDATA: 41
- [derived property] PUSHDATA decimal value: 65
- public_key_data: 04 e8 ad e6 6f 2c c0 e4 30 73 f4 cc ea 47 db 27 9b ba b1 a5 e3 0a 6e 8b a4 9f 12 53 8b 21 5c 5b 9e 0d 28 bd 08 0d 35 fd e8 78 08 1e 8f 05 db c2 3e eb a0 2b 54 4f a8 3e 6d 13 b5 f2 14 56 81 e7 6d



scriptPubKey:
- OP_DUP: 76
- OP_HASH160: a9
- PUSHDATA: 14
- [derived property] PUSHDATA decimal value: 20
- public_key_hash: 0f 9e e7 85 22 f6 cc 8a 88 78 4a e0 2b 04 08 e4 52 d8 02 59
- OP_EQUALVERIFY: 88
- OP_CHECKSIG: ac




Insert the scriptSig and scriptPubKey byte sequences into transaction 1. Also calculate and insert their byte length var_ints.



tx1 (signed):

Expected format (raw hex bytes):
- version: 4 bytes (little-endian) = 01 00 00 00
- input_count: (var_int) = 01
- inputs:
-- previous_output_hash: 32 bytes (big-endian) = ce b4 93 94 1b f4 46 71 99 4f 85 3f e8 58 5f 33 00 70 95 66 40 bd 30 fe 88 5d 42 91 83 e4 82 84
-- previous_output_index: 4 bytes (little-endian) = 09 00 00 00
-- script_length: (var_int) = 8a
-- scriptSig = 47 30 44 02 20 6a 2e ea 0c 90 8e fb d7 80 a3 4c 48 b4 8d 80 94 6a 6e 74 0c 27 24 68 42 7b ad a3 ba 97 73 d3 9a 02 20 76 01 70 28 c4 38 60 20 b1 9b 7d 1f 16 40 55 8c d9 02 ae ff bf bd 85 f4 63 7a 8b e0 e9 2e e1 de 01 41 04 e8 ad e6 6f 2c c0 e4 30 73 f4 cc ea 47 db 27 9b ba b1 a5 e3 0a 6e 8b a4 9f 12 53 8b 21 5c 5b 9e 0d 28 bd 08 0d 35 fd e8 78 08 1e 8f 05 db c2 3e eb a0 2b 54 4f a8 3e 6d 13 b5 f2 14 56 81 e7 6d
-- sequence: 4 bytes (little-endian) = ff ff ff ff
- output_count: (var_int) = 01
- outputs:
-- value: 8 bytes (little-endian) = 5f 0c 03 00 00 00 00 00
-- script_length: (var_int) = 19
-- scriptPubKey = 76 a9 14 0f 9e e7 85 22 f6 cc 8a 88 78 4a e0 2b 04 08 e4 52 d8 02 59 88 ac
- block lock time: 4 bytes = 00 00 00 00



tx1 (signed):

01 00 00 00 01 ce b4 93 94 1b f4 46 71 99 4f 85 3f e8 58 5f 33 00 70 95 66 40 bd 30 fe 88 5d 42 91 83 e4 82 84 09 00 00 00 8a 47 30 44 02 20 6a 2e ea 0c 90 8e fb d7 80 a3 4c 48 b4 8d 80 94 6a 6e 74 0c 27 24 68 42 7b ad a3 ba 97 73 d3 9a 02 20 76 01 70 28 c4 38 60 20 b1 9b 7d 1f 16 40 55 8c d9 02 ae ff bf bd 85 f4 63 7a 8b e0 e9 2e e1 de 01 41 04 e8 ad e6 6f 2c c0 e4 30 73 f4 cc ea 47 db 27 9b ba b1 a5 e3 0a 6e 8b a4 9f 12 53 8b 21 5c 5b 9e 0d 28 bd 08 0d 35 fd e8 78 08 1e 8f 05 db c2 3e eb a0 2b 54 4f a8 3e 6d 13 b5 f2 14 56 81 e7 6d ff ff ff ff 01 5f 0c 03 00 00 00 00 00 19 76 a9 14 0f 9e e7 85 22 f6 cc 8a 88 78 4a e0 2b 04 08 e4 52 d8 02 59 88 ac 00 00 00 00



tx1 (signed):
0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000


It is 223 bytes long.


[...]


Next: previous_output_index

Manually count the outputs of this transaction on the transaction info page.
blockchain.info/tx/8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce
Result = 20
Outputs are implicitly 0-indexed.

My source address is the 10th from the top, so it has an index of 9. I am assuming that blockchain.info displays the output addresses in the same order in which they are stored within the raw transaction and that index=0 is at the top of the list.

So: previous_output_index = 9

In hex, this is 09. It needs to be 4 bytes (little-endian), so add 3 leading zero bytes and reverse it.

So:

previous_output_index = 09 00 00 00


Finally: value

Currently, the source address contains 0.002 bitcoin or 200000 satoshis.

I'll assume the median transaction size of 225 bytes.

At 1 satoshi/byte, a 225-byte transaction will include a transaction fee of 225 satoshis.

The value will be the current amount minus the fee.

200000 - 225 = 199775

value = 199775

I might adjust this a bit depending on how big the actual new transaction turns out to be.

The value needs to be 8 bytes (little-endian).

>>> hex(199775)
>>> '0x30c5f'


Result: 30c5f
Add a 0 at the front to make this hex value to be of even length (i.e. full bytes).
030c5f
03 0c 5f

Add 5 leading zero bytes to make this value 8 bytes long.
00 00 00 00 00 03 0c 5f

Now reverse it to make it little-endian.
5f 0c 03 00 00 00 00 00


So, value = 5f 0c 03 00 00 00 00 00


[...]


aineko:work stjohnpiano$ python

Python 2.7.13 (default, Dec 18 2016, 05:35:59)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

[...]

>>> from binascii import hexlify, unhexlify

[...]

>>> k_bytes = "random_byte_string_axaxaxas_mlo"
>>> n_hex = "FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141"
>>> n_hex = n_hex.replace(' ','')
>>> n_int = int(n_hex, 16)
>>> n_int
>>> 115792089237316195423570985008687907852837564279074904382605163141518161494337L

>>> k_int = int(hexlify(k_bytes), 16)
>>> k_int
>>> 202093010951575558137711783426031372078737853412148247763075567776185543791L

>>> k_int <= (n_int - 1)
>>> True

[...]






Need to get random value
k
as a 32-byte hex bytestring.


[spiano@localhost ~]$ python

Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from binascii import hexlify
>>> k_bytes = "random_byte_string_axaxaxas_mlo"
>>> k_hex = hexlify(k_bytes)
>>> k_hex
'72616e646f6d5f627974655f737472696e675f61786178617861735f6d6c6f'



32-byte random value for signing tx1:
72616e646f6d5f627974655f737472696e675f61786178617861735f6d6c6f








Summary


Source: Creating and signing a standard raw Bitcoin transaction
Transaction: tx1

Inputs:

1) For each input:
- The txid of the previous transaction that transferred the unspent output to the input address.
8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
9
- The private key (in hex bytes) of the input address:
0000000000000000000000007468655f6c6962726172795f6f665f626162656c
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
0.002 bitcoin
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
72616e646f6d5f627974655f737472696e675f61786178617861735f6d6c6f

2) For each output:
- The output address.
12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
199775 satoshi

3) For the whole transaction:
- The fee (in satoshi). The fee must be an integer e.g.
"225"
.
225 satoshi



Output: tx1 (signed)
0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000



End Summary









Next: Let's try out this test case.





I've already created a project directory named "bitcoin_transaction_test_set".

Within the project directory, create a work directory named "work".




Browse to Creating a Bitcoin transaction with two outputs

Go to the Downloadable Assets section.

Excerpt:

Transaction creation toolset (standard transactions):
- bitcoin_functions_2.py
- bjorn_edstrom_ripemd160.py
- create_transaction_2.py
- ecdsa-0.10.tar.gz
- pypy_sha256.py
- transaction_2.py


These assets are all linked in the Downloadable Assets section. Download them.

Place the downloaded assets into the work directory.



I'll follow the recipe in this article:
Recipe for creating and signing a standard Bitcoin transaction
but I'll use the set of assets downloaded from
Creating a Bitcoin transaction with two outputs.



I'll set the change address to be the output address.


Open a terminal. Change directory to the work directory.


Unpack the zipped tape archive file ecdsa-0.10.tar.gz, using the command
tar -zxvf ecdsa-0.10.tar.gz


This unpacking produces a new directory named "ecdsa-0.10" in the work directory. The directory "ecdsa-0.10" contains a directory named "ecdsa". Copy the "ecdsa" directory into the work directory.



Open the file create_transaction_2.py in a text editor.

Scroll to lines 44-79, which should be the section of text that lies between
##### START CONTROLS

and
##### END CONTROLS


Note: In create_transaction_2.py, all control values are strings (i.e. are contained within quotation marks).


Set the variable
random_value
to the 32-byte random value.

Set the variable
random_value_type

to
"hex_bytes"
.


The variable
input_data
is a list that contains one or more inputs. Inputs are represented as dictionaries (key-value stores).

For each input dictionary (everything between and including a pair of braces {}):

- Set the value of the key
"txid"
to be the txid.
8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce

- Set the value of the key
"previous_output_index"
to be the index of the unspent output.
9

- Set the value of the key
"private_key_hex"
to be the private key.
0000000000000000000000007468655f6c6962726172795f6f665f626162656c

- If the input amount is in satoshi, set the value of the key
"satoshi_amount"
to be the input amount.

- If the input amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the input amount.
0.002 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set, then
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.


The variable
output_data
is a list that contains one or more outputs. Outputs are represented as dictionaries (key-value stores).

For each output dictionary (everything between and including a pair of braces {}):

- Set the value of the key
"address"
to be the output address.
12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f

- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.
199775 satoshi

- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set,
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.

Note: You must include an output that is directed to the change address.



Set the variable
change_address
to be the output address.
12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f


If you have chosen to set the fee as an absolute number of satoshi, set the variable
fee
to be the fee amount and set the variable
fee_type
to be
"fee"
.
225 satoshi






Here is how the controls look:


create_transaction_2.py

python 2.7.13
	##### START CONTROLS
	# Note: Hex characters in input variable values must be lowercase. 
	random_values = [
		"72616e646f6d5f627974655f737472696e675f61786178617861735f6d6c6f",	
	]
	# random_value must be between 1 and 32 bytes. If random_value_type is "raw_bytes", then random_value must be between 1 and 32 ASCII characters. If random_value_type is "hex_bytes", then random_value must be an even number of hex characters and between 2 and 64 hex characters (1 byte is represented by 2 hex characters). 
	# Note: Every ECDSA signature (one for each input in a transaction) requires new random entropy. 
	# random_value_type options: ["raw_bytes", "hex_bytes"]
	random_value_type = "hex_bytes"
	input_data = [
		{
		"txid": "8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce",
		"previous_output_index": "9",
		"private_key_hex": "0000000000000000000000007468655f6c6962726172795f6f665f626162656c",
		#"satoshi_amount": "242000", 
		"bitcoin_amount": "0.002", 
		},
	]
	output_data = [
		{
		"address": "12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f",
		"satoshi_amount": "199775", 
		#"bitcoin_amount": "0.003", 
		},
	]
	# note: all inputs and outputs are assumed to be Pay-To-Public-Key-Hash (P2PKH). 
	change_address = "12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f"
	# note: the fee will be subtracted from the amount that is being sent to the change address.
	fee = "225" # satoshi
	fee_rate = "3" # satoshi / byte
	# fee_type options: ["fee", "fee_rate"]
	fee_type = "fee"
	##### END CONTROLS




In the terminal, in the work directory, run the following command:
python create_transaction_2.py


At the end, the output should contain the signed transaction as a hex byte sequence without spaces.




[spiano@localhost work]$ python create_transaction_2.py


### START CREATION OF BITCOIN TRANSACTION

- Fee type: fee
- Fee: 225 (satoshi)
- Number of inputs (i.e. as-yet-unspent outputs): 1
- Number of outputs: 1
- Change address: 12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f
- Amount to be sent to the change address: 0.00199775
- Input addresses, with total-value-to-be-sent:
-- 1CTumCMjzBfccCJBTkHoPQmAwEqU9Uj2sQ: 0.00200000
- Output addresses, with total-value-to-be-received:
-- 12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f: 0.00199775
- Total value of all inputs: 0.00200000
- Total value of all outputs: 0.00199775
- Total value of all inputs is greater than total value of all outputs.
-- Some input value has not been assigned to an output.
-- Extra value: 0.00000225
-- This extra value will be sent to the change address.
-- Change address: 12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f
-- New amount to be sent to change address: 0.00200000
- Estimated transaction size: 223 bytes
- Final fee: 225 (satoshi)
- Final fee rate (using estimated transaction size): 1.0090 (satoshi per byte)
- Fee subtracted from amount-to-be-sent-to-change-address.
- New amount to be sent to change address: 199775 (satoshi)
- Size of signed transaction: 223 bytes
- (signed_tx_size - estimated_tx_size): 0 bytes
- Fee rate in signed transaction: 1.009 (satoshi / byte)

Input 0:
Input: {40 bytes unsigned, 66 bytes signable, 179 bytes signed}
- [data] previous_output_hash: ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284 (32 bytes)
- [data] previous_output_index: 09000000 (4 bytes)
- [signed form] script_length: 8a (1 bytes)
- [signed form] scriptSig: 47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76d (138 bytes)
- [data] sequence: ffffffff (4 bytes)
- [used for signing] private_key_hex: 0000000000000000000000007468655f6c6962726172795f6f665f626162656c (32 bytes)
- [destination, goes into scriptSig] public_key_hex: 04e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76d (65 bytes)
- [signable form] script_length_scriptPubKey: 19 (1 bytes)
- [signable form] scriptPubKey: 76a9147dc03dfbe8c62821bcd1ab95446b88ed7008a76e88ac (25 bytes)
- [more info] address: 1CTumCMjzBfccCJBTkHoPQmAwEqU9Uj2sQ
- [more info] previous_output_index_int: 9
- [more info] txid: 8482e48391425d88fe30bd4066957000335f58e83f854f997146f41b9493b4ce

Output 0:
Output: {34 bytes}
- [data] value: 5f0c030000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac (25 bytes)
- [more info] address: 12RbVkKwHcwHbMZmnSVAyR4g88ZChpQD6f
- [more info] bitcoin_amount: 0.00199775
- [more info] satoshi_amount: 199775
- [more info] public_key_hash_hex: 0f9ee78522f6cc8a88784ae02b0408e452d80259

Transaction (unsigned form): {84 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {40 bytes}
-- previous_output_hash: ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284 (32 bytes)
-- previous_output_index: 09000000 (4 bytes)
-- script_length: [none] (0 bytes)
-- scriptSig: [none] (0 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 5f0c030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Transaction (signable form 0): {114 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284 (32 bytes)
-- previous_output_index: 09000000 (4 bytes)
-- script_length_scriptPubKey: 19 (1 bytes)
-- scriptPubKey: 76a9147dc03dfbe8c62821bcd1ab95446b88ed7008a76e88ac (25 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 5f0c030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)
- hash_type_4_byte: 01000000 (4 bytes)

Transaction (signed form): {223 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {179 bytes}
-- previous_output_hash: ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284 (32 bytes)
-- previous_output_index: 09000000 (4 bytes)
-- script_length: 8a (1 bytes)
-- scriptSig: 47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76d (138 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 5f0c030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Signed transaction:
0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000

### END CREATION OF BITCOIN TRANSACTION




Signed transaction tx_1:

0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000




Now test equality of this newly-created transaction tx1 with the original tx1 from the source article:


[original tx1]
[spiano@localhost work]$ s1="0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000"


[new tx1]
[spiano@localhost work]$ s2="0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal







Good. Successful construction of a test transaction.






I'll confirm that this is identical to the transaction on the blockchain.


txid1:
4bdd5f653d0100ea4735953e8e22e92321af9074926b3bd33c279c289f2d975a




A service for looking up the raw hex form of a transaction:
http://blockchain.info/tx/[txid]?format=hex


Browse to:
http://blockchain.info/tx/4bdd5f653d0100ea4735953e8e22e92321af9074926b3bd33c279c289f2d975a?format=hex

Result:
0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000



Test equality:

[tx1 on the blockchain]
[spiano@localhost work]$ s1="0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000"


[new tx1]
[spiano@localhost work]$ s2="0100000001ceb493941bf44671994f853fe8585f330070956640bd30fe885d429183e48284090000008a47304402206a2eea0c908efbd780a34c48b48d80946a6e740c272468427bada3ba9773d39a022076017028c4386020b19b7d1f1640558cd902aeffbfbd85f4637a8be0e92ee1de014104e8ade66f2cc0e43073f4ccea47db279bbab1a5e30a6e8ba49f12538b215c5b9e0d28bd080d35fde878081e8f05dbc23eeba02b544fa83e6d13b5f2145681e76dffffffff015f0c0300000000001976a9140f9ee78522f6cc8a88784ae02b0408e452d8025988ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal





Good.















Next.




Source: Creating and signing a standard raw Bitcoin transaction: Iteration #2
Transactions: tx1


Excerpts from the preface:

I have discovered errors in these two assets:
- nonstandard_transaction.py
- transaction.py

The hash type (4-byte version) is erroneously appended to the final signed form of the transaction.

To resolve the issue, delete these lines:
- nonstandard_transaction.py: [Line 237, line 269]
- transaction.py: [Line 289, line 312]

[...]

~ StJohn Piano



So the final form of tx1 in this project will have an extra 4 bytes at the end, which should be removed for this test.


Excerpts:

constructed and signed a standard raw Bitcoin transaction that moves the available bitcoin from the source address to the target address. A mining fee was included, set at the low end of the then-current fee range. This was tx1.


[...]


Addresses generated and/or used in this project


Source address:

aineko:work stjohnpiano$ ./generate_bitcoin_address_3.py


### START GENERATION OF BITCOIN ADDRESS

Private key (raw bytes): the_mote_in_god's_eye
Private key (hex bytes): 7468655f6d6f74655f696e5f676f6427735f657965
Private key (32 hex bytes): 00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965
Private key (WIF): 5HpHagT65TZzG1PH41Hd3yo15N3JqZyeumt1YxQeHYAsFab3Qss
Bitcoin address: 138obEZkdWaWEQ4x8ZAYw4MybHSZtX1Nam

### END GENERATION OF BITCOIN ADDRESS



Target address:

aineko:work stjohnpiano$ ./generate_bitcoin_address_3.py


### START GENERATION OF BITCOIN ADDRESS

Private key (raw bytes): roadside_picnic
Private key (hex bytes): 726f6164736964655f7069636e6963
Private key (32 hex bytes): 0000000000000000000000000000000000726f6164736964655f7069636e6963
Private key (WIF): 5HpHagT65TZzG1PH3CSu63k8Dxn19XHMLQhSCuE8qpbeuFK2mGG
Bitcoin address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady

### END GENERATION OF BITCOIN ADDRESS



[...]


I'll use:
- txid0 to mean the txid (transaction id) of tx0
- txid1 to mean the txid of tx1


[...]


tx0:

I transferred some bitcoin from my LocalBitcoins account to the source address. LocalBitcoins created a larger transaction, which included many other transfers, that executed this transfer.

txid0:
e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855



[...]


tx1:

tx1 transferred the available bitcoin from the source address to the target address, minus a transaction fee.

txid1:
745e224ccba0a033c55ea80523f207da18b903418ac1f5d293eed62c19e0334d


Link:
live.blockcypher.com/btc/tx/745e224ccba0a033c55ea80523f207da18b903418ac1f5d293eed62c19e0334d

Key details:
- block_hash:
0000000000000000000eecb4423435b1e52db78e8c83ae9e271978c2ef9d186e
- block_height:
544268
- size:
223 bytes
- fees:
223
- confirmed:
2018-10-03T23:25:01Z

Actual fee rate for tx1 is 223 / 223 = 1 (satoshi / byte).


[...]


Creation of tx1 (final run of the script - the final result has been confirmed to be identical to the original transaction that was actually broadcast):

aineko:work stjohnpiano$ ./create_transaction.py


### START CREATION OF BITCOIN TRANSACTION

- Fee type: fee_rate
- Fee rate: 1.0 (satoshi / byte)
- Number of inputs (i.e. as-yet-unspent outputs): 1
- Number of outputs: 1
- Change address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady
- Amount to be sent to the change address: 0.002
- Input addresses, with total-value-to-be-sent:
-- 138obEZkdWaWEQ4x8ZAYw4MybHSZtX1Nam: 0.00242000
- Output addresses, with total-value-to-be-received:
-- 13xPBB175FtPbPQ84iB8KuawaVy3mHrady: 0.00200000
- Total value of all inputs: 0.00242000
- Total value of all outputs: 0.00200000
- Total value of all inputs is greater than total value of all outputs.
-- Some input value has not been assigned to an output.
-- Extra value: 0.00042000
-- This extra value will be sent to the change address.
-- Change address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady
-- New amount to be sent to change address: 0.00242000
- Estimated transaction size: 223 bytes
- Fee rate: 1.0 (satoshi / byte)
- Calculate 223 * 1.0 and round up to nearest satoshi.
- Final fee: 223 (satoshi)
- Final fee rate (using estimated transaction size): 1.0000 (satoshi per byte)
- Fee subtracted from amount to be sent to change address.
- New amount to be sent to change address: 241777 (satoshi)

Input 0:
Input (without signature):
- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4
- previous_output_index: 08000000
- sequence: ffffffff
- private_key_hex: 00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965
- public_key_hex: 041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58
- script_length_scriptPubKey: 19
- scriptPubKey: 76a914176a0e0c2b9b0e77d630712ad301b749102a304488ac
- script_length: None
- scriptSig: None
- address: 138obEZkdWaWEQ4x8ZAYw4MybHSZtX1Nam
- previous_output_index_int: 8
- txid: e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855

Output 0:
Output:
- value: 71b0030000000000
- script_length: 19
- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac
- address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady
- bitcoin_amount: 0.00241777
- satoshi_amount: 241777
- public_key_hash_hex: 2069a3fae01db74cef12d1d01811afdf6a3e1c2e

Transaction (unsigned form):
- version: 01000000
- input_count: 01
- Input:
-- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4
-- previous_output_index: 08000000
-- script_length: None
-- scriptSig: None
-- sequence: ffffffff
- output_count: 01
- Output:
-- value: 71b0030000000000
-- script_length: 19
-- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac
- block_lock_time: 00000000

Transaction (signable form):
- version: 01000000
- input_count: 01
- Input [to be used to sign this signable form]:
-- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4
-- previous_output_index: 08000000
-- script_length_scriptPubKey: 19
-- scriptPubKey: 76a914176a0e0c2b9b0e77d630712ad301b749102a304488ac
-- sequence: ffffffff
- output_count: 01
- Output:
-- value: 71b0030000000000
-- script_length: 19
-- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac
- block_lock_time: 00000000
- hash_type_4_byte: 01000000

Transaction (signed form):
- version: 01000000
- input_count: 01
- Input:
-- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4
-- previous_output_index: 08000000
-- script_length: 8a
-- scriptSig: 473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58
-- sequence: ffffffff
- output_count: 01
- Output:
-- value: 71b0030000000000
-- script_length: 19
-- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac
- block_lock_time: 00000000
- hash_type_4_byte: 01000000

Signed transaction:
01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac0000000001000000

### END CREATION OF BITCOIN TRANSACTION



[...]


[from within the code of create_transaction.py]

import bitcoin_functions as bf [...] random_value = "The Face of God dhcmrlchtdj" [...] random_value_type = "raw_bytes" [...] if random_value_type == "raw_bytes": random_values = [bf.bytes_to_hex(value) for value in random_values]






Let's get the hex value of the random_value.

Download bitcoin_functions.py from the source article.

Excerpt:

bitcoin_functions.py

python 2.7.13
def bytes_to_hex(input):
	from binascii import hexlify
	validate_bytes(input)
	output = hexlify(input)
	validate_hex(output)
	return output



[spiano@localhost ~]$ python

Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> random_value = "The Face of God dhcmrlchtdj"
>>> from binascii import hexlify
>>> random_value_hex = hexlify(random_value)
>>> random_value_hex
'5468652046616365206f6620476f64206468636d726c636874646a'
>>> len(random_value_hex)
54
>>> 64-54
10
>>> random_value_hex_32_bytes = 10*'0' + random_value_hex
>>> len(random_value_hex_32_bytes)
64
>>> random_value_hex_32_bytes
'00000000005468652046616365206f6620476f64206468636d726c636874646a'



32-byte random value in hex bytes:
00000000005468652046616365206f6620476f64206468636d726c636874646a




Now, the final signed tx1 was:
01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac0000000001000000


But this includes (erroneously) the 4-byte hash type (01000000) at the end.

Removing it produces:

Signed tx1:

01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000



When I created this transaction, I decided to choose an output value (0.002 bitcoin) that was somewhat smaller than the input value (0.00242 bitcoin). All extra value was to be sent to the change address (which, here, is the same as the single output address).


I observe that when I used a service to broadcast this transaction, and recorded the output of Advanced Details / API Call, the "hex" value of the transaction is missing the final 4 bytes
01000000
. Evidently the service simply didn't accept these final bytes.







Summary


Source: Creating and signing a standard raw Bitcoin transaction: Iteration #2
Transaction: tx1

Inputs:

1) For each input:
- The txid of the previous transaction that transferred the unspent output to the input address.
e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
8
- The private key (in hex bytes) of the input address:
00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
0.00242000 bitcoin
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
00000000005468652046616365206f6620476f64206468636d726c636874646a

2) For each output:
- The output address.
13xPBB175FtPbPQ84iB8KuawaVy3mHrady
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
0.002 bitcoin

3) For the whole transaction:
- The fee (in satoshi). The fee must be an integer e.g.
"225"
.
223 satoshi



Output: tx1 (signed)
01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000



End Summary










Next: Let's try out this test case.



The work directory is already set up.


Open a terminal. Change directory to the work directory.


I'll set the change address to be the output address.


Open the file create_transaction_2.py in a text editor.

Scroll to lines 44-79 (now lines 44-74), which should be the section of text that lies between
##### START CONTROLS

and
##### END CONTROLS



Note: In create_transaction_2.py, all control values are strings (i.e. are contained within quotation marks).


Set the variable
random_value
to the 32-byte random value.

Set the variable
random_value_type

to
"hex_bytes"
.


The variable
input_data
is a list that contains one or more inputs. Inputs are represented as dictionaries (key-value stores).

For each input dictionary (everything between and including a pair of braces {}):

- Set the value of the key
"txid"
to be the txid.
e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855

- Set the value of the key
"previous_output_index"
to be the index of the unspent output.
8

- Set the value of the key
"private_key_hex"
to be the private key.
00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965

- If the input amount is in satoshi, set the value of the key
"satoshi_amount"
to be the input amount.

- If the input amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the input amount.
0.00242000 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set, then
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.


The variable
output_data
is a list that contains one or more outputs. Outputs are represented as dictionaries (key-value stores).

For each output dictionary (everything between and including a pair of braces {}):

- Set the value of the key
"address"
to be the output address.
13xPBB175FtPbPQ84iB8KuawaVy3mHrady

- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.

- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.
0.002 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set,
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.

Note: You must include an output that is directed to the change address.



Set the variable
change_address
to be the output address.
13xPBB175FtPbPQ84iB8KuawaVy3mHrady


If you have chosen to set the fee as an absolute number of satoshi, set the variable
fee
to be the fee amount and set the variable
fee_type
to be
"fee"
.
223 satoshi






Here is how the controls look:


create_transaction_2.py

python 2.7.13
	##### START CONTROLS
	# Note: Hex characters in input variable values must be lowercase. 
	random_values = [
		"00000000005468652046616365206f6620476f64206468636d726c636874646a",	
	]
	# random_value must be between 1 and 32 bytes. If random_value_type is "raw_bytes", then random_value must be between 1 and 32 ASCII characters. If random_value_type is "hex_bytes", then random_value must be an even number of hex characters and between 2 and 64 hex characters (1 byte is represented by 2 hex characters). 
	# Note: Every ECDSA signature (one for each input in a transaction) requires new random entropy. 
	# random_value_type options: ["raw_bytes", "hex_bytes"]
	random_value_type = "hex_bytes"
	input_data = [
		{
		"txid": "e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855",
		"previous_output_index": "8",
		"private_key_hex": "00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965",
		#"satoshi_amount": "242000", 
		"bitcoin_amount": "0.00242000", 
		},
	]
	output_data = [
		{
		"address": "13xPBB175FtPbPQ84iB8KuawaVy3mHrady",
		#"satoshi_amount": "199775", 
		"bitcoin_amount": "0.002", 
		},
	]
	# note: all inputs and outputs are assumed to be Pay-To-Public-Key-Hash (P2PKH). 
	change_address = "13xPBB175FtPbPQ84iB8KuawaVy3mHrady"
	# note: the fee will be subtracted from the amount that is being sent to the change address.
	fee = "223" # satoshi
	fee_rate = "3" # satoshi / byte
	# fee_type options: ["fee", "fee_rate"]
	fee_type = "fee"
	##### END CONTROLS




In the terminal, in the work directory, run the following command:
python create_transaction_2.py


At the end, the output should contain the signed transaction as a hex byte sequence without spaces.




[spiano@localhost work]$ python create_transaction_2.py


### START CREATION OF BITCOIN TRANSACTION

- Fee type: fee
- Fee: 223 (satoshi)
- Number of inputs (i.e. as-yet-unspent outputs): 1
- Number of outputs: 1
- Change address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady
- Amount to be sent to the change address: 0.002
- Input addresses, with total-value-to-be-sent:
-- 138obEZkdWaWEQ4x8ZAYw4MybHSZtX1Nam: 0.00242000
- Output addresses, with total-value-to-be-received:
-- 13xPBB175FtPbPQ84iB8KuawaVy3mHrady: 0.00200000
- Total value of all inputs: 0.00242000
- Total value of all outputs: 0.00200000
- Total value of all inputs is greater than total value of all outputs.
-- Some input value has not been assigned to an output.
-- Extra value: 0.00042000
-- This extra value will be sent to the change address.
-- Change address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady
-- New amount to be sent to change address: 0.00242000
- Estimated transaction size: 223 bytes
- Final fee: 223 (satoshi)
- Final fee rate (using estimated transaction size): 1.0000 (satoshi per byte)
- Fee subtracted from amount-to-be-sent-to-change-address.
- New amount to be sent to change address: 241777 (satoshi)
- Size of signed transaction: 223 bytes
- (signed_tx_size - estimated_tx_size): 0 bytes
- Fee rate in signed transaction: 1.0 (satoshi / byte)

Input 0:
Input: {40 bytes unsigned, 66 bytes signable, 179 bytes signed}
- [data] previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4 (32 bytes)
- [data] previous_output_index: 08000000 (4 bytes)
- [signed form] script_length: 8a (1 bytes)
- [signed form] scriptSig: 473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58 (138 bytes)
- [data] sequence: ffffffff (4 bytes)
- [used for signing] private_key_hex: 00000000000000000000007468655f6d6f74655f696e5f676f6427735f657965 (32 bytes)
- [destination, goes into scriptSig] public_key_hex: 041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58 (65 bytes)
- [signable form] script_length_scriptPubKey: 19 (1 bytes)
- [signable form] scriptPubKey: 76a914176a0e0c2b9b0e77d630712ad301b749102a304488ac (25 bytes)
- [more info] address: 138obEZkdWaWEQ4x8ZAYw4MybHSZtX1Nam
- [more info] previous_output_index_int: 8
- [more info] txid: e4609e0f1ca854b8b07381f32ba31adbad9713205f5a4f3f56a5a32853d47855

Output 0:
Output: {34 bytes}
- [data] value: 71b0030000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac (25 bytes)
- [more info] address: 13xPBB175FtPbPQ84iB8KuawaVy3mHrady
- [more info] bitcoin_amount: 0.00241777
- [more info] satoshi_amount: 241777
- [more info] public_key_hash_hex: 2069a3fae01db74cef12d1d01811afdf6a3e1c2e

Transaction (unsigned form): {84 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {40 bytes}
-- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4 (32 bytes)
-- previous_output_index: 08000000 (4 bytes)
-- script_length: [none] (0 bytes)
-- scriptSig: [none] (0 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 71b0030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Transaction (signable form 0): {114 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4 (32 bytes)
-- previous_output_index: 08000000 (4 bytes)
-- script_length_scriptPubKey: 19 (1 bytes)
-- scriptPubKey: 76a914176a0e0c2b9b0e77d630712ad301b749102a304488ac (25 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 71b0030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)
- hash_type_4_byte: 01000000 (4 bytes)

Transaction (signed form): {223 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {179 bytes}
-- previous_output_hash: 5578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4 (32 bytes)
-- previous_output_index: 08000000 (4 bytes)
-- script_length: 8a (1 bytes)
-- scriptSig: 473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58 (138 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 71b0030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Signed transaction:
01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000

### END CREATION OF BITCOIN TRANSACTION




Signed transaction tx_1:

01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000




Now test equality of this newly-created transaction tx1 with the original tx1 from the source article:


[original tx1, minus last 4 bytes]
[spiano@localhost work]$ s1="01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000"


[newly created tx1]
[spiano@localhost work]$ s2="01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal







Good. Successful construction of a test transaction.






I'll confirm that this is identical to the transaction on the blockchain.


txid1:
745e224ccba0a033c55ea80523f207da18b903418ac1f5d293eed62c19e0334d




A service for looking up the raw hex form of a transaction:
http://blockchain.info/tx/[txid]?format=hex


Browse to:
http://blockchain.info/tx/745e224ccba0a033c55ea80523f207da18b903418ac1f5d293eed62c19e0334d?format=hex

Result:
01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000



Test equality:

[tx1 on the blockchain]
[spiano@localhost work]$ s1="01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000"


[new tx1]
[spiano@localhost work]$ s2="01000000015578d45328a3a5563f4f5a5f201397addb1aa32bf38173b0b854a81c0f9e60e4080000008a473044022017ece35581a034a4838a577fe438f108cf22764927a1ad197ed379460c0764cd022066723723eff05c10bac9a2f63b3e782e24fde290701c9408f3d1054722adad360141041ad06846fd7cf9998a827485d8dd5aaba9eccc385ba7759a6e9055fbdf90d7513c0d11fe5e5dcfcf8d4946c67f6c45f8e7f7d7a9c254ca8ebde1ffd64ab9dd58ffffffff0171b00300000000001976a9142069a3fae01db74cef12d1d01811afdf6a3e1c2e88ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal





Good.















Next.




Source: Creating a Bitcoin transaction with two outputs
Transactions: tx1


Excerpts:

Addresses generated


Source address:
- Bitcoin private key:
7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5

- Bitcoin address:
16ASCUS3s7D4UQh6J9oHGuT19agPvD3PFj


Destination address 1:
- Bitcoin private key:
d45941cae4e31c824b041407053c9c15624e6234f9649bfd7d5bb5a93c53fe85

- Bitcoin address:
1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig


Destination address 2:
- Bitcoin private key:
1faf3c86105fe6ee328b29e94197de7e8500ddb8308385a9a8b0cba08d59ce27

- Bitcoin address:
1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P



[...]


Transaction details:
- tx_0: Transferred bitcoin from my LocalBitcoins account to the source address.
- tx_1: Transferred bitcoin from the source address to the two destination addresses. Initial form of tx_1 had a spurious addition of the 1-byte hash type at the end.


[...]


===== Transaction tx_0 =====


txid_0:
f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e




===== Transaction tx_1 =====


txid_1:
db2c3d84708cd9d0e40ae1754021f9146a0d6ab555fc0e1d547d7876c0c092f4

Initial form of tx_1:
01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac0000000001


It had an invalid addition of the 1-byte hash type (01) at the end. The broadcast service removed this byte before broadcasting the transaction.

Mined version of tx_1:

01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000


Final fee: 774 (satoshi)
Size: 257 bytes
Fee rate: ~= 3.023 (satoshi / byte)

Time taken from broadcast to confirmation:
36 minutes


[...]


Transaction creation data


[...]


===== Transaction tx_1 =====


Fee rate: 3 satoshi / byte

The information for the single input:

- txid (of the previous transaction that transferred the unspent output to the input address):
f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e

- previous_output_index (the index of this unspent output in the previous transaction):
12

- private key (of the input address):
7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5

- input amount (amount contained in the unspent output):
0.006 bitcoin (600000 satoshi).
- 32-byte random value (in this case, entropy_1):
257c2ff4ac1606d5e42fe152c2624cffac2aa58cdfe8578a69b12beefbce68b9


The information for the two outputs:

Output 1 (to destination address 1):
- Bitcoin address:
1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig


Output 2 (to destination address 2):
- Bitcoin address:
1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P



[...]


output amounts:
- I'll send half the input amount (so 0.003 btc) to destination address 1. The rest will go to the change output (destination address 2). The transaction fee will be subtracted from the amount that goes into the change output.




The decoded transaction output in the project log shows that 299226 satoshi was sent to output address 2.








Summary


Source: Creating a Bitcoin transaction with two outputs
Transaction: tx1

Inputs:

1) For each input:
- The txid of the previous transaction that transferred the unspent output to the input address.
f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
12
- The private key (in hex bytes) of the input address:
7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
0.006 bitcoin (600000 satoshi)
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
257c2ff4ac1606d5e42fe152c2624cffac2aa58cdfe8578a69b12beefbce68b9

2) For each output:

2a) Output 1:
- The output address.
1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
0.003 bitcoin

2b) Output 2: [this is the change address]
- The output address.
1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
[all remaining bitcoin]

3) For the whole transaction:
- The fee (in satoshi). The fee must be an integer e.g.
"225"
.
774 satoshi



Output: tx1 (signed)
01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000



End Summary








Next: Let's try out this test case.



The work directory is already set up.


Open a terminal. Change directory to the work directory.


Open the file create_transaction_2.py in a text editor.

Scroll to lines 44-79 (now lines 44-74), which should be the section of text that lies between
##### START CONTROLS

and
##### END CONTROLS



Note: In create_transaction_2.py, all control values are strings (i.e. are contained within quotation marks).


Set the variable
random_value
to the 32-byte random value.
257c2ff4ac1606d5e42fe152c2624cffac2aa58cdfe8578a69b12beefbce68b9

Set the variable
random_value_type

to
"hex_bytes"
.


The variable
input_data
is a list that contains one or more inputs. Inputs are represented as dictionaries (key-value stores).

For each input dictionary (everything between and including a pair of braces {}):

- Set the value of the key
"txid"
to be the txid.
f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e

- Set the value of the key
"previous_output_index"
to be the index of the unspent output.
12

- Set the value of the key
"private_key_hex"
to be the private key.
7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5

- If the input amount is in satoshi, set the value of the key
"satoshi_amount"
to be the input amount.

- If the input amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the input amount.
0.006 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set, then
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.


The variable
output_data
is a list that contains one or more outputs. Outputs are represented as dictionaries (key-value stores).

For each output dictionary (everything between and including a pair of braces {}):

Output 1:
- Set the value of the key
"address"
to be the output address.
1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig
- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.
- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.
0.003 bitcoin

Output 2:
- Set the value of the key
"address"
to be the output address.
1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P
- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.
- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.
0.00000001 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set,
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.

Note: You must include an output that is directed to the change address.



Set the variable
change_address
to be output address 2.
1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P


If you have chosen to set the fee as an absolute number of satoshi, set the variable
fee
to be the fee amount and set the variable
fee_type
to be
"fee"
.
774 satoshi






Here is how the controls look:


create_transaction_2.py

python 2.7.13
	##### START CONTROLS
	# Note: Hex characters in input variable values must be lowercase. 
	random_values = [
		"257c2ff4ac1606d5e42fe152c2624cffac2aa58cdfe8578a69b12beefbce68b9",	
	]
	# random_value must be between 1 and 32 bytes. If random_value_type is "raw_bytes", then random_value must be between 1 and 32 ASCII characters. If random_value_type is "hex_bytes", then random_value must be an even number of hex characters and between 2 and 64 hex characters (1 byte is represented by 2 hex characters). 
	# Note: Every ECDSA signature (one for each input in a transaction) requires new random entropy. 
	# random_value_type options: ["raw_bytes", "hex_bytes"]
	random_value_type = "hex_bytes"
	input_data = [
		{
		"txid": "f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e",
		"previous_output_index": "12",
		"private_key_hex": "7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5",
		#"satoshi_amount": "242000", 
		"bitcoin_amount": "0.006", 
		},
	]
	output_data = [
		{
		"address": "1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig",
		#"satoshi_amount": "199775", 
		"bitcoin_amount": "0.003", 
		},
		{
		"address": "1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P",
		#"satoshi_amount": "199775", 
		"bitcoin_amount": "0.00000001", 
		},
	]
	# note: all inputs and outputs are assumed to be Pay-To-Public-Key-Hash (P2PKH). 
	change_address = "1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P"
	# note: the fee will be subtracted from the amount that is being sent to the change address.
	fee = "774" # satoshi
	fee_rate = "3" # satoshi / byte
	# fee_type options: ["fee", "fee_rate"]
	fee_type = "fee"
	##### END CONTROLS




In the terminal, in the work directory, run the following command:
python create_transaction_2.py


At the end, the output should contain the signed transaction as a hex byte sequence without spaces.




[spiano@localhost work]$ python create_transaction_2.py


### START CREATION OF BITCOIN TRANSACTION

- Fee type: fee
- Fee: 774 (satoshi)
- Number of inputs (i.e. as-yet-unspent outputs): 1
- Number of outputs: 2
- Change address: 1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P
- Amount to be sent to the change address: 0.00000001
- Input addresses, with total-value-to-be-sent:
-- 16ASCUS3s7D4UQh6J9oHGuT19agPvD3PFj: 0.00600000
- Output addresses, with total-value-to-be-received:
-- 1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig: 0.00300000
-- 1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P: 0.00000001
- Total value of all inputs: 0.00600000
- Total value of all outputs: 0.00300001
- Total value of all inputs is greater than total value of all outputs.
-- Some input value has not been assigned to an output.
-- Extra value: 0.00299999
-- This extra value will be sent to the change address.
-- Change address: 1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P
-- New amount to be sent to change address: 0.00300000
- Estimated transaction size: 257 bytes
- Final fee: 774 (satoshi)
- Final fee rate (using estimated transaction size): 3.0117 (satoshi per byte)
- Fee subtracted from amount-to-be-sent-to-change-address.
- New amount to be sent to change address: 299226 (satoshi)
- Size of signed transaction: 257 bytes
- (signed_tx_size - estimated_tx_size): 0 bytes
- Fee rate in signed transaction: 3.012 (satoshi / byte)

Input 0:
Input: {40 bytes unsigned, 66 bytes signable, 179 bytes signed}
- [data] previous_output_hash: 6ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef0 (32 bytes)
- [data] previous_output_index: 0c000000 (4 bytes)
- [signed form] script_length: 8a (1 bytes)
- [signed form] scriptSig: 47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2a (138 bytes)
- [data] sequence: ffffffff (4 bytes)
- [used for signing] private_key_hex: 7972b641101c0ad67b0e401b800a9b6f3225c97fc6b8115042cf66968c2fb2e5 (32 bytes)
- [destination, goes into scriptSig] public_key_hex: 04108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2a (65 bytes)
- [signable form] script_length_scriptPubKey: 19 (1 bytes)
- [signable form] scriptPubKey: 76a91438a1681a09280ecb20b62cf661913e811eb3c4b188ac (25 bytes)
- [more info] address: 16ASCUS3s7D4UQh6J9oHGuT19agPvD3PFj
- [more info] previous_output_index_int: 12
- [more info] txid: f02eca2852bf73f3c722db2d151e7755d853efd8dd6224249b14f8b51dffbc6e

Output 0:
Output: {34 bytes}
- [data] value: e093040000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a914c8833727be832b6634d8ddf7bfcf51e379b28f6388ac (25 bytes)
- [more info] address: 1KHDLNmqBtiBELUsmTCkNASg79jfEVKrig
- [more info] bitcoin_amount: 0.003
- [more info] satoshi_amount: 300000
- [more info] public_key_hash_hex: c8833727be832b6634d8ddf7bfcf51e379b28f63

Output 1:
Output: {34 bytes}
- [data] value: da90040000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a914875a093668adff783d0835f4db655c5a47a0ceaa88ac (25 bytes)
- [more info] address: 1DLg5i1kBjXYXy9f82xZcHokEMC9dtct7P
- [more info] bitcoin_amount: 0.00299226
- [more info] satoshi_amount: 299226
- [more info] public_key_hash_hex: 875a093668adff783d0835f4db655c5a47a0ceaa

Transaction (unsigned form): {118 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {40 bytes}
-- previous_output_hash: 6ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef0 (32 bytes)
-- previous_output_index: 0c000000 (4 bytes)
-- script_length: [none] (0 bytes)
-- scriptSig: [none] (0 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 02 (1 bytes)
- Output 0: {34 bytes}
-- value: e093040000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914c8833727be832b6634d8ddf7bfcf51e379b28f6388ac (25 bytes)
- Output 1: {34 bytes}
-- value: da90040000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914875a093668adff783d0835f4db655c5a47a0ceaa88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Transaction (signable form 0): {148 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 6ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef0 (32 bytes)
-- previous_output_index: 0c000000 (4 bytes)
-- script_length_scriptPubKey: 19 (1 bytes)
-- scriptPubKey: 76a91438a1681a09280ecb20b62cf661913e811eb3c4b188ac (25 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 02 (1 bytes)
- Output 0: {34 bytes}
-- value: e093040000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914c8833727be832b6634d8ddf7bfcf51e379b28f6388ac (25 bytes)
- Output 1: {34 bytes}
-- value: da90040000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914875a093668adff783d0835f4db655c5a47a0ceaa88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)
- hash_type_4_byte: 01000000 (4 bytes)

Transaction (signed form): {257 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {179 bytes}
-- previous_output_hash: 6ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef0 (32 bytes)
-- previous_output_index: 0c000000 (4 bytes)
-- script_length: 8a (1 bytes)
-- scriptSig: 47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2a (138 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 02 (1 bytes)
- Output 0: {34 bytes}
-- value: e093040000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914c8833727be832b6634d8ddf7bfcf51e379b28f6388ac (25 bytes)
- Output 1: {34 bytes}
-- value: da90040000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914875a093668adff783d0835f4db655c5a47a0ceaa88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Signed transaction:
01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000

### END CREATION OF BITCOIN TRANSACTION





Signed transaction tx_1:

01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000







Now test equality of this newly-created transaction tx1 with the original tx1 from the source article:


[original tx1]
[spiano@localhost work]$ s1="01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000"


[newly created tx1]
[spiano@localhost work]$ s2="01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal







Good. Successful construction of a test transaction.






I'll confirm that this is identical to the transaction on the blockchain.


txid1:
db2c3d84708cd9d0e40ae1754021f9146a0d6ab555fc0e1d547d7876c0c092f4




A service for looking up the raw hex form of a transaction:
http://blockchain.info/tx/[txid]?format=hex


Browse to:
http://blockchain.info/tx/db2c3d84708cd9d0e40ae1754021f9146a0d6ab555fc0e1d547d7876c0c092f4?format=hex

Result:
01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000



Test equality:

[tx1 on the blockchain]
[spiano@localhost work]$ s1="01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000"


[newly created tx1]
[spiano@localhost work]$ s2="01000000016ebcff1db5f8149b242462ddd8ef53d855771e152ddb22c7f373bf5228ca2ef00c0000008a47304402202d8617427fc589848ef2e0481d148e0ce5d60aaa26e466fdfe62890d0d67ca6902200df89728ea73816cecfdfd6a734ca8b70da7000e213b87dcfdc9d48400164fac014104108e1c01a42bbd09b77a26b5f732f10d7108b2fe31cba137e9d15e24c7c30d2515904dfe646f3e94a3aa482afbbe0dc178b9b212154529768e0ba74452df1c2affffffff02e0930400000000001976a914c8833727be832b6634d8ddf7bfcf51e379b28f6388acda900400000000001976a914875a093668adff783d0835f4db655c5a47a0ceaa88ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal





Good.

















Next.




Source: Creating a Bitcoin transaction with two inputs
Transactions: tx1, tx2


Excerpts:

Original plan of action:

- Generate 32 bytes of entropy and create a "source" address.

[...]

- Using LocalBitcoins, transfer X bitcoin to the source address. LocalBitcoins will construct a larger transaction that will include this transfer. This will be transaction tx_0.

[...]

- Generate 2 sets of 32 bytes of entropy and create two "test" addresses.

[...]

- Assemble information for creating a 1-input 2-output transaction that transfers X/2 bitcoin from the source address into each test address. This will be transaction tx_1.
- Generate 32 bytes of entropy for signing tx_1.
- Create and sign tx_1.

[...]

- Generate 32 bytes of entropy and create a "destination" address.

[...]

- Assemble information for creating a 2-input 1-output transaction that transfers the bitcoin in the test addresses into the destination address. This will be transaction tx_2.
- Generate 2 sets of 32 bytes of entropy for signing tx_2.
- Create and sign tx_2.


[...]


Addresses generated


Source address:
- Bitcoin private key:
7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8

- Bitcoin address:
17pNyD9ur28aBgPhHtAFi6fAyrvgshp5yn


Test address 1:
- Bitcoin private key:
cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f

- Bitcoin address:
1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8


Test address 2:
- Bitcoin private key:
7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af

- Bitcoin address:
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4


Destination address:
- Bitcoin private key:
419292d169b515be1aec881018157f0a41883ea54da2b2405e10fd8a6abb76ec

- Bitcoin address:
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s




[...]


Transaction details:
- tx_0: Transferred bitcoin from my LocalBitcoins account to the source address.
- tx_1: Transferred bitcoin from the source address to the two test addresses.
- tx_2: Transferred bitcoin from the two test addresses to the destination address.


[...]


===== Transaction tx_0 =====


txid_0:
1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19




===== Transaction tx_1 =====


txid_1:
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826

Signed transaction tx_1:
0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000


Final fee: 386 (satoshi)
Size: 257 bytes
Fee rate: 1.502 (satoshi / byte)

Time taken from broadcast to confirmation:
1 hour 52 minutes




===== Transaction tx_2 =====


txid_2:
92616e432f7807bf6d93252fcdca7efb139e56f7aa34835b50670ebc6a4b5649

Signed transaction tx_2:
01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000


Final fee: 443 (satoshi)
Size: 403 bytes
Fee rate: 1.099 (satoshi / byte)

Time taken from broadcast to confirmation:
3 minutes


[...]


Transaction creation data




===== Transaction tx_0 =====


On LocalBitcoins, send 0.004 bitcoin to the source address:
17pNyD9ur28aBgPhHtAFi6fAyrvgshp5yn




===== Transaction tx_1 =====


Fee rate: 1.5 satoshi / byte

The information for the single input (input_1):

- txid (of the previous transaction that transferred the unspent output to the input address):
1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19

- previous_output_index (the index of this unspent output in the previous transaction):
3

- private key (of the input address):
7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8

-- Note: This is the private key for the source address.
- input amount (amount contained in the unspent output):
0.004 bitcoin (400000 satoshi).
- 32-byte random value (in this case, entropy_1):
84425ee562f20d7493420c97eea153d37891eb63ce51fa24f51bfc7646353d32


The information for the two outputs:

Output 1 (to test address 1):
- Bitcoin address:
1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8

- amount: 0.002 bitcoin (200000 satoshi)

Output 2 (to test address 2):
- Bitcoin address:
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4

- amount: 0.002 bitcoin (200000 satoshi)

Change address: test address 2
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4





===== Transaction tx_2 =====


Fee rate: 1.1 satoshi / byte

Input data:

Input_2:
- txid (of the previous transaction tx_1 that transferred the unspent output to the input address):
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826

- previous_output_index (the index of this unspent output in the previous transaction):
0

-- Note: Reading the output from making tx_1, or looking at the data about it above, shows that the output sent to test address 1 appears first in the output list.
- private key (of the input address):
cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f

-- Note: This is the private key for test address 1.
- input amount (amount contained in the unspent output):
0.002 bitcoin (200000 satoshi)
- 32-byte random value (in this case, entropy_2):
5ab5bbe6fdc861a461a6721b2953d32be23c233e5ad5dc08d7c13daa95adbc17


Input_3:
- txid (of the previous transaction tx_1 that transferred the unspent output to the input address):
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826

- previous_output_index (the index of this unspent output in the previous transaction):
1

-- Note: Reading the output from making tx_1, or looking at the data about it above, shows that the output sent to test address 2 appears second in the output list.
- private key (of the input address):
7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af

-- Note: This is the private key for test address 2.
- input amount (amount contained in the unspent output):
0.00199614 bitcoin (199614 satoshi)
- 32-byte random value (in this case, entropy_3):
75fc65686a47326556735be91491a06f380a9db682fe9d50f82ad8d8d8849c2f


Output data:
- Bitcoin address:
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s

- amount: 0.00399614 bitcoin (399614 satoshi)

Change address: destination address
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s









Summary


Source: Creating a Bitcoin transaction with two inputs
Transaction: tx1

Inputs:

1) For each input:
- The txid of the previous transaction that transferred the unspent output to the input address.
1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
3
- The private key (in hex bytes) of the input address:
7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
0.004 bitcoin (400000 satoshi).
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
84425ee562f20d7493420c97eea153d37891eb63ce51fa24f51bfc7646353d32

2) For each output:

2a) Output 1:
- The output address.
1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
0.002 bitcoin (200000 satoshi)

2b) Output 2: [this is the change address]
- The output address.
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
0.002 bitcoin (200000 satoshi)

3) For the whole transaction:
- The fee (in satoshi). The fee must be an integer e.g.
"225"
.
386 satoshi



Output: tx1 (signed)
0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000



Mined txid1:
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826





Source: Creating a Bitcoin transaction with two inputs
Transaction: tx2

Inputs:

1) For each input:

1a)
- The txid of the previous transaction that transferred the unspent output to the input address.
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
0
- The private key (in hex bytes) of the input address:
cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
0.002 bitcoin (200000 satoshi)
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
5ab5bbe6fdc861a461a6721b2953d32be23c233e5ad5dc08d7c13daa95adbc17

1b)
- The txid of the previous transaction that transferred the unspent output to the input address.
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826
- The index of this unspent output in the previous transaction (the "previous_output_index"). This index is implicit and starts at zero, and can be found by examining the list of outputs in the previous transaction, and looking for the particular unspent output that you wish to transfer.
1
- The private key (in hex bytes) of the input address:
7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af
- The bitcoin amount (or satoshi amount) contained within the unspent output. Let's call this the "input amount".
0.00199614 bitcoin (199614 satoshi)
- A 32-byte random value (1 per input), as listed in the initial conditions. The transaction will be signed once using each input and each signature requires 32 bytes of entropy.
75fc65686a47326556735be91491a06f380a9db682fe9d50f82ad8d8d8849c2f

2) For each output:

2a) Output 1: [this is the change address]
- The output address.
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s
- The bitcoin amount (or satoshi amount) that you wish to transfer to the output address. Let's call this the "output amount".
0.00399614 bitcoin (399614 satoshi)

3) For the whole transaction:
- The fee (in satoshi). The fee must be an integer e.g.
"225"
.
443 satoshi



Output: tx2 (signed)
01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000



Mined txid2:
92616e432f7807bf6d93252fcdca7efb139e56f7aa34835b50670ebc6a4b5649


End Summary













Next: Let's try out these test cases.




Test case 1: tx1



The work directory is already set up.


Open a terminal. Change directory to the work directory.


Open the file create_transaction_2.py in a text editor.

Scroll to lines 44-79, which should be the section of text that lies between
##### START CONTROLS

and
##### END CONTROLS



Note: In create_transaction_2.py, all control values are strings (i.e. are contained within quotation marks).


Set the list
random_values
to contain the 32-byte random value:
84425ee562f20d7493420c97eea153d37891eb63ce51fa24f51bfc7646353d32

Set the variable
random_value_type

to
"hex_bytes"
.


The variable
input_data
is a list that contains one or more inputs. Inputs are represented as dictionaries (key-value stores).

For each input dictionary (everything between and including a pair of braces {}):

- Set the value of the key
"txid"
to be the txid.
1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19

- Set the value of the key
"previous_output_index"
to be the index of the unspent output.
3

- Set the value of the key
"private_key_hex"
to be the private key.
7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8

- If the input amount is in satoshi, set the value of the key
"satoshi_amount"
to be the input amount.

- If the input amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the input amount.
0.004 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set, then
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.


The variable
output_data
is a list that contains one or more outputs. Outputs are represented as dictionaries (key-value stores).

For each output dictionary (everything between and including a pair of braces {}):

Output 1:
- Set the value of the key
"address"
to be the output address.
1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8

- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.
- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.
0.002 bitcoin

Output 2:
- Set the value of the key
"address"
to be the output address.
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4
- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.
- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.
0.002 bitcoin

Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set,
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.

Note: You must include an output that is directed to the change address.



Set the variable
change_address
to be output address 2.
19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4


If you have chosen to set the fee as an absolute number of satoshi, set the variable
fee
to be the fee amount and set the variable
fee_type
to be
"fee"
.
386 satoshi







Here is how the controls look:


create_transaction_2.py

python 2.7.13
	##### START CONTROLS
	# Note: Hex characters in input variable values must be lowercase. 
	random_values = [
		"84425ee562f20d7493420c97eea153d37891eb63ce51fa24f51bfc7646353d32",	
	]
	# random_value must be between 1 and 32 bytes. If random_value_type is "raw_bytes", then random_value must be between 1 and 32 ASCII characters. If random_value_type is "hex_bytes", then random_value must be an even number of hex characters and between 2 and 64 hex characters (1 byte is represented by 2 hex characters). 
	# Note: Every ECDSA signature (one for each input in a transaction) requires new random entropy. 
	# random_value_type options: ["raw_bytes", "hex_bytes"]
	random_value_type = "hex_bytes"
	input_data = [
		{
		"txid": "1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19",
		"previous_output_index": "3",
		"private_key_hex": "7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8",
		#"satoshi_amount": "242000", 
		"bitcoin_amount": "0.004", 
		},
	]
	output_data = [
		{
		"address": "1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8",
		#"satoshi_amount": "199775", 
		"bitcoin_amount": "0.002", 
		},
		{
		"address": "19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4",
		#"satoshi_amount": "199775", 
		"bitcoin_amount": "0.002", 
		},
	]
	# note: all inputs and outputs are assumed to be Pay-To-Public-Key-Hash (P2PKH). 
	change_address = "19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4"
	# note: the fee will be subtracted from the amount that is being sent to the change address.
	fee = "386" # satoshi
	fee_rate = "3" # satoshi / byte
	# fee_type options: ["fee", "fee_rate"]
	fee_type = "fee"
	##### END CONTROLS




In the terminal, in the work directory, run the following command:
python create_transaction_2.py


At the end, the output should contain the signed transaction as a hex byte sequence without spaces.






[spiano@localhost work]$ python create_transaction_2.py


### START CREATION OF BITCOIN TRANSACTION

- Fee type: fee
- Fee: 386 (satoshi)
- Number of inputs (i.e. as-yet-unspent outputs): 1
- Number of outputs: 2
- Change address: 19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4
- Amount to be sent to the change address: 0.002
- Input addresses, with total-value-to-be-sent:
-- 17pNyD9ur28aBgPhHtAFi6fAyrvgshp5yn: 0.00400000
- Output addresses, with total-value-to-be-received:
-- 19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4: 0.00200000
-- 1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8: 0.00200000
- Total value of all inputs: 0.00400000
- Total value of all outputs: 0.00400000
- Total value of all inputs exactly matches total value of all outputs.
- Estimated transaction size: 257 bytes
- Final fee: 386 (satoshi)
- Final fee rate (using estimated transaction size): 1.5019 (satoshi per byte)
- Fee subtracted from amount-to-be-sent-to-change-address.
- New amount to be sent to change address: 199614 (satoshi)
- Size of signed transaction: 257 bytes
- (signed_tx_size - estimated_tx_size): 0 bytes
- Fee rate in signed transaction: 1.502 (satoshi / byte)

Input 0:
Input: {40 bytes unsigned, 66 bytes signable, 179 bytes signed}
- [data] previous_output_hash: 194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b (32 bytes)
- [data] previous_output_index: 03000000 (4 bytes)
- [signed form] script_length: 8a (1 bytes)
- [signed form] scriptSig: 47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966 (138 bytes)
- [data] sequence: ffffffff (4 bytes)
- [used for signing] private_key_hex: 7a0d11f6015f941ea486922e45310535c5b71c59d8489447648eebdb6a39e2a8 (32 bytes)
- [destination, goes into scriptSig] public_key_hex: 04c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966 (65 bytes)
- [signable form] script_length_scriptPubKey: 19 (1 bytes)
- [signable form] scriptPubKey: 76a9144ac6a4d0ee2aa064974bf6e571f6f700b7f67ce688ac (25 bytes)
- [more info] address: 17pNyD9ur28aBgPhHtAFi6fAyrvgshp5yn
- [more info] previous_output_index_int: 3
- [more info] txid: 1b1ad779f50b07aac0048471844c0637100561b179457ddd433f1c05adf24a19

Output 0:
Output: {34 bytes}
- [data] value: 400d030000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a914f425b648af85989c1387d532393938fcb7d7bcbb88ac (25 bytes)
- [more info] address: 1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8
- [more info] bitcoin_amount: 0.002
- [more info] satoshi_amount: 200000
- [more info] public_key_hash_hex: f425b648af85989c1387d532393938fcb7d7bcbb

Output 1:
Output: {34 bytes}
- [data] value: be0b030000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a91461999077782b7298b813d83459a9c944da6460bd88ac (25 bytes)
- [more info] address: 19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4
- [more info] bitcoin_amount: 0.00199614
- [more info] satoshi_amount: 199614
- [more info] public_key_hash_hex: 61999077782b7298b813d83459a9c944da6460bd

Transaction (unsigned form): {118 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {40 bytes}
-- previous_output_hash: 194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b (32 bytes)
-- previous_output_index: 03000000 (4 bytes)
-- script_length: [none] (0 bytes)
-- scriptSig: [none] (0 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 02 (1 bytes)
- Output 0: {34 bytes}
-- value: 400d030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914f425b648af85989c1387d532393938fcb7d7bcbb88ac (25 bytes)
- Output 1: {34 bytes}
-- value: be0b030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a91461999077782b7298b813d83459a9c944da6460bd88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Transaction (signable form 0): {148 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b (32 bytes)
-- previous_output_index: 03000000 (4 bytes)
-- script_length_scriptPubKey: 19 (1 bytes)
-- scriptPubKey: 76a9144ac6a4d0ee2aa064974bf6e571f6f700b7f67ce688ac (25 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 02 (1 bytes)
- Output 0: {34 bytes}
-- value: 400d030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914f425b648af85989c1387d532393938fcb7d7bcbb88ac (25 bytes)
- Output 1: {34 bytes}
-- value: be0b030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a91461999077782b7298b813d83459a9c944da6460bd88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)
- hash_type_4_byte: 01000000 (4 bytes)

Transaction (signed form): {257 bytes}
- version: 01000000 (4 bytes)
- input_count: 01 (1 bytes)
- Input 0: {179 bytes}
-- previous_output_hash: 194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b (32 bytes)
-- previous_output_index: 03000000 (4 bytes)
-- script_length: 8a (1 bytes)
-- scriptSig: 47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966 (138 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 02 (1 bytes)
- Output 0: {34 bytes}
-- value: 400d030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a914f425b648af85989c1387d532393938fcb7d7bcbb88ac (25 bytes)
- Output 1: {34 bytes}
-- value: be0b030000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a91461999077782b7298b813d83459a9c944da6460bd88ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Signed transaction:
0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000

### END CREATION OF BITCOIN TRANSACTION





Signed transaction tx_1:

0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000







Now test equality of this newly-created transaction tx1 with the original tx1 from the source article:


[original tx1]
[spiano@localhost work]$ s1="0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000"


[newly created tx1]
[spiano@localhost work]$ s2="0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal







Good. Successful construction of a test transaction.






I'll confirm that this is identical to the transaction on the blockchain.


txid1:
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826




A service for looking up the raw hex form of a transaction:
http://blockchain.info/tx/[txid]?format=hex


Browse to:
http://blockchain.info/tx/8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826?format=hex

Result:
0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000



Test equality:

[tx1 on the blockchain]
[spiano@localhost work]$ s1="0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000"


[newly created tx1]
[spiano@localhost work]$ s2="0100000001194af2ad051c3f43dd7d4579b161051037064c84718404c0aa070bf579d71a1b030000008a47304402204f38e1194a0f71f4cb03b1a8644dae69c543cab339d19f0412511bed0868904a02201a9356663d0ad3527b5633ced4cbc469f98bccb0940f2583c7c6c0c0868973a3014104c0128d7e4cbfef4e1bae30788b7bd7a4b974027eea5d83c07ac041f194707e8bba70a162c557cdbcb89682a5a310a2d5a722b2d58d18920a00b6b4c7024ce966ffffffff02400d0300000000001976a914f425b648af85989c1387d532393938fcb7d7bcbb88acbe0b0300000000001976a91461999077782b7298b813d83459a9c944da6460bd88ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal





Good.







Next: Let's try out test case 2.






Test case 1: tx2



The work directory is already set up.


Open a terminal. Change directory to the work directory.


Open the file create_transaction_2.py in a text editor.

Scroll to lines 44-79, which should be the section of text that lies between
##### START CONTROLS

and
##### END CONTROLS



Note: In create_transaction_2.py, all control values are strings (i.e. are contained within quotation marks).


Set the list
random_values
to contain the two 32-byte random values, in order:
5ab5bbe6fdc861a461a6721b2953d32be23c233e5ad5dc08d7c13daa95adbc17
75fc65686a47326556735be91491a06f380a9db682fe9d50f82ad8d8d8849c2f

Set the variable
random_value_type

to
"hex_bytes"
.



The variable
input_data
is a list that contains one or more inputs. Inputs are represented as dictionaries (key-value stores).

For each input dictionary (everything between and including a pair of braces {}):

Input 1:
- Set the value of the key
"txid"
to be the txid.
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826
- Set the value of the key
"previous_output_index"
to be the index of the unspent output.
0
- Set the value of the key
"private_key_hex"
to be the private key.
cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f
- If the input amount is in satoshi, set the value of the key
"satoshi_amount"
to be the input amount.
- If the input amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the input amount.
0.002 bitcoin
Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set, then
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.

Input 2:
- Set the value of the key
"txid"
to be the txid.
8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826
- Set the value of the key
"previous_output_index"
to be the index of the unspent output.
1
- Set the value of the key
"private_key_hex"
to be the private key.
7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af
- If the input amount is in satoshi, set the value of the key
"satoshi_amount"
to be the input amount.
- If the input amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the input amount.
0.00199614 bitcoin
Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set, then
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.



The variable
output_data
is a list that contains one or more outputs. Outputs are represented as dictionaries (key-value stores).

For each output dictionary (everything between and including a pair of braces {}):

Output 1:
- Set the value of the key
"address"
to be the output address.
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s
- If the output amount is in satoshi, set the value of the key
"satoshi_amount"
to be the output amount.
- If the output amount is in bitcoin, set the value of the key
"bitcoin_amount"
to be the output amount.
0.00399614 bitcoin
Note: Currently, if both of the keys
"satoshi_amount"
and
"bitcoin_amount"
are set,
"bitcoin_amount"
will take precedence. You can comment out the unused key of the pair by placing a number sign ('#') at the start of the relevant line.

Note: You must include an output that is directed to the change address.



Set the variable
change_address
to be output address 1.
17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s


If you have chosen to set the fee as an absolute number of satoshi, set the variable
fee
to be the fee amount and set the variable
fee_type
to be
"fee"
.
443 satoshi





Here is how the controls look:


create_transaction_2.py

python 2.7.13
	##### START CONTROLS
	# Note: Hex characters in input variable values must be lowercase. 
	random_values = [
		"5ab5bbe6fdc861a461a6721b2953d32be23c233e5ad5dc08d7c13daa95adbc17",
		"75fc65686a47326556735be91491a06f380a9db682fe9d50f82ad8d8d8849c2f", 
	]
	# random_value must be between 1 and 32 bytes. If random_value_type is "raw_bytes", then random_value must be between 1 and 32 ASCII characters. If random_value_type is "hex_bytes", then random_value must be an even number of hex characters and between 2 and 64 hex characters (1 byte is represented by 2 hex characters). 
	# Note: Every ECDSA signature (one for each input in a transaction) requires new random entropy. 
	# random_value_type options: ["raw_bytes", "hex_bytes"]
	random_value_type = "hex_bytes"
	input_data = [
		{
		"txid": "8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826",
		"previous_output_index": "0",
		"private_key_hex": "cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f",
		#"satoshi_amount": "242000", 
		"bitcoin_amount": "0.002", 
		},
		{
		"txid": "8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826",
		"previous_output_index": "1",
		"private_key_hex": "7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af",
		#"satoshi_amount": "242000", 
		"bitcoin_amount": "0.00199614", 
		},
	]
	output_data = [
		{
		"address": "17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s",
		#"satoshi_amount": "199775", 
		"bitcoin_amount": "0.00399614", 
		},
	]
	# note: all inputs and outputs are assumed to be Pay-To-Public-Key-Hash (P2PKH). 
	change_address = "17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s"
	# note: the fee will be subtracted from the amount that is being sent to the change address.
	fee = "443" # satoshi
	fee_rate = "3" # satoshi / byte
	# fee_type options: ["fee", "fee_rate"]
	fee_type = "fee"
	##### END CONTROLS




In the terminal, in the work directory, run the following command:
python create_transaction_2.py


At the end, the output should contain the signed transaction as a hex byte sequence without spaces.






[spiano@localhost work]$ python create_transaction_2.py


### START CREATION OF BITCOIN TRANSACTION

- Fee type: fee
- Fee: 443 (satoshi)
- Number of inputs (i.e. as-yet-unspent outputs): 2
- Number of outputs: 1
- Change address: 17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s
- Amount to be sent to the change address: 0.00399614
- Input addresses, with total-value-to-be-sent:
-- 19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4: 0.00199614
-- 1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8: 0.00200000
- Output addresses, with total-value-to-be-received:
-- 17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s: 0.00399614
- Total value of all inputs: 0.00399614
- Total value of all outputs: 0.00399614
- Total value of all inputs exactly matches total value of all outputs.
- Estimated transaction size: 402 bytes
- Final fee: 443 (satoshi)
- Final fee rate (using estimated transaction size): 1.1020 (satoshi per byte)
- Fee subtracted from amount-to-be-sent-to-change-address.
- New amount to be sent to change address: 399171 (satoshi)
- Size of signed transaction: 403 bytes
- (signed_tx_size - estimated_tx_size): 1 bytes
- Fee rate in signed transaction: 1.099 (satoshi / byte)

Input 0:
Input: {40 bytes unsigned, 66 bytes signable, 180 bytes signed}
- [data] previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
- [data] previous_output_index: 00000000 (4 bytes)
- [signed form] script_length: 8b (1 bytes)
- [signed form] scriptSig: 4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2ace (139 bytes)
- [data] sequence: ffffffff (4 bytes)
- [used for signing] private_key_hex: cbac84458fcfbb39f87fca7ab9a9ef2f76812a6f999a75dfa25dbcbb0ee3eb6f (32 bytes)
- [destination, goes into scriptSig] public_key_hex: 0426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2ace (65 bytes)
- [signable form] script_length_scriptPubKey: 19 (1 bytes)
- [signable form] scriptPubKey: 76a914f425b648af85989c1387d532393938fcb7d7bcbb88ac (25 bytes)
- [more info] address: 1PFw45xp5JUcLZfDQnMpto6yJpcjRLqrJ8
- [more info] previous_output_index_int: 0
- [more info] txid: 8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826

Input 1:
Input: {40 bytes unsigned, 66 bytes signable, 179 bytes signed}
- [data] previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
- [data] previous_output_index: 01000000 (4 bytes)
- [signed form] script_length: 8a (1 bytes)
- [signed form] scriptSig: 4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9 (138 bytes)
- [data] sequence: ffffffff (4 bytes)
- [used for signing] private_key_hex: 7d37c1a74d3b87d3994ac6db65b4f298f64a8ed6144edfdb2cacea70cf3070af (32 bytes)
- [destination, goes into scriptSig] public_key_hex: 043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9 (65 bytes)
- [signable form] script_length_scriptPubKey: 19 (1 bytes)
- [signable form] scriptPubKey: 76a91461999077782b7298b813d83459a9c944da6460bd88ac (25 bytes)
- [more info] address: 19u4WSjpp19yoAK9kdRyY9HJ7ad2S8s1E4
- [more info] previous_output_index_int: 1
- [more info] txid: 8f6318124bdd66f4643ccbfcbcb0a802924833505c015929dc7b1cab2ca02826

Output 0:
Output: {34 bytes}
- [data] value: 4317060000000000 (8 bytes)
- [data] script_length: 19 (1 bytes)
- [data] scriptPubKey: 76a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac (25 bytes)
- [more info] address: 17PZ6uyL59vPirNqqpMnB1kjEXkU7ske7s
- [more info] bitcoin_amount: 0.00399171
- [more info] satoshi_amount: 399171
- [more info] public_key_hash_hex: 4614b4066faf3ef831a20186f76381c25dd6ea82

Transaction (unsigned form): {124 bytes}
- version: 01000000 (4 bytes)
- input_count: 02 (1 bytes)
- Input 0: {40 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 00000000 (4 bytes)
-- script_length: [none] (0 bytes)
-- scriptSig: [none] (0 bytes)
-- sequence: ffffffff (4 bytes)
- Input 1: {40 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 01000000 (4 bytes)
-- script_length: [none] (0 bytes)
-- scriptSig: [none] (0 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 4317060000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Transaction (signable form 0): {155 bytes}
- version: 01000000 (4 bytes)
- input_count: 02 (1 bytes)
- Input 0 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 00000000 (4 bytes)
-- script_length_scriptPubKey: 19 (1 bytes)
-- scriptPubKey: 76a914f425b648af85989c1387d532393938fcb7d7bcbb88ac (25 bytes)
-- sequence: ffffffff (4 bytes)
- Input 1 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 01000000 (4 bytes)
-- script_length: 00 (1 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 4317060000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)
- hash_type_4_byte: 01000000 (4 bytes)

Transaction (signable form 1): {155 bytes}
- version: 01000000 (4 bytes)
- input_count: 02 (1 bytes)
- Input 0 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 00000000 (4 bytes)
-- script_length: 00 (1 bytes)
-- sequence: ffffffff (4 bytes)
- Input 1 [to be used to sign this signable form]: {66 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 01000000 (4 bytes)
-- script_length_scriptPubKey: 19 (1 bytes)
-- scriptPubKey: 76a91461999077782b7298b813d83459a9c944da6460bd88ac (25 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 4317060000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)
- hash_type_4_byte: 01000000 (4 bytes)

Transaction (signed form): {403 bytes}
- version: 01000000 (4 bytes)
- input_count: 02 (1 bytes)
- Input 0: {180 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 00000000 (4 bytes)
-- script_length: 8b (1 bytes)
-- scriptSig: 4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2ace (139 bytes)
-- sequence: ffffffff (4 bytes)
- Input 1: {179 bytes}
-- previous_output_hash: 2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f (32 bytes)
-- previous_output_index: 01000000 (4 bytes)
-- script_length: 8a (1 bytes)
-- scriptSig: 4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9 (138 bytes)
-- sequence: ffffffff (4 bytes)
- output_count: 01 (1 bytes)
- Output 0: {34 bytes}
-- value: 4317060000000000 (8 bytes)
-- script_length: 19 (1 bytes)
-- scriptPubKey: 76a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac (25 bytes)
- block_lock_time: 00000000 (4 bytes)

Signed transaction:
01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000

### END CREATION OF BITCOIN TRANSACTION





Signed transaction tx_2:

01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000










Now test equality of this newly-created transaction tx1 with the original tx2 from the source article:


[original tx2]
[spiano@localhost work]$ s1="01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000"


[newly created tx2]
[spiano@localhost work]$ s2="01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal







Good. Successful construction of a test transaction.





I'll confirm that this is identical to the transaction on the blockchain.


txid2:
92616e432f7807bf6d93252fcdca7efb139e56f7aa34835b50670ebc6a4b5649




A service for looking up the raw hex form of a transaction:
http://blockchain.info/tx/[txid]?format=hex


Browse to:
http://blockchain.info/tx/92616e432f7807bf6d93252fcdca7efb139e56f7aa34835b50670ebc6a4b5649?format=hex

Result:
01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000



Test equality:

[tx2 on the blockchain]
[spiano@localhost work]$ s1="01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000"


[newly created tx2]
[spiano@localhost work]$ s2="01000000022628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f000000008b4830450221008d4ecab2a74461cabcd3e5398ca6752594fbd758c3d10ff3a50c5bf45e460a4502206e7607d2f9ff8fa40489c041fdbbcaecfe723caabf28de9451a2be67d8705d1901410426f2ad4968ef7c5412a2cfac85c902f613b7406962477c931d25ddfb2957c494e70f832ac8dd50740c64297038c722542005b0c5191061e444c793b62d9b2aceffffffff2628a02cab1c7bdc2959015c5033489202a8b0bcfccb3c64f466dd4b1218638f010000008a4730440220184f1d7e9cfe7539cd40cae04e71dc21ce05de958620294cb2d490d45356bfc102203f4ae4afe33b93d1cc96c356735d7a4d3b90cfe62cab0d0cf71fd2f7718e99df0141043c8564e9fa9e9530699ffe7c5dd75077698ff96f3786e96bdbb210a109147cedacfdaa784eb4ba538556e8dd70455ba3627fe902f152940d70ffdd55acdb82a9ffffffff0143170600000000001976a9144614b4066faf3ef831a20186f76381c25dd6ea8288ac00000000"


[spiano@localhost work]$ [[ "$s1" = "$s2" ]] && echo equal || echo not-equal

equal





Good.










That's the end of this project.










[start of notes]



Changes from the original text:

- I have not always preserved the format of any excerpts from webpages on other sites (e.g. not preserving the original bold/italic styles, changing the list structures, not preserving hyperlinks).

- I have not always preserved the format of any computer output (e.g. from running bash commands). Examples: Setting input lines in bold text, adding/removing newlines in order to make a sequence of commands easier to read, using hyphens for lists and sublists instead of indentation, breaking wide tables into consecutive sections.


[end of notes]