钱包客户端
钱包客户端是一个用于与一以太坊账户交互的接口,它通过钱包操作提供恢复账户,执行交易,签名消息等功能。
使用createWalletClient 函数创建一个钱包客户端,并赋予一个Transport。
钱包客户端支持签名:
- JSON-RPC账户(例如: 浏览器扩展钱包、WalletConnect等)。
- 本地账户(例如: 私钥/助记词钱包)。
导入
import { createWalletClient } from 'viem'import { createWalletClient } from 'viem'JSON-RPC账户
JSON-RPC账户 推迟通过JSON-RPC将交易和消息签名到目标钱包。 一个例子是通过浏览器扩展钱包(例如 MetaMask)使用window.ethereum发送交易。
下面是一个设置JSON-RPC账户的示例:
1:初始化一个钱包客户端
在我们设置账户和开始使用钱包操作之前,我们需要用custom Transport来设置好钱包客户端,并在里面传入window.ethereum Provider:
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})2: 设置JSON-RPC账户
我们需要检索可以在钱包中访问的地址(例如 MetaMask)。
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
const [address] = await client.getAddresses()
// or: const [address] = await client.requestAddresses() import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
const [address] = await client.getAddresses()
// or: const [address] = await client.requestAddresses() 注意:有一些钱包(如metamask)可能需要先通过
client.requestAddresses请求账户地址 Note: Some Wallets (like MetaMask) may require you to request access to Account addresses viaclient.requestAddressesfirst.
3: 使用钱包操作
现在可以在需要用户签名的钱包操作中使用该地址:
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
const [address] = await client.getAddresses()
const hash = await client.sendTransaction({
account: address,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})
const [address] = await client.getAddresses()
const hash = await client.sendTransaction({
account: address,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})可选项:提升帐户
如果你不希望在每次请求account操作的时候都传入账户,你可以提升账户到钱包客户端
import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const [account] = await window.ethereum.request({ method: 'eth_requestAccounts' })
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const [account] = await window.ethereum.request({ method: 'eth_requestAccounts' })
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})本地账户(私钥,助记词等)
本地帐户在通过JSON-RPC执行方法之前使用私钥执行事务和消息的签名。
在viem中使用本地账户有下面三种方法:
下面是集成私钥账户的步骤,分层确定性(HD)账户的设置方法也是一样的
1: 初始化钱包客户端
在设置账户和开始使用钱包操作之前,需要http Transport来设置钱包客户端:
import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: http()
})import { createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: http()
})2: 设置本地账户
下面使用privateKeyToAccount来实例化一个私钥帐户:
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: http()
})
const account = privateKeyToAccount('0x...') import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: http()
})
const account = privateKeyToAccount('0x...') 3: 使用 钱包操作
现在可以在需要用户签名的钱包操作中使用设好的帐户:
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: http()
})
const account = privateKeyToAccount('0x...')
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const client = createWalletClient({
chain: mainnet,
transport: http()
})
const account = privateKeyToAccount('0x...')
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})可选项:提升帐户
如果你不希望在每次请求account操作的时候都传入账户,你可以提升账户到钱包客户端
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const account = privateKeyToAccount('0x...')
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
const account = privateKeyToAccount('0x...')
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
const hash = await client.sendTransaction({
account,
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})可选的:通过公共操作进行扩展
在使用本地帐户时,可能需要使用与钱包客户端相同的参数(transport、chain等)实例化的公共客户端。
在这种情况下,可以使用公共操作来扩展钱包客户端,这样可以避免处理多个客户端。
import { createWalletClient, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'
const account = privateKeyToAccount('0x...')
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
}).extend(publicActions)
const { request } = await client.simulateContract({ ... }) // Public Action
const hash = await client.writeContract(request) // Wallet Actionimport { createWalletClient, http, publicActions } from 'viem'
import { mainnet } from 'viem/chains'
const account = privateKeyToAccount('0x...')
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
}).extend(publicActions)
const { request } = await client.simulateContract({ ... }) // Public Action
const hash = await client.writeContract(request) // Wallet Action参数
account(可选的)
- Type:
Account | Address
用于钱包客户端的账户。这将用于需要account作为参数的操作。
接受JSON-RPC账户 或者 本地账户(私钥等)。
import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
const client = createWalletClient({
account: privateKeyToAccount('0x...')
key: 'foo',
transport: custom(window.ethereum)
})
const hash = await client.sendTransaction({
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})import { createWalletClient, custom } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
const client = createWalletClient({
account: privateKeyToAccount('0x...')
key: 'foo',
transport: custom(window.ethereum)
})
const hash = await client.sendTransaction({
to: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
value: parseEther('0.001')
})chain(可选的)
- Type: Chain
钱包客户端中的Chain。
用sendTransaction 和 writeContract 来断言该链与钱包的活动链相匹配的操作。
const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})const client = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum)
})cacheTime(可选的)
- Type:
number - Default:
client.pollingInterval
缓存数据在内存中保留的时间(以毫秒为单位)。
const client = createWalletClient({
cacheTime: 10_000,
chain: mainnet,
transport: custom(window.ethereum)
})const client = createWalletClient({
cacheTime: 10_000,
chain: mainnet,
transport: custom(window.ethereum)
})key(可选的)
- Type:
string - Default:
"wallet"
客户端中的key
import { createWalletClient, custom } from 'viem'
const client = createWalletClient({
key: 'foo',
transport: custom(window.ethereum)
})import { createWalletClient, custom } from 'viem'
const client = createWalletClient({
key: 'foo',
transport: custom(window.ethereum)
})name(可选的)
- Type:
string - Default:
"Wallet Client"
客户端中的name
import { createWalletClient, custom } from 'viem'
const client = createWalletClient({
name: 'Foo Wallet Client',
transport: custom(window.ethereum)
})import { createWalletClient, custom } from 'viem'
const client = createWalletClient({
name: 'Foo Wallet Client',
transport: custom(window.ethereum)
})pollingInterval(可选的)
- Type:
number - Default:
4_000
轮询启用的操作的间隔时间(毫秒)
import { createWalletClient, custom } from 'viem'
const client = createWalletClient({
pollingInterval: 10_000,
transport: custom(window.ethereum)
})import { createWalletClient, custom } from 'viem'
const client = createWalletClient({
pollingInterval: 10_000,
transport: custom(window.ethereum)
})