How to Make a Bitcoin Wallet in 20 Minutes

Bitcoin exchange
January 4th, 2020

Making a Bitcoin wallet is incredibly easy.

In fact, it is probably the easiest wallet to make in the world.

It would be a challenge to make a wallet for the traditional banking system in under a year, but making a Bitcoin wallet takes just 20 minutes.

Today I’m going to teach you how.

Before building a wallet, it’s important to know the basics of wallet theory and the bitcoin transaction model.

If this is too basic for you, feel free to skip the next two sections and get right to the code.

Wallet Theory

What is a wallet exactly?

At its core, a wallet (bitcoin or otherwise) provides three main functions:

  1. The ability to view your balance
  2. The ability to receive money
  3. The ability to send money

Depositing is the same as receiving money. Withdrawing is the same as sending money. They are just special cases where you are the sender of the receive action and where you are the receiver of the send action.

Some wallets may complicate this a bit but they are extensions of the core.

In Bitcoin country, wallets really are as simple as this.

Building a wallet will require us to remember these three functions and design around them.

The Bitcoin Transaction Model

The next topic for us to understand is the bitcoin transaction model.

Understanding this will give us the foundation for implementing the three core features of our wallet: view balance, receive and send.

The way that balances are secured on bitcoin is very simple.

Don’t worry if you don’t get it right away, it can be a bit confusing, but rest assured it is simple and it will become second nature to you as time goes on.

In Bitcoin, money resides in virtual locations.

These locations are identified by numbers known as“addresses.”

And they are unlocked by other numbers that act as virtual keys.

These virtual numerical keys are known as“private keys” because you are must keep them private or otherwise anyone can take your money.

Every address has a corresponding private key. They are generated at the same time and have an important mathematical relationship, which we’ll discuss later.

You send money out of your address by using your private key while at the same time making sure not to reveal your key to anyone else.

You do this by performing an action that only your address’s corresponding key could possibly perform: you use the key to create a digital signature around the description of the transaction.

Then you broadcast the transaction to the bitcoin network.

The bitcoin network(AKA the collection of computers that are running the bitcoin software) checks the transaction to make sure(a) it is trying to spend funds that are available(b) it has a signature that corresponds to the hypothetical key that is tied to the address where the funds reside.

How does the network know that they signature corresponds to the address?

This is perhaps the most complicated part to understand, but we will try to get through it as simply as possible.

When Bitcoin address is created, it is created alongside its corresponding private key. The two numbers are linked mathematically.

One number, the bitcoin address, can be easily derived from the other number, but the reverse computation is not feasible.

This is called a one-way function, because you can only go from a to b but not from b to a.

A good analogy would be that it is easy to check that two types of ground meat are the same, and it is easy to grind beef or turkey into ground meat, but it is essentially impossible to turn ground meat into its unground counterpart.

Going from a private key to an address is grinding meat - doable. Generating a signature from a private key is grinding meat - doable. Checking a signature against an address is comparing two types of ground meat - doable. Going from an address to a private key is ungrinding meat impossible.

So there we have it.

The way to know that a transaction was signed by the correct hypothetical private key - the one that corresponds to the address where the money resides - is to check the signature against the address.

You don’t need to actually know the private key to know that the signature is correct. You just need to verify the signature mathematically against the address.

Creating Keys and Addresses

Now we’re ready to build the foundation of our wallet.

This is the code for wallet generation, or the creation of the private keys that allow you to spend your funds and the addresses that allow you to check your balance and receive funds.

As we mentioned before, the address and the private key are mathematically linked.

The process for creating a Bitcoin address looks like this:

  1. Create a large random number that is mathematically infeasible to guess - this is the private key
  2. Use elliptic curve cryptography to calculate a public equivalent of this number - this is the public key(a really large number can be easily raised to a certain exponential power but the resulting number cannot be easily raised to the inverse power)
  3. Take the public key and calculate a cryptographic hash of the public key - this is the public key hash
  4. Build an“unencoded Bitcoin address” by using the Bitcoin scripting language to specify a location where funds can be spent under the following condition, expressed in natural language:“funds can be spent from this address when a valid transaction is revealed with a signature produced from a private key that corresponds to a public key that when hashed equals a particular public key hash”
  5. Take the unencoded bitcoin address and encode it into base58 to produce the final Bitcoin address

Ok so we have our basic steps. Let’s put them into action.

We’re going to be using JavaScript to make this wallet so that it can run in the browser, it can run on the desktop(via desktop app wrappers like Electron), and it can run on mobile(via mobile app wrappers).

The best Bitcoin library for JavaScript is BitcoinJS Lib.

First, we install the library:

npm install bitcoinjs-lib

Next, we import the library:

import * as bitcoin from bitcoinjs-lib

Then, we create a key pair:

const keyPair = bitcoin.ECPair.makeRandom()

const { address } = bitcoin.payments.p2pkh({ pubkey: publicKey })

As far as the core code goes, that’s it.

We can now take this code and incorporate it into a page that lets you generate an account and shows you the private key and bitcoin address.

Implementing “View Balance”

Now that we have implemented wallet creation, we can move on to implementing the first of our core wallet features: the“view balance” feature.

We do this simply by issuing an HTTP request to a Bitcoin blockchain explorer. This is a website that runs a special version of bitcoin node software that keeps track of the balances for all addresses. This in turn means that you can ask it for the balance of any address and it’ll give you the answer.

There are many options out there but the first I would check our is blockcypher.

We can implement the address check as follows:

  .then(response => response.json())
  .then(data => console.log(data))