Search code examples
substrate

How can I get SS58 address from Account ID?


How can I get SS58 address from Account ID? Account ID: 0x6d6f646c63622f62726964670000000000000000000000000000000000000000 ss58 address: ?


Solution

  • https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)

    You can use the JS library to do this simply: https://substrate.dev/docs/en/knowledgebase/advanced/ss58-address-format#polkadotjs

    Here is a set of tools that use this library: https://www.shawntabrizi.com/substrate-js-utilities/

    // Import Polkadot.js API dependencies.
    const { decodeAddress, encodeAddress } = require("@polkadot/keyring");
    const { hexToU8a, isHex } = require("@polkadot/util");
    
    // Specify an address to test.
    const address = "<addressToTest>";
    
    // Check address.
    const isValidSubstrateAddress = () => {
      try {
        encodeAddress(isHex(address) ? hexToU8a(address) : decodeAddress(address));
    
        return true;
      } catch (error) {
        return false;
      }
    };
    
    // Query result.
    const isValid = isValidSubstrateAddress();
    console.log(isValid);