]> Git Repo - VerusCoin.git/blob - src/komodo_gateway.h
test
[VerusCoin.git] / src / komodo_gateway.h
1 /******************************************************************************
2  * Copyright © 2014-2016 The SuperNET Developers.                             *
3  *                                                                            *
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.  *
7  *                                                                            *
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 *
11  *                                                                            *
12  * Removal or modification of this copyright notice is prohibited.            *
13  *                                                                            *
14  ******************************************************************************/
15
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)
19
20 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen)
21 {
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";
23     if ( opretlen == 72 )
24         return("notarized");
25     printf("komodo_opreturn[%c]: ht.%d %.8f opretlen.%d\n",opretbuf[0],height,dstr(value),opretlen);
26     if ( opretbuf[0] == 'D' )
27     {
28         if ( opretlen == 34 )
29         {
30             PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
31             if ( fiatoshis < 0 )
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
37             for (i=0; i<33; i++)
38                 printf("%02x",pubkey33[i]);
39             printf(" checkpubkey check %.8f v %.8f dest.(%s)\n",dstr(checktoshis),dstr(value),destaddr);
40             typestr = "deposit";
41         }
42     }
43     return(typestr);
44 }
45
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)
47 {
48     int32_t i,opretlen,offset = 0; uint256 zero; const char *typestr;
49     typestr = "unknown";
50     if ( script[offset++] == 0x6a )
51     {
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) )
56         {
57             typestr = "pricefeed";
58             komodo_paxpricefeed(height,&scriptbuf[len + scriptboffset] == 'P'],opretlen);
59             printf("height.%d pricefeed len.%d\n",height,opretlen);
60         }
61         else if ( isspecial != 0 )
62         {
63             komodo_stateupdate(0,0,0,0,zero,0,0,0,0,0,value,&script[offset],opretlen);
64             for (i=0; i<len; i++)
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);
67         }
68     }
69     else if ( numvouts > 13 )
70         typestr = "ratify";
71 }
72
73 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
74 {
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 )
78     {
79         if ( (json= cJSON_Parse(retstr)) != 0 )
80         {
81             if ( (result= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
82             {
83                 retval = 0;
84                 isspecial = 0;
85                 for (vout=0; vout<n; vout++)
86                 {
87                     item = jitem(vouts,vout);
88                     value = SATOSHIDEN * jdouble(item,(char *)"value");
89                     if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
90                     {
91                         if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
92                         {
93                             len = (int32_t)strlen(hexstr) >> 1;
94                             if ( len != 74 )
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)) )
97                                 isspecial = 1;
98                             else if ( len <= sizeof(script) )
99                             {
100                                 decode_hex(script,len,hexstr);
101                                 komodo_gateway_voutupdate(symbol,isspecial,height,txi,vout,n,value,script,len);
102                             }
103                         }
104                     }
105                 }
106             }
107             free_json(json);
108         }
109         free(retstr);
110     }
111     return(retval);
112 }
113
114 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
115 {
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 )
119     {
120         if ( (result= cJSON_Parse(retstr)) != 0 )
121         {
122             if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
123             {
124                 sprintf(params,"[\"%s\"]",txidstr);
125                 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
126                 {
127                     //printf("getblock.(%s)\n",retstr2);
128                     if ( (json= cJSON_Parse(retstr2)) != 0 )
129                     {
130                         if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
131                         {
132                             for (i=0; i<n; i++)
133                                 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
134                                     break;
135                             if ( i == n )
136                                 retval = 0;
137                             else printf("error i.%d vs n.%d\n",i,n);
138                         }
139                         free_json(json);
140                     }
141                     free(retstr2);
142                 }
143             } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
144             free_json(result);
145         }
146         free(retstr);
147     }
148     return(retval);
149 }
150
151 void komodo_gateway_iteration(char *symbol)
152 {
153     char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
154     if ( KMDHEIGHT <= 0 )
155         KMDHEIGHT = 1;
156     if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
157     {
158         if ( (infoobj= cJSON_Parse(retstr)) != 0 )
159         {
160             if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
161             {
162                 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
163                 {
164                     if ( (KMDHEIGHT % 100) == 0 )
165                     {
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);
169                     }
170                     if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
171                         break;
172                     usleep(10000);
173                 }
174             }
175             free_json(infoobj);
176         }
177         free(retstr);
178     }
179     else
180     {
181         //printf("error from %s\n",symbol);
182         sleep(30);
183     }
184 }
185
186 #ifdef KOMODO_ISSUER
187 void komodo_gateway_issuer() // from "assetchain" connectblock()
188 {
189     // check for redeems
190 }
191 #else
192
193 void komodo_gateway_redeemer() // from "KMD" connectblock()
194 {
195     
196 }
197 #endif
This page took 0.035162 seconds and 4 git commands to generate.