> For the complete documentation index, see [llms.txt](https://imtoken.gitbook.io/developers/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://imtoken.gitbook.io/developers/zh/products/webview/rpcs.md).

# RPC Methods

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

## 介绍

这是 imToken 应用当前支持的所有 Ethereum RPC 列表，你也可以在 [Ethereum RPC 官方文档](https://eth.wiki/json-rpc/API#json-rpc-methods) 内查看更多信息。

imToken 应用的 WebView (DApp 浏览器) [不会注入 `web3.js`](/developers/zh/products/webview/release-changes.md#bu-zai-zhu-ru-web-3-js) ，我们只提供基础的 `Provider` 以供连接。关于如何使用 `Provider` 将应用连接到 imToken，请参考如下：

* imToken 的 [新手指引文档](/developers/zh/products/webview/development-guide-for-imtoken-dapp.md)
* MetaMask 官方文档: [Ethereum Provider](https://docs.metamask.io/guide/ethereum-provider.html)
* [EIP-1193 定义的 API 说明](https://eips.ethereum.org/EIPS/eip-1193)
* 或是参考下述的简单示例代码：

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

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

## 已实现的 RPC

#### eth\_requestAccounts

> 此方法来源于 [EIP-1102](https://eips.ethereum.org/EIPS/eip-1102)。

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

**Returns**

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

#### eth\_accounts

> 此方法来源于 [the Ethereum RPCs#eth\_accounts](https://eth.wiki/json-rpc/API#eth_accounts)。

#### eth\_sendTransaction

> 此方法来源于 [the Ethereum RPCs#eth\_sendtransaction](https://eth.wiki/json-rpc/API#eth_sendtransaction)。

#### personal\_sign

**别名:** `personal_sign` , `eth_signTypedData` , `eth_signTypedData_v3` `eth_signTypedData_v4`

对于多种不同的鉴权方式之间的区别和历史原因，请参考 [metamask docs (a-brief-history)](https://docs.metamask.io/guide/signing-data.html#a-brief-history) 文档的解释说明。

此方法 (`personal_sign`) 近似于 `eth_sign`，但更安全，可以在 [技术参考](https://github.com/ethereum/go-ethereum/pull/2940) 中查看详细信息。

> 此方法来源于 [the Ethereum RPCs#eth\_sign](https://eth.wiki/json-rpc/API#eth_sign)。

#### net\_version

> 此方法来源于 [the Ethereum RPCs#net\_version](https://eth.wiki/json-rpc/API#net_version)。

#### eth\_chainId

> 此方法来源 [EIP-695](https://eips.ethereum.org/EIPS/eip-695)。

#### wallet\_addEthereumChain

> 此方法来源于 [EIP-3085](https://eips.ethereum.org/EIPS/eip-3085)。

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

{% hint style="warning" %}
API `wallet_addEthereumChain` 仅在高于 `2.8.4` 版本的 imToken 应用内工作。
{% endhint %}

**Returns**

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

**Params**

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

**Example**

```typescript
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](https://eips.ethereum.org/EIPS/eip-3326)。

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

{% hint style="warning" %}
API `wallet_switchEthereumChain` 仅在高于 `2.9.10` 版本的 imToken 应用内工作。
{% endhint %}

**Returns**

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

**Params**

```typescript
type EthereumChain = {
  chainId: string
}
```

**Example**

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

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

### 已废弃

#### enable (废弃的) <a href="#ethereum-enable-deprecated" id="ethereum-enable-deprecated"></a>

{% hint style="warning" %}
使用 `ethereum.request({ method: 'eth_requestAccounts' })` 代替。
{% endhint %}

#### sendAsync (废弃的) <a href="#ethereum-sendasync-deprecated" id="ethereum-sendasync-deprecated"></a>

{% hint style="warning" %}
使用 `ethereum.request()` 代替。
{% endhint %}

#### send (废弃的) <a href="#ethereum-sendasync-deprecated" id="ethereum-sendasync-deprecated"></a>

{% hint style="warning" %}
使用 `ethereum.request()` 代替。
{% endhint %}

#### eth\_sign (废弃的)

{% hint style="warning" %}
使用 `personal_sign` 代替。
{% endhint %}

### 未实现

#### eth\_subscribe

此 RPC 接口当前未在 imToken 应用内实现。&#x20;

## 反馈与帮助

如果你需要请求开发者帮助或反馈问题，请在 [GitHub Discussion](https://github.com/consenlabs/webview/discussions/new) 中创建一个主题。
