Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Apr 19, 2024
1 parent 93c1d05 commit 4253fc3
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 84 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 2.2.3-beta02
## 2.2.3-beta03

- Web Support is here!

Expand Down
2 changes: 1 addition & 1 deletion example/dapp/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:label="dapp"
android:label="Web3Dapp Flutter"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
Expand Down
6 changes: 0 additions & 6 deletions example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class _MyHomePageState extends State<MyHomePage> {
}

// Register event handlers
_web3App!.onSessionConnect.subscribe(_onSessionConnect);
_web3App!.onSessionPing.subscribe(_onSessionPing);
_web3App!.onSessionEvent.subscribe(_onSessionEvent);
_web3App!.onSessionUpdate.subscribe(_onSessionUpdate);
Expand Down Expand Up @@ -134,7 +133,6 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void dispose() {
// Unregister event handlers
_web3App!.onSessionConnect.unsubscribe(_onSessionConnect);
_web3App!.onSessionPing.unsubscribe(_onSessionPing);
_web3App!.onSessionEvent.unsubscribe(_onSessionEvent);
_web3App!.onSessionUpdate.unsubscribe(_onSessionUpdate);
Expand Down Expand Up @@ -256,10 +254,6 @@ class _MyHomePageState extends State<MyHomePage> {
);
}

void _onSessionConnect(SessionConnect? event) {
debugPrint(jsonEncode(event?.session.toJson()));
}

void _onSessionUpdate(SessionUpdate? args) {
debugPrint('[$runtimeType] _onSessionUpdate $args');
}
Expand Down
138 changes: 94 additions & 44 deletions example/dapp/lib/pages/connect_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: use_build_context_synchronously

import 'dart:async';

import 'package:fl_toast/fl_toast.dart';
Expand Down Expand Up @@ -31,6 +33,18 @@ class ConnectPageState extends State<ConnectPage> {
final List<ChainMetadata> _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();
Expand Down Expand Up @@ -75,7 +89,9 @@ class ConnectPageState extends State<ConnectPage> {
: () => _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<Color>(
Expand Down Expand Up @@ -192,7 +208,6 @@ class ConnectPageState extends State<ConnectPage> {
// 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(
Expand All @@ -219,49 +234,10 @@ class ConnectPageState extends State<ConnectPage> {
_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<void> _showQrCode(ConnectResponse response) async {
Expand Down Expand Up @@ -309,6 +285,80 @@ class ConnectPageState extends State<ConnectPage> {
),
);
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion example/dapp/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="dapp">
<meta name="apple-mobile-web-app-title" content="Web3Dapp Flutter">
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<!-- Favicon -->
Expand Down
4 changes: 2 additions & 2 deletions example/dapp/web/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dapp",
"short_name": "dapp",
"name": "Web3Dapp Flutter",
"short_name": "Web3Dapp Flutter",
"start_url": ".",
"display": "standalone",
"background_color": "#0175C2",
Expand Down
1 change: 1 addition & 0 deletions example/wallet/lib/dependencies/deep_link_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 32 additions & 12 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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}');
}
Expand Down Expand Up @@ -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;
}
}
}
15 changes: 0 additions & 15 deletions example/wallet/lib/pages/apps_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -67,20 +66,6 @@ class AppsPageState extends State<AppsPage> 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(() {});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 4253fc3

Please sign in to comment.