Class: Eth::Ens::Resolver
- Inherits:
-
Object
- Object
- Eth::Ens::Resolver
- Defined in:
- lib/eth/ens/resolver.rb
Overview
Utility class for resolving ENS names to Ethereum addresses
Instance Attribute Summary collapse
-
#client ⇒ Object
The client instance used to resolve the ENS.
-
#registry ⇒ Object
The address of the ENS registry on the given chain.
Instance Method Summary collapse
-
#initialize(client, address = DEFAULT_ADDRESS) ⇒ Resolver
constructor
Create an instance of the ENS Resolver.
-
#namehash(ens_name) ⇒ String
Generate node for the given domain name See: docs.ens.domains/contract-api-reference/name-processing.
-
#normalize(input) ⇒ String
Normalize a string as specified by unicode.org/reports/tr46/.
-
#owner(ens_name) ⇒ String
Resolve an ENS name owner.
-
#resolve(ens_name, coin_type = Ens::CoinType::ETHEREUM) ⇒ String
Resolve an ENS name to an address.
-
#resolver(ens_name) ⇒ Eth::Contract
Retrieve the public resolver for the given ENS name.
-
#text(ens_name, key = "description") ⇒ String
Resolve a text record for a given ENS name.
Constructor Details
#initialize(client, address = DEFAULT_ADDRESS) ⇒ Resolver
Create an instance of the ENS Resolver.
37 38 39 40 41 42 43 44 |
# File 'lib/eth/ens/resolver.rb', line 37 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 |
Instance Attribute Details
#client ⇒ Object
The client instance used to resolve the ENS.
28 29 30 |
# File 'lib/eth/ens/resolver.rb', line 28 def client @client end |
#registry ⇒ Object
The address of the ENS registry on the given chain.
31 32 33 |
# File 'lib/eth/ens/resolver.rb', line 31 def registry @registry end |
Instance Method Details
#namehash(ens_name) ⇒ String
Generate node for the given domain name See: docs.ens.domains/contract-api-reference/name-processing
97 98 99 100 101 102 103 104 105 |
# File 'lib/eth/ens/resolver.rb', line 97 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 |
#normalize(input) ⇒ String
Normalize a string as specified by unicode.org/reports/tr46/
111 112 113 114 115 116 117 118 |
# File 'lib/eth/ens/resolver.rb', line 111 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 |
#owner(ens_name) ⇒ String
Resolve an ENS name owner.
50 51 52 |
# File 'lib/eth/ens/resolver.rb', line 50 def owner(ens_name) @client.call(@registry, "owner", namehash(ens_name)) end |
#resolve(ens_name, coin_type = Ens::CoinType::ETHEREUM) ⇒ String
Resolve an ENS name to an address.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eth/ens/resolver.rb', line 72 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 |
#resolver(ens_name) ⇒ Eth::Contract
Retrieve the public resolver for the given ENS name.
59 60 61 62 63 64 65 66 |
# File 'lib/eth/ens/resolver.rb', line 59 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 |
#text(ens_name, key = "description") ⇒ String
Resolve a text record for a given ENS name.
88 89 90 |
# File 'lib/eth/ens/resolver.rb', line 88 def text(ens_name, key = "description") @client.call(resolver(ens_name), "text", namehash(ens_name), key) end |