Appearance
Pregenerating an embedded wallet for existing users
With Privy, you can pregenerate self-custodial Ethereum and Solana embedded wallets for existing users that don't already have them. You can even send assets to the wallet before the user logs in to your app for the first time.
Once the user associated with the account logs in, they will be able to access the pregenerated wallet and any assets sent to them.
INFO
If the user already has any embedded wallets, any additional wallets must be created client-side and cannot be generated.
Using @privy-io/server-auth
To pregenerate embedded wallets for an existing user, use the PrivyClient
's createWallets
method.
tsx
const user = await privy.createWallets({
userId: 'did:privy:clddy332f002tyqpq3b3lv327',
createEthereumWallet: true,
createSolanaWallet: true,
createEthereumSmartWallet: true,
numberOfEthereumWalletsToCreate: 2,
});
As a parameter to the method, pass a CreateWalletsInput
object with the following fields:
Field | Type | Description |
---|---|---|
userId | string | The ID of the user to pregenerate wallets for. |
createEthereumWallet | boolean | (Optional) Whether to pregenerate an Ethereum embedded wallet for the imported user. Defaults to false . You must set this to true to create an Ethereum wallet. |
createSolanaWallet | boolean | (Optional) Whether to pregenerate a Solana embedded wallet for the imported user. Defaults to false . You must set this to true to create a Solana wallet. |
createEthereumSmartWallet | boolean | (Optional) Whether to pregenerate a smart wallet for the imported user. Defaults to false . You must set this to true to create a smart wallet. If createEthereumSmartWallet is true , you must also explicitly set createEthereumWallet to true as well. Learn more about smart wallets here! |
numberOfEthereumWalletsToCreate | number | (Optional) The number of Ethereum wallets to pregenerate for the user. Defaults to 1 . |
This will pregenerate an embedded/smart wallet that can be accessed by logging into the imported account (e.g. [email protected]
in the example above).
Using the REST API
To pregenerate embedded wallets, simply make a POST
request to Privy's wallet generation API:
sh
https://auth.privy.io/api/v1/apps/<your-privy-app-id>/users/wallet
Pass the following fields in the body of the request:
Field | Type | Description |
---|---|---|
user_id | string | The ID of the user to pregenerate the wallet for. |
create_ethereum_wallet | boolean | Whether to create an Ethereum embedded wallet. Set to true to create one. You must set this to true to create an Ethereum wallet. |
number_of_ethereum_wallets_to_create | number | (Optional) The number of HD wallets to create using the initial wallet as a root node. This must be greater than 1 to create additional wallets. |
create_solana_wallet | boolean | Whether to create a Solana embedded wallet. Set to true to create one. You must set this to true to create a Solana wallet. |
create_ethereum_smart_wallet | boolean | Whether to create a smart wallet. Set to true to create one. You must set this to true to create a smart wallet. If create_ethereum_smart_wallet is true , you must also explicitly set create_ethereum_wallet to true as well. Learn more about smart wallets here! |
Below is a sample cURL command for generating a new wallet for a user with Privy:
sh
$ curl --request POST https://auth.privy.io/api/v1/apps/<your-privy-app-id>/users/wallet \
-u "<your-privy-app-id>:<your-privy-app-secret>" \
-H "privy-app-id: <your-privy-app-id>" \
-H "Content-Type: application/json" \
-d '{
"user_id": "did:privy:clddy332f002tyqpq3b3lv327",
"create_ethereum_wallet": true
}'
A successful response will include the new user object along with their user ID and embedded wallet address, like below:
json
{
"id": "did:privy:clddy332f002tyqpq3b3lv327",
"created_at": 1674788927,
"linked_accounts": [
{
"address": "[email protected]",
"type": "email"
},
{
"address": "0x3DAF84b3f09A0E2092302F7560888dBc0952b7B7",
"type": "wallet",
"walletClient": "privy",
"chain_type": "ethereum"
}
]
}
Accessing a pregenerated wallet
A user can easily claim their pregenerated wallet simply by logging into your app with one of their linked accounts.