Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change caching the active sessions for a pairing #35

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions lib/services/wallet_connect_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class WalletConnectService {
late BuildContext _context;
String _sessionTopic = '';

final List<ApproveResponse> _dAppsProposalData = [];
final List<SessionData> dAppsActiveSessions = [];

set context(BuildContext context) => _context = context;

Expand All @@ -43,6 +43,20 @@ class WalletConnectService {
icons: ['https://avatars.githubusercontent.com/u/37784886'],
),
);
getPairings().getAll().forEach((pairingInfo) {
dAppsActiveSessions.addAll(
getSessionsForPairing(pairingInfo.topic).values
);
print('Active pairing: $pairingInfo');
});
print('Pairings num: ${getPairings().getAll().length}');
print('Active sessions: ${getActiveSessions()}');
// print(
// 'Active sessions per pairing: '
// '${getSessionsForPairing(
// '7c2744eae54d0fb37b7f77c41eb7ca14aed92093c9e0179099ec8ed3dc699c1e',
// )}',
// );
_initListeners();
}

Expand Down Expand Up @@ -124,7 +138,7 @@ class WalletConnectService {
);

_sendSuccessfullyApprovedSessionNotification(dAppMetadata);
_dAppsProposalData.add(approveResponse);
dAppsActiveSessions.add(approveResponse.session);
_sessionTopic = approveResponse.session.topic;
} else {
await _wcClient.rejectSession(
Expand All @@ -146,9 +160,8 @@ class WalletConnectService {
chainId: 'zenon:3',
method: 'znn_info',
handler: (topic, params) async {
final dAppMetadata = _dAppsProposalData
final dAppMetadata = dAppsActiveSessions
.firstWhere((element) => element.topic == topic)
.session
.peer
.metadata;

Expand Down Expand Up @@ -214,9 +227,8 @@ class WalletConnectService {
chainId: 'zenon:3',
method: 'znn_sign',
handler: (topic, params) async {
final dAppMetadata = _dAppsProposalData
final dAppMetadata = dAppsActiveSessions
.firstWhere((element) => element.topic == topic)
.session
.peer
.metadata;
if (kCurrentPage != Tabs.lock) {
Expand Down Expand Up @@ -274,9 +286,8 @@ class WalletConnectService {
chainId: 'zenon:3',
method: 'znn_send',
handler: (topic, params) async {
final dAppMetadata = _dAppsProposalData
final dAppMetadata = dAppsActiveSessions
.firstWhere((element) => element.topic == topic)
.session
.peer
.metadata;
if (kCurrentPage != Tabs.lock) {
Expand Down Expand Up @@ -403,6 +414,15 @@ class WalletConnectService {
}) =>
_wcClient.core.pairing.disconnect(topic: topic);

Future<void> disconnectSession() async {
IPairingStore pairingStore = getPairings();
pairingStore.getAll().forEach((element) async {
await _wcClient.disconnectSession(
topic: element.topic,
reason: Errors.getSdkError(Errors.USER_DISCONNECTED));
});
}

Future<void> _emitEventForTheDApp({
required String sessionTopic,
required String changeName,
Expand Down Expand Up @@ -436,7 +456,7 @@ class WalletConnectService {

Map<String, SessionData> getActiveSessions() => _wcClient.getActiveSessions();

Map<String, SessionData> getAllSessions(String pairingTopic) =>
Map<String, SessionData> getSessionsForPairing(String pairingTopic) =>
_wcClient.getSessionsForPairing(
pairingTopic: pairingTopic,
);
Expand Down