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)
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.