Restore Wallet WIP [skip-ci]

This commit is contained in:
Blazebrain 2023-06-09 08:37:02 +01:00
parent f8f682dea3
commit 6d7a384a98
8 changed files with 107 additions and 25 deletions

View file

@ -175,9 +175,9 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
PendingTransactionDescription pendingTransactionDescription;
if (!(syncStatus is SyncedSyncStatus)) {
throw MoneroTransactionCreationException('The wallet is not synced.');
}
// if (!(syncStatus is SyncedSyncStatus)) {
// throw MoneroTransactionCreationException('The wallet is not synced.');
// }
if (hasMultiDestination) {
if (outputs.any((item) => item.sendAll

View file

@ -298,7 +298,7 @@ Future setup({
(WalletType type) => getIt.get<WalletService>(param1: type)));
getIt.registerFactoryParam<WalletNewVM, WalletType, void>((type, _) => WalletNewVM(
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type), _walletInfoSource,
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type),getIt.get<FiatConversionStore>(), _walletInfoSource,
type: type));
getIt.registerFactoryParam<WalletRestorationFromSeedVM, List, void>((args, _) {
@ -307,7 +307,7 @@ Future setup({
final mnemonic = args[2] as String;
return WalletRestorationFromSeedVM(
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type), _walletInfoSource,
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type), _walletInfoSource , getIt.get<FiatConversionStore>(),
type: type, language: language, seed: mnemonic);
});
@ -316,13 +316,13 @@ Future setup({
final language = args[1] as String;
return WalletRestorationFromKeysVM(
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type), _walletInfoSource,
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type), _walletInfoSource, getIt.get<FiatConversionStore>(),
type: type, language: language);
});
getIt.registerFactoryParam<WalletRestorationFromQRVM, WalletType, void>((WalletType type, _) {
return WalletRestorationFromQRVM(getIt.get<AppStore>(),
getIt.get<WalletCreationService>(param1: type), _walletInfoSource, type);
getIt.get<WalletCreationService>(param1: type), getIt.get<FiatConversionStore>(), _walletInfoSource, type);
});
getIt.registerFactory<WalletAddressListViewModel>(() => WalletAddressListViewModel(
@ -739,7 +739,7 @@ Future setup({
getIt.registerFactoryParam<WalletRestoreViewModel, WalletType, void>((type, _) =>
WalletRestoreViewModel(
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type), _walletInfoSource,
getIt.get<AppStore>(), getIt.get<WalletCreationService>(param1: type),getIt.get<FiatConversionStore>(), _walletInfoSource,
type: type));
getIt.registerFactoryParam<WalletRestorePage, WalletType, void>(

View file

@ -1,4 +1,5 @@
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/view_model/restore/restore_mode.dart';
import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:hive/hive.dart';
@ -18,14 +19,14 @@ part 'restore_from_qr_vm.g.dart';
class WalletRestorationFromQRVM = WalletRestorationFromQRVMBase with _$WalletRestorationFromQRVM;
abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store {
WalletRestorationFromQRVMBase(AppStore appStore, WalletCreationService walletCreationService,
WalletRestorationFromQRVMBase(AppStore appStore, WalletCreationService walletCreationService,FiatConversionStore fiatConversationStore,
Box<WalletInfo> walletInfoSource, WalletType type)
: height = 0,
viewKey = '',
spendKey = '',
wif = '',
address = '',
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true);
super(appStore, walletInfoSource, walletCreationService, fiatConversationStore, type: type, isRecovery: true);
@observable
int height;

View file

@ -1,6 +1,11 @@
import 'dart:async';
import 'package:cake_wallet/core/wallet_creation_service.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:cake_wallet/view_model/send/output.dart';
import 'package:cw_core/balance.dart';
import 'package:cw_core/pending_transaction.dart';
import 'package:cw_core/transaction_history.dart';
import 'package:cw_core/transaction_info.dart';
import 'package:hive/hive.dart';
@ -22,7 +27,7 @@ part 'wallet_creation_vm.g.dart';
class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM;
abstract class WalletCreationVMBase with Store {
WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService,
WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService, this._fiatConversationStore,
{required this.type, required this.isRecovery})
: state = InitialExecutionState(),
name = '';
@ -33,11 +38,17 @@ abstract class WalletCreationVMBase with Store {
@observable
ExecutionState state;
@observable
PendingTransaction? pendingTransaction;
WalletType type;
final bool isRecovery;
final WalletCreationService walletCreationService;
final Box<WalletInfo> _walletInfoSource;
final AppStore _appStore;
final FiatConversionStore _fiatConversationStore;
Completer<void> syncCompleter = Completer<void>();
bool nameExists(String name) => walletCreationService.exists(name);
@ -64,8 +75,10 @@ abstract class WalletCreationVMBase with Store {
//! Switch to the restoredWallet in order to activate the node connection
_appStore.changeCurrentWallet(restoredWallet);
await restoredWallet.startSync();
//! Sweep all funds from restoredWallet to newWallet
await sweepAllFundsToNewWallet(type, newWalletAddress, restoreWallet?.txId ?? '');
await sweepAllFundsToNewWallet(restoredWallet, type, newWalletAddress, restoreWallet?.txId ?? '');
//! Switch back to new wallet
_appStore.changeCurrentWallet(newWallet);
@ -117,15 +130,78 @@ abstract class WalletCreationVMBase with Store {
? 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;
Future<void> sweepAllFundsToNewWallet(WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo> wallet,
WalletType type, String newWalletAddress, String paymentId) async {
final output = Output(wallet, _appStore.settingsStore, _fiatConversationStore, () => wallet.currency);
output.address = newWalletAddress;
output.sendAll = true;
output.note = 'testing the sweep all function';
final credentials = _credentials(type, wallet.currency.title, output);
print('About to enter create function');
await createTransaction(wallet, credentials);
// final currentNode = _appStore.settingsStore.getCurrentNode(type);
// final result = await walletCreationService.sweepAllFunds(currentNode, newWalletAddress, paymentId);
}
Object _credentials(WalletType type,String cryptoCurrencyTitle,Output output ) {
switch (type) {
case WalletType.bitcoin:
final priority = _appStore.settingsStore.priority[type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${type}');
}
return bitcoin!.createBitcoinTransactionCredentials([output], priority: priority);
case WalletType.litecoin:
final priority = _appStore.settingsStore.priority[type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${type}');
}
return bitcoin!.createBitcoinTransactionCredentials([output], priority: priority);
case WalletType.monero:
final priority = _appStore.settingsStore.priority[type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${type}');
}
return monero!.createMoneroTransactionCreationCredentials(
outputs:[output], priority: priority);
case WalletType.haven:
final priority = _appStore.settingsStore.priority[type];
if (priority == null) {
throw Exception('Priority is null for wallet type: ${type}');
}
return haven!.createHavenTransactionCreationCredentials(
outputs: [output], priority: priority, assetType: cryptoCurrencyTitle);
default:
throw Exception('Unexpected wallet type: ${type}');
}
}
@action
Future<void> createTransaction(WalletBase wallet, Object credentials) async {
try {
print('in here');
state = IsExecutingState();
print('about to enter wallet create transaction function');
pendingTransaction = await wallet.createTransaction(credentials);
state = ExecutedSuccessfullyState();
} catch (e) {
state = FailureState(e.toString());
}
}
WalletCredentials getCredentials(dynamic options) {

View file

@ -11,16 +11,18 @@ import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/haven/haven.dart';
import '../store/dashboard/fiat_conversion_store.dart';
part 'wallet_new_vm.g.dart';
class WalletNewVM = WalletNewVMBase with _$WalletNewVM;
abstract class WalletNewVMBase extends WalletCreationVM with Store {
WalletNewVMBase(AppStore appStore, WalletCreationService walletCreationService,
WalletNewVMBase(AppStore appStore, WalletCreationService walletCreationService,FiatConversionStore fiatConversationStore,
Box<WalletInfo> walletInfoSource,
{required WalletType type})
: selectedMnemonicLanguage = '',
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: false);
super(appStore, walletInfoSource, walletCreationService,fiatConversationStore, type: type, isRecovery: false);
@observable
String selectedMnemonicLanguage;

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
@ -21,14 +22,14 @@ class WalletRestorationFromKeysVM = WalletRestorationFromKeysVMBase
abstract class WalletRestorationFromKeysVMBase extends WalletCreationVM
with Store {
WalletRestorationFromKeysVMBase(AppStore appStore,
WalletCreationService walletCreationService, Box<WalletInfo> walletInfoSource,
WalletCreationService walletCreationService, Box<WalletInfo> walletInfoSource,FiatConversionStore fiatConversationStore,
{required WalletType type, required this.language})
: height = 0,
viewKey = '',
spendKey = '',
wif = '',
address = '',
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true);
super(appStore, walletInfoSource, walletCreationService, fiatConversationStore, type: type, isRecovery: true);
@observable
int height;

View file

@ -1,3 +1,4 @@
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
@ -21,10 +22,10 @@ class WalletRestorationFromSeedVM = WalletRestorationFromSeedVMBase
abstract class WalletRestorationFromSeedVMBase extends WalletCreationVM
with Store {
WalletRestorationFromSeedVMBase(AppStore appStore,
WalletCreationService walletCreationService, Box<WalletInfo> walletInfoSource,
WalletCreationService walletCreationService, Box<WalletInfo> walletInfoSource,FiatConversionStore fiatConversationStore,
{required WalletType type, required this.language, this.seed = ''})
: height = 0,
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true);
super(appStore, walletInfoSource, walletCreationService ,fiatConversationStore, type: type, isRecovery: true);
@observable
String seed;

View file

@ -1,5 +1,6 @@
import 'package:cake_wallet/bitcoin/bitcoin.dart';
import 'package:cake_wallet/core/mnemonic_length.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/view_model/restore/restore_wallet.dart';
import 'package:flutter/foundation.dart';
import 'package:hive/hive.dart';
@ -23,7 +24,7 @@ class WalletRestoreViewModel = WalletRestoreViewModelBase
with _$WalletRestoreViewModel;
abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
WalletRestoreViewModelBase(AppStore appStore, WalletCreationService walletCreationService,
WalletRestoreViewModelBase(AppStore appStore, WalletCreationService walletCreationService,FiatConversionStore fiatConversationStore,
Box<WalletInfo> walletInfoSource,
{required WalletType type})
: availableModes = (type == WalletType.monero || type == WalletType.haven)
@ -33,7 +34,7 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store {
hasBlockchainHeightLanguageSelector = type == WalletType.monero || type == WalletType.haven,
isButtonEnabled = false,
mode = WalletRestoreMode.seed,
super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true) {
super(appStore, walletInfoSource, walletCreationService, fiatConversationStore, type: type, isRecovery: true) {
isButtonEnabled =
!hasSeedLanguageSelector && !hasBlockchainHeightLanguageSelector;
walletCreationService.changeWalletType(type: type);