Wallet Connect is an open source protocol that allows your wallet to connect and interact with DApps and other wallets. Wallet Connect establishes an encrypted connection between your wallet and DApp by scanning the QR code or using a link. The protocol also has instant notification features to notify users of incoming transactions. You may have seen WalletConnect on Android or iOS mobile wallets like Trust Wallet and MetaMask. A list of cryptocurrency wallet apps that support Wallet Connect can be found here.
Flutter SDK , IDE and Xcode must be installed.
To add the wallet connect package to the project, add the following lines to the pubspec.yaml file:
wallet_connect:
git:
url: https://github.com/test-packages/wallet-connect-dart
You can create dapp connections with the wallet connect package by following the steps below.
- Importing into the project
import 'package:wallet_connect/wallet_connect.dart';
- Handler logic for wallet connect processes
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
},
);
- Approve a session connection request.
wcClient.approveSession(
accounts: [],
chainId: 1,
);
- reject a session connection request.
wcClient.rejectSession();
- Approve a sign transaction request by signing the transaction and sending the signed hex data.
wcClient.approveRequest<String>(
id: id,
result: signedDataAsHex,
);
- Approve a send transaction request by sending the transaction hash generated from sending the transaction.
wcClient.approveRequest<String>(
id: id,
result: transactionHash,
);
- Or reject any of the requests above by specifying request id.
wcClient.rejectRequest(id: id);
- Disconnect from a connected session locally.
wcClient.disconnect();
- Permanently close a connected session.
wcClient.killSession();
- Create WCSession object from wc: uri.
final session = WCSession.from(wcUri);
- Create WCPeerMeta object containing metadata for your app.
final peerMeta = WCPeerMeta(
name: 'Example Wallet',
url: 'https://example.wallet',
description: 'Example Wallet',
icons: [],
);
- Connect to a new session.
wcClient.connectNewSession(session: session, peerMeta: peerMeta);
- Or reject a session connection request.
wcClient.connectFromSessionStore(sessionStore);