Browse Source

Bob sends ETH deposit and payment and able to refund deposit.

etomic
Artem Pikulin 7 years ago
parent
commit
4a0785b1a3
  1. 125
      iguana/exchanges/LP_etomic.c
  2. 4
      iguana/exchanges/LP_include.h
  3. 25
      iguana/exchanges/LP_remember.c
  4. 40
      iguana/exchanges/LP_swap.c
  5. 15
      iguana/exchanges/etomicswap/bob.c
  6. 35
      iguana/exchanges/etomicswap/etomiccurl.c
  7. 8
      iguana/exchanges/etomicswap/etomiccurl.h

125
iguana/exchanges/LP_etomic.c

@ -22,6 +22,8 @@
// Created by artem on 24.01.18.
//
#include "etomicswap/etomiclib.h"
#include "etomicswap/etomiccurl.h"
#include <inttypes.h>
#define ETOMIC_ALICECONTRACT "0xe1D4236C5774D35Dc47dcc2E5E0CcFc463A3289c"
#define ETOMIC_BOBCONTRACT "0x9387Fd3a016bB0205e4e131Dde886B9d2BC000A2"
@ -59,22 +61,6 @@ char *LP_etomicalice_send_payment(struct basilisk_swap *swap)
satoshisToWei(txData.amount, swap->I.alicesatoshis);
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
char aliceSecret[100];
uint8arrayToHex(aliceSecret, swap->I.privAm.bytes, 32);
printf("alice secret: %s\n", aliceSecret);
init_hexbytes_noT(aliceSecret, swap->I.privAm.bytes, 32);
printf("alice secret: %s\n", aliceSecret);
init_hexbytes_noT(aliceSecret, swap->alicepayment.I.actualtxid.bytes, 32);
printf("alice txid: %s\n", aliceSecret);
bits256 hash; int32_t i;
for (i=0; i<32; i++)
hash.bytes[i] = swap->I.privAm.bytes[31-i];
uint8arrayToHex(aliceSecret, hash.bytes, 32);
printf("alice secret: %s\n", aliceSecret);
return(aliceSendsEthPayment(input,txData));
}
else
@ -137,13 +123,6 @@ char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap)
uint8arrayToHex(input.aliceSecret, hash.bytes, 32);
uint8arrayToHex(input.bobHash, swap->secretBn, 20);
char aliceSecret[100];
uint8arrayToHex(aliceSecret, swap->privAm.bytes, 32);
printf("alice secret: %s\n", aliceSecret);
uint8arrayToHex(aliceSecret, hash.bytes, 32);
printf("alice secret: %s\n", aliceSecret);
strcpy(txData.from, swap->etomicsrc);
strcpy(txData.to, ETOMIC_ALICECONTRACT);
strcpy(txData.amount, "0");
@ -151,6 +130,106 @@ char *LP_etomicbob_spends_alice_payment(struct LP_swap_remember *swap)
return bobSpendsAlicePayment(input, txData);
}
char *LP_etomicbob_sends_deposit(struct basilisk_swap *swap)
{
BobSendsEthDepositInput input;
BasicTxData txData;
memset(&txData,0,sizeof(txData));
memset(&input,0,sizeof(input));
uint8arrayToHex(input.depositId, swap->bobdeposit.I.actualtxid.bytes, 32);
strcpy(input.aliceAddress, swap->I.etomicdest);
uint8arrayToHex(input.bobHash, swap->I.secretBn, 20);
strcpy(txData.from, swap->I.etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
satoshisToWei(txData.amount, swap->bobdeposit.I.amount);
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
return bobSendsEthDeposit(input, txData);
}
char *LP_etomicbob_refunds_deposit(struct LP_swap_remember *swap)
{
BobRefundsDepositInput 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);
strcpy(input.aliceAddress, swap->etomicdest);
sprintf(input.aliceCanClaimAfter, "%" PRIu64, receipt.blockNumber + 960);
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);
if (swap->bobtomic[0] != 0) {
strcpy(input.tokenAddress, swap->bobtomic);
} else {
strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000");
}
satoshisToWei(input.amount, swap->values[BASILISK_BOBDEPOSIT]);
strcpy(txData.from, swap->etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, swap->persistentPrivKey.bytes, 32);
return bobRefundsDeposit(input, txData);
}
char *LP_etomicbob_sends_payment(struct basilisk_swap *swap)
{
BobSendsEthPaymentInput input;
BasicTxData txData;
memset(&txData,0,sizeof(txData));
memset(&input,0,sizeof(input));
uint8arrayToHex(input.paymentId, swap->bobpayment.I.actualtxid.bytes, 32);
strcpy(input.aliceAddress, swap->I.etomicdest);
uint8arrayToHex(input.aliceHash, swap->I.secretAm, 20);
strcpy(txData.from, swap->I.etomicsrc);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
satoshisToWei(txData.amount, swap->bobpayment.I.amount);
uint8arrayToHex(txData.secretKey, swap->persistent_privkey.bytes, 32);
return bobSendsEthPayment(input, txData);
}
char *LP_etomicalice_spends_bob_payment(struct LP_swap_remember *swap)
{
AliceSpendsBobPaymentInput input;
BasicTxData txData;
memset(&txData,0,sizeof(txData));
memset(&input,0,sizeof(input));
uint8arrayToHex(input.paymentId, swap->txids[BASILISK_BOBPAYMENT].bytes, 32);
satoshisToWei(input.amount, swap->values[BASILISK_BOBPAYMENT]);
if (swap->bobtomic[0] != 0) {
strcpy(input.tokenAddress, swap->bobtomic);
} else {
strcpy(input.tokenAddress, "0x0000000000000000000000000000000000000000");
}
strcpy(input.bobAddress, swap->etomicsrc);
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);
strcpy(txData.from, swap->etomicdest);
strcpy(txData.to, ETOMIC_BOBCONTRACT);
strcpy(txData.amount, "0");
uint8arrayToHex(txData.secretKey, swap->persistentPrivKey.bytes, 32);
return aliceSpendsBobPayment(input, txData);
}
int32_t LP_etomic_priv2addr(char *coinaddr,bits256 privkey)
{
char str[65],*addrstr;

4
iguana/exchanges/LP_include.h

@ -202,7 +202,7 @@ struct basilisk_swap;
struct basilisk_rawtxinfo
{
char destaddr[64];
char destaddr[64],ethTxid[75];
bits256 txid,signedtxid,actualtxid;
int64_t amount,change,inputsum;
int32_t redeemlen,datalen,completed,vintype,vouttype,numconfirms,spendlen,secretstart,suppress_pubkeys;
@ -276,7 +276,7 @@ struct LP_swap_remember
uint32_t finishtime,tradeid,requestid,quoteid,plocktime,dlocktime,expiration,state,otherstate;
int32_t iambob,finishedflag,origfinishedflag,Predeemlen,Dredeemlen,sentflags[sizeof(txnames)/sizeof(*txnames)];
uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],Predeemscript[1024],Dredeemscript[1024],pubkey33[33],other33[33];
char Agui[65],Bgui[65],gui[65],src[65],dest[65],bobtomic[128],alicetomic[128],etomicsrc[65],etomicdest[65],destaddr[64],Adestaddr[64],Sdestaddr[64],alicepaymentaddr[64],bobpaymentaddr[64],bobdepositaddr[64],alicecoin[65],bobcoin[65],*txbytes[sizeof(txnames)/sizeof(*txnames)];
char Agui[65],Bgui[65],gui[65],src[65],dest[65],bobtomic[128],alicetomic[128],etomicsrc[65],etomicdest[65],destaddr[64],Adestaddr[64],Sdestaddr[64],alicepaymentaddr[64],bobpaymentaddr[64],bobdepositaddr[64],alicecoin[65],bobcoin[65],*txbytes[sizeof(txnames)/sizeof(*txnames)],bobDepositEthTx[75],bobPaymentEthTx[75];
};
struct LP_outpoint

