Linking & handling multiple wallets
Privy allows users to link multiple wallets to their account. Your app may want users to link additional wallets to get a more comprehensive understanding of the user's web3 experience and activities.
Linking multiple wallets
You can prompt a user to link additional wallets to their account using the linkWallet
method. Once this method is invoked, the user's chosen wallet application (e.g. MetaMask) will prompt the user to select the address they want to link.
All of a user's linked wallets will be returned in the linkedAccounts
field of the corresponding user
object.
The active wallet
To make it easier to handle multiple wallets for a single user, Privy has a notion of an active wallet in the client. If a user has linked multiple wallets, the active wallet represents the wallet that the user is actively using in your app.
You can get the current user's active wallet via the user.wallet
field from the user
object.
Additionally, when you need to request a signature or transaction from the user, and you request Privy for their wallet provider, Privy will return the provider for the user's active wallet.
Setting the active wallet
Generally, the wallet that a user has linked most recently will be the active wallet. More concretely:
- If a user has no wallets linked to their account, the active wallet (
user.wallet
) will beundefined
. - If a user has only one wallet linked to their account, that wallet will be the active wallet.
- When a user links a new wallet, that wallet will become the new active wallet.
- If a user unlinks their active wallet, the new active wallet will be the most recently linked of their remaining wallets.
You can override the user's current active wallet by calling the setActiveWallet
method from the usePrivy
hook and specifying the address of the wallet that you would like to become active.
You may want to override the current user's active wallet when certain events take place in your app. For instance, if a user disconnects their current active wallet from your site, and connects a different wallet, you may want to call setActiveWallet
whenever window.ethereum
's accountsChanged
event is fired.