Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
2b2936abe5
  1. 2
      iguana/SuperNET_hexmsg.c
  2. 6
      iguana/exchanges/bitcoin.c
  3. 7
      iguana/exchanges777.h
  4. 45
      iguana/iguana_instantdex.c
  5. 77
      iguana/swaps/iguana_BTCswap.c

2
iguana/SuperNET_hexmsg.c

@ -26,7 +26,7 @@ int32_t SuperNET_hexmsgfind(struct supernet_info *myinfo,bits256 category,bits25
if ( bits256_nonz(subhash) == 0 ) if ( bits256_nonz(subhash) == 0 )
subhash = GENESIS_PUBKEY; subhash = GENESIS_PUBKEY;
packethash = curve25519(category,packethash); packethash = curve25519(category,packethash);
printf("addflag.%d packethash.%llx dest.%llx\n",addflag,(long long)packethash.txid,(long long)category.txid); //printf("addflag.%d packethash.%llx dest.%llx\n",addflag,(long long)packethash.txid,(long long)category.txid);
for (i=0; i<sizeof(Packetcache)/sizeof(*Packetcache); i++) for (i=0; i<sizeof(Packetcache)/sizeof(*Packetcache); i++)
{ {
if ( Packetcache[i] == 0 ) if ( Packetcache[i] == 0 )

6
iguana/exchanges/bitcoin.c

@ -634,7 +634,9 @@ int32_t _iguana_calcrmd160(struct iguana_info *coin,struct vin_info *vp)
plen = *script++; plen = *script++;
if ( bitcoin_pubkeylen(script) != plen ) if ( bitcoin_pubkeylen(script) != plen )
{ {
printf("multisig.%d of %d: invalid pubkey[%02x] len %d\n",i,n,script[0],bitcoin_pubkeylen(script)); static int32_t counter;
if ( counter++ < 3 )
printf("multisig.%d of %d: invalid pubkey[%02x] len %d\n",i,n,script[0],bitcoin_pubkeylen(script));
return(-1); return(-1);
} }
memcpy(vp->signers[i].pubkey,script,plen); memcpy(vp->signers[i].pubkey,script,plen);
@ -703,7 +705,7 @@ int32_t iguana_calcrmd160(struct iguana_info *coin,struct vin_info *vp,uint8_t *
scriptlen = iguana_scriptgen(coin,&vp->M,&vp->N,vp->coinaddr,script,asmstr,vp->rmd160,vp->type,(const struct vin_info *)vp,vout); scriptlen = iguana_scriptgen(coin,&vp->M,&vp->N,vp->coinaddr,script,asmstr,vp->rmd160,vp->type,(const struct vin_info *)vp,vout);
if ( scriptlen != pk_scriptlen || (scriptlen != 0 && memcmp(script,pk_script,scriptlen) != 0) ) if ( scriptlen != pk_scriptlen || (scriptlen != 0 && memcmp(script,pk_script,scriptlen) != 0) )
{ {
if ( vp->type != IGUANA_SCRIPT_OPRETURN ) if ( vp->type != IGUANA_SCRIPT_OPRETURN && vp->type != IGUANA_SCRIPT_DATA )
{ {
int32_t i; int32_t i;
printf("\n--------------------\n"); printf("\n--------------------\n");

7
iguana/exchanges777.h

@ -91,7 +91,12 @@ struct exchange_request
}; };
struct instantdex_offer { char base[24],rel[24]; uint64_t price64,basevolume64,offer64; uint32_t expiration,nonce; char myside,acceptdir; }; struct instantdex_offer { char base[24],rel[24]; uint64_t price64,basevolume64,offer64; uint32_t expiration,nonce; char myside,acceptdir; };
struct instantdex_accept { struct queueitem DL; void *info; uint64_t pendingvolume64,orderid; uint32_t dead; struct instantdex_offer offer; }; struct instantdex_accept
{
struct queueitem DL; void *info;
uint64_t pendingvolume64,orderid,matchid; uint32_t dead;
struct instantdex_offer offer;
};
struct instantdex_accept *instantdex_offerfind(struct supernet_info *myinfo,struct exchange_info *exchange,cJSON *bids,cJSON *asks,uint64_t orderid,char *base,char *rel); struct instantdex_accept *instantdex_offerfind(struct supernet_info *myinfo,struct exchange_info *exchange,cJSON *bids,cJSON *asks,uint64_t orderid,char *base,char *rel);
cJSON *instantdex_acceptjson(struct instantdex_accept *ap); cJSON *instantdex_acceptjson(struct instantdex_accept *ap);

45
iguana/iguana_instantdex.c

@ -59,7 +59,7 @@ 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\n",datalen,msg->sig.allocsize); //printf("signed datalen.%d allocsize.%d\n",datalen,msg->sig.allocsize);
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;
@ -81,10 +81,10 @@ bits256 instantdex_rwoffer(int32_t rwflag,int32_t *lenp,uint8_t *serialized,stru
if ( rwflag == 1 ) if ( rwflag == 1 )
{ {
vcalc_sha256(0,orderhash.bytes,(void *)offer,sizeof(*offer)); vcalc_sha256(0,orderhash.bytes,(void *)offer,sizeof(*offer));
int32_t i; /*int32_t i;
for (i=0; i<sizeof(*offer); i++) for (i=0; i<sizeof(*offer); i++)
printf("%02x ",((uint8_t *)offer)[i]); printf("%02x ",((uint8_t *)offer)[i]);
printf("rwoffer offer\n"); printf("rwoffer offer\n");*/
} }
else else
{ {
@ -102,21 +102,21 @@ bits256 instantdex_rwoffer(int32_t rwflag,int32_t *lenp,uint8_t *serialized,stru
if ( rwflag == 0 ) if ( rwflag == 0 )
{ {
vcalc_sha256(0,orderhash.bytes,(void *)offer,sizeof(*offer)); vcalc_sha256(0,orderhash.bytes,(void *)offer,sizeof(*offer));
int32_t i; /*int32_t i;
for (i=0; i<len; i++) for (i=0; i<len; i++)
printf("%02x ",serialized[i]); printf("%02x ",serialized[i]);
printf("read rwoffer serialized\n"); printf("read rwoffer serialized\n");
for (i=0; i<sizeof(*offer); i++) for (i=0; i<sizeof(*offer); i++)
printf("%02x ",((uint8_t *)offer)[i]); printf("%02x ",((uint8_t *)offer)[i]);
printf("rwoffer offer\n"); printf("rwoffer offer\n");*/
} }
else /*else
{ {
int32_t i; int32_t i;
for (i=0; i<len; i++) for (i=0; i<len; i++)
printf("%02x ",serialized[i]); printf("%02x ",serialized[i]);
printf("wrote rwoffer serialized\n"); printf("wrote rwoffer serialized\n");
} }*/
*lenp = len; *lenp = len;
return(orderhash); return(orderhash);
} }
@ -134,6 +134,7 @@ char *instantdex_sendcmd(struct supernet_info *myinfo,struct instantdex_offer *o
jaddbits256(argjson,"categoryhash",instantdexhash); jaddbits256(argjson,"categoryhash",instantdexhash);
jaddbits256(argjson,"traderpub",myinfo->myaddr.persistent); jaddbits256(argjson,"traderpub",myinfo->myaddr.persistent);
orderhash = instantdex_rwoffer(1,&olen,serialized,offer); orderhash = instantdex_rwoffer(1,&olen,serialized,offer);
if ( 0 )
{ {
struct instantdex_offer checkoffer; bits256 checkhash; int32_t checklen; struct instantdex_offer checkoffer; bits256 checkhash; int32_t checklen;
checkhash = instantdex_rwoffer(0,&checklen,serialized,&checkoffer); checkhash = instantdex_rwoffer(0,&checklen,serialized,&checkoffer);
@ -322,15 +323,15 @@ struct instantdex_accept *instantdex_offerfind(struct supernet_info *myinfo,stru
{ {
if ( now < ap->offer.expiration && ap->dead == 0 ) if ( now < ap->offer.expiration && ap->dead == 0 )
{ {
printf("find cmps %d %d %d %d %d %d\n",strcmp(base,"*") == 0,strcmp(base,ap->offer.base) == 0,strcmp(rel,"*") == 0,strcmp(rel,ap->offer.rel) == 0,orderid == 0,orderid == ap->orderid); //printf("find cmps %d %d %d %d %d %d\n",strcmp(base,"*") == 0,strcmp(base,ap->offer.base) == 0,strcmp(rel,"*") == 0,strcmp(rel,ap->offer.rel) == 0,orderid == 0,orderid == ap->orderid);
if ( (strcmp(base,"*") == 0 || strcmp(base,ap->offer.base) == 0) && (strcmp(rel,"*") == 0 || strcmp(rel,ap->offer.rel) == 0) && (orderid == 0 || orderid == ap->orderid) ) if ( (strcmp(base,"*") == 0 || strcmp(base,ap->offer.base) == 0) && (strcmp(rel,"*") == 0 || strcmp(rel,ap->offer.rel) == 0) && (orderid == 0 || orderid == ap->orderid) )
{ {
printf("found match\n"); //printf("found match\n");
retap = ap; retap = ap;
} }
if ( (item= instantdex_acceptjson(ap)) != 0 ) if ( (item= instantdex_acceptjson(ap)) != 0 )
{ {
printf("item.(%s)\n",jprint(item,0)); //printf("item.(%s)\n",jprint(item,0));
if ( (type= jstr(item,"type")) != 0 ) if ( (type= jstr(item,"type")) != 0 )
{ {
if ( strcmp(type,"bid") == 0 && bids != 0 ) if ( strcmp(type,"bid") == 0 && bids != 0 )
@ -362,8 +363,8 @@ struct instantdex_accept *instantdex_acceptable(struct supernet_info *myinfo,str
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 passed first cmp: %d %d %d %d\n",aveprice,dstr(ap->offer.price64),offerdir == 0,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));
if ( bestprice64 == 0 || (offerdir < 0 && ap->offer.price64 < bestprice64) || (offerdir > 0 && ap->offer.price64 > bestprice64) ) if ( bestprice64 == 0 || (offerdir < 0 && ap->offer.price64 < bestprice64) || (offerdir > 0 && ap->offer.price64 > bestprice64) )
@ -501,15 +502,8 @@ char *instantdex_parse(struct supernet_info *myinfo,struct instantdex_msghdr *ms
return(clonestr("{\"error\":\"orderhash mismatch\"}")); return(clonestr("{\"error\":\"orderhash mismatch\"}"));
} }
A.orderid = orderhash.txid; A.orderid = orderhash.txid;
printf("A.orderid.%llu\n",(long long)A.orderid);
if ( bits256_cmp(traderpub,myinfo->myaddr.persistent) == 0 )
{
printf("got my own request.(%s)\n",jprint(argjson,0));
return(clonestr("{\"result\":\"got my own request\"}"));
}
if ( (ap= instantdex_offerfind(myinfo,exchange,0,0,A.orderid,"*","*")) != 0 ) if ( (ap= instantdex_offerfind(myinfo,exchange,0,0,A.orderid,"*","*")) != 0 )
{ {
printf("found existing trade to match\n");
A = *ap; A = *ap;
} }
else if ( strcmp(cmdstr+3,"offer") != 0 ) else if ( strcmp(cmdstr+3,"offer") != 0 )
@ -517,6 +511,11 @@ 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("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); 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);
@ -560,7 +559,7 @@ 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 ) else if ( (signerbits= acct777_validate(&msg->sig,acct777_msgprivkey(serdata,datalen),msg->sig.pubkey)) != 0 )
{ {
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 - sizeof(*msg)); newlen = (int32_t)(msg->sig.allocsize - sizeof(*msg));
serdata = msg->serialized; serdata = msg->serialized;
if ( (argjson= cJSON_Parse((char *)serdata)) != 0 ) if ( (argjson= cJSON_Parse((char *)serdata)) != 0 )
@ -572,14 +571,14 @@ char *InstantDEX_hexmsg(struct supernet_info *myinfo,void *ptr,int32_t len,char
if ( newlen > 0 ) if ( newlen > 0 )
{ {
orderhash = instantdex_rwoffer(0,&olen,&msg->serialized[slen],&rawoffer); orderhash = instantdex_rwoffer(0,&olen,&msg->serialized[slen],&rawoffer);
printf("received orderhash.%llu\n",(long long)orderhash.txid); //printf("received orderhash.%llu\n",(long long)orderhash.txid);
newlen -= olen; newlen -= olen;
} }
if ( newlen <= 0 ) if ( newlen <= 0 )
serdata = 0, newlen = 0; serdata = 0, newlen = 0;
if ( serdata != 0 || argjson != 0 ) if ( serdata != 0 || argjson != 0 )
{ {
printf("CALL instantdex_parse.(%s)\n",argjson!=0?jprint(argjson,0):""); //printf("CALL instantdex_parse.(%s)\n",argjson!=0?jprint(argjson,0):"");
retjson = cJSON_CreateArray(); retjson = cJSON_CreateArray();
if ( (num= SuperNET_MYINFOS(myinfos,sizeof(myinfos)/sizeof(*myinfos))) == 0 ) if ( (num= SuperNET_MYINFOS(myinfos,sizeof(myinfos)/sizeof(*myinfos))) == 0 )
{ {
@ -638,7 +637,7 @@ void instantdex_update(struct supernet_info *myinfo)
{ {
while ( (m= category_gethexmsg(myinfo,instantdexhash,iter == 0 ? GENESIS_PUBKEY : myinfo->myaddr.persistent)) != 0 ) while ( (m= category_gethexmsg(myinfo,instantdexhash,iter == 0 ? GENESIS_PUBKEY : myinfo->myaddr.persistent)) != 0 )
{ {
printf("gothexmsg len.%d\n",m->len); //printf("gothexmsg len.%d\n",m->len);
pm = (struct instantdex_msghdr *)m->msg; pm = (struct instantdex_msghdr *)m->msg;
if ( m->remoteipbits != 0 ) if ( m->remoteipbits != 0 )
expand_ipbits(remote,m->remoteipbits); expand_ipbits(remote,m->remoteipbits);

77
iguana/swaps/iguana_BTCswap.c

@ -261,21 +261,14 @@ bits256 instantdex_derivekeypair(bits256 *newprivp,uint8_t pubkey[33],bits256 pr
return(bitcoin_pubkey33(pubkey,*newprivp)); return(bitcoin_pubkey33(pubkey,*newprivp));
} }
int32_t instantdex_pubkeyargs(bits256 *sharedprivs,cJSON *newjson,int32_t numpubs,bits256 privkey,bits256 hash,int32_t firstbyte) int32_t instantdex_pubkeyargs(struct bitcoin_swapinfo *swap,cJSON *newjson,int32_t numpubs,bits256 privkey,bits256 hash,int32_t firstbyte)
{ {
char buf[3],*hexstr=0; int32_t i,n,len=0; bits256 pubi; uint64_t txid; uint8_t secret160[20],pubkey[33],*hex=0; char buf[3]; int32_t i,n,m,len=0; bits256 pubi; uint64_t txid; uint8_t secret160[20],pubkey[33];
if ( numpubs > 2 )
{
if ( (hexstr= malloc(777 * sizeof(uint64_t) * 2 * 2 + 1)) == 0 )
return(-1);
if ( (hex= malloc(777 * sizeof(uint64_t) * 2)) == 0 )
return(-1);
}
sprintf(buf,"%c0",'A' - 0x02 + firstbyte); sprintf(buf,"%c0",'A' - 0x02 + firstbyte);
for (i=n=0; i<numpubs*100 && n<numpubs; i++) for (i=n=m=0; i<numpubs*100 && n<numpubs; i++)
{ {
pubi = instantdex_derivekeypair(&sharedprivs[n],pubkey,privkey,hash); pubi = instantdex_derivekeypair(&swap->privkeys[n],pubkey,privkey,hash);
privkey = sharedprivs[n]; privkey = swap->privkeys[n];
//printf("i.%d n.%d numpubs.%d %02x vs %02x\n",i,n,numpubs,pubkey[0],firstbyte); //printf("i.%d n.%d numpubs.%d %02x vs %02x\n",i,n,numpubs,pubkey[0],firstbyte);
if ( pubkey[0] != firstbyte ) if ( pubkey[0] != firstbyte )
continue; continue;
@ -286,46 +279,36 @@ int32_t instantdex_pubkeyargs(bits256 *sharedprivs,cJSON *newjson,int32_t numpub
} }
else else
{ {
calc_rmd160_sha256(secret160,sharedprivs[n].bytes,sizeof(sharedprivs[n])); calc_rmd160_sha256(secret160,swap->privkeys[n].bytes,sizeof(swap->privkeys[n]));
memcpy(&txid,secret160,sizeof(txid)); memcpy(&txid,secret160,sizeof(txid));
len += iguana_rwnum(1,&hex[len],sizeof(txid),&txid); len += iguana_rwnum(1,(uint8_t *)&swap->deck[m][0],sizeof(txid),&txid);
len += iguana_rwnum(1,&hex[len],sizeof(pubi.txid),&pubi.txid); len += iguana_rwnum(1,(uint8_t *)&swap->deck[m][1],sizeof(pubi.txid),&pubi.txid);
m++;
} }
n++; n++;
} }
if ( n > 2 && newjson != 0 && len > 0 )
{
init_hexbytes_noT(hexstr,hex,len);
jaddstr(newjson,"cut",hexstr);
}
if ( hex != 0 )
free(hex);
if ( hexstr != 0 )
free(hexstr);
return(n); return(n);
} }
char *instantdex_choosei(struct bitcoin_swapinfo *swap,cJSON *newjson,cJSON *argjson) char *instantdex_choosei(struct bitcoin_swapinfo *swap,cJSON *newjson,cJSON *argjson,uint8_t *serdata,int32_t datalen)
{ {
char *cutstr; int32_t i,j,max,len = 0; uint64_t x; int32_t i,j,max,len = 0; uint64_t x;
if ( swap->choosei < 0 && (cutstr= jstr(argjson,"cut")) != 0 ) if ( swap->choosei < 0 && serdata != 0 && datalen == sizeof(swap->deck) )
{ {
max = (int32_t)(sizeof(swap->otherscut) / sizeof(*swap->otherscut)); max = (int32_t)(sizeof(swap->otherscut) / sizeof(*swap->otherscut));
if ( strlen(cutstr) == sizeof(swap->otherscut)*2 ) for (i=0; i<max; i++)
{ for (j=0; j<2; j++)
decode_hex((uint8_t *)swap->otherscut,sizeof(swap->otherscut),cutstr); len += iguana_rwnum(1,(uint8_t *)&swap->otherscut[i][j],sizeof(x),&serdata[len]);
for (i=0; i<max; i++)
for (j=0; j<2; j++)
{
len += iguana_rwnum(1,(uint8_t *)&swap->otherscut[i][j],sizeof(x),&x);
swap->otherscut[i][j] = x;
}
} else return(clonestr("{\"error\":\"instantdex_BTCswap offer wrong cutsize\"}"));
OS_randombytes((uint8_t *)&swap->choosei,sizeof(swap->choosei)); OS_randombytes((uint8_t *)&swap->choosei,sizeof(swap->choosei));
swap->choosei %= max; swap->choosei %= max;
jaddnum(newjson,"mychoosei",swap->choosei); jaddnum(newjson,"mychoosei",swap->choosei);
return(0); return(0);
} else return(clonestr("{\"error\":\"instantdex_BTCswap offer no cut\"}")); }
else
{
printf("invalid datalen.%d\n",datalen);
return(clonestr("{\"error\":\"instantdex_BTCswap offer no cut\"}"));
}
} }
void instantdex_getpubs(struct bitcoin_swapinfo *swap,cJSON *argjson,cJSON *newjson) void instantdex_getpubs(struct bitcoin_swapinfo *swap,cJSON *argjson,cJSON *newjson)
@ -411,7 +394,7 @@ 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->privkeys,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);
@ -466,7 +449,7 @@ char *instantdex_btcoffer(struct supernet_info *myinfo,struct exchange_info *exc
printf("instantdex_btcoffer checkA error\n"); printf("instantdex_btcoffer checkA error\n");
} }
swap = calloc(1,sizeof(struct bitcoin_swapinfo)), swap->isbob = dir > 0, swap->choosei = swap->otherschoosei = -1; swap = calloc(1,sizeof(struct bitcoin_swapinfo)), swap->isbob = dir > 0, swap->choosei = swap->otherschoosei = -1;
if ( instantdex_pubkeyargs(swap->privkeys,newjson,777+2,myinfo->persistent_priv,hash,0x02+isbob) != 4 ) if ( instantdex_pubkeyargs(swap,newjson,777+2,myinfo->persistent_priv,hash,0x02+isbob) != 4 )
return(clonestr("{\"error\":\"highly unlikely run of 02 pubkeys\"}")); return(clonestr("{\"error\":\"highly unlikely run of 02 pubkeys\"}"));
if ( price > SMALLVAL ) if ( price > SMALLVAL )
{ {
@ -489,7 +472,7 @@ char *instantdex_btcoffer(struct supernet_info *myinfo,struct exchange_info *exc
} else return(instantdex_sendcmd(myinfo,&ap->offer,newjson,"BTCoffer",GENESIS_PUBKEY,INSTANTDEX_HOPS,0,0)); } else return(instantdex_sendcmd(myinfo,&ap->offer,newjson,"BTCoffer",GENESIS_PUBKEY,INSTANTDEX_HOPS,0,0));
} }
char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exchange,struct instantdex_accept *A,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *data,int32_t datalen) // receiving side char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exchange,struct instantdex_accept *A,char *cmdstr,struct instantdex_msghdr *msg,cJSON *argjson,char *remoteaddr,uint64_t signerbits,uint8_t *serdata,int32_t datalen) // receiving side
{ {
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;
@ -517,8 +500,10 @@ 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) offerside.%d offerdir.%d swap.%p\n",jprint(argjson,0),A->offer.myside,A->offer.acceptdir,A->info); printf("got offer.(%s/%s) %.8f vol %.8f %llu offerside.%d offerdir.%d swap.%p\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);
if ( strcmp(cmdstr,"offer") == 0 && A->info == 0 ) // sender is Bob, receiver is network (Alice) 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 ( A->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) ) if ( A->offer.expiration < (time(NULL) + INSTANTDEX_DURATION) )
return(clonestr("{\"error\":\"instantdex_BTCswap offer too close to expiration\"}")); return(clonestr("{\"error\":\"instantdex_BTCswap offer too close to expiration\"}"));
@ -540,7 +525,7 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
{ {
//printf("NEWJSON.(%s)\n",jprint(newjson,0)); //printf("NEWJSON.(%s)\n",jprint(newjson,0));
instantdex_pendingnotice(myinfo,exchange,ap,A); instantdex_pendingnotice(myinfo,exchange,ap,A);
if ( (retstr= instantdex_choosei(swap,argjson,newjson)) != 0 ) if ( (retstr= instantdex_choosei(swap,argjson,newjson,serdata,datalen)) != 0 )
{ {
printf("Choosei.%s\n",retstr); printf("Choosei.%s\n",retstr);
return(retstr); return(retstr);
@ -551,7 +536,7 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
} }
else else
{ {
printf("no matching trade.(%s) -> InstantDEX_minaccept\n",jprint(argjson,0)); printf("no matching trade for %llu -> InstantDEX_minaccept\n",(long long)A->orderid);
ap = calloc(1,sizeof(*ap)); ap = calloc(1,sizeof(*ap));
*ap = *A; *ap = *A;
queue_enqueue("acceptableQ",&exchange->acceptableQ,&ap->DL,0); queue_enqueue("acceptableQ",&exchange->acceptableQ,&ap->DL,0);
@ -569,7 +554,7 @@ char *instantdex_BTCswap(struct supernet_info *myinfo,struct exchange_info *exch
else else
{ {
swap->state++; swap->state++;
if ( (retstr= instantdex_choosei(swap,argjson,newjson)) != 0 ) if ( (retstr= instantdex_choosei(swap,argjson,newjson,serdata,datalen)) != 0 )
return(retstr); return(retstr);
if ( swap->isbob == 0 ) if ( swap->isbob == 0 )
{ {

Loading…
Cancel
Save