<blockquote>
  <p>I'm implementing BIP49-compatible key derivation for bitcoin-s, but I'm having a hard time finding the magic version bytes to use for the extended keys.</p>
</blockquote>

<p>It's prefixes are: <code>YPUB = 0x049D7CB2</code> and <code>YPRV = 0x049D7878</code></p>

<blockquote>
  <p>Is there multiple schemes for deriving P2WPKH-nested-in-P2SH?</p>
</blockquote>

<p>You are confusing the two. <code>77429938</code> that you have linked in Trezor is used to derive <code>ypub</code>. If you scroll down on Samourai code you will see <code>0x049D7CB2</code> for <code>ypub</code> which is same as <code>77429938</code> in decimal. <code>xpub</code> stands for <code>extended public key</code> and allows you to generate addresses and scan all the funds associated with them, but not spend them. <code>ypub</code> is exactly like an <code>xpub</code>, except the 'Y' denotes with certainty that this particular extended public key belongs to a wallet that is also following the BIP49 bitcoin standard. This instructs the wallet to definitely scan for P2SH(P2WPKH) addresses. There is also <code>zpub</code>, and that instructs the wallet to only scan for <code>bech32</code> addresses that starts with <code>bc</code>.</p>

CC BY-SA 4.0
2019-04-30T15:40:05.470Z
I'm aware of what an `xpub` is. The reason I'm asking if there are multiple schemes is that I can't get the tests from BIP49 to line up with the values found in the Trezor and Samourai code. If I use the magic key version bytes from Trezor and Samourai I end up with xprivs that begin with `uprv`, whereas the BIP49 test vector says I should get a `tprv`. (making it look like the BIP author just used normal non-segwit testnet bytes)
torkel
CC BY-SA 4.0
2019-04-30T15:48:06.153Z
@torkel `tpriv` and `upriv` are for testnet. In https://github.com/trezor/trezor-firmware/blob/master/common/defs/bitcoin/bitcoin.json#L17 the magic numbers listed are for xpriv_magic, xpub_magic, xpub_magic_segwit_p2sh(same as ypub) and xpub_magic_segwit_native(same as zpub)
Ugam Kamat
CC BY-SA 4.0
2019-04-30T16:41:34.210Z
@ugam-kamet I'm aware that `tprv` and `uprv` are for testnet. The issue here is that the values from Trezor and Samourai produce `uprv` keys, whereas the BIP produce `tprv` keys. That's why I'm asking if Trezor and Samourai are not BIP49-compliant, causing multiple schemes for deriving P2WPKH-nested-in-P2SH
torkel
CC BY-SA 4.0
2019-04-30T16:53:57.710Z
@torkel Now I get your confusion. BIP 49 fails to change the HD seed version bytes and as a result it retains the same prefix as BIP 32 i.e. `tpriv`. Trezor and Samourai uses the `ypub` and `zpub` to clearly distinguish if they have to scan for P2SH(P2WPKH). Whatever may be the prefix, the underlying hex of private/public key is the same. So the addresses generated will also be the same. It's just encoding of the keys that is changing.
Ugam Kamat
CC BY-SA 4.0
2019-04-30T16:59:32.373Z
According to Trezor's blog (https://blog.trezor.io/rolling-out-trezor-wallet-segwit-segregated-witness-5700269debc5), they use BIP-49 for P2SH(P2WPKH) derivation
Ugam Kamat
CC BY-SA 4.0
2019-04-30T14:31:08.197Z
2019-04-30T14:31:08.197Z
Ugam Kamat
<p>I'm implementing BIP49-compatible key derivation for <a href="https://github.com/bitcoin-s/bitcoin-s-core/" rel="nofollow noreferrer"><code>bitcoin-s</code></a>, but I'm having a hard time finding the magic version bytes to use for the extended keys. </p>

<p>I've been looking at <a href="https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki" rel="nofollow noreferrer">BIP49</a>, but can't find anything there. </p>

<p>Also, is there multiple schemes for deriving P2WPKH-nested-in-P2SH? I've been looking at the <a href="https://github.com/Samourai-Wallet/ExtLibJ/blob/87fcb87f87ed86c38d4b82aefac6c59ec981bdad/java/com/samourai/wallet/util/FormatsUtilGeneric.java#L30" rel="nofollow noreferrer">Samourai fork of BitcoinJ</a>, <a href="https://github.com/trezor/trezor-firmware/blob/master/common/defs/bitcoin/bitcoin.json#L17" rel="nofollow noreferrer">Trezor's implementation</a> and the test vectors in BIP49, and I can't get the vectors to pass by using the magic version bytes from Trezor/Samourai. </p>

<p>Maybe I'm confused by which magic bytes from Samourai corresponds to mainnet/testnet P2WPKH-nested-in-P2SH, so any clarification is appreciated here. From my understanding, the mainnet version bytes are <code>0x049D7CB2</code> (pub) and <code>0x049D7878</code> (priv) and testnet are <code>0x044a5262</code> (pub) and <code>0x044a4e28</code> (priv).</p>

<p>As a final thing, any pointers to test vectors for BIP49 and BIP84 is appreciated. Currently I've come across the BIPs themselves, as well as <a href="https://github.com/satoshilabs/slips/blob/master/slip-0132.md#bitcoin-test-vectors" rel="nofollow noreferrer">SLIP132</a>.</p>

