22
33import 'dart:async' ;
44
5+ import 'package:event/event.dart' ;
56import 'package:flutter/material.dart' ;
67import 'package:w_common/disposable.dart' ;
78import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart' ;
@@ -58,11 +59,15 @@ class WalletConnectModalService extends ChangeNotifier
5859 @override
5960 String ? get wcUri => connectResponse? .uri.toString ();
6061
61- Map <String , RequiredNamespace > _requiredNamespaces =
62- NamespaceConstants .ethereum;
62+ Map <String , RequiredNamespace > _requiredNamespaces = {};
6363 @override
6464 Map <String , RequiredNamespace > get requiredNamespaces => _requiredNamespaces;
6565
66+ Map <String , RequiredNamespace > _optionalNamespaces =
67+ NamespaceConstants .ethereum;
68+ @override
69+ Map <String , RequiredNamespace > get optionalNamespaces => _optionalNamespaces;
70+
6671 ConnectResponse ? connectResponse;
6772 Future <SessionData >? get sessionFuture => connectResponse? .session.future;
6873 BuildContext ? context;
@@ -83,6 +88,7 @@ class WalletConnectModalService extends ChangeNotifier
8388 String ? projectId,
8489 PairingMetadata ? metadata,
8590 Map <String , RequiredNamespace >? requiredNamespaces,
91+ Map <String , RequiredNamespace >? optionalNamespaces,
8692 Set <String >? recommendedWalletIds,
8793 ExcludedWalletState excludedWalletState = ExcludedWalletState .list,
8894 Set <String >? excludedWalletIds,
@@ -104,6 +110,9 @@ class WalletConnectModalService extends ChangeNotifier
104110 if (requiredNamespaces != null ) {
105111 _requiredNamespaces = requiredNamespaces;
106112 }
113+ if (optionalNamespaces != null ) {
114+ _optionalNamespaces = optionalNamespaces;
115+ }
107116
108117 explorerService.instance = ExplorerService (
109118 projectId: _projectId,
@@ -120,14 +129,14 @@ class WalletConnectModalService extends ChangeNotifier
120129 return ;
121130 }
122131
132+ _registerListeners ();
133+
123134 _initError = null ;
124135 try {
125136 await _web3App! .init ();
126137 await WalletConnectModalServices .init ();
127138 } catch (_) {}
128139
129- _registerListeners ();
130-
131140 if (_web3App! .sessions.getAll ().isNotEmpty) {
132141 _isConnected = true ;
133142 _session = _web3App! .sessions.getAll ().first;
@@ -225,17 +234,17 @@ class WalletConnectModalService extends ChangeNotifier
225234
226235 @override
227236 void close () {
237+ // If we aren't open, then we can't and shouldn't close
228238 if (! _isOpen) {
229239 return ;
230240 }
231- // _isOpen = false;
232241
233242 toastUtils.instance.clear ();
234243 if (context != null ) {
244+ // _isOpen and notifyListeners() are handled when we call Navigator.pop()
245+ // by the open() method
235246 Navigator .pop (context! );
236247 }
237-
238- // notifyListeners();
239248 }
240249
241250 @override
@@ -246,6 +255,15 @@ class WalletConnectModalService extends ChangeNotifier
246255 return ;
247256 }
248257
258+ // Disconnect both the pairing and session
259+ await web3App! .disconnectSession (
260+ topic: session! .pairingTopic,
261+ // ignore: prefer_const_constructors
262+ reason: WalletConnectError (
263+ code: 0 ,
264+ message: 'User disconnected' ,
265+ ),
266+ );
249267 await web3App! .disconnectSession (
250268 topic: session! .topic,
251269 // ignore: prefer_const_constructors
@@ -328,18 +346,31 @@ class WalletConnectModalService extends ChangeNotifier
328346 }
329347
330348 @override
331- void setDefaultChain ({
349+ void setRequiredNamespaces ({
332350 required Map <String , RequiredNamespace > requiredNamespaces,
333351 }) {
334352 _checkInitialized ();
335353
336- LoggerUtil .logger.i ('Setting default chain : $requiredNamespaces ' );
354+ LoggerUtil .logger.i ('Setting required namespaces : $requiredNamespaces ' );
337355
338356 _requiredNamespaces = requiredNamespaces;
339357
340358 notifyListeners ();
341359 }
342360
361+ @override
362+ void setOptionalNamespaces ({
363+ required Map <String , RequiredNamespace > optionalNamespaces,
364+ }) {
365+ _checkInitialized ();
366+
367+ LoggerUtil .logger.i ('Setting optional namespaces: $optionalNamespaces ' );
368+
369+ _optionalNamespaces = optionalNamespaces;
370+
371+ notifyListeners ();
372+ }
373+
343374 @override
344375 String getReferer () {
345376 _checkInitialized ();
@@ -365,6 +396,7 @@ class WalletConnectModalService extends ChangeNotifier
365396
366397 connectResponse = await web3App! .connect (
367398 requiredNamespaces: _requiredNamespaces,
399+ optionalNamespaces: optionalNamespaces,
368400 );
369401
370402 notifyListeners ();
@@ -398,29 +430,50 @@ class WalletConnectModalService extends ChangeNotifier
398430 }
399431
400432 void _registerListeners () {
401- // web3App!.onSessionConnect.subscribe(
402- // _onSessionConnect,
403- // );
433+ web3App! .onSessionConnect.subscribe (
434+ _onSessionConnect,
435+ );
404436 web3App! .onSessionDelete.subscribe (
405437 _onSessionDelete,
406438 );
439+ web3App! .core.relayClient.onRelayClientConnect.subscribe (
440+ _onRelayClientConnect,
441+ );
407442 web3App! .core.relayClient.onRelayClientError.subscribe (
408443 _onRelayClientError,
409444 );
410445 }
411446
412447 void _unregisterListeners () {
413- // web3App!.onSessionConnect.unsubscribe(
414- // _onSessionConnect,
415- // );
448+ web3App! .onSessionConnect.unsubscribe (
449+ _onSessionConnect,
450+ );
416451 web3App! .onSessionDelete.unsubscribe (
417452 _onSessionDelete,
418453 );
454+ web3App! .core.relayClient.onRelayClientConnect.unsubscribe (
455+ _onRelayClientConnect,
456+ );
419457 web3App! .core.relayClient.onRelayClientError.unsubscribe (
420458 _onRelayClientError,
421459 );
422460 }
423461
462+ void _onSessionConnect (SessionConnect ? args) {
463+ print ('Session connected: $args ' );
464+ _isConnected = true ;
465+ _session = args! .session;
466+ _address = NamespaceUtils .getAccount (
467+ _session! .namespaces.values.first.accounts.first,
468+ );
469+
470+ if (_isOpen) {
471+ close ();
472+ } else {
473+ notifyListeners ();
474+ }
475+ }
476+
424477 void _onSessionDelete (SessionDelete ? args) {
425478 _isConnected = false ;
426479 _address = '' ;
@@ -429,9 +482,15 @@ class WalletConnectModalService extends ChangeNotifier
429482 notifyListeners ();
430483 }
431484
485+ void _onRelayClientConnect (EventArgs ? args) {
486+ _initError = null ;
487+
488+ notifyListeners ();
489+ }
490+
432491 void _onRelayClientError (ErrorEvent ? args) {
433- LoggerUtil .logger.e ('Relay client error: $args ' );
434- _initError = args! .error;
492+ LoggerUtil .logger.e ('Relay client error: ${ args ?. error } ' );
493+ _initError = args? .error;
435494
436495 notifyListeners ();
437496 }
@@ -449,18 +508,7 @@ class WalletConnectModalService extends ChangeNotifier
449508 }
450509
451510 try {
452- final SessionData session = await connectResponse! .session.future;
453- _isConnected = true ;
454- _session = session;
455- _address = NamespaceUtils .getAccount (
456- _session! .namespaces.values.first.accounts.first,
457- );
458- // await _toastService.show(
459- // ToastMessage(
460- // type: ToastType.info,
461- // text: 'Connected to Wallet',
462- // ),
463- // );
511+ await connectResponse! .session.future;
464512 } on TimeoutException {
465513 LoggerUtil .logger.i ('Rebuilding session, ending future' );
466514 return ;
@@ -474,12 +522,6 @@ class WalletConnectModalService extends ChangeNotifier
474522 );
475523 return ;
476524 }
477-
478- if (_isOpen) {
479- close ();
480- } else {
481- notifyListeners ();
482- }
483525 }
484526
485527 void _checkInitialized () {
0 commit comments