What is a Bitcoin Wallet?

Demystifying the Bitcoin protocol


There's been lot of buzz around Bitcoin in the last months. The bull market has come and everyone and their dog were getting rich overnight. Here's an unpopular opinion - bull markets suck! They suck for the same reason that fast food sucks - it's not healthy and you never get to see what's going on in the kitchen.

Most people that get into Bitcoin just to try and get their free ticket to the moon will likely never leave an exchange with their money. They never get to experience the blockchain, with all its quirks and its promise for being digital cash, impossible to be controlled by anyone but yourself and transferable directly with no middle men.

Does that sound appealing to you? Have you got no technical knowledge about how Bitcoin works and how to control it? Great! Buckle up, as we have some explaining to do...

A little note before we begin, I will be talking about how Bitcoin works, but not how to use it. I'm sure there are plenty articles covering that topic.

How do I create a Bitcoin account?

There are no Bitcoin 'accounts', so to speak. What I mean by that is there's no website on which you have to create an account, much like you would register an email on Google. Well, maybe there are, but those are scams.

What Bitcoin has are 'wallets'. This name usually refers to a piece of software which you can use to manage you Bitcoins. It can also refer to your personal Bitcoin 'bank account', on which you can store your Bitcoins - and this second meaning is the one which I'm going to elaborate on.

Now that we know how it's called, how is it any different from your Internet bank account? If there's no website to sign up on, how is it done?

Here's where you learn the first beautiful thing about Bitcoin. Your wallet is really just one very large random number. A number so unimaginably large, that there's almost zero chance that anyone can guess it without asking you what it is. And the only way to spend your funds is by knowing that number, so as long as it is secure, you're the only person in the universe that will have access to them. So all you have to do to sign up for Bitcoin is generate that number in a way that makes it as close to real randomness as possible. I'm not kidding - some people used to generate a wallet by rolling a cup of die a couple of times! Although that is a bit custom, so most people do what their wallet software considers secure, like wiggling your mouse for half a minute.

You might be thinking, if you can't tell anyone that number without giving them control over your Bitcoin, how can you let others send money to you? This is done using asymmetric cryptography, a topic which can't really be described in a short blog post. Just to give you an idea: that very big number that we just talked about is transformed into something called private key, and then it is used to obtain its counterpart, the public key. The math used to obtain the public key makes the reverse operation impossible to do at the current state of technology. The public key is then transformed to a Bitcoin address, which people can use to send you coins.

random number <-> private key -> public key -> address

As you can see above, most of those operations are irreversible. Here is an example of every part of this chain:

The random number is 43795029299391545074974536834344553068091376386760708483685936657207583444838
Private key is       KzTvd4uwnyRsFyoyTUWicxvP1HVQhTfsNLGVeyZSD6eajpTYvaRk
Public key is        027a3d7b85c12d70a490b7211992c2489b643bd145e40ea1df85dd7830be85f91a
Bitcoin address is   13aD1pb5BnFjLyKZ5qdjeWf7fM92BgQad7

You can see the code used to print this on https://gist.github.com/bbrtj/df80c13b54df9b720dac1ed9883ac2a7.

Who controls Bitcoin?

Technically no one controls the Bitcoin network. Certain groups of people like software developers, miners or coin holders may have smaller or greater influence on it. The creator of Bitcoin is long gone, so his project has been living on its own for the last decade - and that's a good thing! The whole point of the blockchain technology was to give the control over money back to the people.

Bitcoin puts you in charge - your money can't be taken away from you, not even by the government. No one can stop you from sending your money to anywhere across the globe, the best they can hope for is to make it harder for you to connect to the network, but since it is decentralized it's not a very realistic goal to achieve. This is incredible power humanity never experienced in its history. It has two weak spots though. The first one is the unforgiving nature of cryptocurrencies, a possibility that you will do something wrong and your key, together with all your coins, will get hijacked by someone else - and sadly this is often the case, just because of the deep knowledge one needs in order to do everything right the first time. The second weakness is the infamous five dollar wrench attack, very effective because of the immutability of the network, and the best way to protect yourself from it is not to brag about how much Bitcoin you actually own.

How can I spend my coins?

It is possible to prove ownership of a given private key without disclosing it, through a cryptographic signature. It can be only generated by a private key and can be verified by a public key.

Normally, when you want someone to send you coins, you just send them your Bitcoin address. When they later send something, that transaction is recorded in the blockchain and it is possible to determine that your address contains funds. To spend them, all you need to do is prove ownership of that address, through a valid signature.

Lets see how this works. I will prove ownership of an address by signing the message 'Bartosz owns this Bitcoin address! 13aD1pb5BnFjLyKZ5qdjeWf7fM92BgQad7'. We can then send it to someone together with the signature and the public key. Since Bitcoin addresses are generated from the public keys, they will be able to see if the public key corresponds with the address I claim. The basic idea is that when ...

  • the public key can be used to obtain the right address
  • the signature can be verified against the given public key and message
  • and the address contains coins

... you are the owner of the coins, as it's deemed currently impossible to generate a valid signature without being the owner of the private key. Hence the Bitcoin saying: Not your keys, not your Bitcoin. That's why it is critically important not to keep your coins on an exchange. In case it still isn't clear, here's an one hour version: https://youtu.be/dnC5mFaIW3Q.

We can use this knowledge to create a program that will sign the message and immediately verify it using the same key (which should always be successful). Here's a possible output of such a program:

Message is      Bartosz owns this Bitcoin address! 13aD1pb5BnFjLyKZ5qdjeWf7fM92BgQad7
Signature is    3044022006b04b8c900f0c60a5008700d0e57aeab2339b1cecd15c37c5e022593d004a8402201bc0d43ef8f5858055188dc9ba0d02e3e3d3191d7471413b41f5fe81d7dc041a
Verification is successful

You can see the code used to print this on https://gist.github.com/bbrtj/ec47c3c0041174726101620f546104aa.

Note that if the signature message is hijacked, malicious actors can try to convince someone they are the owner if it doesn't contain any information about your identity! Bitcoin itself doesn't have this problem, as the transaction digests you are signing contain the output addresses.

I'm interested in playing around with it, but I don't trust it with my money!

If the above sentence is true for you, don't worry! Bitcoin has a second network where you can test stuff, shockingly named Testnet. Testnet coins are not unlimited, as the network is still doing all the stuff that Bitcoin does, however there are services which will happily hand over some Testnet coins to you, hoping you'll give them back when you're done. This one's an example: https://testnet-faucet.com/.

So if you're really interested, all you need is an active, unlimited Internet connection and about 5 gigabytes of storage space. I would recommend downloading the Bitcoin daemon (bitcoind) and running it with -prune=1000 and -testnet options. It will require some time to synchronize, but when it finishes you'll have a full node ready to do anything you want with it.


Comments? Suggestions? Send to feedback@bbrtj.eu
Published on 2021-06-12