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

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(

View file

@ -215,7 +215,8 @@ Future setup(
initialType: type,
keyService: getIt.get<KeyService>(),
secureStorage: getIt.get<FlutterSecureStorage>(),
sharedPreferences: getIt.get<SharedPreferences>()));
sharedPreferences: getIt.get<SharedPreferences>(),
walletInfoSource: _walletInfoSource));
getIt.registerFactoryParam<WalletNewVM, WalletType, void>((type, _) =>
WalletNewVM(getIt.get<AppStore>(),

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/core/wallet_creation_service.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
@ -15,7 +16,7 @@ part 'wallet_creation_vm.g.dart';
class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM;
abstract class WalletCreationVMBase with Store {
WalletCreationVMBase(this._appStore, this._walletInfoSource,
WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService,
{@required this.type, @required this.isRecovery}) {
state = InitialExecutionState();
name = '';
@ -29,14 +30,12 @@ abstract class WalletCreationVMBase with Store {
WalletType type;
final bool isRecovery;
final WalletCreationService walletCreationService;
final Box<WalletInfo> _walletInfoSource;
final AppStore _appStore;
bool nameExists(String name) {
final walletNameList = _walletInfoSource.values.map((e) => e.name.toLowerCase()).toList();
return walletNameList.contains(name.toLowerCase());
}
bool nameExists(String name)
=> walletCreationService.exists(name);
Future<void> create({dynamic options}) async {
try {
@ -44,6 +43,8 @@ abstract class WalletCreationVMBase with Store {
if (name?.isEmpty ?? true) {
name = await generateName();
}
walletCreationService.checkIfExists(name);
final dirPath = await pathForWalletDir(name: name, type: type);
final path = await pathForWallet(name: name, type: type);
final credentials = getCredentials(options);

View file

@ -17,19 +17,17 @@ part 'wallet_new_vm.g.dart';
class WalletNewVM = WalletNewVMBase with _$WalletNewVM;
abstract class WalletNewVMBase extends WalletCreationVM with Store {
WalletNewVMBase(AppStore appStore, this._walletCreationService,
WalletNewVMBase(AppStore appStore, WalletCreationService walletCreationService,
Box<WalletInfo> walletInfoSource,
{@required WalletType type})
: selectedMnemonicLanguage = '',
super(appStore, walletInfoSource, type: type, isRecovery: false);
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: false);
@observable
String selectedMnemonicLanguage;
bool get hasLanguageSelector => type == WalletType.monero || type == WalletType.haven;
final WalletCreationService _walletCreationService;
@override
WalletCredentials getCredentials(dynamic options) {
switch (type) {
@ -50,7 +48,7 @@ abstract class WalletNewVMBase extends WalletCreationVM with Store {
@override
Future<WalletBase> process(WalletCredentials credentials) async {
_walletCreationService.changeWalletType(type: type);
return _walletCreationService.create(credentials);
walletCreationService.changeWalletType(type: type);
return walletCreationService.create(credentials);
}
}

View file

@ -20,9 +20,9 @@ class WalletRestorationFromKeysVM = WalletRestorationFromKeysVMBase
abstract class WalletRestorationFromKeysVMBase extends WalletCreationVM
with Store {
WalletRestorationFromKeysVMBase(AppStore appStore,
this._walletCreationService, Box<WalletInfo> walletInfoSource,
WalletCreationService walletCreationService, Box<WalletInfo> walletInfoSource,
{@required WalletType type, @required this.language})
: super(appStore, walletInfoSource, type: type, isRecovery: true);
: super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true);
@observable
int height;
@ -42,7 +42,6 @@ abstract class WalletRestorationFromKeysVMBase extends WalletCreationVM
bool get hasRestorationHeight => type == WalletType.monero;
final String language;
final WalletCreationService _walletCreationService;
@override
WalletCredentials getCredentials(dynamic options) {
@ -68,5 +67,5 @@ abstract class WalletRestorationFromKeysVMBase extends WalletCreationVM
@override
Future<WalletBase> process(WalletCredentials credentials) async =>
_walletCreationService.restoreFromKeys(credentials);
walletCreationService.restoreFromKeys(credentials);
}

View file

@ -20,9 +20,9 @@ class WalletRestorationFromSeedVM = WalletRestorationFromSeedVMBase
abstract class WalletRestorationFromSeedVMBase extends WalletCreationVM
with Store {
WalletRestorationFromSeedVMBase(AppStore appStore,
this._walletCreationService, Box<WalletInfo> walletInfoSource,
WalletCreationService walletCreationService, Box<WalletInfo> walletInfoSource,
{@required WalletType type, @required this.language, this.seed})
: super(appStore, walletInfoSource, type: type, isRecovery: true);
: super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true);
@observable
String seed;
@ -33,7 +33,6 @@ abstract class WalletRestorationFromSeedVMBase extends WalletCreationVM
bool get hasRestorationHeight => type == WalletType.monero;
final String language;
final WalletCreationService _walletCreationService;
@override
WalletCredentials getCredentials(dynamic options) {
@ -53,5 +52,5 @@ abstract class WalletRestorationFromSeedVMBase extends WalletCreationVM
@override
Future<WalletBase> process(WalletCredentials credentials) async =>
_walletCreationService.restoreFromSeed(credentials);
walletCreationService.restoreFromSeed(credentials);
}

View file

@ -22,7 +22,7 @@ class WalletRestoreViewModel = WalletRestoreViewModelBase
with _$WalletRestoreViewModel;
abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
WalletRestoreViewModelBase(AppStore appStore, this._walletCreationService,
WalletRestoreViewModelBase(AppStore appStore, WalletCreationService walletCreationService,
Box<WalletInfo> walletInfoSource,
{@required WalletType type})
: availableModes = (type == WalletType.monero || type == WalletType.haven)
@ -30,11 +30,11 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
: [WalletRestoreMode.seed],
hasSeedLanguageSelector = type == WalletType.monero || type == WalletType.haven,
hasBlockchainHeightLanguageSelector = type == WalletType.monero || type == WalletType.haven,
super(appStore, walletInfoSource, type: type, isRecovery: true) {
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true) {
isButtonEnabled =
!hasSeedLanguageSelector && !hasBlockchainHeightLanguageSelector;
mode = WalletRestoreMode.seed;
_walletCreationService.changeWalletType(type: type);
walletCreationService.changeWalletType(type: type);
}
static const moneroSeedMnemonicLength = 25;
@ -44,7 +44,6 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
final List<WalletRestoreMode> availableModes;
final bool hasSeedLanguageSelector;
final bool hasBlockchainHeightLanguageSelector;
final WalletCreationService _walletCreationService;
@observable
WalletRestoreMode mode;
@ -123,9 +122,9 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
@override
Future<WalletBase> process(WalletCredentials credentials) async {
if (mode == WalletRestoreMode.keys) {
return _walletCreationService.restoreFromKeys(credentials);
return walletCreationService.restoreFromKeys(credentials);
}
return _walletCreationService.restoreFromSeed(credentials);
return walletCreationService.restoreFromSeed(credentials);
}
}