Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
a6fa1bd6d4
  1. 2
      iguana/SuperNET.c
  2. 2
      iguana/exchanges/bitcoin.c
  3. 50
      iguana/iguana_instantdex.c
  4. 223
      iguana/swaps/iguana_BTCswap.c

2
iguana/SuperNET.c

@ -579,7 +579,7 @@ char *SuperNET_DHTsend(struct supernet_info *myinfo,uint64_t destipbits,bits256
addr = &Coins[i]->peers.active[j]; addr = &Coins[i]->peers.active[j];
if ( addr->usock >= 0 && addr->supernet != 0 && (broadcastflag != 0 || category_peer(myinfo,addr,categoryhash,subhash) >= 0) ) if ( addr->usock >= 0 && addr->supernet != 0 && (broadcastflag != 0 || category_peer(myinfo,addr,categoryhash,subhash) >= 0) )
{ {
char str[65]; printf("BROADCAST[%d] %s SEND.(%ld) to %s\n",j,bits256_str(str,categoryhash),strlen(jsonstr),addr->ipaddr); char str[65]; printf("BROADCAST[%d] crc.%x %s SEND.(%ld) to %s\n",j,calc_crc32(0,jsonstr,(int32_t)strlen(jsonstr)),bits256_str(str,categoryhash),strlen(jsonstr),addr->ipaddr);
iguana_send_supernet(Coins[i],addr,jsonstr,maxdelay==0?0:(rand()%maxdelay)); iguana_send_supernet(Coins[i],addr,jsonstr,maxdelay==0?0:(rand()%maxdelay));
} }
} }

2
iguana/exchanges/bitcoin.c

