Is there an address format for P2WPKH or P2WSH? If P2WPKH uses the same address format as P2PKH, how does a wallet know which one to use?
Is there an address format for P2WPKH or P2WSH?
No. BIP142 would have provided an address format for these output scripts, but it is in "deferred" status and expected to remain that way. Wallet developers are being encouraged to discuss a new address format on the bitcoin-dev mailing list, and it is my understanding that one proposal may be coming soon.
In the meantime, BIP70 supports paying to arbitrary outputs, so it can be used for the native segwit outputs, as can any other software that communicates using its own protocol (such as payment channel software like that being used for various Lightning Network implementations).
If P2WPKH uses the same address format as P2PKH, how does a wallet know which one to use?
Although the new address proposal will probably not use base58check like the current address formats for P2PKH and P2SH, the now-deferred proposal in BIP142 explains how this could easily be done by changing the first byte of the address (which is a version number):
"For P2WPKH address, the address version is 6 (0x06) for a main-network address or 3 (0x03) for a testnet address.
"For P2WSH address, the address version is 10 (0x0A) for a main-network address or 40 (0x28) for a testnet address."
-
4There is one now - github.com/bitcoin/bips/blob/master/bip-0173.mediawiki – tsusanka Dec 5 '17 at 12:07
Yes. The format used for encoding native version 0 SegWit scriptPubKey
s (P2WPKH
and P2WSH
) is bech32
, defined by bip-0173
.
Unfortunately, at the time of writing, a weakness has been discovered regarding the usage of this encoding for the proposed version 1 SegWit scriptPubKey
s (Taproot).
There are ongoing discussions for how to modify this encoding for version 1 and onward SegWit addresses.
If P2WPKH uses the same address format as P2PKH, how does a wallet know which one to use?
SegWit scriptPubKey
s, as defined by bip-0141
, are constructed differently than legacy P2PKH
scriptPubKey
s.
Based on the Script, a wallet knows what encoding to use.
To be explicit:
- If the
scriptPubKey
is of the the formDUP HASH160 <pk hash> EQUALVERIFY CHECKSIG
- Use base58-check encoding
- Else if the
scriptPubKey
is of the the form<1-byte push> <2-to-40-bytes push>
- Use bech32 encoding
It's likely that a new branch will be eventually be added to the above logic based on the new encoding that will be chosen for version >= 1
SegWit scriptPubKey
s.