25
iguana/exchanges/LP_remember.c

@ -17,7 +17,6 @@
// LP_remember.c
// marketmaker
//
void basilisk_dontforget_userdata(char *userdataname,FILE *fp,uint8_t *script,int32_t scriptlen)
{
int32_t i; char scriptstr[513];
@ -70,7 +69,12 @@ void basilisk_dontforget(struct basilisk_swap *swap,struct basilisk_rawtx *rawtx
fprintf(fp,",\"bobtomic\":\"%s\"",swap->I.bobtomic);
if ( swap->I.etomicsrc[0] != 0 )
fprintf(fp,",\"etomicsrc\":\"%s\"",swap->I.etomicsrc);
if (swap->bobdeposit.I.ethTxid[0] != 0) {
fprintf(fp,",\"bobDepositEthTx\":\"%s\"", swap->bobdeposit.I.ethTxid);
}
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);
@ -852,17 +856,23 @@ int32_t LP_swap_load(struct LP_swap_remember *rswap,int32_t forceflag)
continue;
}
if (jstr(txobj,"etomicsrc") != 0)
{
if (jstr(txobj,"etomicsrc") != 0) {
strcpy(rswap->etomicsrc,jstr(txobj,"etomicsrc"));
}
if (jstr(txobj,"etomicdest") != 0)
{
if (jstr(txobj,"etomicdest") != 0) {
strcpy(rswap->etomicdest,jstr(txobj,"etomicdest"));
}
rswap->persistentPrivKey = jbits256(txobj,"persistentPrivKey");
if (jstr(txobj,"bobDepositEthTx") != 0) {
strcpy(rswap->bobDepositEthTx, jstr(txobj,"bobDepositEthTx"));
}
if (jstr(txobj,"bobPaymentEthTx") != 0) {
strcpy(rswap->bobPaymentEthTx, jstr(txobj,"bobPaymentEthTx"));
}
rswap->persistentPrivKey = jbits256(txobj, "persistentPrivKey");
rswap->txids[i] = txid;
if ( jstr(txobj,"Apayment") != 0 )
@ -1337,6 +1347,7 @@ 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);
//printf("pubB1.(%s) bobrefund.(%s)\n",bits256_str(str,rswap.pubB1),rswap.txbytes[BASILISK_BOBREFUND]);
}
}

