WIP: move send tx to use abstract class

This commit is contained in:
likho 2023-09-29 16:15:15 +02:00
parent 807fc677d7
commit b178c30620
2 changed files with 120 additions and 101 deletions

View file

@ -233,12 +233,6 @@ Future<String> deleteEpicWallet({
} }
} }
//TODO - remove and use one from abstract class
Future<String> _walletMnemonicWrapper(int throwaway) async {
final String mnemonic = walletMnemonic();
return mnemonic;
}
class EpicCashWallet extends CoinServiceAPI class EpicCashWallet extends CoinServiceAPI
with WalletCache, WalletDB, EpicCashHive { with WalletCache, WalletDB, EpicCashHive {
EpicCashWallet({ EpicCashWallet({
@ -404,88 +398,101 @@ class EpicCashWallet extends CoinServiceAPI
} }
} }
await m.protect(() async { // await m.protect(() async {
if (receiverAddress.startsWith("http://") || // if (receiverAddress.startsWith("http://") ||
receiverAddress.startsWith("https://")) { // receiverAddress.startsWith("https://")) {
const int selectionStrategyIsAll = 0; // const int selectionStrategyIsAll = 0;
ReceivePort receivePort = await getIsolate({ // ReceivePort receivePort = await getIsolate({
"function": "txHttpSend", // "function": "txHttpSend",
"wallet": wallet!, // "wallet": wallet!,
"selectionStrategyIsAll": selectionStrategyIsAll, // "selectionStrategyIsAll": selectionStrategyIsAll,
"minimumConfirmations": MINIMUM_CONFIRMATIONS, // "minimumConfirmations": MINIMUM_CONFIRMATIONS,
"message": txData['onChainNote'], // "message": txData['onChainNote'],
"amount": (txData['recipientAmt'] as Amount).raw.toInt(), // "amount": (txData['recipientAmt'] as Amount).raw.toInt(),
"address": txData['addresss'] as String, // "address": txData['addresss'] as String,
}, name: walletName); // }, name: walletName);
//
message = await receivePort.first; // message = await receivePort.first;
if (message is String) { // if (message is String) {
Logging.instance // Logging.instance
.log("this is a string $message", level: LogLevel.Error); // .log("this is a string $message", level: LogLevel.Error);
stop(receivePort); // stop(receivePort);
throw Exception(message); // throw Exception(message);
} // }
stop(receivePort); // stop(receivePort);
Logging.instance // Logging.instance
.log('Closing txHttpSend!\n $message', level: LogLevel.Info); // .log('Closing txHttpSend!\n $message', level: LogLevel.Info);
} else { // } else {
ReceivePort receivePort = await getIsolate({ // ReceivePort receivePort = await getIsolate({
"function": "createTransaction", // "function": "createTransaction",
"wallet": wallet!, // "wallet": wallet!,
"amount": (txData['recipientAmt'] as Amount).raw.toInt(), // "amount": (txData['recipientAmt'] as Amount).raw.toInt(),
"address": txData['addresss'] as String, // "address": txData['addresss'] as String,
"secretKeyIndex": 0, // "secretKeyIndex": 0,
"epicboxConfig": epicboxConfig.toString(), // "epicboxConfig": epicboxConfig.toString(),
"minimumConfirmations": MINIMUM_CONFIRMATIONS, // "minimumConfirmations": MINIMUM_CONFIRMATIONS,
"onChainNote": txData['onChainNote'], // "onChainNote": txData['onChainNote'],
}, name: walletName); // }, name: walletName);
//
message = await receivePort.first; // message = await receivePort.first;
if (message is String) { // if (message is String) {
Logging.instance // Logging.instance
.log("this is a string $message", level: LogLevel.Error); // .log("this is a string $message", level: LogLevel.Error);
stop(receivePort); // stop(receivePort);
throw Exception("createTransaction isolate failed"); // throw Exception("createTransaction isolate failed");
} // }
stop(receivePort); // stop(receivePort);
Logging.instance.log('Closing createTransaction!\n $message', // Logging.instance.log('Closing createTransaction!\n $message',
level: LogLevel.Info); // level: LogLevel.Info);
} // }
}); // });
// return message; // return message;
final String sendTx = message['result'] as String; var transaction = await epiccash.LibEpiccash.createTransaction(
if (sendTx.contains("Error")) { wallet: wallet!,
throw BadEpicHttpAddressException(message: sendTx); 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<String, String> txAddressInfo = {}; Map<String, String> txAddressInfo = {};
txAddressInfo['from'] = await currentReceivingAddress; txAddressInfo['from'] = await currentReceivingAddress;
txAddressInfo['to'] = txData['addresss'] as String; txAddressInfo['to'] = txData['addresss'] as String;
await putSendToAddresses(sendTx, txAddressInfo); await putSendToAddresses(transaction, txAddressInfo);
Logging.instance.log("CONFIRM_RESULT_IS $sendTx", level: LogLevel.Info); return transaction.slateId;
//
final decodeData = json.decode(sendTx); // Logging.instance.log("CONFIRM_RESULT_IS $sendTx", level: LogLevel.Info);
//
if (decodeData[0] == "transaction_failed") { // final decodeData = json.decode(sendTx);
String errorMessage = decodeData[1] as String; //
throw Exception("Transaction failed with error code $errorMessage"); // if (decodeData[0] == "transaction_failed") {
} else { // String errorMessage = decodeData[1] as String;
final txCreateResult = decodeData[0]; // throw Exception("Transaction failed with error code $errorMessage");
// //TODO: second problem // } else {
final transaction = json.decode(txCreateResult as String); // final txCreateResult = decodeData[0];
// // //TODO: second problem
final tx = transaction[0]; // final transaction = json.decode(txCreateResult as String);
final txLogEntry = json.decode(tx as String); //
final txLogEntryFirst = txLogEntry[0]; // final tx = transaction[0];
final slateId = txLogEntryFirst['tx_slate_id'] as String; // final txLogEntry = json.decode(tx as String);
return slateId!; // final txLogEntryFirst = txLogEntry[0];
} // final slateId = txLogEntryFirst['tx_slate_id'] as String;
// // return slateId!;
//
// }
} catch (e, s) { } catch (e, s) {
Logging.instance.log("Error sending $e - $s", level: LogLevel.Error); Logging.instance.log("Error sending $e - $s", level: LogLevel.Error);
rethrow; rethrow;
} }
// return "";
} }
Future<isar_models.Address> _getReceivingAddressForIndex( Future<isar_models.Address> _getReceivingAddressForIndex(
@ -755,15 +762,10 @@ class EpicCashWallet extends CoinServiceAPI
final List<String> data = _mnemonicString.split(' '); final List<String> data = _mnemonicString.split(' ');
return data; return data;
} else { } else {
await m.protect(() async { _mnemonicString = epiccash.LibEpiccash.getMnemonic();
_mnemonicString = await compute(
_walletMnemonicWrapper,
0,
);
});
await _secureStore.write( await _secureStore.write(
key: '${_walletId}_mnemonic', value: _mnemonicString); key: '${_walletId}_mnemonic', value: _mnemonicString);
final List<String> data = _mnemonicString!.split(' '); final List<String> data = _mnemonicString.split(' ');
return data; return data;
} }
} }
@ -1261,20 +1263,20 @@ class EpicCashWallet extends CoinServiceAPI
} }
Future<bool> putSendToAddresses( Future<bool> putSendToAddresses(
String slateMessage, Map<String, String> txAddressInfo) async { ({String slateId, String commitId}) slateData, Map<String, String> txAddressInfo) async {
try { try {
var slatesToCommits = await getSlatesToCommits(); var slatesToCommits = await getSlatesToCommits();
final slate0 = jsonDecode(slateMessage); // final slate0 = jsonDecode(slateMessage);
final slate = jsonDecode(slate0[0] as String); // final slate = jsonDecode(slate0[0] as String);
final part1 = jsonDecode(slate[0] as String); // final part1 = jsonDecode(slate[0] as String);
final part2 = jsonDecode(slate[1] as String); // final part2 = jsonDecode(slate[1] as String);
final slateId = part1[0]['tx_slate_id']; // final slateId = part1[0]['tx_slate_id'];
final commitId = part2['tx']['body']['outputs'][0]['commit']; // final commitId = part2['tx']['body']['outputs'][0]['commit'];
final from = txAddressInfo['from']; final from = txAddressInfo['from'];
final to = txAddressInfo['to']; final to = txAddressInfo['to'];
slatesToCommits[slateId] = { slatesToCommits[slateData.slateId] = {
"commitId": commitId, "commitId": slateData.commitId,
"from": from, "from": from,
"to": to, "to": to,
}; };

View file

@ -34,7 +34,7 @@ abstract class LibEpiccash {
/// ///
// TODO: ensure the above documentation comment is correct // 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: 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() { static String getMnemonic() {
try { try {
String mnemonic = lib_epiccash.walletMnemonic(); String mnemonic = lib_epiccash.walletMnemonic();
@ -185,7 +185,7 @@ abstract class LibEpiccash {
String wallet, String wallet,
int amount, int amount,
String address, String address,
int secretKey, int secretKeyIndex,
String epicboxConfig, String epicboxConfig,
int minimumConfirmations, int minimumConfirmations,
String note, String note,
@ -195,7 +195,7 @@ abstract class LibEpiccash {
data.wallet, data.wallet,
data.amount, data.amount,
data.address, data.address,
data.secretKey, data.secretKeyIndex,
data.epicboxConfig, data.epicboxConfig,
data.minimumConfirmations, data.minimumConfirmations,
data.note); data.note);
@ -204,25 +204,42 @@ abstract class LibEpiccash {
/// ///
/// Create an Epic transaction /// Create an Epic transaction
/// ///
static Future<String> createTransaction({ static Future<({String slateId, String commitId})> createTransaction({
required String wallet, required String wallet,
required int amount, required int amount,
required String address, required String address,
required int secretKey, required int secretKeyIndex,
required String epicboxConfig, required String epicboxConfig,
required int minimumConfirmations, required int minimumConfirmations,
required String note, required String note,
}) async { }) async {
try { try {
return await compute(_createTransactionWrapper, ( String result = await compute(_createTransactionWrapper, (
wallet: wallet, wallet: wallet,
amount: amount, amount: amount,
address: address, address: address,
secretKey: secretKey, secretKeyIndex: secretKeyIndex,
epicboxConfig: epicboxConfig, epicboxConfig: epicboxConfig,
minimumConfirmations: minimumConfirmations, minimumConfirmations: minimumConfirmations,
note: note, 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) { } catch (e) {
throw ("Error creating epic transaction : ${e.toString()}"); throw ("Error creating epic transaction : ${e.toString()}");
} }
@ -468,7 +485,7 @@ abstract class LibEpiccash {
name: name, name: name,
)); ));
} catch (e) { } catch (e) {
throw ("Error recovering wallet : ${e.toString()}"); throw (e.toString());
} }
} }