Skip to content

JSON-RPC Account

A function to create a JSON-RPC Account.

A JSON-RPC Account defers signing of transactions & messages to the target Wallet over JSON-RPC. An example could be sending a transaction via a Browser Extension Wallet (e.g. MetaMask) with the window.ethereum Provider.

Usage

A JSON-RPC Account can just be initialized as an Address string. In the usage below, we are extracting the address from a Browser Extension Wallet (e.g. MetaMask) with the window.ethereum Provider via eth_requestAccounts:

import 'viem/window'
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
 
const [address] = await window.ethereum.request({ 
  method: 'eth_requestAccounts' 
})
 
const client = createWalletClient({
  account: address, 
  chain: mainnet,
  transport: custom(window.ethereum!)
})