Use this file to discover all available pages before exploring further.
Base Sub Accounts are a feature of the Base Account (formerly known as Coinbase Smart Wallet) that allow you to streamline the user experience of using a Base Account in your app.Follow the guide below to learn how to use Base Sub Accounts with Privy.
By default, when a user uses their Base Account within their app, the user must authorize every signature and transaction via a passkey prompt. This may be interruptive for your app’s user experience, particularly for use cases that require a high-volume of signatures or transactions, such as gaming.Sub Accounts enable you to create an Ethereum account derived from the parent Base Account that is specific to your app, with its own address, signing capabilities, and transaction history. This Sub Account is owned by another wallet, such as an embedded wallet or a local account, and can be configured to not require an explicit (passkey) confirmation from the user on every signature and transaction.Sub accounts can even transact with the balance of the parent account using Spend Permissions, allowing users to spend this balance without explicit passkey prompts.
First, follow the React Quickstart to get your app instrumented with Privy’s basic functionality. Make sure you have updated your @privy-io/react-auth SDK to the latest version.Next, configure your React app to:
Show the Base Account as one of the external wallet options that users can use to connect to your application. To do so, pass 'base_account' to the config.appearance.walletList array.
Next, after the user logs in, create a new Sub Account or get the existing Sub Account for the user that is tied to your app’s domain.To start, get the connected wallet instances for your user’s embedded wallet and Base App by searching for the entries with walletClientType: 'privy' and walletClientType: 'base_account' respectively in your useWallets array:
import {useWallets} from '@privy-io/react-auth';const {wallets} = useWallets();const embeddedWallet = wallets.find((wallet) => wallet.walletClientType === 'privy');const baseAccount = wallets.find((wallet) => wallet.walletClientType === 'base_account');// `embeddedWallet` and `baseAccount` must be defined for users to use Sub Accounts
Next, switch the network of the Base Account to Base or Base Sepolia, and get the wallet’s EIP-1193 provider:
// Switching to Base Sepoliaawait baseAccount.switchChain(84532);const provider = await baseAccount.getEthereumProvider();
Lastly, check if the user has an existing Sub Account using the wallet_getSubAccounts RPC method. If the user does not have an existing Sub Account, create a new one for them using the wallet_addSubAccount RPC:
// Get existing Sub Account if it existsconst { subAccounts: [existingSubAccount]} = await provider.request({ method: 'wallet_getSubAccounts', params: [ { account: baseAccount.address as `0x${string}`, // The address of your user's Base Account domain: window.location.origin // The domain of your app } ]});// Use the existing Sub Account if it exists, otherwise create a new sub accountconst subaccount = existingSubAccount ? existingSubAccount : await provider.request({ method: 'wallet_addSubAccount', params: [ { version: '1', account: { type: 'create', keys: [ { type: 'address', publicKey: embeddedWallet.address as Hex // Pass your user's embedded wallet address } ] } } ] });
3. Configure the SDK to use the embedded wallet for Sub Account operations
Next, configure the Base Account SDK to use the embedded wallet to control Sub Account operations. This allows the embedded wallet to sign messages and transactions on behalf of the Sub Account, avoiding the need for a separate passkey prompt.Use the useBaseAccountSdk hook from Privy’s React SDK to access the instance of the Base Account SDK directly, and use the SDK’s subAccount.setToOwnerAccount method to configure the embedded wallet to sign on behalf of the Sub Account’s operations.As a parameter to this method, pass a function that returns a Promise for a viem LocalAccount representing the user’s embedded wallet. You can use Privy’s toViemAccount utility method to do so.
Show the code for steps 3 & 4 together to create or get a Sub Account.
The code below showcases how to create or get an existing Sub Account for your user, and set the embedded wallet as the Sub Account’s owner.
import {useWallets, useBaseAccountSdk, toViemAccount} from '@privy-io/react-auth';// In your React componentconst {wallets} = useWallets();const {baseAccountSdk} = useBaseAccountSdk();const embeddedWallet = wallets.find((wallet) => wallet.walletClientType === 'privy');const baseAccount = wallets.find((wallet) => wallet.walletClientType === 'base_account');// Call this function when needed, e.g. in a button's `onClick` handlerconst createOrGetSubAccount = async () => { if (!embeddedWallet) throw new Error('User does not have an embedded wallet'); if (!baseAccount) throw new Error('User has not connected a Base Account'); if (!baseAccountSdk) throw new Error('Base Account SDK not initialized'); await baseAccount.switchChain(84532); // Use 8453 for Base Mainnet const provider = await baseAccount.getEthereumProvider(); // Get existing Sub Account const { subAccounts: [existingSubAccount] } = await provider.request({ method: 'wallet_getSubAccounts', params: [ { account: baseAccount.address as `0x${string}`, // The address of your user's Base Account domain: window.location.origin // The domain of your app } ] }); // Create new Sub Account if one does not exist const subaccount = existingSubAccount ? existingSubAccount : await provider.request({ method: 'wallet_addSubAccount', params: [ { version: '1', account: { type: 'create', keys: [ { type: 'address', publicKey: embeddedWallet.address as Hex // Pass your user's embedded wallet address } ] } } ] }); // Configure privy embedded wallets to power Sub Account operations const toOwnerAccount = async () => { const account = await toViemAccount({wallet: embeddedWallet}); return {account}; }; baseAccountSdk.subAccount.setToOwnerAccount(toOwnerAccount);};
4. Sign messages and send transactions with the Sub Account
Lastly, you can sign and send transactions with the Sub Account using the Base Account’s EIP1193 provider. To ensure that signatures and transactions come from the Sub Account, for each of the following RPCs:
personal_sign: pass the Sub Account’s address, not the parent Base Account’s address, as the second parameter.
eth_signTypedData_v4: pass the Sub Account’s address, not the parent Base Account’s address, as the first parameter.
eth_sendTransaction: set from in the transaction object to the Sub Account’s address, not the parent Base Account’s address.
When these methods are invoked, the embedded wallet will sign on behalf of the Sub Account, avoiding the need for an explicit passkey prompt from the user.
Sign a message
Send a transaction
Send a batch of transactions
import {toHex} from 'viem';const message = 'Hello world';const signature = await baseProvider.request({ method: 'personal_sign', params: [toHex(message), subaccount.address] // Pass the Sub Account, not parent Base Account address});
import {parseEther} from 'viem';const txHash = await baseProvider.request({ method: 'eth_sendTransaction', params: [{ from: subaccount.address, // Use Sub Account address as sender to: 'insert-recipient-address', value: parseEther('0.01').toString(), data: '0x' }]});
import {parseEther} from 'viem';const userOpHash = await baseProvider.request({ method: 'wallet_sendCalls', params: [{ from: subaccount.address, // Use Sub Account address calls: [{ to: 'insert-recipient-address', value: parseEther('0.01').toString(), data: '0x' }] }]});
You can combine Sub Accounts with Spend
Permissions to allow the Sub
Account to spend from the balance of the parent Base Account in eth_sendTransaction requests.