Skip to content

Commit 2646c36

Browse files
committed
Updates bitcoin_transfer/spend_your_coin.md
Auto commit by GitBook Editor
1 parent 41b6883 commit 2646c36

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

bitcoin_transfer/spend_your_coin.md

+62-49
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
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.
44

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.
66

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:
88

9-
Create a new **Console Project** (>.net45) and install **QBitNinja.Client** NuGet.
9+
Create a new **Console Project** \(>.net45\) and install **QBitNinja.Client** NuGet.
1010

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:
1212

1313
```cs
1414
var network = Network.Main;
@@ -19,11 +19,11 @@ var address = bitcoinPrivateKey.GetAddress();
1919

2020
Console.WriteLine(bitcoinPrivateKey);
2121
Console.WriteLine(address);
22-
```
22+
```
2323

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/)\).
2525

26-
Import your private key:
26+
Import your private key:
2727

2828
```cs
2929
var bitcoinPrivateKey = new
@@ -33,23 +33,25 @@ var address = bitcoinPrivateKey.GetAddress();
3333

3434
Console.WriteLine(bitcoinPrivateKey); // cSZjE4aJNPpBtU6xvJ6J4iBzDgTmzTjbq8w2kqnYvAprBCyTsG4x
3535
Console.WriteLine(address); // mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv
36-
```
36+
```
37+
38+
And finally get the transaction info:
3739

38-
And finally get the transaction info:
3940
```cs
4041
var client = new QBitNinjaClient(network);
4142
var transactionId = uint256.Parse("e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3");
4243
var transactionResponse = client.GetTransaction(transactionId).Result;
4344

4445
Console.WriteLine(transactionResponse.TransactionId); // e44587cf08b4f03b0e8b4ae7562217796ec47b8c91666681d71329b764add2e3
4546
Console.WriteLine(transactionResponse.Block.Confirmations);
46-
```
47+
```
4748

4849
Now we have every bit of information we need to create our transactions. The main questions are: **from where, to where and how much?**
4950

5051
### From where?
5152

52-
In our case, we want to spend the second outpoint. Here's how we have figured this out:
53+
In our case, we want to spend the second outpoint. Here's how we have figured this out:
54+
5355
```cs
5456
var receivedCoins = transactionResponse.ReceivedCoins;
5557
OutPoint outPointToSpend = null;
@@ -63,7 +65,7 @@ foreach (var coin in receivedCoins)
6365
if(outPointToSpend == null)
6466
throw new Exception("TxOut doesn't contain our ScriptPubKey");
6567
Console.WriteLine("We want to spend {0}. outpoint:", outPointToSpend.N + 1);
66-
```
68+
```
6769

6870
For the payment you will need to reference this outpoint in the transaction. You create a transaction as follows:
6971

@@ -73,31 +75,35 @@ transaction.Inputs.Add(new TxIn()
7375
{
7476
PrevOut = outPointToSpend
7577
});
76-
```
78+
```
7779

7880
### To where?
7981

8082
Do you remember the main questions? **From where, to where and how much?**
8183
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.
8385

8486
The donation address of this book is: [1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB](https://blockchain.info/address/1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB)
8587
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\).
89+
8790
```cs
8891
var hallOfTheMakersAddress = new BitcoinPubKeyAddress("1KF8kUVHK42XzgcmJF4Lxz4wcL5WDL97PB");
89-
```
92+
```
93+
9094
If you are working on the testnet, send the testnet coins to any testnet address.
95+
9196
```cs
9297
var hallOfTheMakersAddress = BitcoinAddress.Create("mzp4No5cmCXjZUpf112B1XWsvWBfws5bbB");
93-
```
98+
```
9499

95100
### How much?
96-
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!
97103
As the diagram shows below, your **transaction output** specifies **0.5** BTC to Hall of The Makers and **0.4999** back to you.
98104
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.
99105

100-
![](../assets/SpendTx.png)
106+
![](../assets/SpendTx.png)
101107

