We are going to create a wallet with no private keys. Nevertheless we will add some addresses to track them.
First launch bitcoind node:
$ bitcoind -noconnect -printtoconsole=0
Create a new wallet
We will use createwallet command:
$ bitcoin-cli help createwallet
createwallet "wallet_name" ( disable_private_keys )
Creates and loads a new wallet.
Arguments:
1. "wallet_name" (string, required) The name for the new wallet. If this is a path, the wallet will be created at the path location.
2. disable_private_keys (boolean, optional, default: false) Disable the possibility of private keys (only watchonlys are possible in this mode).
We set disable_private_keys to true:
$ bitcoin-cli createwallet my_wallet true
Next time we run bitcoind we can load the already existing wallet.
$ bitcoind -wallet=my_wallet -noconnect -printtoconsole=0
or using loadwallet command:
$ bitcoin-cli loadwallet my_wallet
To import an address:
Import an address to default "" label, and do not start a rescan:
$ bitcoin-cli importaddress "3foobarbazfadfasdfadfafad" "" false
We do not start a rescan until we import the last address because rescanning takes a lot of time.
To list current labels:
$ bitcoin-cli listlabels
[ "", "other_label" ]
To show addresses per label:
$ bitcoin-cli getaddressesbylabel ""
{ "3foobarbazfadfasdfadfafad": { "purpose": "receive" } }
When adding the last address we perform a rescan:
$ bitcoin-cli importaddress "bc1foobarbazadasdfasfadaf" "" true
Show current balance
Once a rescan was performed, we can display current balance of added addresses:
$ bitcoin-cli help getbalance
getbalance ( "(dummy)" minconf include_watchonly )
Returns the total available balance.
The available balance is what the wallet considers currently spendable, and is
thus affected by options which limit spendability such as -spendzeroconfchange.
Arguments:
1. (dummy) (string, optional) Remains for backward compatibility. Must be excluded or set to "*".
2. minconf (numeric, optional, default=0) Only include transactions confirmed at least this many times.
3. include_watchonly (bool, optional, default=false) Also include balance in watch-only addresses (see 'importaddress')
To show right balance set include_watchonly to true:
$ bitcoin-cli getbalance "*" 1 true
# Show balance for at least one confirmation.0.05235821
NOTE: For commands like fundrawtransaction to work, we need the public key instead of the address:
We can import the public key using importpubkey command:
$ bitcoin-cli help importpubkey
importpubkey "pubkey" ( "label" rescan )
Adds a public key (in hex) that can be watched as if it were in your wallet but cannot be used to spend. Requires a new wallet backup.
$ bitcoin-cli importpubkey "my_public_key"
Import public key to "" label and perform a rescan:
$ bitcoin-cli importpubkey "my_public_key" "" true
When showing info about an address:
$ bitcoin-cli getaddressinfo "bc1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
{ "address": "bc1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "scriptPubKey": "whatever", "ismine": false, "iswatchonly": true, "isscript": false, "iswitness": true, "witness_version": 0, "witness_program": "whatever", "label": "", "timestamp": 0, "labels": [ { "name": "", "purpose": "receive" } ] }
If we imported the public key:
$ bitcoin-cli getaddressinfo "bc1bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
{ "address": "bc1bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "scriptPubKey": "whatever", "ismine": false, "iswatchonly": true, "isscript": false, "iswitness": true, "witness_version": 0, "witness_program": "whatever", "pubkey": "my_public_key", "label": "", "timestamp": 0, "labels": [ { "name": "", "purpose": "receive" } ] }
List addresses in related groups
We can list addresses in related groups (to track our privacy):
$ bitcoin-cli help listaddressgroupings
listaddressgroupings Lists groups of addresses which have had their common ownership made public by common use as inputs or as the resulting change in past transactions Result: [ [ [ "address", (string) The bitcoin address amount, (numeric) The amount in BTC "label" (string, optional) The label ] ,... ] ,... ]
$ bitcoin-cli listaddressgroupings
[ [ [ "my_first_address", 0.20000000, "" ] ], [ [ "my_second_address", 0.00000000, "" ] ] ]
Edit label of an existing address
To change label of an address we can execute again importaddress command:
$ bitcoin-cli importaddress "my_address" "new_label" false
We could see the new associated address using getaddressinfo command.