_posts/2021-01-22-nakamoto.md 8.2 KiB raw
1
---
2
title: "Nakamoto: a new privacy-preserving bitcoin light-client"
3
headline: "Nakamoto: a new privacy-preserving bitcoin light-client"
4
teaser: "bitcoin light-client"
5
date: "2021.01.22"
6
type: note
7
slug: nakamoto
8
crate: nakamoto
9
layout: default
10
---
11
12
With Bitcoin Core [0.21][bitcoin] out the door offering full support for *Client
13
Side Filtering*,[^1] it seems like a good time to talk about the project I've
14
been spending my weekends on for the last six months.
15
16
*Nakamoto* is a Bitcoin light-client[^2] implementation in [Rust][rust], with
17
a focus on low resource utilization, modularity and privacy.
18
19
The vision for the project is to build a set of high quality and easily
20
embeddable libraries targeting light client wallet functionality, that can
21
run on any platform -- be it mobile or desktop -- while preserving user privacy.
22
23
When we take a look at the greater ecosystem, we see that it isn't uncommon for
24
users to end up trusting third parties when transacting. In the majority of
25
cases - Ethereum being a good example - it is due to poor light-client support.
26
This poses a considerable security and privacy risk that is counter to the
27
nature and *raison d'Γͺtre* of blockchains, which are designed as peer-to-peer
28
systems.  Light-clients are necessary for the average user to securely
29
interface with the network in a peer-to-peer manner, and hence the availability
30
of high quality implementations is paramount.
31
32
### Requirements
33
34
One of the key long-term plans for Nakamoto is to offer a C FFI, enabling
35
interoperation with other languages and opening up the possibility to run on
36
mobile devices. This could have been satisfied by writing Nakamoto in C, though
37
one of the project's primary goals is to offer the most secure implementation
38
possible.  Rust was chosen over C.
39
40
Another important consideration for mobile is *power* and *resource
41
efficiency*. This rules out implementations in managed languages with heavy
42
runtimes such as *bitcoinJ* and nudges us towards simpler designs.
43
44
Finally, user privacy is becoming increasingly important. Privacy influences
45
the choice of client protocols and forces us to think about how the client
46
interacts with the nodes on the network, and what information it reveals.
47
48
### Landscape
49
50
There are very few options out there that are satisfactory. It's fairly common
51
for mobile Bitcoin wallets to have a custom light-client implementation, most
52
of which are still based on [BIP 37][bip37].  Another common avenue is to use
53
something like [Electrum][electrum], which relies on non-standard Bitcoin
54
nodes. [Unfortunately neither of those are privacy-preserving][privacy].
55
56
Nakamoto talks directly to the Bitcoin P2P network, and thus doesn't require
57
special servers to connect to. For simple payment verification (SPV), the new
58
BIP 157 client-side block filtering protocol offers improved privacy.
59
60
Additionally, Nakamoto's client architecture enables it to run on a single
61
thread, minimizing CPU and memory footprint. This allows the library to
62
be easily embedded in any environment.
63
64
### Modularity
65
66
A lot of work has been put into building an I/O-free [protocol core][protocol].
67
This is the key to Nakamoto's modularity and also enables some very powerful
68
testing strategies that only work when the code being tested is
69
[deterministic][], i.e. all inputs can be controlled for.
70
71
Having a clean separation between the networking code and protocol
72
state-machine [reduces the possible failure states][sans-io], and
73
allows the networking code to be swapped based on the user's needs. If
74
performance is more important than efficiency or code complexity,
75
the included *poll*-based network [reactor][], which uses [popol](/popol) under
76
the hood can be swapped for one based on a thread-pool or a
77
thread-per-connection model.
78
79
![](/assets/nakamoto/nakamoto-architecture.svg "Architecture Diagram")
80
81
### On Dependencies
82
83
When developing software that handles money, the security of the software is
84
critical. Targetted attacks on dependencies are perhaps one of the most obvious
85
attack vectors. One of the most important steps to prevent that is writing
86
software that is easy to audit. Nakamoto follows two simple principles:
87
88
1. Less code equals less bugs and less to audit
89
2. Less dependencies equals less moving parts and potential security risks
90
91
The second point is particularly important because dependencies are
92
much harder to track than code that is internal to the library. Third-party
93
code often ends up changing hands, and though one may trust the original
94
maintainer, one may not trust [whoever comes next][attacks].
95
96
These have been guiding principles from the start, and thus I've been very
97
economical with Nakamoto's dependency budget. Compared to other projects with a
98
similar scope, Nakamoto does away with a fraction of the (transitive)
99
dependencies.
100
101
### Closing thoughts
102
103
The project is still in its early days, though the core functionality has been
104
implemented and the client is able to securely synchronize the blockchain and
105
use compact block filters for [wallet functionality][wallet] and payment
106
verification.
107
108
There is a lot of work left to do before Nakamoto is ready to power the next
109
generation of client software on the Bitcoin and [Lightning][ln] network, so
110
if you're interested in peer-to-peer protocols, Rust or Bitcoin, contributions
111
are very welcome. If you are developing wallet software and are interested
112
in using Nakamoto, [get in touch][email], I'd love to help.
113
114
```rust
115
//! A trivial example of a client that connects to the Bitcoin
116
//! testnet and syncs the chain.
117
use std::{net, thread};
118
119
use nakamoto::client::{Client, Config, Network};
120
use nakamoto::client::error::Error;
121
use nakamoto::client::handle::Handle as _;
122
123
/// The network reactor we're going to use. This one ships with Nakamoto,
124
/// as part of the `nakamoto-net-poll` crate. It's a single-threaded
125
/// reactor using non-blocking sockets and `poll(2)`.
126
type Reactor = nakamoto::net::poll::Reactor<net::TcpStream>;
127
128
/// Run the light-client.
129
fn main() -> Result<(), Error> {
130
    let cfg = Config {
131
        network: Network::Testnet,
132
        ..Config::default()
133
    };
134
    // Create a client using the above network reactor.
135
    let client = Client::<Reactor>::new(cfg)?;
136
    let handle = client.handle();
137
138
    // Run the client on a different thread, to not block the main thread.
139
    thread::spawn(|| client.run().unwrap());
140
141
    // Wait for the client to be in sync with the blockchain.
142
    handle.wait_for_ready()?;
143
144
    // The client is in sync. Ask it to shutdown.
145
    handle.shutdown()?;
146
147
    Ok(())
148
}
149
```
150
151
---
152
153
*Nakamoto* is available as a πŸ“¦ [crate][crate].
154
155
* πŸ“œ Browse the [source code][code].
156
* πŸ“– Read the [documentation][docs].
157
* πŸ‘€ Check out the [example][wallet] wallet implementation.
158
159
---
160
161
If Nakamoto's vision is appealing, please consider πŸ’š [funding](/donate)
162
the project or contributing to the [code][].
163
164
[crate]: https://crates.io/crates/nakamoto
165
[docs]: https://docs.rs/nakamoto
166
[code]: https://github.com/cloudhead/nakamoto
167
[rust]: https://www.rust-lang.org/
168
[bitcoin]: https://bitcoin.org/en/release/v0.21.0
169
[protocol]: https://github.com/cloudhead/nakamoto/blob/01eaa27c8ef88e16535cf2adb89f47886ca0a9f6/p2p/src/protocol.rs
170
[bip37]: https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki
171
[reactor]: https://github.com/cloudhead/nakamoto/blob/01eaa27c8ef88e16535cf2adb89f47886ca0a9f6/net/poll/src/reactor.rs
172
[attacks]: https://blog.npmjs.org/post/180565383195/details-about-the-event-stream-incident
173
[sans-io]: https://sans-io.readthedocs.io/how-to-sans-io.html
174
[deterministic]: https://medium.com/@tylerneely/reliable-systems-series-model-based-property-testing-e89a433b360
175
[ln]: https://en.wikipedia.org/wiki/Lightning_Network
176
[privacy]: https://en.bitcoin.it/wiki/BIP37_privacy_problems
177
[electrum]: https://electrum.org/
178
[wallet]: https://github.com/cloudhead/nakamoto/blob/cdf0737e3cda2ae29b0862962a649f9a6d02ebeb/wallet/src/lib.rs
179
[email]: mailto:nakamoto@cloudhead.io
180
181
[^1]: *Client Side Block Filtering*, or BIP 157 is the new light-client protocol:
182
    <https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki>
183
184
[^2]: A light-client, or SPV node is a client able to operate securely
185
    without access to the full blockchain. Typically, light-clients use
186
    only block headers to verify the chain, and thus offer a different
187
    security guarantee to full nodes.