Class: Eth::Ens::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/eth/ens/resolver.rb

Overview

Utility class for resolving ENS names to Ethereum addresses

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, address = DEFAULT_ADDRESS) ⇒ Resolver

Create an instance of the ENS Resolver.

Parameters:

  • client (Eth::Client)

    The client instance used to resolve the ENS.

  • address (String) (defaults to: DEFAULT_ADDRESS)

    The address of the ENS registry on the given chain.



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

#clientObject

The client instance used to resolve the ENS.



28
29
30
# File 'lib/eth/ens/resolver.rb', line 28

def client
  @client
end

#registryObject

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

Parameters:

  • ens_name (String)

    The ENS name, e.g., fancy.eth.

Returns:

  • (String)

    The node as a hex string.



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/

Parameters:

  • input (String)

    The input string

Returns:

  • (String)

    The normalized output string



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.

Parameters:

  • ens_name (String)

    The ENS name, e.g., fancy.eth.

Returns:

  • (String)

    The owner address of the name as a hex string.



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.

Parameters:

  • ens_name (String)

    The ENS name, e.g., fancy.eth.

Returns:

  • (String)

    The owner address of the name as a hex string.



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.

Parameters:

  • ens_name (String)

    The ENS name, e.g., fancy.eth.

Returns:

  • (Eth::Contract)

    The public resolver contract that can be used to resolve ENS names.



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.

Parameters:

  • ens_name (String)

    The ENS name, e.g., fancy.eth.

  • key (String) (defaults to: "description")

    The key for the text record, e.g., url.

Returns:

  • (String)

    The text record.



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