Add aditional check if wallet exists with special name

This commit is contained in:
M
2022-07-14 12:48:59 +01:00
parent 52b8bafac9
commit a63c099f26
7 changed files with 45 additions and 28 deletions
+21 -1
View File
@@ -1,6 +1,8 @@
import 'package:cake_wallet/di.dart';
import 'package:cw_core/wallet_info.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive/hive.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:cake_wallet/core/key_service.dart';
import 'package:cw_core/wallet_base.dart';
@@ -14,7 +16,8 @@ class WalletCreationService {
{WalletType initialType,
this.secureStorage,
this.keyService,
this.sharedPreferences})
this.sharedPreferences,
this.walletInfoSource})
: type = initialType {
if (type != null) {
changeWalletType(type: type);
@@ -25,6 +28,7 @@ class WalletCreationService {
final FlutterSecureStorage secureStorage;
final SharedPreferences sharedPreferences;
final KeyService keyService;
final Box<WalletInfo> walletInfoSource;
WalletService _service;
void changeWalletType({@required WalletType type}) {
@@ -32,7 +36,21 @@ class WalletCreationService {
_service = getIt.get<WalletService>(param1: type);
}
bool exists(String name) {
final walletName = name.toLowerCase();
return walletInfoSource
.values
.any((walletInfo) => walletInfo.name.toLowerCase() == walletName);
}
void checkIfExists(String name) {
if (exists(name)) {
throw Exception('Wallet with name ${name} already exists!');
}
}
Future<WalletBase> create(WalletCredentials credentials) async {
checkIfExists(credentials.name);
final password = generateWalletPassword(type);
credentials.password = password;
await keyService.saveWalletPassword(
@@ -41,6 +59,7 @@ class WalletCreationService {
}
Future<WalletBase> restoreFromKeys(WalletCredentials credentials) async {
checkIfExists(credentials.name);
final password = generateWalletPassword(type);
credentials.password = password;
await keyService.saveWalletPassword(
@@ -49,6 +68,7 @@ class WalletCreationService {
}
Future<WalletBase> restoreFromSeed(WalletCredentials credentials) async {
checkIfExists(credentials.name);
final password = generateWalletPassword(type);
credentials.password = password;
await keyService.saveWalletPassword(