How can I convert a Hedera Account ID Alias (hexadecimal) into a Hedera Account ID Alias (chacracters only)?

173 views Asked by At

I am wondering how I can convert an Account ID alias (hexadecimal) into an Account ID alias (characters only). For example, from the format 0.0.302a300506032b657003210030a028ee7fd716c438de818a8831ed2235d0f85e430ab036dbfac173eb50aef9 to the format 0.0.HIQQEXWKW53RKN4W6XXC4Q232SYNZ3SZANVZZSUME5B5PRGXL663UAQA. Is there any function in the Hedera SDK that can accomplish this?

2

There are 2 answers

0
bguiz On BEST ANSWER

You can retrieve this information using a Mirror Node API request: ​

    const accountInfoFetchUrl = `https://testnet.mirrornode.hedera.com/api/v1/accounts?account.publickey=${publicKey}&balance=false&limit=1&order=desc`;
    const accountInfoResponse = await fetch(accountInfoFetchUrl);
    const accountInfo = (await accountInfo.json).accounts[0];

​ The public key is passed in using the account.publickey query parameter.

The accountInfo objects contain the field that you are looking for (and several others).


Additional info

The "very long account ID" is actually a "long account ID", which is defined in HIP-32 - Auto Account Creation.

Note that this should not be confused with any of the acount alias formats defined in HIP-583 - Expand alias support in CryptoCreate & CryptoTransfer Transactions.

0
user21166322 On

Another way to do this, without invoking the Mirror Node APIs, is to perform the required IETF RFC 4648 base32url conversion locally. To do this, you will need the rfc4648 npm package. This process is described in HIP-32.

const publicKeyRawStr = publicKey.toStringRaw();
const protoBufPrefix = `1220`;
const bufferRaw = Buffer.from(`${protoBufPrefix}${publicKeyRawStr}`, 'hex');
const longAccountId = base32.stringify(bufferRaw, { pad: false });

console.log(`publicKeyRawStr: ${publicKeyRawStr}`);
console.log(`longAccountId: ${longAccountId}`);