Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Move tutorials from Blog to Tutorial section #103

Merged
merged 4 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ const tutorialSidebar = [
collapsable: false,
children: [
'/tutorials/hello-world',
'/tutorials/Bitcoin_Core_RPC_Demo',
'/tutorials/compact_filters_demo',
'/tutorials/descriptors_in_the_wild',
rajarshimaitra marked this conversation as resolved.
Show resolved Hide resolved
'/tutorials/hidden-power-of-bitcoin',
'/tutorials/descriptor_based_paper_wallet',
'/tutorials/spending_policy_demo'
],
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Using BDK to build a wallet backed by a Bitcoin Core full node"
title: "BDK wallet with Bitcoin core RPC "
description: "Tutorial showing usage of Bitcoin core backend with BDK wallet"
authors:
- Rajarshi Maitra
Expand All @@ -9,7 +9,7 @@ hidden: true
draft: false
---

### Introduction
## Introduction
BDK wallet developer library can be used to easily deploy wallets with various kinds of blockchain backend support, like [`electrum`](https://github.com/romanz/electrs), [`esplora`](https://github.com/Blockstream/esplora), `compact-filters` ([BIP157](https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki)) etc. With the latest release of BDK [`v0.10.0`](https://github.com/bitcoindevkit/bdk/releases/tag/v0.10.0), BDK now supports Bitcoin Core as a blockchain backend. BDK talks with Bitcoin Core using rust-bitcoin's [bitcoincore-rpc](https://github.com/rust-bitcoin/rust-bitcoincore-rpc) library.

This allows wallet devs to quickly deploy their wallet that can talk to a bitcoin full node (home raspi nodes) out of the box. Wallet devs don't need to worry about connecting to a full node with correct RPC calls, all of that is handled by BDK under the hood. All they need is to identify the full node's RPC IP address and the correct RPC credentials.
Expand All @@ -18,7 +18,7 @@ In this tutorial we will see how to write a very simplistic wallet code that can

Unlike other tutorials, we will not use `bdk-cli` tools, but instead write rust code directly using `BDK` devkit. In the end we will end up with our own simple bitcoin wallet.

### Prerequisite
## Prerequisite
To run with this tutorial you would need to have a bitcoin core node running in regtest mode. Get the bitcoin core binary either from the [bitcoin core repo](https://bitcoincore.org/bin/bitcoin-core-0.21.1/) or [build from source](https://github.com/bitcoin/bitcoin/blob/v0.21.1/doc/build-unix.md).

Then configure the node with a following `bitcoin.conf` file
Expand All @@ -33,7 +33,7 @@ rpcpassword=password

Apart from that, you would need to install rust in your system. Grab the installation one-liner from [here](https://www.rust-lang.org/tools/install).

### Setting Up
## Setting Up
Create a new cargo binary repository.
```shell
mkdir ~/tutorial
Expand Down Expand Up @@ -80,7 +80,7 @@ edition = "2018"
[dependencies]
```

### Setting dependencies
## Setting dependencies
Once the rust binary is compiled and running, we now need to specify the dependencies we need to work on our library.

Remember that BDK provides almost everything we would need to build a wallet out of the box. So we don't need any more dependencies apart from BDK. We will use another small rust crate called [`dirs_next`](https://crates.io/crates/dirs-next) to find our home directory and store wallet files in a subfolder there.
Expand Down Expand Up @@ -131,7 +131,7 @@ use std::str::FromStr;
```
With this we are now ready to add our wallet code.

### Getting Descriptors
## Getting Descriptors

BDK is a descriptor based wallet library. That means when we specify our wallet key-chain we need to tell BDK about it in the format of a descriptor. You can read up on descriptors more [here](https://bitcoindevkit.org/descriptors/). A descriptor string looks like this
`"wpkh([b8b575c2/84'/1'/0'/0]tprv8icWtRzy9CWgFxpGMLSdAeE4wWyz39XGc6SwykeTo13tYm14JkVVQAf7jz8WDDarCgNJrG3aEPJEqchDWeJdiaWpS3FwbLB9SzsN57V7qxB/*)"`.
Expand Down Expand Up @@ -209,7 +209,7 @@ chng: "wpkh([89df6a67/84'/1'/0'/1]tprv8iSRXyLtTKJNCECQxBJ19cgx2ueS7mC7GNq7VqTWY3
```
Voila! Now we have nice descriptors strings handy to use for our BDK wallet construction.

### Talking to Bitcoin Core Programmatically
## Talking to Bitcoin Core Programmatically
Like all other tutorials we will use two wallets to send coins back and forth. A Bitcoin Core wallet accessible via `bitcoin-cli` command line tools, and a BDK wallet maintained by BDK library.

But unlike other tutorials, we won't be using `bitcoin-cli` to talk to the Core wallet (we can, but let's spice things up). Instead, we will use the `bitcoin-rpc` library, to talk with our core node listening at `127.0.0.1:18443`, from inside our main function. This will allow us to write code, that will handle both the core and BDK wallet, from inside of the same function, and we won't have to switch terminals!
Expand Down Expand Up @@ -257,7 +257,7 @@ GetBlockchainInfoResult {
```
Thats it. Now we can programmatically talk to our core node.

### Get some balance in core wallet.
## Get some balance in core wallet.
We have told our rpc client that we would use a wallet named `test`. But currently, our core node doesn't have such a wallet. So let's create the wallet and fund it with some test coins.
```rust
fn main() {
Expand Down Expand Up @@ -288,7 +288,7 @@ core balance: Amount(50.00000000 BTC)
```
Great! We now have 50 regtest BTC to play with.

### Setup the BDK wallet
## Setup the BDK wallet
Now that we are done setting up the core wallet. The last remaining step is to setup the BDK wallet. For this we will use the previous descriptor generation function and write code as below.

**Note**: You might want to comment out the previous code in `main()`, as running them again will create more coins in core, which isn't an issue, but might be confusing.
Expand Down Expand Up @@ -366,7 +366,7 @@ cargo run
bdk address: bcrt1q9vkmujggvzs0rd4z6069v3v0jucje7ua7ap308
```

### Sending Sats Around
## Sending Sats Around

Now that we have covered all the groundwork, we have all we need to send coins back and forth between core and BDK wallet.

Expand Down Expand Up @@ -637,7 +637,7 @@ fn get_descriptors() -> (String, String) {
}
```

### Conclusion
## Conclusion
In this tutorial we saw some very basic BDK wallet functionality with a bitcoin core backend as the source and sync of blockchain data. This is just tip of the iceberg of BDK capabilities. BDK allows flexibility in all the dimensions of a bitcoin wallet, that is key chain, blockchain backend and database management. With all that power, we just implemented a trustless, non-custodial, private bitcoin wallet, backed by a bitcoin full node, with less than 200 lines of code (including lots of comments).

BDK thus allows wallet devs, to only focus on stuff that they care about, writing wallet logic. All the backend stuff like blockchain, key management, and databases are abstracted away under the hood.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
---
title: "Using BDK to create BIP157 SPV wallet (aka Neutrino)"
title: "BDK wallet as a BIP157 SPV light client"
description: "Tutorial showing usage of compact filters (BIP157) using bdk-cli command line tools"
authors:
- Rajarshi Maitra
date: "2021-06-20"
tags: ["tutorial", "BDK", "bdk-cli", "compact_filters", "BIP157", "Neutrino"]
permalink: "/blog/2021/06/using-bdk-to-create-bip157-spv-wallet-aka-neutrino/"
---

## Introduction

#### Compact Filters:
### Compact Filters:
Compact filters are the latest specification of Bitcoin SPV node implementation as per [BIP157](https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki) and [BIP158](https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki). Such light clients were envisioned by Satoshi himself in his original white paper, but due to lack of robust privacy and trust guarantees using conventional [bloomfilters](https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki), these type of nodes never got popular.

Enters [BIP157](https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki), which described a new type of filters for Bitcoin Blockchain data, known as `compact_filters`. The [Neutrino](https://github.com/lightninglabs/neutrino) project pioneered the use of compact filter based light client nodes for using with Lightning Network wallets. Using compact filters, a light-node can talk to one or more full nodes, and fetch relevant information from the blockchain, with much more robust privacy and security guarantees than previously possible. Compact filter based nodes are best suitable to be used with mobile wallets, to create more trustless mobile applications on Bitcoin. Any wallet application that needs to have an "eye on the blockchain" has an use for such light clients.
Expand All @@ -21,7 +20,7 @@ Example of such `compact_filters` wallets in wild is [Breeze](https://github.com

Bitcoin core supports serving `BIP157` type filters from `v0.21.0`.

#### BDK and Compact filters
### BDK and Compact filters
BDK is a bitcoin wallet development library that can be used to create bitcoin wallets with custom `Database` and `Blockchain` backends. BDK is a [descriptor](https://bitcoindevkit.org/descriptors/) based wallet, i.e. the wallet keychain is described by a set of descriptors.

Using BDK one can instantiate wallets of various kinds as per requirement. BDK abstracts away all the heavy lifting works, and allow wallet devs to concentrate on logic that they care about, i.e. writing wallet codes. For more detailed documentation on BDK capabilities check these [blog](https://bitcoindevkit.org/blog/2020/12/hello-world/), [bog](https://bitcoindevkit.org/blog/2020/11/descriptors-in-the-wild/) and [docs](https://docs.rs/bdk/).
Expand All @@ -37,7 +36,7 @@ BDK also supports [BIP158](https://github.com/bitcoin/bips/blob/master/bip-0158.

This capability can be unlocked by compiling BDK with the `compact_filters` feature. Once enabled, BDK will be able to create wallets with the `compact_filters` type `Blockchain` backend. (The default backend is electrum server)

#### bdk-cli
### bdk-cli
`bdk-cli` is a lightweight [REPL](https://codewith.mu/en/tutorials/1.0/repl) wrapper over the BDK library to facilitate quick and easy demonstration of BDK capabilities in command-line. Wallet devs can use this tool to quickly try out different possibilities with BDK.

In this tutorial, We will use `bdk-cli` to demonstrate some basic wallet functionalities using `compact_filters` backend.
Expand Down Expand Up @@ -65,7 +64,7 @@ Following things are required to start with the tutorial.

If you already have these two setup and working, you can skip this and jump to the [Tutorial](#tutorial) section.

#### Install and run `bitcoind`
### Install and run `bitcoind`
You can definitely do it with your own `bitcoind` installation. `BIP157` support has been included in Bitcoin Core `v0.21.0`. So anything above that will work.

You also need to ensure proper configuration settings for signalling `compact_filters` support.
Expand Down Expand Up @@ -103,7 +102,7 @@ For ease of testing, the BDK project hosts docker images that can be used to spa
```
In the output, the `version` should show `210000`. `localservicesnames` should contain `"COMPACT_FILTERS"`. If you see this, then Bitcoin Core is correctly configured.

#### Install and run bdk-cli
### Install and run bdk-cli
- Install `bdk-cli` with `compact_filters` feature

```shell
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
title: "Descriptor-based paper wallets"
title: "Making Descriptor-based paper wallets"
description: "Demonstrate how to create descriptor-based paper wallet and how to spend them with bdk"
authors:
- Riccardo Casatta
- Steve Myers
date: "2021-03-30"
tags: ["guide", "descriptor", "paper wallets"]
permalink: "/blog/2021/03/descriptor-based-paper-wallets/"
---

## Introduction

In this post, we will use the [Rusty Paper Wallet] tool to create a multi-owned descriptor-based paper wallet. We will use [bdk] via the [bdk-cli] tool to test our descriptor and to be able to sweep the funds from our paper wallet to a new address.

## About paper wallets
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
title: "Descriptors in the wild"
title: "A Multisig between BDK and Core"
description: "Guide to setup a 2-of-2 multisig using Bitcoin Core and BDK"
authors:
- Gabriele Domenichini
date: "2020-11-18"
tags: ["guide", "descriptor"]
permalink: "/blog/2020/11/descriptors-in-the-wild/"
---

## Introduction

I have tried to setup a 2 of 2 multi signature infrastructure with two
different wallets, which know nothing about each other, but are compliant with
two very important protocols: [Output Descriptors] and [Partially Signed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Miniscript Policy & Descriptors - Hidden Powers of Bitcoin"
title: "Hidden Powers of Miniscript Policy & Descriptors"
description: "Introduction to Descriptor and Miniscript, making a Multisig Wallet and Testing Miniscript Policies"
authors:
- Sandipan Dey
Expand All @@ -10,12 +10,14 @@ hidden: true
draft: false
---

## Introduction

To send people BTC - we simply scan a QR Code *(or paste an address)*, enter some amount and *whoosh* - sent!
Users might think, just like traditional currency, we can only exchange money using Bitcoin.
As it so happens, the underlying technology Bitcoin supports specify outputs not as addresses, but as programming scripts.
This opens us to a world of possibilities using Bitcoin.

### Script
## Script

Bitcoin supports [Script](https://en.bitcoin.it/wiki/Script), a **stack-based** lightweight programming language.
Any script written in **Script** *(pun intended)* contains `OP_*` codes and raw byte arrays that Bitcoin Full Nodes understand and process.
Expand All @@ -35,14 +37,14 @@ scriptPubKey: OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
scriptSig: <sig> <pubKey>
```

##### Examples of things achievable using Bitcoin Script:
#### Examples of things achievable using Bitcoin Script:

1. `Pay Someone (p2pkh/p2wpkh)` - A specific public key must sign to spend the coins.
2. `Escrow (2-of-3-multisig)` - Two parties need to sign together to spend the coins.
3. `Vault (locked)` - A specific key will not be able to spend the coins until a timeout but another master key will always be able to spend them.
4. `HTLC` - The receiver needs disclose a secret before a timeout, else the coins are transferred back to the payee.

##### Motivation for Policies
#### Motivation for Policies

Unfortunately, due to its low-level and unusual stack-based nature, Script is pretty hard to reason about and use.
Despite being around since Bitcoin's creation, writing and understanding Script is not trivial.
Expand All @@ -52,7 +54,7 @@ When writing a script, we would want to know that if the logic we wrote is **cor
The community wanted an easy alternative way of writing Script that would create the most optimized Script code.
This gave rise to **Miniscript**.

### Miniscript
## Miniscript

[Miniscript](http://bitcoin.sipa.be/miniscript/) tackles the above problems head-on.
It is an expressive way to create policies on Bitcoin Scripts in a structured and simple fashion.
Expand All @@ -67,7 +69,7 @@ Miniscript compiler compiles a **spending policy** down to Miniscript.
It doesn't contain any signature, it's mainly a combinator language for designing spending conditions.
You can try out the compiler online by using [this link](http://bitcoin.sipa.be/miniscript/#:~:text=Policy%20to%20Miniscript%20compiler).

##### Fragments
#### Fragments

Here are some fragments which can be combined to create powerful expressions.

Expand All @@ -81,7 +83,7 @@ Here are some fragments which can be combined to create powerful expressions.
Bitcoin Script allows us to use another alternate stack. The combinator functions use this second stack to evaluate expressions of `thresh`, `and`, `aor` and `or`.
The complete Miniscript Reference can be found [here](http://bitcoin.sipa.be/miniscript/#:~:text=Miniscript%20reference).

##### Example Policies
#### Example Policies

Here are the Miniscript Policies for the examples we looked at earlier.
Note `A`, `B`, `C` are placeholders for keys *(`xpub`/`xprv`)* involved in the tx.
Expand Down Expand Up @@ -113,7 +115,7 @@ The Miniscript Policy Compiler is written in Rust and is present in [this reposi
In this blog, we will later use the same using [bitcoindevkit/bdk](https://github.com/bitcoindevkit/bdk), a lightweight descriptor-based wallet library
with a [cli](https://github.com/bitcoindevkit/bdk-cli).

### Descriptors
## Descriptors

The Bitcoin scriptpubkey supports various schemes like P2PKH, P2SH, P2WPKH, P2TR (Segwit v1) etc.
A Descriptor is a simple "description" of what scriptpubkey to be used for a given policy.
Expand Down Expand Up @@ -166,20 +168,20 @@ Of course we cannot use `pk(multi(...))`, `pkh(multi(...))` or `wpkh(multi(...))
For example a descriptor like `wsh(multi(2, PKA, PKB, PKC))` describes a P2WSH type address created by a `2-of-3` multisig
script using `PKA`, `PKB` and `PKC` pubkeys.

### Where it all comes together...
## Where it all comes together...

In this section, we are going to make a descriptor-based wallet and derive addresses from `bitcoin-cli` and then use `bdk-cli` to confirm that the addresses generated for descriptor wallets are deterministic for a given descriptor.

We will also try to create a vault miniscript policy and push funds to the vault with a lock time of 2 months.
During this time, we will try to break our vault and see our transactions failing.

##### Tools and Armor
#### Tools and Armor

- [docker](https://docs.docker.com/engine/install/)
- [bdk-cli](https://github.com/bitcoindevkit/bdk-cli)
- [miniscriptc](https://bitcoindevkit.org/bdk-cli/compiler/#installation)

##### Setting Up
#### Setting Up

We require `bitcoind` to run in `regtest` mode. Use the following config file, or any other config
that you are familiar with.
Expand All @@ -198,7 +200,7 @@ rpcpassword=password
bitcoind
```

#### Keys and Generating Addresses
### Keys and Generating Addresses

Quick installation for `bdk-cli` and `miniscriptc`:
```bash
Expand Down Expand Up @@ -262,7 +264,7 @@ Notes:

Note that both `bdk-cli` and `bitcoin-cli` produced the exact same addresses. So now we have definitive proof that descriptors can make wallets portable. That single string will be able to make any wallet generate the same set of addresses and hence they can sync and broadcast transactions in the same manner!

#### Making a MultiSig Descriptor for Funds
### Making a MultiSig Descriptor for Funds

In the real-life, most of us hold two kinds of savings accounts - one to store huge funds saved throughout our lifetime *(probably without internet banking functionalities)*
and another for regular expenses.
Expand Down Expand Up @@ -418,7 +420,7 @@ one wallet know whats the next address to create without talking to other wallet
If all the wallet shares the correct descriptor string they will always create the exact sequence of addresses and
by passing around PSBTs they would know how to sign them, without talking to each other. This solves a major problem of multisig interoperability. And BDK makes this process as seamless as possible.

### Retention Bonus - Smart Contract with Bitcoin
## Retention Bonus - Smart Contract with Bitcoin

Let us consider that a company wants to give its employees a retention bonus for two months.
If an employee stays with that company for over 2 months, the employee would get 1 BTC as a reward.
Expand Down Expand Up @@ -563,7 +565,7 @@ So this time it worked, because we have simulated 2 months passing by generating
and Employe wallet gets updated.
Hence, we saw that we can generate some smart contracts using Bitcoin.

### Inspirations
## Inspirations

1. [Descriptors from Bitcoin Core](https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md)
1. [Miniscript](http://bitcoin.sipa.be/miniscript)
Expand Down
Loading