solady icon indicating copy to clipboard operation
solady copied to clipboard

Idea: Add `ENS` resolution and reverse resolution

Open RogerPodacter opened this issue 3 years ago • 3 comments

Ok don't kill me for this, but there is no good copy-paste!

If you don't believe me, just ask ENS:

Solidity libraries for on-chain resolution are not yet available, but ENS resolution is straightforward enough it can be done trivially without a library.

Then they go on to offer this snippet:

abstract contract ENS {
    function resolver(bytes32 node) public virtual view returns (Resolver);
}

abstract contract Resolver {
    function addr(bytes32 node) public virtual view returns (address);
}

contract MyContract {
    // Same address for Mainet, Ropsten, Rinkerby, Gorli and other networks;
    ENS ens = ENS(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e);

    function resolve(bytes32 node) public view returns(address) {
        Resolver resolver = ens.resolver(node);
        return resolver.addr(node);
    }
}

And say:

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.

More "chain shame"! Why can't I resolve a string ENS name to an address on chain like a human being?

And with REVERSE resolution, ie going from address => .eth name, I haven't been able to find a canonical method anywhere! I use this but I'm honestly not sure where it comes from:

interface DefaultReverseResolver {
  function name(bytes32 node) external view returns (string memory);
}

interface ENS {
  function resolver(bytes32 node) external view returns (address);
}

interface ReverseRegistrar {
  function node(address addr) external pure returns (bytes32);
}

ENS public ens = ENS(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e);
ReverseRegistrar public reverseResolver = ReverseRegistrar(0x084b1c3C81545d370f3634392De611CaaBFf8148);

function addressToEnsName(address addr) public view returns (string memory) {
    bytes32 node = reverseResolver.node(addr);
    address resolvedAddr = ens.resolver(node);
    
    if (resolvedAddr == address(0)) return addr.toHexString();
    
    return DefaultReverseResolver(resolvedAddr).name(node);
}

Anyway, just an idea! Don't kill me!

RogerPodacter avatar Nov 05 '22 21:11 RogerPodacter

Re-upping this as my above code is not correct in that, when you do a reverse resolution, there is no automatic validation that the resolved name actually points to the address in question. This makes the code even more nightmarish:

function addressToEthName(address addr) public view returns (string memory) {
    bytes32 node = DefaultReverseResolver.node(addr);
    address resolverAddr = Ens.resolver(node);
    
    if (resolverAddr == address(0)) return '';
    
    string memory name = ForwardResolver(resolverAddr).name(node);
    
    bytes32 tldNode = keccak256(abi.encodePacked(bytes32(0), keccak256(bytes("eth"))));
    
    bytes32 forwardNode = keccak256(abi.encodePacked(tldNode, keccak256(bytes(name.split(".")[0]));
    
    address forwardResolver = ethEns.resolver(forwardNode);
    
    if (forwardResolver == address(0)) return '';
    
    address resolved = ForwardResolver(forwardResolver).addr(forwardNode);
    
    return resolved == addr ? name : '';
}

IMO this is completely inhumane for the most iconic ENS use-case: mapping address => vectorized.eth! "Chain shamers" will say this shouldn't happen on chain, but come on.

Solady needn't hardcode any ENS addresses as ENS is a standard anyway and there might be competing ENS-compatible services that spring up that this could help as well.

Is it time to live like human beings?

RogerPodacter avatar Nov 21 '22 15:11 RogerPodacter

I think this requires a new repo.

Might consider if I have surplus time and energy.

Solady tries to be chain agnostic and project agnostic as much as possible.

Vectorized avatar Nov 21 '22 15:11 Vectorized

That's fair. It is an EVM-wide thing but it's a pretty specific project, even if it is arguably the most iconic. If only there were an organization with millions of dollars who could invest in something like this hmmm... ENS DAO!

RogerPodacter avatar Nov 22 '22 23:11 RogerPodacter