4

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.

1 Answer 1

7

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

1
  • Yes, this was the issue. Accepted as the answer - thanks!
    – Bob T
    Commented Oct 13, 2021 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.