EVM provider and private key
After a user signs in on an EVM chain, useWeb3Auth().connection.ethereumProvider is an EIP-1193
provider. Pass it to viem or ethers to read accounts, sign messages, and send transactions. To use
Wagmi hooks instead, see Ethereum hooks (Wagmi).
Use the provider with viem
import { useWeb3Auth } from '@web3auth/react-native-sdk'
import { createPublicClient, createWalletClient, custom, formatEther, parseEther } from 'viem'
function EvmActions() {
const { connection } = useWeb3Auth()
const getClients = () => {
const transport = custom(connection!.ethereumProvider!)
return {
walletClient: createWalletClient({ transport }),
publicClient: createPublicClient({ transport }),
}
}
const getBalance = async () => {
const { walletClient, publicClient } = getClients()
const [address] = await walletClient.getAddresses()
return formatEther(await publicClient.getBalance({ address }))
}
const sendTransaction = async (to: `0x${string}`, valueEth: string) => {
const { walletClient } = getClients()
const [account] = await walletClient.getAddresses()
return walletClient.sendTransaction({ account, to, value: parseEther(valueEth), chain: null })
}
// ...
}
Use the provider with ethers
import { useWeb3Auth } from '@web3auth/react-native-sdk'
import { ethers } from 'ethers'
function EthersActions() {
const { connection } = useWeb3Auth()
const signMessage = async (message: string) => {
const provider = new ethers.BrowserProvider(connection!.ethereumProvider!)
const signer = await provider.getSigner()
return signer.signMessage(message)
}
// ...
}
Export the private key
caution
Avoid exporting private keys. Sign with the provider instead. Key export works only when it's enabled for your project on the Embedded Wallets dashboard, and not when ERC-4337 smart accounts are enabled.
import { useWeb3Auth } from '@web3auth/react-native-sdk'
function ExportPrivateKey() {
const { connection } = useWeb3Auth()
const exportPrivateKey = async () => {
// Fails if key export is disabled on the dashboard
return connection?.ethereumProvider?.request({ method: 'eth_private_key' })
}
// ...
}