Skip to content

Commit b16f00b

Browse files
eric-stacksgitbook-bot
authored andcommitted
GITBOOK-6: update Bitcoin Primer Cover
1 parent f5a41af commit b16f00b

35 files changed

+1811
-0
lines changed
372 KB
Loading
205 KB
Loading
31.3 KB
Loading
263 KB
Loading

docs/tutorials/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
description: Your end-to-end learning experience.
3+
---
4+
5+
# Welcome to Tutorials
6+
7+
<div data-with-frame="true"><figure><img src=".gitbook/assets/build-better-apps.png" alt=""><figcaption></figcaption></figure></div>
8+
9+
This tutorials section consists of end-to-end guides on building complete applications with Stacks. The goal with these tutorials is to learn by building. They will walk you through the process of building complete apps from start to finish.
10+
11+
> _**A tutorial is a lesson**, that takes a student by the hand through a learning experience. - diataxis_
12+
13+
Our tutorials section has launched with its first complete tutorial: the Bitcoin Primer, which gives a holistic introduction to both Bitcoin and Stacks. Additional tutorials are in the works to walk through how to build different things with Stacks.
14+
15+
<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th data-type="content-ref"></th><th data-hidden data-card-cover data-type="image">Cover image</th></tr></thead><tbody><tr><td>Bitcoin Primer</td><td>A high-level introduction to Bitcoin and how to expand its functionality with Stacks.</td><td><a href="/broken/pages/EQcxwjO33WM06SIolnxb">Broken link</a></td><td><a href=".gitbook/assets/bitcoinprimertutorial.png">bitcoinprimertutorial.png</a></td></tr></tbody></table>