CC BY-SA 4.0
2019-04-30T14:32:20.577Z
It's always a good practice to post multiple questions separately, so that people who know one but not the other can help you out. For example, I do not have the document for test vectors handy but I have tried to answer other questions. I'll edit my answer if and when I get the test vectors.
Ugam Kamat
<wallet><bip32-hd-wallets>
What's the magic key version bytes for BIP49
torkel
# What's the magic key version bytes for BIP49

(Asked by: username_0 on 2019-04-30)

I'm implementing BIP49-compatible key derivation for [`bitcoin-s`](https://github.com/bitcoin-s/bitcoin-s-core/), but I'm having a hard time finding the magic version bytes to use for the extended keys.

I've been looking at [BIP49](https://github.com/bitcoin/bips/blob/master/bip-0049.mediawiki), but can't find anything there.

Also, is there multiple schemes for deriving P2WPKH-nested-in-P2SH? I've been looking at the [Samourai fork of BitcoinJ](https://github.com/Samourai-Wallet/ExtLibJ/blob/87fcb87f87ed86c38d4b82aefac6c59ec981bdad/java/com/samourai/wallet/util/FormatsUtilGeneric.java#L30), [Trezor's implementation](https://github.com/trezor/trezor-firmware/blob/master/common/defs/bitcoin/bitcoin.json#L17) and the test vectors in BIP49, and I can't get the vectors to pass by using the magic version bytes from Trezor/Samourai.

Maybe I'm confused by which magic bytes from Samourai corresponds to mainnet/testnet P2WPKH-nested-in-P2SH, so any clarification is appreciated here. From my understanding, the mainnet version bytes are `0x049D7CB2` (pub) and `0x049D7878` (priv) and testnet are `0x044a5262` (pub) and `0x044a4e28` (priv).

As a final thing, any pointers to test vectors for BIP49 and BIP84 is appreciated. Currently I've come across the BIPs themselves, as well as [SLIP132](https://github.com/satoshilabs/slips/blob/master/slip-0132.md#bitcoin-test-vectors).

**Tags:** wallet, bip32-hd-wallets

> **Comments:**
> **username_1**: It's always a good practice to post multiple questions separately, so that people who know one but not the other can help you out. For example, I do not have the document for test vectors handy but I have tried to answer other questions. I'll edit my answer if and when I get the test vectors.  

---

## 1 answers

### Answer by username_1 on 2019-04-30. Score: 2

> I'm implementing BIP49-compatible key derivation for bitcoin-s, but I'm having a hard time finding the magic version bytes to use for the extended keys.

It's prefixes are: `YPUB = 0x049D7CB2` and `YPRV = 0x049D7878`

> Is there multiple schemes for deriving P2WPKH-nested-in-P2SH?

You are confusing the two. `77429938` that you have linked in Trezor is used to derive `ypub`. If you scroll down on Samourai code you will see `0x049D7CB2` for `ypub` which is same as `77429938` in decimal. `xpub` stands for `extended public key` and allows you to generate addresses and scan all the funds associated with them, but not spend them. `ypub` is exactly like an `xpub`, except the 'Y' denotes with certainty that this particular extended public key belongs to a wallet that is also following the BIP49 bitcoin standard. This instructs the wallet to definitely scan for P2SH(P2WPKH) addresses. There is also `zpub`, and that instructs the wallet to only scan for `bech32` addresses that starts with `bc`.

> **Comments:**
> **username_0**: I'm aware of what an `xpub` is. The reason I'm asking if there are multiple schemes is that I can't get the tests from BIP49 to line up with the values found in the Trezor and Samourai code. If I use the magic key version bytes from Trezor and Samourai I end up with xprivs that begin with `uprv`, whereas the BIP49 test vector says I should get a `tprv`. (making it look like the BIP author just used normal non-segwit testnet bytes)  
> **username_1**: @username_0 `tpriv` and `upriv` are for testnet. In https://github.com/trezor/trezor-firmware/blob/master/common/defs/bitcoin/bitcoin.json#L17 the magic numbers listed are for xpriv_magic, xpub_magic, xpub_magic_segwit_p2sh(same as ypub) and xpub_magic_segwit_native(same as zpub)  
> **username_0**: @username_1-kamet I'm aware that `tprv` and `uprv` are for testnet. The issue here is that the values from Trezor and Samourai produce `uprv` keys, whereas the BIP produce `tprv` keys. That's why I'm asking if Trezor and Samourai are not BIP49-compliant, causing multiple schemes for deriving P2WPKH-nested-in-P2SH  
> **username_1**: @username_0 Now I get your confusion. BIP 49 fails to change the HD seed version bytes and as a result it retains the same prefix as BIP 32 i.e. `tpriv`. Trezor and Samourai uses the `ypub` and `zpub` to clearly distinguish if they have to scan for P2SH(P2WPKH). Whatever may be the prefix, the underlying hex of private/public key is the same. So the addresses generated will also be the same. It's just encoding of the keys that is changing.  
> **username_1**: According to Trezor's blog (https://blog.trezor.io/rolling-out-trezor-wallet-segwit-segregated-witness-5700269debc5), they use BIP-49 for P2SH(P2WPKH) derivation  

---

