diff --git a/iguana/exchanges/LP_etomic.c b/iguana/exchanges/LP_etomic.c index 37688d9bb..dd94fef49 100644 --- a/iguana/exchanges/LP_etomic.c +++ b/iguana/exchanges/LP_etomic.c @@ -61,6 +61,31 @@ char *LP_etomicalice_send_fee(struct basilisk_swap *swap) } } +uint8_t LP_etomic_verify_alice_fee(struct basilisk_swap *swap) +{ + EthTxData data = getEthTxData(swap->otherfee.I.ethTxid); + if (data.exists == 0 || strcmp(data.from, swap->I.etomicdest) != 0) { + return 0; + } + if ( strcmp(swap->I.alicestr,"ETH") == 0 ) + { + uint64_t txValue = weiToSatoshi(data.valueHex); + if (strcmp(data.to, ETH_FEE_ACCEPTOR) != 0 || txValue != swap->otherfee.I.amount) { + return(0); + } + return(1); + } + else + { + if (strcmp(data.to, swap->I.alicetomic) != 0) { + return(0); + } + char weiAmount[70]; + satoshisToWei(weiAmount, swap->otherfee.I.amount); + return(verifyAliceErc20FeeData(ETH_FEE_ACCEPTOR, weiAmount, data.input)); + } +} + char *LP_etomicalice_send_payment(struct basilisk_swap *swap) { AliceSendsEthPaymentInput input; AliceSendsErc20PaymentInput input20; BasicTxData txData; diff --git a/iguana/exchanges/LP_transaction.c b/iguana/exchanges/LP_transaction.c index a374b2aef..97c246416 100644 --- a/iguana/exchanges/LP_transaction.c +++ b/iguana/exchanges/LP_transaction.c @@ -2133,7 +2133,7 @@ int32_t LP_verify_otherfee(struct basilisk_swap *swap,uint8_t *data,int32_t data else printf("locktime mismatch in otherfee, reject %u vs %u\n",swap->otherfee.I.locktime,swap->I.started+1); #ifndef NOTETOMIC if (swap->otherfee.I.ethTxid[0] != 0 && LP_etomic_is_empty_tx_id(swap->otherfee.I.ethTxid) == 0) { - if (LP_etomic_wait_for_confirmation(swap->otherfee.I.ethTxid) < 0) { + if (LP_etomic_wait_for_confirmation(swap->otherfee.I.ethTxid) < 0 || LP_etomic_verify_alice_fee(swap) == 0) { return(-1); } } diff --git a/iguana/exchanges/etomicswap/etomiclib.cpp b/iguana/exchanges/etomicswap/etomiclib.cpp index 425edd988..75d73db90 100644 --- a/iguana/exchanges/etomicswap/etomiclib.cpp +++ b/iguana/exchanges/etomicswap/etomiclib.cpp @@ -498,6 +498,12 @@ void satoshisToWei(char *dest, uint64_t input) strcat(dest, "0000000000"); } +uint64_t weiToSatoshi(char *wei) +{ + u256 satoshi = jsToU256(wei) / exp10<10>(); + return static_cast<uint64_t>(satoshi); +} + char *sendEth(char *to, char *amount, char *privKey) { TransactionSkeleton tx; @@ -516,6 +522,16 @@ char *sendEth(char *to, char *amount, char *privKey) return result; } +std::stringstream getErc20TransferData(char *to, char *amount) +{ + std::stringstream ss; + ss << "0xa9059cbb" + << "000000000000000000000000" + << toHex(jsToAddress(to)) + << toHex(toBigEndian(jsToU256(amount))); + return ss; +} + char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey) { TransactionSkeleton tx; @@ -528,11 +544,7 @@ char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey) tx.nonce = getNonce(from); free(from); - std::stringstream ss; - ss << "0xa9059cbb" - << "000000000000000000000000" - << toHex(jsToAddress(to)) - << toHex(toBigEndian(jsToU256(amount))); + std::stringstream ss = getErc20TransferData(to, amount); tx.data = jsToBytes(ss.str()); char *rawTx = signTx(tx, privKey); @@ -540,3 +552,12 @@ char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey) free(rawTx); return result; } + +uint8_t verifyAliceErc20FeeData(char *to, char *amount, char *data) +{ + std::stringstream ss = getErc20TransferData(to, amount); + if (strcmp(ss.str().c_str(), data) != 0) { + return 0; + } + return 1; +} diff --git a/iguana/exchanges/etomicswap/etomiclib.h b/iguana/exchanges/etomicswap/etomiclib.h index 495c63489..784e668f6 100644 --- a/iguana/exchanges/etomicswap/etomiclib.h +++ b/iguana/exchanges/etomicswap/etomiclib.h @@ -180,9 +180,12 @@ uint64_t getErc20Allowance(char *owner, char *spender, char *tokenAddress); void uint8arrayToHex(char *dest, uint8_t *input, int len); void satoshisToWei(char *dest, uint64_t input); +uint64_t weiToSatoshi(char *wei); char *sendEth(char *to, char *amount, char *privKey); char *sendErc20(char *tokenAddress, char *to, char *amount, char *privKey); + +uint8_t verifyAliceErc20FeeData(char *to, char *amount, char *data); // Your prototype or Definition #ifdef __cplusplus }