Appearance
Creating Solana wallets
With Privy, you can create Solana wallets for your users manually from your frontend once are authenticated in your app, or via wallet pregeneration from your server before they have even logged in.
Manual creation
To create an embedded wallet on Solana for your user, use the createWallet
method returned by Privy's useSolanaWallets
hook:
tsx
const {createWallet} = useSolanaWallets();
When invoked, createWallet
returns a Promise for a WalletWithMetadata
object representing the created wallet. This wallet object has walletClientType: 'privy'
and chainType: 'solana'
and is also automatically added to the user.linkedAccounts
array for the current user.
As an example, you might have a createWallet
button in your app like so:
tsx
import {usePrivy, useSolanaWallets} from '@privy-io/react-auth';
function CreateWalletButton() {
const {authenticated, user} = usePrivy();
const {createWallet} = useSolanaWallets();
const hasExistingSolanaWallet = !!user.linkedAccounts.find(
(account): account is WalletWithMetadata =>
account.type === 'wallet' &&
account.walletClientType === 'privy' &&
account.chainType === 'solana',
);
// Users must be `authenticated` and not have an existing Solana embedded wallet
// to create a Solana embedded wallet
return (
<button disabled={!authenticated || hasExistingSolanaWallet} onClick={createWallet}>
Create a Solana wallet
</button>
);
}
The Promise returned by createWallet
will reject with an error if:
- The user is not
authenticated
- The user already has an existing Solana embedded wallet
- Embedded wallet creation fails unexpectedly in the browser (e.g., the user closes the page prematurely)
INFO
If your app uses both Ethereum and Solana embedded wallets, you must create the Ethereum embedded wallet first before creating a Solana embedded wallet. If you create a Solana embedded wallet first, you will not be able to create an Ethereum embedded wallet later. We are actively working on support for creating an Ethereum embedded wallet for a user with an existing Solana embedded wallet.
Pregenerating Solana wallets
With Privy, you can pregenerate self-custodial 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.
Follow the embedded wallet pregeneration guide to create Solana wallets for your users.
Once the user associated with the account logs in, they will be able to access the pregenerated wallet and any assets sent to them.