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;
24 char symbol[4]; uint8_t rmd160[20],shortflag;
27 void komodo_gateway_deposits(CMutableTransaction& txNew)
29 struct pax_transaction *ptr; uint8_t *script,opret[10000],data[10000]; int32_t i,len=0,opretlen=0,numvouts=1;
30 PENDING_KOMODO_TX = 0;
31 while ( (ptr= (struct pax_transaction *)queue_dequeue(&DepositsQ,0)) != 0 )
33 txNew.vout.resize(numvouts+1);
34 txNew.vout[numvouts].nValue = ptr->fiatoshis;
35 txNew.vout[numvouts].scriptPubKey.resize(25);
36 script = (uint8_t *)&txNew.vout[numvouts].scriptPubKey[0];
40 memcpy(script,ptr->rmd160,20), script += 20;
45 printf("%02x",((uint8_t *)&ptr->txid)[i]);
46 data[len++] = ((uint8_t *)&ptr->txid)[i];
48 data[len++] = ptr->vout & 0xff;
49 data[len++] = (ptr->vout >> 8) & 0xff;
50 printf(" vout.%u DEPOSIT %.8f\n",ptr->vout,(double)KOMODO_DEPOSIT/COIN);
51 PENDING_KOMODO_TX += ptr->fiatoshis;
53 queue_enqueue((char *)"PENDINGS",&PendingsQ,&ptr->DL,0);
57 opretlen = komodo_opreturnscript(opret,'I',data,len);
58 txNew.vout.resize(numvouts+1);
59 txNew.vout[numvouts].nValue = 0;
60 txNew.vout[numvouts].scriptPubKey.resize(opretlen);
61 script = (uint8_t *)&txNew.vout[numvouts].scriptPubKey[0];
62 memcpy(script,opret,opretlen);
64 printf("total numvouts.%d %.8f\n",numvouts,dstr(PENDING_KOMODO_TX));
67 void komodo_gateway_deposit(uint64_t value,int32_t shortflag,char *symbol,uint64_t fiatoshis,uint8_t *rmd160,uint256 txid,uint16_t vout) // assetchain context
69 struct pax_transaction *ptr;
70 ptr = (struct pax_transaction *)calloc(1,sizeof(*ptr));
71 ptr->komodoshis = value;
72 ptr->fiatoshis = fiatoshis;
73 memcpy(ptr->symbol,symbol,3);
74 memcpy(ptr->rmd160,rmd160,20);
75 ptr->shortflag = shortflag;
78 KOMODO_DEPOSIT += fiatoshis;
79 queue_enqueue((char *)"DEPOSITS",&DepositsQ,&ptr->DL,0);
82 int32_t komodo_gateway_depositremove(uint256 txid,uint16_t vout) // assetchain context
84 int32_t iter,n=0; queue_t *Q; struct pax_transaction *ptr; struct queueitem *item;
85 for (iter=0; iter<2; iter++)
87 Q = (iter == 0) ? &DepositsQ : &PendingsQ;
88 portable_mutex_lock(&Q->mutex);
92 DL_FOREACH(Q->list,item)
94 if ( memcmp(&ptr->txid,&txid,sizeof(txid)) == 0 && ptr->vout == vout )
96 if ( KOMODO_DEPOSIT >= ptr->fiatoshis )
97 KOMODO_DEPOSIT -= ptr->fiatoshis;
98 else KOMODO_DEPOSIT = 0;
99 printf("DELETE %.8f DEPOSIT %s %.8f\n",dstr(ptr->komodoshis),ptr->symbol,dstr(ptr->fiatoshis));
100 DL_DELETE(Q->list,&ptr->DL);
102 myfree(ptr,sizeof(struct queueitem));
107 portable_mutex_unlock(&Q->mutex);
112 const char *komodo_opreturn(int32_t height,uint64_t value,uint8_t *opretbuf,int32_t opretlen,uint256 txid,uint16_t vout)
114 uint8_t rmd160[20],addrtype,shortflag,pubkey33[33]; int32_t i,j,len,tokomodo=0; char base[4],coinaddr[64],destaddr[64]; int64_t fiatoshis,checktoshis; const char *typestr = "unknown";
115 //printf("komodo_opreturn[%c]: ht.%d %.8f opretlen.%d\n",opretbuf[0],height,dstr(value),opretlen);
119 if ( opretbuf[0] == ((tokomodo != 0) ? 'D' : 'W') )
121 if ( opretlen == 34 )
123 memset(base,0,sizeof(base));
124 PAX_pubkey(0,&opretbuf[1],&addrtype,rmd160,base,&shortflag,&fiatoshis);
126 fiatoshis = -fiatoshis;
127 bitcoin_address(coinaddr,addrtype,rmd160,20);
128 checktoshis = PAX_fiatdest(tokomodo,destaddr,pubkey33,coinaddr,height,base,fiatoshis);
129 for (i=0; i<opretlen; i++)
130 printf("%02x",opretbuf[i]);
131 printf(" DEPOSIT %.8f %c%s -> %s ",dstr(fiatoshis),shortflag!=0?'-':'+',base,coinaddr);
133 printf("%02x",((uint8_t *)&txid)[i]);
134 printf(" <- txid.v%u ",vout);
136 printf("%02x",pubkey33[i]);
137 printf(" checkpubkey check %.8f v %.8f dest.(%s)\n",dstr(checktoshis),dstr(value),destaddr);
140 if ( strncmp(KOMODO_SOURCE,base,strlen(base)) == 0 && value >= (9999*checktoshis)/10000 && shortflag == ASSETCHAINS_SHORTFLAG )
142 komodo_gateway_deposit(value,shortflag,symbol,fiatoshis,rmd160,txid,vout);
145 if ( tokomodo != 0 && value <= (10000*checktoshis)/9999 )
152 else if ( opretbuf[0] == 'I' )
154 uint256 issuedtxid; uint16_t issuedvout;
155 opretbuf++, opretlen--;
156 for (i=len=0; i<opretlen/34; i++)
160 ((uint8_t *)&issuedtxid)[j] = opretbuf[len++];
161 printf("%02x",((uint8_t *)&issuedtxid)[j]);
163 issuedvout = opretbuf[len++];
164 issuedvout = (vout << 8) | opretbuf[len++];
165 printf(" issuedtxid v%d i.%d opretlen.%d\n",issuedvout,i,opretlen);
166 if ( komodo_gateway_depositremove(issuedtxid,issuedvout) == 0 )
167 printf("error removing deposit\n");
173 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)
175 int32_t i,opretlen,offset = 0; uint256 zero,utxid; const char *typestr;
177 memcpy(&utxid,&txid,sizeof(utxid));
178 if ( 0 )//txi != 0 || vout != 0 )
180 for (i=0; i<len; i++)
181 printf("%02x",script[i]);
182 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);
184 if ( script[offset++] == 0x6a )
186 offset += komodo_scriptitemlen(&opretlen,&script[offset]);
187 if ( isspecial != 0 && len >= offset+32*2+4 && strcmp((char *)&script[offset+32*2+4],"KMD") == 0 )
188 typestr = "notarized";
189 else if ( txi == 0 && vout == 1 && opretlen == 149 )
191 typestr = "pricefeed";
192 komodo_paxpricefeed(height,&script[offset],opretlen);
193 //printf("height.%d pricefeed len.%d\n",height,opretlen);
195 else komodo_stateupdate(height,0,0,0,utxid,0,0,0,0,0,value,&script[offset],opretlen,vout);
197 else if ( numvouts > 13 )
201 int32_t komodo_gateway_tx(char *symbol,int32_t height,int32_t txi,char *txidstr,uint32_t port)
203 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;
204 sprintf(params,"[\"%s\", 1]",txidstr);
205 if ( (retstr= komodo_issuemethod((char *)"getrawtransaction",params,port)) != 0 )
207 if ( (json= cJSON_Parse(retstr)) != 0 )
209 if ( (result= jobj(json,(char *)"result")) != 0 && (vouts= jarray(&n,result,(char *)"vout")) != 0 )
213 for (vout=0; vout<n; vout++)
215 item = jitem(vouts,vout);
216 txid = jbits256(item,(char *)"txid");
217 value = SATOSHIDEN * jdouble(item,(char *)"value");
218 if ( (sobj= jobj(item,(char *)"scriptPubKey")) != 0 )
220 if ( (hexstr= jstr(sobj,(char *)"hex")) != 0 )
222 len = (int32_t)strlen(hexstr) >> 1;
223 if ( 0 && len != 74 )
224 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));
225 if ( vout == 0 && ((memcmp(&hexstr[2],CRYPTO777_PUBSECPSTR,66) == 0 && len == 35) || (memcmp(&hexstr[6],CRYPTO777_RMD160STR,40) == 0 && len == 25)) )
227 else if ( len <= sizeof(script) )
229 decode_hex(script,len,hexstr);
230 komodo_gateway_voutupdate(symbol,isspecial,height,txi,txid,vout,n,value,script,len);
243 int32_t komodo_gateway_block(char *symbol,int32_t height,uint16_t port)
245 char *retstr,*retstr2,params[128],*txidstr; int32_t i,n,retval = -1; cJSON *json,*tx,*result,*result2;
246 sprintf(params,"[%d]",height);
247 if ( (retstr= komodo_issuemethod((char *)"getblockhash",params,port)) != 0 )
249 if ( (result= cJSON_Parse(retstr)) != 0 )
251 if ( (txidstr= jstr(result,(char *)"result")) != 0 && strlen(txidstr) == 64 )
253 sprintf(params,"[\"%s\"]",txidstr);
254 if ( (retstr2= komodo_issuemethod((char *)"getblock",params,port)) != 0 )
256 //printf("getblock.(%s)\n",retstr2);
257 if ( (json= cJSON_Parse(retstr2)) != 0 )
259 if ( (result2= jobj(json,(char *)"result")) != 0 && (tx= jarray(&n,result2,(char *)"tx")) != 0 )
262 if ( komodo_gateway_tx(symbol,height,i,jstri(tx,i),port) < 0 )
266 else printf("error i.%d vs n.%d\n",i,n);
272 } else printf("strlen.%ld (%s)\n",strlen(txidstr),txidstr);
280 void komodo_gateway_iteration(char *symbol)
282 char *retstr; int32_t i,kmdheight; cJSON *infoobj,*result; uint256 zero; uint16_t port = 7771;
283 if ( KMDHEIGHT <= 0 )
285 if ( (retstr= komodo_issuemethod((char *)"getinfo",0,port)) != 0 )
287 if ( (infoobj= cJSON_Parse(retstr)) != 0 )
289 if ( (result= jobj(infoobj,(char *)"result")) != 0 && (kmdheight= jint(result,(char *)"blocks")) != 0 )
291 for (i=0; i<1000 && KMDHEIGHT<kmdheight; i++,KMDHEIGHT++)
293 printf("KMDHEIGHT %d\n",KMDHEIGHT);
294 if ( (KMDHEIGHT % 100) == 0 )
296 fprintf(stderr,"%s.%d ",symbol,KMDHEIGHT);
297 memset(&zero,0,sizeof(zero));
298 komodo_stateupdate(KMDHEIGHT,0,0,0,zero,0,0,0,0,KMDHEIGHT,0,0,0,0);
300 if ( komodo_gateway_block(symbol,KMDHEIGHT,port) < 0 )
311 //printf("error from %s\n",symbol);
317 void komodo_gateway_issuer() // from "assetchain" connectblock()
323 void komodo_gateway_redeemer() // from "KMD" connectblock()