Module: Hop::Discovery

Defined in:
lib/hop/discovery.rb

Constant Summary collapse

WELL_KNOWN_PATH =
"/.well-known/hop"

Class Method Summary collapse

Class Method Details

.resolve(base_url, insecure_tls: false) ⇒ Object

Fetch + verify base_url's well-known. Returns address_bytes:, wss_url:. Raises on a missing/malformed/unverified record.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/hop/discovery.rb', line 23

def self.resolve(base_url, insecure_tls: false)
  uri = URI.parse(base_url)
  http = Net::HTTP.new(uri.host, uri.port || 443)
  http.use_ssl = true
  http.verify_mode = insecure_tls ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
  res = http.get(WELL_KNOWN_PATH)
  raise "well-known fetch failed: HTTP #{res.code}" unless res.code.to_i == 200

  body = JSON.parse(res.body)
  info = Hop::FFI.verify_reach(Base64.strict_decode64(body["reach"]), Time.now.to_i)
  raise "reach record failed verification (bad signature or expired)" unless info

  { address: Hop::FFI.to_b58(info[:address]), address_bytes: info[:address], wss_url: info[:endpoint] }
end

.well_known_body(endpoint, public_url, ttl_secs = 3600) ⇒ Object



16
17
18
19
# File 'lib/hop/discovery.rb', line 16

def self.well_known_body(endpoint, public_url, ttl_secs = 3600)
  record = endpoint.sign_reach(public_url, ttl_secs)
  JSON.generate({ "address" => endpoint.address, "endpoint" => public_url, "reach" => Base64.strict_encode64(record) })
end