/****************************************************************************** * Copyright © 2014-2018 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. * * * ******************************************************************************/ #include #include #include #include #include "cJSON.c" char hexbyte(int32_t c) { c &= 0xf; if ( c < 10 ) return('0'+c); else if ( c < 16 ) return('a'+c-10); else return(0); } int32_t _unhex(char c) { if ( c >= '0' && c <= '9' ) return(c - '0'); else if ( c >= 'a' && c <= 'f' ) return(c - 'a' + 10); else if ( c >= 'A' && c <= 'F' ) return(c - 'A' + 10); return(-1); } int32_t is_hexstr(char *str,int32_t n) { int32_t i; if ( str == 0 || str[0] == 0 ) return(0); for (i=0; str[i]!=0; i++) { if ( n > 0 && i >= n ) break; if ( _unhex(str[i]) < 0 ) break; } if ( n == 0 ) return(i); return(i == n); } int32_t unhex(char c) { int32_t hex; if ( (hex= _unhex(c)) < 0 ) { //printf("unhex: illegal hexchar.(%c)\n",c); } return(hex); } unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); } int32_t decode_hex(unsigned char *bytes,int32_t n,char *hex) { int32_t adjust,i = 0; //printf("decode.(%s)\n",hex); if ( is_hexstr(hex,n) <= 0 ) { memset(bytes,0,n); return(n); } if ( hex[n-1] == '\n' || hex[n-1] == '\r' ) hex[--n] = 0; if ( hex[n-1] == '\n' || hex[n-1] == '\r' ) hex[--n] = 0; if ( n == 0 || (hex[n*2+1] == 0 && hex[n*2] != 0) ) { if ( n > 0 ) { bytes[0] = unhex(hex[0]); printf("decode_hex n.%d hex[0] (%c) -> %d hex.(%s) [n*2+1: %d] [n*2: %d %c] len.%ld\n",n,hex[0],bytes[0],hex,hex[n*2+1],hex[n*2],hex[n*2],(long)strlen(hex)); } bytes++; hex++; adjust = 1; } else adjust = 0; if ( n > 0 ) { for (i=0; i>4) & 0xf); hexbytes[i*2 + 1] = hexbyte(message[i] & 0xf); //printf("i.%d (%02x) [%c%c]\n",i,message[i],hexbytes[i*2],hexbytes[i*2+1]); } hexbytes[len*2] = 0; //printf("len.%ld\n",len*2+1); return((int32_t)len*2+1); } long _stripwhite(char *buf,int accept) { int32_t i,j,c; if ( buf == 0 || buf[0] == 0 ) return(0); for (i=j=0; buf[i]!=0; i++) { buf[j] = c = buf[i]; if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') ) j++; } buf[j] = 0; return(j); } char *clonestr(char *str) { char *clone; if ( str == 0 || str[0] == 0 ) { printf("warning cloning nullstr.%p\n",str); //#ifdef __APPLE__ // while ( 1 ) sleep(1); //#endif str = (char *)""; } clone = (char *)malloc(strlen(str)+16); strcpy(clone,str); return(clone); } int32_t safecopy(char *dest,char *src,long len) { int32_t i = -1; if ( src != 0 && dest != 0 && src != dest ) { if ( dest != 0 ) memset(dest,0,len); for (i=0; i0; i--) str[i] = str[i-1]; str[0] = '/'; str[n+1] = 0; }*/ #endif return(str); #endif } void *loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep) { FILE *fp; long filesize,buflen = *allocsizep; uint8_t *buf = *bufp; *lenp = 0; if ( (fp= fopen(portable_path(fname),"rb")) != 0 ) { fseek(fp,0,SEEK_END); filesize = ftell(fp); if ( filesize == 0 ) { fclose(fp); *lenp = 0; printf("loadfile null size.(%s)\n",fname); return(0); } if ( filesize > buflen ) { *allocsizep = filesize; *bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64); } rewind(fp); if ( buf == 0 ) printf("Null buf ???\n"); else { if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize ) printf("error reading filesize.%ld\n",(long)filesize); buf[filesize] = 0; } fclose(fp); *lenp = filesize; //printf("loaded.(%s)\n",buf); } //else printf("OS_loadfile couldnt load.(%s)\n",fname); return(buf); } void *filestr(long *allocsizep,char *_fname) { long filesize = 0; char *fname,*buf = 0; void *retptr; *allocsizep = 0; fname = malloc(strlen(_fname)+1); strcpy(fname,_fname); retptr = loadfile(fname,(uint8_t **)&buf,&filesize,allocsizep); free(fname); return(retptr); } char *send_curl(char *url,char *fname) { long fsize; char curlstr[1024]; sprintf(curlstr,"curl --url \"%s\" > %s",url,fname); system(curlstr); return(filestr(&fsize,fname)); } cJSON *get_urljson(char *url,char *fname) { char *jsonstr; cJSON *json = 0; if ( (jsonstr= send_curl(url,fname)) != 0 ) { //printf("(%s) -> (%s)\n",url,jsonstr); json = cJSON_Parse(jsonstr); free(jsonstr); } return(json); } ////////////////////////////////////////////// // start of dapp ////////////////////////////////////////////// uint64_t get_btcusd() { cJSON *pjson,*bpi,*usd; uint64_t btcusd = 0; if ( (pjson= get_urljson("http://api.coindesk.com/v1/bpi/currentprice.json","/tmp/oraclefeed.json")) != 0 ) { if ( (bpi= jobj(pjson,"bpi")) != 0 && (usd= jobj(bpi,"USD")) != 0 ) { btcusd = jdouble(usd,"rate_float") * SATOSHIDEN; printf("BTC/USD %.4f\n",dstr(btcusd)); } free_json(pjson); } return(btcusd); } cJSON *get_komodocli(char **retstrp,char *acname,char *method,char *arg0,char *arg1,char *arg2) { long fsize; cJSON *retjson = 0; char cmdstr[32768],*jsonstr,*fname = "/tmp/komodocli"; if ( acname[0] != 0 ) sprintf(cmdstr,"./komodo-cli -ac_name=%s %s %s %s %s > %s\n",acname,method,arg0,arg1,arg2,fname); else sprintf(cmdstr,"./komodo-cli %s %s %s %s > %s\n",method,arg0,arg1,arg2,fname); system(cmdstr); *retstrp = 0; if ( (jsonstr= filestr(&fsize,fname)) != 0 ) { fprintf(stderr,"%s -> jsonstr.(%s)\n",cmdstr,jsonstr); if ( (jsonstr[0] == '{' || jsonstr[0] == '[') && (retjson= cJSON_Parse(jsonstr)) == 0 ) *retstrp = jsonstr; else free(jsonstr); } return(retjson); } void komodobroadcast(char *acname,cJSON *hexjson) { char *hexstr,*retstr; cJSON *retjson; if ( (hexstr= jstr(hexjson,"hex")) != 0 ) { if ( (retjson= get_komodocli(&retstr,acname,"sendrawtransaction",hexstr,"","")) != 0 ) { //fprintf(stderr,"broadcast.(%s)\n",jprint(retjson,0)); free_json(retjson); } else if ( retstr != 0 ) { fprintf(stderr,"txid.(%s)\n",retstr); free(retstr); } } } int32_t get_KMDheight() { cJSON *retjson; char *retstr; int32_t height=0; if ( (retjson= get_komodocli(&retstr,"","getinfo","","","")) != 0 ) { height = jint(retjson,"blocks"); fprintf(stderr,"KMDheight.%d\n",height); free_json(retjson); } else if ( retstr != 0 ) { fprintf(stderr,"get_KMDheight error.(%s)\n",retstr); free(retstr); } return(height); } bits256 get_KMDblockhash(int32_t height) { cJSON *retjson; char *retstr,heightstr[32]; bits256 hash; memset(hash.bytes,0,sizeof(hash)); sprintf(heightstr,"%d",height); if ( (retjson= get_komodocli(&retstr,"","getblockhash",heightstr,"","")) != 0 ) { fprintf(stderr,"unexpected blockhash json.(%s)\n",jprint(retjson,0)); free_json(retjson); } else if ( retstr != 0 ) { fprintf(stderr,"get_KMDblockhash.(%s)\n",retstr); if ( strlen(retstr) == 64 ) decode_hex(hash.bytes,32,retstr); free(retstr); } return(hash); } bits256 get_KMDmerkleroot(bits256 blockhash) { cJSON *retjson; char *retstr,str[65]; bits256 merkleroot; memset(merkleroot.bytes,0,sizeof(merkleroot)); if ( (retjson= get_komodocli(&retstr,"","getblockheader",bits256_str(str,blockhash),"","")) != 0 ) { merkleroot = jbits256(retjson,"merkleroot"); fprintf(stderr,"got merkleroot.(%s)\n",bits256_str(str,merkleroot)); free_json(retjson); } else if ( retstr != 0 ) { fprintf(stderr,"get_KMDmerkleroot error.(%s)\n",retstr); free(retstr); } return(merkleroot); } int32_t get_KMDheader(bits256 *blockhashp,bits256 *merklerootp,int32_t prevheight) { int32_t height = 0; char str[65]; if ( prevheight == 0 ) height = get_KMDheight(); else height = prevheight + 1; if ( height > 0 ) { *blockhashp = get_KMDblockhash(height); *merklerootp = get_KMDmerkleroot(*blockhashp); return(height); } return(0); } int32_t get_oracledata(int32_t prevheight,char *hexstr,int32_t maxsize,char *format) { uint32_t i,height; uint64_t price; bits256 blockhash,merkleroot; hexstr[0] = 0; if ( format[0] == 'L' || format[0] == 'l' ) { if ( (price= get_btcusd()) != 0 ) { for (i=0; i<8; i++) sprintf(&hexstr[i*2],"%02x",(uint8_t)((price >> (i*8)) & 0xff)); hexstr[16] = 0; return(16); } } else if ( strcmp(format,"Ihh") == 0 ) { if ( (height= get_KMDheader(&blockhash,&merkleroot,prevheight)) > prevheight ) { for (i=0; i<4; i++) sprintf(&hexstr[i*2],"%02x",(uint8_t)((height >> (i*8)) & 0xff)); for (i=0; i<32; i++) sprintf(&hexstr[8 + i*2],"%02x",blockhash.bytes[i]); for (i=0; i<32; i++) sprintf(&hexstr[8 + 64 + i*2],"%02x",merkleroot.bytes[i]); hexstr[8 + 64*2] = 0; return(height); } } return(0); } /* oraclescreate "BTCUSD" "coindeskpricedata" "L" -> 4895f631316a649e216153aee7a574bd281686265dc4e8d37597f72353facac3 oraclesregister 4895f631316a649e216153aee7a574bd281686265dc4e8d37597f72353facac3 1000000 -> 11c54d4ab17293217276396e27d86f714576ff55a3300dac34417047825edf93 oraclessubscribe 4895f631316a649e216153aee7a574bd281686265dc4e8d37597f72353facac3 02ebc786cb83de8dc3922ab83c21f3f8a2f3216940c3bf9da43ce39e2a3a882c92 1.5 -> ce4e4afa53765b11a74543dacbd3174a93f33f12bb94cdc080c2c023726b5838 oraclesdata 4895f631316a649e216153aee7a574bd281686265dc4e8d37597f72353facac3 000000ff00000000 -> e8a8c897e97389dcac31d81b617ab73a829110bd5c6f99f9f533b9c0e22700d0 oraclessamples 4895f631316a649e216153aee7a574bd281686265dc4e8d37597f72353facac3 90ff8813a93b5b2615ec43974ff4fc91e4373dfd672d995676c43ff2dcda1010 10 -> { "result": "success", "samples": [ [ "4278190080" ] ] } oraclescreate test testsformat s -> 17a841a919c284cea8a676f34e793da002e606f19a9258a3190bed12d5aaa3ff oraclesregister -> 7825ad75ba854ab12868f7d2e06b4061903687fe93f41a2a99202a6b9ca3c029 oraclessubscribe 17a841a919c284cea8a676f34e793da002e606f19a9258a3190bed12d5aaa3ff 02ebc786cb83de8dc3922ab83c21f3f8a2f3216940c3bf9da43ce39e2a3a882c92 1.5 -> faf8a6676f6389abad9e7f397015d200395c9f8a24c4ded291d83e6265b2f4d1 oraclesdata 17a841a919c284cea8a676f34e793da002e606f19a9258a3190bed12d5aaa3ff 03404040 -> e8a8c897e97389dcac31d81b617ab73a829110bd5c6f99f9f533b9c0e22700d0 */ //#define ORACLETXID "4895f631316a649e216153aee7a574bd281686265dc4e8d37597f72353facac3" //#define MYPUBKEY "02ebc786cb83de8dc3922ab83c21f3f8a2f3216940c3bf9da43ce39e2a3a882c92" //#define ACNAME "ORCL" int32_t main(int32_t argc,char **argv) { cJSON *clijson,*clijson2,*regjson,*item; int32_t i,n,height,prevheight = 0; char *format,*acname,*oraclestr,*pkstr,*pubstr,*retstr,*retstr2,hexstr[4096]; uint64_t price; if ( argc != 5 ) { printf("usage: oraclefeed $ACNAME $ORACLETXID $MYPUBKEY $FORMAT\nPowered by CoinDesk (%s) %.8f\n","https://www.coindesk.com/price/",dstr(get_btcusd())); return(-1); } acname = argv[1]; oraclestr = argv[2]; pkstr = argv[3]; format = argv[4]; if ( strncmp(format,"Ihh",3) != 0 && format[0] != 'L' ) { printf("only formats of L and Ihh are supported now\n"); return(-1); } while ( 1 ) { retstr = 0; if ( (clijson= get_komodocli(&retstr,acname,"oraclesinfo",oraclestr,"","")) != 0 ) { if ( (regjson= jarray(&n,clijson,"registered")) != 0 ) { for (i=0; i