Class: Stellar::Federation

Inherits:
Object
  • Object
show all
Defined in:
lib/stellar/federation.rb

Class Method Summary collapse

Class Method Details

.lookup(federated_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stellar/federation.rb', line 8

def self.lookup(federated_name)
  _, domain = federated_name.split("*")
  if domain.nil?
    raise InvalidFederationAddress.new
  end

  domain_req = Faraday.new("https://#{domain}/.well-known/stellar.toml").get

  unless domain_req.status == 200
    raise InvalidStellarDomain, "Domain does not contain stellar.toml file"
  end

  fed_server_url = Tomlrb.parse(domain_req.body)["FEDERATION_SERVER"]
  if fed_server_url.nil?
    raise InvalidStellarTOML, "Invalid Stellar TOML file"
  end

  unless fed_server_url&.match?(URI::DEFAULT_PARSER.make_regexp)
    raise InvalidFederationURL, "Invalid Federation Server URL"
  end

  lookup_req = Faraday.new(fed_server_url).get { |req|
    req.params[:q] = federated_name
    req.params[:type] = "name"
  }

  unless lookup_req.status == 200
    raise AccountNotFound, "Account not found"
  end

  JSON.parse(lookup_req.body)["account_id"]
end