Class: Privy::PrivyClient

Inherits:
Object
  • Object
show all
Defined in:
lib/privy/public_api/privy_client.rb

Constant Summary collapse

DEFAULT_REQUEST_EXPIRY_MS =

15 minutes. Used when a caller doesn’t pass ‘request_expiry:` and the client’s PrivyRequestExpiryOptions doesn’t override ‘default_ms`.

15 * 60 * 1_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id: ENV["PRIVY_APP_ID"], app_secret: ENV["PRIVY_APP_SECRET"], environment: nil, base_url: ENV["PRIVY_API_BASE_URL"], max_retries: Privy::Client::DEFAULT_MAX_RETRIES, timeout: Privy::Client::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: Privy::Client::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: Privy::Client::DEFAULT_MAX_RETRY_DELAY, request_expiry: Privy::PrivyRequestExpiryOptions.build, authorization_key_cache_max_capacity: Privy::JwtExchangeService::DEFAULT_CACHE_MAX_CAPACITY) ⇒ PrivyClient

Returns a new instance of PrivyClient.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/privy/public_api/privy_client.rb', line 20

def initialize(
  app_id: ENV["PRIVY_APP_ID"],
  app_secret: ENV["PRIVY_APP_SECRET"],
  environment: nil,
  base_url: ENV["PRIVY_API_BASE_URL"],
  max_retries: Privy::Client::DEFAULT_MAX_RETRIES,
  timeout: Privy::Client::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: Privy::Client::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: Privy::Client::DEFAULT_MAX_RETRY_DELAY,
  request_expiry: Privy::PrivyRequestExpiryOptions.build,
  authorization_key_cache_max_capacity: Privy::JwtExchangeService::DEFAULT_CACHE_MAX_CAPACITY
)
  @app_id = app_id
  @app_secret = app_secret
  @request_expiry_options = request_expiry

  @api = Privy::Client.new(
    app_id: app_id,
    app_secret: app_secret,
    environment: environment,
    base_url: base_url,
    max_retries: max_retries,
    timeout: timeout,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay
  )

  @wallets = Privy::Services::Wallets.new(client: @api, privy_client: self)
  @users = Privy::Services::Users.new(client: @api, privy_client: self)
  @policies = Privy::Services::Policies.new(client: @api, privy_client: self)
  @key_quorums = Privy::Services::KeyQuorums.new(client: @api, privy_client: self)
  @jwt_exchange = Privy::JwtExchangeService.new(
    wallets_resource: @api.wallets,
    cache_max_capacity: authorization_key_cache_max_capacity
  )
end

Instance Attribute Details

#apiObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



12
13
14
# File 'lib/privy/public_api/privy_client.rb', line 12

def api
  @api
end

#app_idObject (readonly)

Returns the value of attribute app_id.



9
10
11
# File 'lib/privy/public_api/privy_client.rb', line 9

def app_id
  @app_id
end

#app_secretObject (readonly)

Returns the value of attribute app_secret.



9
10
11
# File 'lib/privy/public_api/privy_client.rb', line 9

def app_secret
  @app_secret
end

#jwt_exchangeObject (readonly)

Service accessors.



15
16
17
# File 'lib/privy/public_api/privy_client.rb', line 15

def jwt_exchange
  @jwt_exchange
end

#key_quorumsObject (readonly)

Service accessors.



15
16
17
# File 'lib/privy/public_api/privy_client.rb', line 15

def key_quorums
  @key_quorums
end

#policiesObject (readonly)

Service accessors.



15
16
17
# File 'lib/privy/public_api/privy_client.rb', line 15

def policies
  @policies
end

#request_expiry_optionsPrivy::PrivyRequestExpiryOptions (readonly)



18
19
20
# File 'lib/privy/public_api/privy_client.rb', line 18

def request_expiry_options
  @request_expiry_options
end

#usersObject (readonly)

Service accessors.



15
16
17
# File 'lib/privy/public_api/privy_client.rb', line 15

def users
  @users
end

#walletsObject (readonly)

Service accessors.



15
16
17
# File 'lib/privy/public_api/privy_client.rb', line 15

def wallets
  @wallets
end

Instance Method Details

#compute_request_expiry(override_absolute_ms = nil) ⇒ Integer?

Resolves the absolute Unix-ms timestamp to send as ‘privy-request-expiry`.

Precedence:

1. Explicit per-call value (caller wins, even over `disabled: true`).
2. `disabled: true` -> nil (no header sent).
3. `request_expiry_options.default_ms` (offset from now).
4. {DEFAULT_REQUEST_EXPIRY_MS} (15 minutes from now).

Parameters:

  • override_absolute_ms (Integer, nil) (defaults to: nil)

    Absolute Unix-ms timestamp from caller.

Returns:

  • (Integer, nil)


67
68
69
70
71
72
73
# File 'lib/privy/public_api/privy_client.rb', line 67

def compute_request_expiry(override_absolute_ms = nil)
  return override_absolute_ms unless override_absolute_ms.nil?
  return nil if @request_expiry_options.disabled

  Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) +
    (@request_expiry_options.default_ms || DEFAULT_REQUEST_EXPIRY_MS)
end