6

I have a Public key (hex) and would like to convert it to an SS58 Address. What is the right function to use?

I found the docs here with an code-sniped: https://substrate-developer-hub.github.io/docs/en/knowledgebase/advanced/ss58-address-format

👇

Public key (hex): 0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a

SS58 Address: 12bzRJfh7arnnfPPUZHeJUaE62QLEwhK48QnH9LXeK2m1iZU

👇

// Import Polkadot.js API dependencies.
const { decodeAddress, encodeAddress } = require("@polkadot/keyring");
const { hexToU8a, isHex } = require("@polkadot/util");

isHex(address)
hexToU8a(address)
decodeAddress(address)
encodeAddress(address)

But these functions do not bring the right results..

Any suggestions?

3 Answers 3

10

All you need is to add prefix when using encodeAddress(address, prefix), as in the wiki for the Polkadot-js API (https://polkadot.js.org/docs/keyring/start/ss58/)

A snippet:

const { encodeAddress } = require("@polkadot/keyring");

const address = "0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a";

console.log(encodeAddress(address, 0)); //"12bzRJfh7arnnfPPUZHeJUaE62QLEwhK48QnH9LXeK2m1iZU"

Note: prefix 0 is for Polkadot address

4

You can simply use subkey inspect:

subkey inspect --keystore-uri 0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a --network polkadot
URI: 
Secret Key URI `` is account:
  Secret seed:       0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e
  Public key (hex):  0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a
  Public key (SS58): 12bzRJfh7arnnfPPUZHeJUaE62QLEwhK48QnH9LXeK2m1iZU
  Account ID:        0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a
  SS58 Address:      12bzRJfh7arnnfPPUZHeJUaE62QLEwhK48QnH9LXeK2m1iZU
1

subscan provide convert tool here

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.