LearnBitcoin

Bitcoin Dev Archive 2023

segwit naming ambiguity

5 messages, 11 Aug 2023. Started by Tobin Harding.

Tobin Harding, symphonicbtc, Antoine Poinsot, Pavol Rusnak, Andrew Poelstra

Permalink Original thread on gnusha.org

Tobin Harding permalink original

Question for OG bitcoin API designers please.

If you were to see the following function

    `is_segwit()`

would you assume it returns `true` or `false` for a p2tr transaction?

Currently we (rust-bitcoin) are being liberal with the use of `v0` but its a pretty ugly. Is there an official, or widely used, name for segwit v0?

Thanks, Tobin.

symphonicbtc in reply to Tobin Harding permalink original

Transactions should be considered segwit if they match the witness program structure of "A scriptPubKey (or redeemScript as defined in BIP16/P2SH) that consists of a 1-byte push opcode (for 0 to 16) followed by a data push between 2 and 40 bytes" as defined in [BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program). This would return true for version 0 segwit outputs as well as version 1 taproot outputs.

I would propose a different function for differentiating v0 vs taproot scripts. Bitcoin core uses an [enum](https://github.com/bitcoin/bitcoin/blob/master/src/script/standard.h#L51), of which you can use something similar. Core also uses a different [enum](https://github.com/bitcoin/bitcoin/blob/master/src/outputtype.h) for wallet logic, but it has a highly confusing naming scheme which imo should be avoided.

Additionally, witness programs nested inside P2SH need to be considered. The context in which these are interpreted depends on whether or not they should be differentiated from other P2SH, as is shown with the 2 separate enums from core. It is possible that this would cause some ambiguity, and it should be explicitly documented how such functions would return for different types of programs. These nested programs should technically also be considered segwit for a simple `is_segwit`, as they have segregated witness data.

v0 is the most common way of referring to version 0 segwit.

Symphonic

------- Original Message ------- On Friday, August 11th, 2023 at 4:45 AM, Tobin Harding via bitcoin-dev bitcoin-dev@lists•linuxfoundation•org wrote:

Quoted text (17 lines)
> Question for OG bitcoin API designers please.
>
> If you were to see the following function
>
> `is_segwit()`
>
> would you assume it returns `true` or `false` for a p2tr transaction?
>
> Currently we (rust-bitcoin) are being liberal with the use of `v0` but
> its a pretty ugly. Is there an official, or widely used, name for segwit v0?
>
> Thanks,
> Tobin.
> _______________________________________________
> bitcoin-dev mailing list
> bitcoin-dev@lists•linuxfoundation•org
> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
Antoine Poinsot in reply to Tobin Harding permalink original

Hey Tobin,

I would assume `is_segwit()` is true for P2TR, since Taproot is Segwit.

I'm not aware of a different term for "is P2WPKH or P2WSH" that "is Segwit v0". Maybe look into Murch's BIP about wording? He could have a better name for Segwit v0 there.

Cheers, Antoine

------- Original Message ------- On Friday, August 11th, 2023 at 6:45 AM, Tobin Harding via bitcoin-dev <bitcoin-dev@lists•linuxfoundation•org> wrote:

Quoted text (19 lines)
> Question for OG bitcoin API designers please.
>
> If you were to see the following function
>
> `is_segwit()`
>
> would you assume it returns `true` or `false` for a p2tr transaction?
>
>
> Currently we (rust-bitcoin) are being liberal with the use of `v0` but
> its a pretty ugly. Is there an official, or widely used, name for segwit v0?
>
>
> Thanks,
> Tobin.
> _______________________________________________
> bitcoin-dev mailing list
> bitcoin-dev@lists•linuxfoundation•org
> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
Pavol Rusnak in reply to Tobin Harding permalink original

Here's my idea for a clean API:

1. `is_segwit()` returns true for a p2tr output

2. a function `segwit_version()` which returns an int according to segwit version used

3. helper functions like `is_p2wpkh()`, `is_p2tr()`, `is_p2wsh()` for all script types which all return a bool

On Fri, 11 Aug 2023 at 08:41, Tobin Harding via bitcoin-dev < bitcoin-dev@lists•linuxfoundation•org> wrote:

Quoted text (21 lines)
> Question for OG bitcoin API designers please.
>
> If you were to see the following function
>
>     `is_segwit()`
>
> would you assume it returns `true` or `false` for a p2tr transaction?
>
>
> Currently we (rust-bitcoin) are being liberal with the use of `v0` but
> its a pretty ugly. Is there an official, or widely used, name for segwit
> v0?
>
>
> Thanks,
> Tobin.
> _______________________________________________
> bitcoin-dev mailing list
> bitcoin-dev@lists•linuxfoundation•org
> https://lists.linuxfoundation.org/mailman/listinfo/bitcoin-dev
>
Andrew Poelstra in reply to Tobin Harding permalink original

On Fri, Aug 11, 2023 at 02:45:57PM +1000, Tobin Harding via bitcoin-dev wrote:

Quoted text (12 lines)
> Question for OG bitcoin API designers please.
>
> If you were to see the following function
>
>     `is_segwit()`
>
> would you assume it returns `true` or `false` for a p2tr transaction?
>
>
> Currently we (rust-bitcoin) are being liberal with the use of `v0` but
> its a pretty ugly. Is there an official, or widely used, name for segwit v0?
>

As others have said, I think `is_segwit` should match all segwit versions. Pavol also sorta answered your "widely used name" question though he didn't draw attention to it -- segwit v0 outputs are called "p2wsh" or "p2wpkh".

But I don't know any term that covers "p2wsh or p2wpkh but not p2tr". Other than "segwit_v0", which we are currently using, and which I agree is a bit of a mouthful.