I guide you through creating a Bitcoin transaction manually, step by step, without overloading your brain with much explanation.

Introduction

Andreas Antonopoulos mentioned once if he would be in prison he would reinvent an offline version of the Bitcoin consensus algorithm by playing Sudoku. Doing a role-playing like this on Bitcoin meetups makes a lot more sense than you would initially think, let me explain why.

In Andy Hunt's book, The Pragmatic Thinking and Learning he told a story of how he learned wall climbing. First he paid a lot of money for a lesson, then the instructor told the group go climb wall and went for a cup of coffee. After half an hour of climbing the wall and cursing the lazy instructor, it came back and started explaining how to climb the wall. The explanation made a lot more sense after some experience, than it would've mean before that. The reason is because there are two primary way of learning: synthesis and analysis. If you cannot identify which one was which in the wall-climbing class you were not paying attention my dear reader.

Role-playing is also a similar synthetic learning experience. Since playing Sudoku requires people and you are just a loner in front of your screen, I will not go down the role-playing rabbit hole, but instead will arm you with a similar synthetic learning experience. We are going to go through how to construct a Bitcoin transaction with your hand, without deep stuff. Just follow on and it will magically make sense why your wallet software is presenting you so much useless information. This, of course, is primarily important in allowing you to win /r/Bitcoin arguments. And, at the end of the day, isn't that what it's really all about? But also it will provide you a holistic understanding what you will unconsciously dig up every time when you get lost during Bitcoin development and thinkering.

To complete this tutorial you need...

...to be interested in Bitcoin. That is all, you do not need to be a programmer, all you need is curiosity.

Quick advice

Do not get scared of the length of this article, it is a surprisingly quick read, so keep on reading, I will make it fun!

Overview

First generate a Bitcoin private key. Next fund it. Build the transaction. Finally submit it to the network.

Video tutorial

I recorded my test quickly completing the necessary steps of this tutorial article on the Bitcoin mainnet.

Walkthrough

1. Generate your Bitcoin private key

First you have to make a decision where you want to work. On the mainnet or on the testnet . Working on the mainnet means working with real bitcoins, unlike on the testnet where the coins are worthless, because they are easily and freely acquirable.

According to Nicolas Dorier, creator of the NBitcoin C# library Practicing Bitcoin Programming on MainNet makes mistakes more memorable. But there are an other advantage for it, too: it would also enable you to send me the bitcoins what you are going to work with during this tutorial as a thanks for it.

Now go to https://www.bitaddress.org/ or https://www.bitaddress.org/?testnet=true, depending on which network you want to use and generate an address and a private key pair for yourself, and note it.

A mainnet Bitcoin address looks like this: 1JdtiE7aN6PekZHXihSJQcSDT2ZGbKNHVe

While a testnet one like this: mzXrxNNYrVJgwNL8PGZWTJYbktrhg14kQb

Mainnet private key: L3xU6CiTYtL8eBJBscMG8GPsZ2c7NNcX8BYrih4WGmoztLfXByL7

Testnet private key: cTLUtfv2Sq5K1yjs5fcRmxitL51i35WmVxaCGUCbfCp722V5aMVY

2. Acquire some bitcoins

Next send some bitcoins to the address . If you are working on the testnet google "get bitcoin testnet coins" and you find many website willing to give you some for free, like this one. Check out the your address in a block explorer. Here is a block explorer for the testnet, too: https://testnet.smartbit.com.au/

Take a look at your address:

Then find your transaction, what you just sent to your address. Here is mine.

And note the transaction id: 22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c

Now that we prepared the environment, we can finally start constructing our transaction by hand.

3. Construct the a transaction

So far you have 3 lines of information, probably kept in a very secure .txt file:

Private key: cTLUtfv2Sq5K1yjs5fcRmxitL51i35WmVxaCGUCbfCp722V5aMVY Address: mzXrxNNYrVJgwNL8PGZWTJYbktrhg14kQb Transaction id: 22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c

