Class: Privy::Services::Wallets

Inherits:
Resources::Wallets show all
Defined in:
lib/privy/public_api/services/wallets.rb

Instance Attribute Summary collapse

Attributes inherited from Resources::Wallets

#balance, #earn, #transactions

Instance Method Summary collapse

Methods inherited from Resources::Wallets

#_init_import, #_submit_import, #_transfer, #authenticate_with_jwt, #create_wallets_with_recovery, #export, #get, #get_wallet_by_address, #list

Constructor Details

#initialize(client:, privy_client:) ⇒ Wallets

Returns a new instance of Wallets.



8
9
10
11
# File 'lib/privy/public_api/services/wallets.rb', line 8

def initialize(client:, privy_client:)
  super(client: client)
  @privy_client = privy_client
end

Instance Attribute Details

#privy_clientObject (readonly)

Returns the value of attribute privy_client.



6
7
8
# File 'lib/privy/public_api/services/wallets.rb', line 6

def privy_client
  @privy_client
end

Instance Method Details

#create(wallet_create_params:, idempotency_key: nil, request_options: nil) ⇒ Privy::Models::Wallet

Create a new wallet on the requested chain and for the requested owner.

Examples:

Create an ownerless Ethereum wallet

client.wallets.create(wallet_create_params: {chain_type: :ethereum})

Create a wallet with a P-256 key owner

client.wallets.create(wallet_create_params: {
  chain_type: :ethereum,
  owner: {public_key: base64_p256_public_key}
})

Parameters:

  • wallet_create_params (Hash)

    Body parameters for wallet creation.

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

    Ensures the request is executed only once.

  • request_options (Privy::RequestOptions, Hash, nil) (defaults to: nil)

    Transport-level config (timeouts, retries).

Options Hash (wallet_create_params:):

  • :chain_type (Symbol)

    The wallet chain type (required).

  • :owner (Hash, nil)

    Owner specified as user_id: or public_key:.

  • :owner_id (String, nil)

    Key quorum ID to set as owner.

  • :policy_ids (Array<String>, nil)

    Up to one policy ID to enforce.

  • :display_name (String, nil)

    A human-readable label for the wallet.

  • :external_id (String, nil)

    Customer-provided identifier for external mapping.

  • :additional_signers (Array<Hash>, nil)

    Additional signers for the wallet.

Returns:



36
37
38
39
40
# File 'lib/privy/public_api/services/wallets.rb', line 36

def create(wallet_create_params:, idempotency_key: nil, request_options: nil)
  combined_params = wallet_create_params.merge(request_options: request_options)
  combined_params[:privy_idempotency_key] = idempotency_key if idempotency_key
  super(combined_params)
end

#raw_sign(wallet_id, raw_sign_input:, authorization_context: nil, idempotency_key: nil, request_expiry: nil, request_options: nil) ⇒ Privy::Models::RawSignResponse

Sign a hash or raw bytes with a wallet by wallet ID.

Examples:

Sign a pre-computed hash

client.wallets.raw_sign("wallet-id", raw_sign_input: {
  params: {hash: "0x1234...abcdef"}
})

Sign raw bytes with a hash function

client.wallets.raw_sign("wallet-id", raw_sign_input: {
  params: {bytes: "0a0234ea...", encoding: "hex", hash_function: "sha256"}
}, authorization_context: ctx)

Parameters:

  • wallet_id (String)

    ID of the wallet to sign with.

  • raw_sign_input (Hash)

    Body parameters for the raw_sign operation (RawSignInput shape).

  • authorization_context (Privy::Authorization::AuthorizationContext, nil) (defaults to: nil)

    Authorization context for owned wallets.

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

    Ensures the request is executed only once.

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

    Absolute Unix-ms timestamp at which the request expires. Defaults to the value computed by the client’s PrivyRequestExpiryOptions.

  • request_options (Privy::RequestOptions, Hash, nil) (defaults to: nil)

    Transport-level config (timeouts, retries).

Options Hash (raw_sign_input:):

  • :params (Hash)

    The signing parameters (required). Either hash: or encoding:, hash_function:.

