From 4253fc3fe9a8853cfd25ef4afcbd4b69bad720d9 Mon Sep 17 00:00:00 2001 From: Alfreedom <00tango.bromine@icloud.com> Date: Fri, 19 Apr 2024 18:07:50 +0200 Subject: [PATCH] minor changes --- CHANGELOG.md | 2 +- .../android/app/src/main/AndroidManifest.xml | 2 +- example/dapp/lib/main.dart | 6 - example/dapp/lib/pages/connect_page.dart | 138 ++++++++++++------ example/dapp/web/index.html | 2 +- example/dapp/web/manifest.json | 4 +- .../lib/dependencies/deep_link_handler.dart | 1 + .../lib/dependencies/web3wallet_service.dart | 44 ++++-- example/wallet/lib/pages/apps_page.dart | 15 -- lib/src/version.dart | 2 +- pubspec.yaml | 2 +- 11 files changed, 134 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38e6fa4e..6920ca75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## 2.2.3-beta02 +## 2.2.3-beta03 - Web Support is here! diff --git a/example/dapp/android/app/src/main/AndroidManifest.xml b/example/dapp/android/app/src/main/AndroidManifest.xml index 602aecaf..2b63c4b5 100644 --- a/example/dapp/android/app/src/main/AndroidManifest.xml +++ b/example/dapp/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ { } // Register event handlers - _web3App!.onSessionConnect.subscribe(_onSessionConnect); _web3App!.onSessionPing.subscribe(_onSessionPing); _web3App!.onSessionEvent.subscribe(_onSessionEvent); _web3App!.onSessionUpdate.subscribe(_onSessionUpdate); @@ -134,7 +133,6 @@ class _MyHomePageState extends State { @override void dispose() { // Unregister event handlers - _web3App!.onSessionConnect.unsubscribe(_onSessionConnect); _web3App!.onSessionPing.unsubscribe(_onSessionPing); _web3App!.onSessionEvent.unsubscribe(_onSessionEvent); _web3App!.onSessionUpdate.unsubscribe(_onSessionUpdate); @@ -256,10 +254,6 @@ class _MyHomePageState extends State { ); } - void _onSessionConnect(SessionConnect? event) { - debugPrint(jsonEncode(event?.session.toJson())); - } - void _onSessionUpdate(SessionUpdate? args) { debugPrint('[$runtimeType] _onSessionUpdate $args'); } diff --git a/example/dapp/lib/pages/connect_page.dart b/example/dapp/lib/pages/connect_page.dart index 7f042bf4..91e45881 100644 --- a/example/dapp/lib/pages/connect_page.dart +++ b/example/dapp/lib/pages/connect_page.dart @@ -1,3 +1,5 @@ +// ignore_for_file: use_build_context_synchronously + import 'dart:async'; import 'package:fl_toast/fl_toast.dart'; @@ -31,6 +33,18 @@ class ConnectPageState extends State { final List _selectedChains = []; bool _shouldDismissQrCode = true; + @override + void initState() { + super.initState(); + widget.web3App.onSessionConnect.subscribe(_onSessionConnect); + } + + @override + void dispose() { + widget.web3App.onSessionConnect.unsubscribe(_onSessionConnect); + super.dispose(); + } + void setTestnet(bool value) { if (value != _testnetOnly) { _selectedChains.clear(); @@ -75,7 +89,9 @@ class ConnectPageState extends State { : () => _onConnect(showToast: (m) async { await showPlatformToast(child: Text(m), context: context); }, closeModal: () { - Navigator.of(context).pop(); + if (Navigator.canPop(context)) { + Navigator.of(context).pop(); + } }), style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( @@ -192,7 +208,6 @@ class ConnectPageState extends State { // final uri = 'metamask://wc?uri=$encodedUri'; if (await canLaunchUrlString(uri)) { final openApp = await showDialog( - // ignore: use_build_context_synchronously context: context, builder: (BuildContext context) { return AlertDialog( @@ -219,49 +234,10 @@ class ConnectPageState extends State { _showQrCode(res); } - try { - debugPrint('Awaiting session proposal settlement'); - final _ = await res.session.future; - - showToast?.call(StringConstants.connectionEstablished); + debugPrint('Awaiting session proposal settlement'); + final _ = await res.session.future; - // Send off an auth request now that the pairing/session is established - debugPrint('Requesting authentication'); - final AuthRequestResponse authRes = await widget.web3App.requestAuth( - pairingTopic: res.pairingTopic, - params: AuthRequestParams( - chainId: _selectedChains[0].chainId, - domain: Constants.domain, - aud: Constants.aud, - // statement: 'Welcome to example flutter app', - ), - ); - - debugPrint('Awaiting authentication response'); - final authResponse = await authRes.completer.future; - - if (authResponse.error != null) { - debugPrint('Authentication failed: ${authResponse.error}'); - showToast?.call(StringConstants.authFailed); - } else { - showToast?.call(StringConstants.authSucceeded); - closeModal?.call(); - } - - // ignore: use_build_context_synchronously - if (_shouldDismissQrCode && Navigator.canPop(context)) { - // ignore: use_build_context_synchronously - Navigator.pop(context); - } - } catch (e) { - // ignore: use_build_context_synchronously - if (_shouldDismissQrCode && Navigator.canPop(context)) { - // ignore: use_build_context_synchronously - Navigator.pop(context); - } - showToast?.call(StringConstants.connectionFailed); - closeModal?.call(); - } + showToast?.call(StringConstants.connectionEstablished); } Future _showQrCode(ConnectResponse response) async { @@ -309,6 +285,80 @@ class ConnectPageState extends State { ), ); } + + void _onSessionConnect(SessionConnect? event) async { + if (event == null) return; + + if (_shouldDismissQrCode && Navigator.canPop(context)) { + _shouldDismissQrCode = false; + Navigator.pop(context); + } + + final shouldAuth = await showDialog( + context: context, + barrierDismissible: false, + builder: (BuildContext context) { + return AlertDialog( + insetPadding: const EdgeInsets.all(0.0), + contentPadding: const EdgeInsets.all(0.0), + backgroundColor: Colors.white, + title: const Text('Request Auth?'), + actions: [ + TextButton( + onPressed: () => Navigator.pop(context, false), + child: const Text('Cancel'), + ), + TextButton( + onPressed: () { + Navigator.pop(context, true); + }, + child: const Text('Yes!'), + ), + ], + ); + }, + ); + if (!shouldAuth) return; + + try { + final scheme = event.session.peer.metadata.redirect?.native ?? ''; + launchUrlString(scheme, mode: LaunchMode.externalApplication); + + final pairingTopic = event.session.pairingTopic; + // Send off an auth request now that the pairing/session is established + debugPrint('Requesting authentication'); + final authRes = await widget.web3App.requestAuth( + pairingTopic: pairingTopic, + params: AuthRequestParams( + chainId: _selectedChains[0].chainId, + domain: Constants.domain, + aud: Constants.aud, + // statement: 'Welcome to example flutter app', + ), + ); + + debugPrint('Awaiting authentication response'); + final authResponse = await authRes.completer.future; + + if (authResponse.error != null) { + debugPrint('Authentication failed: ${authResponse.error}'); + showPlatformToast( + child: const Text(StringConstants.authFailed), + context: context, + ); + } else { + showPlatformToast( + child: const Text(StringConstants.authSucceeded), + context: context, + ); + } + } catch (e) { + showPlatformToast( + child: const Text(StringConstants.connectionFailed), + context: context, + ); + } + } } class QRCodeScreen extends StatefulWidget { diff --git a/example/dapp/web/index.html b/example/dapp/web/index.html index c17d9370..bb85c85d 100644 --- a/example/dapp/web/index.html +++ b/example/dapp/web/index.html @@ -23,7 +23,7 @@ - + diff --git a/example/dapp/web/manifest.json b/example/dapp/web/manifest.json index b996394c..760f6ae4 100644 --- a/example/dapp/web/manifest.json +++ b/example/dapp/web/manifest.json @@ -1,6 +1,6 @@ { - "name": "dapp", - "short_name": "dapp", + "name": "Web3Dapp Flutter", + "short_name": "Web3Dapp Flutter", "start_url": ".", "display": "standalone", "background_color": "#0175C2", diff --git a/example/wallet/lib/dependencies/deep_link_handler.dart b/example/wallet/lib/dependencies/deep_link_handler.dart index 2188ee3c..beb9d9af 100644 --- a/example/wallet/lib/dependencies/deep_link_handler.dart +++ b/example/wallet/lib/dependencies/deep_link_handler.dart @@ -50,6 +50,7 @@ class DeepLinkHandler { if (kIsWeb) return; if (scheme.isEmpty) return; await Future.delayed(Duration(milliseconds: delay)); + debugPrint('[WALLET] [DeepLinkHandler] redirecting to $scheme'); try { await launchUrlString(scheme, mode: LaunchMode.externalApplication); } catch (e) { diff --git a/example/wallet/lib/dependencies/web3wallet_service.dart b/example/wallet/lib/dependencies/web3wallet_service.dart index 138df710..d90eb4ef 100644 --- a/example/wallet/lib/dependencies/web3wallet_service.dart +++ b/example/wallet/lib/dependencies/web3wallet_service.dart @@ -86,6 +86,7 @@ class Web3WalletService extends IWeb3WalletService { _web3Wallet!.core.pairing.onPairingInvalid.subscribe(_onPairingInvalid); _web3Wallet!.core.pairing.onPairingCreate.subscribe(_onPairingCreate); _web3Wallet!.onSessionProposal.subscribe(_onSessionProposal); + _web3Wallet!.onSessionConnect.subscribe(_onSessionConnect); _web3Wallet!.onSessionProposalError.subscribe(_onSessionProposalError); _web3Wallet!.onAuthRequest.subscribe(_onAuthRequest); _web3Wallet!.core.relayClient.onRelayClientError.subscribe( @@ -109,6 +110,7 @@ class Web3WalletService extends IWeb3WalletService { _web3Wallet!.core.pairing.onPairingInvalid.unsubscribe(_onPairingInvalid); _web3Wallet!.core.pairing.onPairingCreate.unsubscribe(_onPairingCreate); _web3Wallet!.onSessionProposal.unsubscribe(_onSessionProposal); + _web3Wallet!.onSessionConnect.unsubscribe(_onSessionConnect); _web3Wallet!.onSessionProposalError.unsubscribe(_onSessionProposalError); _web3Wallet!.onAuthRequest.unsubscribe(_onAuthRequest); _web3Wallet!.core.relayClient.onRelayClientError.unsubscribe( @@ -221,30 +223,39 @@ class Web3WalletService extends IWeb3WalletService { } void _onRelayClientMessage(MessageEvent? event) async { - debugPrint('[$runtimeType] [WALLET] _onRelayClientMessage $event'); if (event != null) { - final jsonRpcObject = await EthUtils.decodeMessageEvent(event); - if (jsonRpcObject is JsonRpcRequest) { - if (jsonRpcObject.method != 'wc_sessionDelete' && - jsonRpcObject.method != 'wc_pairingDelete' && - jsonRpcObject.method != 'wc_sessionPing') { + final jsonObject = await EthUtils.decodeMessageEvent(event); + debugPrint('[$runtimeType] [WALLET] _onRelayClientMessage $jsonObject'); + if (jsonObject is JsonRpcRequest) { + if (jsonObject.method != 'wc_sessionDelete' && + jsonObject.method != 'wc_pairingDelete' && + jsonObject.method != 'wc_sessionPing') { DeepLinkHandler.waiting.value = true; } } else { final session = _web3Wallet!.sessions.get(event.topic); final scheme = session?.peer.metadata.redirect?.native ?? ''; + final isSuccess = jsonObject.result != null; + final title = isSuccess ? null : 'Error'; + final message = isSuccess ? null : jsonObject.error?.message ?? ''; DeepLinkHandler.goTo( scheme, - modalTitle: jsonRpcObject.result != null ? null : 'Error', - modalMessage: jsonRpcObject.result != null - ? null - : jsonRpcObject.error?.message ?? 'Error', - success: jsonRpcObject.result != null, + modalTitle: title, + modalMessage: message, + success: isSuccess, ); } } } + void _onSessionConnect(SessionConnect? args) { + debugPrint('[$runtimeType] [WALLET] _onSessionConnect $args'); + if (args != null) { + final scheme = args.session.peer.metadata.redirect?.native ?? ''; + DeepLinkHandler.goTo(scheme); + } + } + void _onRelayClientError(ErrorEvent? args) { debugPrint('[$runtimeType] [WALLET] _onRelayClientError ${args?.error}'); } @@ -297,14 +308,23 @@ class Web3WalletService extends IWeb3WalletService { s: sig, ), ); + final scheme = args.requester.metadata.redirect?.native ?? ''; + DeepLinkHandler.goTo(scheme); } else { await _web3Wallet!.respondAuthRequest( id: args.id, iss: iss, error: Errors.getSdkError(Errors.USER_REJECTED_AUTH), ); + // TODO this should be triggered on _onRelayClientMessage + final scheme = args.requester.metadata.redirect?.native ?? ''; + DeepLinkHandler.goTo( + scheme, + modalTitle: 'Error', + modalMessage: 'User rejected', + success: false, + ); } - DeepLinkHandler.waiting.value = false; } } } diff --git a/example/wallet/lib/pages/apps_page.dart b/example/wallet/lib/pages/apps_page.dart index 897a5e73..a1c39221 100644 --- a/example/wallet/lib/pages/apps_page.dart +++ b/example/wallet/lib/pages/apps_page.dart @@ -8,7 +8,6 @@ import 'package:walletconnect_flutter_v2_wallet/dependencies/deep_link_handler.d import 'package:walletconnect_flutter_v2_wallet/dependencies/i_web3wallet_service.dart'; import 'package:walletconnect_flutter_v2_wallet/pages/app_detail_page.dart'; import 'package:walletconnect_flutter_v2_wallet/utils/constants.dart'; -import 'package:walletconnect_flutter_v2_wallet/utils/eth_utils.dart'; import 'package:walletconnect_flutter_v2_wallet/utils/string_constants.dart'; import 'package:walletconnect_flutter_v2_wallet/widgets/pairing_item.dart'; import 'package:walletconnect_flutter_v2_wallet/widgets/qr_scan_sheet.dart'; @@ -67,20 +66,6 @@ class AppsPageState extends State with GetItStateMixin { } void _refreshState(dynamic event) async { - debugPrint('[WALLET] [$runtimeType] $event'); - if (event is MessageEvent) { - final jsonRpcObject = await EthUtils.decodeMessageEvent(event); - if (jsonRpcObject != null) { - showPlatformToast( - child: Text(jsonRpcObject.toString()), - // ignore: use_build_context_synchronously - context: context, - ); - } - } - if (event is SessionConnect) { - DeepLinkHandler.waiting.value = false; - } setState(() {}); } diff --git a/lib/src/version.dart b/lib/src/version.dart index 8ef9b083..60da050d 100644 --- a/lib/src/version.dart +++ b/lib/src/version.dart @@ -1,2 +1,2 @@ // Generated code. Do not modify. -const packageVersion = '2.2.3-beta02'; +const packageVersion = '2.2.3-beta03'; diff --git a/pubspec.yaml b/pubspec.yaml index 30eed754..dc3cc0b0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: walletconnect_flutter_v2 description: This repository contains oficial implementation of WalletConnect v2 protocols for Flutter applications. The communications protocol for web3. -version: 2.2.3-beta02 +version: 2.2.3-beta03 repository: https://github.com/WalletConnect/WalletConnectFlutterV2 environment: