跳至正文
Anon Wallet

DApp 集成指南

Anon Wallet 在 window.ethereum 注入标准 EIP-1193 provider。多数已支持 MetaMask 的 DApp 无需改动即可使用。

检测

Anon 在 window.ethereum 设置以下属性:

window.ethereum.isAnon    // true
window.ethereum.isMetaMask  // true (for compatibility)

若要明确识别 Anon 而非 MetaMask,请检查 isAnon:

if (window.ethereum?.isAnon) {
  console.log("Anon Wallet detected");
}

请求账户

const accounts = await window.ethereum.request({
  method: "eth_requestAccounts",
});
// accounts[0] is the user's active public address

用户看到列出 DApp 主机名的授权弹窗,批准后返回账户。

发送交易

const txHash = await window.ethereum.request({
  method: "eth_sendTransaction",
  params: [{
    from: accounts[0],
    to: "0xRecipient...",
    value: "0xde0b6b3a7640000", // 1 ETH in hex wei
    data: "0x",
  }],
});

Anon 显示交易授权弹窗。用户可选择公开余额,或在有屏蔽余额且符合条件时使用私密余额。

签署消息

// Personal sign
const signature = await window.ethereum.request({
  method: "personal_sign",
  params: ["Hello from my DApp", accounts[0]],
});

// EIP-712 typed data
const signature = await window.ethereum.request({
  method: "eth_signTypedData_v4",
  params: [accounts[0], JSON.stringify(typedData)],
});

切换网络

await window.ethereum.request({
  method: "wallet_switchEthereumChain",
  params: [{ chainId: "0xa4b1" }], // Arbitrum
});

若尚未配置请求的链,Anon 提示用户添加。

事件监听

// Account changed
window.ethereum.on("accountsChanged", (accounts) => {
  console.log("Active account:", accounts[0]);
});

// Chain changed
window.ethereum.on("chainChanged", (chainId) => {
  console.log("Chain:", parseInt(chainId, 16));
});

// Connected
window.ethereum.on("connect", ({ chainId }) => {
  console.log("Connected to chain:", chainId);
});

支持的方法

方法 支持情况
eth_requestAccounts ✓
eth_accounts ✓
eth_sendTransaction ✓
eth_signTransaction ✓
personal_sign ✓
eth_signTypedData_v4 ✓
eth_getBalance ✓,增强隐身地址支持
eth_call ✓,增强隐身地址支持
wallet_switchEthereumChain ✓
wallet_addEthereumChain ✓
wallet_getCapabilities ✓,EIP-5792
wallet_sendCalls ✓,EIP-5792
wallet_getCallsStatus ✓,EIP-5792

使用 EIP-5792 批量调用

要使用批量交易、辅助资金等高级能力,请调用 wallet_sendCalls。见批量交易。