RPC Methods

imToken 应用内的 Ethereum RPCs 实现说明。

介绍

这是 imToken 应用当前支持的所有 Ethereum RPC 列表,你也可以在 Ethereum RPC 官方文档 内查看更多信息。

imToken 应用的 WebView (DApp 浏览器) 不会注入 web3.js ,我们只提供基础的 Provider 以供连接。关于如何使用 Provider 将应用连接到 imToken,请参考如下:

// 现代应用推荐方式
window.web3 = new Web3(ethereum)

// 或是使用 ethers
await window.ethereum.enable()
const provider = new ethers.providers.Web3Provider(window.ethereum)

已实现的 RPC

eth_requestAccounts

此方法来源于 EIP-1102

请求用户提供一个以太坊地址。

Returns

string[] - 一个单一的、十六进制的以太坊地址字符串的数组。

eth_accounts

此方法来源于 the Ethereum RPCs#eth_accounts

eth_sendTransaction

此方法来源于 the Ethereum RPCs#eth_sendtransaction

personal_sign

别名: personal_sign , eth_signTypedData , eth_signTypedData_v3 eth_signTypedData_v4

对于多种不同的鉴权方式之间的区别和历史原因,请参考 metamask docs (a-brief-history) 文档的解释说明。

此方法 (personal_sign) 近似于 eth_sign,但更安全,可以在 技术参考 中查看详细信息。

此方法来源于 the Ethereum RPCs#eth_sign

net_version

此方法来源于 the Ethereum RPCs#net_version

eth_chainId

此方法来源 EIP-695

wallet_addEthereumChain

此方法来源于 EIP-3085

用于将以太坊链添加到钱包应用程序的 API。

API wallet_addEthereumChain 仅在高于 2.8.4 版本的 imToken 应用内工作。

Returns

null - 如果请求成功,该方法返回 null,否则返回错误。

Params

type EthereumChain = {
  chainId: string
  chainName: string
  nativeCurrency: {
    name: string
    symbol: string // 2-6 characters long
    decimals: number // 18
  };
  rpcUrls: string[]
  blockExplorerUrls?: string[]
}

Example

const chain = {
  chainId: "0x64",
  chainName: "xDAI Chain",
  rpcUrls: ["https://dai.poa.network"],
  iconUrls: [
    "https://xdaichain.com/fake/example/url/xdai.svg",
    "https://xdaichain.com/fake/example/url/xdai.png",
  ],
  nativeCurrency: {
    name: "xDAI",
    symbol: "xDAI",
    decimals: 18,
  },
}

window.ethereum.request({
  method: "wallet_addEthereumChain",
  params: [chain],
})

wallet_switchEthereumChain

此方法来源于 EIP-3326

用于切换当前以太坊网络的 API。

API wallet_switchEthereumChain 仅在高于 2.9.10 版本的 imToken 应用内工作。

Returns

null - 如果请求成功,该方法返回 null,否则返回错误。

Params

type EthereumChain = {
  chainId: string
}

Example

const chain = {
  chainId: "0x64",
}

window.ethereum.request({
  method: "wallet_switchEthereumChain",
  params: [chain],
})

已废弃

enable (废弃的)

使用 ethereum.request({ method: 'eth_requestAccounts' }) 代替。

sendAsync (废弃的)

使用 ethereum.request() 代替。

send (废弃的)

使用 ethereum.request() 代替。

eth_sign (废弃的)

使用 personal_sign 代替。

未实现

eth_subscribe

此 RPC 接口当前未在 imToken 应用内实现。

反馈与帮助

如果你需要请求开发者帮助或反馈问题,请在 GitHub Discussion 中创建一个主题。

最后更新于