Check if an address is a smart contract address

27 views Asked by At

Is there a way to check if an address is a smart contract address?

The smart contract addresses all have a sequence like q...q (E.g.: erd1qqqqqqqqqqqqqpgqhe8t5jewej70zupmh44jurgn29psua5l2jps3ntjj3), but is there something more formal to check this?

1

There are 1 answers

0
ROMANIA_engineer On

If you have the bech32 representation of an address you can convert it to hexadecimal representation and check if starts with 16 of 0.

E.g.:

Using https://utils.multiversx.com/converters , this value:

erd1qqqqqqqqqqqqqpgqhe8t5jewej70zupmh44jurgn29psua5l2jps3ntjj3

can be converted to

00000000000000000500be4eba4b2eccbcf1703bbd6b2e0d1351430e769f5483

Or programmatically:

https://github.com/multiversx/mx-sdk-js-core/blob/e059d13fe4e3789af7cc457cc448b86235faaca9/src/address.ts#L202

isContractAddress(): boolean {
    return this.hex().startsWith(SMART_CONTRACT_HEX_PUBKEY_PREFIX);
}

where the constant is

const SMART_CONTRACT_HEX_PUBKEY_PREFIX = "0".repeat(16);