diff --git a/lib/connection_bridge/model/saved_dapp_data.dart b/lib/connection_bridge/model/saved_dapp_data.dart index a6dfe7718..66061ab0e 100644 --- a/lib/connection_bridge/model/saved_dapp_data.dart +++ b/lib/connection_bridge/model/saved_dapp_data.dart @@ -3,6 +3,7 @@ import 'package:beacon_flutter/beacon_flutter.dart'; import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:wallet_connect/wallet_connect.dart'; +import 'package:walletconnect_flutter_v2/walletconnect_flutter_v2.dart'; part 'saved_dapp_data.g.dart'; @@ -10,22 +11,34 @@ part 'saved_dapp_data.g.dart'; class SavedDappData extends Equatable { const SavedDappData({ this.peer, - required this.walletAddress, - required this.blockchainType, + this.walletAddress, + this.blockchainType, this.wcSessionStore, + + //v2 + this.sessionConnect, }); factory SavedDappData.fromJson(Map json) => _$SavedDappDataFromJson(json); final P2PPeer? peer; - final String walletAddress; - final BlockchainType blockchainType; + final String? walletAddress; + final BlockchainType? blockchainType; final WCSessionStore? wcSessionStore; + //v2 + @JsonKey(includeFromJson: false, includeToJson: false) + final SessionConnect? sessionConnect; + Map toJson() => _$SavedDappDataToJson(this); @override - List get props => - [peer, walletAddress, blockchainType, wcSessionStore]; + List get props => [ + peer, + walletAddress, + blockchainType, + wcSessionStore, + sessionConnect, + ]; } diff --git a/lib/connection_bridge/repository/connected_dapp_repository.dart b/lib/connection_bridge/repository/connected_dapp_repository.dart index f4c14dd04..cefc24394 100644 --- a/lib/connection_bridge/repository/connected_dapp_repository.dart +++ b/lib/connection_bridge/repository/connected_dapp_repository.dart @@ -89,41 +89,56 @@ class ConnectedDappRepository { Future insert(SavedDappData savedDappData) async { final List savedPeerDatas = await findAll(); - final SavedDappData? matchedData = savedPeerDatas.firstWhereOrNull( - (SavedDappData savedData) { - switch (savedDappData.blockchainType) { - case BlockchainType.ethereum: - case BlockchainType.fantom: - case BlockchainType.polygon: - case BlockchainType.binance: - return savedData.walletAddress == savedDappData.walletAddress && - savedData.wcSessionStore!.remotePeerMeta.name == - savedDappData.wcSessionStore!.remotePeerMeta.name; - case BlockchainType.tezos: - return savedData.walletAddress == savedDappData.walletAddress && - savedData.peer!.name == savedDappData.peer!.name; - } - }, - - /// Note: Assumption - name is always unique - ); - - if (matchedData != null) { - await delete(matchedData); + // final SavedDappData? matchedData = savedPeerDatas.firstWhereOrNull( + // (SavedDappData savedData) { + // switch (savedDappData.blockchainType) { + // case BlockchainType.ethereum: + // case BlockchainType.fantom: + // case BlockchainType.polygon: + // case BlockchainType.binance: + // return false; + // case BlockchainType.tezos: + // return savedData.walletAddress == savedDappData.walletAddress && + // savedData.peer!.name == savedDappData.peer!.name; + // } + // }, + + // /// Note: Assumption - name is always unique + // ); + + if (savedDappData.blockchainType != null && + savedDappData.blockchainType == BlockchainType.tezos) { + final SavedDappData? matchedData = savedPeerDatas.firstWhereOrNull( + (SavedDappData savedData) => + savedData.walletAddress == savedDappData.walletAddress && + savedData.peer!.name == savedDappData.peer!.name, + + /// Note: Assumption - name is always unique + ); + if (matchedData != null) { + await delete(matchedData); + } } log.i('saving dapp Data'); late String id; - switch (savedDappData.blockchainType) { - case BlockchainType.ethereum: - case BlockchainType.fantom: - case BlockchainType.polygon: - case BlockchainType.binance: - id = savedDappData.wcSessionStore!.session.topic; - break; - case BlockchainType.tezos: - id = savedDappData.peer!.publicKey; - break; + // switch (savedDappData.blockchainType) { + // case BlockchainType.ethereum: + // case BlockchainType.fantom: + // case BlockchainType.polygon: + // case BlockchainType.binance: + // id = savedDappData.wcSessionStore!.session.topic; + // break; + // case BlockchainType.tezos: + // id = savedDappData.peer!.publicKey; + // break; + // } + + if (savedDappData.blockchainType != null && + savedDappData.blockchainType == BlockchainType.tezos) { + id = savedDappData.peer!.publicKey; + } else { + id = savedDappData.sessionConnect!.session.pairingTopic; } await _secureStorageProvider.set( diff --git a/lib/connection_bridge/wallet_connect/cubit/wallet_connect_cubit.dart b/lib/connection_bridge/wallet_connect/cubit/wallet_connect_cubit.dart index 3bf3e3df0..74892a7dc 100644 --- a/lib/connection_bridge/wallet_connect/cubit/wallet_connect_cubit.dart +++ b/lib/connection_bridge/wallet_connect/cubit/wallet_connect_cubit.dart @@ -328,8 +328,12 @@ class WalletConnectCubit extends Cubit { void _onSessionConnect(SessionConnect? args) { if (args != null) { log.i(args); - log.i(args.session); //sessions.value.add(args.session); + + final savedDappData = SavedDappData(sessionConnect: args); + + log.i(savedDappData.toJson()); + unawaited(connectedDappRepository.insert(savedDappData)); } } @@ -554,7 +558,7 @@ class WalletConnectCubit extends Cubit { } Future disconnectSession(PairingInfo pairing) async { - log.i('rdisconnectSession: ${pairing.topic}'); + log.i('disconnectSession: ${pairing.topic}'); await _web3Wallet!.disconnectSession( topic: pairing.topic, reason: Errors.getSdkError(Errors.USER_DISCONNECTED), diff --git a/lib/dashboard/connection/confirm_connection/cubit/confirm_connection_cubit.dart b/lib/dashboard/connection/confirm_connection/cubit/confirm_connection_cubit.dart index 866a64a7a..0e2c45015 100644 --- a/lib/dashboard/connection/confirm_connection/cubit/confirm_connection_cubit.dart +++ b/lib/dashboard/connection/confirm_connection/cubit/confirm_connection_cubit.dart @@ -119,7 +119,6 @@ class ConfirmConnectionCubit extends Cubit { // log.i(savedDappData.toJson()); // await connectedDappRepository.insert(savedDappData); - /// v2 final SessionProposalEvent? sessionProposalEvent = walletConnectState.sessionProposalEvent; @@ -128,6 +127,9 @@ class ConfirmConnectionCubit extends Cubit { namespaces: sessionProposalEvent.params.generatedNamespaces!, ); + /// v2 + /// dApp saved onSessionConnect function in wallet connect cubit + break; } emit(