Privy enables your users to import a private key into their embedded wallet.
This allows users to bring an existing wallet into your application, enabling them to manage their assets directly within your app. For example, a user can import their MetaMask wallet private key to access and manage their assets within your app seamlessly. This method can also be used by developers to migrate users from a different embedded wallet provider to Privy.
Privy’s React SDK currently supports importing wallets on EVM networks and Solana.
To have your user import a private key into their embedded wallet, use the importWallet method from the useImportWallet hook. As a parameter to this method, pass an object containing the user’s privateKey as a string.
import {usePrivy} from '@privy-io/react-auth';import {useImportWallet} from '@privy-io/react-auth/solana';function ImportWalletButton() { const {ready, authenticated} = usePrivy(); const {importWallet} = useImportWallet(); const [privateKey, setPrivateKey] = useState(''); const handleImport = async () => { try { const wallet = await importWallet({privateKey: privateKey}); console.log('Wallet imported successfully:', wallet); } catch (error) { console.error('Failed to import wallet:', error); } }; // Check that your user is authenticated const isAuthenticated = ready && authenticated; return ( <div> <input type="text" value={privateKey} onChange={(e) => setPrivateKey(e.target.value)} placeholder="Enter your private key" /> <button onClick={handleImport} disabled={!isAuthenticated}> Import my wallet </button> </div> );}
If importWallet succeeds, the imported wallet will be added to the user’s linkedAccounts and the wallets array returned by the useWallets hook, with an imported field that is marked true. As an example, you can find the imported wallet like so:
Using Privy’s native signature and transaction methods
To use Privy’s native signMessage, signTypedData, and sendTransaction methods with an imported wallet, simply pass the address for the imported wallet as the final optional parameter to these methods:
Copy
Ask AI
const {signMessage} = useSignMessage();const signature = await signMessage( { message: 'insert-message-to-sign' }, { uiOptions: insertOptionalUIConfigOrUndefined, address: 'insert-imported-wallet-address' // Replace with the imported wallet address in EIP55 format });
Copy
Ask AI
const {signMessage} = useSignMessage();const signature = await signMessage( { message: 'insert-message-to-sign' }, { uiOptions: insertOptionalUIConfigOrUndefined, address: 'insert-imported-wallet-address' // Replace with the imported wallet address in EIP55 format });
Copy
Ask AI
const {signTypedData} = useSignTypedData();const signature = await signTypedData(insertTypedDataObject, { uiOptions: insertOptionalUIConfigOrUndefined, address: 'insert-imported-wallet-address' // Replace with the imported wallet address in EIP55 format});
Copy
Ask AI
const {sendTransaction} = useSendTransaction();const signature = await sendTransaction(insertTransactionRequest, { address: 'insert-imported-wallet-address', // Replace with the imported wallet address in EIP55 format uiOptions: insertOptionalUIConfigOrUndefined, fundWalletConfig: insertOptionalFundWalletConfigOrUndefined});
Privy enables your users to export the full private key for their embedded wallet, even if it was imported. This allows them to use their embedded wallet address with another wallet client, such as MetaMask in any application.
To have your user export their embedded wallet’s private key, use the exportWallet method from the usePrivy hook. As a parameter to this method, you must pass an object containing an address field with the user’s imported wallet address as a string.
As an example, you might have an export wallet button in your app like so:
Copy
Ask AI
import {usePrivy, useWallets} from '@privy-io/react-auth';function ExportWalletButton() { const {ready, authenticated, exportWallet} = usePrivy(); const {wallets} = useWallets(); // Check that your user is authenticated const isAuthenticated = ready && authenticated; // Find your user's imported wallet const importedWallet = wallets.find( (wallet) => wallet.walletClientType === 'privy' && wallet.imported ); const exportImportedWallet = async () => { if (!importedWallet) return; await exportWallet({address: importedWallet.address}); }; return ( <button onClick={exportImportedWallet} disabled={!isAuthenticated || !importedWallet}> Export my wallet </button> );}
To have your user export their embedded wallet’s private key, use the exportWallet method from the usePrivy hook. As a parameter to this method, you must pass an object containing an address field with the user’s imported wallet address as a string.
As an example, you might have an export wallet button in your app like so:
Copy
Ask AI
import {usePrivy, useWallets} from '@privy-io/react-auth';function ExportWalletButton() { const {ready, authenticated, exportWallet} = usePrivy(); const {wallets} = useWallets(); // Check that your user is authenticated const isAuthenticated = ready && authenticated; // Find your user's imported wallet const importedWallet = wallets.find( (wallet) => wallet.walletClientType === 'privy' && wallet.imported ); const exportImportedWallet = async () => { if (!importedWallet) return; await exportWallet({address: importedWallet.address}); }; return ( <button onClick={exportImportedWallet} disabled={!isAuthenticated || !importedWallet}> Export my wallet </button> );}
To have your user export their embedded wallet’s private key, use the exportWallet method from the useSolanaWallets hook. As a parameter to this method, you must pass an object containing an address field with the user’s imported wallet address as a string.
As an example, you might have an export wallet button in your app like so:
Copy
Ask AI
import {usePrivy} from '@privy-io/react-auth';import {useSolanaWallets} from '@privy-io/react-auth/solana';function ExportWalletButton() { const {ready, authenticated} = usePrivy(); const {wallets, exportWallet} = useSolanaWallets(); // Check that your user is authenticated const isAuthenticated = ready && authenticated; // Find your user's imported wallet const importedWallet = wallets.find( (wallet) => wallet.walletClientType === 'privy' && wallet.imported ); const exportImportedWallet = async () => { if (!importedWallet) return; await exportWallet({address: importedWallet.address}); }; return ( <button onClick={exportImportedWallet} disabled={!isAuthenticated || !importedWallet}> Export my wallet </button> );}
Exporting the seed phrase for an imported embedded wallet is not supported, as it is impossible to
recover the full seed phrase for the wallet from the imported private key.