In this post, I'll show you how to build a client for the world's first decentralized cat name database. This will be a censorship- and spam-resistant set of cat names which anyone can write to, without an account.
First, I'll break down the requirements. For some more theory on this, see World-writable data structures by burning bitcoins.
- Singleton: There should be a single set of cat names that is shared by everyone in the world.
- World-writable: Anyone should be able to write to this set, without any sort of account.
- Censorship-resistant: It should not be possible to censor particular cat names from the set.
- Spam-resistant: The set should not be filled with junk content.
As the backbone for this project, we'll use the burn-stream node.js library. If you're having trouble with any part of this project, or if you want to understand more about what's going on under the hood, check out the burn-stream documentation on GitHub.
To get started, run
npm install burn-stream webcoin bitcore-lib.
There's a lot of data in the Bitcoin blockchain, so we need some way to identify the relevant data for our particular application. Here's a config file that we can use; let's save it to app-config.json. Of course, you can modify this if you want to make your own data structure.
In order to run burn-stream, we need a running webcoin Node. The only tricky part of this process is that we need to patch it in order to insert our own checkpoint; soon there should be a better interface for doing this.
If you want to see more logs, try setting the env var
* (see visionmedia/debug).
Now that we have a working node, it's easy to make an instance of
BurnStream. Each message in the stream will contain a version byte,
0x00, which will allow us to make future enhancements to our cat name storage protocol without breaking existing clients. The cat names will be stored as a JSON array. Here we'll make a class,
CatNames, which emits
All right, there we go! This program will print out cat names as they are read in. Designing a better UI than
console.log is left as an exercise to the reader.
Writing is a little trickier than reading, since in order to write you actually need to spend some bitcoins. Fortunately, since our cat name database runs on the Bitcoin testnet, you only need testnet bitcoins, which you can easily get for free.
In this tutorial, we'll make a writing script and save it to bin/write.js.
First, install burn-stream-writer with npm and set it up using the instructions in its documentation. Save the client config to
Here's our writer script. It has a dry run feature which allows us to check the message before writing it to the blockchain.
To invoke this we can run, for example,
node bin/write.js -- Tiger Snowball.
When you run this, your message should be written to the cat database so that everyone can see it.
The database, as currently designed, is vulnerable to spam. If spam becomes a problem, we could add a delete operation to our set.
We are also not considering the weight associated with each cat name. We could turn our set into a weighted set, where each name is weighted with the amount of bitcoins burned for it. This would help us to build a ranking of which cat names are more popular.
A cat database, while compelling, somehow feels like a frivolous use of this technology. My hope is that this tutorial gives you some ideas for your own decentralized data structures. Please feel free to email or tweet at me if you have any questions, or if you'd like to share what you've made.