a) Inspect our funding transaction

First let's take a look at our transaction. We are going to use QBit Ninja for that.

http:// tapi.qbit.ninja /transactions/ 22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c

If you are working on the mainnet you want to use api.qbit.ninja , instead of tapi.qbit.ninja .

Take a look at the output:

{ "transaction": "0100000002d9dced2b6fc80c706d3564670cb6706afe7a798863a9218efcdcf415d58f0f82000000006a473044022030b8bea478444bd52f08de33b082cde1176d3137111f506eefefa91b47b1f6bf02204f12746abd1aeac5805872d163592cf145967fa0619339a9c5348d674852ef4801210224ec1e4c270ce373e6999eebfa01d0a7e7db3c537c026f265233350d5aab81fbfeffffffa0706db65c5e3594d43df5a2a8b6dfd3c9ee506b678f8c26f7820b324b26aa0f000000006a473044022061b718034f876590d6d80bac77a63248b2548d934849acd02c4f4236309e853002201aded6b24f553b6902cf571276b37b12f76b75650164d8738c74469b4edd547e012103d649294a0ca4db920a69eacd6a75cb8a38ae1b81129900621ce45e6ba3438a7bfeffffff0280a90300000000001976a914d0965947ebb329b776328624ebde8f8b32dc639788ac1cc80f00000000001976a914c2a420d34fc86cff932b8c3191549a0ddfd2b0d088acba770f00", "transactionId": "22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "isCoinbase": false, "block": null, "spentCoins": [ { "transactionId": "820f8fd515f4dcfc8e21a96388797afe6a70b60c6764356d700cc86f2beddcd9", "index": 0, "value": 100000, "scriptPubKey": "76a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac", "redeemScript": null }, { "transactionId": "0faa264b320b82f7268c8f676b50eec9d3dfb6a8a2f53dd494355e5cb66d70a0", "index": 0, "value": 1180443, "scriptPubKey": "76a914f3821cff5a90328271d8596198f68e97fbe2ea0e88ac", "redeemScript": null } ], "receivedCoins": [ { "transactionId": "22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "index": 0, "value": 240000, "scriptPubKey": "76a914d0965947ebb329b776328624ebde8f8b32dc639788ac", "redeemScript": null }, { "transactionId": "22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "index": 1, "value": 1034268, "scriptPubKey": "76a914c2a420d34fc86cff932b8c3191549a0ddfd2b0d088ac", "redeemScript": null } ], "firstSeen": "2016-10-31T09:13:18.4420023+00:00", "fees": 6175 }

Neat, isn't it? How long were you trying to understand it, before got to reading this sentence? As I said in the beginning, this tutorial aims to be more of a synthetic learning experience, rather than an analytic one, so do not get lost in the details. If spent more then a few minutes you are doing it wrong, next time do it better.

During this guide I will refer you to various tools to see a lot. This is also aligned with my goal of providing you a synthetic learning experience and hopefully by the end when you reached your goal to broadcast your transaction you will feel like you have learned something, even though you might cannot put into words what it is. This is called: experience. Now to practice the not getting lost in the details part take a look at the same query I made to Smartbit's API:

{ "success":true, "transaction":{ "txid":"22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "hash":"22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "block":null, "confirmations":0, "version":"1", "locktime":1013690, "time":null, "first_seen":1477905197, "propagation":"1.0000", "double_spend":false, "size":372, "vsize":372, "input_amount":"0.01280443", "input_amount_int":1280443, "output_amount":"0.01274268", "output_amount_int":1274268, "fee":"0.00006175", "fee_int":6175, "fee_size":"16.59946237", "coinbase":false, "input_count":2, "inputs":[ { "addresses":[ "n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi" ], "value":"0.00100000", "value_int":100000, "txid":"820f8fd515f4dcfc8e21a96388797afe6a70b60c6764356d700cc86f2beddcd9", "vout":0, "script_sig":{ "asm":"3044022030b8bea478444bd52f08de33b082cde1176d3137111f506eefefa91b47b1f6bf02204f12746abd1aeac5805872d163592cf145967fa0619339a9c5348d674852ef4801 0224ec1e4c270ce373e6999eebfa01d0a7e7db3c537c026f265233350d5aab81fb", "hex":"473044022030b8bea478444bd52f08de33b082cde1176d3137111f506eefefa91b47b1f6bf02204f12746abd1aeac5805872d163592cf145967fa0619339a9c5348d674852ef4801210224ec1e4c270ce373e6999eebfa01d0a7e7db3c537c026f265233350d5aab81fb" }, "type":"pubkeyhash", "witness":null, "sequence":4294967294 }, { "addresses":[ "n3iWN6buGo6yY3HaupTa37bkvqtqSqLfhG" ], "value":"0.01180443", "value_int":1180443, "txid":"0faa264b320b82f7268c8f676b50eec9d3dfb6a8a2f53dd494355e5cb66d70a0", "vout":0, "script_sig":{ "asm":"3044022061b718034f876590d6d80bac77a63248b2548d934849acd02c4f4236309e853002201aded6b24f553b6902cf571276b37b12f76b75650164d8738c74469b4edd547e01 03d649294a0ca4db920a69eacd6a75cb8a38ae1b81129900621ce45e6ba3438a7b", "hex":"473044022061b718034f876590d6d80bac77a63248b2548d934849acd02c4f4236309e853002201aded6b24f553b6902cf571276b37b12f76b75650164d8738c74469b4edd547e012103d649294a0ca4db920a69eacd6a75cb8a38ae1b81129900621ce45e6ba3438a7b" }, "type":"pubkeyhash", "witness":null, "sequence":4294967294 } ], "output_count":2, "outputs":[ { "addresses":[ "mzXrxNNYrVJgwNL8PGZWTJYbktrhg14kQb" ], "value":"0.00240000", "value_int":240000, "n":0, "script_pub_key":{ "asm":"OP_DUP OP_HASH160 d0965947ebb329b776328624ebde8f8b32dc6397 OP_EQUALVERIFY OP_CHECKSIG", "hex":"76a914d0965947ebb329b776328624ebde8f8b32dc639788ac" }, "req_sigs":1, "type":"pubkeyhash", "spend_txid":null }, { "addresses":[ "myG816eAyq41xY6ZTfYTBNmE8n4aN1YM7x" ], "value":"0.01034268", "value_int":1034268, "n":1, "script_pub_key":{ "asm":"OP_DUP OP_HASH160 c2a420d34fc86cff932b8c3191549a0ddfd2b0d0 OP_EQUALVERIFY OP_CHECKSIG", "hex":"76a914c2a420d34fc86cff932b8c3191549a0ddfd2b0d088ac" }, "req_sigs":1, "type":"pubkeyhash", "spend_txid":null } ], "tx_index":12079340, "block_index":null } }

Even more confusing, isn't it? I guess the only thing you should've been noticing is that it is different from the output that Ninja gave me.

Try it yourself: https://testnet-api.smartbit.com.au/v1/blockchain/tx/22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c

For main net: it's https://api.smartbit.com.au

Wow, this looks even worse, than mine, right? The outputs are in json form, you can beautify it with a json beautifier, like: https://jsonformatter.curiousconcept.com/

Did you spot the outputs contain your transaction id? As an exercise try to find them. (It's ok to use ctrl+f).

What? The Ninja has it in 3 places, and Smartbit only 2? This totally doesn't make any sense, right? Or does it?

b) Construct your transaction

So far I was inspecting my funding transaction. Now take a look at a possible way I can spend this transaction, but before that let's figure out what do I mean by spending a transaction? Why did I say transaction, don't I want to spend bitcoins? But where are the bitcoins? Now our mental model of transactions are going to start failing here. But fear no more, I have hired the best illustrator on the field to help us clear up our misconceptions:



You have a bitcoin address where you send many transactions.



Now imagine the output of tx1 has been already spent, so you can only access your bitcoins on tx2 and tx3 . So the outputs on the tx2 and tx3 are unspent. You are still able to spend them if you have the corresponding private key for the address. They are called Unspent Transaction Outputs or UTXOs.

Ok, but how do you spend them? By connecting an UTXO with an input of an other transaction what you want to create and then broadcast this connection. That's roughly what we call spending bitcoins.



Let's spend the tx2 by connecting its output to a new tx. And then sign that connection with the private key of the tx2.

Now if you were listening carefully you are starting to recognize the most important parts of a transaction:

an input ,

, an output ,

, a bitcoin address ,

, and a signature of the whole transaction with the private key that is able to spend the input.

The terminology I used here is an outright lie and I would be the first one who starts cursing the author for it on Reddit, but at least you can understand it. Now let me tell you what is the correct way to describe the structure of a transaction:

Table stolen from Mastering Bitcoin book Size Field Description 4 bytes Version Specifies which rules this transaction follows 1–9 bytes (VarInt) Input Counter How many inputs are included Variable Inputs One or more transaction inputs 1–9 bytes (VarInt) Output Counter How many outputs are included Variable Outputs One or more transaction outputs 4 bytes Locktime A Unix timestamp or block number

The version and the locktime is not important for us, so let's not talk about them. The input and output counters are simply two numbers telling you how many inputs and outputs you have. But this calculation can be easily done by any machine so they won't be important for us, neither. Wait... I told you there is only one input and only one output for a transaction, well I lied again. There can be more, but we will mostly ignore it in our tutorial, since in our transaction there will only be one input and one output.

So where is the bitcoin address? You specify it in the output. The signature? In the input.

Let me illustrate to you:

{ "in":[ { "prev_out":{ "hash":"22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "n":0 }, "scriptSig":"3044022020ea35009d17d25b8a926675ddf0045c397d3df55b0ae115ef80db7849529b9302201f13bd2cbd1ca0a24e2c5ab28030aa9b7b3dcacf175652dad82fe9d5973f340901 0281d32f6d35b46906eb562ff8b48f4f938c077bcb29d46b0550ff5c61883d3d2d" } ], "out":[ { "value":"0.00226000", "scriptPubKey":"OP_DUP OP_HASH160 c2a420d34fc86cff932b8c3191549a0ddfd2b0d0 OP_EQUALVERIFY OP_CHECKSIG" } ] }

It is an actual way I can spend my funding transaction. Copypaste it and change the values with your own transaction you are about to create, here is how:

in:prev_out:hash

Do you know what this is? The transaction id of my funding transaction. This is the transaction you sent to your initial address. Go ahead and change it.

in:prev_out:n

How about this? Check your funding transaction with Smartbit's HTTP API again:

Mainnet: https://www.smartbit.com.au/api

Testnet: https://testnet.smartbit.com.au/api

Find Transaction and select the Parameters tab. Fill out the Transaction ID field and click GET .

Take a look at the transaction:outputs . How many outputs you see there? I see two in my transaction:

"addresses": [ "mzXrxNNYrVJgwNL8PGZWTJYbktrhg14kQb" ], "value": "0.00240000", "n": 0 "addresses": [ "myG816eAyq41xY6ZTfYTBNmE8n4aN1YM7x" ], "value": "0.01034268", "n": 1

You will probably see more than one, too. I will skip the explanation and leave you with the job of figuring out what the in:prev_out:n should be in the transaction you are creating.

in:scriptSig

This is the signature. Let's just not rush filling this out right now. You will have to sign the transaction after you constructed it.

out:scriptPubKey

This is the bitcoin address I want to send my money. Well, sort of. Let's examine the relationship a deeper level:

"scriptPubKey":"OP_DUP OP_HASH160 c2a420d34fc86cff932b8c3191549a0ddfd2b0d0 OP_EQUALVERIFY OP_CHECKSIG"

Do you see those random numbers I made bold? That is actually the hash of a public key, whatever that means. Bitcoin Addresses are composed of a version byte which identify the network where to use the address and the hash of a public key.

Ok, this is might be a little confusing I didn't even tell you what is a public key, so don't worry if you don't understand it, it doesn't matter for us now, the only takeaway here is this relationship:

bitcoin address = scriptPubKey + network identifier

So it does not matter if you are working on the testnet or on the mainnet the scriptPubKey will be the same.

So how to get the scriptPubKey from a Bitcoin address? I did not find any online tool, that you could use for that so I built a little software that will help you with it. Download here: https://github.com/nopara73/BitcoinGenie

After you run it tell the software:

I want to know a scriptPubKey

And it will ask for a Bitcoin address from you. And gives back its scriptPubKey.

If you are on the testnet you can specify any address (scriptPubkey), even the one I used here. It doesn't matter, testnet coins are worthless. If you are on the mainnet you want to send it to yourself or to me, here: 1LYLuYMXkCXDxSfsNoDp8FCb2mA36r29u7

out:value

This is more interesting than it initially seems like.

Suppose if you have one input that has 1 bitcoin on it and you want to send that to one output why do you even have to specify the value for the output? What if you want to send only half a bitcoin? Then you would have to specify the input and specify two output with a value of 0.5btc to the address you want to send and with a value of 0.5btc back to your address. Remember you have to spend a whole input, you can't just spend half of it!

But look at mine:

I specified "value":"0.00226000" . But look at my input: https://testnet.smartbit.com.au/tx/22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c

It has 0.0024btc on it. So why didn't I specified this to my output? What's the deal with the remaining (0.0024-0.00226)? It goes nowhere. Does it get destroyed then? No. This goes to the miners. This is the transaction fee. So how do you calculate the miner fee? The higher the fee, the faster your transaction confirms.

So how do you make sure your fee is high enough to confirm within 1-2 blocks?

It is pretty easy, just go here: https://bitcoinfees.21.co/api/v1/fees/recommended

It will tell you how much fee you need to pay relative to your transaction size. Here is its output at the time I am writing this article:

{"fastestFee":70,"halfHourFee":70,"hourFee":60}

This means to get the fastest fee I have to multiply the size of my transaction by 70. Normal bitcoin transactions are about 200byte, so my transaction fee should be 70*200=14000. Wow, that's a lot of bitcoins isn't it? Not really these are satoshis. 1 satoshi = 0.00000001 bitcoin. So my 14000satoshi is 0.00014btc. That's 0.1usd at the time of writing. (The price of Bitcoin ~700usd, 2016.10.30.)

So what is your transaction fee should be? And what is the out:value you should specify?

c) Sign your transaction

Now let's construct our transaction with the BitcoinGenie, so we can submit it to the bitcoin network. I warn you, don't try to play with it too much, I don't handle errors and it doesn't handle inputs outside the scope of this tutorial.

Now run the Genie again and tell it:

I want to create a transaction

At this point you are probably able to answer every question of the Genie by yourself.

Stop reading and try it alone.

Howdy friend, I am your Bitcoin genie. What's your wish? <code>I want to create a transaction</code> Great, here is a template for your transaction: { "hash": "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43", "ver": 1, "vin_sz": 0, "vout_sz": 0, "lock_time": 0, "size": 10, "in": [], "out": [] } Specify its inputs and outputs for me First the inputs What is the id of the transaction that you want to spend? <code>22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c</code> Which output of it do you want to spend? What is its id? <code>0</code> Awesome, here is how your transaction looks like now: { "hash": "c8f0ba9cc5083f26cafb5062304c183986c1f4602a27da0c83634af4afca41ec", "ver": 1, "vin_sz": 1, "vout_sz": 0, "lock_time": 0, "size": 51, "in": [ { "prev_out": { "hash": "22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "n": 0 }, "scriptSig": "" } ], "out": [] } Now let's construct the output What is the scriptPubKey you want to send your bitcoins to? <code>OP_DUP OP_HASH160 c2a420d34fc86cff932b8c3191549a0ddfd2b0d0 OP_EQUALVERIFY OP_CHECKSIG</code> How much bitcoins you want to send there? <code>0.00226</code> Awesome, here is how your transaction looks like now: { "hash": "c76361f91fc762455a3aab1b27c58369d8973e0141a9725095d318aba99bca33", "ver": 1, "vin_sz": 1, "vout_sz": 1, "lock_time": 0, "size": 85, "in": [ { "prev_out": { "hash": "22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "n": 0 }, "scriptSig": "" } ], "out": [ { "value": "0.00226000", "scriptPubKey": "OP_DUP OP_HASH160 c2a420d34fc86cff932b8c3191549a0ddfd2b0d0 OP_EQUALVERIFY OP_CHECKSIG" } ] } Now tell me your privateKey so I can sign the transaction: <code>cTLUtfv2Sq5K1yjs5fcRmxitL51i35WmVxaCGUCbfCp722V5aMVY</code> You signed your transaction on the TestNet Here is how your final transaction looks like: { "hash": "2a6ed4792dcefd16b7d213f2171ab75e407e53d6a4a43d1d5911b720f4a89857", "ver": 1, "vin_sz": 1, "vout_sz": 1, "lock_time": 0, "size": 191, "in": [ { "prev_out": { "hash": "22ab5e9b703c0d4cb6023e3a1622b493adc8f83a79771c83a73dfa38ef35b07c", "n": 0 }, "scriptSig": "3044022020ea35009d17d25b8a926675ddf0045c397d3df55b0ae115ef80db7849529b9302201f13bd2cbd1ca0a24e2c5ab28030aa9b7b3dcacf175652dad82fe9d5973f340901 0281d32f6d35b46906eb562ff8b48f4f938c077bcb29d46b0550ff5c61883d3d2d" } ], "out": [ { "value": "0.00226000", "scriptPubKey": "OP_DUP OP_HASH160 c2a420d34fc86cff932b8c3191549a0ddfd2b0d0 OP_EQUALVERIFY OP_CHECKSIG" } ] } Here is the hex of your transaction: 01000000017cb035ef38fa3da7831c77793af8c8ad93b422163a3e02b64c0d3c709b5eab22000000006a473044022020ea35009d17d25b8a926675ddf0045c397d3df55b0ae115ef80db7849529b9302201f13bd2cbd1ca0a24e2c5ab28030aa9b7b3dcacf175652dad82fe9d5973f340901210281d32f6d35b46906eb562ff8b48f4f938c077bcb29d46b0550ff5c61883d3d2dffffffff01d0720300000000001976a914c2a420d34fc86cff932b8c3191549a0ddfd2b0d088ac00000000 Press enter to exit...

Great job, finally we can move on and broadcast it!

4. Broadcast your to the network

Do you see the hex of your transaction? Now note that. That is what we will submit to the Bitcoin network.

But first let's do quick check. Go to Smartbit's API and choose DECODE TRANSACTION :

Mainnet: https://www.smartbit.com.au/api

Testnet: https://testnet.smartbit.com.au/api

Copypaste your hex. Everything looks fine? Great, now go ahead and PUSH TRANSACTION on the same page.

{ "success":true, "txid":"2a6ed4792dcefd16b7d213f2171ab75e407e53d6a4a43d1d5911b720f4a89857" }

If you get the same output then you have done everything well.

Check out the transaction you just made: https://testnet.smartbit.com.au/tx/2a6ed4792dcefd16b7d213f2171ab75e407e53d6a4a43d1d5911b720f4a89857

Conclusion

Congratulations, you made it. You know more about Bitcoin than most of the Bitcoin enthusiasts out there. You are now officially a Bitcoin wizard.