Controlling the wallet

As we can see wallet component startup and shutdown is largely driven from outside the wallet codebase from src/init.cpp.

Once the wallet component is started and any wallets supplied via argument have been verified and loaded, wallet functionality ceases to be called from init.cpp and instead is controlled using external programs in a number of ways. The wallet can be controlled using bitcoin-cli or bitcoin-qt GUI, and wallet files can be interacted with using the stand-alone bitcoin-wallet tool.

Both bitcoind and bitcoin-qt run a (JSON) RPC server which is ready to service, amongst other things, commands to interact with wallets. The command line tool bitcoin-cli will allow interaction of any RPC server started by either bitcoind or bitcoin-qt.

If using bitcoin-qt there is also an RPC console built into the GUI or you can run with -server=1 to allow access via bitcoin-cli.

If using the bitcoin-qt GUI itself then communication with the wallet is done directly via qt’s WalletModel interface.

Commands which can be used to control the wallet via RPC are listed in rpcwallet.cpp.

Wallet via RPC

If we take a look at the loadwallet RPC we can see similarities to WalletClientImpl's LoadWallets() function.

However this time the function will check the WalletContext to check that we have a wallet context (in this case a reference to a chain interface) loaded. Next it will call wallet.cpp#LoadWallet which starts by grabbing g_loading_wallet_mutex and adding the wallet to g_loading_wallet_set, before calling LoadWalletInternal which adds the wallet to vpwallets and sets up various event notifications.

Further operation of the wallet RPCs are detailed in their man pages, but one thing to take note of is that whilst loadwallet() (and unloadwallet()) both take a wallet_name argument, the other wallet RPCs do not. Therefore in order to control a specific wallet from an instance of bitcoin{d|-qt} that has multiple wallets loaded, bitcoin-cli must be called with the -rpcwallet argument, to specify the wallet which the action should be performed against, e.g. bitcoin-cli --rpcwallet=your_wallet_name getbalance

Via bitcoin-cli tool

Blockchain Commons contains numerous guides and examples of controlling the wallet using bitcoin-cli, including: