cake_wallet/lib/view_model/wallet_creation_vm.dart

78 lines
2.5 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/core/wallet_creation_service.dart';
2020-06-20 07:10:00 +00:00
import 'package:flutter/foundation.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,
2020-07-06 20:09:03 +00:00
{@required this.type, @required this.isRecovery}) {
2020-09-21 11:50:26 +00:00
state = InitialExecutionState();
2020-06-20 07:10:00 +00:00
name = '';
}
@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);
2020-06-20 07:10:00 +00:00
Future<void> create({dynamic options}) async {
try {
2020-09-21 11:50:26 +00:00
state = IsExecutingState();
if (name?.isEmpty ?? true) {
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);
2020-09-15 20:35:49 +00:00
final credentials = getCredentials(options);
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);
2020-09-15 20:35:49 +00:00
credentials.walletInfo = walletInfo;
2020-09-21 11:50:26 +00:00
final wallet = await process(credentials);
walletInfo.address = wallet.walletAddresses.address;
2020-07-06 20:09:03 +00:00
await _walletInfoSource.add(walletInfo);
_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();
}