You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: bitcoin_transfer/spend_your_coin.md
+62-49
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
So now that you know what a **bitcoin address**, a **ScriptPubKey**, a **private key** and a **miner** are, you will make your first **transaction** by hand.
4
4
5
-
As you proceed through this lesson you will add code line by line as it is presented to build a method that will leave feedback for the book in a Twitter style message.
5
+
As you proceed through this lesson you will add code line by line as it is presented to build a method that will leave feedback for the book in a Twitter style message.
6
6
7
-
Let’s start by looking at the **transaction** that contains the **TxOut** that you want to spend as we did previously:
7
+
Let’s start by looking at the **transaction** that contains the **TxOut** that you want to spend as we did previously:
8
8
9
-
Create a new **Console Project**(>.net45) and install **QBitNinja.Client** NuGet.
9
+
Create a new **Console Project**\(>.net45\) and install **QBitNinja.Client** NuGet.
10
10
11
-
Have you already generated and noted down a private key yourself? Have you already get the corresponding bitcoin address and sent some funds there? If not, don't worry, I quickly reiterate how you can do it:
11
+
Have you already generated and noted down a private key yourself? Have you already get the corresponding bitcoin address and sent some funds there? If not, don't worry, I quickly reiterate how you can do it:
12
12
13
13
```cs
14
14
varnetwork=Network.Main;
@@ -19,11 +19,11 @@ var address = bitcoinPrivateKey.GetAddress();
19
19
20
20
Console.WriteLine(bitcoinPrivateKey);
21
21
Console.WriteLine(address);
22
-
```
22
+
```
23
23
24
-
Note the **bitcoinPrivateKey** and the **address**, send some coins there and note the transaction id (you can find it, probably, in your wallet software or with a blockexplorer, like [blockchain.info](http://blockchain.info/)).
24
+
Note the **bitcoinPrivateKey** and the **address**, send some coins there and note the transaction id \(you can find it, probably, in your wallet software or with a blockexplorer, like [blockchain.info](http://blockchain.info/)\).
25
25
26
-
Import your private key:
26
+
Import your private key:
27
27
28
28
```cs
29
29
varbitcoinPrivateKey=new
@@ -33,23 +33,25 @@ var address = bitcoinPrivateKey.GetAddress();
Do you remember the main questions? **From where, to where and how much?**
81
83
Constructing the **TxIn** and adding it to the transaction is the answer to the "from where" question.
82
-
Constructing the **TxOut** and adding it to the transaction is the answer to the remaining ones.
84
+
Constructing the **TxOut** and adding it to the transaction is the answer to the remaining ones.
83
85
84
86
The donation address of this book is: [1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB](https://blockchain.info/address/1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB)
85
87
This money goes into my "Coffee and Sushi Wallet" that will keep me fed and compliant while writing the rest of the book.
86
-
If you succeed in completing this challenge you will be able to find your contribution amongst the **Hall of the Makers** on http://n.bitcoin.ninja/ (ordered by generosity).
88
+
If you succeed in completing this challenge you will be able to find your contribution amongst the **Hall of the Makers** on [http://n.bitcoin.ninja/](http://n.bitcoin.ninja/)\(ordered by generosity\).
If you want to send **0.5 BTC** from a **transaction input** with **1 BTC** you actually have to spend it all!
101
+
102
+
If you want to send **0.5 BTC** from a **transaction input** with **1 BTC** you actually have to spend it all!
97
103
As the diagram shows below, your **transaction output** specifies **0.5** BTC to Hall of The Makers and **0.4999** back to you.
98
104
What happens to the remaining **0.0001 BTC**? This is the miner fee in order to incentivize them to add this transaction into their next block.
99
105
100
-

106
+

101
107
102
108
```cs
103
109
TxOuthallOfTheMakersTxOut=newTxOut()
@@ -114,10 +120,10 @@ TxOut changeBackTxOut = new TxOut()
114
120
115
121
transaction.Outputs.Add(hallOfTheMakersTxOut);
116
122
transaction.Outputs.Add(changeBackTxOut);
117
-
```
123
+
```
118
124
119
125
We can do some fine tuning here.
120
-
You can check the address I am working with in this chapter's examples on a blockexplorer (I am working on the testnet): http://tbtc.blockr.io/address/info/mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv
126
+
You can check the address I am working with in this chapter's examples on a blockexplorer \(I am working on the testnet\): [http://tbtc.blockr.io/address/info/mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv](http://tbtc.blockr.io/address/info/mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv)
121
127
122
128
```cs
123
129
// How much you want to TO
@@ -129,11 +135,12 @@ var hallOfTheMakersAmount = new Money(0.5m, MoneyUnit.BTC);
To sum up take a look at my whole transaction before we sign it:
173
-
I have 3 **TxOut**, 2 with **value**, 1 without **value** (which contains the message). You can notice the differences between the **scriptPubKey**s of the "normal" **TxOut**s and the **scriptPubKey** of the **TxOut** with the message:
181
+
I have 3 **TxOut**, 2 with **value**, 1 without **value**\(which contains the message\). You can notice the differences between the **scriptPubKey**s of the "normal" **TxOut**s and the **scriptPubKey** of the **TxOut** with the message:
174
182
175
183
```json
176
184
{
@@ -204,41 +212,44 @@ I have 3 **TxOut**, 2 with **value**, 1 without **value** (which contains the me
204
212
}
205
213
]
206
214
}
207
-
```
215
+
```
208
216
209
-
Take a closer look at **TxIn**. We have **prev_out** and **scriptSig** there.
210
-
**Exercise:** try to figure out what **scriptSig** will be and how to get it in our code before you read further!
217
+
Take a closer look at **TxIn**. We have **prev\_out** and **scriptSig** there.
218
+
**Exercise:** try to figure out what **scriptSig** will be and how to get it in our code before you read further!
211
219
212
-
Let's check out the **hash** of **prev_out** in a blockexplorer: http://tbtc.blockr.io/api/v1/tx/info/e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3
213
-
In **prev_out****n** is 1. Since we are indexing from 0, this means I want to spend the second output of the transaction.
214
-
In the blockexplorer we can see the corresponding address is ```mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv``` and I can get the scriptSig from the address like this:
220
+
Let's check out the **hash** of **prev\_out** in a blockexplorer: [http://tbtc.blockr.io/api/v1/tx/info/e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3](http://tbtc.blockr.io/api/v1/tx/info/e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3)
221
+
In **prev\_out****n** is 1. Since we are indexing from 0, this means I want to spend the second output of the transaction.
222
+
In the blockexplorer we can see the corresponding address is `mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv` and I can get the scriptSig from the address like this:
Now that we have created the transaction, we must sign it. In other words, you will have to prove that you own the TxOut that you referenced in the input.
231
+
Now that we have created the transaction, we must sign it. In other words, you will have to prove that you own the TxOut that you referenced in the input.
224
232
225
233
Signing can be [complicated](https://en.bitcoin.it/w/images/en/7/70/Bitcoin_OpCheckSig_InDetail.png), but we’ll make it simple.
226
234
227
-
First let's revisit the **scriptSig** of **in** and how we can get it from code. Remember, we copy/pasted the address above from a blockexplorer, now let's get it from our QBitNinja transactionResponse:
235
+
First let's revisit the **scriptSig** of **in** and how we can get it from code. Remember, we copy/pasted the address above from a blockexplorer, now let's get it from our QBitNinja transactionResponse:
Then you need to provide your private key in order to sign the transaction:
241
+
Then you need to provide your private key in order to sign the transaction:
234
242
235
243
```cs
236
244
transaction.Sign(bitcoinPrivateKey, false);
237
-
```
245
+
```
238
246
239
247
### Propagate your transactions
240
-
Congratulations, you have signed your first transaction! Your transaction is ready to roll! All that is left is to propagate it to the network so the miners can see it.
241
-
#### With QBitNinja:
248
+
249
+
Congratulations, you have signed your first transaction! Your transaction is ready to roll! All that is left is to propagate it to the network so the miners can see it.
Console.WriteLine("Success! You can check out the hash of the transaciton in any block explorer:");
253
264
Console.WriteLine(transaction.GetHash());
254
265
}
255
-
```
266
+
```
256
267
257
-
#### With your own Bitcoin Core:
268
+
#### With your own Bitcoin Core:
258
269
259
270
```cs
260
271
using (varnode=Node.ConnectToLocal(network)) //Connect to the node
@@ -266,13 +277,15 @@ using (var node = Node.ConnectToLocal(network)) //Connect to the node
266
277
node.SendMessage(newTxPayload(transaction));
267
278
Thread.Sleep(500); //Wait a bit
268
279
}
269
-
```
280
+
```
270
281
271
282
The **using** code block will take care of closing the connection to the node. That's it!
272
283
273
284
You can also connect directly to the Bitcoin network, however I advise you to connect to your own trusted node as it is faster and easier.
274
-
275
-
## Need more practice?
285
+
286
+
## Need more practice?
287
+
276
288
YouTube: [How to make your first transaction with NBitcoin](https://www.youtube.com/watch?v=X4ZwRWIF49w)
277
289
CodeProject: [Create a Bitcoin transaction by hand.](http://www.codeproject.com/Articles/1151054/Create-a-Bitcoin-transaction-by-hand)
278
-
CodeProject: [DotNetWallet - Build your own Bitcoin wallet in C#](https://www.codeproject.com/script/Articles/ArticleVersion.aspx?waid=214550&aid=1115639)
290
+
CodeProject: [DotNetWallet - Build your own Bitcoin wallet in C\#](https://www.codeproject.com/script/Articles/ArticleVersion.aspx?waid=214550&aid=1115639)
0 commit comments