Skip to content

Instantly share code, notes, and snippets.

@ilanolkies
Created September 27, 2019 19:08
Show Gist options
  • Save ilanolkies/1a5d88e7b843e2436e5dd41d2c735dbd to your computer and use it in GitHub Desktop.
Save ilanolkies/1a5d88e7b843e2436e5dd41d2c735dbd to your computer and use it in GitHub Desktop.
RNS Address resolver
const Contract = require('web3-eth-contract');
const namehash = require('eth-ens-namehash').hash;
// Install npm required packages (web3-eth-contract & eth-ens-namehash)
// Run it with node addr-resolver-cli.js name.rsk
// First, connect to RSK Public nodes
const rskNode = 'https://public-node.rsk.co';
Contract.setProvider(rskNode);
// Instance RNS Registry
const rnsAbi = [
{
"constant": true,
"inputs": [
{ "name": "node", "type": "bytes32" }
],
"name": "resolver",
"outputs": [
{ "name": "", "type": "address" }
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
const rnsAddress = '0xcb868aeabd31e2b66f74e9a55cf064abb31a4ad5';
const rns = new Contract(rnsAbi, rnsAddress);
let resolverAddress;
// Now we calculate the name's node
const node = namehash(process.argv[2])
// And get the associated resolver
rns.methods.resolver(node).call()
.then(_resolverAddress => {
if (_resolverAddress == '0x0000000000000000000000000000000000000000') {
console.error('This name has no resolver');
process.exit();
}
// Instance resolver contract as ERC-165
resolverAddress = _resolverAddress;
const erc165Abi = [
{
"constant": true,
"inputs": [
{ "name": "interfaceID", "type": "bytes4" }
],
"name": "supportsInterface",
"outputs": [
{ "name": "", "type": "bool" }
],
"payable": false,
"stateMutability": "pure",
"type": "function"
}
];
const resolver = new Contract(erc165Abi, resolverAddress);
return resolver;
})
.then(resolver => {
// Check if it supports addr resolution
const addrInterface = '0x3b3b57de';
return resolver.methods.supportsInterface(addrInterface).call();
})
.then(supportsAddr => {
if (!supportsAddr) {
console.error('Addr resolution not supported');
process.exit();
}
})
.then(() => {
// Instance an addr resolver
const addrResolverAbi = [
{
"constant": true,
"inputs": [
{ "name": "node", "type": "bytes32" }
],
"name": "addr",
"outputs": [
{ "name": "", "type": "address" }
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
const resolver = new Contract(addrResolverAbi, resolverAddress);
// Get the addr resolution
return resolver.methods.addr(node).call();
})
.then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment