钱包连接
Wallet Connect 客户端(Dart语言),高度借鉴了 Trust Wallet 的 wallet-connect-kotlin。
用法
import 'package:wallet_connect/wallet_connect.dart';
- 创建 Wallet Connect 客户端实例并定义回调。
final wcClient = WCClient(
onConnect: () {
// Respond to connect callback
},
onDisconnect: (code, reason) {
// Respond to disconnect callback
},
onFailure: (error) {
// Respond to connection failure callback
},
onSessionRequest: (id, peerMeta) {
// Respond to connection request callback
},
onEthSign: (id, message) {
// Respond to personal_sign or eth_sign or eth_signTypedData request callback
},
onEthSendTransaction: (id, tx) {
// Respond to eth_sendTransaction request callback
},
onEthSignTransaction: (id, tx) {
// Respond to eth_signTransaction request callback
},
);
- 从 wc: uri 创建 WCSession 对象。
final session = WCSession.from(wcUri);
- 创建 WCPeerMeta 对象,包含您应用的元数据。
final peerMeta = WCPeerMeta(
name: 'Example Wallet',
url: 'https://example.wallet',
description: 'Example Wallet',
icons: [],
);
- 连接到新会话。
wcClient.connectNewSession(session: session, peerMeta: peerMeta);
- 或者连接到已保存的会话(来自步骤 8)。
wcClient.connectFromSessionStore(sessionStore);
- 批准会话连接请求。
wcClient.approveSession(
accounts: [], // account addresses
chainId: 1, // chain id
);
- 或者拒绝会话连接请求。
wcClient.rejectSession();
- 从 sessionStore getter 获取当前会话以供将来使用。
final sessionStore = wcClient.sessionStore;
- 通过签署交易并发送签名后的十六进制数据来批准签名交易请求。
wcClient.approveRequest<String>(
id: id,
result: signedDataAsHex,
);
- 通过发送交易的交易哈希来批准发送交易请求。
wcClient.approveRequest<String>(
id: id,
result: transactionHash,
);
- 通过发送生成的签名十六进制数据来批准签名请求。
wcClient.approveRequest<String>(
id: id,
result: signedDataAsHex,
);
- 或者通过指定请求 ID 来拒绝上述任何请求。
wcClient.rejectRequest(id: id);
- 本地断开已连接的会话。
wcClient.disconnect();
- 永久关闭已连接的会话。
wcClient.killSession();