Skip to content

Commit f2ebbd1

Browse files
author
Sterling Long
committed
2.1.3 -> 2.1.4, optional namespaces, network error reconnection
1 parent df5f80d commit f2ebbd1

14 files changed

Lines changed: 209 additions & 103 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 2.0.0
2+
3+
- Added setOptionalNamespaces to WalletConnectModalService
4+
- Renamed setDefaultNamespaces to setRequiredNamespaces
5+
- Network Error (Default connect button) now attempts to reconnect if you click on it
6+
17
## 1.2.4
28

39
- Added small size constraint back to WalletConnectModalConnect button

example/sign/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class _MyAppState extends State<MyApp> {
3636
DeviceOrientation.portraitDown,
3737
]);
3838

39-
LoggerUtil.setLogLevel(Level.verbose);
39+
LoggerUtil.setLogLevel(Level.info);
4040
}
4141

4242
// This widget is the root of your application.

example/sign/lib/pages/sign_page.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ class SignPageState extends State<SignPage>
8787

8888
await _walletConnectModalService?.init();
8989

90+
for (final PairingInfo p
91+
in _walletConnectModalService!.web3App!.pairings.getAll()) {
92+
LoggerUtil.logger.i('Deleting pairing: ${p.topic}');
93+
await _walletConnectModalService!.web3App!.pairings.delete(p.topic);
94+
}
95+
9096
if (_walletConnectModalService!.isInitialized) {
9197
// Loop through all the chain data
9298
for (final ChainMetadata chain in ChainData.allChains) {
@@ -369,8 +375,8 @@ class SignPageState extends State<SignPage>
369375
.i('Updated Required Namespaces, namespaces: $requiredNamespaces');
370376
LoggerUtil.logger
371377
.i('Updated Required Namespaces, service: $_walletConnectModalService');
372-
_walletConnectModalService?.setDefaultChain(
373-
requiredNamespaces: requiredNamespaces,
378+
_walletConnectModalService?.setOptionalNamespaces(
379+
optionalNamespaces: requiredNamespaces,
374380
);
375381
}
376382

lib/constants/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22

33
class WalletConnectModalConstants {
4-
static const WALLETCONNECT_MODAL_VERSION = '1.2.4';
4+
static const WALLETCONNECT_MODAL_VERSION = '2.0.0';
55

66
static const Key helpPageKey = Key('helpPageKey');
77
static const Key qrCodePageKey = Key('qrCodePageKey');

lib/constants/string_constants.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class StringConstants {
55

66
// TEXT
77
static const String connectButtonError = 'Network Error';
8+
static const String connectButtonReconnecting = 'Reconnecting';
89
static const String connectButtonIdle = 'Connect Wallet';
910
static const String connectButtonConnecting = 'Connecting...';
1011
static const String connectButtonConnected = 'Disconnect';
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import 'package:logger/logger.dart';
22

33
class LoggerUtil {
4-
static final Logger logger = Logger(
4+
static Logger logger = Logger(
55
level: Level.nothing,
66
printer: PrettyPrinter(),
77
);
88

99
static void setLogLevel(Level level) {
10-
Logger.level = level;
10+
logger = Logger(
11+
level: level,
12+
printer: PrettyPrinter(
13+
methodCount: 10,
14+
),
15+
);
1116
}
1217
}

lib/services/walletconnect_modal/i_walletconnect_modal_service.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,23 @@ abstract class IWalletConnectModalService implements ChangeNotifier {
6262
/// The required namespaces that will be used when connecting to the wallet
6363
Map<String, RequiredNamespace> get requiredNamespaces;
6464

65+
/// The optional namespaces that will be used when connecting to the wallet
66+
Map<String, RequiredNamespace> get optionalNamespaces;
67+
6568
/// Sets the required namespaces that will be used when connecting to the wallet
66-
/// The default is set to the [NamespaceConstants.ethereum] namespace.
67-
void setDefaultChain({
69+
/// The default is blank. If you are building a multichain dApp it is recommended
70+
/// you use the optional namespaces instead so you dApp can function with more
71+
/// wallets.
72+
void setRequiredNamespaces({
6873
required Map<String, RequiredNamespace> requiredNamespaces,
6974
});
7075

76+
/// Sets the optional namespaces that will be used when connecting to the wallet
77+
/// The default is set to the [NamespaceConstants.ethereum] namespace.
78+
void setOptionalNamespaces({
79+
required Map<String, RequiredNamespace> optionalNamespaces,
80+
});
81+
7182
/// Rebuilds the connection URI.
7283
/// If the dapp attempts to connect to a wallet, and the connection proposal is consumed,
7384
/// but not accepted or rejected (no response), and they navigate back to the dapp and try again

lib/services/walletconnect_modal/walletconnect_modal_service.dart

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import 'dart:async';
44

5+
import 'package:event/event.dart';
56
import 'package:flutter/material.dart';
67
import 'package:w_common/disposable.dart';
78
import '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() {

lib/widgets/walletconnect_modal_button.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ class WalletConnectModalButton extends StatelessWidget {
77
super.key,
88
required this.child,
99
this.onPressed,
10+
this.disabled = false,
1011
this.borderRadius = 100,
1112
});
1213

1314
final Widget child;
1415
final void Function()? onPressed;
16+
final bool disabled;
1517
final double borderRadius;
1618

1719
@override
@@ -20,9 +22,9 @@ class WalletConnectModalButton extends StatelessWidget {
2022
WalletConnectModalTheme.getData(context);
2123

2224
// No onPressed means the button is disabled and grayed out.
23-
if (onPressed == null) {
25+
if (disabled) {
2426
return MaterialButton(
25-
onPressed: () {},
27+
onPressed: onPressed ?? () {},
2628
color: themeData.overlay030,
2729
disabledColor: themeData.overlay030,
2830
shape: RoundedRectangleBorder(

0 commit comments

Comments
 (0)