Skip to content

Commit

Permalink
feat: Alert user to switch polygon network
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Jul 24, 2023
1 parent 1f8b113 commit cd37ea8
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,5 @@ enum ResponseString {
RESPONSE_STRING_errorGeneratingProof,
RESPONSE_STRING_successfullyGeneratingProof,
RESPONSE_STRING_pleaseAddXtoConnectToTheDapp,
RESPONSE_STRING_pleaseSwitchPolygonNetwork,
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,11 @@ extension ResponseStringX on ResponseString {
return globalMessage.RESPONSE_STRING_pleaseAddXtoConnectToTheDapp(
injectedMessage ?? '',
);

case ResponseString.RESPONSE_STRING_pleaseSwitchPolygonNetwork:
return globalMessage.RESPONSE_STRING_pleaseSwitchPolygonNetwork(
injectedMessage ?? '',
);
}
}
}
18 changes: 18 additions & 0 deletions lib/app/shared/enum/polygon_id_network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ extension PolygonIdNetworkX on PolygonIdNetwork {
return 'Polygon Mumbai';
}
}

String get tester {
switch (this) {
case PolygonIdNetwork.PolygonMainnet:
return 'polygon:main';
case PolygonIdNetwork.PolygonMumbai:
return 'polygon:mumbai';
}
}

String get oppositeNetwork {
switch (this) {
case PolygonIdNetwork.PolygonMainnet:
return 'mumbai(testnet)';
case PolygonIdNetwork.PolygonMumbai:
return 'mainnet';
}
}
}
2 changes: 2 additions & 0 deletions lib/app/shared/message_handler/global_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,6 @@ class GlobalMessage {
l10n.successfullyGeneratingProof;
String RESPONSE_STRING_pleaseAddXtoConnectToTheDapp(String value) =>
l10n.pleaseAddXtoConnectToTheDapp(value);
String RESPONSE_STRING_pleaseSwitchPolygonNetwork(String value) =>
l10n.pleaseSwitchPolygonNetwork(value);
}
6 changes: 6 additions & 0 deletions lib/app/shared/message_handler/response_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,12 @@ class ResponseMessage with MessageHandler {
context,
injectedMessage: injectedMessage,
);
case ResponseString.RESPONSE_STRING_pleaseSwitchPolygonNetwork:
return ResponseString.RESPONSE_STRING_pleaseSwitchPolygonNetwork
.localise(
context,
injectedMessage: injectedMessage,
);
}
}
return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ class BackupPolygonIdIdentityCubit extends Cubit<BackupPolygonIdIdentityState> {

await polygonIdCubit.initialise();

final polygonIdNetwork =
await secureStorageProvider.get(SecureStorageKeys.polygonIdNetwork);

String network = Parameters.POLYGON_MAIN_NETWORK;

if (polygonIdNetwork == PolygonIdNetwork.PolygonMainnet.toString()) {
if (polygonIdCubit.state.currentNetwork ==
PolygonIdNetwork.PolygonMainnet) {
network = Parameters.POLYGON_MAIN_NETWORK;
} else {
network = Parameters.POLYGON_TEST_NETWORK;
Expand Down
7 changes: 7 additions & 0 deletions lib/l10n/arb/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -917,5 +917,12 @@
"placeholders": {
"chain": {}
}
},
"pleaseSwitchPolygonNetwork": "Please switch to polygon {networkType} to perform this action.",
"@pleaseSwitchPolygonNetwork": {
"description": "",
"placeholders": {
"networkType": {}
}
}
}
12 changes: 8 additions & 4 deletions lib/l10n/untranslated.json
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@
"noInformationWillBeSharedFromThisCredentialMessage",
"burn",
"wouldYouLikeToConfirmThatYouIntendToBurnThisNFT",
"pleaseAddXtoConnectToTheDapp"
"pleaseAddXtoConnectToTheDapp",
"pleaseSwitchPolygonNetwork"
],

