Resolving Names On-chain
Solidity libraries for on-chain resolution are not yet available, but ENS resolution is straightforward enough it can be done trivially without a library. First, we define some pared-down interfaces containing only the methods we need:
1
abstract contract ENS {
2
function resolver(bytes32 node) public virtual view returns (Resolver);
3
}
4
5
abstract contract Resolver {
6
function addr(bytes32 node) public virtual view returns (address);
7
}
Copied!
For resolution, only the resolver function in the ENS contract is required; other methods permit looking up owners and updating ENS from within a contract that owns a name.
With these definitions, looking up a name given its node hash is straightforward:
1
contract MyContract {
2
// Same address for Mainet, Ropsten, Rinkerby, Gorli and other networks;
3
ENS ens = ENS(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e);
4
5
function resolve(bytes32 node) public view returns(address) {
6
Resolver resolver = ens.resolver(node);
7
return resolver.addr(node);
8
}
9
}
Copied!
While it is possible for a contract to process a human-readable name into a node hash, we highly recommend working with node hashes instead, as they are easier and more efficient to work with, and allow contracts to leave the complex work of normalizing the name to their callers outside the blockchain. Where a contract always resolves the same names, those names may be converted to a node hash and stored in the contract as a constant.
Last modified 1mo ago
Copy link
Edit on GitHub