|
| 1 | +# WalletConnect 安装和使用指南 |
| 2 | + |
| 3 | +## 安装 |
| 4 | + |
| 5 | +在 DApp 中使用 WalletConnect 时,可以安装 WalletConnect JavaScript 客户端库: |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @walletconnect/client |
| 9 | +``` |
| 10 | + |
| 11 | +对于钱包提供方,需要集成 WalletConnect SDK(提供 iOS、Android 和 Web 版本)。 |
| 12 | + |
| 13 | +## 基本使用 |
| 14 | + |
| 15 | +### 步骤 1:初始化 WalletConnect 客户端 |
| 16 | + |
| 17 | +在 JavaScript/TypeScript 代码中,导入并初始化 WalletConnect 客户端: |
| 18 | + |
| 19 | +```javascript |
| 20 | +import WalletConnect from "@walletconnect/client"; |
| 21 | +import QRCodeModal from "@walletconnect/qrcode-modal"; |
| 22 | + |
| 23 | +// 创建 WalletConnect 客户端实例 |
| 24 | +const connector = new WalletConnect({ |
| 25 | + bridge: "https://bridge.walletconnect.org", // 必填项 |
| 26 | + qrcodeModal: QRCodeModal, |
| 27 | +}); |
| 28 | +``` |
| 29 | + |
| 30 | +### 步骤 2:检查连接并创建会话 |
| 31 | + |
| 32 | +如果没有已建立的连接,则创建一个新会话,提示用户连接钱包。 |
| 33 | + |
| 34 | +```javascript |
| 35 | +// 检查是否已连接 |
| 36 | +if (!connector.connected) { |
| 37 | + // 创建新会话 |
| 38 | + await connector.createSession(); |
| 39 | +} |
| 40 | + |
| 41 | +// 监听会话事件 |
| 42 | +connector.on("connect", (error, payload) => { |
| 43 | + if (error) { |
| 44 | + throw error; |
| 45 | + } |
| 46 | + |
| 47 | + // 连接成功 |
| 48 | + const { accounts, chainId } = payload.params[0]; |
| 49 | +}); |
| 50 | + |
| 51 | +connector.on("session_update", (error, payload) => { |
| 52 | + if (error) { |
| 53 | + throw error; |
| 54 | + } |
| 55 | + |
| 56 | + // 更新的账户和链ID |
| 57 | + const { accounts, chainId } = payload.params[0]; |
| 58 | +}); |
| 59 | + |
| 60 | +connector.on("disconnect", (error, payload) => { |
| 61 | + if (error) { |
| 62 | + throw error; |
| 63 | + } |
| 64 | + |
| 65 | + // 已断开连接 |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +### 步骤 3:发送交易 |
| 70 | + |
| 71 | +连接成功后,即可与区块链进行交互。例如,发送一笔交易: |
| 72 | + |
| 73 | +```javascript |
| 74 | +// 设置交易详情 |
| 75 | +const tx = { |
| 76 | + from: accounts[0], // 用户地址 |
| 77 | + to: "0xRecipientAddress", |
| 78 | + value: "0xAmountInWei", // 金额,单位为 wei |
| 79 | + data: "0x", // 可选数据 |
| 80 | +}; |
| 81 | + |
| 82 | +// 发送交易请求 |
| 83 | +const result = await connector.sendTransaction(tx); |
| 84 | +``` |
| 85 | + |
| 86 | +## 高级配置 |
| 87 | + |
| 88 | +### 多链支持 |
| 89 | + |
| 90 | +要支持多个区块链网络,可以在初始化时配置: |
| 91 | + |
| 92 | +```javascript |
| 93 | +const connector = new WalletConnect({ |
| 94 | + bridge: "https://bridge.walletconnect.org", |
| 95 | + qrcodeModal: QRCodeModal, |
| 96 | + chainId: 1, // 默认以太坊主网 |
| 97 | + supportedChainIds: [1, 56, 137] // 支持的链ID:以太坊、BSC、Polygon |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +### 自定义配置 |
| 102 | + |
| 103 | +```javascript |
| 104 | +const connector = new WalletConnect({ |
| 105 | + bridge: "https://your-custom-bridge.com", |
| 106 | + qrcodeModal: QRCodeModal, |
| 107 | + clientMeta: { |
| 108 | + name: "Your DApp", |
| 109 | + description: "Your DApp Description", |
| 110 | + url: "https://your-dapp.com", |
| 111 | + icons: ["https://your-dapp.com/icon.png"] |
| 112 | + } |
| 113 | +}); |
| 114 | +``` |
| 115 | + |
| 116 | +## 错误处理最佳实践 |
| 117 | + |
| 118 | +### 连接错误处理 |
| 119 | + |
| 120 | +```javascript |
| 121 | +connector.on("connect", (error, payload) => { |
| 122 | + if (error) { |
| 123 | + console.error("连接错误:", error); |
| 124 | + // 实现重试逻辑 |
| 125 | + return; |
| 126 | + } |
| 127 | + // 连接成功处理 |
| 128 | +}); |
| 129 | + |
| 130 | +// 自定义错误处理 |
| 131 | +try { |
| 132 | + await connector.createSession(); |
| 133 | +} catch (error) { |
| 134 | + if (error.message.includes("User rejected")) { |
| 135 | + console.log("用户拒绝连接"); |
| 136 | + } else { |
| 137 | + console.error("创建会话失败:", error); |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### 交易错误处理 |
| 143 | + |
| 144 | +```javascript |
| 145 | +try { |
| 146 | + const result = await connector.sendTransaction(tx); |
| 147 | +} catch (error) { |
| 148 | + if (error.message.includes("insufficient funds")) { |
| 149 | + console.error("余额不足"); |
| 150 | + } else if (error.message.includes("gas")) { |
| 151 | + console.error("Gas 费用估算失败"); |
| 152 | + } else { |
| 153 | + console.error("交易失败:", error); |
| 154 | + } |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +## 安全性考虑 |
| 159 | + |
| 160 | +### 数据加密 |
| 161 | + |
| 162 | +- 确保所有敏感数据在传输前进行加密 |
| 163 | +- 不要在客户端存储私钥或助记词 |
| 164 | +- 使用安全的通信协议(HTTPS) |
| 165 | + |
| 166 | +### 交易安全 |
| 167 | + |
| 168 | +- 在发送交易前进行金额和地址验证 |
| 169 | +- 实现交易确认对话框 |
| 170 | +- 显示详细的交易信息供用户确认 |
| 171 | + |
| 172 | +### 会话管理 |
| 173 | + |
| 174 | +- 定期检查会话状态 |
| 175 | +- 实现自动断开连接的超时机制 |
| 176 | +- 在用户离开应用时清理会话数据 |
| 177 | + |
| 178 | +## 性能优化 |
| 179 | + |
| 180 | +- 使用 WebSocket 保持连接状态 |
| 181 | +- 实现重连机制 |
| 182 | +- 缓存常用数据 |
| 183 | +- 批量处理交易请求 |
| 184 | + |
| 185 | +## 常见问题 |
| 186 | + |
| 187 | +- **二维码未显示**:请确保 `QRCodeModal` 正确导入并配置。 |
| 188 | +- **连接问题**:请检查桥接 URL,默认桥接为 `"https://bridge.walletconnect.org"`,也支持自托管桥接。 |
| 189 | +- **断开连接**:检查网络状态,确保 WebSocket 连接正常。 |
| 190 | +- **交易失败**:验证账户余额、Gas 费用设置是否合理。 |
| 191 | +- **兼容性问题**:确认钱包版本是否支持当前协议版本。 |
0 commit comments