4.1 KiB

title, sidebar_position
title sidebar_position
Ethereum Integration 3

Ethereum Integration

Complete guide for integrating Oko with Ethereum and EVM-compatible chains.

:::tip Get started faster Prefer a ready-to-run example? Try the EVM (Next.js) starter template. :::

Installation

npm install @oko-wallet/oko-sdk-eth

Basic Setup

import { OkoEthWallet } from "@oko-wallet/oko-sdk-eth";
import { createWalletClient, custom } from "viem";
import { mainnet } from "viem/chains";

// Initialize eth wallet
const initRes = OkoEthWallet.init({
  api_key: "your-api-key",
});

if (!initRes.success) {
  throw new Error(`Eth wallet initialization failed: ${initRes.err}`);
}

const ethWallet = initRes.data;
const provider = await ethWallet.getEthereumProvider();

On-chain Signing

Send Transaction

import { parseEther } from "viem";

const recipientAddress = "0x...";

const [account] = await provider.request({ method: "eth_accounts" });
const hash = await provider.request({
  method: "eth_sendTransaction",
  params: [
    {
      from: account,
      to: recipientAddress,
      value: parseEther("0.1"),
      gas: BigInt(21000),
    },
  ],
});

Off-chain Signing

Personal Message

import { toHex } from "viem";

const message = toHex("Welcome to Oko! 🚀");

const [account] = await provider.request({ method: "eth_accounts" });
const signature = await provider.request({
  method: "personal_sign",
  params: [message, account],
});

EIP-712 Typed Data

const typedData = {
  domain: {
    name: "Example DApp",
    version: "1",
    chainId: 1,
    verifyingContract: "0x742d35Cc6634C0532925a3b8D8967d01B41cdB88",
  },
  types: {
    Person: [
      { name: "name", type: "string" },
      { name: "wallet", type: "address" },
    ],
  },
  primaryType: "Person",
  message: {
    name: "Alice",
    wallet: "0x742d35Cc6634C0532925a3b8D8967d01B41cdB88",
  },
} as const;

const signature = await provider.request({
  method: "eth_signTypedData_v4",
  params: ["0x742d35Cc6634C0532925a3b8D8967d01B41cdB88", typedData],
});

Multi-Chain Support

// Add chain
await provider.request({
  method: "wallet_addEthereumChain",
  params: [
    {
      chainId: "1",
      chainName: "Ethereum Mainnet",
      rpcUrls: ["https://rpc.ankr.com/eth"],
      nativeCurrency: {
        name: "Ethereum",
        symbol: "ETH",
        decimals: 18,
      },
    },
  ],
});

// Switch chains
await provider.request({
  method: "wallet_switchEthereumChain",
  params: [{ chainId: toHex(1) }],
});

Viem Integration

import { createPublicClient, createWalletClient, custom } from "viem";
import { mainnet } from "viem/chains";

const publicClient = createPublicClient({
  chain: mainnet,
  transport: custom(provider),
});

const walletClient = createWalletClient({
  chain: mainnet,
  transport: custom(provider),
});

Contract Interactions

// Deploy contract
const deployHash = await walletClient.deployContract({
  account: userAddress,
  abi: contractABI,
  bytecode: contractBytecode,
});

// Call contract function
const callHash = await walletClient.sendTransaction({
  account: userAddress,
  to: contractAddress,
  data: encodeFunctionData({
    abi: ERC20ContractABI,
    functionName: "transfer",
    args: [toAddress, amount],
  }),
});

Ethers Integration

V5

const provider = await ethWallet.getEthereumProvider();
const ethersProvider = new ethers.providers.Web3Provider(provider);

V6

const provider = await ethWallet.getEthereumProvider();
const ethersProvider = new ethers.BrowserProvider(provider);
const signer = await ethersProvider.getSigner();

Next Steps