Search code examples
cryptographyblockchainsubstratepolkadotpolkadot-js

How to get the Polkadot address from the polkadot public key? Polkadot.js


I tried @polkadot/util-crypto lib and @polkadot/keyring to convert, public key to polkadot address but no help.

Is there any method provided by the polkadot.js?

Steps on how to get the address will also work great.

Thank you


Solution

  • @polkadot/util-crypto exposes the function encodeAddress which can input your public key bytes/hex and output an SS58 encoded address. If you want to get the address for a specific network, like Polkadot, you need to provide a secondary parameter which is the SS58 Prefix for that network. In the case of Polkadot it is 0.

    https://docs.substrate.io/reference/address-formats/#address-type

    For example:

    let address = util_crypto.encodeAddress("0x263158a10b39debac59bd1239bc64fb4bd678f507814d24f59efd46279111c71", 0)
    
    document.getElementById("output").innerText = address;
    <script src="//unpkg.com/polkadot-js-bundle/polkadot.js"></script>
    
    <div id="output"></output>