From 49327f75667511d541949244de09f0578811e304 Mon Sep 17 00:00:00 2001 From: reasje Date: Wed, 18 Oct 2023 14:06:00 +0330 Subject: [PATCH] fix: Added duplicate account check in import --- assets/flutter_i18n/en.json | 3 ++- .../import_account/import_account_page.dart | 8 +++++-- .../import_account_presenter.dart | 23 ++++++++++++++++++- .../import_account/import_account_state.dart | 3 ++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/assets/flutter_i18n/en.json b/assets/flutter_i18n/en.json index 334f3b21..97bf7f83 100644 --- a/assets/flutter_i18n/en.json +++ b/assets/flutter_i18n/en.json @@ -298,5 +298,6 @@ "view_private_key_notice": "Warning: Never disclose this key. Anyone with your private keys can steal any assets held in your account.", "removing_account": "Removing account", "removing_account_warning": "Are you sure you want to remove this account?", - "remove": "Remove" + "remove": "Remove", + "duplicate_account_import_notice": "The account you are trying to import is a duplicate" } \ No newline at end of file diff --git a/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_page.dart b/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_page.dart index c3c9750b..282c9de7 100644 --- a/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_page.dart +++ b/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_page.dart @@ -49,8 +49,12 @@ class ImportAccountPage extends HookConsumerWidget { translate('x_not_empty') .replaceFirst('{0}', translate('private_key'))); if (res != null) return res; - return Validation.checkEthereumPrivateKey( - context, value ?? ''); + + final isPrivateKey = + Validation.checkEthereumPrivateKey(context, value ?? ''); + if (isPrivateKey != null) return isPrivateKey; + + return presenter.checkDuplicate(value ?? ''); }, onChanged: (value) { presenter.changeAbleToSave( diff --git a/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_presenter.dart b/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_presenter.dart index b8f807ed..7854ee7d 100644 --- a/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_presenter.dart +++ b/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_presenter.dart @@ -18,10 +18,19 @@ class ImportAccountPresenter extends CompletePresenter { final TextEditingController privateKeyController = TextEditingController(); + @override + void initState() { + super.initState(); + + listen(_accountUserCase.accounts, (value) { + notify(() => state.accounts = value); + }); + } + void onSave() async { loading = true; try { - final index = _accountUserCase.accounts.value.length; + final index = state.accounts.length; final privateKey = privateKeyController.text; final newAccount = @@ -41,6 +50,18 @@ class ImportAccountPresenter extends CompletePresenter { } } + String? checkDuplicate(String privateKey) { + if (privateKey.isEmpty) return translate('invalid_format'); + + final foundIndex = state.accounts + .indexWhere((element) => element.privateKey == privateKey); + + if (foundIndex != -1) { + return translate('duplicate_account_import_notice')!; + } + return null; + } + void changeAbleToSave(bool value) { notify(() => state.ableToSave = value); } diff --git a/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_state.dart b/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_state.dart index 6052c416..62dba5ba 100644 --- a/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_state.dart +++ b/lib/features/settings/subfeatures/accounts/subfeatures/import_account/import_account_state.dart @@ -4,7 +4,8 @@ import 'package:mxc_logic/mxc_logic.dart'; class ImportAccountState with EquatableMixin { bool ableToSave = false; bool isLoading = false; + List accounts = []; @override - List get props => [ableToSave, isLoading]; + List get props => [ableToSave, isLoading, accounts]; }