"es": [
Expand Down Expand Up @@ -1674,7 +1675,8 @@
"noInformationWillBeSharedFromThisCredentialMessage",
"burn",
"wouldYouLikeToConfirmThatYouIntendToBurnThisNFT",
"pleaseAddXtoConnectToTheDapp"
"pleaseAddXtoConnectToTheDapp",
"pleaseSwitchPolygonNetwork"
],

"fr": [
Expand Down Expand Up @@ -1816,7 +1818,8 @@
"noInformationWillBeSharedFromThisCredentialMessage",
"burn",
"wouldYouLikeToConfirmThatYouIntendToBurnThisNFT",
"pleaseAddXtoConnectToTheDapp"
"pleaseAddXtoConnectToTheDapp",
"pleaseSwitchPolygonNetwork"
],

"it": [
Expand Down Expand Up @@ -2655,6 +2658,7 @@
"noInformationWillBeSharedFromThisCredentialMessage",
"burn",
"wouldYouLikeToConfirmThatYouIntendToBurnThisNFT",
"pleaseAddXtoConnectToTheDapp"
"pleaseAddXtoConnectToTheDapp",
"pleaseSwitchPolygonNetwork"
]
}
60 changes: 46 additions & 14 deletions lib/polygon_id/cubit/polygon_id_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
'https://$ipfsApiKey:$ipfsApiKeySecret@ipfs.infura.io:5001';

String network = Parameters.POLYGON_MAIN_NETWORK;
PolygonIdNetwork currentNetwork = PolygonIdNetwork.PolygonMainnet;

