You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

715 lines
30 KiB

7 years ago
/******************************************************************************
* Copyright © 2014-2017 The SuperNET Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* SuperNET software, including this file may be copied, modified, propagated *
* or distributed except according to the terms contained in the LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
//
// LP_signatures.c
// marketmaker
//
struct basilisk_request *LP_requestinit(struct basilisk_request *rp,bits256 srchash,bits256 desthash,char *src,uint64_t srcsatoshis,char *dest,uint64_t destsatoshis,uint32_t timestamp,uint32_t quotetime,int32_t DEXselector)
{
struct basilisk_request R;
memset(rp,0,sizeof(*rp));
rp->srchash = srchash;
rp->srcamount = srcsatoshis;
rp->timestamp = timestamp;
rp->DEXselector = DEXselector;
safecopy(rp->src,src,sizeof(rp->src));
safecopy(rp->dest,dest,sizeof(rp->dest));
R = *rp;
rp->requestid = basilisk_requestid(rp);
rp->quotetime = quotetime;
rp->desthash = desthash;
rp->destamount = destsatoshis;
rp->quoteid = basilisk_quoteid(rp);
7 years ago
//printf("r.%u %u, q.%u %u: %s %.8f -> %s %.8f\n",rp->timestamp,rp->requestid,rp->quotetime,rp->quoteid,rp->src,dstr(rp->srcamount),rp->dest,dstr(rp->destamount));
7 years ago
return(rp);
}
cJSON *LP_quotejson(struct LP_quoteinfo *qp)
{
7 years ago
double price; char etomic[64],activesymbol[65]; cJSON *retjson = cJSON_CreateObject();
if ( jobj(retjson,"gui") == 0 )
jaddstr(retjson,"gui",qp->gui[0] != 0 ? qp->gui : LP_gui);
7 years ago
jadd64bits(retjson,"aliceid",qp->aliceid);
7 years ago
jaddnum(retjson,"tradeid",qp->tradeid);
7 years ago
jaddstr(retjson,"base",qp->srccoin);
7 years ago
if ( LP_etomicsymbol(activesymbol,etomic,qp->srccoin) != 0 )
7 years ago
jaddstr(retjson,"bobetomic",etomic);
7 years ago
jaddstr(retjson,"etomicsrc",qp->etomicsrc);
7 years ago
jaddstr(retjson,"rel",qp->destcoin);
7 years ago
if ( LP_etomicsymbol(activesymbol,etomic,qp->destcoin) != 0 )
7 years ago
jaddstr(retjson,"alicetomic",etomic);
7 years ago
jaddstr(retjson,"etomicdest",qp->etomicdest);
7 years ago
if ( qp->coinaddr[0] != 0 )
jaddstr(retjson,"address",qp->coinaddr);
if ( qp->timestamp != 0 )
jaddnum(retjson,"timestamp",qp->timestamp);
if ( bits256_nonz(qp->txid) != 0 )
{
jaddbits256(retjson,"txid",qp->txid);
jaddnum(retjson,"vout",qp->vout);
}
if ( bits256_nonz(qp->srchash) != 0 )
jaddbits256(retjson,"srchash",qp->srchash);
if ( qp->txfee != 0 )
jadd64bits(retjson,"txfee",qp->txfee);
if ( qp->quotetime != 0 )
jaddnum(retjson,"quotetime",qp->quotetime);
if ( qp->satoshis != 0 )
jadd64bits(retjson,"satoshis",qp->satoshis);
if ( bits256_nonz(qp->desthash) != 0 )
jaddbits256(retjson,"desthash",qp->desthash);
if ( bits256_nonz(qp->txid2) != 0 )
{
jaddbits256(retjson,"txid2",qp->txid2);
jaddnum(retjson,"vout2",qp->vout2);
}
if ( bits256_nonz(qp->desttxid) != 0 )
{
if ( qp->destaddr[0] != 0 )
jaddstr(retjson,"destaddr",qp->destaddr);
jaddbits256(retjson,"desttxid",qp->desttxid);
jaddnum(retjson,"destvout",qp->destvout);
}
if ( bits256_nonz(qp->feetxid) != 0 )
{
jaddbits256(retjson,"feetxid",qp->feetxid);
jaddnum(retjson,"feevout",qp->feevout);
}
if ( qp->desttxfee != 0 )
jadd64bits(retjson,"desttxfee",qp->desttxfee);
if ( qp->destsatoshis != 0 )
{
jadd64bits(retjson,"destsatoshis",qp->destsatoshis);
if ( qp->satoshis != 0 )
{
price = (double)qp->destsatoshis / (qp->satoshis - qp->txfee);
7 years ago
jaddnum(retjson,"price",price);
7 years ago
}
}
7 years ago
if ( qp->R.requestid != 0 )
jaddnum(retjson,"requestid",qp->R.requestid);
if ( qp->R.quoteid != 0 )
jaddnum(retjson,"quoteid",qp->R.quoteid);
7 years ago
return(retjson);
}
int32_t LP_quoteparse(struct LP_quoteinfo *qp,cJSON *argjson)
{
7 years ago
uint32_t rid,qid; char etomic[64],activesymbol[65],*etomicstr;
7 years ago
memset(qp,0,sizeof(*qp));
7 years ago
safecopy(qp->gui,LP_gui,sizeof(qp->gui));
7 years ago
safecopy(qp->srccoin,jstr(argjson,"base"),sizeof(qp->srccoin));
7 years ago
if ( LP_etomicsymbol(activesymbol,etomic,qp->srccoin) != 0 )
7 years ago
{
7 years ago
if ( (etomicstr= jstr(argjson,"bobetomic")) == 0 || strcmp(etomicstr,etomic) != 0 )
7 years ago
{
printf("etomic src mismatch (%s) vs (%s)\n",etomicstr!=0?etomicstr:"",etomic);
return(-1);
}
}
7 years ago
safecopy(qp->coinaddr,jstr(argjson,"address"),sizeof(qp->coinaddr));
7 years ago
safecopy(qp->etomicsrc,jstr(argjson,"etomicsrc"),sizeof(qp->etomicsrc));
7 years ago
safecopy(qp->destcoin,jstr(argjson,"rel"),sizeof(qp->destcoin));
7 years ago
if ( LP_etomicsymbol(activesymbol,etomic,qp->destcoin) != 0 )
7 years ago
{
7 years ago
if ( (etomicstr= jstr(argjson,"alicetomic")) == 0 || strcmp(etomicstr,etomic) != 0 )
7 years ago
{
printf("etomic dest mismatch (%s) vs (%s)\n",etomicstr!=0?etomicstr:"",etomic);
return(-1);
}
}
7 years ago
safecopy(qp->destaddr,jstr(argjson,"destaddr"),sizeof(qp->destaddr));
7 years ago
safecopy(qp->etomicdest,jstr(argjson,"etomicdest"),sizeof(qp->etomicdest));
7 years ago
qp->aliceid = j64bits(argjson,"aliceid");
7 years ago
qp->tradeid = juint(argjson,"tradeid");
7 years ago
qp->timestamp = juint(argjson,"timestamp");
qp->quotetime = juint(argjson,"quotetime");
qp->txid = jbits256(argjson,"txid");
qp->txid2 = jbits256(argjson,"txid2");
qp->vout = jint(argjson,"vout");
qp->vout2 = jint(argjson,"vout2");
qp->feevout = jint(argjson,"feevout");
qp->srchash = jbits256(argjson,"srchash");
qp->desttxid = jbits256(argjson,"desttxid");
qp->feetxid = jbits256(argjson,"feetxid");
qp->destvout = jint(argjson,"destvout");
qp->desthash = jbits256(argjson,"desthash");
qp->satoshis = j64bits(argjson,"satoshis");
qp->destsatoshis = j64bits(argjson,"destsatoshis");
qp->txfee = j64bits(argjson,"txfee");
qp->desttxfee = j64bits(argjson,"desttxfee");
7 years ago
qp->R.requestid = juint(argjson,"requestid");
qp->R.quoteid = juint(argjson,"quoteid");
7 years ago
if ( qp->R.requestid == 0 )
7 years ago
{
7 years ago
rid = basilisk_requestid(&qp->R);
7 years ago
if ( qp->R.requestid != 0 && qp->R.requestid != rid )
printf("requestid.%u -> %u\n",qp->R.requestid,rid);
7 years ago
qp->R.requestid = rid;
}
7 years ago
if ( qp->R.quoteid == 0 )
7 years ago
{
7 years ago
qid = basilisk_quoteid(&qp->R);
7 years ago
if ( qp->R.quoteid != 0 && qp->R.quoteid != qid )
printf("quoteid.%u -> %u\n",qp->R.quoteid,qid);
7 years ago
qp->R.quoteid = qid;
7 years ago
}
7 years ago
return(0);
}
void LP_txfees(uint64_t *txfeep,uint64_t *desttxfeep,char *base,char *rel)
{
*txfeep = LP_txfeecalc(LP_coinfind(base),0,0);
*desttxfeep = LP_txfeecalc(LP_coinfind(rel),0,0);
7 years ago
//printf("LP_txfees(%.8f %.8f)\n",dstr(*txfeep),dstr(*desttxfeep));
7 years ago
}
int32_t LP_quoteinfoinit(struct LP_quoteinfo *qp,struct LP_utxoinfo *utxo,char *destcoin,double price,uint64_t satoshis,uint64_t destsatoshis)
{
memset(qp,0,sizeof(*qp));
if ( qp->timestamp == 0 )
qp->timestamp = (uint32_t)time(NULL);
safecopy(qp->destcoin,destcoin,sizeof(qp->destcoin));
LP_txfees(&qp->txfee,&qp->desttxfee,utxo->coin,qp->destcoin);
qp->satoshis = satoshis;//(destsatoshis / price) + 0.49;
qp->destsatoshis = destsatoshis;
/*if ( qp->txfee >= qp->satoshis || qp->txfee >= utxo->deposit.value || utxo->deposit.value < LP_DEPOSITSATOSHIS(qp->satoshis) ) //utxo->iambob == 0 ||
{
printf("quoteinit error.(%d %d %d %d) %.8f vs %.8f\n",utxo->iambob == 0,qp->txfee >= qp->satoshis,qp->txfee >= utxo->deposit.value,utxo->deposit.value < LP_DEPOSITSATOSHIS(qp->satoshis),dstr(utxo->deposit.value),dstr(LP_DEPOSITSATOSHIS(qp->satoshis)));
return(-1);
}*/
qp->txid = utxo->payment.txid;
qp->vout = utxo->payment.vout;
qp->txid2 = utxo->deposit.txid;
qp->vout2 = utxo->deposit.vout;
if ( qp->desttxfee >= qp->destsatoshis )
{
printf("quoteinit desttxfee %.8f < %.8f destsatoshis\n",dstr(qp->desttxfee),dstr(qp->destsatoshis));
return(-2);
}
safecopy(qp->srccoin,utxo->coin,sizeof(qp->srccoin));
safecopy(qp->coinaddr,utxo->coinaddr,sizeof(qp->coinaddr));
qp->srchash = utxo->pubkey;
return(0);
}
int32_t LP_quotedestinfo(struct LP_quoteinfo *qp,bits256 desttxid,int32_t destvout,bits256 feetxid,int32_t feevout,bits256 desthash,char *destaddr)
{
qp->desttxid = desttxid;
qp->destvout = destvout;
qp->desthash = desthash;
qp->feetxid = feetxid;
qp->feevout = feevout;
safecopy(qp->destaddr,destaddr,sizeof(qp->destaddr));
return(0);
}
7 years ago
char *LP_quotereceived(struct LP_quoteinfo *qp)
7 years ago
{
7 years ago
struct LP_cacheinfo *ptr; double price;
//LP_quoteparse(&Q,argjson);
price = (double)qp->destsatoshis / (qp->satoshis - qp->txfee);
if ( (ptr= LP_cacheadd(qp->srccoin,qp->destcoin,qp->txid,qp->vout,price,qp)) != 0 )
7 years ago
{
7 years ago
ptr->Q = *qp;
printf(">>>>>>>>>> received quote %s/%s %.8f\n",qp->srccoin,qp->destcoin,price);
7 years ago
return(clonestr("{\"result\":\"updated\"}"));
} else return(clonestr("{\"error\":\"nullptr\"}"));
}
7 years ago
int32_t LP_bitcoinsig_add(cJSON *item,bits256 priv,uint8_t *pubsecp,bits256 sighash)
{
7 years ago
static void *ctx; int32_t i,j,siglen; uint8_t pub33[33],sig[65]; char sigstr[256];
7 years ago
if ( ctx == 0 )
ctx = bitcoin_ctx();
for (j=0; j<100; j++)
{
if ( (siglen= bitcoin_sign(ctx,"sigadd",sig,sighash,priv,1)) > 0 && siglen == 65 )
{
memset(pub33,0,33);
if ( bitcoin_recoververify(ctx,"test",sig,sighash,pub33,0) == 0 && memcmp(pub33,pubsecp,33) == 0 )
{
init_hexbytes_noT(sigstr,sig,siglen);
jaddstr(item,"sig",sigstr);
return(siglen);
}
if ( 0 )
{
for (i=0; i<33; i++)
printf("%02x",pubsecp[i]);
printf(" pubsecp -> ");
for (i=0; i<33; i++)
printf("%02x",pub33[i]);
printf(" mismatched recovered pubkey.%d of %d\n",j,100);
}
}
}
return(-1);
}
bits256 LP_price_sighash(uint32_t timestamp,uint8_t *pubsecp,bits256 pubkey,char *base,char *rel,uint64_t price64)
{
uint8_t buf[sizeof(pubkey) + 33 + sizeof(uint64_t)*3 + sizeof(timestamp)]; uint64_t basebits,relbits; bits256 sighash;
basebits = stringbits(base);
relbits = stringbits(rel);
memcpy(buf,pubkey.bytes,sizeof(pubkey));
memcpy(&buf[sizeof(pubkey)],pubsecp,33);
memcpy(&buf[sizeof(pubkey)+33],&price64,sizeof(price64));
memcpy(&buf[sizeof(pubkey)+33+sizeof(price64)],&basebits,sizeof(basebits));
memcpy(&buf[sizeof(pubkey)+33+sizeof(price64)+sizeof(basebits)],&relbits,sizeof(relbits));
memcpy(&buf[sizeof(pubkey)+33+sizeof(price64)+sizeof(basebits)+sizeof(relbits)],&timestamp,sizeof(timestamp));
vcalc_sha256(0,sighash.bytes,buf,sizeof(buf));
return(sighash);
}
bits256 LP_pubkey_sighash(uint32_t timestamp,bits256 pubkey,uint8_t *rmd160,uint8_t *pubsecp)
{
uint8_t buf[sizeof(pubkey) + 20 + 33 + sizeof(timestamp)]; bits256 sighash;
memcpy(buf,pubkey.bytes,sizeof(pubkey));
memcpy(&buf[sizeof(pubkey)],rmd160,20);
memcpy(&buf[sizeof(pubkey)+20],pubsecp,33);
memcpy(&buf[sizeof(pubkey)+20+33],&timestamp,sizeof(timestamp));
vcalc_sha256(0,sighash.bytes,buf,sizeof(buf));
return(sighash);
}
7 years ago
bits256 LP_utxos_sighash(uint32_t timestamp,uint8_t *pubsecp,bits256 pubkey,bits256 utxoshash)
7 years ago
{
7 years ago
uint8_t buf[sizeof(pubkey)+sizeof(utxoshash)+33+sizeof(timestamp)]; bits256 sighash;
7 years ago
memcpy(buf,pubkey.bytes,sizeof(pubkey));
memcpy(&buf[sizeof(pubkey)],pubsecp,33);
memcpy(&buf[sizeof(pubkey)+33],&timestamp,sizeof(timestamp));
7 years ago
memcpy(&buf[sizeof(pubkey)+33+sizeof(timestamp)],utxoshash.bytes,sizeof(utxoshash));
vcalc_sha256(0,sighash.bytes,buf,sizeof(buf));
7 years ago
return(sighash);
}
7 years ago
int32_t LP_utxos_sigcheck(uint32_t timestamp,char *sigstr,char *pubsecpstr,bits256 pubkey,bits256 utxoshash)
7 years ago
{
7 years ago
static void *ctx; int32_t retval=-1; uint8_t pub33[33],pubsecp[33],sig[65]; bits256 sighash; char str[65]; struct LP_pubkey_info *pubp;
7 years ago
if ( ctx == 0 )
ctx = bitcoin_ctx();
7 years ago
pubp = LP_pubkeyfind(pubkey);
7 years ago
if ( (pubp == 0 || pubp->numerrors < LP_MAXPUBKEY_ERRORS) && sigstr != 0 && pubsecpstr != 0 && strlen(sigstr) == 65*2 && strlen(pubsecpstr) == 33 *2 )
7 years ago
{
decode_hex(sig,65,sigstr);
decode_hex(pubsecp,33,pubsecpstr);
7 years ago
sighash = LP_utxos_sighash(timestamp,pubsecp,pubkey,utxoshash);
retval = bitcoin_recoververify(ctx,"utxos",sig,sighash,pub33,0);
7 years ago
if ( memcmp(pub33,pubsecp,33) != 0 || retval != 0 )
{
7 years ago
static uint32_t counter;
7 years ago
if ( counter++ <= LP_MAXPUBKEY_ERRORS )
7 years ago
{
if ( pubp != 0 )
pubp->numerrors++;
7 years ago
if ( pubp != 0 && pubp->numerrors > LP_MAXPUBKEY_ERRORS/2 )
7 years ago
printf("LP_utxos_sigcheck failure.%d, probably from %s with older version\n",pubp!=0?pubp->numerrors:-1,bits256_str(str,pubkey));
7 years ago
}
7 years ago
retval = -1;
} else retval = 0;
}
return(retval);
}
7 years ago
bits256 LP_utxoshash_calc(cJSON *array)
{
int32_t i,j,n; bits256 utxoshash,txid; cJSON *item;
memset(utxoshash.bytes,0,sizeof(utxoshash));
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
for (i=0; i<n; i++)
{
item = jitem(array,i);
txid = jbits256(item,"tx_hash");
for (j=0; j<4; j++)
utxoshash.ulongs[j] ^= txid.ulongs[j];
utxoshash.uints[0] ^= jint(item,"tx_pos");
}
}
return(utxoshash);
}
int32_t LP_utxos_sigadd(cJSON *item,uint32_t timestamp,bits256 priv,uint8_t *pubsecp,bits256 pubkey,bits256 utxoshash)
7 years ago
{
bits256 sighash;
7 years ago
sighash = LP_utxos_sighash(timestamp,pubsecp,pubkey,utxoshash);
7 years ago
return(LP_bitcoinsig_add(item,priv,pubsecp,sighash));
}
7 years ago
void LP_postutxos(char *symbol,char *coinaddr)
{
7 years ago
bits256 zero; uint32_t timestamp; bits256 utxoshash; char pubsecpstr[67]; struct iguana_info *coin; cJSON *array,*reqjson = cJSON_CreateObject();
7 years ago
return;
7 years ago
if ( (coin= LP_coinfind(symbol)) != 0 && (array= LP_address_utxos(coin,coinaddr,1)) != 0 )
{
//printf("LP_postutxos pubsock.%d %s %s\n",pubsock,symbol,coin->smartaddr);
if ( cJSON_GetArraySize(array) == 0 )
free_json(array);
else
{
memset(zero.bytes,0,sizeof(zero));
jaddstr(reqjson,"method","postutxos");
jaddstr(reqjson,"coin",symbol);
jaddstr(reqjson,"coinaddr",coinaddr);
jadd(reqjson,"utxos",array);
7 years ago
timestamp = (uint32_t)time(NULL);
jaddnum(reqjson,"timetamp",timestamp);
init_hexbytes_noT(pubsecpstr,G.LP_pubsecp,33);
jaddstr(reqjson,"pubsecp",pubsecpstr);
jaddbits256(reqjson,"pubkey",G.LP_mypub25519);
7 years ago
utxoshash = LP_utxoshash_calc(array);
7 years ago
//char str[65]; printf("utxoshash add %s\n",bits256_str(str,utxoshash));
7 years ago
LP_utxos_sigadd(reqjson,timestamp,G.LP_privkey,G.LP_pubsecp,G.LP_mypub25519,utxoshash);
7 years ago
//printf("post (%s) -> %d\n",msg,LP_mypubsock);
7 years ago
LP_reserved_msg(0,symbol,symbol,zero,jprint(reqjson,1));
7 years ago
}
}
}
7 years ago
int32_t LP_price_sigcheck(uint32_t timestamp,char *sigstr,char *pubsecpstr,bits256 pubkey,char *base,char *rel,uint64_t price64)
{
7 years ago
static void *ctx; int32_t retval=-1; uint8_t pub33[33],pubsecp[33],sig[65]; bits256 sighash; struct LP_pubkey_info *pubp;
7 years ago
if ( ctx == 0 )
ctx = bitcoin_ctx();
7 years ago
pubp = LP_pubkeyfind(pubkey);
if ( (pubp == 0 || pubp->numerrors < LP_MAXPUBKEY_ERRORS) && sigstr != 0 && pubsecpstr != 0 && strlen(sigstr) == 65*2 && strlen(pubsecpstr) == 33 *2 )
7 years ago
{
decode_hex(sig,65,sigstr);
decode_hex(pubsecp,33,pubsecpstr);
sighash = LP_price_sighash(timestamp,pubsecp,pubkey,base,rel,price64);
retval = bitcoin_recoververify(ctx,"price",sig,sighash,pub33,0);
if ( memcmp(pub33,pubsecp,33) != 0 || retval != 0 )
{
7 years ago
if ( pubp != 0 )
pubp->numerrors++;
7 years ago
printf("LP_price_sigcheck failure\n");
retval = -1;
7 years ago
}
else
{
retval = 0;
7 years ago
//printf("valid price sig %s/%s %.8f\n",base,rel,dstr(price64));
7 years ago
}
7 years ago
}
return(retval);
}
int32_t LP_price_sigadd(cJSON *item,uint32_t timestamp,bits256 priv,uint8_t *pubsecp,bits256 pubkey,char *base,char *rel,uint64_t price64)
{
bits256 sighash;
sighash = LP_price_sighash(timestamp,pubsecp,pubkey,base,rel,price64);
return(LP_bitcoinsig_add(item,priv,pubsecp,sighash));
}
7 years ago
char *LP_pricepings(void *ctx,char *myipaddr,int32_t pubsock,char *base,char *rel,double price)
{
7 years ago
struct iguana_info *basecoin,*relcoin,*kmd; struct LP_address *ap; char pubsecpstr[67]; uint32_t numutxos,timestamp; uint64_t price64,balance,minsize,maxsize; bits256 zero; cJSON *reqjson;
reqjson = cJSON_CreateObject();
7 years ago
// LP_addsig
7 years ago
if ( (basecoin= LP_coinfind(base)) != 0 && (relcoin= LP_coinfind(rel)) != 0 )//&& basecoin->electrum == 0 )//&& relcoin->electrum == 0 )
{
memset(zero.bytes,0,sizeof(zero));
jaddbits256(reqjson,"pubkey",G.LP_mypub25519);
jaddstr(reqjson,"base",base);
jaddstr(reqjson,"rel",rel);
7 years ago
price64 = price * SATOSHIDEN + 0.0000000049;
7 years ago
jaddnum(reqjson,"price",price);
7 years ago
jadd64bits(reqjson,"price64",price64);
jaddstr(reqjson,"method","postprice");
7 years ago
timestamp = (uint32_t)time(NULL);
jaddnum(reqjson,"timestamp",timestamp);
init_hexbytes_noT(pubsecpstr,G.LP_pubsecp,33);
jaddstr(reqjson,"pubsecp",pubsecpstr);
7 years ago
if ( (kmd= LP_coinfind("KMD")) != 0 && (ap= LP_address(kmd,kmd->smartaddr)) != 0 && ap->instantdex_credits != 0 )
jaddnum(reqjson,"credits",dstr(ap->instantdex_credits));
if ( (numutxos= LP_address_minmax(&balance,&minsize,&maxsize,basecoin,basecoin->smartaddr)) != 0 )
{
7 years ago
//printf("send %s numutxos.%d balance %.8f min %.8f max %.8f\n",base,numutxos,dstr(balance),dstr(minsize),dstr(maxsize));
jaddstr(reqjson,"utxocoin",base);
jaddnum(reqjson,"n",numutxos);
jaddnum(reqjson,"bal",dstr(balance));
jaddnum(reqjson,"min",dstr(minsize));
jaddnum(reqjson,"max",dstr(maxsize));
}
7 years ago
LP_price_sigadd(reqjson,timestamp,G.LP_privkey,G.LP_pubsecp,G.LP_mypub25519,base,rel,price64);
7 years ago
LP_reserved_msg(0,base,rel,zero,jprint(reqjson,1));
return(clonestr("{\"result\":\"success\"}"));
} else return(clonestr("{\"error\":\"electrum node cant post bob asks\"}"));
7 years ago
}
char *LP_postprice_recv(cJSON *argjson)
{
bits256 pubkey; double price; char *base,*rel;
7 years ago
//printf("PRICE POSTED.(%s)\n",jprint(argjson,0));
7 years ago
if ( (base= jstr(argjson,"base")) != 0 && (rel= jstr(argjson,"rel")) != 0 && (price= jdouble(argjson,"price")) > SMALLVAL )
{
pubkey = jbits256(argjson,"pubkey");
if ( bits256_nonz(pubkey) != 0 )
{
7 years ago
if ( LP_price_sigcheck(juint(argjson,"timestamp"),jstr(argjson,"sig"),jstr(argjson,"pubsecp"),pubkey,base,rel,j64bits(argjson,"price64")) == 0 )
{
7 years ago
//printf("call pricefeed update\n");
LP_pricefeedupdate(pubkey,base,rel,price,jstr(argjson,"utxocoin"),jint(argjson,"n"),jdouble(argjson,"bal")*SATOSHIDEN,jdouble(argjson,"min")*SATOSHIDEN,jdouble(argjson,"max")*SATOSHIDEN,jdouble(argjson,"credits")*SATOSHIDEN);
7 years ago
return(clonestr("{\"result\":\"success\"}"));
7 years ago
}
else
{
printf("sig failure\n");
return(clonestr("{\"error\":\"sig failure\"}"));
}
7 years ago
}
}
return(clonestr("{\"error\":\"missing fields in posted price\"}"));
}
7 years ago
int32_t _LP_pubkey_sigcheck(uint8_t *sig,int32_t siglen,uint32_t timestamp,bits256 pub,uint8_t *rmd160,uint8_t *pubsecp)
7 years ago
{
7 years ago
static void *ctx; uint8_t pub33[33]; bits256 sighash;
7 years ago
if ( ctx == 0 )
ctx = bitcoin_ctx();
7 years ago
sighash = LP_pubkey_sighash(timestamp,pub,rmd160,pubsecp);
return(bitcoin_recoververify(ctx,"pubkey",sig,sighash,pub33,0));
7 years ago
}
7 years ago
int32_t LP_pubkey_sigadd(cJSON *item,uint32_t timestamp,bits256 priv,bits256 pub,uint8_t *rmd160,uint8_t *pubsecp)
7 years ago
{
7 years ago
bits256 sighash;
sighash = LP_pubkey_sighash(timestamp,pub,rmd160,pubsecp);
return(LP_bitcoinsig_add(item,priv,pubsecp,sighash));
7 years ago
}
7 years ago
int32_t LP_pubkey_sigcheck(struct LP_pubkey_info *pubp,cJSON *item)
7 years ago
{
7 years ago
int32_t i,len,siglen,retval=-1; uint8_t rmd160[20],checkrmd160[20],pubsecp[33],sig[65],zeroes[20]; char *sigstr,*hexstr,*pubsecpstr;
7 years ago
if ( (hexstr= jstr(item,"rmd160")) != 0 && strlen(hexstr) == 2*sizeof(rmd160) )
7 years ago
{
7 years ago
decode_hex(rmd160,sizeof(rmd160),hexstr);
memset(zeroes,0,sizeof(zeroes));
7 years ago
if ( memcmp(zeroes,rmd160,sizeof(rmd160)) != 0 )
7 years ago
{
7 years ago
//if ( memcmp(rmd160,pubp->rmd160,20) != 0 )
7 years ago
{
7 years ago
if ( (pubsecpstr= jstr(item,"pubsecp")) != 0 && is_hexstr(pubsecpstr,0) == 66 )
7 years ago
{
7 years ago
decode_hex(pubsecp,sizeof(pubsecp),pubsecpstr);
7 years ago
calc_rmd160_sha256(checkrmd160,pubsecp,33);
7 years ago
if ( memcmp(checkrmd160,rmd160,20) == 0 )
7 years ago
{
7 years ago
if ( (sigstr= jstr(item,"sig")) != 0 && (len= is_hexstr(sigstr,0)) == 65*2 )
7 years ago
{
7 years ago
siglen = len >> 1;
decode_hex(sig,siglen,sigstr);
7 years ago
if ( _LP_pubkey_sigcheck(sig,siglen,juint(item,"timestamp"),pubp->pubkey,rmd160,pubsecp) == 0 )
7 years ago
{
7 years ago
if ( memcmp(rmd160,pubp->rmd160,20) != 0 )
{
7 years ago
//for (i=0; i<20; i++)
// printf("%02x",pubp->rmd160[i]);
7 years ago
memcpy(pubp->rmd160,rmd160,sizeof(pubp->rmd160));
memcpy(pubp->pubsecp,pubsecp,sizeof(pubp->pubsecp));
memcpy(pubp->sig,sig,sizeof(pubp->sig));
pubp->siglen = siglen;
7 years ago
//char str[65]; printf(" -> rmd160.(%s) for %s (%s) sig.%s\n",hexstr,bits256_str(str,pubp->pubkey),pubsecpstr,sigstr);
7 years ago
}
7 years ago
pubp->timestamp = juint(item,"timestamp");
7 years ago
retval = 0;
7 years ago
} else pubp->numerrors++;
7 years ago
}
7 years ago
}
7 years ago
else if ( 0 )
7 years ago
{
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
printf(" rmd160 vs ");
for (i=0; i<20; i++)
printf("%02x",checkrmd160[i]);
printf(" for %s\n",pubsecpstr);
}
7 years ago
}
7 years ago
}
7 years ago
}
7 years ago
}
7 years ago
return(retval);
}
void LP_notify_pubkeys(void *ctx,int32_t pubsock)
{
7 years ago
bits256 zero; uint32_t timestamp; char LPipaddr[64],secpstr[67]; cJSON *reqjson = cJSON_CreateObject();
7 years ago
memset(zero.bytes,0,sizeof(zero));
jaddstr(reqjson,"method","notify");
jaddstr(reqjson,"rmd160",G.LP_myrmd160str);
jaddbits256(reqjson,"pub",G.LP_mypub25519);
init_hexbytes_noT(secpstr,G.LP_pubsecp,33);
jaddstr(reqjson,"pubsecp",secpstr);
7 years ago
timestamp = (uint32_t)time(NULL);
jaddnum(reqjson,"timestamp",timestamp);
LP_pubkey_sigadd(reqjson,timestamp,G.LP_privkey,G.LP_mypub25519,G.LP_myrmd160,G.LP_pubsecp);
7 years ago
if ( IAMLP != 0 )
7 years ago
{
7 years ago
if ( LP_rarestpeer(LPipaddr) != 0 )
7 years ago
{
7 years ago
jaddstr(reqjson,"isLP",LPipaddr);
7 years ago
if ( strcmp(LPipaddr,LP_myipaddr) == 0 )
jaddnum(reqjson,"ismine",1);
//printf("notify send isLP.%s ismine.%d\n",LPipaddr,strcmp(LPipaddr,LP_myipaddr) == 0);
7 years ago
} else printf("no LPipaddr\n");
7 years ago
}
7 years ago
jaddnum(reqjson,"session",G.LP_sessionid);
7 years ago
LP_reserved_msg(1,"","",zero,jprint(reqjson,1));
7 years ago
}
char *LP_notify_recv(cJSON *argjson)
{
7 years ago
bits256 pub; struct LP_pubkey_info *pubp; char *ipaddr;
7 years ago
pub = jbits256(argjson,"pub");
if ( bits256_nonz(pub) != 0 )
{
if ( (pubp= LP_pubkeyadd(pub)) != 0 )
LP_pubkey_sigcheck(pubp,argjson);
7 years ago
if ( (ipaddr= jstr(argjson,"isLP")) != 0 )
7 years ago
{
//printf("notify got isLP %s %d\n",ipaddr,jint(argjson,"ismine"));
7 years ago
LP_peer_recv(ipaddr,jint(argjson,"ismine"),pubp);
7 years ago
if ( IAMLP != 0 && G.LP_IAMLP == 0 && strcmp(ipaddr,LP_myipaddr) == 0 )
7 years ago
{
if ( bits256_cmp(pub,G.LP_mypub25519) != 0 )
{
char str[65]; printf("that's me! and it is from %s which isnt me\n",bits256_str(str,pub));
G.LP_IAMLP = 1;
}
}
7 years ago
LP_addpeer(LP_mypeer,LP_mypubsock,ipaddr,RPC_port,RPC_port+10,RPC_port+20,1,juint(argjson,"session"),G.netid);
7 years ago
}
7 years ago
//char str[65]; printf("%.3f NOTIFIED pub %s rmd160 %s\n",OS_milliseconds()-millis,bits256_str(str,pub),rmd160str);
7 years ago
}
return(clonestr("{\"result\":\"success\",\"notify\":\"received\"}"));
}
7 years ago
void LP_smartutxos_push(struct iguana_info *coin)
{
7 years ago
uint64_t value; bits256 zero,txid; int32_t i,vout,height,n; cJSON *array,*item,*req;
7 years ago
return;
7 years ago
if ( coin->smartaddr[0] == 0 )
return;
7 years ago
//LP_notify_pubkeys(coin->ctx,LP_mypubsock);
7 years ago
if ( (array= LP_address_utxos(coin,coin->smartaddr,1)) != 0 )
{
memset(zero.bytes,0,sizeof(zero));
if ( (n= cJSON_GetArraySize(array)) > 0 )
{
//printf("PUSH %s %s\n",coin->symbol,coin->smartaddr);
for (i=0; i<n; i++)
{
item = jitem(array,i);
txid = jbits256(item,"tx_hash");
vout = jint(item,"tx_pos");
value = j64bits(item,"value");
height = jint(item,"height");
req = cJSON_CreateObject();
jaddstr(req,"method","uitem");
jaddstr(req,"coin",coin->symbol);
jaddstr(req,"coinaddr",coin->smartaddr);
jaddbits256(req,"txid",txid);
jaddnum(req,"vout",vout);
jaddnum(req,"ht",height);
jadd64bits(req,"value",value);
//printf("ADDR_UNSPENTS[] <- %s\n",jprint(req,0));
7 years ago
LP_reserved_msg(0,"","",zero,jprint(req,1));
7 years ago
}
}
free_json(array);
}
}
char *LP_uitem_recv(cJSON *argjson)
{
7 years ago
bits256 txid; int32_t vout,height; uint64_t value; struct iguana_info *coin; char *coinaddr,*symbol;
7 years ago
printf("LP_uitem_recv deprecated\n");
7 years ago
txid = jbits256(argjson,"txid");
vout = jint(argjson,"vout");
height = jint(argjson,"ht");
value = j64bits(argjson,"value");
coinaddr = jstr(argjson,"coinaddr");
7 years ago
if ( (symbol= jstr(argjson,"coin")) != 0 && coinaddr != 0 && (coin= LP_coinfind(symbol)) != 0 )
7 years ago
{
7 years ago
//char str[65]; printf("uitem %s %s %s/v%d %.8f ht.%d\n",symbol,coinaddr,bits256_str(str,txid),vout,dstr(value),height);
7 years ago
if ( strcmp(coin->smartaddr,coinaddr) != 0 )
7 years ago
LP_address_utxoadd(0,(uint32_t)time(NULL),"LP_uitem_recv",coin,coinaddr,txid,vout,value,height,-1);
7 years ago
//else printf("ignore external uitem %s %s\n",symbol,coin->smartaddr);
7 years ago
}
return(clonestr("{\"result\":\"success\"}"));
}
7 years ago
void LP_listunspent_query(char *symbol,char *coinaddr)
{
bits256 zero; cJSON *reqjson = cJSON_CreateObject();
memset(zero.bytes,0,sizeof(zero));
jaddstr(reqjson,"method","addr_unspents");
jaddstr(reqjson,"coin",symbol);
jaddstr(reqjson,"address",coinaddr);
7 years ago
LP_reserved_msg(0,"","",zero,jprint(reqjson,1));
7 years ago
}
void LP_query(void *ctx,char *myipaddr,int32_t mypubsock,char *method,struct LP_quoteinfo *qp)
{
7 years ago
cJSON *reqjson; bits256 zero; char *msg; struct iguana_info *coin; int32_t flag = 0;
7 years ago
if ( strcmp(method,"request") == 0 )
{
if ( LP_allocated(qp->desttxid,qp->destvout) == 0 && LP_allocated(qp->feetxid,qp->feevout) == 0 )
{
7 years ago
LP_unavailableset(qp->desttxid,qp->destvout,qp->timestamp+LP_AUTOTRADE_TIMEOUT*4,qp->srchash);
LP_unavailableset(qp->feetxid,qp->feevout,qp->timestamp+LP_AUTOTRADE_TIMEOUT*4,qp->srchash);
}
7 years ago
else
{
printf("couldnt find my txid to make request\n");
return;
}
}
reqjson = LP_quotejson(qp);
if ( bits256_nonz(qp->desthash) != 0 )
flag = 1;
jaddbits256(reqjson,"pubkey",qp->srchash);
jaddstr(reqjson,"method",method);
7 years ago
if ( jobj(reqjson,"timestamp") == 0 )
jaddnum(reqjson,"timestamp",time(NULL));
7 years ago
if ( strcmp(method,"connect") == 0 )
7 years ago
{
if ( (coin= LP_coinfind("KMD")) != 0 )
jadd(reqjson,"proof",LP_instantdex_txids(0,coin->smartaddr));
}
7 years ago
msg = jprint(reqjson,1);
7 years ago
//printf("etomicdest.(%s) QUERY.(%s)\n",qp->etomicdest,msg);
7 years ago
//if ( bits256_nonz(qp->srchash) == 0 || strcmp(method,"request") != 0 )
7 years ago
{
memset(&zero,0,sizeof(zero));
7 years ago
if ( bits256_nonz(qp->srchash) != 0 )
7 years ago
LP_reserved_msg(1,qp->srccoin,qp->destcoin,qp->srchash,clonestr(msg));
7 years ago
LP_reserved_msg(1,qp->srccoin,qp->destcoin,zero,clonestr(msg));
free(msg);
/*portable_mutex_lock(&LP_reservedmutex);
7 years ago
if ( num_Reserved_msgs[1] < sizeof(Reserved_msgs[1])/sizeof(*Reserved_msgs[1])-2 )
Reserved_msgs[1][num_Reserved_msgs[1]++] = msg;
if ( num_Reserved_msgs[0] < sizeof(Reserved_msgs[0])/sizeof(*Reserved_msgs[0])-2 )
Reserved_msgs[0][num_Reserved_msgs[0]++] = msg2;
7 years ago
portable_mutex_unlock(&LP_reservedmutex);*/
7 years ago
} //else LP_broadcast_message(LP_mypubsock,qp->srccoin,qp->destcoin,qp->srchash,msg);
7 years ago
}