I am trying to create bitcoin child addresses so that when money comes to them, this money is displayed on the main address. I seem to be able to create child addresses, but for some reason, when money comes to them, the balance of the main address does not change. I wanted to find out if there is an error in my code or if I misunderstood how child addresses work?
My code:
import bip32utils
from bitcoinlib.encoding import *
from bitcoinlib.wallets import *
from bitcoinlib.keys import *
from bitcoinlib.mnemonic import Mnemonic
from bitcoinlib.wallets import Wallet, wallet_delete
from data.config import SECRET_PHRASE_BTC
btc_seed = Mnemonic('english').to_seed(SECRET_PHRASE_BTC)
btc_root_key = bip32utils.BIP32Key.fromEntropy(btc_seed)
btc_root_address = btc_root_key.Address()
child_key = btc_root_key.ChildKey(0).ChildKey(0)
child_address = child_key.Address()
print(child_address)
As far as I know, you can generate a Bitcoin parent key (address) by using a mnemonic. Then if you want to hold various Bitcoin addresses (to distinguish people sending you bitcoins, for privacy reasons...) then you may be interested in deriving multiple Bitcoin child keys from your Bitcoin parent key (that way you have to remember a single mnemonic). This is the purpose of BIP32. However from a Bitcoin UTXOs (balance) point of view, if bitcoins are sent to a Bitcoin child address, except if you have the Bitcoin parent private key and the derivation path, there is no link between your Bitcoin parent and a child address. This is the reason why the Bitcoin amount received on a child address doesn't show up on a parent one.
To count your total balance (received on parent and child addresses), you have to fetch the balance for each address and sum.