Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: Added duplicate account check in import
Browse files Browse the repository at this point in the history
  • Loading branch information
reasje committed Oct 18, 2023
1 parent 187f634 commit 49327f7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion assets/flutter_i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ class ImportAccountPresenter extends CompletePresenter<ImportAccountState> {

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 =
Expand All @@ -41,6 +50,18 @@ class ImportAccountPresenter extends CompletePresenter<ImportAccountState> {
}
}

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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:mxc_logic/mxc_logic.dart';
class ImportAccountState with EquatableMixin {
bool ableToSave = false;
bool isLoading = false;
List<Account> accounts = [];

@override
List<Object?> get props => [ableToSave, isLoading];
List<Object?> get props => [ableToSave, isLoading, accounts];
}

0 comments on commit 49327f7

Please sign in to comment.