I am developing my own wallet in python for which I have already implemented BIP32 and BIP39. According to BIP32 documentation, change should be stored in different addresses as well as payments received. This is fine. However, how can I spend my bitcoins when they are stored in different addresses? Let's suppose I have 1000000 satoshis in 10000 different addresses that are derived from m/0'/1'/
path, and each address has 100 satoshis. If I need to send my 1000000 satoshis to XYZ address, can I build a single raw transaction to achieve this, or do I have to create a raw transaction for every single address that is storing my change? Isn't this very expensive?
1 Answer
You can do this with 1 transaction, it will get more expensive, but it has nothing to do with whether you use separate addresses or not (or with whether or not you're using BIP32 in any way or form, for that matter).
Bitcoin uses a UTXO model: every transaction has 1 or more inputs, and one or more outputs. Every output creates a UTXO (which you can think of as "a coin", with a certain address and a certain amount). Every input consumes an entire UTXO. Change is simply an output that goes back to yourself, because the sum of the input values exceeds the amount you want to send. Change always requires a separate output, whether it reuses an address or not.
So, what matters to the complexity (and thus cost) of your transaction is how many inputs you need. If that's outputs created in 10000 outputs, you will need 10000 inputs, regardless of whether they're all in the same address or not.
-
So it doesn't really matter if they come from different addresses as long as I have the private key for each one of those addresses so I can sign and spend every input? ohhh ok that makes so much sense! Thanks a lot! Commented Apr 3, 2020 at 23:35