Returns:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/privy/public_api/services/wallets.rb', line 151

def raw_sign(
  wallet_id,
  raw_sign_input:,
  authorization_context: nil,
  idempotency_key: nil,
  request_expiry: nil,
  request_options: nil
)
  prepared = Privy::Authorization.prepare_request(
    privy_client,
    method: :post,
    url: Privy::Authorization.signed_url(privy_client, "v1/wallets/#{wallet_id}/raw_sign"),
    body: raw_sign_input,
    authorization_context: authorization_context,
    idempotency_key: idempotency_key,
    request_expiry: privy_client.compute_request_expiry(request_expiry)
  )
  combined_params = raw_sign_input.merge(request_options: request_options)
  Privy::Authorization.merge_prepared_headers!(combined_params, prepared.headers)
  super(wallet_id, combined_params)
end

#rpc(wallet_id, wallet_rpc_request_body:, authorization_context: nil, idempotency_key: nil, request_expiry: nil, request_options: nil) ⇒ Privy::Models::WalletRpcResponse

Sign a message or transaction with a wallet by wallet ID.

Examples:

Personal sign on an ownerless wallet

client.wallets.rpc("wallet-id", wallet_rpc_request_body: {
  method: "personal_sign",
  chain_type: "ethereum",
  params: {message: "hello", encoding: "utf-8"}
})

Sign a transaction with authorization

client.wallets.rpc("wallet-id", wallet_rpc_request_body: {
  method: "eth_signTransaction",
  chain_type: "ethereum",
  params: {transaction: {to: "0x...", value: "0x0", chain_id: 1}}
}, authorization_context: ctx)

Parameters:

  • wallet_id (String)

    ID of the wallet.

  • wallet_rpc_request_body (Hash)

    The RPC request body, discriminated by :method.

  • authorization_context (Privy::Authorization::AuthorizationContext, nil) (defaults to: nil)

    Authorization context for owned wallets.

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

    Ensures the request is executed only once.

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

    Absolute Unix-ms timestamp at which the request expires. Defaults to the value computed by the client’s PrivyRequestExpiryOptions.

  • request_options (Privy::RequestOptions, Hash, nil) (defaults to: nil)

    Transport-level config (timeouts, retries).

Options Hash (wallet_rpc_request_body:):

  • :method (String)

    The RPC method name (e.g. “personal_sign”, “eth_signTransaction”).

  • :chain_type (String)

    The chain type (e.g. “ethereum”, “solana”).

  • :params (Hash)

    Method-specific parameters.

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/privy/public_api/services/wallets.rb', line 106

def rpc(
  wallet_id,
  wallet_rpc_request_body:,
  authorization_context: nil,
  idempotency_key: nil,
  request_expiry: nil,
  request_options: nil
)
  prepared = Privy::Authorization.prepare_request(
    privy_client,
    method: :post,
    url: Privy::Authorization.signed_url(privy_client, "v1/wallets/#{wallet_id}/rpc"),
    body: wallet_rpc_request_body,
    authorization_context: authorization_context,
    idempotency_key: idempotency_key,
    request_expiry: privy_client.compute_request_expiry(request_expiry)
  )
  combined_params = {wallet_rpc_request_body: wallet_rpc_request_body, request_options: request_options}
  Privy::Authorization.merge_prepared_headers!(combined_params, prepared.headers)
  super(wallet_id, combined_params)
end

#transfer(wallet_id, wallet_transfer_params:, authorization_context: nil, idempotency_key: nil, request_expiry: nil, request_options: nil) ⇒ Privy::Models::TransferActionResponse

Transfer tokens from a wallet to a destination address.

Examples:

Transfer USDC on Base

client.wallets.transfer("wallet-id", wallet_transfer_params: {
  source: {asset: "usdc", amount: "10.5", chain: "base"},
  destination: {address: "0xB00F...28a2"}
})

Cross-asset transfer with slippage

