Hi,
I am running a service that accepts user deposits and sends withdrawals. I am using Bitcoin Core for the wallet.
I'd like to go full-Segwit with my service, but here's the problem: bitcoind wallet does not generate P2SH-P2WPKH (Segwit) change addresses by default. So even if I generate P2SH-P2WPKH addresses to accept deposits from my users via
addwitnessaddress, whenever I send coins, the change is going to a P2PKH address.
I could manually create a P2SH-P2WPKH change address by (in pseudocode):
tx = createrawtransaction([], amounts)
chaddr = getrawchangeaddress()
chaddr = addwitnessaddress(chaddr) # this creates a P2SH-P2WPKH address from the P2PKH change address
# now fund the transaction
tx = fundrawtransaction(tx, changeAddress: chaddr)['hex']
tx = signrawtransaction(tx)['hex']
sendrawtransaction(tx)
No the thing is that
addwitnessaddress(chaddr) breaks things a little since the resulting address is added to account addresses, and is NOT marked as a change address. Therefore it is used as a regular account address and generates send/receive transactions. It looks like it does not break anything except creating additional transactions returned by
listtransactions and inaccurate "amount" fields.
So, a solution I'd really love to see is:
1. make the wallet generate segwit change addresses by itself so that we can just use
sendtoaddress and other RPC calls without workarounds (maybe an option in config?),
2. somehow make the address resulting from
addwitnessaddress(chaddr) as a change address,
3. or allow
getrawchangeaddress to generate P2SH-P2WPH address in the first place.
Obviously (1) would be much better, but at this point I am looking for any solution that would "just work" and not break my workflow.
Cheers and thanks,
Ethan
Thanks for this code Ethan. Have you managed to do this with sendmany as well?
And something else. When you pay, the change is coming back to a Legacy Address (1...). What if you add with addwitnessaddress a SegWit address to this new Legacy change address? Will the balance on the newly created segwit address be the same as the Legacy change address? If yes, when paying again from wallet, will the wallet use the Legacy change address or the segwit address as input? Are there any risks with creating a segwit address for a legacy address that has a balance on it?