Convert cardano Bech32 address to stake key

Cardano payment addresses are bech32 encoded since Shelly. These addresses can be decoded in python

from typing import Optional
import bech32
import binascii


def resolve_addr2stake_py(address: str) -> Optional[str]:
    hrp, by = bech32.bech32_decode(address)
    if hrp != 'addr':
        return None
    words = bech32.convertbits(by, 5, 8, False)
    res = ''
    for w in words:
        res = f'{res}{format(w, "x").zfill(2)}'
    mainnet_addr = f'e1{res[-56:]}'
    array = binascii.unhexlify(mainnet_addr)
    words = [x for x in array]
    bech32_words = bech32.convertbits(words, 8, 5)
    bech32_addr = bech32.bech32_encode('stake', bech32_words)
    return bech32_addr

You can confirm this works by

address = 'addr1qxdvcswn0exwc2vjfr6u6f6qndfhmk94xjrt5tztpelyk4yg83zn9d4vrrtzs98lcl5u5q6mv7ngmg829xxvy3g5ydls7c76wu'

resolve_addr2stake_py(address)

Out: 'stake1uxyrc3fjk6kp343gznlu06w2qddk0f5d5r4znrxzg52zxlclk0hlq'


One thought on “Convert cardano Bech32 address to stake key”

Leave a Reply

Your email address will not be published. Required fields are marked *