From 077a622879006356657de76a9f4ec0407d96ecba Mon Sep 17 00:00:00 2001 From: Bibash Shrestha Date: Fri, 18 Oct 2024 17:47:46 +0545 Subject: [PATCH] feat: Remove polygon id specific backup and restore --- lib/credentials/cubit/credentials_cubit.dart | 6 +- .../drawer/wallet_security/backup/backup.dart | 2 - .../backup_polygon_identity.dart | 2 - .../cubit/backup_polygon_identity_cubit.dart | 116 ---------- .../cubit/backup_polygon_identity_state.dart | 53 ----- .../view/backup_polygon_identity_page.dart | 134 ----------- .../wallet_security/backup/src/src.dart | 1 - .../backup/src/view/backup_menu.dart | 93 -------- .../wallet_security/restore/restore.dart | 1 - .../cubit/restore_credential_cubit.dart | 110 +++------ .../restore_credential.dart | 1 - .../view/restore_credential_page.dart | 1 - .../restore_polygonid_credential_page.dart | 210 ------------------ .../wallet_security/restore/src/src.dart | 1 - .../restore/src/view/restore_menu.dart | 110 --------- .../src/view/wallet_security_menu.dart | 49 +++- 16 files changed, 80 insertions(+), 810 deletions(-) delete mode 100644 lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/backup_polygon_identity.dart delete mode 100644 lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_cubit.dart delete mode 100644 lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_state.dart delete mode 100644 lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/view/backup_polygon_identity_page.dart delete mode 100644 lib/dashboard/drawer/wallet_security/backup/src/src.dart delete mode 100644 lib/dashboard/drawer/wallet_security/backup/src/view/backup_menu.dart delete mode 100644 lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_polygonid_credential_page.dart delete mode 100644 lib/dashboard/drawer/wallet_security/restore/src/src.dart delete mode 100644 lib/dashboard/drawer/wallet_security/restore/src/view/restore_menu.dart diff --git a/lib/credentials/cubit/credentials_cubit.dart b/lib/credentials/cubit/credentials_cubit.dart index 99491fce4..1e28d4d37 100644 --- a/lib/credentials/cubit/credentials_cubit.dart +++ b/lib/credentials/cubit/credentials_cubit.dart @@ -495,11 +495,9 @@ class CredentialsCubit extends Cubit { Future recoverWallet({ required List credentials, - required bool isPolygonIdCredentials, }) async { - if (!isPolygonIdCredentials) { - await credentialsRepository.deleteAll(); - } + await credentialsRepository.deleteAll(); + for (final credential in credentials) { await credentialsRepository.insert(credential); } diff --git a/lib/dashboard/drawer/wallet_security/backup/backup.dart b/lib/dashboard/drawer/wallet_security/backup/backup.dart index 92084202c..78f28b7f0 100644 --- a/lib/dashboard/drawer/wallet_security/backup/backup.dart +++ b/lib/dashboard/drawer/wallet_security/backup/backup.dart @@ -1,4 +1,2 @@ export 'backup_credential/backup_credential.dart'; export 'backup_mnemonic/backup_mnemonic.dart'; -export 'backup_polygon_identity/backup_polygon_identity.dart'; -export 'src/src.dart'; diff --git a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/backup_polygon_identity.dart b/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/backup_polygon_identity.dart deleted file mode 100644 index 8c941dea3..000000000 --- a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/backup_polygon_identity.dart +++ /dev/null @@ -1,2 +0,0 @@ -export 'cubit/backup_polygon_identity_cubit.dart'; -export 'view/backup_polygon_identity_page.dart'; diff --git a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_cubit.dart b/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_cubit.dart deleted file mode 100644 index 15d4f256a..000000000 --- a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_cubit.dart +++ /dev/null @@ -1,116 +0,0 @@ -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:altme/activity_log/activity_log.dart'; -import 'package:altme/app/app.dart'; -import 'package:altme/polygon_id/polygon_id.dart'; -import 'package:altme/wallet/cubit/wallet_cubit.dart'; - -import 'package:cryptocurrency_keys/cryptocurrency_keys.dart'; -import 'package:equatable/equatable.dart'; -import 'package:file_saver/file_saver.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:json_annotation/json_annotation.dart'; -import 'package:secure_storage/secure_storage.dart'; - -part 'backup_polygon_identity_cubit.g.dart'; -part 'backup_polygon_identity_state.dart'; - -class BackupPolygonIdIdentityCubit extends Cubit { - BackupPolygonIdIdentityCubit({ - required this.secureStorageProvider, - required this.cryptoKeys, - required this.walletCubit, - required this.fileSaver, - required this.polygonIdCubit, - required this.activityLogManager, - }) : super(const BackupPolygonIdIdentityState()); - - final SecureStorageProvider secureStorageProvider; - final CryptocurrencyKeys cryptoKeys; - final WalletCubit walletCubit; - final FileSaver fileSaver; - final PolygonIdCubit polygonIdCubit; - final ActivityLogManager activityLogManager; - - Future saveEncryptedFile() async { - emit(state.loading()); - await Future.delayed(const Duration(milliseconds: 500)); - final mnemonic = - await secureStorageProvider.get(SecureStorageKeys.ssiMnemonic); - final isPermissionStatusGranted = await getStoragePermission(); - - try { - if (!isPermissionStatusGranted) { - throw ResponseMessage( - message: ResponseString.STORAGE_PERMISSION_DENIED_MESSAGE, - ); - } - - final dateTime = getDateTimeWithoutSpace(); - final fileName = 'altme-polygonid-identity-$dateTime'; - - await polygonIdCubit.initialise(); - - String network = Parameters.POLYGON_MAIN_NETWORK; - - if (polygonIdCubit.state.currentNetwork == - PolygonIdNetwork.PolygonMainnet) { - network = Parameters.POLYGON_MAIN_NETWORK; - } else { - network = Parameters.POLYGON_TEST_NETWORK; - } - - final polygonCredentials = await polygonIdCubit.polygonId.backupIdentity( - mnemonic: mnemonic!, - network: network, - ); - - if (polygonCredentials == null) { - throw ResponseMessage( - message: ResponseString - .RESPONSE_STRING_SOMETHING_WENT_WRONG_TRY_AGAIN_LATER, - ); - } - - final String encryptedString = polygonCredentials; - - final fileBytes = Uint8List.fromList(utf8.encode(encryptedString)); - - final filePath = await fileSaver.saveAs( - name: fileName, - bytes: fileBytes, - ext: 'txt', - mimeType: MimeType.text, - ); - - if (filePath != null && filePath.isEmpty) { - emit(state.copyWith(status: AppStatus.idle)); - } else { - await activityLogManager.saveLog(LogData(type: LogType.backupData)); - emit( - state.copyWith( - status: AppStatus.success, - filePath: filePath, - messageHandler: ResponseMessage( - message: ResponseString - .RESPONSE_STRING_BACKUP_CREDENTIAL_SUCCESS_MESSAGE, - ), - ), - ); - } - } catch (e) { - if (e is MessageHandler) { - emit(state.error(messageHandler: e)); - } else { - emit( - state.error( - messageHandler: ResponseMessage( - message: ResponseString.RESPONSE_STRING_BACKUP_CREDENTIAL_ERROR, - ), - ), - ); - } - } - } -} diff --git a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_state.dart b/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_state.dart deleted file mode 100644 index 6fd04c2fa..000000000 --- a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/cubit/backup_polygon_identity_state.dart +++ /dev/null @@ -1,53 +0,0 @@ -part of 'backup_polygon_identity_cubit.dart'; - -@JsonSerializable() -class BackupPolygonIdIdentityState extends Equatable { - const BackupPolygonIdIdentityState({ - this.status = AppStatus.init, - this.message, - this.filePath = '', - }); - - factory BackupPolygonIdIdentityState.fromJson(Map json) => - _$BackupPolygonIdIdentityStateFromJson(json); - - final AppStatus status; - final StateMessage? message; - final String filePath; - - BackupPolygonIdIdentityState loading() { - return BackupPolygonIdIdentityState( - status: AppStatus.loading, - filePath: filePath, - ); - } - - BackupPolygonIdIdentityState error({ - required MessageHandler messageHandler, - }) { - return BackupPolygonIdIdentityState( - status: AppStatus.error, - filePath: filePath, - message: StateMessage.error(messageHandler: messageHandler), - ); - } - - BackupPolygonIdIdentityState copyWith({ - required AppStatus status, - MessageHandler? messageHandler, - String? filePath, - }) { - return BackupPolygonIdIdentityState( - status: status, - filePath: filePath ?? this.filePath, - message: messageHandler == null - ? null - : StateMessage.success(messageHandler: messageHandler), - ); - } - - Map toJson() => _$BackupPolygonIdIdentityStateToJson(this); - - @override - List get props => [status, filePath, message]; -} diff --git a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/view/backup_polygon_identity_page.dart b/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/view/backup_polygon_identity_page.dart deleted file mode 100644 index 4cfbf11bb..000000000 --- a/lib/dashboard/drawer/wallet_security/backup/backup_polygon_identity/view/backup_polygon_identity_page.dart +++ /dev/null @@ -1,134 +0,0 @@ -import 'package:altme/activity_log/activity_log.dart'; -import 'package:altme/app/app.dart'; -import 'package:altme/dashboard/drawer/drawer.dart'; -import 'package:altme/l10n/l10n.dart'; -import 'package:altme/onboarding/onboarding.dart'; -import 'package:altme/polygon_id/cubit/polygon_id_cubit.dart'; - -import 'package:altme/wallet/wallet.dart'; -import 'package:cryptocurrency_keys/cryptocurrency_keys.dart'; -import 'package:file_saver/file_saver.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; - -import 'package:secure_storage/secure_storage.dart'; - -class BackupPolygonIdIdentityPage extends StatelessWidget { - const BackupPolygonIdIdentityPage({ - super.key, - }); - - static Route route() { - return MaterialPageRoute( - settings: const RouteSettings(name: '/BackupPolygonIdIdentityPage'), - builder: (_) => const BackupPolygonIdIdentityPage(), - ); - } - - @override - Widget build(BuildContext context) { - return BlocProvider( - create: (context) => BackupPolygonIdIdentityCubit( - secureStorageProvider: getSecureStorage, - cryptoKeys: const CryptocurrencyKeys(), - walletCubit: context.read(), - fileSaver: FileSaver.instance, - polygonIdCubit: context.read(), - activityLogManager: ActivityLogManager(getSecureStorage), - ), - child: const BackupPolygonIdIdentityView(), - ); - } -} - -class BackupPolygonIdIdentityView extends StatelessWidget { - const BackupPolygonIdIdentityView({super.key}); - - @override - Widget build(BuildContext context) { - final l10n = context.l10n; - - return BasePage( - title: l10n.backupPolygonIdIdentity, - titleAlignment: Alignment.topCenter, - padding: const EdgeInsets.only( - top: 0, - bottom: Sizes.spaceSmall, - left: Sizes.spaceSmall, - right: Sizes.spaceSmall, - ), - titleLeading: BackLeadingButton( - onPressed: () { - if (context.read().state.status != - AppStatus.loading) { - Navigator.of(context).pop(); - } - }, - ), - body: BlocConsumer( - listener: (context, state) async { - if (state.status == AppStatus.loading) { - LoadingView().show(context: context); - } else { - LoadingView().hide(); - } - - if (state.message != null) { - AlertMessage.showStateMessage( - context: context, - stateMessage: state.message!, - ); - //set a delay to sure about showing message - await Future.delayed(const Duration(milliseconds: 800)); - } - - if (state.status == AppStatus.success) { - Navigator.of(context).pop(); - Navigator.of(context).pop(); - } - }, - builder: (context, state) { - return Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - const MStepper( - totalStep: 2, - step: 2, - ), - const SizedBox(height: Sizes.spaceNormal), - Text( - l10n.saveBackupCredentialTitle, - textAlign: TextAlign.center, - style: Theme.of(context).textTheme.titleLarge, - ), - const SizedBox(height: Sizes.spaceXLarge), - Text( - l10n.saveBackupPolygonCredentialSubtitle, - textAlign: TextAlign.center, - style: Theme.of(context).textTheme.titleMedium, - ), - const SizedBox(height: Sizes.spaceXLarge), - Image.asset( - ImageStrings.receiveSqure, - width: Sizes.icon6x, - height: Sizes.icon6x, - ), - ], - ); - }, - ), - navigation: Padding( - padding: const EdgeInsets.all(Sizes.spaceSmall), - child: MyElevatedButton( - onPressed: () async { - await context - .read() - .saveEncryptedFile(); - }, - text: l10n.backupCredentialButtonTitle, - ), - ), - ); - } -} diff --git a/lib/dashboard/drawer/wallet_security/backup/src/src.dart b/lib/dashboard/drawer/wallet_security/backup/src/src.dart deleted file mode 100644 index c3954bcfb..000000000 --- a/lib/dashboard/drawer/wallet_security/backup/src/src.dart +++ /dev/null @@ -1 +0,0 @@ -export 'view/backup_menu.dart'; diff --git a/lib/dashboard/drawer/wallet_security/backup/src/view/backup_menu.dart b/lib/dashboard/drawer/wallet_security/backup/src/view/backup_menu.dart deleted file mode 100644 index fe11259ae..000000000 --- a/lib/dashboard/drawer/wallet_security/backup/src/view/backup_menu.dart +++ /dev/null @@ -1,93 +0,0 @@ -import 'package:altme/app/app.dart'; -import 'package:altme/dashboard/dashboard.dart'; -import 'package:altme/l10n/l10n.dart'; -import 'package:flutter/material.dart'; - -class BackupMenu extends StatelessWidget { - const BackupMenu({super.key}); - - static Route route() { - return MaterialPageRoute( - settings: const RouteSettings(name: '/BackupMenu'), - builder: (_) => const BackupMenu(), - ); - } - - @override - Widget build(BuildContext context) { - return const BackupView(); - } -} - -class BackupView extends StatelessWidget { - const BackupView({super.key}); - - @override - Widget build(BuildContext context) { - final l10n = context.l10n; - return Drawer( - backgroundColor: Theme.of(context).colorScheme.surface, - child: SafeArea( - child: SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 15), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const BackLeadingButton( - padding: EdgeInsets.zero, - ), - WalletLogo( - height: 90, - width: MediaQuery.of(context).size.shortestSide * 0.5, - showPoweredBy: true, - ), - DrawerItem( - title: l10n.backupCredential, - onTap: () async { - await securityCheck( - context: context, - localAuthApi: LocalAuthApi(), - onSuccess: () { - Navigator.of(context).push( - BackupMnemonicPage.route( - title: l10n.backupCredential, - isValidCallback: () { - Navigator.of(context) - .push(BackupCredentialPage.route()); - }, - ), - ); - }, - ); - }, - ), - DrawerItem( - title: l10n.backupPolygonIdIdentity, - onTap: () async { - await securityCheck( - context: context, - localAuthApi: LocalAuthApi(), - onSuccess: () { - Navigator.of(context).push( - BackupMnemonicPage.route( - title: l10n.backupPolygonIdIdentity, - isValidCallback: () { - Navigator.of(context).push( - BackupPolygonIdIdentityPage.route(), - ); - }, - ), - ); - }, - ); - }, - ), - ], - ), - ), - ), - ), - ); - } -} diff --git a/lib/dashboard/drawer/wallet_security/restore/restore.dart b/lib/dashboard/drawer/wallet_security/restore/restore.dart index c09f122e5..bc996ae1d 100644 --- a/lib/dashboard/drawer/wallet_security/restore/restore.dart +++ b/lib/dashboard/drawer/wallet_security/restore/restore.dart @@ -1,3 +1,2 @@ export 'restore_credential/restore_credential.dart'; export 'restore_credential_mnemonics/restore_credential_mnemonic.dart'; -export 'src/src.dart'; diff --git a/lib/dashboard/drawer/wallet_security/restore/restore_credential/cubit/restore_credential_cubit.dart b/lib/dashboard/drawer/wallet_security/restore/restore_credential/cubit/restore_credential_cubit.dart index 479f54d67..b368b7215 100644 --- a/lib/dashboard/drawer/wallet_security/restore/restore_credential/cubit/restore_credential_cubit.dart +++ b/lib/dashboard/drawer/wallet_security/restore/restore_credential/cubit/restore_credential_cubit.dart @@ -40,7 +40,6 @@ class RestoreCredentialCubit extends Cubit { } Future recoverWallet({ - required bool isPolygonIdCredentials, required bool isFromOnBoarding, }) async { if (state.backupFilePath == null) return; @@ -71,90 +70,43 @@ class RestoreCredentialCubit extends Cubit { late List credentialList; - if (isPolygonIdCredentials) { - final polygonIdNetwork = - await secureStorageProvider.get(SecureStorageKeys.polygonIdNetwork); - - String network = Parameters.POLYGON_MAIN_NETWORK; - - if (polygonIdNetwork == PolygonIdNetwork.PolygonMainnet.toString()) { - network = Parameters.POLYGON_MAIN_NETWORK; - } else { - network = Parameters.POLYGON_TEST_NETWORK; - } - - final privateIdentityEntity = await polygonId.restoreIdentity( - mnemonic: stringForBackup, - encryptedDb: text, - network: network, - ); - - final List claims = await polygonId.restoreClaims( - privateIdentityEntity: privateIdentityEntity, - ); - - final credentials = claims.map( - (ClaimEntity claimEntity) { - final jsonCredential = claimEntity.info; - final credentialPreview = Credential.fromJson(jsonCredential); - - final credentialModel = CredentialModel( - id: claimEntity.id, - image: 'image', - data: jsonCredential, - shareLink: '', - credentialPreview: credentialPreview, - expirationDate: claimEntity.expiration, - jwt: null, - format: 'ldp_vc', - activities: [Activity(acquisitionAt: DateTime.now())], - ); - return credentialModel; - }, - ); - credentialList = credentials.toList(); - } else { - final json = jsonDecode(text) as Map; - if (!json.containsKey('cipherText') || - !json.containsKey('authenticationTag') || - json['cipherText'] is! String || - json['authenticationTag'] is! String) { - throw ResponseMessage( - message: ResponseString - .RESPONSE_STRING_RECOVERY_CREDENTIAL_JSON_FORMAT_ERROR_MESSAGE, - ); - } - final encryption = Encryption( - cipherText: json['cipherText'] as String, - authenticationTag: json['authenticationTag'] as String, + final json = jsonDecode(text) as Map; + if (!json.containsKey('cipherText') || + !json.containsKey('authenticationTag') || + json['cipherText'] is! String || + json['authenticationTag'] is! String) { + throw ResponseMessage( + message: ResponseString + .RESPONSE_STRING_RECOVERY_CREDENTIAL_JSON_FORMAT_ERROR_MESSAGE, ); - final decryptedText = - await cryptoKeys.decrypt(stringForBackup, encryption); - final decryptedJson = jsonDecode(decryptedText) as Map; - if (!decryptedJson.containsKey('date') || - !decryptedJson.containsKey('credentials') || - decryptedJson['date'] is! String) { - throw ResponseMessage( - message: ResponseString - .RESPONSE_STRING_RECOVERY_CREDENTIAL_JSON_FORMAT_ERROR_MESSAGE, - ); - } - - final List credentialJson = - decryptedJson['credentials'] as List; - final credentials = credentialJson.map( - (dynamic credential) => - CredentialModel.fromJson(credential as Map), + } + final encryption = Encryption( + cipherText: json['cipherText'] as String, + authenticationTag: json['authenticationTag'] as String, + ); + final decryptedText = + await cryptoKeys.decrypt(stringForBackup, encryption); + final decryptedJson = jsonDecode(decryptedText) as Map; + if (!decryptedJson.containsKey('date') || + !decryptedJson.containsKey('credentials') || + decryptedJson['date'] is! String) { + throw ResponseMessage( + message: ResponseString + .RESPONSE_STRING_RECOVERY_CREDENTIAL_JSON_FORMAT_ERROR_MESSAGE, ); - - credentialList = credentials.toList(); } - await credentialsCubit.recoverWallet( - credentials: credentialList, - isPolygonIdCredentials: isPolygonIdCredentials, + final List credentialJson = + decryptedJson['credentials'] as List; + final credentials = credentialJson.map( + (dynamic credential) => + CredentialModel.fromJson(credential as Map), ); + credentialList = credentials.toList(); + + await credentialsCubit.recoverWallet(credentials: credentialList); + if (walletCubit.state.currentAccount != null) { await credentialsCubit.loadAllCredentials( blockchainType: walletCubit.state.currentAccount!.blockchainType, diff --git a/lib/dashboard/drawer/wallet_security/restore/restore_credential/restore_credential.dart b/lib/dashboard/drawer/wallet_security/restore/restore_credential/restore_credential.dart index 48af3f3cc..cc0c96a1a 100644 --- a/lib/dashboard/drawer/wallet_security/restore/restore_credential/restore_credential.dart +++ b/lib/dashboard/drawer/wallet_security/restore/restore_credential/restore_credential.dart @@ -1,4 +1,3 @@ export 'cubit/restore_credential_cubit.dart'; export 'view/restore_credential_page.dart'; -export 'view/restore_polygonid_credential_page.dart'; export 'widgets/widgets.dart'; diff --git a/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_credential_page.dart b/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_credential_page.dart index bb9e1bcbf..07ec15fe3 100644 --- a/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_credential_page.dart +++ b/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_credential_page.dart @@ -171,7 +171,6 @@ class _RestoreCredentialViewState extends State { onPressed: state.backupFilePath == null ? null : () => context.read().recoverWallet( - isPolygonIdCredentials: false, isFromOnBoarding: widget.fromOnBoarding, ), text: l10n.loadFile, diff --git a/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_polygonid_credential_page.dart b/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_polygonid_credential_page.dart deleted file mode 100644 index d54a818f9..000000000 --- a/lib/dashboard/drawer/wallet_security/restore/restore_credential/view/restore_polygonid_credential_page.dart +++ /dev/null @@ -1,210 +0,0 @@ -import 'dart:io'; - -import 'package:altme/activity_log/activity_log.dart'; -import 'package:altme/app/app.dart'; -import 'package:altme/credentials/credentials.dart'; -import 'package:altme/dashboard/dashboard.dart'; -import 'package:altme/l10n/l10n.dart'; -import 'package:altme/onboarding/onboarding.dart'; - -import 'package:altme/wallet/wallet.dart'; -import 'package:cryptocurrency_keys/cryptocurrency_keys.dart'; -import 'package:file_picker/file_picker.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:path_provider/path_provider.dart'; -import 'package:permission_handler/permission_handler.dart'; -import 'package:polygonid/polygonid.dart'; -import 'package:secure_storage/secure_storage.dart'; - -class RestorePolygonIdCredentialPage extends StatelessWidget { - const RestorePolygonIdCredentialPage({super.key}); - - static Route route() => MaterialPageRoute( - builder: (context) => const RestorePolygonIdCredentialPage(), - settings: const RouteSettings(name: '/RestorePolygonIdCredentialPage'), - ); - - @override - Widget build(BuildContext context) { - return BlocProvider( - create: (context) => RestoreCredentialCubit( - secureStorageProvider: getSecureStorage, - cryptoKeys: const CryptocurrencyKeys(), - walletCubit: context.read(), - credentialsCubit: context.read(), - polygonId: PolygonId(), - activityLogManager: ActivityLogManager(getSecureStorage), - ), - child: const RestorePolygonIdCredentialView(), - ); - } -} - -class RestorePolygonIdCredentialView extends StatefulWidget { - const RestorePolygonIdCredentialView({super.key}); - - @override - _RestorePolygonIdCredentialViewState createState() => - _RestorePolygonIdCredentialViewState(); -} - -class _RestorePolygonIdCredentialViewState - extends State { - @override - void initState() { - super.initState(); - } - - @override - Widget build(BuildContext context) { - final l10n = context.l10n; - return BasePage( - title: l10n.restorePolygonIdCredentials, - titleAlignment: Alignment.topCenter, - padding: const EdgeInsets.only( - top: 0, - left: Sizes.spaceSmall, - right: Sizes.spaceSmall, - bottom: Sizes.spaceSmall, - ), - titleLeading: BackLeadingButton( - onPressed: () { - if (context.read().state.status != - AppStatus.loading) { - Navigator.of(context).pop(); - } - }, - ), - body: BlocConsumer( - listener: (context, state) async { - if (state.status == AppStatus.loading) { - LoadingView().show(context: context); - } else { - LoadingView().hide(); - } - - if (state.status == AppStatus.success && - state.recoveredCredentialLength != null) { - final credentialLength = state.recoveredCredentialLength; - AlertMessage.showStateMessage( - context: context, - stateMessage: StateMessage.success( - stringMessage: l10n.recoveryCredentialSuccessMessage( - '''$credentialLength ${credentialLength! > 1 ? '${l10n.credential}s' : l10n.credential}''', - ), - ), - ); - await Future.delayed(const Duration(milliseconds: 800)); - Navigator.of(context).pop(); - Navigator.of(context).pop(); - } - - if (state.message != null) { - AlertMessage.showStateMessage( - context: context, - stateMessage: state.message!, - ); - } - }, - builder: (context, state) { - return Column( - mainAxisAlignment: MainAxisAlignment.center, - mainAxisSize: MainAxisSize.max, - children: [ - const MStepper( - totalStep: 2, - step: 2, - ), - const SizedBox( - height: Sizes.spaceNormal, - ), - Text( - l10n.restoreCredentialStep2Title, - textAlign: TextAlign.center, - style: Theme.of(context).textTheme.labelMedium, - ), - const SizedBox( - height: Sizes.spaceNormal, - ), - UploadFile( - filePath: state.backupFilePath, - onTap: () async { - if (Platform.isAndroid) { - final appDir = (await getTemporaryDirectory()).path; - await Directory(appDir).delete(recursive: true); - } - await _pickRestoreFile(); - }, - ), - ], - ); - }, - ), - navigation: Padding( - padding: const EdgeInsets.all(Sizes.spaceSmall), - child: BlocBuilder( - builder: (context, state) { - return MyElevatedButton( - onPressed: state.backupFilePath == null - ? null - : () => context.read().recoverWallet( - isPolygonIdCredentials: true, - isFromOnBoarding: false, - ), - text: l10n.loadFile, - ); - }, - ), - ), - ); - } - - Future _pickRestoreFile() async { - final l10n = context.l10n; - final storagePermission = await Permission.storage.request(); - if (storagePermission.isDenied) { - AlertMessage.showStateMessage( - context: context, - stateMessage: StateMessage.success( - stringMessage: l10n.storagePermissionDeniedMessage, - ), - ); - return; - } - - if (storagePermission.isPermanentlyDenied) { - await _showPermissionPopup(); - return; - } - - if (storagePermission.isGranted || storagePermission.isLimited) { - final pickedFile = await FilePicker.platform.pickFiles( - type: FileType.custom, - allowMultiple: false, - allowedExtensions: ['txt'], - ); - context - .read() - .setFilePath(filePath: pickedFile?.files.first.path); - } - } - - Future _showPermissionPopup() async { - final localizations = context.l10n; - final confirm = await showDialog( - context: context, - builder: (context) => ConfirmDialog( - title: localizations.storagePermissionRequired, - subtitle: localizations.storagePermissionPermanentlyDeniedMessage, - yes: localizations.ok, - no: localizations.cancel, - ), - ) ?? - false; - - if (confirm) { - await openAppSettings(); - } - } -} diff --git a/lib/dashboard/drawer/wallet_security/restore/src/src.dart b/lib/dashboard/drawer/wallet_security/restore/src/src.dart deleted file mode 100644 index 9212bf7a8..000000000 --- a/lib/dashboard/drawer/wallet_security/restore/src/src.dart +++ /dev/null @@ -1 +0,0 @@ -export 'view/restore_menu.dart'; diff --git a/lib/dashboard/drawer/wallet_security/restore/src/view/restore_menu.dart b/lib/dashboard/drawer/wallet_security/restore/src/view/restore_menu.dart deleted file mode 100644 index e9cbe65be..000000000 --- a/lib/dashboard/drawer/wallet_security/restore/src/view/restore_menu.dart +++ /dev/null @@ -1,110 +0,0 @@ -import 'package:altme/app/app.dart'; -import 'package:altme/dashboard/dashboard.dart'; -import 'package:altme/l10n/l10n.dart'; -import 'package:flutter/material.dart'; - -class RestoreMenu extends StatelessWidget { - const RestoreMenu({super.key}); - - static Route route() { - return MaterialPageRoute( - settings: const RouteSettings(name: '/RestoreMenu'), - builder: (_) => const RestoreMenu(), - ); - } - - @override - Widget build(BuildContext context) { - return const RestoreView(); - } -} - -class RestoreView extends StatelessWidget { - const RestoreView({super.key}); - - @override - Widget build(BuildContext context) { - final l10n = context.l10n; - return Drawer( - backgroundColor: Theme.of(context).colorScheme.surface, - child: SafeArea( - child: SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 15), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const BackLeadingButton( - padding: EdgeInsets.zero, - ), - WalletLogo( - height: 90, - width: MediaQuery.of(context).size.shortestSide * 0.5, - showPoweredBy: true, - ), - DrawerItem( - title: l10n.restoreCredential, - onTap: () async { - final confirm = await showDialog( - context: context, - builder: (context) => ConfirmDialog( - title: l10n.warningDialogTitle, - subtitle: - l10n.restorationCredentialWarningDialogSubtitle, - yes: l10n.showDialogYes, - no: l10n.showDialogNo, - ), - ) ?? - false; - - if (confirm) { - await securityCheck( - context: context, - localAuthApi: LocalAuthApi(), - onSuccess: () { - Navigator.of(context).push( - RestoreCredentialMnemonicPage.route( - title: l10n.restoreCredential, - isValidCallback: () { - Navigator.of(context).push( - RestoreCredentialPage.route( - fromOnBoarding: false, - ), - ); - }, - ), - ); - }, - ); - } - }, - ), - DrawerItem( - title: l10n.restorePolygonIdCredentials, - onTap: () { - securityCheck( - context: context, - localAuthApi: LocalAuthApi(), - onSuccess: () { - Navigator.of(context).push( - RestoreCredentialMnemonicPage.route( - title: l10n.restorePolygonIdCredentials, - isValidCallback: () { - Navigator.of(context).push( - RestorePolygonIdCredentialPage.route(), - ); - }, - ), - ); - }, - ); - }, - ), - ], - ), - ), - ), - ), - ); - } -} diff --git a/lib/dashboard/drawer/wallet_security/src/view/wallet_security_menu.dart b/lib/dashboard/drawer/wallet_security/src/view/wallet_security_menu.dart index debc59010..dd7d20968 100644 --- a/lib/dashboard/drawer/wallet_security/src/view/wallet_security_menu.dart +++ b/lib/dashboard/drawer/wallet_security/src/view/wallet_security_menu.dart @@ -102,7 +102,21 @@ class WalletSecurityView extends StatelessWidget { title: l10n.backup, onTap: () async { if (Parameters.useMnemonicsForBackup) { - await Navigator.of(context).push(BackupMenu.route()); + await securityCheck( + context: context, + localAuthApi: LocalAuthApi(), + onSuccess: () { + Navigator.of(context).push( + BackupMnemonicPage.route( + title: l10n.backupCredential, + isValidCallback: () { + Navigator.of(context) + .push(BackupCredentialPage.route()); + }, + ), + ); + }, + ); } else { await Navigator.of(context) .push(BackupCredentialPage.route()); @@ -113,7 +127,38 @@ class WalletSecurityView extends StatelessWidget { title: l10n.restore, onTap: () async { if (Parameters.useMnemonicsForBackup) { - await Navigator.of(context).push(RestoreMenu.route()); + final confirm = await showDialog( + context: context, + builder: (context) => ConfirmDialog( + title: l10n.warningDialogTitle, + subtitle: + l10n.restorationCredentialWarningDialogSubtitle, + yes: l10n.showDialogYes, + no: l10n.showDialogNo, + ), + ) ?? + false; + + if (confirm) { + await securityCheck( + context: context, + localAuthApi: LocalAuthApi(), + onSuccess: () { + Navigator.of(context).push( + RestoreCredentialMnemonicPage.route( + title: l10n.restoreCredential, + isValidCallback: () { + Navigator.of(context).push( + RestoreCredentialPage.route( + fromOnBoarding: false, + ), + ); + }, + ), + ); + }, + ); + } } else { await Navigator.of(context).push( RestoreCredentialPage.route(fromOnBoarding: false),