Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/privy/public_api/services/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ def initialize(client:, privy_client:)
super(client: client)
@privy_client = privy_client
end

# Create a new user with linked accounts. Optionally pre-generate embedded wallets
# for the user.
#
# @example Create a user from an email address
# client.users.create(user_create_params: {
# linked_accounts: [{address: "tom.bombadill@privy.io", type: :email}]
# })
#
# @example Create a user with custom metadata and a pregenerated Ethereum wallet
# client.users.create(user_create_params: {
# linked_accounts: [{address: "tom.bombadill@privy.io", type: :email}],
# custom_metadata: {plan: "pro"},
# wallets: [{chain_type: :ethereum}]
# })
#
# @param user_create_params [Hash] Body parameters for user creation.
# @option user_create_params [Array<Hash>] :linked_accounts Linked accounts to associate with the user (required).
# @option user_create_params [Hash{Symbol=>String, Float, Boolean}, nil] :custom_metadata Custom metadata associated with the user.
# @option user_create_params [Array<Hash>, nil] :wallets Wallets to pregenerate for the user.
# @param request_options [Privy::RequestOptions, Hash, nil] Transport-level config (timeouts, retries).
#
# @return [Privy::Models::User]
def create(user_create_params:, request_options: nil)
super(user_create_params.merge(request_options: request_options))
end
end
end
end
30 changes: 30 additions & 0 deletions test/privy/integration/services/users_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require_relative "../integration_test_helper"

class Privy::Test::Integration::UsersTest < Privy::Test::IntegrationTest
def test_create_user_with_custom_auth_and_pregenerated_ethereum_wallet
custom_user_id = "test-user-#{Time.now.to_f.to_s.tr('.', '-')}"

user = client.users.create(
user_create_params: {
linked_accounts: [{custom_user_id: custom_user_id, type: :custom_auth}],
wallets: [{chain_type: :ethereum}]
}
)

refute_nil(user.id, "expected user.id to be set")

custom_auth = user.linked_accounts.find { |a| a.type == :custom_auth }
refute_nil(custom_auth, "expected to find custom_auth linked account")
assert_equal(custom_user_id, custom_auth.custom_user_id)

wallet = user.linked_accounts.find { |a| a.type == :wallet }
refute_nil(wallet, "expected to find wallet linked account")
assert_equal(:ethereum, wallet.chain_type)
refute_nil(wallet.address, "expected wallet.address to be set")
assert_match(/^0x[a-fA-F0-9]{40}$/, wallet.address)
ensure
client.users.delete(user.id) if user&.id
end
end