Join the Rootstock Discord Community to get the latest updates from the Rootstock Ecosystem!

RNS JS Library - Utils

Available methods

isValidDomain

Validates the given domain syntax. It allows dots and alphanumeric lowercase characters.

Signature

isValidDomain(domain:string): boolean;

Parameters

  • domain: Domain to be validated.

Returns

  • bool: true if valid, false if not

Examples

Valid domain:

rns.utils.isValidDomain('testing123.rsk').then(console.log)

Invalid domain:

rns.utils.isValidDomain('nOtVali-d.rsk').then(console.log)

isValidLabel

Validates the given label syntax. It allows alphanumeric lowercase characters.

Signature

isValidLabel(label:string): boolean;

Parameters

  • label: Label to be validated.

Returns

  • bool: true if valid, false if not

Examples

Valid label:

rns.utils.isValidLabel('testing123').then(console.log)

Invalid label:

rns.utils.isValidLabel('no%tvAl1d').then(console.log)

isValidTld

Validates the given top level domain. For now, the only valid TLD is rsk

Signature

isValidTld(tld:string): boolean;

Parameters

  • tld: TLD to be validated.

Returns

  • bool: true if valid, false if not

Examples

Valid TLD:

rns.utils.isValidTld('rsk').then(console.log)

Invalid TLD:

rns.utils.isValidTld('notvalid').then(console.log)

namehash

Returns the namehash representation of the given domain

Signature

namehash(domain:string): boolean;

Parameters

  • domain: Domain to be converted.

Returns

  • string: Namehash representation of the given domain

Examples

rns.utils.namehash('testing.rsk').then(console.log)

labelhash

Returns 0x + the keccak256 representation of the given label.

Signature

labelhash(label:string): boolean;

Parameters

  • label: Label to be converted.

Returns

  • string: Labelhash representation of the given domain

Examples

rns.utils.labelhash('testing').then(console.log)

hasAccounts

Checks if the given web3 instance has accounts to sign transactions

Signature

hasAccounts(web3:Web3): boolean;

Parameters

  • web3: Web3 instance.

Returns

  • bool: true if has accounts, false if not

Examples

const web3 = new Web3('https://public-node.rsk.co');
rns.utils.hasAccounts(web3).then(console.log)

hasMethod

Checks if the contract in the given address has the given method

Signature

hasMethod(web3:Web3, contractAddress:string, methodId: string): boolean;

Parameters

  • web3: Web3 instance.
  • contractAddress: address of the contract to check.
  • methodId: the first 4 bytes of the keccak256 hash of the ASCII form of the signature

How to obtain the methodId?

Given the following Solidity function

function max(uint256 a, uint256 b) internal pure returns (uint256) { return a >= b ? a : b; }

> The signature is `max(uint256,uint256)`.
>
> The methodId is calculated with this Javascript function:
>
>```javascript
function getMethodId(signature) {
  const hash = keccak256(signature);
  return `0x${hash.substring(0, 8)}`;
}

Returns

  • bool: true if has method, false if not

Examples

const web3 = new Web3('https://public-node.rsk.co');
const address = '0x0000000000000000000000000000000000000001';
const methodHash = '0x01ffc9a7';
rns.utils.hasMethod(web3, contractAddress, methodHash).then(console.log)

Receive updates

Get the latest updates from the Rootstock ecosystem

Loading...