@ -1499,7 +1499,7 @@ uint64_t TRADE(int32_t dotrade,char **retstrp,struct exchange_info *exchange,cha
char *str,coinaddr[64]; uint64_t txid = 0; cJSON *tmp,*json=0; struct instantdex_accept *ap; char *str,coinaddr[64]; uint64_t txid = 0; cJSON *tmp,*json=0; struct instantdex_accept *ap;
struct supernet_info *myinfo; uint8_t pubkey[33]; struct iguana_info *other; struct supernet_info *myinfo; uint8_t pubkey[33]; struct iguana_info *other;
myinfo = SuperNET_accountfind(argjson); myinfo = SuperNET_accountfind(argjson);
printf("TRADE with myinfo.%p\n",myinfo); //printf("TRADE with myinfo.%p\n",myinfo);
if ( retstrp != 0 ) if ( retstrp != 0 )
*retstrp = 0; *retstrp = 0;
if ( strcmp(base,"BTC") == 0 || strcmp(base,"btc") == 0 ) if ( strcmp(base,"BTC") == 0 || strcmp(base,"btc") == 0 )

50
iguana/iguana_instantdex.c

@ -59,14 +59,14 @@ struct instantdex_msghdr *instantdex_msgcreate(struct supernet_info *myinfo,stru
otherpubkey = acct777_msgpubkey(data,datalen); otherpubkey = acct777_msgpubkey(data,datalen);
timestamp = (uint32_t)time(NULL); timestamp = (uint32_t)time(NULL);
acct777_sign(&msg->sig,myinfo->privkey,otherpubkey,timestamp,data,datalen); acct777_sign(&msg->sig,myinfo->privkey,otherpubkey,timestamp,data,datalen);
printf("signed datalen.%d allocsize.%d crc.%x\n",datalen,msg->sig.allocsize,calc_crc32(0,data,datalen)); //printf("signed datalen.%d allocsize.%d crc.%x\n",datalen,msg->sig.allocsize,calc_crc32(0,data,datalen));
if ( (signerbits= acct777_validate(&msg->sig,acct777_msgprivkey(data,datalen),msg->sig.pubkey)) != 0 ) if ( (signerbits= acct777_validate(&msg->sig,acct777_msgprivkey(data,datalen),msg->sig.pubkey)) != 0 )
{ {
//int32_t i; //int32_t i;
char str[65],str2[65]; //char str[65],str2[65];
//for (i=0; i<datalen; i++) //for (i=0; i<datalen; i++)
// printf("%02x",data[i]); // printf("%02x",data[i]);
printf(">>>>>>>>>>>>>>>> validated [%ld] len.%d (%s + %s)\n",(long)data-(long)msg,datalen,bits256_str(str,acct777_msgprivkey(data,datalen)),bits256_str(str2,msg->sig.pubkey)); //printf(">>>>>>>>>>>>>>>> validated [%ld] len.%d (%s + %s)\n",(long)data-(long)msg,datalen,bits256_str(str,acct777_msgprivkey(data,datalen)),bits256_str(str2,msg->sig.pubkey));
memset(buf,0,sizeof(buf)); memset(buf,0,sizeof(buf));
acct777_rwsig(1,buf,&msg->sig); acct777_rwsig(1,buf,&msg->sig);
memcpy(&msg->sig,buf,sizeof(buf)); memcpy(&msg->sig,buf,sizeof(buf));
@ -156,13 +156,13 @@ char *instantdex_sendcmd(struct supernet_info *myinfo,struct instantdex_offer *o
break; break;
memcpy(msg->serialized,reqstr,slen); memcpy(msg->serialized,reqstr,slen);
memcpy(&msg->serialized[slen],serialized,olen); memcpy(&msg->serialized[slen],serialized,olen);
printf("extralen.%d datalen.%d slen.%d olen.%d\n",extralen,datalen,slen,olen); //printf("extralen.%d datalen.%d slen.%d olen.%d\n",extralen,datalen,slen,olen);
if ( extralen > 0 ) if ( extralen > 0 )
memcpy(&msg->serialized[slen + olen],extraser,extralen); memcpy(&msg->serialized[slen + olen],extraser,extralen);
free(reqstr); free(reqstr);
if ( instantdex_msgcreate(myinfo,msg,datalen) != 0 ) if ( instantdex_msgcreate(myinfo,msg,datalen) != 0 )
{ {
printf("instantdex send.(%s) datalen.%d allocsize.%d crc.%x\n",cmdstr,datalen,msg->sig.allocsize,calc_crc32(0,(void *)((long)msg + 8),datalen-8)); printf(">>>>>>>>>>>> instantdex send.(%s) datalen.%d allocsize.%d crc.%x\n",cmdstr,datalen,msg->sig.allocsize,calc_crc32(0,(void *)((long)msg + 8),datalen-8));
hexstr = malloc(msg->sig.allocsize*2 + 1); hexstr = malloc(msg->sig.allocsize*2 + 1);
init_hexbytes_noT(hexstr,(uint8_t *)msg,msg->sig.allocsize); init_hexbytes_noT(hexstr,(uint8_t *)msg,msg->sig.allocsize);
retstr = SuperNET_categorymulticast(myinfo,0,instantdexhash,desthash,hexstr,0,hops,1,argjson,0); retstr = SuperNET_categorymulticast(myinfo,0,instantdexhash,desthash,hexstr,0,hops,1,argjson,0);
@ -365,10 +365,10 @@ struct instantdex_accept *instantdex_acceptable(struct supernet_info *myinfo,str
{ {
if ( now < ap->offer.expiration && ap->dead == 0 && (offerbits == 0 || offerbits != ap->offer.offer64) ) if ( now < ap->offer.expiration && ap->dead == 0 && (offerbits == 0 || offerbits != ap->offer.offer64) )
{ {
printf("check offerbits.%llu vs %llu: %d %d %d %d %d %d %d %d\n",(long long)offerbits,(long long)ap->offer.offer64,A->offer.basevolume64 > 0.,strcmp(A->offer.base,"*") == 0 ,strcmp(A->offer.base,ap->offer.base) == 0, strcmp(A->offer.rel,"*") == 0 ,strcmp(A->offer.rel,ap->offer.rel) == 0,A->offer.basevolume64 <= (ap->offer.basevolume64 - ap->pendingvolume64),offerdir,instantdex_bidaskdir(ap)); //printf("check offerbits.%llu vs %llu: %d %d %d %d %d %d %d %d\n",(long long)offerbits,(long long)ap->offer.offer64,A->offer.basevolume64 > 0.,strcmp(A->offer.base,"*") == 0 ,strcmp(A->offer.base,ap->offer.base) == 0, strcmp(A->offer.rel,"*") == 0 ,strcmp(A->offer.rel,ap->offer.rel) == 0,A->offer.basevolume64 <= (ap->offer.basevolume64 - ap->pendingvolume64),offerdir,instantdex_bidaskdir(ap));
if ( A->offer.basevolume64 > 0. && (strcmp(A->offer.base,"*") == 0 || strcmp(A->offer.base,ap->offer.base) == 0) && (strcmp(A->offer.rel,"*") == 0 || strcmp(A->offer.rel,ap->offer.rel) == 0) && minvol <= (ap->offer.basevolume64 - ap->pendingvolume64) && offerdir*instantdex_bidaskdir(ap) < 0 ) if ( A->offer.basevolume64 > 0. && (strcmp(A->offer.base,"*") == 0 || strcmp(A->offer.base,ap->offer.base) == 0) && (strcmp(A->offer.rel,"*") == 0 || strcmp(A->offer.rel,ap->offer.rel) == 0) && minvol <= (ap->offer.basevolume64 - ap->pendingvolume64) && offerdir*instantdex_bidaskdir(ap) < 0 )
{ {
printf("aveprice %.8f %.8f offerdir.%d first cmp: %d %d %d\n",aveprice,dstr(ap->offer.price64),offerdir,A->offer.price64 == 0,(offerdir > 0 && ap->offer.price64 >= A->offer.price64),(offerdir < 0 && ap->offer.price64 <= A->offer.price64)); //printf("aveprice %.8f %.8f offerdir.%d first cmp: %d %d %d\n",aveprice,dstr(ap->offer.price64),offerdir,A->offer.price64 == 0,(offerdir > 0 && ap->offer.price64 >= A->offer.price64),(offerdir < 0 && ap->offer.price64 <= A->offer.price64));
if ( offerdir == 0 || A->offer.price64 == 0 || ((offerdir < 0 && ap->offer.price64 >= A->offer.price64) || (offerdir > 0 && ap->offer.price64 <= A->offer.price64)) ) if ( offerdir == 0 || A->offer.price64 == 0 || ((offerdir < 0 && ap->offer.price64 >= A->offer.price64) || (offerdir > 0 && ap->offer.price64 <= A->offer.price64)) )
{ {
printf("passed second cmp: offerdir.%d best %.8f ap %.8f\n",offerdir,dstr(bestprice64),dstr(ap->offer.price64)); printf("passed second cmp: offerdir.%d best %.8f ap %.8f\n",offerdir,dstr(bestprice64),dstr(ap->offer.price64));
@ -417,10 +417,10 @@ bits256 instantdex_acceptset(struct instantdex_accept *ap,char *base,char *rel,i
ap->offer.basevolume64 = volume * SATOSHIDEN; ap->offer.basevolume64 = volume * SATOSHIDEN;
vcalc_sha256(0,hash.bytes,(void *)&ap->offer,sizeof(ap->offer)); vcalc_sha256(0,hash.bytes,(void *)&ap->offer,sizeof(ap->offer));
ap->orderid = hash.txid; ap->orderid = hash.txid;
int32_t i; //int32_t i;
for (i=0; i<sizeof(ap->offer); i++) //for (i=0; i<sizeof(ap->offer); i++)
printf("%02x ",((uint8_t *)&ap->offer)[i]); // printf("%02x ",((uint8_t *)&ap->offer)[i]);
printf("\n(%s/%s) %.8f %.8f acceptdir.%d myside.%d\n",base,rel,price,volume,acceptdir,myside); //printf("\n(%s/%s) %.8f %.8f acceptdir.%d myside.%d\n",base,rel,price,volume,acceptdir,myside);
return(hash); return(hash);
} }
@ -511,6 +511,13 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
printf("orderhash %llu mismatch id.%llu\n",(long long)orderhash.txid,(long long)j64bits(argjson,"id")); printf("orderhash %llu mismatch id.%llu\n",(long long)orderhash.txid,(long long)j64bits(argjson,"id"));
return(clonestr("{\"error\":\"orderhash mismatch\"}")); return(clonestr("{\"error\":\"orderhash mismatch\"}"));
} }
//char str[65],str2[65];
if ( bits256_cmp(traderpub,myinfo->myaddr.persistent) == 0 )
{
printf("got my own packet\n");
return(clonestr("{\"result\":\"got my own packet\"}"));
}
//else printf("got mismatched traderpub.%s vs %s\n",bits256_str(str,traderpub),bits256_str(str2,myinfo->myaddr.persistent));
A.orderid = orderhash.txid; A.orderid = orderhash.txid;
if ( (ap= instantdex_offerfind(myinfo,exchange,0,0,A.orderid,"*","*")) != 0 ) if ( (ap= instantdex_offerfind(myinfo,exchange,0,0,A.orderid,"*","*")) != 0 )
{ {
@ -521,12 +528,7 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
printf("cant find existing order.%llu that matches\n",(long long)A.orderid); printf("cant find existing order.%llu that matches\n",(long long)A.orderid);
return(clonestr("{\"error\":\"cant find matching order\"}")); return(clonestr("{\"error\":\"cant find matching order\"}"));
} }
else if ( bits256_cmp(traderpub,myinfo->myaddr.persistent) == 0 ) //printf("call (%s/%s) swap baserel.%d acceptdir.%d\n",A.offer.base,A.offer.rel,A.offer.myside,A.offer.acceptdir);
{
printf("got my own request.(%s)\n",jprint(argjson,0));
return(clonestr("{\"result\":\"got my own request\"}"));
}
printf("call (%s/%s) swap baserel.%d acceptdir.%d\n",A.offer.base,A.offer.rel,A.offer.myside,A.offer.acceptdir);
if ( strncmp(cmdstr,"BTC",3) == 0 ) if ( strncmp(cmdstr,"BTC",3) == 0 )
retstr = instantdex_BTCswap(myinfo,exchange,&A,cmdstr+3,msg,argjson,remoteaddr,signerbits,serdata,datalen); retstr = instantdex_BTCswap(myinfo,exchange,&A,cmdstr+3,msg,argjson,remoteaddr,signerbits,serdata,datalen);
else if ( strncmp(cmdstr,"NXT",3) == 0 ) else if ( strncmp(cmdstr,"NXT",3) == 0 )
@ -541,7 +543,7 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
ap->info = A.info; ap->info = A.info;
ap->pendingvolume64 = A.pendingvolume64; ap->pendingvolume64 = A.pendingvolume64;
} }
printf("after swap ap.%p (%s)\n",ap,retstr); //printf("after swap ap.%p (%s)\n",ap,retstr);
return(retstr); return(retstr);
} }
return(clonestr("{\"error\":\"request needs argjson\"}")); return(clonestr("{\"error\":\"request needs argjson\"}"));
@ -555,10 +557,10 @@ char *InstantDEX_hexmsg(struct supernet_info *myinfo,void *ptr,int32_t len,char
cJSON *retjson,*item,*argjson = 0; cJSON *retjson,*item,*argjson = 0;
datalen = len - (int32_t)sizeof(msg->sig); datalen = len - (int32_t)sizeof(msg->sig);
serdata = (void *)((long)msg + sizeof(msg->sig)); serdata = (void *)((long)msg + sizeof(msg->sig));
printf("a signed datalen.%d allocsize.%d crc.%x\n",datalen,msg->sig.allocsize,calc_crc32(0,serdata,datalen)); //printf("a signed datalen.%d allocsize.%d crc.%x\n",datalen,msg->sig.allocsize,calc_crc32(0,serdata,datalen));
acct777_rwsig(0,(void *)&msg->sig,(void *)tmp); acct777_rwsig(0,(void *)&msg->sig,(void *)tmp);
memcpy(&msg->sig,tmp,sizeof(msg->sig)); memcpy(&msg->sig,tmp,sizeof(msg->sig));
printf("b signed datalen.%d allocsize.%d crc.%x\n",datalen,msg->sig.allocsize,calc_crc32(0,serdata,datalen)); // printf("b signed datalen.%d allocsize.%d crc.%x\n",datalen,msg->sig.allocsize,calc_crc32(0,serdata,datalen));
if ( remoteaddr != 0 && remoteaddr[0] == 0 && strcmp("127.0.0.1",remoteaddr) == 0 && ((uint8_t *)msg)[len-1] == 0 && (argjson= cJSON_Parse((char *)msg)) != 0 ) if ( remoteaddr != 0 && remoteaddr[0] == 0 && strcmp("127.0.0.1",remoteaddr) == 0 && ((uint8_t *)msg)[len-1] == 0 && (argjson= cJSON_Parse((char *)msg)) != 0 )
{ {
printf("string instantdex_hexmsg RESULT.(%s)\n",jprint(argjson,0)); printf("string instantdex_hexmsg RESULT.(%s)\n",jprint(argjson,0));
@ -570,10 +572,10 @@ char *InstantDEX_hexmsg(struct supernet_info *myinfo,void *ptr,int32_t len,char
else if ( (signerbits= acct777_validate(&msg->sig,acct777_msgprivkey(serdata,datalen),msg->sig.pubkey)) != 0 || 1 ) else if ( (signerbits= acct777_validate(&msg->sig,acct777_msgprivkey(serdata,datalen),msg->sig.pubkey)) != 0 || 1 )
{ {
flag++; flag++;
printf("InstantDEX_hexmsg <<<<<<<<<<<<< sigsize.%ld VALIDATED [%ld] len.%d t%u allocsize.%d (%s) [%d]\n",sizeof(msg->sig),(long)serdata-(long)msg,datalen,msg->sig.timestamp,msg->sig.allocsize,(char *)msg->serialized,serdata[datalen-1]); //printf("InstantDEX_hexmsg <<<<<<<<<<<<< sigsize.%ld VALIDATED [%ld] len.%d t%u allocsize.%d (%s) [%d]\n",sizeof(msg->sig),(long)serdata-(long)msg,datalen,msg->sig.timestamp,msg->sig.allocsize,(char *)msg->serialized,serdata[datalen-1]);
newlen = (int32_t)(msg->sig.allocsize - ((long)msg->serialized - (long)msg)); newlen = (int32_t)(msg->sig.allocsize - ((long)msg->serialized - (long)msg));
serdata = msg->serialized; serdata = msg->serialized;
printf("newlen.%d diff.%ld alloc.%d datalen.%d\n",newlen,((long)msg->serialized - (long)msg),msg->sig.allocsize,datalen); //printf("newlen.%d diff.%ld alloc.%d datalen.%d\n",newlen,((long)msg->serialized - (long)msg),msg->sig.allocsize,datalen);
if ( (argjson= cJSON_Parse((char *)serdata)) != 0 ) if ( (argjson= cJSON_Parse((char *)serdata)) != 0 )
{ {
slen = (int32_t)strlen((char *)serdata) + 1; slen = (int32_t)strlen((char *)serdata) + 1;
@ -586,7 +588,7 @@ char *InstantDEX_hexmsg(struct supernet_info *myinfo,void *ptr,int32_t len,char
newlen -= olen; newlen -= olen;
newlen -= ((long)msg->serialized - (long)msg); newlen -= ((long)msg->serialized - (long)msg);
serdata = &serdata[olen]; serdata = &serdata[olen];
printf("received orderhash.%llu olen.%d slen.%d newlen.%d\n",(long long)orderhash.txid,olen,slen,newlen); //printf("received orderhash.%llu olen.%d slen.%d newlen.%d\n",(long long)orderhash.txid,olen,slen,newlen);
} else olen = 0; } else olen = 0;
if ( newlen <= 0 ) if ( newlen <= 0 )
serdata = 0, newlen = 0; serdata = 0, newlen = 0;
@ -640,7 +642,7 @@ char *instantdex_queueaccept(struct supernet_info *myinfo,struct instantdex_acce
instantdex_acceptset(ap,base,rel,duration,myside,acceptdir,price,basevolume,offerer,0); instantdex_acceptset(ap,base,rel,duration,myside,acceptdir,price,basevolume,offerer,0);
queue_enqueue("acceptableQ",&exchange->acceptableQ,&ap->DL,0); queue_enqueue("acceptableQ",&exchange->acceptableQ,&ap->DL,0);
retstr = jprint(instantdex_acceptjson(ap),1); retstr = jprint(instantdex_acceptjson(ap),1);
printf("acceptableQ %llu (%s)\n",(long long)ap->orderid,retstr); //printf("acceptableQ %llu (%s)\n",(long long)ap->orderid,retstr);
return(retstr); return(retstr);
} else return(clonestr("{\"error\":\"invalid exchange\"}")); } else return(clonestr("{\"error\":\"invalid exchange\"}"));
} }

223
iguana/swaps/iguana_BTCswap.c

@ -336,7 +336,7 @@ cJSON *instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *
cJSON *newjson; char serstr[sizeof(bits256)*2+1],*hexstr=0; int32_t i,wrongfirstbyte,errs; cJSON *newjson; char serstr[sizeof(bits256)*2+1],*hexstr=0; int32_t i,wrongfirstbyte,errs;
bits256 hashpriv,pubi,otherpriv,*privs=0; uint8_t serialized[sizeof(bits256)],otherpubkey[33]; bits256 hashpriv,pubi,otherpriv,*privs=0; uint8_t serialized[sizeof(bits256)],otherpubkey[33];
newjson = cJSON_CreateObject();//instantdex_acceptsendjson(A); newjson = cJSON_CreateObject();//instantdex_acceptsendjson(A);
printf("acceptsend.(%s)\n",jprint(newjson,0)); //printf("acceptsend.(%s)\n",jprint(newjson,0));
if ( swap->otherschoosei < 0 && jobj(argjson,"mychoosei") != 0 ) if ( swap->otherschoosei < 0 && jobj(argjson,"mychoosei") != 0 )
{ {
printf("otherschoosei.%d\n",swap->otherschoosei); printf("otherschoosei.%d\n",swap->otherschoosei);
@ -345,7 +345,7 @@ cJSON *instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *
} }
if ( juint(argjson,"verified") != 0 ) if ( juint(argjson,"verified") != 0 )
swap->otherverifiedcut = 1; swap->otherverifiedcut = 1;
printf("otherverified.%d\n",swap->otherverifiedcut); //printf("otherverified.%d\n",swap->otherverifiedcut);
if ( jobj(argjson,"myprivs") != 0 && swap->cutverified == 0 ) if ( jobj(argjson,"myprivs") != 0 && swap->cutverified == 0 )
{ {
printf("got privs\n"); printf("got privs\n");
@ -393,13 +393,12 @@ cJSON *instantdex_newjson(struct supernet_info *myinfo,struct bitcoin_swapinfo *
} }
} }
jaddnum(newjson,"verified",swap->otherverifiedcut); jaddnum(newjson,"verified",swap->otherverifiedcut);
printf("otherverified.%d\n",swap->otherverifiedcut); //printf("otherverified.%d\n",swap->otherverifiedcut);
if ( instantdex_pubkeyargs(swap,newjson,2+flag777*777,myinfo->persistent_priv,hash,0x02 + swap->isbob) != 2+flag777*777 ) if ( instantdex_pubkeyargs(swap,newjson,2+flag777*777,myinfo->persistent_priv,hash,0x02 + swap->isbob) != 2+flag777*777 )
{ {
printf("error generating pubkeyargs\n"); printf("error generating pubkeyargs\n");
return(0); return(0);
} }
printf("call getpubs\n");
instantdex_getpubs(swap,argjson,newjson); instantdex_getpubs(swap,argjson,newjson);
if ( sendprivs != 0 ) if ( sendprivs != 0 )
{ {
@ -483,7 +482,6 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
char *retstr=0; uint64_t satoshis[2]; int32_t reftime,offerdir = 0; double minperc; char *retstr=0; uint64_t satoshis[2]; int32_t reftime,offerdir = 0; double minperc;
struct instantdex_accept *ap; struct bitcoin_swapinfo *swap = 0; bits256 orderhash,traderpub; struct instantdex_accept *ap; struct bitcoin_swapinfo *swap = 0; bits256 orderhash,traderpub;
struct iguana_info *coinbtc,*altcoin; cJSON *newjson=0; uint8_t pubkey[33]; struct iguana_info *coinbtc,*altcoin; cJSON *newjson=0; uint8_t pubkey[33];
printf("serdatalen.%d\n",datalen);
if ( exchange == 0 ) if ( exchange == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap null exchange ptr\"}")); return(clonestr("{\"error\":\"instantdex_BTCswap null exchange ptr\"}"));
offerdir = instantdex_bidaskdir(A); offerdir = instantdex_bidaskdir(A);
@ -507,9 +505,7 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
minperc = INSTANTDEX_MINPERC; minperc = INSTANTDEX_MINPERC;
if ( swap == 0 && strcmp(cmdstr,"offer") != 0 ) if ( swap == 0 && strcmp(cmdstr,"offer") != 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap no swap info after offer\"}")); return(clonestr("{\"error\":\"instantdex_BTCswap no swap info after offer\"}"));
printf("got offer.(%s/%s) %.8f vol %.8f %llu offerside.%d offerdir.%d swap.%p decksize.%ld\n",A->offer.base,A->offer.rel,dstr(A->offer.price64),dstr(A->offer.basevolume64),(long long)A->orderid,A->offer.myside,A->offer.acceptdir,A->info,sizeof(swap->deck)); printf("T.%d [%d] got offer.(%s/%s) %.8f vol %.8f %llu offerside.%d offerdir.%d swap.%p decksize.%ld\n",bits256_cmp(traderpub,myinfo->myaddr.persistent),swap!=0?swap->state:-1,A->offer.base,A->offer.rel,dstr(A->offer.price64),dstr(A->offer.basevolume64),(long long)A->orderid,A->offer.myside,A->offer.acceptdir,A->info,sizeof(swap->deck));
if ( swap != 0 )
printf("found existing trade to match isbob.%d state.%d\n",swap->isbob,swap->state);
if ( strcmp(cmdstr,"offer") == 0 ) // sender is Bob, receiver is network (Alice) if ( strcmp(cmdstr,"offer") == 0 ) // sender is Bob, receiver is network (Alice)
{ {
if ( A->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) ) if ( A->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) )
@ -550,126 +546,129 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
retstr = clonestr("{\"result\":\"added new order to orderbook\"}"); retstr = clonestr("{\"result\":\"added new order to orderbook\"}");
} }
} }
else if ( strcmp(cmdstr,"step1") == 0 && swap->state == 1 ) // either else
{ {
printf("got step1, should have other's choosei\n"); if ( strcmp(cmdstr,"step1") == 0 && swap->state == 1 ) // either
if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,1)) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step1 null newjson\"}"));
else if ( swap->otherschoosei < 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step1, no didnt choosei\"}"));
else
{ {
swap->state++; printf("got step1, should have other's choosei\n");
if ( (retstr= instantdex_choosei(swap,argjson,newjson,serdata,datalen)) != 0 ) if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,1)) == 0 )
return(retstr); return(clonestr("{\"error\":\"instantdex_BTCswap step1 null newjson\"}"));
if ( swap->isbob == 0 ) else if ( swap->otherschoosei < 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step1, no didnt choosei\"}"));
else
{ {
int64_t insurance = (satoshis[1] * INSTANTDEX_INSURANCERATE + coinbtc->chain->txfee); // txfee prevents dust attack swap->state++;
if ( (swap->feetx= instantdex_bobtx(myinfo,coinbtc,&swap->ftxid,swap->otherpubs[0],swap->mypubs[0],swap->privkeys[swap->otherschoosei],reftime,insurance,1)) != 0 ) if ( (retstr= instantdex_choosei(swap,argjson,newjson,serdata,datalen)) != 0 )
return(retstr);
if ( swap->isbob == 0 )
{ {
jaddstr(newjson,"feetx",swap->feetx); int64_t insurance = (satoshis[1] * INSTANTDEX_INSURANCERATE + coinbtc->chain->txfee); // txfee prevents dust attack
jaddbits256(newjson,"ftxid",swap->ftxid); if ( (swap->feetx= instantdex_bobtx(myinfo,coinbtc,&swap->ftxid,swap->otherpubs[0],swap->mypubs[0],swap->privkeys[swap->otherschoosei],reftime,insurance,1)) != 0 )
// broadcast to network {
jaddstr(newjson,"feetx",swap->feetx);
jaddbits256(newjson,"ftxid",swap->ftxid);
// broadcast to network
}
} }
return(instantdex_sendcmd(myinfo,&A->offer,newjson,swap->isbob != 0 ? "BTCstep1" : "BTCstep2",swap->othertrader,INSTANTDEX_HOPS,swap->deck,sizeof(swap->deck)));
} }
return(instantdex_sendcmd(myinfo,&A->offer,newjson,swap->isbob != 0 ? "BTCstep1" : "BTCstep2",swap->othertrader,INSTANTDEX_HOPS,swap->deck,sizeof(swap->deck)));
} }
} else if ( strcmp(cmdstr,"step2") == 0 && swap->isbob != 0 && swap->state == 2 ) // Bob
else if ( strcmp(cmdstr,"step2") == 0 && swap->isbob != 0 && swap->state == 2 ) // Bob
{
printf("got step2, should have other's privkeys\n");
if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step2 null newjson\"}"));
else if ( swap->cutverified == 0 || swap->otherverifiedcut == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step2, both sides didnt validate\"}"));
else //if ( instantdex_feeverify() == 0 )
{ {
swap->state++; printf("got step2, should have other's privkeys\n");
//char *instantdex_bobtx(myinfo,coin,&paytxid,pubA0,pubB0,privAm,reftime,relsatoshis,0); if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 )
if ( (swap->deposit= instantdex_bobtx(myinfo,coinbtc,&swap->dtxid,swap->otherpubs[0],swap->mypubs[0],swap->privkeys[swap->otherschoosei],reftime,satoshis[swap->isbob],1)) != 0 ) return(clonestr("{\"error\":\"instantdex_BTCswap step2 null newjson\"}"));
else if ( swap->cutverified == 0 || swap->otherverifiedcut == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step2, both sides didnt validate\"}"));
else //if ( instantdex_feeverify() == 0 )
{ {
jaddstr(newjson,"deposit",swap->deposit); swap->state++;
jaddbits256(newjson,"dtxid",swap->dtxid); //char *instantdex_bobtx(myinfo,coin,&paytxid,pubA0,pubB0,privAm,reftime,relsatoshis,0);
jaddbits256(newjson,"pubBn",bitcoin_pubkey33(pubkey,swap->privkeys[swap->otherschoosei])); if ( (swap->deposit= instantdex_bobtx(myinfo,coinbtc,&swap->dtxid,swap->otherpubs[0],swap->mypubs[0],swap->privkeys[swap->otherschoosei],reftime,satoshis[swap->isbob],1)) != 0 )
// broadcast to network {
return(instantdex_sendcmd(myinfo,&A->offer,newjson,"BTCstep3",swap->othertrader,INSTANTDEX_HOPS,0,0)); jaddstr(newjson,"deposit",swap->deposit);
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step2, cant create deposit\"}")); jaddbits256(newjson,"dtxid",swap->dtxid);
} //else return(clonestr("{\"error\":\"instantdex_BTCswap step2 invalid fee\"}")); jaddbits256(newjson,"pubBn",bitcoin_pubkey33(pubkey,swap->privkeys[swap->otherschoosei]));
} // broadcast to network
else if ( strcmp(cmdstr,"step3") == 0 && swap->isbob == 0 && swap->state == 2 ) // Alice return(instantdex_sendcmd(myinfo,&A->offer,newjson,"BTCstep3",swap->othertrader,INSTANTDEX_HOPS,0,0));
{ } else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step2, cant create deposit\"}"));
printf("Alice got step3 should have Bob's choosei\n"); } //else return(clonestr("{\"error\":\"instantdex_BTCswap step2 invalid fee\"}"));
if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 ) }
return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3 null newjson\"}")); else if ( strcmp(cmdstr,"step3") == 0 && swap->isbob == 0 && swap->state == 2 ) // Alice
else if ( swap->cutverified == 0 || swap->otherverifiedcut == 0 || bits256_nonz(swap->pubBn) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step3, both sides didnt validate\"}"));
else if ( instantdex_paymentverify(myinfo,coinbtc,swap,A,argjson,1) == 0 )
{ {
swap->state++; printf("Alice got step3 should have Bob's choosei\n");
//char *instantdex_alicetx(myinfo,altcoin,&alicetxid,pubAm,pubBn,satoshis); if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 )
swap->pubAm = bitcoin_pubkey33(pubkey,swap->privkeys[swap->otherschoosei]); return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3 null newjson\"}"));
if ( (swap->altpayment= instantdex_alicetx(myinfo,altcoin,swap->altmsigaddr,&swap->aptxid,swap->pubAm,swap->pubBn,satoshis[swap->isbob])) != 0 ) else if ( swap->cutverified == 0 || swap->otherverifiedcut == 0 || bits256_nonz(swap->pubBn) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step3, both sides didnt validate\"}"));
else if ( instantdex_paymentverify(myinfo,coinbtc,swap,A,argjson,1) == 0 )
{ {
jaddstr(newjson,"altpayment",swap->altpayment); swap->state++;
jaddstr(newjson,"altmsigaddr",swap->altmsigaddr); //char *instantdex_alicetx(myinfo,altcoin,&alicetxid,pubAm,pubBn,satoshis);
jaddbits256(newjson,"aptxid",swap->aptxid); swap->pubAm = bitcoin_pubkey33(pubkey,swap->privkeys[swap->otherschoosei]);
jaddbits256(newjson,"pubAm",swap->pubAm); if ( (swap->altpayment= instantdex_alicetx(myinfo,altcoin,swap->altmsigaddr,&swap->aptxid,swap->pubAm,swap->pubBn,satoshis[swap->isbob])) != 0 )
// broadcast to network {
return(instantdex_sendcmd(myinfo,&A->offer,newjson,"BTCstep4",swap->othertrader,INSTANTDEX_HOPS,0,0)); jaddstr(newjson,"altpayment",swap->altpayment);
} else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3, error making altpay\"}")); jaddstr(newjson,"altmsigaddr",swap->altmsigaddr);
} else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3, invalid deposit\"}")); jaddbits256(newjson,"aptxid",swap->aptxid);
} jaddbits256(newjson,"pubAm",swap->pubAm);
else if ( strcmp(cmdstr,"step4") == 0 && swap->isbob != 0 && swap->state == 3 ) // Bob // broadcast to network
{ return(instantdex_sendcmd(myinfo,&A->offer,newjson,"BTCstep4",swap->othertrader,INSTANTDEX_HOPS,0,0));
printf("Bob got step4 should have Alice's altpayment\n"); } else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3, error making altpay\"}"));
if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 ) } else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3, invalid deposit\"}"));
return(clonestr("{\"error\":\"instantdex_BTCswap Bob step4 null newjson\"}")); }
else if ( bits256_nonz(swap->pubAm) == 0 ) else if ( strcmp(cmdstr,"step4") == 0 && swap->isbob != 0 && swap->state == 3 ) // Bob
return(clonestr("{\"error\":\"instantdex_BTCswap step4, no pubAm\"}"));
else if ( instantdex_altpaymentverify(myinfo,altcoin,swap,A,argjson) == 0 )
{ {
swap->state++; printf("Bob got step4 should have Alice's altpayment\n");
if ( (swap->deposit= instantdex_bobtx(myinfo,coinbtc,&swap->ptxid,swap->mypubs[1],swap->otherpubs[0],swap->privkeys[swap->otherschoosei],reftime,satoshis[swap->isbob],0)) != 0 ) if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap Bob step4 null newjson\"}"));
else if ( bits256_nonz(swap->pubAm) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap step4, no pubAm\"}"));
else if ( instantdex_altpaymentverify(myinfo,altcoin,swap,A,argjson) == 0 )
{ {
jaddstr(newjson,"payment",swap->payment); swap->state++;
jaddbits256(newjson,"ptxid",swap->ptxid); if ( (swap->deposit= instantdex_bobtx(myinfo,coinbtc,&swap->ptxid,swap->mypubs[1],swap->otherpubs[0],swap->privkeys[swap->otherschoosei],reftime,satoshis[swap->isbob],0)) != 0 )
// broadcast to network {
return(instantdex_sendcmd(myinfo,&A->offer,newjson,"BTCstep5",swap->othertrader,INSTANTDEX_HOPS,0,0)); jaddstr(newjson,"payment",swap->payment);
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step4, cant create payment\"}")); jaddbits256(newjson,"ptxid",swap->ptxid);
} else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3, invalid deposit\"}")); // broadcast to network
} return(instantdex_sendcmd(myinfo,&A->offer,newjson,"BTCstep5",swap->othertrader,INSTANTDEX_HOPS,0,0));
else if ( strcmp(cmdstr,"step5") == 0 && swap->isbob == 0 && swap->state == 4 ) // Alice } else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step4, cant create payment\"}"));
{ } else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step3, invalid deposit\"}"));
printf("Alice got step5 should have Bob's payment\n"); }
if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 ) else if ( strcmp(cmdstr,"step5") == 0 && swap->isbob == 0 && swap->state == 4 ) // Alice
return(clonestr("{\"error\":\"instantdex_BTCswap Alice step5 null newjson\"}"));
else if ( instantdex_paymentverify(myinfo,coinbtc,swap,A,argjson,0) == 0 )
{ {
swap->state++; printf("Alice got step5 should have Bob's payment\n");
/*if ( (swap->spendtx= instantdex_spendpayment(myinfo,coinbtc,&swap->stxid,swap,argjson,newjson)) != 0 ) if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 )
return(clonestr("{\"error\":\"instantdex_BTCswap Alice step5 null newjson\"}"));
else if ( instantdex_paymentverify(myinfo,coinbtc,swap,A,argjson,0) == 0 )
{ {
// broadcast to network swap->state++;
return(instantdex_sendcmd(myinfo,&A->A,newjson,"BTCstep6",swap->othertrader,INSTANTDEX_HOPS)); /*if ( (swap->spendtx= instantdex_spendpayment(myinfo,coinbtc,&swap->stxid,swap,argjson,newjson)) != 0 )
} else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step5, cant spend payment\"}"));*/ {
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6, invalid payment\"}")); // broadcast to network
} return(instantdex_sendcmd(myinfo,&A->A,newjson,"BTCstep6",swap->othertrader,INSTANTDEX_HOPS));
else if ( strcmp(cmdstr,"step6") == 0 ) // Bob } else return(clonestr("{\"error\":\"instantdex_BTCswap Alice step5, cant spend payment\"}"));*/
{ } else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6, invalid payment\"}"));
printf("Bob got step6 should have Alice's privkey\n"); }
if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 ) else if ( strcmp(cmdstr,"step6") == 0 ) // Bob
return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6 null newjson\"}"));
/*else if ( instantdex_spendverify(myinfo,coinbtc,swap,A,argjson,0) == 0 )
{ {
if ( (swap->altspend= instantdex_spendaltpayment(myinfo,altcoin,&swap->astxid,swap,argjson,newjson)) != 0 ) printf("Bob got step6 should have Alice's privkey\n");
{ if ( (newjson= instantdex_newjson(myinfo,swap,argjson,orderhash,A,0,0)) == 0 )
jaddstr(newjson,"altspend",swap->altspend); return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6 null newjson\"}"));
jaddbits256(newjson,"astxid",swap->astxid); /*else if ( instantdex_spendverify(myinfo,coinbtc,swap,A,argjson,0) == 0 )
// broadcast to network {
return(clonestr("{\"result\":\"Bob finished atomic swap\"}")); if ( (swap->altspend= instantdex_spendaltpayment(myinfo,altcoin,&swap->astxid,swap,argjson,newjson)) != 0 )
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6, cant spend altpayment\"}")); {
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6, invalid spend\"}"));*/ jaddstr(newjson,"altspend",swap->altspend);
jaddbits256(newjson,"astxid",swap->astxid);
// broadcast to network
return(clonestr("{\"result\":\"Bob finished atomic swap\"}"));
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6, cant spend altpayment\"}"));
} else return(clonestr("{\"error\":\"instantdex_BTCswap Bob step6, invalid spend\"}"));*/
}
else retstr = clonestr("{\"error\":\"BTC swap got unrecognized command\"}");
} }
else retstr = clonestr("{\"error\":\"BTC swap got unrecognized command\"}");
if ( retstr == 0 ) if ( retstr == 0 )
retstr = clonestr("{\"error\":\"BTC swap null retstr\"}"); retstr = clonestr("{\"error\":\"BTC swap null retstr\"}");
if ( swap != 0 ) if ( swap != 0 )
@ -901,7 +900,7 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
return(clonestr("{\"error\":\"txid mismatches orderid\"}")); return(clonestr("{\"error\":\"txid mismatches orderid\"}"));
satoshis[0] = A->offer.basevolume64; satoshis[0] = A->offer.basevolume64;
satoshis[1] = instantdex_relsatoshis(A->offer.price64,A->offer.basevolume64); satoshis[1] = instantdex_relsatoshis(A->offer.price64,A->offer.basevolume64);
printf("got offer.(%s) offerside.%d offerdir.%d\n",jprint(argjson,0),A->offer.myside,A->offer.acceptdir); //printf("got offer.(%s) offerside.%d offerdir.%d\n",jprint(argjson,0),A->offer.myside,A->offer.acceptdir);
if ( strcmp(cmdstr,"offer") == 0 ) // sender is Bob, receiver is network (Alice) if ( strcmp(cmdstr,"offer") == 0 ) // sender is Bob, receiver is network (Alice)
{ {
if ( A->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) ) if ( A->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) )

Loading…
Cancel
Save