cake_wallet/lib/view_model/wallet_creation_vm.dart

97 lines
3.3 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/core/wallet_creation_service.dart';
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 {
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
WalletType type;
2020-07-06 20:09:03 +00:00
final bool isRecovery;
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
bool nameExists(String name)
=> walletCreationService.exists(name);
bool typeExists(WalletType type)
=> walletCreationService.typeExists(type);
Future<void> create({dynamic options, RestoredWallet? restoreWallet}) async {
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) {
name = await generateName();
}
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);
final credentials = restoreWallet != null
? getCredentialsFromRestoredWallet(options, restoreWallet)
: getCredentials(options);
2020-09-15 20:35:49 +00:00
final walletInfo = WalletInfo.external(
id: WalletBase.idFor(name, type),
2020-07-06 20:09:03 +00:00
name: name,
type: type,
isRecovery: isRecovery,
2020-09-15 20:35:49 +00:00
restoreHeight: credentials.height ?? 0,
2020-09-21 11:50:26 +00:00
date: DateTime.now(),
path: path,
dirPath: dirPath,
2022-10-12 17:09:57 +00:00
address: '',
showIntroCakePayCard: (!walletCreationService.typeExists(type)) && type != WalletType.haven);
2020-09-15 20:35:49 +00:00
credentials.walletInfo = walletInfo;
//! Restored
final wallet = restoreWallet != null
? await processFromRestoredWallet(credentials, restoreWallet)
: await process(credentials);
walletInfo.address = wallet.walletAddresses.address;
2020-07-06 20:09:03 +00:00
await _walletInfoSource.add(walletInfo);
//---- Code to create new
//! Before we switch
_appStore.changeCurrentWallet(wallet);
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
}
}
WalletCredentials getCredentials(dynamic options) =>
throw UnimplementedError();
2020-09-21 11:50:26 +00:00
Future<WalletBase> process(WalletCredentials credentials) =>
2020-06-20 07:10:00 +00:00
throw UnimplementedError();
WalletCredentials getCredentialsFromRestoredWallet(dynamic options, RestoredWallet restoreWallet) =>
throw UnimplementedError();
Future<WalletBase> processFromRestoredWallet(WalletCredentials credentials, RestoredWallet restoreWallet) =>
throw UnimplementedError();
2020-06-20 07:10:00 +00:00
}