From 84385107f4e0b1f9e6defcdcae9d5b57d1370cdc Mon Sep 17 00:00:00 2001 From: Artem Pikulin Date: Thu, 15 Feb 2018 14:04:06 +0700 Subject: [PATCH] Add methods for reclaim/refunds. Stop put persistent privkey to file. --- iguana/exchanges/LP_etomic.c | 107 +++++++++++++++++++---- iguana/exchanges/LP_remember.c | 55 +++++++++--- iguana/exchanges/LP_swap.c | 2 +- iguana/exchanges/etomicswap/bob.c | 2 +- iguana/exchanges/etomicswap/etomiccurl.c | 11 ++- 5 files changed, 142 insertions(+), 35 deletions(-) diff --git a/iguana/exchanges/LP_etomic.c b/iguana/exchanges/LP_etomic.c index dcad27e4e..128092a66 100644 --- a/iguana/exchanges/LP_etomic.c +++ b/iguana/exchanges/LP_etomic.c @@ -81,23 +81,35 @@ char *LP_etomicalice_send_payment(struct basilisk_swap *swap) } } -char *LP_etomicalice_reclaims_payment(struct basilisk_swap *swap) +char *LP_etomicalice_reclaims_payment(struct LP_swap_remember *swap) { AliceReclaimsAlicePaymentInput input; BasicTxData txData; memset(&txData,0,sizeof(txData)); memset(&input,0,sizeof(input)); - uint8arrayToHex(input.dealId, swap->alicepayment.I.actualtxid.bytes, 32); - satoshisToWei(input.amount, swap->I.alicesatoshis); - strcpy(input.tokenAddress, swap->I.alicetomic); - strcpy(input.bobAddress, swap->I.etomicdest); - uint8arrayToHex(input.aliceHash, swap->I.secretAm, 20); - uint8arrayToHex(input.bobSecret, swap->I.secretBn256, 32); - strcpy(txData.from, swap->I.etomicsrc); + uint8arrayToHex(input.dealId, swap->txids[BASILISK_ALICEPAYMENT].bytes, 32); + satoshisToWei(input.amount, swap->values[BASILISK_ALICEPAYMENT]); + + if (swap->alicetomic[0] != 0) { + strcpy(input.tokenAddress, swap->alicetomic); + } else { + strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000"); + } + strcpy(input.bobAddress, swap->etomicdest); + uint8arrayToHex(input.aliceHash, swap->secretAm, 20); + bits256 invertedSecret; + int32_t i; + for (i=0; i<32; i++) { + invertedSecret.bytes[i] = swap->privBn.bytes[31 - i]; + } + uint8arrayToHex(input.bobSecret, invertedSecret.bytes, 32); + + strcpy(txData.from, swap->etomicsrc); strcpy(txData.to, ETOMIC_ALICECONTRACT); strcpy(txData.amount, "0"); - uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32); + uint8arrayToHex(txData.secretKey, swap->persistentPrivKey.bytes, 32); + return aliceReclaimsAlicePayment(input, txData); } char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap) @@ -111,16 +123,18 @@ char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap) uint8arrayToHex(input.dealId, swap->txids[BASILISK_ALICEPAYMENT].bytes, 32); satoshisToWei(input.amount, swap->destamount); - if (swap->alicetomic[0] != 0) + if (swap->alicetomic[0] != 0) { strcpy(input.tokenAddress, swap->alicetomic); - else + } else { strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000"); + } strcpy(input.aliceAddress, swap->etomicdest); - bits256 hash; int32_t i; - for (i=0; i<32; i++) - hash.bytes[i] = swap->privAm.bytes[31-i]; - uint8arrayToHex(input.aliceSecret, hash.bytes, 32); + bits256 invertedSecret; int32_t i; + for (i=0; i<32; i++) { + invertedSecret.bytes[i] = swap->privAm.bytes[31 - i]; + } + uint8arrayToHex(input.aliceSecret, invertedSecret.bytes, 32); uint8arrayToHex(input.bobHash, swap->secretBn, 20); strcpy(txData.from, swap->etomicsrc); @@ -199,6 +213,33 @@ char *LP_etomicbob_sends_payment(struct basilisk_swap *swap) return bobSendsEthPayment(input, txData); } +char *LP_etomicbob_reclaims_payment(struct LP_swap_remember *swap) +{ + BobReclaimsBobPaymentInput input; + BasicTxData txData; + memset(&txData,0,sizeof(txData)); + memset(&input,0,sizeof(input)); + + EthTxReceipt receipt = getEthTxReceipt(swap->bobPaymentEthTx); + uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32); + strcpy(input.aliceAddress, swap->etomicdest); + sprintf(input.bobCanClaimAfter, "%" PRIu64, receipt.blockNumber + 480); + uint8arrayToHex(input.aliceHash, swap->secretAm, 20); + + if (swap->bobtomic[0] != 0) { + strcpy(input.tokenAddress, swap->bobtomic); + } else { + strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000"); + } + satoshisToWei(input.amount, swap->values[BASILISK_BOBPAYMENT]); + + strcpy(txData.from, swap->etomicsrc); + strcpy(txData.to, ETOMIC_BOBCONTRACT); + strcpy(txData.amount, "0"); + uint8arrayToHex(txData.secretKey, swap->persistentPrivKey.bytes, 32); + return bobReclaimsBobPayment(input, txData); +} + char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap) { AliceSpendsBobPaymentInput input; @@ -219,11 +260,12 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap) } strcpy(input.bobAddress, swap->etomicsrc); - bits256 hash; int32_t i; + bits256 invertedSecret; int32_t i; + for (i=0; i<32; i++) { - hash.bytes[i] = swap->privAm.bytes[31 - i]; + invertedSecret.bytes[i] = swap->privAm.bytes[31 - i]; } - uint8arrayToHex(input.aliceSecret, hash.bytes, 32); + uint8arrayToHex(input.aliceSecret, invertedSecret.bytes, 32); strcpy(txData.from, swap->etomicdest); strcpy(txData.to, ETOMIC_BOBCONTRACT); @@ -232,6 +274,35 @@ char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap) return aliceSpendsBobPayment(input, txData); } +char *LP_etomicalice_claims_bob_deposit(struct LP_swap_remember *swap) +{ + AliceClaimsBobDepositInput input; + BasicTxData txData; + + memset(&txData,0,sizeof(txData)); + memset(&input,0,sizeof(input)); + EthTxReceipt receipt = getEthTxReceipt(swap->bobDepositEthTx); + + uint8arrayToHex(input.depositId, swap->txids[BASILISK_BOBDEPOSIT].bytes, 32); + satoshisToWei(input.amount, swap->values[BASILISK_BOBDEPOSIT]); + sprintf(input.aliceCanClaimAfter, "%" PRIu64, receipt.blockNumber + 960); + + if (swap->bobtomic[0] != 0) { + strcpy(input.tokenAddress, swap->bobtomic); + } else { + strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000"); + } + + strcpy(input.bobAddress, swap->etomicsrc); + uint8arrayToHex(input.bobHash, swap->secretBn, 20); + + strcpy(txData.from, swap->etomicdest); + strcpy(txData.to, ETOMIC_BOBCONTRACT); + strcpy(txData.amount, "0"); + uint8arrayToHex(txData.secretKey, swap->persistentPrivKey.bytes, 32); + return aliceClaimsBobDeposit(input, txData); +} + char *sendEthTx(struct basilisk_swap *swap, struct basilisk_rawtx *rawtx) { if (rawtx == &swap->alicepayment && swap->I.alicetomic[0] != 0) { diff --git a/iguana/exchanges/LP_remember.c b/iguana/exchanges/LP_remember.c index c9d0eed50..4a2b9719e 100644 --- a/iguana/exchanges/LP_remember.c +++ b/iguana/exchanges/LP_remember.c @@ -75,9 +75,6 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx if (swap->bobpayment.I.ethTxid[0] != 0) { fprintf(fp,",\"bobPaymentEthTx\":\"%s\"", swap->bobpayment.I.ethTxid); } - char persistentPrivKeyStr[100]; - bits256_str(persistentPrivKeyStr,swap->persistent_privkey); - fprintf(fp,",\"persistentPrivKey\":\"%s\"",persistentPrivKeyStr); fprintf(fp,",\"alicecoin\":\"%s\"",swap->I.alicestr); if ( swap->I.alicetomic[0] != 0 ) @@ -872,8 +869,6 @@ int32_t LP_swap_load(struct LP_swap_remember *rswap,int32_t forceflag) strcpy(rswap->bobPaymentEthTx, jstr(txobj,"bobPaymentEthTx")); } - rswap->persistentPrivKey = jbits256(txobj, "persistentPrivKey"); - rswap->txids[i] = txid; if ( jstr(txobj,"Apayment") != 0 ) safecopy(rswap->alicepaymentaddr,jstr(txobj,"Apayment"),sizeof(rswap->alicepaymentaddr)); @@ -945,6 +940,21 @@ int32_t LP_swap_load(struct LP_swap_remember *rswap,int32_t forceflag) //printf("%s %s %.8f\n",txnames[i],bits256_str(str,txid),dstr(value)); } } + + struct iguana_info *ecoin,*coin; + if (rswap->iambob == 1) { + coin = LP_coinfind(rswap->bobcoin); + } else { + coin = LP_coinfind(rswap->alicecoin); + } + + bits256 privkey = LP_privkey(coin->symbol,coin->smartaddr,coin->taddr); + if (coin->etomic[0] != 0) { + if ((ecoin= LP_coinfind("ETOMIC")) != 0) { + privkey = LP_privkey(ecoin->symbol, ecoin->smartaddr, ecoin->taddr); + } + } + rswap->persistentPrivKey = privkey; free_json(txobj); } //else printf("no symbol\n"); free(fstr); @@ -1207,7 +1217,10 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rev.bytes[j] = rswap.myprivs[0].bytes[31 - j]; if ( (rswap.txbytes[BASILISK_ALICESPEND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"alicespend",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_ALICESPEND],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 ) { - LP_etomicalice_spends_bob_payment(&rswap); + if (rswap.bobtomic[0] != 0) { + char *aliceSpendEthTxId = LP_etomicalice_spends_bob_payment(&rswap); + free(aliceSpendEthTxId); + } } } LP_txbytes_update("alicespend",rswap.bobcoin,rswap.txbytes[BASILISK_ALICESPEND],&rswap.txids[BASILISK_ALICESPEND],&rswap.paymentspent,&rswap.sentflags[BASILISK_ALICESPEND]); @@ -1233,8 +1246,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( redeemlen > 0 ) { len = basilisk_swapuserdata(userdata,zero,1,rswap.myprivs[0],redeemscript,redeemlen); - if ( (rswap.txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"aliceclaim",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBDEPOSIT],0,0,rswap.pubkey33,0,claimtime,&rswap.values[BASILISK_ALICECLAIM],0,0,rswap.bobdepositaddr,1,bob->zcash)) != 0 ) - printf("claimtime.%u aliceclaim.(%s)\n",claimtime,rswap.txbytes[BASILISK_ALICECLAIM]); + if ( (rswap.txbytes[BASILISK_ALICECLAIM]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"aliceclaim",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBDEPOSIT],0,0,rswap.pubkey33,0,claimtime,&rswap.values[BASILISK_ALICECLAIM],0,0,rswap.bobdepositaddr,1,bob->zcash)) != 0 ) { + printf("claimtime.%u aliceclaim.(%s)\n", claimtime, rswap.txbytes[BASILISK_ALICECLAIM]); + if (rswap.bobtomic[0] != 0) { + char *aliceClaimsEthTxId = LP_etomicalice_claims_bob_deposit(&rswap); + free(aliceClaimsEthTxId); + } + } } LP_txbytes_update("aliceclaim",rswap.bobcoin,rswap.txbytes[BASILISK_ALICECLAIM],&rswap.txids[BASILISK_ALICECLAIM],&rswap.depositspent,&rswap.sentflags[BASILISK_ALICECLAIM]); } @@ -1254,8 +1272,13 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti rswap.privBn = basilisk_swap_privBn_extract(&rswap.txids[BASILISK_BOBREFUND],rswap.bobcoin,rswap.txids[BASILISK_BOBDEPOSIT],rswap.privBn); if ( bits256_nonz(rswap.txids[BASILISK_ALICEPAYMENT]) != 0 && bits256_nonz(rswap.privAm) != 0 && bits256_nonz(rswap.privBn) != 0 ) { - if ( (rswap.txbytes[BASILISK_ALICERECLAIM]= basilisk_swap_Aspend("alicereclaim",rswap.alicecoin,rswap.Atxfee,alice->wiftaddr,alice->taddr,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,rswap.privAm,rswap.privBn,rswap.txids[BASILISK_ALICEPAYMENT],0,rswap.pubkey33,rswap.expiration,&rswap.values[BASILISK_ALICERECLAIM],rswap.alicepaymentaddr,alice->zcash)) != 0 ) - printf("alicereclaim.(%s)\n",rswap.txbytes[BASILISK_ALICERECLAIM]); + if ( (rswap.txbytes[BASILISK_ALICERECLAIM]= basilisk_swap_Aspend("alicereclaim",rswap.alicecoin,rswap.Atxfee,alice->wiftaddr,alice->taddr,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,rswap.privAm,rswap.privBn,rswap.txids[BASILISK_ALICEPAYMENT],0,rswap.pubkey33,rswap.expiration,&rswap.values[BASILISK_ALICERECLAIM],rswap.alicepaymentaddr,alice->zcash)) != 0 ) { + printf("alicereclaim.(%s)\n", rswap.txbytes[BASILISK_ALICERECLAIM]); + if (rswap.alicetomic[0] != 0) { + char *aliceReclaimEthTx = LP_etomicalice_reclaims_payment(&rswap); + free(aliceReclaimEthTx); + } + } } LP_txbytes_update("alicereclaim",rswap.alicecoin,rswap.txbytes[BASILISK_ALICERECLAIM],&rswap.txids[BASILISK_ALICERECLAIM],&rswap.Apaymentspent,&rswap.sentflags[BASILISK_ALICERECLAIM]); } @@ -1288,7 +1311,8 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti if ( (rswap.txbytes[BASILISK_BOBSPEND]= basilisk_swap_Aspend("bobspend",rswap.alicecoin,rswap.Atxfee,alice->wiftaddr,alice->taddr,alice->pubtype,alice->p2shtype,alice->isPoS,alice->wiftype,ctx,rswap.privAm,rswap.privBn,rswap.txids[BASILISK_ALICEPAYMENT],0,rswap.pubkey33,rswap.expiration,&rswap.values[BASILISK_BOBSPEND],rswap.alicepaymentaddr,alice->zcash)) != 0 ) { //printf("bobspend.(%s)\n",rswap.txbytes[BASILISK_BOBSPEND]); - LP_etomicbob_spends_alice_payment(&rswap); + char *bobSpendEthTx = LP_etomicbob_spends_alice_payment(&rswap); + free(bobSpendEthTx); } } LP_txbytes_update("bobspend",rswap.alicecoin,rswap.txbytes[BASILISK_BOBSPEND],&rswap.txids[BASILISK_BOBSPEND],&rswap.Apaymentspent,&rswap.sentflags[BASILISK_BOBSPEND]); @@ -1313,6 +1337,10 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti len = basilisk_swapuserdata(userdata,zero,1,rswap.myprivs[1],redeemscript,redeemlen); if ( (rswap.txbytes[BASILISK_BOBRECLAIM]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"bobreclaim",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[1],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBPAYMENT],0,0,rswap.pubkey33,0,claimtime,&rswap.values[BASILISK_BOBRECLAIM],0,0,rswap.bobpaymentaddr,1,bob->zcash)) != 0 ) { + if (rswap.bobtomic[0] != 0) { + char *bobReclaimEthTx = LP_etomicbob_reclaims_payment(&rswap); + free(bobReclaimEthTx); + } //int32_t z; //for (z=0; z<20; z++) // printf("%02x",rswap.secretAm[z]); @@ -1347,7 +1375,10 @@ cJSON *basilisk_remember(int64_t *KMDtotals,int64_t *BTCtotals,uint32_t requesti len = basilisk_swapuserdata(userdata,rswap.privBn,0,rswap.myprivs[0],redeemscript,redeemlen); if ( (rswap.txbytes[BASILISK_BOBREFUND]= basilisk_swap_bobtxspend(&signedtxid,rswap.Btxfee,"bobrefund",rswap.bobcoin,bob->wiftaddr,bob->taddr,bob->pubtype,bob->p2shtype,bob->isPoS,bob->wiftype,ctx,rswap.myprivs[0],0,redeemscript,redeemlen,userdata,len,rswap.txids[BASILISK_BOBDEPOSIT],0,0,rswap.pubkey33,1,rswap.expiration,&rswap.values[BASILISK_BOBREFUND],0,0,rswap.bobdepositaddr,1,bob->zcash)) != 0 ) { - LP_etomicbob_refunds_deposit(&rswap); + if (rswap.bobtomic[0] != 0) { + char *bobRefundsEthTx = LP_etomicbob_refunds_deposit(&rswap); + free(bobRefundsEthTx); + } //printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,rswap.pubB1),rswap.txbytes[BASILISK_BOBREFUND]); } } diff --git a/iguana/exchanges/LP_swap.c b/iguana/exchanges/LP_swap.c index 527f37461..fdb9e6bc3 100644 --- a/iguana/exchanges/LP_swap.c +++ b/iguana/exchanges/LP_swap.c @@ -596,7 +596,7 @@ int32_t LP_rawtx_spendscript(struct basilisk_swap *swap,int32_t height,struct ba rawtx->I.redeemlen = recvbuf[offset++]; uint8arrayToHex(rawtx->I.ethTxid, &recvbuf[offset], 32); offset += 32; - printf("ETH txid received: %s", rawtx->I.ethTxid); + printf("ETH txid received: %s\n", rawtx->I.ethTxid); data = &recvbuf[offset]; if ( rawtx->I.redeemlen > 0 && rawtx->I.redeemlen < 0x100 ) { diff --git a/iguana/exchanges/etomicswap/bob.c b/iguana/exchanges/etomicswap/bob.c index a2a1e0361..2ee3f5cc2 100644 --- a/iguana/exchanges/etomicswap/bob.c +++ b/iguana/exchanges/etomicswap/bob.c @@ -199,7 +199,7 @@ int main(int argc, char** argv) case TX_RECEIPT: printf("getTxReceipt\n"); EthTxReceipt txReceipt; - txReceipt = getEthTxReceipt("0x9c3f31859dcba6d1032ed82074a0fe74a04b749d5e9feedc8a49bf78bf8f2b2c"); + txReceipt = getEthTxReceipt("0x82afa1b00f8a63e1a91430162e5cb2d4ebe915831ffd56e6e3227814913e23e6"); printf("%" PRIu64 "\n", txReceipt.blockNumber); printf("%s\n", txReceipt.blockHash); break; diff --git a/iguana/exchanges/etomicswap/etomiccurl.c b/iguana/exchanges/etomicswap/etomiccurl.c index 52d6bdf57..4212e7614 100644 --- a/iguana/exchanges/etomicswap/etomiccurl.c +++ b/iguana/exchanges/etomicswap/etomiccurl.c @@ -4,7 +4,7 @@ #include #include "../../../includes/cJSON.h" -static char* ethRpcUrl = "https://ropsten.infura.io/y07GHxUyTgeN2mdfOonu"; +static char *ethRpcUrl = "https://ropsten.infura.io/y07GHxUyTgeN2mdfOonu"; struct string { char *ptr; @@ -182,8 +182,13 @@ EthTxReceipt getEthTxReceipt(char *txId) cJSON_Delete(request); cJSON *json = cJSON_Parse(requestResult); cJSON *tmp = cJSON_GetObjectItem(json, "result"); - strcpy(result.blockHash, cJSON_GetObjectItem(tmp, "blockHash")->valuestring); - result.blockNumber = (uint64_t)strtol(cJSON_GetObjectItem(tmp, "blockNumber")->valuestring, NULL, 0); + if (is_cJSON_Null(tmp)) { + strcpy(result.blockHash, "0x0000000000000000000000000000000000000000000000000000000000000000"); + result.blockNumber = 0; + } else { + strcpy(result.blockHash, cJSON_GetObjectItem(tmp, "blockHash")->valuestring); + result.blockNumber = (uint64_t) strtol(cJSON_GetObjectItem(tmp, "blockNumber")->valuestring, NULL, 0); + } cJSON_Delete(json); free(requestResult); free(string);