cake_wallet/lib/view_model/wallet_creation_vm.dart

69 lines
2.2 KiB
Dart
Raw Normal View History

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';
import 'package:cake_wallet/core/wallet_base.dart';
2020-06-20 07:10:00 +00:00
import 'package:cake_wallet/core/wallet_credentials.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/entities/pathForWallet.dart';
import 'package:cake_wallet/entities/wallet_info.dart';
import 'package:cake_wallet/entities/wallet_type.dart';
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 {
2020-09-21 11:50:26 +00:00
WalletCreationVMBase(this._appStore, this._walletInfoSource,
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;
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
Future<void> create({dynamic options}) async {
try {
2020-09-21 11:50:26 +00:00
state = IsExecutingState();
2020-10-29 10:49:07 +00:00
name = await generateName();
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.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();
}