class Eth::Ens::Resolver
Utility class for resolving ENS names to Ethereum addresses
def initialize(client, address = DEFAULT_ADDRESS)
-
address
(String
) -- The address of the ENS registry on the given chain. -
client
(Eth::Client
) -- The client instance used to resolve the ENS.
def initialize(client, address = DEFAULT_ADDRESS) @client = client @registry = Eth::Contract.from_abi( name: "ENSRegistryWithFallback", address: address, abi: JSON.parse(File.read(File.join(File.dirname(__FILE__), "../../../abi/ens_registry.json"))), ) end
def namehash(ens_name)
-
(String)
- The node as a hex string.
Parameters:
-
ens_name
(String
) -- The ENS name, e.g., `fancy.eth`.
def namehash(ens_name) node = Util.hex_to_bin("0" * 64) name = normalize(ens_name) name.split(".").reverse.each do |label| hash = Util.keccak256(label) node = Util.keccak256(node + hash) end Util.bin_to_prefixed_hex node end
def normalize(input)
-
(String)
- The normalized output string
Parameters:
-
input
(String
) -- The input string
def normalize(input) name = input.dup if name.gsub!(/[`~!@#$%^&*()_=+\[\]{}<>,;:'"\/\\|?]/, "").nil? return input.downcase else raise ArgumentError, "Provided ENS name contains illegal characters: #{input}" end end
def owner(ens_name)
-
(String)
- The owner address of the name as a hex string.
Parameters:
-
ens_name
(String
) -- The ENS name, e.g., `fancy.eth`.
def owner(ens_name) @client.call(@registry, "owner", namehash(ens_name)) end
def resolve(ens_name, coin_type = Ens::CoinType::ETHEREUM)
-
(String)
- The owner address of the name as a hex string.
Parameters:
-
ens_name
(String
) -- The ENS name, e.g., `fancy.eth`.
def resolve(ens_name, coin_type = Ens::CoinType::ETHEREUM) if coin_type === Ens::CoinType::ETHEREUM return @client.call(resolver(ens_name), "addr", namehash(ens_name)) elsif coin_type === Ens::CoinType::ETHEREUM_CLASSIC data = @client.call(resolver(ens_name), "addr", namehash(ens_name), coin_type) return Util.bin_to_prefixed_hex data else raise NotImplementedError, "Coin type #{coin_type} not implemented!" end end
def resolver(ens_name)
-
(Eth::Contract)
- The public resolver contract that can be used
Parameters:
-
ens_name
(String
) -- The ENS name, e.g., `fancy.eth`.
def resolver(ens_name) address = @client.call(@registry, "resolver", namehash(ens_name)) Eth::Contract.from_abi( name: "ENSPublicResolver", address: address, abi: JSON.parse(File.read(File.join(File.dirname(__FILE__), "../../../abi/ens_resolver.json"))), ) end
def text(ens_name, key = "description")
-
(String)
- The text record.
Parameters:
-
key
(String
) -- The key for the text record, e.g., `url`. -
ens_name
(String
) -- The ENS name, e.g., `fancy.eth`.
def text(ens_name, key = "description") @client.call(resolver(ens_name), "text", namehash(ens_name), key) end