<p>The problem stems from <code>bech32</code> being a base5 encoding, which means that one character of address representation encodes only 5 bits of the actual address data.</p>
<p>The <code>bech32_decode</code> function from <a href="https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py" rel="nofollow noreferrer">Python Reference Implementation</a> leaves the decoded data as it is: every byte may have only 5 youngest bits used. Meanwhile the Cardano tools use converted notation, where bits are shifted left and &quot;packed&quot;.</p>
<p>In the PRI you may add another step to get the packed data into <code>payload</code> var:</p>
<pre><code>hrp, _payload32 = bech32_decode(address)
payload = convertbits(_payload32, 5, 8, False)
</code></pre>

CC BY-SA 4.0
2021-11-11T20:40:56.947Z
2021-11-11T20:40:56.947Z
emesik
<p>I would use the bech32 decoder tool available here:
<a href="https://github.com/input-output-hk/bech32" rel="nofollow noreferrer">https://github.com/input-output-hk/bech32</a></p>
<p>You can also find it in Daedalus in the binaries folder for your platform.</p>
<pre><code>$ bech32 &lt;&lt;&lt; addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx
71c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f
</code></pre>
<p>This outputs the address in a hex format which is easier to understand as each 2 hex characters are one byte.</p>
<p>7# at the start means that it's a smart contract address, I believe. The 1 in 71 means a mainnet address. 70 would be a testnet address.</p>
<p>More info about addresses can be found here:
<a href="https://github.com/cardano-foundation/CIPs/blob/master/CIP-0019/CIP-0019.md" rel="nofollow noreferrer">https://github.com/cardano-foundation/CIPs/blob/master/CIP-0019/CIP-0019.md</a></p>

CC BY-SA 4.0
2021-07-02T09:38:04.430Z
OK, it seems the bech32 implementations differ. Thank you for pointing out the IOHK source. Somehow I missed it.
emesik
CC BY-SA 4.0
2021-07-01T20:38:47.163Z
2021-07-01T20:38:47.163Z
Andrew Westberg - BCSH
<p>I'm trying to write address validation in Python. According to <a href="https://github.com/cardano-foundation/CIPs/pull/78" rel="nofollow noreferrer">the docs</a>, Shelley addresses are encoded with bech32 and the first byte of the payload consists of <code>header_type</code> (4 bits) and <code>network_tag</code> (4 bits).</p>
<p>So, let's consider the following address:
<code>addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx</code></p>
<pre><code>In [2]: bech32.bech32_decode(&quot;addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx&quot;)
Out[2]: 
('addr',
 [14,
  7,
  1,
  23,
  22,
  6,
[...]
</code></pre>
<p>The first byte of the payload is 14, which would mean <code>network_tag = 14</code> and <code>header_type = 0</code>. However, (a) the HRP doesn't contain the <code>_test</code> suffix and (b) <code>cardano-address</code> reports different network tag than my result does:</p>
<pre><code>$ echo addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx | cardano-address address inspect
{
    &quot;spending_shared_hash&quot;: &quot;c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f&quot;,
    &quot;stake_shared_hash_bech32&quot;: &quot;stake_shared_vkh1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z7p6v69g&quot;,
    &quot;address_style&quot;: &quot;Shelley&quot;,
    &quot;network_tag&quot;: 1,
    &quot;spending_shared_hash_bech32&quot;: &quot;addr_shared_vkh1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z70dxhec&quot;,
    &quot;stake_reference&quot;: &quot;none&quot;,
    &quot;stake_shared_hash&quot;: &quot;c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f&quot;
}
</code></pre>
<p>I guess I'm making some stupid mistake, but where?</p>

<address><cardano-address><bech32><python>
bech32 in Shelley addresses
# bech32 in Shelley addresses

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

I'm trying to write address validation in Python. According to the docs, Shelley addresses are encoded with bech32 and the first byte of the payload consists of header_type (4 bits) and network_tag (4 bits).
So, let's consider the following address:
addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx
In [2]: bech32.bech32_decode("addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx")
Out[2]: 
('addr',
 [14,
  7,
  1,
  23,
  22,
  6,
[...]

The first byte of the payload is 14, which would mean network_tag = 14 and header_type = 0. However, (a) the HRP doesn't contain the _test suffix and (b) cardano-address reports different network tag than my result does:
$ echo addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx | cardano-address address inspect
{
    "spending_shared_hash": "c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f",
    "stake_shared_hash_bech32": "stake_shared_vkh1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z7p6v69g",
    "address_style": "Shelley",
    "network_tag": 1,
    "spending_shared_hash_bech32": "addr_shared_vkh1cda3khwqv60360rp5m7akt50m6ttapacs8rqhn5w342z70dxhec",
    "stake_reference": "none",
    "stake_shared_hash": "c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f"
}

I guess I'm making some stupid mistake, but where?

**Tags:** address, cardano-address, bech32, python

---

## 2 answers

### Answer by username_0 on 2021-11-11. Score: 3 | Accepted answer

The problem stems from bech32 being a base5 encoding, which means that one character of address representation encodes only 5 bits of the actual address data.
The bech32_decode function from Python Reference Implementation leaves the decoded data as it is: every byte may have only 5 youngest bits used. Meanwhile the Cardano tools use converted notation, where bits are shifted left and "packed".
In the PRI you may add another step to get the packed data into payload var:
hrp, _payload32 = bech32_decode(address)
payload = convertbits(_payload32, 5, 8, False)


---

### Answer by username_1 on 2021-07-01. Score: 3

I would use the bech32 decoder tool available here:
https://github.com/input-output-hk/bech32
You can also find it in Daedalus in the binaries folder for your platform.
$ bech32 <<< addr1w8phkx6acpnf78fuvxn0mkew3l0fd058hzquvz7w36x4gtcyjy7wx
71c37b1b5dc0669f1d3c61a6fddb2e8fde96be87b881c60bce8e8d542f

This outputs the address in a hex format which is easier to understand as each 2 hex characters are one byte.
7# at the start means that it's a smart contract address, I believe. The 1 in 71 means a mainnet address. 70 would be a testnet address.
More info about addresses can be found here:
https://github.com/cardano-foundation/CIPs/blob/master/CIP-0019/CIP-0019.md

> **Comments:**
> **username_0**: OK, it seems the bech32 implementations differ. Thank you for pointing out the IOHK source. Somehow I missed it.  

---

