Skip to content

Integrating with third-party libraries

Using the wallet's EIP-1193 provider, you can easily integrate Privy alongside a third-party library like viem, ethers, or web3js, to interface with the embedded wallet.

INFO

Third-party libraries may require additional shims to be used in a React Native environment.

Integrating with viem

First, import the necessary methods, objects, and networks from viem:

ts
import {createWalletClient, custom} from 'viem';
// Replace 'mainnet' with your desired network
import {mainnet} from 'viem/chains';

Next, get an EIP-1193 provider for the user's embedded wallet, and switch its network to your desired network:

ts
await wallet.provider.request({
  method: 'wallet_switchEthereumChain',
  // Replace '0x1' with the chain ID of your desired network
  params: [{chainId: '0x1'}],
});

Lastly, initialize a viem Wallet Client from the EIP-1193 provider:

ts
const walletClient = createWalletClient({
  // Replace this with your desired network that you imported from viem
  chain: mainnet,
  transport: custom(wallet.provider),
});

You can now use methods implemented by viem's Wallet Client, including signMessage, signTypedData, and sendTransaction!

Integrating with ethers

First, import ethers:

ts
import {ethers} from 'ethers';

Next, get an EIP-1193 provider for the user's embedded wallet, and switch its network to your desired network:

ts
await wallet.provider.request({
  method: 'wallet_switchEthereumChain',
  // Replace '0x1' with the chain ID of your desired network
  params: [{chainId: '0x1'}],
});

Lastly, initialize an ethers provider and signer from this EIP-1193 provider:

ts
const ethersProvider = new ethers.providers.Web3Provider(wallet.provider);
const ethersSigner = ethersProvider.getSigner();

You can then use methods implemented by ethers' providers and signers, including signMessage and sendTransaction.

Integrating with web3.js

First, import web3:

ts
import {Web3} from 'web3';

Next, get an EIP-1193 provider for the user's embedded wallet, and switch its network to your desired network:

ts
await wallet.provider.request({
  method: 'wallet_switchEthereumChain',
  // Replace '0x1' with the chain ID of your desired network
  params: [{chainId: '0x1'}],
});

Lastly, initialize an ethers provider and signer from this EIP-1193 provider:

ts
const web3 = new Web3(wallet.getEthereumProvider());
const ethersSigner = ethersProvider.getSigner();

You can then use interfaces by web3.js' for signing messages, sending transactions, and more.