Note: I will default to talking about bitcoin to avoid awkward generalization, but all blockchain-based currencies will have similar concepts.
The term “wallet” usually refers to one of 2 things.
- The collection of key pairs that allow you to spend bitcoins you have received and to determine your current balance.
- The software program (app) that generates and manages those key pairs.
The wallet enables your interaction with the blockchain, so first here’s some blockchain background:
- You can think of a blockchain as a secure ledger (in this context “secure” means difficult to change in a fraudulent manner).
- The entries in the ledger are bitcoin transactions. Each transaction has one or more inputs and one or more outputs.
- Transaction outputs are identified by addresses which are just numbers determined in a particular way.
- Each transaction input contains some number of bitcoins. The outputs can contain bitcoins or be empty.
- The rules of bitcoin transactions require that all of the input coins are moved to the outputs. This means that every bitcoin is stored on the ledger as the output of some previous transaction (these are called “unspent transaction outputs” or UTXOs).
- The coins are inseparable from the blockchain. They only exist as entries on the ledger (This means the coins aren’t technically “in your wallet” as would be the case for a paper or metal currency).
- Everyone participating in the network has (or can have) a full copy of the blockchain.
- Each cryptocurrency has its own blockchain.
If you want someone to pay you in bitcoin, you give the payer a payment address. This address represents an output of the transaction on the blockchain. To make the transaction, the payer provides one or more inputs to the transaction (these inputs were previously the outputs of some other transaction(s)). If the payer’s inputs sum to more than the amount they want to pay you, they must also provide another output (payment address) to receive the change (because the rules require that the inputs be completely spent).
Here’s a conceptual example of a bitcoin transaction. The addresses in this example are contrived for clarity of the example and have no resemblance to actual bitcoin addresses. This example also ignores transaction fees for simplicity:
Bob has 1.2 bitcoins that he received in 3 different transactions of 0.3, 0.4, and 0.5 bitcoins. These were paid to addresses 12345, 12346, and 12347 respectively. Bob wants to pay you 0.6 bitcoins. You give Bob the payment address 22345. Bob (actually Bob’s bitcoin software) will create a transaction with inputs 12345 (0.3 btc) and 12346 (0.4 btc) for for a total of 0.7 btc going into the transaction. The outputs of the transaction will be your payment address 22345 which will receive 0.6 btc and Bob’s change address 12348 which will receive 0.1 btc. The sum of the outputs equals the sum of the inputs and 12345 and 12346 will no longer contain any coins.
Each of the addresses in the transaction, 12345, 12346, 12347, 12348, and 22345 have a corresponding secret part that is required to send the associated bitcoins to another address. When you create the address, you also create the secret part. In the example transaction, Bob used the secret parts to spend the bitcoins at 12345 and 12346. Since these aren’t real bitcoin addresses, I’ll just call these like secret(12345) and secret(12346)
Your bitcoin wallet is the list of all these addresses and their corresponding secret parts. The secret part is just another number. So before the transaction:
Bob’s wallet had:
- 12345 + secret(12345) representing 0.3 btc
- 12346 + secret(12346) representing 0.4 btc
- 12347 + secret(12347) representing 0.5 btc
Your wallet was empty.
After the transaction:
Bob’s wallet has:
- 12345 + secret(12345) representing 0 btc
- 12346 + secret(12346) representing 0 btc
- 12347 + secret(12347) representing 0.5 btc
- 12348 + secret(12348) representing 0.1 btc
Your wallet has:
- 22345 + secret(22345) representing 0.6 btc
- You (or probably your wallet software) use the list of non-secret addresses to determine your bitcoin balance by querying the blockchain for the bitcoin value associated with each one and then adding them up.
- Since the blockchain is public, anyone with the list can know your balance (but not necessarily that it belongs to you).
- To spend the bitcoins at each address you need the corresponding secret parts.
- Anyone with a secret part can spend the coins at the corresponding address so it is important to keep your wallet absolutely secret.
- It’s also important not to lose your wallet since there is no way to recover the secrets.
The wallet software is responsible for generating and keeping track of your key pairs (the addresses and secret parts) and for interacting with the blockchain on your behalf – by querying it for your balance and submitting transactions.
Key pair generation
The address pairs (the public part and the secret part) are actually cryptographic key pairs. These key pairs are generated independently by the wallet software and generation doesn’t require cooperation or interaction with the bitcoin network or the blockchain.
Some wallets generate the keys randomly so there is no relationship between successive addresses (which means if you lose or forget one there is no way to get it back and also that if someone manages to get one of yours they only get the bitcoins at that one and not all of your bitcoins.). Other wallets are called deterministic which means that they generate the keys pseudorandomly beginning with an initial “seed” of data. With a given seed, the wallet will always generate the same series of address key pairs. The benefit of a deterministic wallet is that as long as you can remember the seed you can recreate lost addresses. The downside is that if someone is able to steal your seed, they can access all of your bitcoins.