Skip to content

Commit 7b076a8

Browse files
author
Sterling Long
committed
Disconnect all sessions, 2.1.11
1 parent b65e95e commit 7b076a8

8 files changed

Lines changed: 148 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.11
2+
3+
- `WalletConnectModalService.disconnect` now disconnects all sessions, unless you tell it to only disconnect the current one
4+
15
## 2.1.10
26

37
- Fixed an issue with disconnect not working properly

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 = '2.1.10';
4+
static const WALLETCONNECT_MODAL_VERSION = '2.1.11';
55

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

lib/services/walletconnect_modal/i_walletconnect_modal_service.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ abstract class IWalletConnectModalService implements ChangeNotifier {
5555

5656
/// Disconnects the session and pairing, if any.
5757
/// If there is no session, this does nothing.
58-
Future<void> disconnect();
58+
Future<void> disconnect({
59+
bool disconnectAllSessions = true,
60+
});
5961

6062
Future<void> launchCurrentWallet();
6163

lib/services/walletconnect_modal/walletconnect_modal_service.dart

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ class WalletConnectModalService extends ChangeNotifier
262262
}
263263

264264
@override
265-
Future<void> disconnect() async {
265+
Future<void> disconnect({
266+
bool disconnectAllSessions = true,
267+
}) async {
266268
checkInitialized();
267269

268270
// If we don't have a session, disconnect automatically and notify listeners
@@ -273,31 +275,14 @@ class WalletConnectModalService extends ChangeNotifier
273275
return;
274276
}
275277

276-
// Disconnect both the pairing and session
277-
await web3App!.disconnectSession(
278-
topic: session!.pairingTopic,
279-
// ignore: prefer_const_constructors
280-
reason: WalletConnectError(
281-
code: 0,
282-
message: 'User disconnected',
283-
),
284-
);
285-
// Disconnecting the session will produce the onSessionDisconnect callback
286-
await web3App!.disconnectSession(
287-
topic: session!.topic,
288-
// ignore: prefer_const_constructors
289-
reason: WalletConnectError(
290-
code: 0,
291-
message: 'User disconnected',
292-
),
293-
);
294-
295-
// As a failsafe (If the session is expired for example), set the session to null and notify listeners
296-
if (_session != null) {
297-
_isConnected = false;
298-
_address = '';
299-
_session = null;
300-
notifyListeners();
278+
// If we want to disconnect all sessions, loop through them and disconnect them
279+
if (disconnectAllSessions) {
280+
for (final SessionData session in web3App!.sessions.getAll()) {
281+
await disconnectSession(session);
282+
}
283+
} else {
284+
// Disconnect the session
285+
await disconnectSession(_session!);
301286
}
302287
}
303288

@@ -442,6 +427,36 @@ class WalletConnectModalService extends ChangeNotifier
442427

443428
////// Private methods //////
444429
430+
@protected
431+
Future<void> disconnectSession(SessionData toDisconnect) async {
432+
// Disconnect both the pairing and session
433+
await web3App!.disconnectSession(
434+
topic: toDisconnect.pairingTopic,
435+
// ignore: prefer_const_constructors
436+
reason: WalletConnectError(
437+
code: 0,
438+
message: 'User disconnected',
439+
),
440+
);
441+
// Disconnecting the session will produce the onSessionDisconnect callback
442+
await web3App!.disconnectSession(
443+
topic: toDisconnect.topic,
444+
// ignore: prefer_const_constructors
445+
reason: WalletConnectError(
446+
code: 0,
447+
message: 'User disconnected',
448+
),
449+
);
450+
451+
// As a failsafe (If the session is expired for example), set the session to null and notify listeners
452+
if (_session != null && session!.topic == toDisconnect.topic) {
453+
_isConnected = false;
454+
_address = '';
455+
_session = null;
456+
notifyListeners();
457+
}
458+
}
459+
445460
@protected
446461
Redirect? constructRedirect() {
447462
if (session == null) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: walletconnect_modal_flutter
22
description: The WalletConnectModal for WalletConnect built using Flutter.
3-
version: 2.1.10
3+
version: 2.1.11
44
repository: https://github.com/WalletConnect/WalletConnectModalFlutter
55

66
environment:

test/mock_classes.mocks.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ class MockExplorerService extends _i1.Mock implements _i13.ExplorerService {
403403
returnValue: '',
404404
) as String);
405405
@override
406+
set previousRecentWallet(String? _previousRecentWallet) => super.noSuchMethod(
407+
Invocation.setter(
408+
#previousRecentWallet,
409+
_previousRecentWallet,
410+
),
411+
returnValueForMissingStub: null,
412+
);
413+
@override
406414
_i4.Future<void> init() => (super.noSuchMethod(
407415
Invocation.method(
408416
#init,
@@ -629,10 +637,12 @@ class MockWalletConnectModalService extends _i1.Mock
629637
returnValueForMissingStub: _i4.Future<void>.value(),
630638
) as _i4.Future<void>);
631639
@override
632-
_i4.Future<void> disconnect() => (super.noSuchMethod(
640+
_i4.Future<void> disconnect({bool? disconnectAllSessions = true}) =>
641+
(super.noSuchMethod(
633642
Invocation.method(
634643
#disconnect,
635644
[],
645+
{#disconnectAllSessions: disconnectAllSessions},
636646
),
637647
returnValue: _i4.Future<void>.value(),
638648
returnValueForMissingStub: _i4.Future<void>.value(),
@@ -697,6 +707,16 @@ class MockWalletConnectModalService extends _i1.Mock
697707
returnValue: '',
698708
) as String);
699709
@override
710+
_i4.Future<void> disconnectSession(_i6.SessionData? toDisconnect) =>
711+
(super.noSuchMethod(
712+
Invocation.method(
713+
#disconnectSession,
714+
[toDisconnect],
715+
),
716+
returnValue: _i4.Future<void>.value(),
717+
returnValueForMissingStub: _i4.Future<void>.value(),
718+
) as _i4.Future<void>);
719+
@override
700720
void registerListeners() => super.noSuchMethod(
701721
Invocation.method(
702722
#registerListeners,

test/test_data.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,21 @@ final testSession = SessionData(
148148
self: connectionMetadata,
149149
peer: connectionMetadata,
150150
);
151+
152+
final testSession2 = SessionData(
153+
topic: 'x',
154+
pairingTopic: 'y',
155+
relay: Relay('irn'),
156+
expiry: 1,
157+
acknowledged: true,
158+
controller: 'test',
159+
namespaces: {
160+
'test': const Namespace(
161+
accounts: ['abc'],
162+
methods: ['method1'],
163+
events: [],
164+
),
165+
},
166+
self: connectionMetadata,
167+
peer: connectionMetadata,
168+
);

test/walletconnect_modal_service/walletconnect_modal_service_unit_test.dart

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,58 @@ void main() {
219219
);
220220
});
221221

222-
test('calls disconnectSession on web3App', () async {
222+
test(
223+
'only disconnects the current session, if disconnectAllSessions is false',
224+
() async {
223225
when(sessions.getAll()).thenReturn(
224-
[testSession],
226+
[testSession, testSession2],
227+
);
228+
229+
await service.init();
230+
231+
int count = 0;
232+
f() {
233+
count++;
234+
}
235+
236+
service.addListener(f);
237+
238+
when(
239+
web3App.disconnectSession(
240+
topic: anyNamed('topic'),
241+
reason: anyNamed('reason'),
242+
),
243+
).thenAnswer((_) async {});
244+
245+
await service.disconnect(disconnectAllSessions: false);
246+
247+
verifyInOrder([
248+
web3App.disconnectSession(
249+
topic: testSession.pairingTopic,
250+
reason: anyNamed('reason'),
251+
),
252+
web3App.disconnectSession(
253+
topic: testSession.topic,
254+
reason: anyNamed('reason'),
255+
)
256+
]);
257+
258+
expect(service.isConnected, isFalse);
259+
expect(service.session, isNull);
260+
expect(service.address, '');
261+
expect(count, 1);
262+
263+
// Should null things out and notify listeners
264+
await service.disconnect();
265+
266+
expect(count, 2);
267+
268+
service.removeListener(f);
269+
});
270+
271+
test('calls disconnectSession multiple times on web3App', () async {
272+
when(sessions.getAll()).thenReturn(
273+
[testSession, testSession2],
225274
);
226275

227276
await service.init();
@@ -250,6 +299,14 @@ void main() {
250299
web3App.disconnectSession(
251300
topic: testSession.topic,
252301
reason: anyNamed('reason'),
302+
),
303+
web3App.disconnectSession(
304+
topic: testSession2.pairingTopic,
305+
reason: anyNamed('reason'),
306+
),
307+
web3App.disconnectSession(
308+
topic: testSession2.topic,
309+
reason: anyNamed('reason'),
253310
)
254311
]);
255312

0 commit comments

Comments
 (0)