user2259: 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).

user2402: 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.

user2259: 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)

2259: <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>
 2402: <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>
 2259: <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>
