user4335: I am trying to extract the bech32 stake address from a Shelly address using Javascript. I followed the example steps outlined in the answer another question (thanks for the help!) and put together the following code sample. The interim values are correct until the final step (bech32 encoding the stake portion of the hex-encoded address):
import { bech32 } from 'bech32';

// input Shelley address
const address = 'addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu';

// decode bech32 shelley address and convert to hex 
const addressWords = bech32.decode(address, 1000);
const payload = bech32.fromWords(addressWords.words);
const addressDecoded = `${Buffer.from(payload).toString('hex')}`;
// ✅ should be 019acc41d37e4cec299248f5cd27409b537dd8b53486ba2c4b0e7e4b54883c4532b6ac18d62814ffc7e9ca035b67a68da0ea298cc24514237f

// stake part of the address is the last 56 bytes
const stakeAddressDecoded = addressDecoded.substr(addressDecoded.length - 56);
// ✅ should be 883c4532b6ac18d62814ffc7e9ca035b67a68da0ea298cc24514237f

// convert to bech32 to get stake address
const stakeAddress = bech32.encode(
  'stake',
  bech32.toWords(Uint8Array.from(Buffer.from(stakeAddressDecoded, 'hex'))),
  1000
);
// should be  : stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq
// ❌ returns : stake13q7y2v4k4svdv2q5llr7njsrtdn6drdqag5cesj9zs3h7xjwn42

Am I missing a step before bech32 encoding the stake portion of the address? Thanks in advance.

user46: You're missing the prefix, i.e. e1 in this case.

const stakeAddressDecoded = 'e1'+addressDecoded.substr(addressDecoded.length - 56);


You have to add e1 (or e0 in case of testnet) as prefix.
Check page 113-114 for more details on prefix specifications: https://hydra.iohk.io/build/7918420/download/1/ledger-spec.pdf

4335: <p>I am trying to extract the bech32 stake address from a Shelly address using Javascript. I followed the <a href="https://cardano.stackexchange.com/a/2004/4335">example steps outlined in the answer another question</a> (thanks for the help!) and put together the following code sample. The interim values are correct until the final step (bech32 encoding the stake portion of the hex-encoded address):</p>
<pre><code>import { bech32 } from 'bech32';

// input Shelley address
const address = 'addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu';

// decode bech32 shelley address and convert to hex 
const addressWords = bech32.decode(address, 1000);
const payload = bech32.fromWords(addressWords.words);
const addressDecoded = `${Buffer.from(payload).toString('hex')}`;
// ✅ should be 019acc41d37e4cec299248f5cd27409b537dd8b53486ba2c4b0e7e4b54883c4532b6ac18d62814ffc7e9ca035b67a68da0ea298cc24514237f

// stake part of the address is the last 56 bytes
const stakeAddressDecoded = addressDecoded.substr(addressDecoded.length - 56);
// ✅ should be 883c4532b6ac18d62814ffc7e9ca035b67a68da0ea298cc24514237f

// convert to bech32 to get stake address
const stakeAddress = bech32.encode(
  'stake',
  bech32.toWords(Uint8Array.from(Buffer.from(stakeAddressDecoded, 'hex'))),
  1000
);
// should be  : stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq
// ❌ returns : stake13q7y2v4k4svdv2q5llr7njsrtdn6drdqag5cesj9zs3h7xjwn42
</code></pre>
<p>Am I missing a step before bech32 encoding the stake portion of the address? Thanks in advance.</p>
 46: <p>You're missing the prefix, i.e. <code>e1</code> in this case.</p>
<pre><code>
const stakeAddressDecoded = 'e1'+addressDecoded.substr(addressDecoded.length - 56);

</code></pre>
<p>You have to add <code>e1</code> (or <code>e0</code> in case of testnet) as prefix.</p>
<p>Check page 113-114 for more details on prefix specifications: <a href="https://hydra.iohk.io/build/7918420/download/1/ledger-spec.pdf" rel="noreferrer">https://hydra.iohk.io/build/7918420/download/1/ledger-spec.pdf</a></p>
