Bitcoin Forum
April 20, 2024, 04:30:22 AM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: P2SH-P2WPKH (Segwit) change addresses in Bitcoin Core - how?  (Read 3732 times)
ethan_nx (OP)
Sr. Member
****
Offline Offline

Activity: 745
Merit: 471


Admin at YOLOdice.com - fast, fair, play/invest.


View Profile WWW
August 21, 2017, 11:15:13 AM
Last edit: August 21, 2017, 04:37:33 PM by ethan_nx
Merited by ABCbits (1)
 #1

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):

Code:
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

1713587422
Hero Member
*
Offline Offline

Posts: 1713587422

View Profile Personal Message (Offline)

Ignore
1713587422
Reply with quote  #2

1713587422
Report to moderator
1713587422
Hero Member
*
Offline Offline

Posts: 1713587422

View Profile Personal Message (Offline)

Ignore
1713587422
Reply with quote  #2

1713587422
Report to moderator
1713587422
Hero Member
*
Offline Offline

Posts: 1713587422

View Profile Personal Message (Offline)

Ignore
1713587422
Reply with quote  #2

1713587422
Report to moderator
"Bitcoin: the cutting edge of begging technology." -- Giraffe.BTC
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713587422
Hero Member
*
Offline Offline

Posts: 1713587422

View Profile Personal Message (Offline)

Ignore
1713587422
Reply with quote  #2

1713587422
Report to moderator
1713587422
Hero Member
*
Offline Offline

Posts: 1713587422

View Profile Personal Message (Offline)

Ignore
1713587422
Reply with quote  #2

1713587422
Report to moderator
1713587422
Hero Member
*
Offline Offline

Posts: 1713587422

View Profile Personal Message (Offline)

Ignore
1713587422
Reply with quote  #2

1713587422
Report to moderator
amaclin1
Sr. Member
****
Offline Offline

Activity: 770
Merit: 305


View Profile
August 21, 2017, 04:17:50 PM
 #2

https://bitcointalk.org/index.php?topic=1687368.0

Bitcoin SV GUI client for Windows and Linux
https://github.com/AlisterMaclin/bitcoin-sv/releases
ethan_nx (OP)
Sr. Member
****
Offline Offline

Activity: 745
Merit: 471


Admin at YOLOdice.com - fast, fair, play/invest.


View Profile WWW
August 21, 2017, 04:34:40 PM
 #3

Thanks for the link. But actually this discussion did not reach any conclusion. My use case is simple: I am using Bitcoin Core as a wallet and I want to benefit from Segwit addresses. Since Bitcoin Core is THE Bitcoin wallet, it should somehow help its users migrate to Segwit setups.

This problem will become more common in the next few days when people realize it's not THAT easy to use Segwit.

I know the initial rationale for not enabling Segwit addresses by default is to protect the privacy, but come on, it should not be that difficult to use Bitcoin Core as a 100% Segwit wallet.

pianist
Legendary
*
Offline Offline

Activity: 954
Merit: 1003


View Profile
August 31, 2017, 11:18:49 AM
 #4

+1, need a simple solution to migrate to segwit addresses.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3374
Merit: 6511


Just writing some code


View Profile WWW
August 31, 2017, 02:34:40 PM
 #5

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?),
This is being worked on for the next version of Core (after 0.15.0). It will likely be a minor release.

pianist
Legendary
*
Offline Offline

Activity: 954
Merit: 1003


View Profile
August 31, 2017, 05:18:32 PM
 #6

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?),
This is being worked on for the next version of Core (after 0.15.0). It will likely be a minor release.

any announces from Core?

of course, we can manually create raw transactions, but it's not a good solution
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3374
Merit: 6511


Just writing some code


View Profile WWW
August 31, 2017, 05:30:33 PM
 #7

any announces from Core?
No, not yet. This type of stuff isn't announced ahead of time, it just happens and goes into a release which is then announced. However we are actively discussing this in Github PRs and issues related to adding additional segwit wallet functionality and on IRC.

pianist
Legendary
*
Offline Offline

Activity: 954
Merit: 1003


View Profile
August 31, 2017, 08:27:08 PM
 #8

any announces from Core?
No, not yet. This type of stuff isn't announced ahead of time, it just happens and goes into a release which is then announced. However we are actively discussing this in Github PRs and issues related to adding additional segwit wallet functionality and on IRC.

can you give some links to these discussions?
 I would like to read/join Smiley
miguelmorales85
Hero Member
*****
Offline Offline

Activity: 544
Merit: 506


View Profile
August 31, 2017, 08:54:12 PM
 #9

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?),
This is being worked on for the next version of Core (after 0.15.0). It will likely be a minor release.
Are you sure about this? I've heard comments that this is not by default on 0.15
You still have to use the cli
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3374
Merit: 6511


Just writing some code


View Profile WWW
August 31, 2017, 09:04:16 PM
 #10

can you give some links to these discussions?
 I would like to read/join Smiley
A lot of it happens in the #bitcoin-core-dev channel on IRC, usually during the weekly dev meetings (every Thursday at 19:00 UTC).

Are you sure about this? I've heard comments that this is not by default on 0.15
You still have to use the cli
That is correct. That's why I said "after 0.15.0" and "It will likely be a minor release" (hint: 0.15.0 is not a minor release).

