<p>Credit to someone on Reddit who helped me with this:</p>
<p>Deriving the staking address requires you to manually update the header byte before bech32 encoding the hex account. All I needed to do was add <code>e1</code> to the start of the account (<code>1110 0001</code>)</p>

CC BY-SA 4.0
2021-07-18T01:26:06.423Z
2021-07-18T01:26:06.423Z
ImminentFate
<p>Maybe it is a bit late, but hope it helps.</p>
<p>You can use this python library <a href="https://github.com/cffls/pycardano" rel="nofollow noreferrer">PyCardano</a> to derive the stake key.</p>
<p>Here is the code example:</p>
<pre><code>&gt;&gt;&gt; from pycardano import Address, Network

&gt;&gt;&gt; addr = Address.from_primitive(&quot;addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu&quot;)
&gt;&gt;&gt; addr2 = Address(staking_part=addr.staking_part, network=Network.MAINNET)
&gt;&gt;&gt; addr2
stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq
</code></pre>
<p>We first convert the address string to an <code>Address</code> object, <code>addr</code>. Then we use the staking part of the address to create a new stake address.</p>

CC BY-SA 4.0
2022-04-27T21:08:40.257Z
2022-04-27T21:08:40.257Z
Jerry
<p>I'm trying to get a stake address from a Shelley address in Python using this library: <a href="https://pypi.org/project/bech32/" rel="noreferrer">https://pypi.org/project/bech32/</a></p>
<p>I can convert an address from bech32 to hex without issue, but I'm getting stuck grabbing the staking part (final 56 bytes) and converting that to Bech32. I keep getting a staking address that doesn't actually exist:</p>
<pre><code>
address_words = bech32.bech32_decode(&quot;addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu&quot;)

address_hexlist = bech32.convertbits(address_words[1], 5, 16)

account = ''.join([f'{c:04x}' for c in address_hexlist])[-58:-2]

btr = bytes.fromhex(account)

stake_words = bech32.convertbits(btr, 8, 5)

stakeaddr = bech32.bech32_encode(&quot;addr&quot;, stake_words)
print(stakeaddr)
</code></pre>
<p>Using the above, I get the correct account = <code>883c4532b6ac18d62814ffc7e9ca035b67a68da0ea298cc24514237f</code></p>
<p>However, I get the wrong staking address at the end: <code>stake13q7y2v4k4svdv2q5llr7njsrtdn6drdqag5cesj9zs3h7xjwn42</code></p>
<p>The correct staking address should be: <code>stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq</code></p>
<p>The annoying thing is, the wrong address I get is the same as what you get trying to use <a href="https://slowli.github.io/bech32-buffer/" rel="noreferrer">https://slowli.github.io/bech32-buffer/</a> which makes me think I'm missing something specific that cardano does differently to the bech32 spec.</p>
<p>I can't use the provided <code>bech32.exe</code> binary because it refuses to work (even if it did work, I'd still like to get it working in python).</p>

CC BY-SA 4.0
2021-07-17T11:35:25.637Z
Great first post, welcome aboard.
gRebel
<address><staking><python><bech32>
How do I properly derive a stake key from an address using Bech32?
# How do I properly derive a stake key from an address using Bech32?

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

I'm trying to get a stake address from a Shelley address in Python using this library: https://pypi.org/project/bech32/
I can convert an address from bech32 to hex without issue, but I'm getting stuck grabbing the staking part (final 56 bytes) and converting that to Bech32. I keep getting a staking address that doesn't actually exist:

address_words = bech32.bech32_decode("addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu")

address_hexlist = bech32.convertbits(address_words[1], 5, 16)

account = ''.join([f'{c:04x}' for c in address_hexlist])[-58:-2]

btr = bytes.fromhex(account)

stake_words = bech32.convertbits(btr, 8, 5)

stakeaddr = bech32.bech32_encode("addr", stake_words)
print(stakeaddr)

Using the above, I get the correct account = 883c4532b6ac18d62814ffc7e9ca035b67a68da0ea298cc24514237f
However, I get the wrong staking address at the end: stake13q7y2v4k4svdv2q5llr7njsrtdn6drdqag5cesj9zs3h7xjwn42
The correct staking address should be: stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq
The annoying thing is, the wrong address I get is the same as what you get trying to use https://slowli.github.io/bech32-buffer/ which makes me think I'm missing something specific that cardano does differently to the bech32 spec.
I can't use the provided bech32.exe binary because it refuses to work (even if it did work, I'd still like to get it working in python).

**Tags:** address, staking, python, bech32
> **Comments:**
> **username_1**: Great first post, welcome aboard.  

---

## 2 answers

### Answer by username_0 on 2021-07-18. Score: 5

Credit to someone on Reddit who helped me with this:

Deriving the staking address requires you to manually update the header byte before bech32 encoding the hex account. All I needed to do was add `e1` to the start of the account (`1110 0001`)


---

### Answer by username_2 on 2022-04-27. Score: 1

Maybe it is a bit late, but hope it helps.
You can use this python library PyCardano to derive the stake key.
Here is the code example:
>>> from pycardano import Address, Network

>>> addr = Address.from_primitive("addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu")
>>> addr2 = Address(staking_part=addr.staking_part, network=Network.MAINNET)
>>> addr2
stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq

We first convert the address string to an Address object, addr. Then we use the staking part of the address to create a new stake address.


---

