From b178c306208d5f467df85e391edd76257c9e5116 Mon Sep 17 00:00:00 2001 From: likho Date: Fri, 29 Sep 2023 16:15:15 +0200 Subject: [PATCH] WIP: move send tx to use abstract class --- .../coins/epiccash/epiccash_wallet.dart | 188 +++++++++--------- lib/wallets/example/libepiccash.dart | 33 ++- 2 files changed, 120 insertions(+), 101 deletions(-) diff --git a/lib/services/coins/epiccash/epiccash_wallet.dart b/lib/services/coins/epiccash/epiccash_wallet.dart index 4ad538e82..32250e496 100644 --- a/lib/services/coins/epiccash/epiccash_wallet.dart +++ b/lib/services/coins/epiccash/epiccash_wallet.dart @@ -233,12 +233,6 @@ Future deleteEpicWallet({ } } -//TODO - remove and use one from abstract class -Future _walletMnemonicWrapper(int throwaway) async { - final String mnemonic = walletMnemonic(); - return mnemonic; -} - class EpicCashWallet extends CoinServiceAPI with WalletCache, WalletDB, EpicCashHive { EpicCashWallet({ @@ -404,88 +398,101 @@ class EpicCashWallet extends CoinServiceAPI } } - await m.protect(() async { - if (receiverAddress.startsWith("http://") || - receiverAddress.startsWith("https://")) { - const int selectionStrategyIsAll = 0; - ReceivePort receivePort = await getIsolate({ - "function": "txHttpSend", - "wallet": wallet!, - "selectionStrategyIsAll": selectionStrategyIsAll, - "minimumConfirmations": MINIMUM_CONFIRMATIONS, - "message": txData['onChainNote'], - "amount": (txData['recipientAmt'] as Amount).raw.toInt(), - "address": txData['addresss'] as String, - }, name: walletName); - - message = await receivePort.first; - if (message is String) { - Logging.instance - .log("this is a string $message", level: LogLevel.Error); - stop(receivePort); - throw Exception(message); - } - stop(receivePort); - Logging.instance - .log('Closing txHttpSend!\n $message', level: LogLevel.Info); - } else { - ReceivePort receivePort = await getIsolate({ - "function": "createTransaction", - "wallet": wallet!, - "amount": (txData['recipientAmt'] as Amount).raw.toInt(), - "address": txData['addresss'] as String, - "secretKeyIndex": 0, - "epicboxConfig": epicboxConfig.toString(), - "minimumConfirmations": MINIMUM_CONFIRMATIONS, - "onChainNote": txData['onChainNote'], - }, name: walletName); - - message = await receivePort.first; - if (message is String) { - Logging.instance - .log("this is a string $message", level: LogLevel.Error); - stop(receivePort); - throw Exception("createTransaction isolate failed"); - } - stop(receivePort); - Logging.instance.log('Closing createTransaction!\n $message', - level: LogLevel.Info); - } - }); + // await m.protect(() async { + // if (receiverAddress.startsWith("http://") || + // receiverAddress.startsWith("https://")) { + // const int selectionStrategyIsAll = 0; + // ReceivePort receivePort = await getIsolate({ + // "function": "txHttpSend", + // "wallet": wallet!, + // "selectionStrategyIsAll": selectionStrategyIsAll, + // "minimumConfirmations": MINIMUM_CONFIRMATIONS, + // "message": txData['onChainNote'], + // "amount": (txData['recipientAmt'] as Amount).raw.toInt(), + // "address": txData['addresss'] as String, + // }, name: walletName); + // + // message = await receivePort.first; + // if (message is String) { + // Logging.instance + // .log("this is a string $message", level: LogLevel.Error); + // stop(receivePort); + // throw Exception(message); + // } + // stop(receivePort); + // Logging.instance + // .log('Closing txHttpSend!\n $message', level: LogLevel.Info); + // } else { + // ReceivePort receivePort = await getIsolate({ + // "function": "createTransaction", + // "wallet": wallet!, + // "amount": (txData['recipientAmt'] as Amount).raw.toInt(), + // "address": txData['addresss'] as String, + // "secretKeyIndex": 0, + // "epicboxConfig": epicboxConfig.toString(), + // "minimumConfirmations": MINIMUM_CONFIRMATIONS, + // "onChainNote": txData['onChainNote'], + // }, name: walletName); + // + // message = await receivePort.first; + // if (message is String) { + // Logging.instance + // .log("this is a string $message", level: LogLevel.Error); + // stop(receivePort); + // throw Exception("createTransaction isolate failed"); + // } + // stop(receivePort); + // Logging.instance.log('Closing createTransaction!\n $message', + // level: LogLevel.Info); + // } + // }); // return message; - final String sendTx = message['result'] as String; - if (sendTx.contains("Error")) { - throw BadEpicHttpAddressException(message: sendTx); - } + var transaction = await epiccash.LibEpiccash.createTransaction( + wallet: wallet!, + amount: (txData['recipientAmt'] as Amount).raw.toInt(), + address: txData['addresss'] as String, + secretKeyIndex: 0, + epicboxConfig: epicboxConfig.toString(), + minimumConfirmations: MINIMUM_CONFIRMATIONS, + note: txData['onChainNote'] as String); + // final String sendTx = message['result'] as String; + // if (sendTx.contains("Error")) { + // throw BadEpicHttpAddressException(message: sendTx); + // } + // Map txAddressInfo = {}; txAddressInfo['from'] = await currentReceivingAddress; txAddressInfo['to'] = txData['addresss'] as String; - await putSendToAddresses(sendTx, txAddressInfo); + await putSendToAddresses(transaction, txAddressInfo); - Logging.instance.log("CONFIRM_RESULT_IS $sendTx", level: LogLevel.Info); - - final decodeData = json.decode(sendTx); - - if (decodeData[0] == "transaction_failed") { - String errorMessage = decodeData[1] as String; - throw Exception("Transaction failed with error code $errorMessage"); - } else { - final txCreateResult = decodeData[0]; - // //TODO: second problem - final transaction = json.decode(txCreateResult as String); - - final tx = transaction[0]; - final txLogEntry = json.decode(tx as String); - final txLogEntryFirst = txLogEntry[0]; - final slateId = txLogEntryFirst['tx_slate_id'] as String; - return slateId!; - } + return transaction.slateId; + // + // Logging.instance.log("CONFIRM_RESULT_IS $sendTx", level: LogLevel.Info); + // + // final decodeData = json.decode(sendTx); + // + // if (decodeData[0] == "transaction_failed") { + // String errorMessage = decodeData[1] as String; + // throw Exception("Transaction failed with error code $errorMessage"); + // } else { + // final txCreateResult = decodeData[0]; + // // //TODO: second problem + // final transaction = json.decode(txCreateResult as String); + // + // final tx = transaction[0]; + // final txLogEntry = json.decode(tx as String); + // final txLogEntryFirst = txLogEntry[0]; + // final slateId = txLogEntryFirst['tx_slate_id'] as String; + // // return slateId!; + // + // } } catch (e, s) { Logging.instance.log("Error sending $e - $s", level: LogLevel.Error); rethrow; } + // return ""; } Future _getReceivingAddressForIndex( @@ -755,15 +762,10 @@ class EpicCashWallet extends CoinServiceAPI final List data = _mnemonicString.split(' '); return data; } else { - await m.protect(() async { - _mnemonicString = await compute( - _walletMnemonicWrapper, - 0, - ); - }); + _mnemonicString = epiccash.LibEpiccash.getMnemonic(); await _secureStore.write( key: '${_walletId}_mnemonic', value: _mnemonicString); - final List data = _mnemonicString!.split(' '); + final List data = _mnemonicString.split(' '); return data; } } @@ -1261,20 +1263,20 @@ class EpicCashWallet extends CoinServiceAPI } Future putSendToAddresses( - String slateMessage, Map txAddressInfo) async { + ({String slateId, String commitId}) slateData, Map txAddressInfo) async { try { var slatesToCommits = await getSlatesToCommits(); - final slate0 = jsonDecode(slateMessage); - final slate = jsonDecode(slate0[0] as String); - final part1 = jsonDecode(slate[0] as String); - final part2 = jsonDecode(slate[1] as String); - final slateId = part1[0]['tx_slate_id']; - final commitId = part2['tx']['body']['outputs'][0]['commit']; + // final slate0 = jsonDecode(slateMessage); + // final slate = jsonDecode(slate0[0] as String); + // final part1 = jsonDecode(slate[0] as String); + // final part2 = jsonDecode(slate[1] as String); + // final slateId = part1[0]['tx_slate_id']; + // final commitId = part2['tx']['body']['outputs'][0]['commit']; final from = txAddressInfo['from']; final to = txAddressInfo['to']; - slatesToCommits[slateId] = { - "commitId": commitId, + slatesToCommits[slateData.slateId] = { + "commitId": slateData.commitId, "from": from, "to": to, }; diff --git a/lib/wallets/example/libepiccash.dart b/lib/wallets/example/libepiccash.dart index 8e51e0049..3dfb96cd4 100644 --- a/lib/wallets/example/libepiccash.dart +++ b/lib/wallets/example/libepiccash.dart @@ -34,7 +34,7 @@ abstract class LibEpiccash { /// // TODO: ensure the above documentation comment is correct // TODO: ensure this will always return the mnemonic. If not, this function should throw an exception - // TODO: probably remove this as we don't use it in stack wallet. We store the mnemonic separately + //Function is used in _getMnemonicList() static String getMnemonic() { try { String mnemonic = lib_epiccash.walletMnemonic(); @@ -185,7 +185,7 @@ abstract class LibEpiccash { String wallet, int amount, String address, - int secretKey, + int secretKeyIndex, String epicboxConfig, int minimumConfirmations, String note, @@ -195,7 +195,7 @@ abstract class LibEpiccash { data.wallet, data.amount, data.address, - data.secretKey, + data.secretKeyIndex, data.epicboxConfig, data.minimumConfirmations, data.note); @@ -204,25 +204,42 @@ abstract class LibEpiccash { /// /// Create an Epic transaction /// - static Future createTransaction({ + static Future<({String slateId, String commitId})> createTransaction({ required String wallet, required int amount, required String address, - required int secretKey, + required int secretKeyIndex, required String epicboxConfig, required int minimumConfirmations, required String note, }) async { try { - return await compute(_createTransactionWrapper, ( + String result = await compute(_createTransactionWrapper, ( wallet: wallet, amount: amount, address: address, - secretKey: secretKey, + secretKeyIndex: secretKeyIndex, epicboxConfig: epicboxConfig, minimumConfirmations: minimumConfirmations, note: note, )); + + if (result.toUpperCase().contains("ERROR")) { + throw Exception("Error creating transaction ${result.toString()}"); + } + + //Decode sent tx and return Slate Id + final slate0 = jsonDecode(result); + final slate = jsonDecode(slate0[0] as String); + final part1 = jsonDecode(slate[0] as String); + final part2 = jsonDecode(slate[1] as String); + + ({String slateId, String commitId}) data = ( + slateId: part1[0]['tx_slate_id'], + commitId: part2['tx']['body']['outputs'][0]['commit'], + ); + + return data; } catch (e) { throw ("Error creating epic transaction : ${e.toString()}"); } @@ -468,7 +485,7 @@ abstract class LibEpiccash { name: name, )); } catch (e) { - throw ("Error recovering wallet : ${e.toString()}"); + throw (e.toString()); } }