Skip to content

Commit

Permalink
feat: saved paired dApp via wallet connect
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Jul 4, 2023
1 parent c947d06 commit d86d8d3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 40 deletions.
25 changes: 19 additions & 6 deletions lib/connection_bridge/model/saved_dapp_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,42 @@ 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';

@JsonSerializable()
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<String, dynamic> 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<String, dynamic> toJson() => _$SavedDappDataToJson(this);

@override
List<Object?> get props =>
[peer, walletAddress, blockchainType, wcSessionStore];
List<Object?> get props => [
peer,
walletAddress,
blockchainType,
wcSessionStore,
sessionConnect,
];
}
77 changes: 46 additions & 31 deletions lib/connection_bridge/repository/connected_dapp_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,41 +89,56 @@ class ConnectedDappRepository {
Future<int> insert(SavedDappData savedDappData) async {
final List<SavedDappData> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,12 @@ class WalletConnectCubit extends Cubit<WalletConnectState> {
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));
}
}

Expand Down Expand Up @@ -554,7 +558,7 @@ class WalletConnectCubit extends Cubit<WalletConnectState> {
}

Future<void> 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class ConfirmConnectionCubit extends Cubit<ConfirmConnectionState> {
// log.i(savedDappData.toJson());
// await connectedDappRepository.insert(savedDappData);

/// v2
final SessionProposalEvent? sessionProposalEvent =
walletConnectState.sessionProposalEvent;

Expand All @@ -128,6 +127,9 @@ class ConfirmConnectionCubit extends Cubit<ConfirmConnectionState> {
namespaces: sessionProposalEvent.params.generatedNamespaces!,
);

/// v2
/// dApp saved onSessionConnect function in wallet connect cubit
break;
}
emit(
Expand Down

0 comments on commit d86d8d3

Please sign in to comment.