40
iguana/exchanges/LP_swap.c

@ -792,6 +792,7 @@ void LP_bobloop(void *_swap)
printf("error bobscripts deposit\n");
else
{
uint8_t error = 0;
swap->bobrefund.utxovout = 0;
swap->bobrefund.utxotxid = swap->bobdeposit.I.signedtxid;
basilisk_bobdeposit_refund(swap,swap->I.putduration);
@ -799,12 +800,36 @@ void LP_bobloop(void *_swap)
LP_swapsfp_update(&swap->I.req);
LP_swap_critical = (uint32_t)time(NULL);
if ( LP_waitfor(swap->N.pair,swap,LP_SWAPSTEP_TIMEOUT*10,LP_verify_otherfee) < 0 )
{
error = 1;
printf("error waiting for alicefee\n");
else if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x200,data,maxlen,&swap->bobdeposit,0x100,0) == 0 )
printf("error sending bobdeposit\n");
else if ( LP_waitfor(swap->N.pair,swap,1800,LP_verify_alicepayment) < 0 )
}
if ( error == 0 )
{
if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x200,data,maxlen,&swap->bobdeposit,0x100,0) != 0 )
{
if (swap->I.bobtomic[0] != 0)
{
char *depositTx = LP_etomicbob_sends_deposit(swap);
strcpy(swap->bobdeposit.I.ethTxid, depositTx);
free(depositTx);
}
}
else
{
error = 1;
printf("error sending bobdeposit\n");
}
}
if ( error == 0 && LP_waitfor(swap->N.pair,swap,1800,LP_verify_alicepayment) < 0 )
{
error = 1;
printf("error waiting for alicepayment\n");
else
}
if (error == 0)
{
LP_swap_critical = (uint32_t)time(NULL);
if ( basilisk_bobscripts_set(swap,0,1) < 0 )
@ -819,8 +844,13 @@ void LP_bobloop(void *_swap)
sleep(10);
}
LP_swap_critical = (uint32_t)time(NULL);
if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 )
if ( LP_swapdata_rawtxsend(swap->N.pair,swap,0x8000,data,maxlen,&swap->bobpayment,0x4000,0) == 0 ) {
printf("error sending bobpayment\n");
} else if (swap->I.bobtomic[0] != 0) {
char *paymentTx = LP_etomicbob_sends_payment(swap);
strcpy(swap->bobpayment.I.ethTxid, paymentTx);
free(paymentTx);
}
//if ( LP_waitfor(swap->N.pair,swap,10,LP_verify_alicespend) < 0 )
// printf("error waiting for alicespend\n");
//swap->sentflag = 1;

