Interfacing with external wallets
With Privy, your users can connect external wallets like MetaMask, Coinbase Wallet, Rainbow Wallet, and more to your app. You can use popular web3 libraries for requesting signatures and transactions from external wallets that have been connected through Privy
Below, we outline how you can integrate Privy with ethers.js
, web3.js
, wagmi
, and other popular libraries.
When you request the current user's wallet provider using the methods below, if a user has multiple wallets linked to their account, Privy will return the provider for their active wallet.
Ethers.js
You can get an ethers-compatible provider from a user's wallet by calling the getEthersProvider
method from the usePrivy
hook. You may then use this as an ethers’ provider
to read blockchain data and send requests to a user’s wallet.
import { usePrivy } from '@privy-io/react-auth';
export default function MyComponent() {
const { getEthersProvider } = usePrivy();
// Get an ethers provider and signer from the user's wallet
const provider = getEthersProvider();
const signer = provider.getSigner();
return <>{/* your code here */}</>
}
Web3.js
You can get a web3.js-compatible provider from a user's wallet by calling the getWeb3jsProvider
method from the usePrivy
hook. You can then initialize your web3
client with this provider
.
import { usePrivy } from '@privy-io/react-auth';
import Web3 from 'web3';
export default function MyComponent() {
const { getWeb3jsProvider } = usePrivy();
// Initialize your Web3.js client with the provider
const web3 = new Web3(getWeb3jsProvider());
return <>{/* your code here */}</>
}
Ethereum Javascript API
Privy also exposes a generic EIP-1193 (Ethereum Javascript API) provider that you may use to communicate with a user’s wallet (via JSON-RPC), without needing an external library like those listed above. You can access this provider using the getEthereumProvider
from theusePrivy
React hook.
import { usePrivy } from '@privy-io/react-auth';
export default function MyComponent() {
const { getEthereumProvider } = usePrivy();
const ethereum = getEthereumProvider();
// You can now use the ethereum.request({method: ...})
// syntax as documented in the EIP.
return <>{/* your code here */}</>
}
Wagmi
Privy offers a Wagmi Connector package that you can use to integrate wagmi
alongside Privy in your app. Please see our guide for more.