102108
```cs
103109
TxOut hallOfTheMakersTxOut = new TxOut()
@@ -114,10 +120,10 @@ TxOut changeBackTxOut = new TxOut()
114120

115121
transaction.Outputs.Add(hallOfTheMakersTxOut);
116122
transaction.Outputs.Add(changeBackTxOut);
117-
```
123+
```
118124

119125
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)
121127

122128
```cs
123129
// How much you want to TO
@@ -129,11 +135,12 @@ var hallOfTheMakersAmount = new Money(0.5m, MoneyUnit.BTC);
129135
*/
130136
var minerFee = new Money(0.0001m, MoneyUnit.BTC);
131137
// How much you want to spend FROM
132-
var txInAmount = receivedCoins[(int) outPointToSpend.N].TxOut.Amount;
138+
var txInAmount = (Money)receivedCoins[(int) outPointToSpend.N].Amount;
133139
Money changeBackAmount = txInAmount - hallOfTheMakersAmount - minerFee;
134140
```
135141

136-
Let's add our calculated values to our TxOuts:
142+
Let's add our calculated values to our TxOuts:
143+
137144
```cs
138145
TxOut hallOfTheMakersTxOut = new TxOut()
139146
{
@@ -146,18 +153,19 @@ TxOut changeBackTxOut = new TxOut()
146153
Value = changeBackAmount,
147154
ScriptPubKey = bitcoinPrivateKey.ScriptPubKey
148155
};
149-
```
156+
```
157+
158+
And add them to our transaction:
150159

151-
And add them to our transaction:
152160
```cs
153161
transaction.Outputs.Add(hallOfTheMakersTxOut);
154162
transaction.Outputs.Add(changeBackTxOut);
155-
```
163+
```
156164

157165
### Message on The Blockchain
158166

159167
Now add your feedback! This must be less than 40 bytes, or it will crash the application.
160-
This feedback, along with your transaction will appear (after your transaction is confirmed) in the [Hall of The Makers](http://n.bitcoin.ninja/).
168+
This feedback, along with your transaction will appear \(after your transaction is confirmed\) in the [Hall of The Makers](http://n.bitcoin.ninja/).
161169

162170
```cs
163171
var message = "nopara73 loves NBitcoin!";
@@ -167,10 +175,10 @@ transaction.Outputs.Add(new TxOut()
167175
Value = Money.Zero,
168176
ScriptPubKey = TxNullDataTemplate.Instance.GenerateScriptPubKey(bytes)
169177
});
170-
```
178+
```
171179

172180
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:
174182

175183
```json
176184
{
@@ -204,41 +212,44 @@ I have 3 **TxOut**, 2 with **value**, 1 without **value** (which contains the me
204212
}
205213
]
206214
}
207-
```
215+
```
208216

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!
211219

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:
215223

216224
```cs
217225
var address = BitcoinAddress.Create("mzK6Jy5mer3ABBxfHdcxXEChsn3mkv8qJv");
218226
transaction.Inputs[0].ScriptSig = address.ScriptPubKey;
219-
```
227+
```
220228

221229
### Sign your transaction
222230

223-
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.
224232

225233
Signing can be [complicated](https://en.bitcoin.it/w/images/en/7/70/Bitcoin_OpCheckSig_InDetail.png), but we’ll make it simple.
226234

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:
228236

229237
```cs
230238
transaction.Inputs[0].ScriptSig = bitcoinPrivateKey.ScriptPubKey;
231-
```
239+
```
232240

233-
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:
234242

235243
```cs
236244
transaction.Sign(bitcoinPrivateKey, false);
237-
```
245+
```
238246

239247
### 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.
250+
251+
#### With QBitNinja:
252+
242253
```cs
243254
BroadcastResponse broadcastResponse = client.Broadcast(transaction).Result;
244255

@@ -252,9 +263,9 @@ else
252263
Console.WriteLine("Success! You can check out the hash of the transaciton in any block explorer:");
253264
Console.WriteLine(transaction.GetHash());
254265
}
255-
```
266+
```
256267

257-
#### With your own Bitcoin Core:
268+
#### With your own Bitcoin Core:
258269

259270
```cs
260271
using (var node = Node.ConnectToLocal(network)) //Connect to the node
@@ -266,13 +277,15 @@ using (var node = Node.ConnectToLocal(network)) //Connect to the node
266277
node.SendMessage(new TxPayload(transaction));
267278
Thread.Sleep(500); //Wait a bit
268279
}
269-
```
280+
```
270281

271282
The **using** code block will take care of closing the connection to the node. That's it!
272283

273284
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+
276288
YouTube: [How to make your first transaction with NBitcoin](https://www.youtube.com/watch?v=X4ZwRWIF49w)
277289
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)
291+

0 commit comments

Comments
 (0)