From 5c726a639c036a885188f8213ba31cc15615ce05 Mon Sep 17 00:00:00 2001 From: ryleedavis Date: Thu, 28 Sep 2023 16:22:24 -0600 Subject: [PATCH] WIP: call abstract wrapper class for addressInfo, openWallet, deleteWallet, chainHeight --- .../coins/epiccash/epiccash_wallet.dart | 48 ++++++++++----- lib/wallets/example/libepiccash.dart | 58 +++++++++++++------ 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/lib/services/coins/epiccash/epiccash_wallet.dart b/lib/services/coins/epiccash/epiccash_wallet.dart index aae3343e7..b15b2673d 100644 --- a/lib/services/coins/epiccash/epiccash_wallet.dart +++ b/lib/services/coins/epiccash/epiccash_wallet.dart @@ -202,10 +202,6 @@ Future _cancelTransactionWrapper(Tuple2 data) async { return cancelTransaction(data.item1, data.item2); } -Future _deleteWalletWrapper(Tuple2 data) async { - return deleteWallet(data.item1, data.item2); -} - Future deleteEpicWallet({ required String walletId, required SecureStorageInterface secureStore, @@ -229,7 +225,7 @@ Future deleteEpicWallet({ return "Tried to delete non existent epic wallet file with walletId=$walletId"; } else { try { - return _deleteWalletWrapper(Tuple2(wallet, config!)); + return epiccash.LibEpiccash.deleteWallet(wallet: wallet, config: config!); } catch (e, s) { Logging.instance.log("$e\n$s", level: LogLevel.Error); return "deleteEpicWallet($walletId) failed..."; @@ -531,7 +527,11 @@ class EpicCashWallet extends CoinServiceAPI epicboxConfig: epicboxConfig.toString() ); - String? walletAddress = await epiccash.getAddressInfo(data); + String? walletAddress = await epiccash.LibEpiccash.getAddressInfo( + wallet: wallet, + index: index, + epicboxConfig: epicboxConfig.toString(), + ); Logging.instance .log("WALLET_ADDRESS_IS $walletAddress", level: LogLevel.Info); @@ -656,7 +656,8 @@ class EpicCashWallet extends CoinServiceAPI final config = await getRealConfig(); final password = await _secureStore.read(key: '${_walletId}_password'); - final walletOpen = openWallet(config, password!); + final walletOpen = await epiccash.LibEpiccash.openWallet( + config: config, password: password!); await _secureStore.write(key: '${_walletId}_wallet', value: walletOpen); if (getCachedId() == null) { @@ -683,7 +684,11 @@ class EpicCashWallet extends CoinServiceAPI index: index, epicboxConfig: epicboxConfig.toString() ); - String? walletAddress = await epiccash.getAddressInfo(data); + String? walletAddress = await epiccash.LibEpiccash.getAddressInfo( + wallet: wallet, + index: index, + epicboxConfig: epicboxConfig.toString(), + ); Logging.instance .log("WALLET_ADDRESS_IS $walletAddress", level: LogLevel.Info); @@ -741,10 +746,16 @@ class EpicCashWallet extends CoinServiceAPI password: password, name: name ); - await epiccash.createNewWallet(walletData); + await epiccash.LibEpiccash.initializeNewWallet( + config: stringConfig, + mnemonic: mnemonicString, + password: password, + name: name, + ); //Open wallet - final walletOpen = openWallet(stringConfig, password); + final walletOpen = await epiccash.LibEpiccash.openWallet( + config: stringConfig, password: password); await _secureStore.write(key: '${_walletId}_wallet', value: walletOpen); //Store Epic box address info @@ -845,7 +856,13 @@ class EpicCashWallet extends CoinServiceAPI final available = balance.spendable.raw.toInt(); ({String wallet, int amount, int availableAmount}) data = (wallet: wallet!, amount: satoshiAmount, availableAmount: available); - var transactionFees = await epiccash.transactionFees(data); + var transactionFees = await epiccash.LibEpiccash.getTransactionFees( + wallet: wallet, + amount: satoshiAmount, + // todo: double check + minimumConfirmations: MINIMUM_CONFIRMATIONS, + available: available, + ); int realfee = 0; try { @@ -1165,7 +1182,8 @@ class EpicCashWallet extends CoinServiceAPI ]); //Open Wallet - final walletOpen = openWallet(stringConfig, password); + final walletOpen = await epiccash.LibEpiccash.openWallet( + config: stringConfig, password: password); await _secureStore.write(key: '${_walletId}_wallet', value: walletOpen); //Store Epic box address info @@ -1195,10 +1213,8 @@ class EpicCashWallet extends CoinServiceAPI final config = await getRealConfig(); int? latestHeight; await m.protect(() async { - latestHeight = await compute( - _getChainHeightWrapper, - config, - ); + latestHeight = + await epiccash.LibEpiccash.getChainHeight(config: config); }); await updateCachedChainHeight(latestHeight!); diff --git a/lib/wallets/example/libepiccash.dart b/lib/wallets/example/libepiccash.dart index 3fff124e0..8e51e0049 100644 --- a/lib/wallets/example/libepiccash.dart +++ b/lib/wallets/example/libepiccash.dart @@ -292,6 +292,24 @@ abstract class LibEpiccash { } } + static Future _chainHeightWrapper( + ({ + String config, + }) data, + ) async { + return lib_epiccash.getChainHeight(data.config); + } + + static Future getChainHeight({ + required String config, + }) async { + try { + return await compute(_chainHeightWrapper, (config: config,)); + } catch (e) { + throw ("Error getting chain height : ${e.toString()}"); + } + } + /// /// Private function for address info function /// @@ -348,18 +366,18 @@ abstract class LibEpiccash { /// /// get transaction fees for Epic /// - static Future<({int fee, bool strategyUseAll, int total})> getTransactionFees({ + static Future<({int fee, bool strategyUseAll, int total})> + getTransactionFees({ required String wallet, required int amount, required int minimumConfirmations, required int available, }) async { try { - String fees = await compute(_transactionFeesWrapper, ( - wallet: wallet, - amount: amount, - minimumConfirmations: minimumConfirmations, + wallet: wallet, + amount: amount, + minimumConfirmations: minimumConfirmations, )); if (available == amount) { @@ -376,21 +394,23 @@ abstract class LibEpiccash { } } int largestSatoshiFee = - ((required - available) * Decimal.fromInt(100000000)) - .toBigInt() - .toInt(); + ((required - available) * Decimal.fromInt(100000000)) + .toBigInt() + .toInt(); var amountSending = amount - largestSatoshiFee; //Get fees for this new amount - ({String wallet, int amount, }) data = (wallet: wallet, amount: amountSending); + ({ + String wallet, + int amount, + }) data = (wallet: wallet, amount: amountSending); fees = await compute(_transactionFeesWrapper, ( - wallet: wallet, - amount: amountSending, - minimumConfirmations: minimumConfirmations, + wallet: wallet, + amount: amountSending, + minimumConfirmations: minimumConfirmations, )); } } - if (fees.toUpperCase().contains("ERROR")) { //Check if the error is an //Throw the returned error @@ -399,13 +419,13 @@ abstract class LibEpiccash { var decodedFees = json.decode(fees); var feeItem = decodedFees[0]; ({ - bool strategyUseAll, - int total, - int fee, + bool strategyUseAll, + int total, + int fee, }) feeRecord = ( - strategyUseAll: feeItem['selection_strategy_is_use_all'], - total: feeItem['total'], - fee: feeItem['fee'], + strategyUseAll: feeItem['selection_strategy_is_use_all'], + total: feeItem['total'], + fee: feeItem['fee'], ); return feeRecord; } catch (e) {