Skip to content
John Newbery edited this page May 27, 2022 · 1 revision

Written: Sept 2019

Wallet development

What are a wallet’s functions?

  • Key management
    • Identify owned transactions
    • Generating new addresses
    • Determining how to sign transactions
  • Constructing and sending transactions
    • Parsing addresses and turning them into txOuts
    • Selecting UTXOs (coin selection)
    • Signing inputs
  • Persistence
    • Storing keys
    • Storing UTXOs
    • Storing transaction history
    • Storing metadata (eg how far through the blockchain have I parsed?)

Glossary

Initialization and Interfaces

Why all this indirection?

To fully separate the wallet from the node:

  • Well defined interface is easier to reason about
  • Individual components can be tested in isolation
  • Separate wallet into a different process
  • Potential for different wallet implementations

Code Management

  • coinselection.cpp|h - Coin selection algorithm
  • crytper.cpp|h - encrypting the wallet’s private keys
  • [wallet]db.cpp|h - interface to wallet’s database for persistent storage
  • init.cpp - initializing the wallet module
  • load.cpp|h - loading/starting/stopping individual wallets
  • rpc.cpp|h* - wallet’s RPC interface
  • wallettool.cpp|h - standalone wallet tool binary
  • wallet.cpp|h - EVERYTHING ELSE

Key Management

Identify Owned Transactions

Generating Addresses

  • The Bitcoin Core wallet was originally a collection of unrelated private keys
  • If a new address was required, a new private key could be generated
  • What are the problems with this?
  • Giving an address out and then restoring from a backup loses funds!

Keypools

HD Wallets

Generating Keys

(https://github.com/bitcoin/bitcoin/blob/431d81b61ca968da2d7c25f0d56455a44cd46fed/src/wallet/wallet.cpp#L190)

Constructing and Sending Transactions

Parsing addresses and Constructing Transactions

Selecting UTXOs (coin selection)

Signing inputs

Sending Transactions

Persistence

Future Directions