Sending payments from an HD wallet I found these two questions very helpful in understanding HD wallets

1) How do hierarchical deterministic wallets work transaction-wise?

2) a few questions about how HD wallet works

However, I'm still not clear how sending payments would work in the case of HD wallet. Assume the following scenario:


derived public key #15 received 1 BTC in one transaction
derived public key #20 received 2 BTC in one transaction


Now that I want to send 2.5 BTC to some address, what is the procedure of doing that? Do I need to do two separate transactions?

Also I found it overall confusing in terms of what are taken care of by the wallet (bitcoin-core, etc) and what should be done by the user. Thus, would appreciate it if anyone can explain the above by highlighting some steps in bitcoin-core for example.
HD wallets are not special when it comes to transactions. Transactions don't care whether the keys involved were derived deterministically or generated randomly.

Think of a wallet split into two parts: the first part (which we will call the key handler) handles the private/public keypairs and creating digital signatures. The second part (which we will call the transaction handler) handles transaction receiving and transaction creation. Suppose the first part has some functions getnewpubkey (returns a new address) and signdatawithpubkey (receives a public key and some piece of data to sign, returns a digital signature).

So there are two different types of key handlers - ones which randomly generate keys and ones which generate them deterministically (aka HD wallets). These two can be used interchangeable with the transaction handler; the transaction handler does the exact same thing and it doesn't care about how the keys are generated. All it needs is to get keys when it needs them, and for the key handler to sign transactions when the transaction handler asks it to.



In your scenario, regardless of whether a HD wallet was used, the transaction handler knows of two transactions that it can spend from. These two transactions were sent to the addresses that correspond to two keys that the transaction handler asked the key handler to provide (i.e. it called getnewpubkey twice).

Now when you want to send, the transaction handler constructs a transaction that has your two received transactions as the inputs. Once it creates the transaction, it sends it to the key handler to sign (so it will call signwithpubkey twice and pass in the same transaction on both calls but one pubkey for the first call and the other pubkey for the second call). Then it takes those signatures and the unsigned transaction, combines them, and produces a final transaction which is then broadcast to the network.

Notice how the transaction handler which actually receives and creates the transaction does not know nor does it care about how the keys were derived. You could have used a key handler that randomly generates keys or one which deterministically generates them; it makes no difference.




  what should be done by the user. Thus, would appreciate it if anyone can explain the above by highlighting some steps in bitcoin-core for example.


The user does not need to do anything. The user clicks the buttons to get a new address when he wants a new address and clicks the buttons to send Bitcoin when he wants to send. Everything (choosing which transactions to use as inputs, creating the transaction, signing the transaction, etc.) is handled by the software. What the user does with transactions does not change whether the keys were randomly or deterministically generated.