Appearance
Pregenerating an embedded wallet
With Privy, you can pregenerate self-custodial Ethereum and Solana embedded wallets associated with a given account, like an email address or phone number, without requiring the user to login. 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.
Using @privy-io/server-auth
To pregenerate embedded wallets for a given user, use the PrivyClient
's importUser
method.
tsx
const user = await privy.importUser({
linkedAccounts: [
{
type: 'email',
address: '[email protected]',
},
],
createEthereumWallet: true,
createSolanaWallet: true,
createEthereumSmartWallet: true,
});
As a parameter to the method, pass an ImportUserInput
object with the following fields:
Field | Type | Description |
---|---|---|
linkedAccounts | LinkedAccount[] | An array including all of the user's linked accounts. These objects are in the same shape as the linked accounts returned by getUser . For each linked account, you must specify the type and must not include a verifiedAt timestamp. |
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! |
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 import user API:
sh
https://auth.privy.io/api/v1/users
In the body of the request:
- Include a
linked_accounts
array that contains the user's account to claim the wallet. - To create an Ethereum wallet, include a
create_ethereum_wallet
boolean with the valuetrue
. - To create an Solana wallet, include a
create_solana_wallet
boolean with the valuetrue
. - To create a smart wallet, include both
create_ethereum_smart_wallet
boolean with the valuetrue
, andcreate_ethereum_wallet
with the valuetrue
.
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/users \
-u "<your-privy-app-id>:<your-privy-app-secret>" \
-H "privy-app-id: <your-privy-app-id>" \
-H "Content-Type: application/json" \
-d '{
"create_ethereum_wallet": true,
"linked_accounts": [
{
"address": "[email protected]",
"type": "email"
}
]
}'
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.