Casimir1904
Full Member
***
Offline Offline

Activity: 209
Merit: 100


Radix-The Decentralized Finance Protocol


View Profile
September 01, 2017, 01:29:14 PM
 #11

It would make sense to enable it as config option.
changeAddressType = P2PKH,P2SH-P2PK,P2SH-P2WPKH
So users can choose what type of change address they want.

   R A D I X   ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬   The Decentralized Finance Protocol
█████████ GET TOKENS █████████    Facebook      Telegram      Twitter
The Radix DeFi Protocol is    SCALABLE SECURE COMMUNITY DRIVEN
btc_enigma
Hero Member
*****
Offline Offline

Activity: 688
Merit: 565


View Profile
September 03, 2017, 07:15:55 AM
 #12

Even I am looking forward to this. I thought segwit support in core meant this was already working.

It would make sense to enable it as config option.
changeAddressType = P2PKH,P2SH-P2PK,P2SH-P2WPKH
So users can choose what type of change address they want.

This makes sense , default can be P2SH-P2WPKH

ethan_nx (OP)
Sr. Member
****
Offline Offline

Activity: 745
Merit: 471


Admin at YOLOdice.com - fast, fair, play/invest.


View Profile WWW
September 04, 2017, 01:18:24 PM
 #13

I have just enabled full Segwit support in YOLOdice. I am using the code from my first post to generate segwit change addresses and it seems to work fine. The only thing I added was a global lock so that no two processes create a withdrawal - this is to avoid race condition while using fundrawtransaction.

As I said earlier, this method has one disadvantage - it creates extra entries returned by "listtransactions" and an extra entry for "gettransaction" details because the change output is treated as a 1st class transfer now.

Anyway, it works find for me. My rationale to get early on the segwit bandwagon is mostly to reduce fees.

Cheers,
Ethan

btc_enigma
Hero Member
*****
Offline Offline

Activity: 688
Merit: 565


View Profile
September 04, 2017, 03:00:33 PM
 #14

Hi Ethan,

Quote
tx = fundrawtransaction(tx, changeAddress: chaddr)['hex']

I really like the way you have used fundrawtransaction with segwitchange address

can you give some links to these discussions?
 I would like to read/join Smiley
A lot of it happens in the #bitcoin-core-dev channel on IRC, usually during the weekly dev meetings (every Thursday at 19:00 UTC).

Are you sure about this? I've heard comments that this is not by default on 0.15
You still have to use the cli
That is correct. That's why I said "after 0.15.0" and "It will likely be a minor release" (hint: 0.15.0 is not a minor release).

achow, do you have any idea on the timeline of this minor release? If it will take more than two months, I might as well use Ethan's solution (want to urgently reduce tx fee right)

achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3374
Merit: 6511


Just writing some code


View Profile WWW
September 04, 2017, 09:30:25 PM
 #15

achow, do you have any idea on the timeline of this minor release? If it will take more than two months, I might as well use Ethan's solution (want to urgently reduce tx fee right)
There is no ETA for the minor release.

s2
Full Member
***
Offline Offline

Activity: 198
Merit: 123


View Profile
September 06, 2017, 11:54:25 AM
 #16

Hi Ethan,
Thanks for sharing this.  Would you suggest others follow in your footsteps or would you suggest waiting until core has the minor update that includes segwit change addresses?
I.e. is there any obvious risk to implementing it the way you've done?
Casimir1904
Full Member
***
Offline Offline

Activity: 209
Merit: 100


Radix-The Decentralized Finance Protocol


View Profile
September 11, 2017, 10:48:17 PM
 #17

Hi Ethan,
Thanks for sharing this.  Would you suggest others follow in your footsteps or would you suggest waiting until core has the minor update that includes segwit change addresses?
I.e. is there any obvious risk to implementing it the way you've done?


I wont suggest to do such changes if you don't fully understand what you're doing.
I'm also waiting for support of different address types as change address ( prefered configurable and don't care much what the default is ).

   R A D I X   ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬   The Decentralized Finance Protocol
█████████ GET TOKENS █████████    Facebook      Telegram      Twitter
The Radix DeFi Protocol is    SCALABLE SECURE COMMUNITY DRIVEN
amaclin1
Sr. Member
****
Offline Offline

Activity: 770
Merit: 305


View Profile
September 12, 2017, 05:15:24 AM
 #18

I'm also waiting for support of different address types as change address
what about data-carrier (OP_RETURN) address type for a change?  Grin

Bitcoin SV GUI client for Windows and Linux
https://github.com/AlisterMaclin/bitcoin-sv/releases
columbo
Jr. Member
*
Offline Offline

Activity: 45
Merit: 1


View Profile
November 14, 2017, 03:51:40 PM
 #19

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):

Code:
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?
Boussac
Legendary
*
Offline Offline

Activity: 1220
Merit: 1015


e-ducat.fr


View Profile WWW
November 20, 2017, 11:40:29 AM
 #20

P2SH-P2WPKH addresses are meant to allow users to create segwit utxos with their (BIP16-enabled) current wallet.
We should all aim for native (Bech32) P2WPKH addresses.
Only electrum 3.0 currently supports those.
I understand that the feature will be merged soon on Bitcoin Core.

Pages: [1] 2 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!