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.

295 lines
12 KiB

8 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. *
* *
******************************************************************************/
// included from basilisk.c
8 years ago
// deposit address <coin> -> corresponding KMD address, if KMD deposit starts JUMBLR
// jumblr address <coin> is the destination of JUMBLR and JUMBLR BTC (would need tracking to map back to non-BTC)
// <symbol> address <coin> is DEX'ed for <SYMBOL>
// return value convention: -1 error, 0 partial match, >= 1 exact match
int32_t smartaddress_type(char *typestr)
{
char upper[64];
if ( strcmp(typestr,"deposit") != 0 && strcmp(typestr,"jumblr") != 0 )
{
upper[sizeof(upper)-1] = 0;
strncpy(upper,typestr,sizeof(upper)-1);
touppercase(upper);
if ( iguana_coinfind(upper) != 0 )
return(0);
}
return(-1);
}
bits256 jumblr_privkey(struct supernet_info *myinfo,char *coinaddr,uint8_t pubtype,char *KMDaddr,char *prefix)
{
bits256 privkey,pubkey; uint8_t pubkey33[33]; char passphrase[sizeof(myinfo->jumblr_passphrase) + 64];
sprintf(passphrase,"%s%s",prefix,myinfo->jumblr_passphrase);
if ( myinfo->jumblr_passphrase[0] == 0 )
strcpy(myinfo->jumblr_passphrase,"password");
conv_NXTpassword(privkey.bytes,pubkey.bytes,(uint8_t *)passphrase,(int32_t)strlen(passphrase));
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey);
bitcoin_address(coinaddr,pubtype,pubkey33,33);
bitcoin_address(KMDaddr,60,pubkey33,33);
//printf("(%s) -> (%s %s)\n",passphrase,coinaddr,KMDaddr);
return(privkey);
}
cJSON *smartaddress_json(struct smartaddress *ap)
{
8 years ago
char coinaddr[64],*symbol; int32_t j,n; struct iguana_info *coin; cJSON *array,*ritem,*item,*retjson = cJSON_CreateObject();
8 years ago
bitcoin_address(coinaddr,60,ap->pubkey33,33);
8 years ago
jaddstr(retjson,"KMD",coinaddr);
8 years ago
bitcoin_address(coinaddr,0,ap->pubkey33,33);
8 years ago
jaddstr(retjson,"BTC",coinaddr);
8 years ago
if ( ap->typejson != 0 )
{
8 years ago
printf("smartjson.(%s)\n",jprint(ap->typejson,0));
8 years ago
//jadd(item,"type",ap->typejson);
array = cJSON_CreateArray();
8 years ago
if ( is_cJSON_Array(ap->typejson) != 0 && (n= cJSON_GetArraySize(ap->typejson)) > 0 )
8 years ago
{
8 years ago
jadd(retjson,"type",jitem(ap->typejson,0));
8 years ago
for (j=1; j<n; j++)
{
8 years ago
item = jitem(ap->typejson,j);
if ( (symbol= jstr(item,"s")) != 0 )
8 years ago
{
8 years ago
if ( (coin= iguana_coinfind(symbol)) != 0 )
8 years ago
{
8 years ago
bitcoin_address(coinaddr,coin->chain->pubtype,ap->pubkey33,33);
ritem = cJSON_CreateObject();
jaddstr(ritem,"coin",symbol);
jaddstr(ritem,"address",coinaddr);
jaddnum(ritem,"maxbid",jdouble(item,"b"));
jaddnum(ritem,"minask",jdouble(item,"a"));
jaddi(array,ritem);
8 years ago
}
}
}
}
8 years ago
jadd(retjson,"coins",array);
8 years ago
}
8 years ago
return(retjson);
8 years ago
}
8 years ago
int32_t _smartaddress_add(struct supernet_info *myinfo,bits256 privkey,char *symbol,double maxbid,double minask)
8 years ago
{
8 years ago
char coinaddr[64],*jsym,tmp[64]; uint8_t addrtype,rmd160[20]; cJSON *item,*nitem; struct smartaddress *ap; int32_t i,j,n;
8 years ago
if ( myinfo->numsmartaddrs < sizeof(myinfo->smartaddrs)/sizeof(*myinfo->smartaddrs) )
{
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( bits256_cmp(myinfo->smartaddrs[i].privkey,privkey) == 0 )
8 years ago
{
ap = &myinfo->smartaddrs[i];
if ( ap->typejson == 0 )
return(-1);
else
{
n = cJSON_GetArraySize(ap->typejson);
for (j=0; j<n; j++)
{
8 years ago
item = jitem(ap->typejson,j);
jsym = jstr(item,"s");
if ( jsym != 0 && strcmp(jsym,symbol) == 0 )
{
8 years ago
nitem = cJSON_CreateObject();
jaddstr(nitem,"s",symbol);
jaddnum(nitem,"b",maxbid);
jaddnum(nitem,"a",minask);
cJSON_ReplaceItemInArray(ap->typejson,j,nitem);
8 years ago
printf("updated.(%s)\n",jprint(ap->typejson,0));
8 years ago
return(0);
8 years ago
}
8 years ago
}
}
8 years ago
item = cJSON_CreateObject();
strcpy(tmp,symbol), touppercase(tmp), jaddstr(item,"s",tmp);
if ( maxbid != 0. )
jaddnum(item,"b",maxbid);
if ( minask != 0. )
jaddnum(item,"a",minask);
jaddi(ap->typejson,item);
8 years ago
return(i+1);
}
ap = &myinfo->smartaddrs[myinfo->numsmartaddrs];
ap->typejson = cJSON_CreateArray();
if ( smartaddress_type(symbol) < 0 )
return(-1);
8 years ago
item = cJSON_CreateObject(), jaddstr(item,"type",symbol), jaddi(ap->typejson,item);
item = cJSON_CreateObject(), jaddstr(item,"s","KMD"), jaddi(ap->typejson,item);
item = cJSON_CreateObject(), jaddstr(item,"s","BTC"), jaddi(ap->typejson,item);
8 years ago
printf("created.(%s)\n",jprint(ap->typejson,0));
8 years ago
ap->privkey = privkey;
bitcoin_pubkey33(myinfo->ctx,ap->pubkey33,privkey);
8 years ago
calc_rmd160_sha256(ap->rmd160,ap->pubkey33,33);
8 years ago
ap->pubkey = curve25519(privkey,curve25519_basepoint9());
8 years ago
bitcoin_address(coinaddr,0,ap->pubkey33,33);
8 years ago
for (i=0; i<20; i++)
printf("%02x",ap->rmd160[i]);
8 years ago
bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
printf(", ");
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
8 years ago
printf (" <- rmd160 for %d %s\n",myinfo->numsmartaddrs,coinaddr);
return(++myinfo->numsmartaddrs + 1);
8 years ago
}
8 years ago
printf("too many smartaddresses %d vs %d\n",myinfo->numsmartaddrs,(int32_t)(sizeof(myinfo->smartaddrs)/sizeof(*myinfo->smartaddrs)));
8 years ago
return(-1);
}
8 years ago
int32_t smartaddress_add(struct supernet_info *myinfo,bits256 privkey,char *symbol,double maxbid,double minask)
{
int32_t retval;
portable_mutex_lock(&myinfo->smart_mutex);
retval = _smartaddress_add(myinfo,privkey,symbol,maxbid,minask);
portable_mutex_unlock(&myinfo->smart_mutex);
return(retval);
}
int32_t smartaddress_symbolmatch(char *typestr,double *bidaskp,struct smartaddress *ap,char *symbol)
8 years ago
{
8 years ago
int32_t j,n; char *str; cJSON *item;
8 years ago
if ( ap->typejson != 0 && (n= cJSON_GetArraySize(ap->typejson)) > 0 )
{
8 years ago
item = jitem(ap->typejson,0);
if ( (str= jstr(item,"type")) != 0 )
strncpy(typestr,str,63);
else typestr[0] = 0;
for (j=1; j<n; j++)
8 years ago
{
8 years ago
item = jitem(ap->typejson,j);
str = jstr(item,"s");
if ( str != 0 && strcmp(str,symbol) == 0 )
{
bidaskp[0] = jdouble(item,"b");
bidaskp[1] = jdouble(item,"a");
8 years ago
return(j);
8 years ago
}
8 years ago
}
}
return(-1);
}
8 years ago
int32_t smartaddress(struct supernet_info *myinfo,char *typestr,double *bidaskp,bits256 *privkeyp,char *symbol,char *coinaddr)
8 years ago
{
8 years ago
int32_t i,j,retval = -1; uint8_t addrtype,rmd160[20]; struct smartaddress *ap;
8 years ago
memset(privkeyp,0,sizeof(*privkeyp));
8 years ago
memset(bidaskp,0,sizeof(*bidaskp) * 2);
8 years ago
bitcoin_addr2rmd160(&addrtype,rmd160,coinaddr);
8 years ago
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( memcmp(myinfo->smartaddrs[i].rmd160,rmd160,20) == 0 )
{
8 years ago
ap = &myinfo->smartaddrs[i];
*privkeyp = ap->privkey;
8 years ago
if ( (j= smartaddress_symbolmatch(typestr,bidaskp,ap,symbol)) >= 0 )
retval = 0;
else retval = (i+1);
break;
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<20; i++)
printf("%02x",rmd160[i]);
printf(" <- rmd160 smartaddress cant find (%s) of %d\n",coinaddr,myinfo->numsmartaddrs);
8 years ago
return(retval);
8 years ago
}
8 years ago
int32_t smartaddress_pubkey(struct supernet_info *myinfo,char *typestr,double *bidaskp,bits256 *privkeyp,char *symbol,bits256 pubkey)
8 years ago
{
8 years ago
int32_t i,j,retval = -1; struct smartaddress *ap;
8 years ago
memset(privkeyp,0,sizeof(*privkeyp));
8 years ago
memset(bidaskp,0,sizeof(*bidaskp) * 2);
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( bits256_cmp(myinfo->smartaddrs[i].pubkey,pubkey) == 0 )
{
8 years ago
ap = &myinfo->smartaddrs[i];
8 years ago
if ( (j= smartaddress_symbolmatch(typestr,bidaskp,ap,symbol)) >= 0 )
retval = 0;
else retval = (i+1);
break;
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
return(retval);
8 years ago
}
8 years ago
int32_t smartaddress_pubkey33(struct supernet_info *myinfo,char *typestr,double *bidaskp,bits256 *privkeyp,char *symbol,uint8_t *pubkey33)
8 years ago
{
8 years ago
int32_t i,j,retval = -1; struct smartaddress *ap;
8 years ago
memset(privkeyp,0,sizeof(*privkeyp));
8 years ago
memset(bidaskp,0,sizeof(*bidaskp) * 2);
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
if ( memcmp(myinfo->smartaddrs[i].pubkey33,pubkey33,33) == 0 )
{
8 years ago
ap = &myinfo->smartaddrs[i];
*privkeyp = ap->privkey;
8 years ago
if ( (j= smartaddress_symbolmatch(typestr,bidaskp,ap,symbol)) >= 0 )
retval = 0;
else retval = (i+1);
break;
8 years ago
}
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
return(retval);
8 years ago
}
#include "../includes/iguana_apidefs.h"
#include "../includes/iguana_apideclares.h"
#include "../includes/iguana_apideclares2.h"
ZERO_ARGS(InstantDEX,smartaddresses)
{
int32_t i; cJSON *retjson = cJSON_CreateArray();
8 years ago
portable_mutex_lock(&myinfo->smart_mutex);
8 years ago
for (i=0; i<myinfo->numsmartaddrs; i++)
jaddi(retjson,smartaddress_json(&myinfo->smartaddrs[i]));
8 years ago
portable_mutex_unlock(&myinfo->smart_mutex);
8 years ago
return(jprint(retjson,1));
}
8 years ago
TWO_STRINGS_AND_TWO_DOUBLES(InstantDEX,smartaddress,type,symbol,maxbid,minask)
8 years ago
{
8 years ago
char prefix[64],coinaddr[64],KMDaddr[64],typestr[64]; double bidask[2]; uint8_t pubkey33[33]; bits256 privkey;
8 years ago
if ( smartaddress_type(type) < 0 )
return(clonestr("{\"error\":\"non-supported smartaddress type\"}"));
if ( iguana_coinfind(symbol) == 0 )
return(clonestr("{\"error\":\"non-supported smartaddress symbol\"}"));
if ( strcmp(type,"deposit") == 0 || strcmp(type,"jumblr") == 0 )
{
8 years ago
if ( smartaddress_pubkey(myinfo,typestr,bidask,&privkey,symbol,strcmp(type,"deposit") == 0 ? myinfo->jumblr_depositkey : myinfo->jumblr_pubkey) < 0 )
8 years ago
return(clonestr("{\"error\":\"unexpected missing smartaddress deposit/jumblr\"}"));
}
else
{
strcpy(prefix,type);
tolowercase(prefix);
if ( strcmp(prefix,"btc") == 0 || strcmp(prefix,"kmd") == 0 )
return(clonestr("{\"success\":\"no need add BTC or KMD to smartaddress\"}"));
strcat(prefix," ");
privkey = jumblr_privkey(myinfo,coinaddr,0,KMDaddr,prefix);
}
if ( (coin= iguana_coinfind(symbol)) == 0 )
return(clonestr("{\"error\":\"non-supported smartaddress symbol\"}"));
bitcoin_pubkey33(myinfo->ctx,pubkey33,privkey);
bitcoin_address(coinaddr,coin->chain->pubtype,pubkey33,33);
8 years ago
smartaddress_add(myinfo,privkey,symbol,maxbid,minask);
8 years ago
return(InstantDEX_smartaddresses(myinfo,0,0,0));
8 years ago
}
8 years ago
#include "../includes/iguana_apiundefs.h"