React Native theorytheory 0/50 · 0%
Web3 integration · medium

31. Signing transactions with ethers/viem

Building and signing transactions in a mobile wallet.

Libraries like `ethers` or `viem` work in React Native (with a few polyfills for crypto primitives) to build, sign and broadcast transactions from a locally-held private key or a hardware-backed signer.

import { ethers } from "ethers";

async function sendEth(privateKey: string, to: string, amountEth: string, provider: ethers.Provider) {
  const wallet = new ethers.Wallet(privateKey, provider);
  const tx = await wallet.sendTransaction({ to, value: ethers.parseEther(amountEth) });
  return tx.hash;
}

React Native lacks some Node built-ins (like `crypto`), so projects typically add `react-native-get-random-values` and other polyfills before importing these libraries.

Check your understanding

  1. 1. What must you typically polyfill in React Native for crypto libraries?

  2. 2. What does ethers.parseEther do?

  3. 3. What does wallet.sendTransaction return?