<blockquote>
  <p>I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?</p>
</blockquote>

<p>No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).</p>

CC BY-SA 4.0
2019-01-09T17:37:39.567Z
Ah, I see now.  Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.
kawthuldrok
CC BY-SA 4.0
2019-01-07T22:15:05.863Z
2019-01-07T22:15:05.863Z
G. Maxwell
<p>From <a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Segwit_address_format" rel="nofollow noreferrer">BIP173</a> > Specification > Segwit address format > Decoding:</p>

<blockquote>
  <p>Software interpreting a segwit address:</p>
  
  <ul>
  <li>MUST verify that the human-readable part is "bc" for mainnet and "tb"    for testnet. </li>
  <li>MUST verify that the first decoded data value (the    witness version) is between 0 and 16, inclusive</li>
  </ul>
</blockquote>

CC BY-SA 4.0
2019-01-09T18:47:56.420Z
2019-01-10T17:06:21.567Z
2019-01-10T17:06:21.567Z
kawthuldrok
<p>The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.</p>

<p>We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.</p>

<p>Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.</p>

CC BY-SA 4.0
2019-01-07T21:39:40.623Z
2019-01-07T21:39:40.623Z
Pieter Wuille
<p>Looking at the code referenced in bip-0173, specifically for example here:
<a href="https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js" rel="nofollow noreferrer">https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js</a></p>

<pre><code>  function decode (hrp, addr) {
      var dec = bech32.decode(addr);
      if (dec === null || dec.hrp !== hrp || dec.data.length &lt; 1 || dec.data[0] &gt; 16) {
        return null;
      }
      ...
</code></pre>

<p>I understand that this is an error checking statement, but why check a (user?) inputted hrp vs the decoded hrp from bech32.decode?</p>

<p>Also I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex.  Is this correct?</p>

<segregated-witness><bech32-address>
Why does the decode function of the segwit bech32 encoder/decoder take a hrp (human readable part) as input?
kawthuldrok
# Why does the decode function of the segwit bech32 encoder/decoder take a hrp (human readable part) as input?

(Asked by: username_0 on 2019-01-07)

Looking at the code referenced in bip-0173, specifically for example here:
https://github.com/sipa/bech32/blob/master/ref/javascript/segwit_addr.js
  function decode (hrp, addr) {
      var dec = bech32.decode(addr);
      if (dec === null || dec.hrp !== hrp || dec.data.length < 1 || dec.data[0] > 16) {
        return null;
      }
      ...

I understand that this is an error checking statement, but why check a (user?) inputted hrp vs the decoded hrp from bech32.decode?
Also I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex.  Is this correct?

**Tags:** segregated-witness, bech32-address


---

## 3 answers

### Answer by username_2 on 2019-01-07. Score: 8 | Accepted answer

The HRP is part of the bech32 encoded string, and in the bech32 decoding API, it is returned along with the payload by the decoder, after checking the checksum.

We still want to compare it with the expected HRP in BIP173, which encodes the chain the software is operating on.

Otherwise you could have a testnet node that accepts mainnet BIP173 addresses or the other way around.


---

### Answer by username_1 on 2019-01-07. Score: 5

> I'm assuming that the "dec.data[0] > 16" check is to make sure that the byte at index zero doesn't exceed a value of 16, which would be invalid hex. Is this correct?

No, it's because only versions 0 - 16 are specified. Other versions might behave entirely differently. (As an aside, if it were a comparison for the sake of clamping to the range of a single hex character the test would be >15 not >16).

> **Comments:**
> **username_0**: Ah, I see now.  Also, dec.data[0] is a byte's worth of data (two hex chars), which can hold 256 different states.  

---

### Answer by username_0 on 2019-01-09. Score: 1

From [BIP173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Segwit_address_format) > Specification > Segwit address format > Decoding:

> Software interpreting a segwit address:
>
> * MUST verify that the human-readable part is "bc" for mainnet and "tb" for testnet.
> * MUST verify that the first decoded data value (the witness version) is between 0 and 16, inclusive


---