docs/tutorials/SUMMARY.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Table of contents
2+
3+
* [Welcome to Tutorials](README.md)
4+
5+
## Bitcoin Primer
6+
7+
* [Introduction](bitcoin-primer/introduction.md)
8+
* [Why Build on Bitcoin?](bitcoin-primer/why-build-on-bitcoin/README.md)
9+
* [Bitcoin Economy vs Ethereum Economy](bitcoin-primer/why-build-on-bitcoin/bitcoin-economy-vs-ethereum-economy.md)
10+
* [Energy Usage](bitcoin-primer/why-build-on-bitcoin/energy-usage.md)
11+
* [Technology & Economic Incentives](bitcoin-primer/why-build-on-bitcoin/technology-and-economic-incentives.md)
12+
* [How to Build on Bitcoin](bitcoin-primer/why-build-on-bitcoin/how-to-build-on-bitcoin.md)
13+
* [How Bitcoin Works](bitcoin-primer/how-bitcoin-works/README.md)
14+
* [Introduction](bitcoin-primer/how-bitcoin-works/introduction.md)
15+
* [Transactions](bitcoin-primer/how-bitcoin-works/transactions.md)
16+
* [Blocks](bitcoin-primer/how-bitcoin-works/blocks.md)
17+
* [Miners](bitcoin-primer/how-bitcoin-works/miners.md)
18+
* [Addresses and Keys](bitcoin-primer/how-bitcoin-works/addresses-and-keys.md)
19+
* [Blockchain](bitcoin-primer/how-bitcoin-works/blockchain.md)
20+
* [Script](bitcoin-primer/how-bitcoin-works/script.md)
21+
* [Updates & Forks](bitcoin-primer/how-bitcoin-works/updates-and-forks.md)
22+
* [Bitcoin Development Basics](bitcoin-primer/bitcoin-development-basics/README.md)
23+
* [Bitcoin Script Fundamentals](bitcoin-primer/bitcoin-development-basics/bitcoin-script-fundamentals.md)
24+
* [Creating & Sending a Bitcoin Transaction](bitcoin-primer/bitcoin-development-basics/creating-and-sending-a-bitcoin-transaction.md)
25+
* [Getting Started with Stacks](bitcoin-primer/getting-started-with-stacks/README.md)
26+
* [Initial Setup](bitcoin-primer/getting-started-with-stacks/initial-setup.md)
27+
* [Creating Our Project](bitcoin-primer/getting-started-with-stacks/creating-our-project.md)
28+
* [The Stacks Stack](bitcoin-primer/getting-started-with-stacks/the-stacks-stack.md)
29+
* [Anatomy of a Full-Stack Stacks App](bitcoin-primer/getting-started-with-stacks/anatomy-of-a-full-stack-stacks-app.md)
30+
* [Stacks Development Fundamentals](bitcoin-primer/stacks-development-fundamentals/README.md)
31+
* [Local Stacks Dev Workflow](bitcoin-primer/stacks-development-fundamentals/local-stacks-dev-workflow.md)
32+
* [Working with Clarity](bitcoin-primer/stacks-development-fundamentals/working-with-clarity.md)
33+
* [Testing Clarity Contracts](bitcoin-primer/stacks-development-fundamentals/testing-clarity-contracts.md)
34+
* [Frontend with Stacks.js](bitcoin-primer/stacks-development-fundamentals/frontend-with-stacks.js.md)
35+
* [Deploying Stacks Apps](bitcoin-primer/stacks-development-fundamentals/deploying-stacks-apps.md)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
icon: seedling
3+
---
4+
5+
# Bitcoin Development Basics
6+
7+
In the last sections, we talked about why now is an excellent time for developers to start building on Bitcoin and how Bitcoin works. Now we're going to start applying some of that knowledge and get started programming.
8+
9+
We're going to start by looking at the fundamentals of Bitcoin scripting and how Bitcoin transactions are constructed. Along the way, you'll learn important fundamentals about how Bitcoin works, but you'll also begin to see the major drawbacks of trying to build with only Bitcoin.
10+
11+
We're going to start off in this module by utilizing the command line to get Bitcoin set up on our local machine and begin working with it.
12+
13+
The first thing we need to do is get Bitcoin set up on our machine. Start by [downloading Bitcoin Core](https://bitcoin.org/en/bitcoin-core/).
14+
15+
<figure><img src="https://lwfiles.mycourse.app/635fe723662899c6bfb19e1d-public/363e23022af117cdd932b0f3cadfe860.jpeg" alt=""><figcaption></figcaption></figure>
16+
17+
Once it's installed you'll want to run `bitcoind`, which is the CLI we use to actually run Bitcoin.
18+
19+
Note that you don't need to run and download a full node in order to follow along here. We can run Bitcoin is `regtest` mode in order to create our own localized private Bitcoin network.
20+
21+
You can do that by running `bitcoind -regtest -daemon` which will run a regtest version of Bitcoin in the background as a daemon.
22+
23+
The next thing you need to familiarize yourself with is the Bitcoin CLI.
24+
25+
The first thing we are going to do is generate some new blocks and send the block reward to a new address that we'll generate.
26+
27+
We can do that with `bitcoin-cli -regtest generatetoaddress 101 $(bitcoin-cli -regtest getnewaddress)`
28+
29+
Here were telling the CLI to enter regtest mode, then generate 101 new blocks and send the block reward to the provided address, which in this case is a script to generate a new address using the CLI.
30+
31+
An array of the generated block hashes will be returned to us.
32+
33+
<figure><img src="https://lwfiles.mycourse.app/635fe723662899c6bfb19e1d-public/8700e9c212639634619e84ad5deda276.jpeg" alt=""><figcaption></figcaption></figure>
34+
35+
Now we can see the list of UTXOs we have created:
36+
37+
`bitcoin-cli -regtest listunspent`
38+
39+
And get the balance of our newly generated address:
40+
41+
`bitcoin-cli -regtest getbalance`
42+
43+
Alright we now have Bitcoin running locally and have a basic understanding of how to interact with it. Let's pause and take a look at how Bitcoin Script works before coming back and using the CLI to create some transactions.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Bitcoin Script Fundamentals
2+
3+
In the last lesson, we briefly covered Bitcoin's scripting language, Script. Now let's dive a little deeper into that and look at how we can actually use Script to write Bitcoin applications that do useful things.
4+
5+
We're going to start with a very simple Hello World script to learn the basics, then we'll look at how we can create some more complex scripts down below as we create the MVP of our app.
6+
7+
We'll be using an [online Bitcoin IDE](https://siminchen.github.io/bitcoinIDE/build/editor.html) in order to write and visualize our script.
8+
9+
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676575449444/230898b8-c0ac-4362-9650-b38fc072a5b2.png?auto=compress,format\&format=webp)
10+
11+
This visualization is extremely helpful for understanding how Bitcoin Script actually works. We have some code here by default: `1 2 OP_ADD`.
12+
13+
Take a second to try and guess what this code will do. Then use the 'Step' button to go through the operations step by step and see what happens.
14+
15+
What we've done here is added each number to the stack, and then applied the `OP_ADD` function to them. `OP_ADD` takes two inputs and adds them together. You can see all the available OPCODES and what they do in the [OPCODE reference](https://en.bitcoin.it/wiki/Script#Opcodes).
16+
17+
Now let's try adding another number here, running through the step function and seeing what happens. It might not be what you expect.
18+
19+
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676575954167/a96fe443-e718-4db7-881d-71fec13f07e6.png?auto=compress,format\&format=webp)
20+
21+
Why did we not get a single `0x6` back instead of this `0x5`? Because remember that we go data by data and apply these OPCODE functions. The `OP_ADD` function takes the top _two_ pieces of data, adds them, and then pushes the result back on to the stack.
22+
23+
So here we are adding 2 and 3 last, so `OP_ADD` will then take those, add them, and push them. If we want to add all of these numbers together we could add another `OP_ADD` function and see what that does.
24+
25+
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676576076195/94b52849-13f6-4328-a7bc-c5ca7b332976.png?auto=compress,format\&format=webp)
26+
27+
Now I want you to try and write a script that does the following:
28+
29+
1. Adds together two integers.
30+
2. Checks to see if the value of those integers is greater than 7.
31+
32+
If it is, the execution will be successful, otherwise it won't be. Recall that a script is considered successful and valid if it returns 1 or greater. Look at the OPCODES and figure out what that means for this particular challenge.
33+
34+
You'll need to explore the OPCODE reference in order to complete this task.
35+
36+
Try to do this on your own before looking at the solution below 👇🏻
37+
38+
No cheating.
39+
40+
🔽
41+
42+
🔽
43+
44+
🔽
45+
46+
🔽
47+
48+
🔽
49+
50+
🔽
51+
52+
🔽
53+
54+
🔽
55+
56+
🔽
57+
58+
🔽
59+
60+
Ready for the solution?
61+
62+
`2 2 op_add 7 op_greaterthan`
63+
64+
Add this to the IDE and step through each step to figure out why this is the solution if you got something different.
65+
66+
We are first adding together 2 and 2, resulting in 4. Then the op\_greaterthan opcode checks to see if \`a\` is greater than \`b\`. In this case, 4 is not greater than 7, so the execution is unsuccessful. If you were to replace 2 and 2 with 4 and 5, or something else more than 7, it would work.
67+
68+
How does scripting work in the context of transactions? Remember that fundamentally, Bitcoin is built on UTXOs. The UTXO set consists of all of the unspent transaction outputs and represents the current state of the network as far as who can spend what bitcoins.
69+
70+
Scripting is how we actually create and enforce these spending conditions.
71+
72+
So if I have 1 bitcoin, that might mean that I have two UTXOs, each representing half a bitcoin. Scripting is what makes it impossible for anybody but me, with my private key, to spend that UTXO.
73+
74+
How does this work?
75+
76+
Each UTXO comes with a locking script that is a sort of cryptographic puzzle that needs to be solved before it can be used as a valid input to another transaction.
77+
78+
So if I want to use one of my UTXOs, I need to solve that puzzle. When I go to create a new transaction, I provide an unlocking script along with the UTXO that I want to use as input.
79+
80+
This unlocking script serves as the solution to the puzzle provided by the locking script.
81+
82+
Usually, these puzzles will utilize our public and private keys to verify that we are in fact the owners of these UTXOs, so that the only way to unlock the UTXO is to provide the correct combination of public and private key.
83+
84+
Depending on the block explorer, we can see the output scripts included in transactions. For example, take a look at [this transaction](https://mempool.space/tx/d084830f0b8cfadc340b0575fe4543979cd1900e8a3ade9a045b94070f848a49), scroll down and you can see the scripts that were included with these outputs:
85+
86+
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1676583583094/ea088e1f-60a3-4e9b-9166-fee0c8a6c419.png?auto=compress,format\&format=webp)
87+
88+
This is the locking script or the script that the receiver of this output must be able to unlock in order to use this output.
89+
90+
So when we go to use this output by providing it as an input to another transaction, we then give an unlocking script in order to unlock it and actually be able to do that.
91+
92+
If we click this little arrow next to the input, we can go to the previous transaction containing the output used for this input.
93+
94+
Then we can see the locking script that was provided for this new input. The unlocking script provides the signature of the private key and the public key that are required to fulfill the requirements of the locking script.
95+
96+
Upon creating a new transaction, these two values are combined with the locking script via the stack and the result needs to be valid for this transaction to be considered valid.
97+
98+
Walkthrough of a P2PKH Script
99+
100+
In the above transaction example, you can see that the first output has a P2PKH script.
101+
102+
P2PKH stands for pay to public key hash, and it is the most common type of script in Bitcoin. This is the script that gets run when you make a basic transaction from one address to another.
103+
104+
That script consists of the following code:
105+
106+
`OP_DUP OP_HASH160 f1304d590d8f1a6b8cefd655c584616f8f68ea81 OP_EQUALVERIFY OP_CHECKSIG`
107+
108+
Let's walk through exactly what this script is doing when we initiate a transaction.
109+
110+
When we use our wallet to initiate a transaction, remember that what we are actually doing is creating a new input by utilizing one of our existing outputs, or UTXOs more specifically, as that new input. When we do that, we provide the transaction ID of the output we want to use.
111+
112+
The wallet will then go find the output that matches that transaction ID, look at the locking script, the `scriptPubKey` field, and see if the unlocking script we are providing with our new input satisfies the conditions required.
113+
114+
We do this by combining these two scripts together. In the Input Scripts section of the screenshot, we can see the P2PKH script consists of two large hexadecimal numbers.
115+
116+
These represent hashes of the signature of the sender's public key and their public key, respectively.
117+
118+
We will place the unlocking script in front of the locking script and run the code. If it evaluates to `true` then we are allowed to spend this as a valid input to a new transaction.
119+
120+
So using the transaction above, the whole thing would look like this:
121+
122+
`47304402203939a0a60876c330fd5165f4648cd431d8b379a6ac1a9022e31afa10ef30d827022035e07560ca6068669ce17d7acaac193 54a148ac453d2feb518b236aa8fc1cc2a012102dc109689e8655dc90d09544397d8b04989cd42b8037d15ba629b12d5ab77c068 OP_DUP OP_HASH160 f1304d590d8f1a6b8cefd655c584616f8f68ea81 OP_EQUALVERIFY OP_CHECKSIG`
123+
124+
Okay there's a lot here, but try and go through this step by step and figure out what exactly it is doing by using what we've already learned about stack-based languages and Bitcoin OPCODES.
125+
126+
You can refer to Learn Me a Bitcoin if you get stuck. But writing down how this script works step-by-step will be an excellent exercise to help you understand how scripting in Bitcoin works.
127+
128+
Then, go through all of the standard scripts on Learn Me a Bitcoin and familiarize yourself with how they work.

0 commit comments

Comments
 (0)