client.wallets.transfer("wallet-id", wallet_transfer_params: {
  source: {asset: "usdc", amount: "10.5", chain: "base"},
  destination: {address: "0xB00F...28a2", asset: "usdt", chain: "ethereum"},
  amount_type: "exact_input",
  slippage_bps: 100
}, authorization_context: ctx)

Parameters:

  • wallet_id (String)

    ID of the wallet to transfer from.

  • wallet_transfer_params (Hash)

    Body parameters for the transfer operation.

  • authorization_context (Privy::Authorization::AuthorizationContext, nil) (defaults to: nil)

    Authorization context for owned wallets.

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

    Ensures the request is executed only once.

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

    Absolute Unix-ms timestamp at which the request expires. Defaults to the value computed by the client’s PrivyRequestExpiryOptions.

  • request_options (Privy::RequestOptions, Hash, nil) (defaults to: nil)

    Transport-level config (timeouts, retries).

Options Hash (wallet_transfer_params:):

  • :source (Hash)

    Source asset, amount, and chain (required).

  • :destination (Hash)

    Destination address, and optionally asset and chain (required).

  • :amount_type (String)

    Whether amount refers to input or output token (“exact_input” or “exact_output”).

  • :slippage_bps (Integer)

    Maximum allowed slippage in basis points (1 bps = 0.01%).

Returns:



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/privy/public_api/services/wallets.rb', line 203

def transfer(
  wallet_id,
  wallet_transfer_params:,
  authorization_context: nil,
  idempotency_key: nil,
  request_expiry: nil,
  request_options: nil
)
  prepared = Privy::Authorization.prepare_request(
    privy_client,
    method: :post,
    url: Privy::Authorization.signed_url(privy_client, "v1/wallets/#{wallet_id}/transfer"),
    body: wallet_transfer_params,
    authorization_context: authorization_context,
    idempotency_key: idempotency_key,
    request_expiry: privy_client.compute_request_expiry(request_expiry)
  )
  combined_params = wallet_transfer_params.merge(request_options: request_options)
  Privy::Authorization.merge_prepared_headers!(combined_params, prepared.headers)
  _transfer(wallet_id, combined_params)
end

#update(wallet_id, wallet_update_params:, authorization_context: nil, request_expiry: nil, request_options: nil) ⇒ Privy::Models::Wallet

Update a wallet’s policies or authorization key configuration.

Examples:

Update wallet policies

client.wallets.update("wallet-id", wallet_update_params: {
  policy_ids: ["policy-id"]
}, authorization_context: ctx)

Parameters:

  • wallet_id (String)

    ID of the wallet to update.

  • wallet_update_params (Hash)

    Body parameters for the update.

  • authorization_context (Privy::Authorization::AuthorizationContext, nil) (defaults to: nil)

    Authorization context for owned wallets.

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

    Absolute Unix-ms timestamp at which the request expires. Defaults to the value computed by the client’s PrivyRequestExpiryOptions.

  • request_options (Privy::RequestOptions, Hash, nil) (defaults to: nil)

    Transport-level config (timeouts, retries).

Options Hash (wallet_update_params:):

  • :owner (Hash, nil)

    New owner specified as user_id: or public_key:.

  • :owner_id (String, nil)

    Key quorum ID to set as owner.

  • :policy_ids (Array<String>, nil)

    New policy IDs to enforce on the wallet.

  • :display_name (String, nil)

    A human-readable label for the wallet. Set to nil to clear.

  • :additional_signers (Array<Hash>, nil)

    Additional signers for the wallet.

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/privy/public_api/services/wallets.rb', line 63

def update(wallet_id, wallet_update_params:, authorization_context: nil, request_expiry: nil, request_options: nil)
  prepared = Privy::Authorization.prepare_request(
    privy_client,
    method: :patch,
    url: Privy::Authorization.signed_url(privy_client, "v1/wallets/#{wallet_id}"),
    body: wallet_update_params,
    authorization_context: authorization_context,
    request_expiry: privy_client.compute_request_expiry(request_expiry)
  )
  combined_params = wallet_update_params.merge(request_options: request_options)
  Privy::Authorization.merge_prepared_headers!(combined_params, prepared.headers)
  super(wallet_id, combined_params)
end