diff --git a/basilisk/basilisk_swap.c b/basilisk/basilisk_swap.c index c63bf9761..6fc893805 100755 --- a/basilisk/basilisk_swap.c +++ b/basilisk/basilisk_swap.c @@ -2867,7 +2867,7 @@ bits256 basilisk_swap_spendtxid(struct supernet_info *myinfo,char *symbol,char * if ( bits256_nonz(spendtxid) != 0 ) { basilisk_swap_getcoinaddr(myinfo,symbol,destaddr,spendtxid,0); - char str[65]; printf("found spendtxid.(%s) -> %s\n",bits256_str(str,spendtxid),destaddr); + //char str[65]; printf("found spendtxid.(%s) -> %s\n",bits256_str(str,spendtxid),destaddr); break; } } @@ -3016,6 +3016,9 @@ char *basilisk_swap_Aspend(char *name,struct supernet_info *myinfo,char *symbol, { pubAm = bitcoin_pubkey33(myinfo->ctx,tmp33,privAm); pubBn = bitcoin_pubkey33(myinfo->ctx,tmp33,privBn); + char str[65]; + printf("pubAm.(%s)\n",bits256_str(str,pubAm)); + printf("pubBn.(%s)\n",bits256_str(str,pubBn)); spendlen = basilisk_alicescript(redeemscript,&redeemlen,spendscript,0,msigaddr,coin->chain->p2shtype,pubAm,pubBn); //char str[65]; printf("%s utxo.(%s) redeemlen.%d spendlen.%d\n",msigaddr,bits256_str(str,utxotxid),redeemlen,spendlen); /*rev = privAm; @@ -3056,36 +3059,50 @@ bits256 basilisk_swap_privBn_extract(struct supernet_info *myinfo,bits256 *bobre return(privBn); } -bits256 basilisk_swap_spendupdate(struct supernet_info *myinfo,char *symbol,int32_t *sentflagp,int32_t *alicespentp,int32_t *bobspentp,bits256 txid,int32_t vout,char *aliceaddr,char *bobaddr) +bits256 basilisk_swap_spendupdate(struct supernet_info *myinfo,char *symbol,int32_t *sentflags,bits256 *txids,int32_t utxoind,int32_t alicespent,int32_t bobspent,int32_t vout,char *aliceaddr,char *bobaddr) { - bits256 spendtxid; char destaddr[64],str[65]; + bits256 spendtxid,txid; char destaddr[64],str[65]; + txid = txids[utxoind]; memset(&spendtxid,0,sizeof(spendtxid)); + if ( aliceaddr != 0 ) + printf("aliceaddr.(%s)\n",aliceaddr); + if ( bobaddr != 0 ) + printf("bobaddr.(%s)\n",bobaddr); if ( bits256_nonz(txid) != 0 ) { spendtxid = basilisk_swap_spendtxid(myinfo,symbol,destaddr,txid,vout); if ( bits256_nonz(spendtxid) != 0 ) { - *sentflagp = 1; + sentflags[utxoind] = 1; if ( aliceaddr != 0 && strcmp(destaddr,aliceaddr) == 0 ) { - printf("ALICE spent.(%s) -> %s\n",bits256_str(str,txid),destaddr); - *alicespentp = 1; + //printf("ALICE spent.(%s) -> %s\n",bits256_str(str,txid),destaddr); + sentflags[alicespent] = 1; + txids[alicespent] = spendtxid; } else if ( bobaddr != 0 && strcmp(destaddr,bobaddr) == 0 ) { - printf("BOB spent.(%s) -> %s\n",bits256_str(str,txid),destaddr); - *bobspentp = 1; + //printf("BOB spent.(%s) -> %s\n",bits256_str(str,txid),destaddr); + sentflags[bobspent] = 1; + txids[bobspent] = spendtxid; } else { - if ( aliceaddr != 0 && bobspentp == 0 ) - *bobspentp = 1; - else if ( bobaddr != 0 && alicespentp == 0 ) - *alicespentp = 1; - printf("OTHER dest spent.(%s) -> %s\n",bits256_str(str,txid),destaddr); + //char str[65]; + //printf("OTHER dest spent.(%s) -> %s\n",bits256_str(str,txid),destaddr); + if ( aliceaddr != 0 ) + { + sentflags[bobspent] = 1; + txids[bobspent] = spendtxid; + } + else if ( bobaddr != 0 ) + { + sentflags[alicespent] = 1; + txids[alicespent] = spendtxid; + } } } - } + } else printf("utxoind.%d null txid\n",utxoind); return(spendtxid); } @@ -3107,7 +3124,7 @@ char *txnames[] = { "alicespend", "bobspend", "bobpayment", "alicepayment", "bob cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64_t *BTCtotals,uint32_t requestid,uint32_t quoteid) { - FILE *fp; struct iguana_info *coin; int32_t sentflags[sizeof(txnames)/sizeof(*txnames)],i,j,len,needflag,secretstart,redeemlen,addflag,iambob = -1; uint64_t srcamount,destamount=0,value; uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],pubkey33[33],redeemscript[512],userdata[512]; uint32_t plocktime,dlocktime,expiration=0,r,q,state,otherstate; char *secretstr,*srcstr,*deststr,str[65],src[64],dest[64],fname[512],*fstr,*dest33,*symbol,str2[65],*Adest,*Bdest,destaddr[64],alicecoin[64],bobcoin[64],*txbytes[sizeof(txnames)/sizeof(*txnames)]; long fsize; cJSON *txobj,*item,*sentobj,*array; bits256 checktxid,txid,pubA0,pubB0,pubB1,privAm,privBn,paymentspent,Apaymentspent,depositspent,zero,privkey,rev,myprivs[2],txids[sizeof(txnames)/sizeof(*txnames)]; + FILE *fp; struct iguana_info *coin; int32_t sentflags[sizeof(txnames)/sizeof(*txnames)],i,j,len,needflag,secretstart,redeemlen,addflag,finishedflag = 0,iambob = -1; uint64_t srcamount,destamount=0,value; uint8_t secretAm[20],secretAm256[32],secretBn[20],secretBn256[32],pubkey33[33],redeemscript[512],userdata[512]; uint32_t plocktime,dlocktime,expiration=0,r,q,state,otherstate; char *secretstr,*srcstr,*deststr,str[65],src[64],dest[64],fname[512],*fstr,*dest33,*symbol,str2[65],*Adest,*Bdest,*AAdest,*ABdest,destaddr[64],Adestaddr[64],alicecoin[64],bobcoin[64],*txbytes[sizeof(txnames)/sizeof(*txnames)]; long fsize; cJSON *txobj,*item,*sentobj,*array; bits256 checktxid,txid,pubA0,pubB0,pubB1,privAm,privBn,paymentspent,Apaymentspent,depositspent,zero,privkey,rev,myprivs[2],txids[sizeof(txnames)/sizeof(*txnames)]; sprintf(fname,"%s/SWAPS/%u-%u.finished",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); if ( (fp= fopen(fname,"rb")) != 0 ) { @@ -3147,9 +3164,9 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 if ( (dest33= jstr(item,"dest33")) != 0 && strlen(dest33) == 66 ) { decode_hex(pubkey33,33,dest33); - for (i=0; i<33; i++) - printf("%02x",pubkey33[i]); - printf(" <- %s dest33\n",dest33); + //for (i=0; i<33; i++) + // printf("%02x",pubkey33[i]); + //printf(" <- %s dest33\n",dest33); } plocktime = juint(item,"plocktime"); dlocktime = juint(item,"dlocktime"); @@ -3223,7 +3240,9 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 else if ( i == BASILISK_BOBSPEND || i == BASILISK_ALICEPAYMENT || i == BASILISK_ALICERECLAIM ) safecopy(alicecoin,symbol,sizeof(alicecoin)); if ( (sentobj= basilisk_swapgettx(myinfo,symbol,txid)) == 0 ) + { printf("%s %s ready to broadcast\n",symbol,bits256_str(str2,txid)); + } else { checktxid = jbits256(sentobj,"txid"); @@ -3252,11 +3271,16 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 printf("iambob.%d src.%s dest.%s bob.%s alice.%s pubA0.(%s)\n",iambob,src,dest,bobcoin,alicecoin,bits256_str(str,pubA0)); //strcpy(bobcoin,"KMD"); //strcpy(alicecoin,"BTC"); - destaddr[0] = 0; - Adest = Bdest = 0; + Adestaddr[0] = destaddr[0] = 0; + Adest = Bdest = AAdest = ABdest = 0; if ( iambob == 0 ) { if ( (coin= iguana_coinfind(alicecoin)) != 0 ) + { + bitcoin_address(Adestaddr,coin->chain->pubtype,pubkey33,33); + AAdest = Adestaddr; + } + if ( (coin= iguana_coinfind(bobcoin)) != 0 ) { bitcoin_address(destaddr,coin->chain->pubtype,pubkey33,33); Adest = destaddr; @@ -3269,42 +3293,27 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 bitcoin_address(destaddr,coin->chain->pubtype,pubkey33,33); Bdest = destaddr; } + if ( (coin= iguana_coinfind(alicecoin)) != 0 ) + { + bitcoin_address(Adestaddr,coin->chain->pubtype,pubkey33,33); + ABdest = Adestaddr; + } } - /*printf("privAm.(%s)\n",bits256_str(str,privAm)); - printf("pubB0.(%s)\n",bits256_str(str,pubB0)); - printf("myprivs0.(%s)\n",bits256_str(str,myprivs[0])); - printf("myprivs1.(%s)\n",bits256_str(str,myprivs[1])); - printf("privBn.(%s)\n",bits256_str(str,privBn));*/ - printf("Apaymentspent.(%s)\n",bits256_str(str,Apaymentspent)); - printf("paymentspent.(%s)\n",bits256_str(str,paymentspent)); - printf("depositspent.(%s)\n",bits256_str(str,depositspent)); - if ( bobcoin[0] != 0 && alicecoin[0] != 0 && (Adest != 0 || Bdest != 0) ) + //printf("privAm.(%s) %p/%p\n",bits256_str(str,privAm),Adest,AAdest); + //printf("privBn.(%s) %p/%p\n",bits256_str(str,privBn),Bdest,ABdest); + if ( bobcoin[0] != 0 && alicecoin[0] != 0 ) { - paymentspent = basilisk_swap_spendupdate(myinfo,bobcoin,&sentflags[BASILISK_BOBPAYMENT],&sentflags[BASILISK_ALICESPEND],&sentflags[BASILISK_BOBRECLAIM],txids[BASILISK_BOBPAYMENT],0,Adest,Bdest); - Apaymentspent = basilisk_swap_spendupdate(myinfo,alicecoin,&sentflags[BASILISK_ALICEPAYMENT],&sentflags[BASILISK_ALICERECLAIM],&sentflags[BASILISK_BOBSPEND],txids[BASILISK_ALICEPAYMENT],0,Adest,Bdest); - depositspent = basilisk_swap_spendupdate(myinfo,bobcoin,&sentflags[BASILISK_BOBDEPOSIT],&sentflags[BASILISK_ALICECLAIM],&sentflags[BASILISK_BOBREFUND],txids[BASILISK_BOBDEPOSIT],0,Adest,Bdest); + paymentspent = basilisk_swap_spendupdate(myinfo,bobcoin,sentflags,txids,BASILISK_BOBPAYMENT,BASILISK_ALICESPEND,BASILISK_BOBRECLAIM,0,Adest,Bdest); + Apaymentspent = basilisk_swap_spendupdate(myinfo,alicecoin,sentflags,txids,BASILISK_ALICEPAYMENT,BASILISK_ALICERECLAIM,BASILISK_BOBSPEND,0,AAdest,ABdest); + depositspent = basilisk_swap_spendupdate(myinfo,bobcoin,sentflags,txids,BASILISK_BOBDEPOSIT,BASILISK_ALICECLAIM,BASILISK_BOBREFUND,0,Adest,Bdest); + printf("Apaymentspent.(%s) alice.%d bob.%d\n",bits256_str(str,Apaymentspent),sentflags[BASILISK_ALICERECLAIM],sentflags[BASILISK_BOBSPEND]); + printf("paymentspent.(%s) alice.%d bob.%d\n",bits256_str(str,paymentspent),sentflags[BASILISK_ALICESPEND],sentflags[BASILISK_BOBRECLAIM]); + printf("depositspent.(%s) alice.%d bob.%d\n",bits256_str(str,depositspent),sentflags[BASILISK_ALICECLAIM],sentflags[BASILISK_BOBREFUND]); if ( bits256_nonz(paymentspent) != 0 && bits256_nonz(Apaymentspent) != 0 && bits256_nonz(depositspent) != 0 ) { - printf("SWAP %u-%u finished!\n",requestid,quoteid); - sprintf(fname,"%s/SWAPS/%u-%u.finished",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); - jaddstr(item,"result","success"); - jaddstr(item,"status","finished"); - jaddnum(item,"requestid",requestid); - jaddnum(item,"quoteid",quoteid); - bits256_str(str,paymentspent), jaddbits256(item,"paymentspent",paymentspent); - bits256_str(str,Apaymentspent), jaddbits256(item,"Apaymentspent",Apaymentspent); - bits256_str(str,depositspent), jaddbits256(item,"depositspent",depositspent); - if ( (fp= fopen(fname,"wb")) != 0 ) - { - char *itemstr; - itemstr = jprint(item,0); - fprintf(fp,"%s\n",itemstr); - free(itemstr); - fclose(fp); - } - return(item); + finishedflag = 1; } - if ( iambob == 0 ) + if ( finishedflag == 0 && iambob == 0 ) { if ( sentflags[BASILISK_ALICESPEND] == 0 ) { @@ -3339,12 +3348,6 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 { //if ( txbytes[BASILISK_ALICECLAIM] == 0 ) { - /*privBn = basilisk_swap_privBn_extract(myinfo,&txids[BASILISK_BOBREFUND],bobcoin,txids[BASILISK_BOBDEPOSIT],privBn); - if ( bits256_nonz(privBn) != 0 ) - { - revcalc_rmd160_sha256(secretBn,privBn); - vcalc_sha256(0,secretBn256,privBn.bytes,sizeof(privBn)); - }*/ redeemlen = basilisk_swap_bobredeemscript(1,&secretstart,redeemscript,dlocktime,pubA0,pubB0,pubB1,privAm,privBn,secretAm,secretAm256,secretBn,secretBn256); if ( redeemlen > 0 ) { @@ -3361,7 +3364,7 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 } } else printf("now %u before expiration %u\n",(uint32_t)time(NULL),expiration); } - if ( sentflags[BASILISK_ALICEPAYMENT] != 0 && bits256_nonz(Apaymentspent) == 0 ) + if ( sentflags[BASILISK_ALICEPAYMENT] != 0 && bits256_nonz(Apaymentspent) == 0 && sentflags[BASILISK_ALICECLAIM] == 0 ) { //if ( txbytes[BASILISK_ALICERECLAIM] == 0 ) { @@ -3380,12 +3383,10 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 } } } - else if ( iambob == 1 ) + else if ( finishedflag == 0 && iambob == 1 ) { if ( sentflags[BASILISK_BOBSPEND] == 0 && bits256_nonz(Apaymentspent) == 0 ) { - if ( bits256_nonz(txids[BASILISK_ALICESPEND]) == 0 ) - txids[BASILISK_ALICESPEND] = paymentspent; // check destaddr to determine alice/bob if ( bits256_nonz(txids[BASILISK_ALICESPEND]) != 0 ) { //if ( txbytes[BASILISK_BOBSPEND] == 0 ) @@ -3463,6 +3464,24 @@ cJSON *basilisk_remember(struct supernet_info *myinfo,uint64_t *KMDtotals,uint64 free(txbytes[i]); } jadd(item,"sentflags",array); + jaddstr(item,"result","success"); + jaddstr(item,"status","finished"); + bits256_str(str,paymentspent), jaddbits256(item,"paymentspent",paymentspent); + bits256_str(str,Apaymentspent), jaddbits256(item,"Apaymentspent",Apaymentspent); + bits256_str(str,depositspent), jaddbits256(item,"depositspent",depositspent); + if ( finishedflag != 0 ) + { + printf("SWAP %u-%u finished!\n",requestid,quoteid); + sprintf(fname,"%s/SWAPS/%u-%u.finished",GLOBAL_DBDIR,requestid,quoteid), OS_compatible_path(fname); + if ( (fp= fopen(fname,"wb")) != 0 ) + { + char *itemstr; + itemstr = jprint(item,0); + fprintf(fp,"%s\n",itemstr); + free(itemstr); + fclose(fp); + } + } return(item); }