15
iguana/exchanges/etomicswap/bob.c

@ -7,6 +7,7 @@
#include <inttypes.h>
#include <string.h>
#include "etomiclib.h"
#include "etomiccurl.h"
char* bobContractAddress = "0x9387Fd3a016bB0205e4e131Dde886B9d2BC000A2";
char* aliceAddress = "0x485d2cc2d13a9e12E4b53D606DB1c8adc884fB8a";
@ -27,6 +28,7 @@ int main(int argc, char** argv)
BOB_APPROVES_ERC20,
BOB_ETH_BALANCE,
BOB_ERC20_BALANCE,
TX_RECEIPT
};
if (argc < 2) {
return 1;
@ -194,18 +196,19 @@ int main(int argc, char** argv)
case BOB_ERC20_BALANCE:
printf("%" PRIu64 "\n", getErc20Balance(bobAddress, tokenAddress));
break;
case TX_RECEIPT:
printf("getTxReceipt\n");
EthTxReceipt txReceipt;
txReceipt = getEthTxReceipt("0x9c3f31859dcba6d1032ed82074a0fe74a04b749d5e9feedc8a49bf78bf8f2b2c");
printf("%" PRIu64 "\n", txReceipt.blockNumber);
printf("%s\n", txReceipt.blockHash);
break;
default:
return 1;
}
char *pubkey = getPubKeyFromPriv(getenv("BOB_PK"));
printf("pubkey: %s\n", pubkey);
free(pubkey);
uint8_t test[2];
test[0] = 100;
test[1] = 100;
char buf[30];
uint8arrayToHex(buf, test, 2);
printf("hex: %s\n", buf);
uint64_t satoshis = 100000000;
char weiBuffer[100];

35
iguana/exchanges/etomicswap/etomiccurl.c

@ -88,6 +88,7 @@ char* sendRawTx(char* rawTx)
strcpy(txId, tmp);
cJSON_Delete(json);
free(requestResult);
free(string);
return txId;
}
@ -109,6 +110,7 @@ int getNonce(char* address)
int nonce = (int)strtol(cJSON_GetObjectItem(json, "result")->valuestring, NULL, 0);
cJSON_Delete(json);
free(requestResult);
free(string);
return nonce;
}
@ -132,6 +134,7 @@ char* getEthBalanceRequest(char* address)
strcpy(balance, tmp);
cJSON_Delete(json);
free(requestResult);
free(string);
return balance;
}
@ -154,9 +157,35 @@ char* ethCall(char* to, const char* data)
cJSON_Delete(request);
cJSON *json = cJSON_Parse(requestResult);
char* tmp = cJSON_GetObjectItem(json, "result")->valuestring;
char* balance = (char*)malloc(strlen(tmp) + 1);
strcpy(balance, tmp);
char* result = (char*)malloc(strlen(tmp) + 1);
strcpy(result, tmp);
cJSON_Delete(json);
free(requestResult);
return balance;
free(string);
return result;
}
EthTxReceipt getEthTxReceipt(char *txId)
{
EthTxReceipt result;
char* string;
cJSON *request = cJSON_CreateObject();
cJSON *params = cJSON_CreateArray();
cJSON_AddItemToObject(request, "jsonrpc", cJSON_CreateString("2.0"));
cJSON_AddItemToObject(request, "method", cJSON_CreateString("eth_getTransactionReceipt"));
cJSON_AddItemToArray(params, cJSON_CreateString(txId));
cJSON_AddItemToObject(request, "params", params);
cJSON_AddItemToObject(request, "id", cJSON_CreateNumber(2));
string = cJSON_PrintUnformatted(request);
char *requestResult = sendRequest(string);
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);
cJSON_Delete(json);
free(requestResult);
free(string);
return result;
}

8
iguana/exchanges/etomicswap/etomiccurl.h

@ -1,11 +1,19 @@
#include <stdint.h>
#ifdef __cplusplus
extern "C"{
#endif
typedef struct
{
uint64_t blockNumber;
char blockHash[75];
} EthTxReceipt;
char* sendRawTx(char* rawTx);
char* ethCall(char* to, const char* data);
int getNonce(char* address);
char* getEthBalanceRequest(char* address);
EthTxReceipt getEthTxReceipt(char *txId);
#ifdef __cplusplus
}

Loading…
Cancel
Save