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 // paxdeposit equivalent in reverse makes opreturn and KMD does the same in reverse
18 struct pax_transaction
22 uint64_t komodoshis,fiatoshis;
23 int32_t marked,height;
25 char symbol[16],coinaddr[64]; uint8_t rmd160[20],shortflag;
28 void komodo_gateway_deposit(char *coinaddr,uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout,int32_t height) // assetchain context
30 struct pax_transaction *pax;
31 pax = (struct pax_transaction *)calloc(1,sizeof(*pax));
34 strcpy(pax->coinaddr,coinaddr);
35 pax->komodoshis = value;
36 pax->shortflag = shortflag;
37 strcpy(pax->symbol,symbol);
38 pax->fiatoshis = fiatoshis;
39 memcpy(pax->rmd160,rmd160,20);
41 printf("ADD DEPOSIT %s %.8f -> %s TO PAX ht.%d\n",symbol,dstr(fiatoshis),coinaddr,height);
46 printf("MARK DEPOSIT ht.%d\n",height);
50 pthread_mutex_lock(&komodo_mutex);
51 HASH_ADD_KEYPTR(hh,PAX,&pax->txid,sizeof(pax->txid),pax);
52 pthread_mutex_unlock(&komodo_mutex);
53 KOMODO_DEPOSIT += fiatoshis;
56 struct pax_transaction *komodo_paxfind(struct pax_transaction *space,uint256 txid,uint16_t vout)
58 struct pax_transaction *pax;
59 pthread_mutex_lock(&komodo_mutex);
60 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
62 memcpy(space,pax,sizeof(*pax));
63 pthread_mutex_unlock(&komodo_mutex);
67 struct pax_transaction *komodo_paxmark(struct pax_transaction *space,uint256 txid,uint16_t vout,int32_t mark)
69 struct pax_transaction *pax;
70 pthread_mutex_lock(&komodo_mutex);
71 HASH_FIND(hh,PAX,&txid,sizeof(txid),pax);
75 memcpy(space,pax,sizeof(*pax));
77 pthread_mutex_unlock(&komodo_mutex);
81 int32_t komodo_issued_opreturn(uint8_t *shortflagp,char *base,uint256 *txids,uint16_t *vouts,uint8_t *opretbuf,int32_t opretlen)
84 if ( opretbuf[opretlen-5] == '-' )
88 base[i] = opretbuf[opretlen-4+i];
89 if ( strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 ) // shortflag
91 //printf("BASE.(%s) vs (%s)\n",base,ASSETCHAINS_SYMBOL);
92 opretbuf++, opretlen--;
93 for (n=len=0; n<opretlen/34; n++)
97 ((uint8_t *)&txids[n])[j] = opretbuf[len++];
98 //printf("%02x",((uint8_t *)&txids[n])[j]);
100 vouts[n] = opretbuf[len++];
101 vouts[n] = (opretbuf[len++] << 8) | vouts[n];
102 //printf(" issuedtxid v%d i.%d opretlen.%d\n",vouts[n],n,opretlen);
108 void komodo_gateway_deposits(CMutableTransaction *txNew)
110 struct pax_transaction *pax,*tmp; uint8_t *script,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
111 PENDING_KOMODO_TX = 0;
112 HASH_ITER(hh,PAX,pax,tmp)
114 if ( pax->marked != 0 )
116 txNew->vout.resize(numvouts+1);
117 txNew->vout[numvouts].nValue = pax->fiatoshis;
118 txNew->vout[numvouts].scriptPubKey.resize(25);
119 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
123 memcpy(script,pax->rmd160,20), script += 20;
128 printf("%02x",((uint8_t *)&pax->txid)[i]);
129 data[len++] = ((uint8_t *)&pax->txid)[i];
131 data[len++] = pax->vout & 0xff;
132 data[len++] = (pax->vout >> 8) & 0xff;
133 printf(" vout.%u DEPOSIT %.8f <- paxdeposit.%s\n",pax->vout,(double)txNew->vout[numvouts].nValue/COIN,ASSETCHAINS_SYMBOL);
134 PENDING_KOMODO_TX += pax->fiatoshis;
135 if ( numvouts++ >= 64 )
140 if ( ASSETCHAINS_SHORTFLAG != 0 )
142 for (i=0; ASSETCHAINS_SYMBOL[i]!=0; i++)
143 data[len++] = ASSETCHAINS_SYMBOL[i];
145 opretlen = komodo_opreturnscript(opret,'I',data,len);
146 txNew->vout.resize(numvouts+1);
147 txNew->vout[numvouts].nValue = 0;
148 txNew->vout[numvouts].scriptPubKey.resize(opretlen);
149 script = (uint8_t *)&txNew->vout[numvouts].scriptPubKey[0];
150 memcpy(script,opret,opretlen);
151 printf("total numvouts.%d %.8f opretlen.%d\n",numvouts,dstr(PENDING_KOMODO_TX),opretlen);
152 } else KOMODO_DEPOSIT = 0;
155 int32_t komodo_check_deposit(int32_t height,const CBlock& block) // verify above block is valid pax pricing
157 int32_t i,j,n,num,opretlen,offset=1; uint256 hash,txids[64]; uint8_t shortflag; char base[16]; uint16_t vouts[64]; uint8_t *script; struct pax_transaction *pax,space;
158 n = block.vtx[0].vout.size();
159 script = (uint8_t *)block.vtx[0].vout[n-1].scriptPubKey.data();
160 if ( n <= 2 || script[0] != 0x6a )
162 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
163 //printf("checkdeposit n.%d [%02x] [%c] %d vs %d\n",n,script[0],script[offset],script[offset],'I');
164 if ( script[offset] == 'I' && opretlen < block.vtx[0].vout[n-1].scriptPubKey.size() )
166 if ( (num= komodo_issued_opreturn(&shortflag,base,txids,vouts,&script[offset],opretlen)) > 0 )
168 for (i=1; i<n-1; i++)
170 if ( (pax= komodo_paxfind(&space,txids[i-1],vouts[i-1])) == 0 || pax->fiatoshis != block.vtx[0].vout[i].nValue )
175 hash = block.GetHash();
177 printf("%02x",((uint8_t *)&hash)[j]);
178 printf(" ht.%d blockhash couldnt find vout.[%d]\n",height,i);
182 //printf("opretlen.%d num.%d\n",opretlen,num);
187 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
189 uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,n,len,tokomodo=0; char base[4],coinaddr[64],destaddr[64]; struct pax_transaction space; uint256 txids[64]; uint16_t vouts[64]; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
190 tokomodo = (komodo_is_issuer() == 0);
191 if ( ASSETCHAINS_SYMBOL[0] != 0 )
193 for (i=0; i<opretlen; i++)
194 printf("%02x",opretbuf[i]);
195 printf(" opret[%c] tokomodo.%d\n",opretbuf[0],tokomodo);
197 if ( opretbuf[0] == ((tokomodo == 0) ? 'D' : 'W') )
199 if ( opretlen == 34 )
201 memset(base,0,sizeof(base));
202 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
204 fiatoshis = -fiatoshis;
205 bitcoin_address(coinaddr,addrtype,rmd160,20);
206 checktoshis = PAX_fiatdest(tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis);
208 if ( tokomodo == 0 && strncmp(ASSETCHAINS_SYMBOL,base,strlen(base)) == 0 )
211 printf("%02x",((uint8_t *)&txid)[i]);
212 printf(" <- txid.v%u ",vout);
214 printf("%02x",pubkey33[i]);
215 printf(" checkpubkey check %.8f v %.8f dest.(%s) height.%d\n",dstr(checktoshis),dstr(value),destaddr,height);
216 if ( value >= checktoshis && shortflag == ASSETCHAINS_SHORTFLAG )
218 komodo_gateway_deposit(coinaddr,value,shortflag,base,fiatoshis,rmd160,txid,vout,height);
223 if ( value <= checktoshis )
230 else if ( strncmp((char *)"KMD",(char *)&opretbuf[opretlen-4],3) != 0 )
232 if ( tokomodo == 0 && opretbuf[0] == 'I' )
234 if ( (n= komodo_issued_opreturn(&shortflag,base,txids,vouts,opretbuf,opretlen)) > 0 && shortflag == ASSETCHAINS_SHORTFLAG )
239 printf("%02x",((uint8_t *)&txids[i])[j]);
240 printf(" issuedtxid v%d i.%d of n.%d opretlen.%d\n",vouts[i],i,n,opretlen);
241 if ( komodo_paxmark(&space,txids[i],vouts[i],height) == 0 )
243 printf("%s queue remove deposit\n",ASSETCHAINS_SYMBOL);
244 komodo_gateway_deposit(0,0,0,0,0,0,txids[i],vouts[i],height);
253 void komodo_gateway_voutupdate(char *symbol,int32_t isspecial,int32_t height,int32_t txi,bits256 txid,int32_t vout,int32_t numvouts,uint64_t value,uint8_t *script,int32_t len)
255 int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
257 memcpy(&utxid,&txid,sizeof(utxid));
258 if ( 0 )//txi != 0 || vout != 0 )
260 for (i=0; i<len; i++)
261 printf("%02x",script[i]);
262 printf(" <- %s VOUTUPDATE.%d txi.%d vout.%d %.8f scriptlen.%d OP_RETURN.%d (%s) len.%d\n",symbol,height,txi,vout,dstr(value),len,script[0] == 0x6a,typestr,opretlen);
264 if ( script[offset++] == 0x6a )
266 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
267 if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],ASSETCHAINS_SYMBOL[0]==0?"KMD":ASSETCHAINS_SYMBOL) == 0 )
268 typestr = "notarized";
269 else if ( txi == 0 && vout == 1 && opretlen == 149 )
271 typestr = "pricefeed";
272 komodo_paxpricefeed(height,&script[offset],opretlen);
273 //printf("height.%d pricefeed len.%d\n",height,opretlen);
275 else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
277 else if ( numvouts > 13 )
281 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
283 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; bits256 txid;
284 sprintf(params,"[\"%s\", 1]",txidstr);
285 if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
287 if ( (json= cJSON_Parse(retstr)) != 0 )
289 if ( (result= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
293 txid = jbits256(result,(char *)"txid");
294 for (vout=0; vout<n; vout++)
296 item = jitem(vouts,vout);
297 value = SATOSHIDEN * jdouble(item,(char *)"value");
298 if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
300 if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
302 len = (int32_t)strlen(hexstr) >> 1;
303 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
305 else if ( len <= sizeof(script) )
307 decode_hex(script,len,hexstr);
308 komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
313 } else printf("error getting txids.(%s)\n",retstr);
321 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
323 char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx=0,*result=0,*result2;
324 sprintf(params,"[%d]",height);
325 if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
327 if ( (result= cJSON_Parse(retstr)) != 0 )
329 if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
331 sprintf(params,"[\"%s\"]",txidstr);
332 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
334 //printf("getblock.(%s)\n",retstr2);
335 if ( (json= cJSON_Parse(retstr2)) != 0 )
337 if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
340 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
344 else printf("komodo_gateway_block ht.%d error i.%d vs n.%d\n",height,i,n);
345 } else printf("cant get result.%p or tx.%p\n",result,tx);
347 } else printf("cant parse2.(%s)\n",retstr2);
349 } else printf("error getblock %s\n",params);
350 } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
352 } else printf("couldnt parse.(%s)\n",retstr);
354 } else printf("error from getblockhash %d\n",height);
358 void komodo_gateway_iteration(char *symbol)
360 char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
361 if ( KMDHEIGHT <= 0 )
364 if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
366 if ( (infoobj= cJSON_Parse(retstr)) != 0 )
368 if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
370 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
372 if ( (KMDHEIGHT % 100) == 0 )
374 fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
375 memset(&zero,0,sizeof(zero));
376 komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
378 if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
380 printf("error KMDHEIGHT %d\n",KMDHEIGHT);
385 if ( KMDHEIGHT >= kmdheight )
386 KOMODO_REALTIME = (uint32_t)time(NULL);
394 printf("error from %s\n",symbol);