2

How can i convert an address of the form "5DTNtGF2DmZ7QwGxg6n1xWzF6cEokNyNre7mUqpVuozrLxu3" to hex using polkadotjs packages?

Here is the context:

I have multiples array of datas, containing address: balance. I have to make interactions between these data (like searching the balance of the specific address). The problem is that, between array, the format is different (addresses start by 3, sometime by 5, sometimes are in hex...) so i cannot compare anything as the format is different.

So i'd like to convert everything back to a hex account id, where everything will correspond.

I'v found a way to change the prefix, decode into array of 32 int, but i couldnt find a way to convert to hex...

3 Answers 3

10

You have a couple of options.

First, if you already have an initialized API in your app -

const ADDR = "5DTNtGF2DmZ7QwGxg6n1xWzF6cEokNyNre7mUqpVuozrLxu3"

// create a type from this
const id = api.registry.createType("AccountId", ADDR)

// convert to hex
const hex = id.toHex()

// alternatively, you can just compare directly to 
// something else that can be an ss58 address, hex
// or even Uint8Array (raw decoded bytes are compared)
const isEqual = id.eq("SomethingElse")

Your second option is via the decodeAddress -

import { u8aToHex } from "@polkadot/util"
import { decodeAddress } from "@polkadot/util-crypto"

const ADDR = "5DTNtGF2DmZ7QwGxg6n1xWzF6cEokNyNre7mUqpVuozrLxu3"

// convert to hex
const hex = u8aToHex(decodeAddress(ADDR))

// test for equality
const isEqual = hex === "0xdeadbeef"

Option 3 - if the array is from the API, you can use the first option and just use the .eq directly on it (without creating)

3
  • Thanks a lot ! And by curiosity, did you find this on documentation ?
    – Nathan
    Jun 29, 2022 at 14:53
  • 1
    For the first option - all Codec objects have .toHex() and .eq() on them that will convert and compare, so that is standard to all API-returned objects. For the second option, it is just a variation of polkadot.js.org/docs/util-crypto/examples/validate-address
    – Luna
    Jun 29, 2022 at 14:55
  • ooooh ok, thanks again !
    – Nathan
    Jun 29, 2022 at 15:33
1

There is also the /accounts/{address}/validate endpoint in Sidecar which takes a ss58 or hex (from a u8 array) formatted address and returns the following information :

  • isValid
  • ss58Prefix
  • network
  • accountId

You can find more info regarding this endpoint :

  • in Sidecar docs
  • in the latest PR where the accountId was added
  • in the corresponding function validateAddress in sidecar code (src/services/accounts/AccountsValidateService.ts)
0

https://www.shawntabrizi.com/substrate-js-utilities/

Source: https://github.com/shawntabrizi/substrate-js-utilities

import { u8aToHex } from "@polkadot/util";

/* AccountId to Hex */
let a2h = {
    "account": document.getElementById("account-a2h"),
    "hex": document.getElementById("hex-a2h")
};

a2h.account.addEventListener("input", account2hex);
a2h.hex.addEventListener("input", hex2account);

function account2hex() {
    try {
        a2h.hex.value = u8aToHex(decodeAddress(a2h.account.value));
    } catch (e) {
        a2h.hex.value = "Error";
        console.error(e);
    }
}

function hex2account() {
    try {
        a2h.account.value = encodeAddress(a2h.hex.value);
    } catch (e) {
        a2h.account.value = "Error";
        console.error(e);
    }
}
2
  • See this issue for more formal specs to be re-added to some docs soon github.com/paritytech/ss58-registry/issues/112
    – Nuke
    Jun 29, 2022 at 14:00
  • As i said in my question, i'll need a solution from polkadotjs package. I imagine that u8aToHex comes from polkadotjs, if yes, it was exactly the function i was searching for. But from which package ? api ? util-crypto ?
    – Nathan
    Jun 29, 2022 at 14:15

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.