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 {
|
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
|
|
|
|
2020-11-12 16:31:53 +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
|
|
|
|
2022-06-29 17:21:21 +00:00
|
|
|
bool nameExists(String name) {
|
|
|
|
final walletNameList = _walletInfoSource.values.map((e) => e.name.toLowerCase()).toList();
|
|
|
|
|
|
|
|
return walletNameList.contains(name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
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();
|
2022-01-03 18:25:48 +00:00
|
|
|
if (name?.isEmpty ?? true) {
|
|
|
|
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);
|
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);
|
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();
|
|
|
|
}
|