If I open Metamask or Coinbase, for example, I see a balance. This balance is a flat number, similar to what we'd see in a bank to more intuitively check my networth or balance of a particular wallet.

I'm wondering how this is done, from a programmatic perspective. I think I kind of understand this idea of UTXO where if I have coins like (1 coin, 3 coin, 5 coin) and Alice charges 7 coin for some service, I would send 5 coin and 3 coin as input. The output would be 7 coin -> Alice wallet and 1 coin back to my wallet. (Let's ignore fees).

But how do I derive the balance of my wallet?

Intuitively, I get I could start at the genesis block and iterate linearly through the blockchain, however, each block has N transactions, so this seems like a simple derivation would entail scanning N blocks * M transactions/block.

This seems inefficient. How can I understand this better?

2 Answers 2


Your intuition is correct: in order to compute a wallet's balance bitcoind will scan through the entire blockchain, looking for a set of addresses that correspond to its own private keys. As you point out this is a rather time-consuming task. However we should note here that bitcoind needs to iterate through the blockchain and verify all transactions for validity anyway when bootstrapping, so checking if an address belongs to its wallet is rather trivial, compared to verifying scripts and signatures, so it's not adding much to that bootstrap time.

Once you are bootstrapped, you know that anything up to the point you verified is not going to change, so you can just incrementally scan new blocks as they come it, which often takes less than a second with todays code.

It is possible to build indices on which address was used in which transaction, which can help locating transactions that involve a certain transaction quickly, and there are wallets that make use of this (electrum, and all hardware wallets), however this is usefull mostly when you don't know all addresses at verification time, and you want to serve this information to users so they don't have to sync it themselves. To build the index however they themselves had to verify the entire chain. This is true for all blockchains by the way, either you are verifying the chain from its genesis, or you are trusting a third-party not to lie by omission (they can not report addresses that are yours), this is for example the case for metamask as well.

One final detail is that some seed schemas have a birthday date / height, which allows a wallet to start scanning from that height, since the seed basically says "don't bother scanning below, I had no addresses before this time". This saving is less efficient the older the seed is, since the skipped part will become smaller compared to the part that needs to be processed.


The first time you run bitcoin it will download and process every single block and will generate an index of all unspent outputs like

{ ‘outputABC’ : 1 btc
  ‘outputDEF’ : 3 btc

etc. When this table is constructed you use it to lookup all the values of the outputs owned by each address in your wallet.

  • Thanks, that makes a lot of sense!
    – Ryan
    Jan 29, 2022 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.