1 /******************************************************************************
2 * Copyright © 2014-2016 The SuperNET Developers. *
4 * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
5 * the top-level directory of this distribution for the individual copyright *
6 * holder information and the developer policies on copyright and licensing. *
8 * Unless otherwise agreed in a custom licensing agreement, no part of the *
9 * SuperNET software, including this file may be copied, modified, propagated *
10 * or distributed except according to the terms contained in the LICENSE file *
12 * Removal or modification of this copyright notice is prohibited. *
14 ******************************************************************************/
16 // convert paxdeposit into new coins in the next block
17 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
18 // need to save most processed block in other chain(s)
20 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen)
22 uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i; char base[4],coinaddr[64],destaddr[64]; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
25 printf("komodo_opreturn[%c]: ht.%d %.8f opretlen.%d\n",opretbuf[0],height,dstr(value),opretlen);
26 if ( opretbuf[0] == 'D' )
30 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
32 fiatoshis = -fiatoshis;
33 bitcoin_address(coinaddr,addrtype,rmd160,20);
34 checktoshis = PAX_fiatdest(destaddr,pubkey33,coinaddr,height,base,fiatoshis);
35 printf("DEPOSIT %.8f %c%s -> %s\n",dstr(fiatoshis),shortflag!=0?'-':'+',base,coinaddr);
36 // verify price value for fiatoshis of base
38 printf("%02x",pubkey33[i]);
39 printf(" checkpubkey check %.8f v %.8f dest.(%s)\n",dstr(checktoshis),dstr(value),destaddr);
46 void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len)
48 int32_t i,opretlen,offset = 0; uint256 zero; const char *typestr;
50 if ( script[offset++] == 0x6a )
52 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
53 if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],"KMD") == 0 )
54 typestr = "notarized";
55 else if ( txi == 0 && (script[offset] == 'P' || opretlen == 149) )
57 typestr = "pricefeed";
58 komodo_paxpricefeed(height,&scriptbuf[len + scriptboffset] == 'P'],opretlen);
59 printf("height.%d pricefeed len.%d\n",height,opretlen);
61 else if ( isspecial != 0 )
63 komodo_stateupdate(0,0,0,0,zero,0,0,0,0,0,value,&script[offset],opretlen);
65 printf("%02x",script[i]);
66 printf(" <- %s VOUTUPDATE.%d txi.%d vout.%d %.8f scriptlen.%d OP_RETURN.%d (%s)\n",symbol,height,txi,vout,dstr(value),len,script[0] == 0x6a,typestr);
69 else if ( numvouts > 13 )
73 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
75 char *retstr,params[256],*hexstr; uint8_t script[10000]; cJSON *json,*result,*vouts,*item,*sobj; int32_t vout,n,len,isspecial,retval = -1; uint64_t value;
76 sprintf(params,"[\"%s\", 1]",txidstr);
77 if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
79 if ( (json= cJSON_Parse(retstr)) != 0 )
81 if ( (result= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
85 for (vout=0; vout<n; vout++)
87 item = jitem(vouts,vout);
88 value = SATOSHIDEN * jdouble(item,(char *)"value");
89 if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
91 if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
93 len = (int32_t)strlen(hexstr) >> 1;
95 printf("ht.%d txi.%d vout.%d/%d %s script (%d %d)\n",height,txi,vout,n,hexstr,memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66),memcmp(&hexstr[6],CRYPTO777_RMD160STR,40));
96 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
98 else if ( len <= sizeof(script) )
100 decode_hex(script,len,hexstr);
101 komodo_gateway_voutupdate(symbol,isspecial,height,txi,vout,n,value,script,len);
114 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
116 char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx,*result,*result2;
117 sprintf(params,"[%d]",height);
118 if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
120 if ( (result= cJSON_Parse(retstr)) != 0 )
122 if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
124 sprintf(params,"[\"%s\"]",txidstr);
125 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
127 //printf("getblock.(%s)\n",retstr2);
128 if ( (json= cJSON_Parse(retstr2)) != 0 )
130 if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
133 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
137 else printf("error i.%d vs n.%d\n",i,n);
143 } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
151 void komodo_gateway_iteration(char *symbol)
153 char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
154 if ( KMDHEIGHT <= 0 )
156 if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
158 if ( (infoobj= cJSON_Parse(retstr)) != 0 )
160 if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
162 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
164 if ( (KMDHEIGHT % 100) == 0 )
166 fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
167 memset(&zero,0,sizeof(zero));
168 komodo_stateupdate(0,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0);
170 if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
181 //printf("error from %s\n",symbol);
187 void komodo_gateway_issuer() // from "assetchain" connectblock()
193 void komodo_gateway_redeemer() // from "KMD" connectblock()