1

It appears that there are many way to traverse Bitcoin addresses. I am looking best practice, and I am also looking for the documented logic of how common wallets do it so I can replicate their behaviour.

For example, Trezor creates the first address at this location: m/49'/0'/0'/0/0, and then the next address will be m/49'/0'/0'/0/1, and so on. So, it appears that it would be easy to just use an iterator over the last digit.

But, when you spend from m/49'/0'/0'/0/0, it moves the leftover to m/49'/0'/0'/1/0 . So, suddenly, I not only have to iterate over the last digit, I also have to iterate over the penultimate digit. I could have said "second last", but I said penultimate because it sounds more intelligent.

Is there standard practice for traversing addresses? Something like, keep traversing over addresses until you hit an address with no transactions? Or possibly say three addresses in a row with no transactions? Or is this really the wild west and we just do whatever we feel like?

  • I believe you might be looking for github.com/bitcoin/bips/blob/master/…, but I'm not sure it fully answers your question – MeshCollider Jan 8 '18 at 0:24
  • I think answers my question as long as there are no gotchas. Are there corner cases that are not covered here? I actually found that Ledger is not 100% with Trezor and Vice Versa - i.e. they don't follow the same algorithm, so one must not be following the spec correctly. – Melbourne Developer Jan 8 '18 at 0:49
  • Would you mind clarifying what the difference between the two is? – MeshCollider Jan 8 '18 at 0:51
  • I don't know. It's just that when I restored my Trezor wallet on to the Ledger, the Ledger was missing some addresses, and therefore the balances didn't add up. – Melbourne Developer Jan 8 '18 at 2:24
  • @MeshCollider, but actually, this document is for BIP0044, and yet Trezor creates addresses at 49 which is different to what BIP0044 recommends. I'm so confused. – Melbourne Developer Jan 8 '18 at 2:27
3

Most (but not all) wallets will follow what is specified in BIPs 32, 44, and 49.

BIP 32 defines what derivation paths are and what they mean. It is the standard for deriving all of the keys in a HD wallet.

BIPs 44 and 49 specify derivation paths that wallets should use. BIP 44 defines a standard derivation path format: m / purpose' / coin_type' / account' / change / address_index. It also defines the purpose for non-segwit Bitcoin keys.

When a wallet is deriving the keys, the path for external keys, i.e. those keys which you get by clicking "New Receive Address" (or similar) is m/44'/0'/0'/0/i and i is just incremented for new keys.

For internal keys, i.e. keys for change addresses, the derivation path will set the change field to 1, so the path will be m/44'/0'/0'/1/i and i is incremented for each new change address that is needed.

Since that change field is essentially a boolean, there is no need to iterate through any indexes other than 0 and 1.

BIP 49 specifies the purpose for keys used for P2SH nested segwit addresses. It just sets the purpose field to be 49, so the derivation paths will be m/49'/0'/0'/k/i where k is the change field and i is the address_index. The change and address_index fields remain the same as defined in BIP 44.


It is important to keep in mind that not all wallets follow BIPs 32, 44, and 49. For example, Armory uses its own HD wallet algorithm which predates BIP 32, although BIP 32 support for Armory is currently in the works. Other wallets such as Bitcoin Core and MultiBit HD do not follow BIP 44. Instead they use different derivation paths. For Bitcoin Core it's m/k'/i' where k is change (same definition as with BIP 44) and i is address_index (same definition as with BIP 44).

  • This is a very clear explanation. Thankyou very much. It's enough for me to put the pieces together and also finish off my wallet. – Melbourne Developer Jan 8 '18 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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