cake_wallet/lib/view_model/wallet_creation_vm.dart

161 lines
5.8 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';
import 'package:cw_core/balance.dart';
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/transaction_info.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';
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/haven/haven.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 {
//! Create a restoredWallet from the scanned wallet parameters
final restoredWallet =
await createNewWalletWithoutSwitching(options: options, restoreWallet: restoreWallet);
print('Restored Wallet Address ' + restoredWallet.walletAddresses.address);
//TODO Get transactions details to verify 10 confirmations
//! Create the newWallet that will received the funds
final newWallet = await createNewWalletWithoutSwitching(
options: options,
regenerateName: true,
);
final newWalletAddress = newWallet.walletAddresses.address;
print('New Wallet Address ' + newWalletAddress);
//! Switch to the restoredWallet in order to activate the node connection
_appStore.changeCurrentWallet(restoredWallet);
//! Sweep all funds from restoredWallet to newWallet
await sweepAllFundsToNewWallet(type, newWalletAddress, restoreWallet?.txId ?? '');
//! Switch back to new wallet
_appStore.changeCurrentWallet(newWallet);
//! Add the new Wallet info to the walletInfoSource
await _walletInfoSource.add(newWallet.walletInfo);
//! Approve authentication as successful
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
}
}
Future<WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>>
createNewWalletWithoutSwitching(
{dynamic options, RestoredWallet? restoreWallet, bool regenerateName = false}) async {
state = IsExecutingState();
if (name.isEmpty) {
name = await generateName();
}
if (regenerateName) {
name = await generateName();
}
walletCreationService.checkIfExists(name);
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);
final walletInfo = WalletInfo.external(
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);
credentials.walletInfo = walletInfo;
final wallet = restoreWallet != null
? await processFromRestoredWallet(credentials, restoreWallet)
: await process(credentials);
walletInfo.address = wallet.walletAddresses.address;
return wallet;
}
Future<Map<String, dynamic>> sweepAllFundsToNewWallet(
WalletType type, String address, String paymentId) async {
final currentNode = _appStore.settingsStore.getCurrentNode(type);
final result = await walletCreationService.sweepAllFunds(currentNode, address, paymentId);
return result;
}
WalletCredentials getCredentials(dynamic options) {
switch (type) {
case WalletType.monero:
return monero!
.createMoneroNewWalletCredentials(name: name, language: options as String? ?? '');
case WalletType.bitcoin:
return bitcoin!.createBitcoinNewWalletCredentials(name: name);
case WalletType.litecoin:
return bitcoin!.createBitcoinNewWalletCredentials(name: name);
case WalletType.haven:
return haven!
.createHavenNewWalletCredentials(name: name, language: options as String? ?? '');
default:
throw Exception('Unexpected type: ${type.toString()}');
}
}
Future<WalletBase> process(WalletCredentials credentials) {
walletCreationService.changeWalletType(type: type);
return walletCreationService.create(credentials);
}
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
}