// set polygon main network a first
if (polygonIdNetwork == null) {
Expand All @@ -80,6 +81,7 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {

if (polygonIdNetwork == PolygonIdNetwork.PolygonMainnet.toString()) {
network = Parameters.POLYGON_MAIN_NETWORK;
currentNetwork = PolygonIdNetwork.PolygonMainnet;
await polygonId.init(
network: network,
web3Url: Parameters.INFURA_URL,
Expand All @@ -91,6 +93,7 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
);
} else {
network = Parameters.POLYGON_TEST_NETWORK;
currentNetwork = PolygonIdNetwork.PolygonMumbai;
await polygonId.init(
network: network,
web3Url: Parameters.INFURA_MUMBAI_URL,
Expand All @@ -108,7 +111,13 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
//addIdentity
await polygonId.addIdentity(mnemonic: mnemonic!, network: network);
log.i('$network - get Identity');
emit(state.copyWith(status: AppStatus.init, isInitialised: true));
emit(
state.copyWith(
status: AppStatus.init,
isInitialised: true,
currentNetwork: currentNetwork,
),
);
} catch (e) {
emit(state.copyWith(status: AppStatus.error, isInitialised: false));
throw Exception('INIT_ISSUE - $e');
Expand Down Expand Up @@ -159,7 +168,12 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
//addIdentity
await polygonId.addIdentity(mnemonic: mnemonic!, network: network);
log.i('$network - get Identity');
emit(state.copyWith(status: AppStatus.idle));
emit(
state.copyWith(
status: AppStatus.idle,
currentNetwork: polygonIdNetwork,
),
);
} catch (e) {
emit(state.copyWith(status: AppStatus.error));
throw Exception('UPDATE_ISSUE - $e');
Expand Down Expand Up @@ -245,6 +259,33 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
final Iden3MessageEntity iden3MessageEntity =
await getIden3Message(message: state.scannedResponse!);

bool checkNetwork = true;

if (iden3MessageEntity.messageType == Iden3MessageType.authRequest) {
final body = iden3MessageEntity.body as AuthBodyRequest;

if (body.scope!.isNotEmpty) {
log.i('do not consider network for verifier');
checkNetwork = false;
}
}

if (checkNetwork &&
!iden3MessageEntity.from.contains(state.currentNetwork.tester)) {
emit(
state.copyWith(
status: AppStatus.error,
message: StateMessage.error(
messageHandler: ResponseMessage(
ResponseString.RESPONSE_STRING_pleaseSwitchPolygonNetwork,
),
injectedMessage: state.currentNetwork.oppositeNetwork,
),
),
);
return;
}

if (iden3MessageEntity.messageType == Iden3MessageType.authRequest) {
final body = iden3MessageEntity.body as AuthBodyRequest;

Expand Down Expand Up @@ -345,12 +386,9 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
final mnemonic =
await secureStorageProvider.get(SecureStorageKeys.ssiMnemonic);

final polygonIdNetwork =
await secureStorageProvider.get(SecureStorageKeys.polygonIdNetwork);

String network = Parameters.POLYGON_MAIN_NETWORK;

if (polygonIdNetwork == PolygonIdNetwork.PolygonMainnet.toString()) {
if (state.currentNetwork == PolygonIdNetwork.PolygonMainnet) {
network = Parameters.POLYGON_MAIN_NETWORK;
} else {
network = Parameters.POLYGON_TEST_NETWORK;
Expand Down Expand Up @@ -422,12 +460,9 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
}) async {
final mnemonic = await getSecureStorage.get(SecureStorageKeys.ssiMnemonic);

final polygonIdNetwork =
await secureStorageProvider.get(SecureStorageKeys.polygonIdNetwork);

String network = Parameters.POLYGON_MAIN_NETWORK;

if (polygonIdNetwork == PolygonIdNetwork.PolygonMainnet.toString()) {
if (state.currentNetwork == PolygonIdNetwork.PolygonMainnet) {
network = Parameters.POLYGON_MAIN_NETWORK;
} else {
network = Parameters.POLYGON_TEST_NETWORK;
Expand Down Expand Up @@ -505,12 +540,9 @@ class PolygonIdCubit extends Cubit<PolygonIdState> {
required Iden3MessageEntity iden3MessageEntity,
required String mnemonic,
}) async {
final polygonIdNetwork =
await secureStorageProvider.get(SecureStorageKeys.polygonIdNetwork);

String network = Parameters.POLYGON_MAIN_NETWORK;

if (polygonIdNetwork == PolygonIdNetwork.PolygonMainnet.toString()) {
if (state.currentNetwork == PolygonIdNetwork.PolygonMainnet) {
network = Parameters.POLYGON_MAIN_NETWORK;
} else {
network = Parameters.POLYGON_TEST_NETWORK;
Expand Down
5 changes: 5 additions & 0 deletions lib/polygon_id/cubit/polygon_id_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class PolygonIdState extends Equatable {
this.scannedResponse,
this.claims,
this.credentialManifests,
this.currentNetwork = PolygonIdNetwork.PolygonMainnet,
});

factory PolygonIdState.fromJson(Map<String, dynamic> json) =>
Expand All @@ -25,6 +26,7 @@ class PolygonIdState extends Equatable {
final String? scannedResponse;
final List<ClaimEntity>? claims;
final List<CredentialManifest>? credentialManifests;
final PolygonIdNetwork currentNetwork;

PolygonIdState error({required StateMessage message}) {
return copyWith(
Expand All @@ -43,6 +45,7 @@ class PolygonIdState extends Equatable {
String? scannedResponse,
List<ClaimEntity>? claims,
List<CredentialManifest>? credentialManifests,
PolygonIdNetwork? currentNetwork,
}) {
return PolygonIdState(
status: status,
Expand All @@ -53,6 +56,7 @@ class PolygonIdState extends Equatable {
scannedResponse: scannedResponse ?? this.scannedResponse,
claims: claims ?? this.claims,
credentialManifests: credentialManifests ?? this.credentialManifests,
currentNetwork: currentNetwork ?? this.currentNetwork,
);
}

Expand All @@ -65,6 +69,7 @@ class PolygonIdState extends Equatable {
message,
isInitialised,
scannedResponse,
currentNetwork,
loadingText,
credentialManifests,
];
Expand Down

0 comments on commit cd37ea8

Please sign in to comment.