Class: Myrr::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/myrr/client.rb

Overview

Client for the Myrr protocol server API.

Wraps the register, challenge, verify, revoke, and lookup endpoints. Also provides generic post, get, and delete methods for resource clients.

Examples:

client = Myrr::Client.new(server_url: "http://localhost:8080", api_key: "sk-...")
result = client.register(public_key: hex_pub_key)
result = client.verify(did_key)

Instance Method Summary collapse

Constructor Details

#initialize(server_url:, api_key: nil, open_timeout: 5, read_timeout: 10) ⇒ Client

Returns a new instance of Client.

Parameters:

  • server_url (String)

    Base URL of the protocol server

  • api_key (String, nil) (defaults to: nil)

    API key for authenticated operations

  • open_timeout (Integer) (defaults to: 5)

    HTTP open timeout in seconds

  • read_timeout (Integer) (defaults to: 10)

    HTTP read timeout in seconds



20
21
22
23
24
25
# File 'lib/myrr/client.rb', line 20

def initialize(server_url:, api_key: nil, open_timeout: 5, read_timeout: 10)
  @server_url = server_url.chomp("/")
  @api_key = api_key
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#challenge(did_key) ⇒ String

Get a challenge nonce for the given identity.

Parameters:

  • did_key (String)

    The did:myrr: identifier

Returns:

  • (String)

    Challenge nonce

Raises:

  • (Myrr::Error)

    On server error or if identity not found



44
45
46
47
# File 'lib/myrr/client.rb', line 44

def challenge(did_key)
  resp = post("/v1/identities/challenge", { id: did_key })
  resp["challenge"]
end

#delete(path) ⇒ Hash

Send a DELETE request to the protocol server.

Parameters:

  • path (String)

    API path

Returns:

  • (Hash)

    Parsed JSON response



124
125
126
127
128
129
130
131
# File 'lib/myrr/client.rb', line 124

def delete(path)
  uri = URI("#{@server_url}#{path}")
  http = build_http(uri)
  request = Net::HTTP::Delete.new(uri)

  response = http.request(request)
  handle_response(response)
end

#get(path, **params) ⇒ Hash

Send a GET request to the protocol server.

Parameters:

  • path (String)

    API path (e.g., "/v1/wallets")

  • params (Hash)

    Query parameters

Returns:

  • (Hash)

    Parsed JSON response



109
110
111
112
113
114
115
116
117
118
# File 'lib/myrr/client.rb', line 109

def get(path, **params)
  uri = URI("#{@server_url}#{path}")
  uri.query = URI.encode_www_form(params) if params.any?

  http = build_http(uri)
  request = Net::HTTP::Get.new(uri)

  response = http.request(request)
  handle_response(response)
end

#lookup_by_owner(owner_type:, owner_value:) ⇒ Array<Hash>

Look up identities by owner.

Parameters:

  • owner_type (String)

    Owner type (e.g., "email")

  • owner_value (String)

    Owner value (e.g., "user@example.com")

Returns:

  • (Array<Hash>)

    List of matching identities



81
82
83
# File 'lib/myrr/client.rb', line 81

def lookup_by_owner(owner_type:, owner_value:)
  get("/v1/identities", owner_type: owner_type, owner_value: owner_value)
end

#post(path, body, authenticated: false) ⇒ Hash

Send a POST request to the protocol server.

Parameters:

  • path (String)

    API path (e.g., "/v1/wallets")

  • body (Hash)

    Request body

  • authenticated (Boolean) (defaults to: false)

    Whether to include the API key

Returns:

  • (Hash)

    Parsed JSON response



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/myrr/client.rb', line 91

def post(path, body, authenticated: false)
  uri = URI("#{@server_url}#{path}")
  http = build_http(uri)

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{@api_key}" if authenticated && @api_key
  request.body = JSON.generate(body)

  response = http.request(request)
  handle_response(response)
end

#register(public_key:, owner: nil) ⇒ Hash

Register a new agent identity.

Parameters:

  • public_key (String)

    Hex-encoded Ed25519 public key

  • owner (Hash, nil) (defaults to: nil)

    Optional owner info (+type+ and value)

Returns:

  • (Hash)

    Response with id, did_key, status, created_at

Raises:



33
34
35
36
37
# File 'lib/myrr/client.rb', line 33

def register(public_key:, owner: nil)
  body = { public_key: public_key }
  body[:owner] = owner if owner
  post("/v1/identities/register", body)
end

#revoke(did_key) ⇒ Hash

Revoke an agent identity.

Parameters:

  • did_key (String)

    The did:myrr: identifier

Returns:

  • (Hash)

    Response with status, did_key, revoked_at

Raises:



72
73
74
# File 'lib/myrr/client.rb', line 72

def revoke(did_key)
  post("/v1/identities/revoke", { id: did_key }, authenticated: true)
end

#verify(did_key) ⇒ Hash?

Verify an agent's identity. Performs a status check via the protocol server.

Returns a hash with did_key, status, and optional owner. Returns nil if the identity is not found.

Parameters:

  • did_key (String)

    The did:myrr: identifier

Returns:

  • (Hash, nil)

    Agent info or nil



56
57
58
59
60
61
62
63
64
65
# File 'lib/myrr/client.rb', line 56

def verify(did_key)
  challenge(did_key)
  { "did_key" => did_key, "status" => "active" }
rescue Error => e
  if e.message.include?("not_found") || e.message.include?("revoked")
    nil
  else
    raise
  end
end