2022-07-14 11:48:59 +00:00
|
|
|
import 'package:cake_wallet/core/wallet_creation_service.dart';
|
2023-04-10 23:16:13 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2023-03-30 22:33:59 +00:00
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
2023-08-04 17:55:56 +00:00
|
|
|
import 'package:cake_wallet/di.dart';
|
|
|
|
import 'package:cake_wallet/entities/background_tasks.dart';
|
2023-04-21 18:36:47 +00:00
|
|
|
import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
|
2020-07-06 20:09:03 +00:00
|
|
|
import 'package:hive/hive.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
import 'package:mobx/mobx.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/core/execution_state.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/wallet_base.dart';
|
|
|
|
import 'package:cw_core/wallet_credentials.dart';
|
|
|
|
import 'package:cw_core/pathForWallet.dart';
|
|
|
|
import 'package:cw_core/wallet_info.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/store/app_store.dart';
|
2020-10-22 18:24:24 +00:00
|
|
|
import 'package:cake_wallet/entities/generate_name.dart';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
part 'wallet_creation_vm.g.dart';
|
|
|
|
|
|
|
|
class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM;
|
|
|
|
|
|
|
|
abstract class WalletCreationVMBase with Store {
|
2022-07-14 11:48:59 +00:00
|
|
|
WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService,
|
2022-10-12 17:09:57 +00:00
|
|
|
{required this.type, required this.isRecovery})
|
|
|
|
: state = InitialExecutionState(),
|
|
|
|
name = '';
|
2020-06-20 07:10:00 +00:00
|
|
|
|
|
|
|
@observable
|
|
|
|
String name;
|
|
|
|
|
|
|
|
@observable
|
2020-09-21 11:50:26 +00:00
|
|
|
ExecutionState state;
|
2020-06-20 07:10:00 +00:00
|
|
|
|
2023-03-30 22:33:59 +00:00
|
|
|
@observable
|
|
|
|
String? walletPassword;
|
|
|
|
|
2023-04-10 23:16:13 +00:00
|
|
|
@observable
|
|
|
|
String? repeatedWalletPassword;
|
|
|
|
|
2023-03-30 22:33:59 +00:00
|
|
|
bool get hasWalletPassword => SettingsStoreBase.walletPasswordDirectInput;
|
|
|
|
|
2020-11-12 16:31:53 +00:00
|
|
|
WalletType type;
|
2020-07-06 20:09:03 +00:00
|
|
|
final bool isRecovery;
|
2022-07-14 11:48:59 +00:00
|
|
|
final WalletCreationService walletCreationService;
|
2020-09-10 14:51:59 +00:00
|
|
|
final Box<WalletInfo> _walletInfoSource;
|
2020-09-21 11:50:26 +00:00
|
|
|
final AppStore _appStore;
|
2020-06-20 07:10:00 +00:00
|
|
|
|
2023-10-05 01:09:07 +00:00
|
|
|
bool nameExists(String name) => walletCreationService.exists(name);
|
2022-06-29 17:21:21 +00:00
|
|
|
|
2023-10-05 01:09:07 +00:00
|
|
|
bool typeExists(WalletType type) => walletCreationService.typeExists(type);
|
2022-08-30 18:03:02 +00:00
|
|
|
|
2023-11-14 23:17:15 +00:00
|
|
|
Future<void> create({dynamic options, RestoredWallet? restoreWallet, bool? isTestnet}) async {
|
2023-04-21 18:36:47 +00:00
|
|
|
final type = restoreWallet?.type ?? this.type;
|
2020-06-20 07:10:00 +00:00
|
|
|
try {
|
2020-09-21 11:50:26 +00:00
|
|
|
state = IsExecutingState();
|
2022-10-12 17:09:57 +00:00
|
|
|
if (name.isEmpty) {
|
2023-10-05 01:09:07 +00:00
|
|
|
name = await generateName();
|
2022-01-03 18:25:48 +00:00
|
|
|
}
|
2022-07-14 11:48:59 +00:00
|
|
|
|
2023-04-10 23:16:13 +00:00
|
|
|
if (hasWalletPassword && (walletPassword?.isEmpty ?? true)) {
|
|
|
|
throw Exception(S.current.wallet_password_is_empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasWalletPassword && walletPassword != repeatedWalletPassword) {
|
|
|
|
throw Exception(S.current.repeated_password_is_incorrect);
|
|
|
|
}
|
|
|
|
|
2022-07-14 11:48:59 +00:00
|
|
|
walletCreationService.checkIfExists(name);
|
2020-09-21 11:50:26 +00:00
|
|
|
final dirPath = await pathForWalletDir(name: name, type: type);
|
|
|
|
final path = await pathForWallet(name: name, type: type);
|
2023-04-21 18:36:47 +00:00
|
|
|
final credentials = restoreWallet != null
|
|
|
|
? getCredentialsFromRestoredWallet(options, restoreWallet)
|
|
|
|
: getCredentials(options);
|
2023-10-05 01:09:07 +00:00
|
|
|
|
2020-09-15 20:35:49 +00:00
|
|
|
final walletInfo = WalletInfo.external(
|
2023-10-05 01:09:07 +00:00
|
|
|
id: WalletBase.idFor(name, type),
|
|
|
|
name: name,
|
|
|
|
type: type,
|
|
|
|
isRecovery: isRecovery,
|
|
|
|
restoreHeight: credentials.height ?? 0,
|
|
|
|
date: DateTime.now(),
|
|
|
|
path: path,
|
|
|
|
dirPath: dirPath,
|
|
|
|
address: '',
|
|
|
|
showIntroCakePayCard: (!walletCreationService.typeExists(type)) && type != WalletType.haven,
|
|
|
|
derivationPath: credentials.derivationPath,
|
|
|
|
derivationType: credentials.derivationType,
|
|
|
|
);
|
2020-09-15 20:35:49 +00:00
|
|
|
credentials.walletInfo = walletInfo;
|
2023-04-21 18:36:47 +00:00
|
|
|
final wallet = restoreWallet != null
|
|
|
|
? await processFromRestoredWallet(credentials, restoreWallet)
|
|
|
|
: await process(credentials);
|
2021-07-13 05:46:34 +00:00
|
|
|
walletInfo.address = wallet.walletAddresses.address;
|
2020-07-06 20:09:03 +00:00
|
|
|
await _walletInfoSource.add(walletInfo);
|
2020-12-15 16:29:10 +00:00
|
|
|
_appStore.changeCurrentWallet(wallet);
|
2023-08-04 17:55:56 +00:00
|
|
|
getIt.get<BackgroundTasks>().registerSyncTask();
|
2020-09-21 11:50:26 +00:00
|
|
|
_appStore.authenticationStore.allowed();
|
|
|
|
state = ExecutedSuccessfullyState();
|
2020-06-20 07:10:00 +00:00
|
|
|
} catch (e) {
|
2020-09-21 11:50:26 +00:00
|
|
|
state = FailureState(e.toString());
|
2020-06-20 07:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 01:09:07 +00:00
|
|
|
WalletCredentials getCredentials(dynamic options) => throw UnimplementedError();
|
|
|
|
|
|
|
|
Future<WalletBase> process(WalletCredentials credentials) => throw UnimplementedError();
|
2023-04-21 18:36:47 +00:00
|
|
|
|
2023-10-05 01:09:07 +00:00
|
|
|
WalletCredentials getCredentialsFromRestoredWallet(
|
|
|
|
dynamic options, RestoredWallet restoreWallet) =>
|
2023-04-21 18:36:47 +00:00
|
|
|
throw UnimplementedError();
|
|
|
|
|
2023-10-05 01:09:07 +00:00
|
|
|
Future<WalletBase> processFromRestoredWallet(
|
|
|
|
WalletCredentials credentials, RestoredWallet restoreWallet) =>
|
2023-04-21 18:36:47 +00:00
|
|
|
throw UnimplementedError();
|
2020-06-20 07:10:00 +00:00
|
|
|
}
|