]>
Commit | Line | Data |
---|---|---|
eeaaf554 | 1 | /****************************************************************************** |
2 | * Copyright © 2014-2017 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 | /* | |
17 | z_exportkey "zaddr" | |
18 | z_exportwallet "filename" | |
19 | z_getoperationstatus (["operationid", ... ]) | |
20 | z_gettotalbalance ( minconf ) | |
21 | z_importkey "zkey" ( rescan ) | |
22 | z_importwallet "filename" | |
23 | z_listaddresses | |
24 | z_sendmany "fromaddress" [{"address":... ,"amount":..., "memo":"<hex>"},...] ( minconf ) ( fee ) | |
25 | */ | |
26 | ||
27 | #define JUMBLR_ADDR "RGhxXpXSSBTBm9EvNsXnTQczthMCxHX91t" | |
28 | #define JUMBLR_BTCADDR "18RmTJe9qMech8siuhYfMtHo8RtcN1obC6" | |
29 | #define JUMBLR_MAXSECRETADDRS 777 | |
30 | #define JUMBLR_SYNCHRONIZED_BLOCKS 2 // 60 | |
31 | #define JUMBLR_INCR (99.65 / 100) | |
32 | #define JUMBLR_FEE 0.001 | |
33 | #define JUMBLR_TXFEE 0.01 | |
9bbeff91 | 34 | #define SMALLVAL 0.000000000000001 |
eeaaf554 | 35 | |
3f318aa4 | 36 | #define JUMBLR_ERROR_DUPLICATEDEPOSIT -1 |
37 | #define JUMBLR_ERROR_SECRETCANTBEDEPOSIT -2 | |
38 | #define JUMBLR_ERROR_TOOMANYSECRETS -3 | |
44165ded | 39 | #define JUMBLR_ERROR_NOTINWALLET -4 |
3f318aa4 | 40 | |
eeaaf554 | 41 | struct jumblr_item |
42 | { | |
43 | UT_hash_handle hh; | |
44 | int64_t amount,fee,txfee; | |
45 | uint32_t spent,pad; | |
46 | char opid[64],src[128],dest[128],status; | |
47 | } *Jumblrs; | |
48 | ||
49 | char Jumblr_secretaddrs[JUMBLR_MAXSECRETADDRS][64],Jumblr_deposit[64]; | |
50 | int32_t Jumblr_numsecretaddrs; // if 0 -> run silent mode | |
51 | ||
a8ec5e86 | 52 | char *jumblr_issuemethod(char *userpass,char *method,char *params,uint16_t port) |
b487a8fe | 53 | { |
54 | cJSON *retjson,*resjson = 0; char *retstr; | |
55 | if ( (retstr= komodo_issuemethod(userpass,method,params,port)) != 0 ) | |
56 | { | |
57 | if ( (retjson= cJSON_Parse(retstr)) != 0 ) | |
58 | { | |
eb416c9c | 59 | if ( jobj(retjson,(char *)"result") != 0 ) |
60 | resjson = jduplicate(jobj(retjson,(char *)"result")); | |
61 | else if ( jobj(retjson,(char *)"error") != 0 ) | |
62 | resjson = jduplicate(jobj(retjson,(char *)"error")); | |
b487a8fe | 63 | else |
64 | { | |
65 | resjson = cJSON_CreateObject(); | |
eb416c9c | 66 | jaddstr(resjson,(char *)"error",(char *)"cant parse return"); |
b487a8fe | 67 | } |
68 | free_json(retjson); | |
69 | } | |
70 | free(retstr); | |
71 | } | |
72 | if ( resjson != 0 ) | |
73 | return(jprint(resjson,1)); | |
eb416c9c | 74 | else return(clonestr((char *)"{\"error\":\"unknown error\"}")); |
b487a8fe | 75 | } |
76 | ||
19acd959 | 77 | char *jumblr_importaddress(char *address) |
eeaaf554 | 78 | { |
79 | char params[1024]; | |
80 | sprintf(params,"[\"%s\", \"%s\", false]",address,address); | |
b487a8fe | 81 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"importaddress",params,7771)); |
eeaaf554 | 82 | } |
83 | ||
44165ded | 84 | char *jumblr_validateaddress(char *addr) |
85 | { | |
86 | char params[1024]; | |
87 | sprintf(params,"[\"%s\"]",addr); | |
88 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"validateaddress",params,7771)); | |
89 | } | |
90 | ||
eeaaf554 | 91 | int32_t Jumblr_secretaddrfind(char *searchaddr) |
92 | { | |
93 | int32_t i; | |
94 | for (i=0; i<Jumblr_numsecretaddrs; i++) | |
95 | { | |
96 | if ( strcmp(searchaddr,Jumblr_secretaddrs[i]) == 0 ) | |
97 | return(i); | |
98 | } | |
99 | return(-1); | |
100 | } | |
101 | ||
102 | int32_t Jumblr_secretaddradd(char *secretaddr) // external | |
103 | { | |
104 | int32_t ind; | |
3f318aa4 | 105 | if ( secretaddr != 0 && secretaddr[0] != 0 ) |
eeaaf554 | 106 | { |
3f318aa4 | 107 | if ( Jumblr_numsecretaddrs < JUMBLR_MAXSECRETADDRS ) |
eeaaf554 | 108 | { |
3f318aa4 | 109 | if ( strcmp(Jumblr_deposit,secretaddr) != 0 ) |
110 | { | |
111 | if ( (ind= Jumblr_secretaddrfind(secretaddr)) < 0 ) | |
112 | { | |
113 | safecopy(Jumblr_secretaddrs[Jumblr_numsecretaddrs],secretaddr,64); | |
114 | Jumblr_numsecretaddrs++; | |
115 | } else return(ind); | |
116 | } else return(JUMBLR_ERROR_SECRETCANTBEDEPOSIT); | |
117 | } else return(JUMBLR_ERROR_TOOMANYSECRETS); | |
118 | } | |
119 | else | |
120 | { | |
121 | memset(Jumblr_secretaddrs,0,sizeof(Jumblr_secretaddrs)); | |
122 | Jumblr_numsecretaddrs = 0; | |
eeaaf554 | 123 | } |
124 | return(Jumblr_numsecretaddrs); | |
125 | } | |
126 | ||
b487a8fe | 127 | int32_t Jumblr_depositaddradd(char *depositaddr) // external |
eeaaf554 | 128 | { |
44165ded | 129 | int32_t ind,retval = JUMBLR_ERROR_DUPLICATEDEPOSIT; char *retstr; cJSON *retjson,*ismine; |
eeaaf554 | 130 | if ( depositaddr == 0 ) |
19acd959 | 131 | depositaddr = (char *)""; |
9bbeff91 | 132 | if ( (ind= Jumblr_secretaddrfind(depositaddr)) < 0 ) |
eeaaf554 | 133 | { |
134 | safecopy(Jumblr_deposit,depositaddr,sizeof(Jumblr_deposit)); | |
44165ded | 135 | if ( (retstr= jumblr_validateaddress(depositaddr)) != 0 ) |
b487a8fe | 136 | { |
137 | if ( (retjson= cJSON_Parse(retstr)) != 0 ) | |
138 | { | |
44165ded | 139 | if ( (ismine= jobj(retjson,(char *)"ismine")) != 0 && is_cJSON_True(ismine) != 0 ) |
a8ec5e86 | 140 | retval = 0; |
44165ded | 141 | else retval = JUMBLR_ERROR_NOTINWALLET; |
b487a8fe | 142 | free_json(retjson); |
143 | } | |
144 | free(retstr); | |
145 | } | |
146 | } | |
147 | return(retval); | |
eeaaf554 | 148 | } |
149 | ||
150 | int32_t Jumblr_secretaddr(char *secretaddr) | |
151 | { | |
152 | uint32_t r; | |
153 | if ( Jumblr_numsecretaddrs > 0 ) | |
154 | { | |
19acd959 | 155 | OS_randombytes((uint8_t *)&r,sizeof(r)); |
eeaaf554 | 156 | r %= Jumblr_numsecretaddrs; |
157 | safecopy(secretaddr,Jumblr_secretaddrs[r],64); | |
158 | } | |
159 | return(r); | |
160 | } | |
161 | ||
162 | int32_t jumblr_addresstype(char *addr) | |
163 | { | |
aa3be97e | 164 | if ( addr[0] == '"' && addr[strlen(addr)-1] == '"' ) |
c8e62868 | 165 | { |
aa3be97e | 166 | addr[strlen(addr)-1] = 0; |
167 | addr++; | |
c8e62868 | 168 | } |
aa3be97e | 169 | if ( addr[0] == 'z' && addr[1] == 'c' && strlen(addr) >= 40 ) |
170 | return('z'); | |
eeaaf554 | 171 | else if ( strlen(addr) < 40 ) |
172 | return('t'); | |
c8e62868 | 173 | printf("strange.(%s)\n",addr); |
174 | return(-1); | |
eeaaf554 | 175 | } |
176 | ||
177 | struct jumblr_item *jumblr_opidfind(char *opid) | |
178 | { | |
179 | struct jumblr_item *ptr; | |
180 | HASH_FIND(hh,Jumblrs,opid,(int32_t)strlen(opid),ptr); | |
181 | return(ptr); | |
182 | } | |
183 | ||
184 | struct jumblr_item *jumblr_opidadd(char *opid) | |
185 | { | |
8ac54273 | 186 | struct jumblr_item *ptr = 0; |
187 | if ( opid != 0 && (ptr= jumblr_opidfind(opid)) == 0 ) | |
eeaaf554 | 188 | { |
19acd959 | 189 | ptr = (struct jumblr_item *)calloc(1,sizeof(*ptr)); |
eeaaf554 | 190 | safecopy(ptr->opid,opid,sizeof(ptr->opid)); |
191 | HASH_ADD_KEYPTR(hh,Jumblrs,ptr->opid,(int32_t)strlen(ptr->opid),ptr); | |
192 | if ( ptr != jumblr_opidfind(opid) ) | |
193 | printf("jumblr_opidadd.(%s) ERROR, couldnt find after add\n",opid); | |
194 | } | |
195 | return(ptr); | |
196 | } | |
197 | ||
eeaaf554 | 198 | char *jumblr_zgetnewaddress() |
199 | { | |
200 | char params[1024]; | |
19acd959 | 201 | sprintf(params,"[]"); |
b487a8fe | 202 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getnewaddress",params,7771)); |
eeaaf554 | 203 | } |
204 | ||
205 | char *jumblr_zlistoperationids() | |
206 | { | |
207 | char params[1024]; | |
19acd959 | 208 | sprintf(params,"[]"); |
b487a8fe | 209 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listoperationids",params,7771)); |
eeaaf554 | 210 | } |
211 | ||
212 | char *jumblr_zgetoperationresult(char *opid) | |
213 | { | |
214 | char params[1024]; | |
215 | sprintf(params,"[[\"%s\"]]",opid); | |
b487a8fe | 216 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getoperationresult",params,7771)); |
eeaaf554 | 217 | } |
218 | ||
219 | char *jumblr_zgetoperationstatus(char *opid) | |
220 | { | |
221 | char params[1024]; | |
222 | sprintf(params,"[[\"%s\"]]",opid); | |
b487a8fe | 223 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getoperationstatus",params,7771)); |
eeaaf554 | 224 | } |
225 | ||
226 | char *jumblr_sendt_to_z(char *taddr,char *zaddr,double amount) | |
227 | { | |
228 | char params[1024]; double fee = (amount-3*JUMBLR_TXFEE) * JUMBLR_FEE; | |
229 | if ( jumblr_addresstype(zaddr) != 'z' || jumblr_addresstype(taddr) != 't' ) | |
19acd959 | 230 | return(clonestr((char *)"{\"error\":\"illegal address in t to z\"}")); |
eeaaf554 | 231 | sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}, {\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",taddr,zaddr,amount-fee-JUMBLR_TXFEE,JUMBLR_ADDR,fee,JUMBLR_TXFEE); |
61a0f96b | 232 | printf("t -> z: %s\n",params); |
b487a8fe | 233 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,7771)); |
eeaaf554 | 234 | } |
235 | ||
236 | char *jumblr_sendz_to_z(char *zaddrS,char *zaddrD,double amount) | |
237 | { | |
238 | char params[1024]; double fee = (amount-2*JUMBLR_TXFEE) * JUMBLR_FEE; | |
239 | if ( jumblr_addresstype(zaddrS) != 'z' || jumblr_addresstype(zaddrD) != 'z' ) | |
19acd959 | 240 | return(clonestr((char *)"{\"error\":\"illegal address in z to z\"}")); |
eeaaf554 | 241 | sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}, {\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",zaddrS,zaddrD,amount-fee-JUMBLR_TXFEE,JUMBLR_ADDR,fee,JUMBLR_TXFEE); |
61a0f96b | 242 | printf("z -> z: %s\n",params); |
b487a8fe | 243 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,7771)); |
eeaaf554 | 244 | } |
245 | ||
246 | char *jumblr_sendz_to_t(char *zaddr,char *taddr,double amount) | |
247 | { | |
248 | char params[1024]; double fee = (amount-JUMBLR_TXFEE) * JUMBLR_FEE; | |
249 | if ( jumblr_addresstype(zaddr) != 'z' || jumblr_addresstype(taddr) != 't' ) | |
19acd959 | 250 | return(clonestr((char *)"{\"error\":\"illegal address in z to t\"}")); |
eeaaf554 | 251 | sprintf(params,"[\"%s\", [{\"address\":\"%s\",\"amount\":%.8f}, {\"address\":\"%s\",\"amount\":%.8f}], 1, %.8f]",zaddr,taddr,amount-fee-JUMBLR_TXFEE,JUMBLR_ADDR,fee,JUMBLR_TXFEE); |
61a0f96b | 252 | printf("z -> t: %s\n",params); |
b487a8fe | 253 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_sendmany",params,7771)); |
eeaaf554 | 254 | } |
255 | ||
256 | char *jumblr_zlistreceivedbyaddress(char *addr) | |
257 | { | |
258 | char params[1024]; | |
259 | sprintf(params,"[\"%s\", 1]",addr); | |
b487a8fe | 260 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_listreceivedbyaddress",params,7771)); |
eeaaf554 | 261 | } |
262 | ||
263 | char *jumblr_getreceivedbyaddress(char *addr) | |
264 | { | |
265 | char params[1024]; | |
266 | sprintf(params,"[\"%s\", 1]",addr); | |
b487a8fe | 267 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"getreceivedbyaddress",params,7771)); |
eeaaf554 | 268 | } |
269 | ||
270 | char *jumblr_importprivkey(char *wifstr) | |
271 | { | |
272 | char params[1024]; | |
273 | sprintf(params,"[\"%s\", \"\", false]",wifstr); | |
b487a8fe | 274 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"importprivkey",params,7771)); |
eeaaf554 | 275 | } |
276 | ||
277 | char *jumblr_zgetbalance(char *addr) | |
278 | { | |
279 | char params[1024]; | |
280 | sprintf(params,"[\"%s\", 1]",addr); | |
b487a8fe | 281 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"z_getbalance",params,7771)); |
eeaaf554 | 282 | } |
283 | ||
284 | char *jumblr_listunspent(char *coinaddr) | |
285 | { | |
286 | char params[1024]; | |
287 | sprintf(params,"[1, 99999999, [\"%s\"]]",coinaddr); | |
b487a8fe | 288 | return(jumblr_issuemethod(KMDUSERPASS,(char *)"listunspent",params,7771)); |
eeaaf554 | 289 | } |
290 | ||
291 | int64_t jumblr_receivedby(char *addr) | |
292 | { | |
293 | char *retstr; int64_t total = 0; | |
294 | if ( (retstr= jumblr_getreceivedbyaddress(addr)) != 0 ) | |
295 | { | |
296 | total = atof(retstr) * SATOSHIDEN; | |
297 | free(retstr); | |
298 | } | |
299 | return(total); | |
300 | } | |
301 | ||
302 | int64_t jumblr_balance(char *addr) | |
303 | { | |
304 | char *retstr; double val; cJSON *retjson; int32_t i,n; int64_t balance = 0; | |
305 | if ( jumblr_addresstype(addr) == 't' ) | |
306 | { | |
307 | if ( (retstr= jumblr_listunspent(addr)) != 0 ) | |
308 | { | |
61a0f96b | 309 | //printf("jumblr.[%s].(%s)\n","KMD",retstr); |
eeaaf554 | 310 | if ( (retjson= cJSON_Parse(retstr)) != 0 ) |
311 | { | |
b487a8fe | 312 | if ( (n= cJSON_GetArraySize(retjson)) > 0 && is_cJSON_Array(retjson) != 0 ) |
eeaaf554 | 313 | for (i=0; i<n; i++) |
19acd959 | 314 | balance += SATOSHIDEN * jdouble(jitem(retjson,i),(char *)"amount"); |
eeaaf554 | 315 | free_json(retjson); |
316 | } | |
317 | free(retstr); | |
318 | } | |
319 | } | |
320 | else if ( (retstr= jumblr_zgetbalance(addr)) != 0 ) | |
321 | { | |
322 | if ( (val= atof(retstr)) > SMALLVAL ) | |
323 | balance = val * SATOSHIDEN; | |
324 | free(retstr); | |
325 | } | |
326 | return(balance); | |
327 | } | |
328 | ||
329 | int32_t jumblr_itemset(struct jumblr_item *ptr,cJSON *item,char *status) | |
330 | { | |
331 | cJSON *params,*amounts,*dest; char *from,*addr; int32_t i,n; int64_t amount; | |
332 | /*"params" : { | |
333 | "fromaddress" : "RDhEGYScNQYetCyG75Kf8Fg61UWPdwc1C5", | |
334 | "amounts" : [ | |
335 | { | |
336 | "address" : "zc9s3UdkDFTnnwHrMCr1vYy2WmkjhmTxXNiqC42s7BjeKBVUwk766TTSsrRPKfnX31Bbu8wbrTqnjDqskYGwx48FZMPHvft", | |
337 | "amount" : 3.00000000 | |
338 | } | |
339 | ], | |
340 | "minconf" : 1, | |
341 | "fee" : 0.00010000 | |
342 | }*/ | |
19acd959 | 343 | if ( (params= jobj(item,(char *)"params")) != 0 ) |
eeaaf554 | 344 | { |
345 | //printf("params.(%s)\n",jprint(params,0)); | |
19acd959 | 346 | if ( (from= jstr(params,(char *)"fromaddress")) != 0 ) |
eeaaf554 | 347 | { |
348 | safecopy(ptr->src,from,sizeof(ptr->src)); | |
349 | } | |
19acd959 | 350 | if ( (amounts= jarray(&n,params,(char *)"amounts")) != 0 ) |
eeaaf554 | 351 | { |
352 | for (i=0; i<n; i++) | |
353 | { | |
354 | dest = jitem(amounts,i); | |
355 | //printf("%s ",jprint(dest,0)); | |
19acd959 | 356 | if ( (addr= jstr(dest,(char *)"address")) != 0 && (amount= jdouble(dest,(char *)"amount")*SATOSHIDEN) > 0 ) |
eeaaf554 | 357 | { |
358 | if ( strcmp(addr,JUMBLR_ADDR) == 0 ) | |
359 | ptr->fee = amount; | |
360 | else | |
361 | { | |
362 | ptr->amount = amount; | |
363 | safecopy(ptr->dest,addr,sizeof(ptr->dest)); | |
364 | } | |
365 | } | |
366 | } | |
367 | } | |
19acd959 | 368 | ptr->txfee = jdouble(params,(char *)"fee") * SATOSHIDEN; |
eeaaf554 | 369 | } |
370 | return(1); | |
371 | } | |
372 | ||
373 | void jumblr_opidupdate(struct jumblr_item *ptr) | |
374 | { | |
375 | char *retstr,*status; cJSON *retjson,*item; | |
376 | if ( ptr->status == 0 ) | |
377 | { | |
378 | if ( (retstr= jumblr_zgetoperationstatus(ptr->opid)) != 0 ) | |
379 | { | |
380 | if ( (retjson= cJSON_Parse(retstr)) != 0 ) | |
381 | { | |
eb416c9c | 382 | if ( cJSON_GetArraySize(retjson) == 1 && is_cJSON_Array(retjson) != 0 ) |
eeaaf554 | 383 | { |
384 | item = jitem(retjson,0); | |
385 | //printf("%s\n",jprint(item,0)); | |
19acd959 | 386 | if ( (status= jstr(item,(char *)"status")) != 0 ) |
eeaaf554 | 387 | { |
19acd959 | 388 | if ( strcmp(status,(char *)"success") == 0 ) |
eeaaf554 | 389 | { |
390 | ptr->status = jumblr_itemset(ptr,item,status); | |
391 | if ( (jumblr_addresstype(ptr->src) == 't' && jumblr_addresstype(ptr->src) == 'z' && strcmp(ptr->src,Jumblr_deposit) != 0) || (jumblr_addresstype(ptr->src) == 'z' && jumblr_addresstype(ptr->src) == 't' && Jumblr_secretaddrfind(ptr->dest) < 0) ) | |
392 | { | |
393 | printf("a non-jumblr t->z pruned\n"); | |
394 | free(jumblr_zgetoperationresult(ptr->opid)); | |
395 | ptr->status = -1; | |
396 | } | |
397 | ||
398 | } | |
19acd959 | 399 | else if ( strcmp(status,(char *)"failed") == 0 ) |
eeaaf554 | 400 | { |
401 | printf("jumblr_opidupdate %s failed\n",ptr->opid); | |
402 | free(jumblr_zgetoperationresult(ptr->opid)); | |
403 | ptr->status = -1; | |
404 | } | |
405 | } | |
406 | } | |
407 | free_json(retjson); | |
408 | } | |
409 | free(retstr); | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
414 | void jumblr_prune(struct jumblr_item *ptr) | |
415 | { | |
416 | struct jumblr_item *tmp; char oldsrc[128]; int32_t flag = 1; | |
417 | printf("jumblr_prune %s\n",ptr->opid); | |
418 | strcpy(oldsrc,ptr->src); | |
419 | free(jumblr_zgetoperationresult(ptr->opid)); | |
420 | while ( flag != 0 ) | |
421 | { | |
422 | flag = 0; | |
423 | HASH_ITER(hh,Jumblrs,ptr,tmp) | |
424 | { | |
425 | if ( strcmp(oldsrc,ptr->dest) == 0 ) | |
426 | { | |
427 | printf("jumblr_prune %s (%s -> %s) matched oldsrc\n",ptr->opid,ptr->src,ptr->dest); | |
428 | free(jumblr_zgetoperationresult(ptr->opid)); | |
429 | strcpy(oldsrc,ptr->src); | |
430 | flag = 1; | |
431 | break; | |
432 | } | |
433 | } | |
434 | } | |
435 | } | |
436 | ||
437 | void jumblr_opidsupdate() | |
438 | { | |
439 | char *retstr; cJSON *array; int32_t i,n; struct jumblr_item *ptr; | |
440 | if ( (retstr= jumblr_zlistoperationids()) != 0 ) | |
441 | { | |
442 | if ( (array= cJSON_Parse(retstr)) != 0 ) | |
443 | { | |
b487a8fe | 444 | if ( (n= cJSON_GetArraySize(array)) > 0 && is_cJSON_Array(array) != 0 ) |
eeaaf554 | 445 | { |
8ac54273 | 446 | printf("%s -> n%d\n",retstr,n); |
eeaaf554 | 447 | for (i=0; i<n; i++) |
448 | { | |
449 | if ( (ptr= jumblr_opidadd(jstri(array,i))) != 0 ) | |
450 | { | |
451 | if ( ptr->status == 0 ) | |
452 | jumblr_opidupdate(ptr); | |
453 | //printf("%d: %s -> %s %.8f\n",ptr->status,ptr->src,ptr->dest,dstr(ptr->amount)); | |
454 | if ( jumblr_addresstype(ptr->src) == 'z' && jumblr_addresstype(ptr->dest) == 't' ) | |
455 | jumblr_prune(ptr); | |
456 | } | |
457 | } | |
458 | } | |
459 | free_json(array); | |
460 | } | |
461 | free(retstr); | |
462 | } | |
463 | } | |
464 | ||
465 | void jumblr_iteration() | |
466 | { | |
467 | static int32_t lastheight; | |
9bbeff91 | 468 | char *zaddr,*retstr,secretaddr[64]; int32_t iter,height,counter,chosen_one,n; uint64_t amount=0,total=0; double fee; struct jumblr_item *ptr,*tmp; uint8_t r,s; |
eeaaf554 | 469 | height = (int32_t)chainActive.Tip()->nHeight; |
470 | if ( lastheight == height ) | |
471 | return; | |
472 | if ( (height % JUMBLR_SYNCHRONIZED_BLOCKS) != 0 ) | |
473 | return; | |
474 | fee = JUMBLR_INCR * JUMBLR_FEE; | |
475 | OS_randombytes(&r,sizeof(r)); | |
476 | s = ((r >> 2) % 3); | |
477 | switch ( s ) | |
478 | { | |
479 | case 0: // public -> z, need to importprivkey | |
480 | if ( Jumblr_deposit[0] != 0 && (total= jumblr_balance(Jumblr_deposit)) >= (JUMBLR_INCR + 3*(fee+JUMBLR_TXFEE))*SATOSHIDEN ) | |
481 | { | |
482 | if ( (zaddr= jumblr_zgetnewaddress()) != 0 ) | |
483 | { | |
484 | amount = 0; | |
485 | if ( (height % (JUMBLR_SYNCHRONIZED_BLOCKS*JUMBLR_SYNCHRONIZED_BLOCKS)) == 0 && total >= SATOSHIDEN * ((JUMBLR_INCR + 3*fee)*100 + 3*JUMBLR_TXFEE) ) | |
486 | amount = SATOSHIDEN * ((JUMBLR_INCR + 3*fee)*100 + 3*JUMBLR_TXFEE); | |
487 | else if ( (r & 3) == 0 && total >= SATOSHIDEN * ((JUMBLR_INCR + 3*fee)*10 + 3*JUMBLR_TXFEE) ) | |
488 | amount = SATOSHIDEN * ((JUMBLR_INCR + 3*fee)*10 + 3*JUMBLR_TXFEE); | |
489 | else amount = SATOSHIDEN * ((JUMBLR_INCR + 3*fee) + 3*JUMBLR_TXFEE); | |
490 | if ( amount > 0 && (retstr= jumblr_sendt_to_z(Jumblr_deposit,zaddr,dstr(amount))) != 0 ) | |
491 | { | |
492 | printf("sendt_to_z.(%s)\n",retstr); | |
493 | free(retstr); | |
494 | } | |
495 | free(zaddr); | |
496 | } else printf("no zaddr from jumblr_zgetnewaddress\n"); | |
497 | } | |
498 | else if ( Jumblr_deposit[0] != 0 ) | |
499 | printf("%s total %.8f vs %.8f\n",Jumblr_deposit,dstr(total),(JUMBLR_INCR + 3*(fee+JUMBLR_TXFEE))); | |
500 | break; | |
501 | case 1: // z -> z | |
502 | jumblr_opidsupdate(); | |
503 | chosen_one = -1; | |
504 | for (iter=counter=0; iter<2; iter++) | |
505 | { | |
506 | counter = n = 0; | |
507 | HASH_ITER(hh,Jumblrs,ptr,tmp) | |
508 | { | |
509 | if ( jumblr_addresstype(ptr->src) == 't' && jumblr_addresstype(ptr->dest) == 'z' ) | |
510 | { | |
511 | if ( ptr->spent == 0 && (total= jumblr_balance(ptr->dest)) >= (fee + JUMBLR_FEE)*SATOSHIDEN ) | |
512 | { | |
513 | if ( iter == 1 && counter == chosen_one ) | |
514 | { | |
515 | if ( (zaddr= jumblr_zgetnewaddress()) != 0 ) | |
516 | { | |
517 | if ( (retstr= jumblr_sendz_to_z(ptr->dest,zaddr,dstr(total))) != 0 ) | |
518 | { | |
519 | printf("n.%d counter.%d chosen_one.%d sendz_to_z.(%s)\n",n,counter,chosen_one,retstr); | |
520 | free(retstr); | |
521 | } | |
522 | ptr->spent = (uint32_t)time(NULL); | |
523 | free(zaddr); | |
524 | break; | |
525 | } | |
526 | } | |
527 | counter++; | |
528 | } | |
529 | } | |
530 | n++; | |
531 | } | |
532 | if ( counter == 0 ) | |
533 | break; | |
534 | if ( iter == 0 ) | |
535 | { | |
536 | OS_randombytes((uint8_t *)&chosen_one,sizeof(chosen_one)); | |
537 | if ( chosen_one < 0 ) | |
538 | chosen_one = -chosen_one; | |
539 | chosen_one %= counter; | |
540 | printf("jumblr z->z chosen_one.%d of %d, from %d\n",chosen_one,counter,n); | |
541 | } | |
542 | } | |
543 | break; | |
544 | case 2: // z -> public | |
545 | if ( Jumblr_numsecretaddrs > 0 ) | |
546 | { | |
547 | jumblr_opidsupdate(); | |
548 | chosen_one = -1; | |
549 | for (iter=0; iter<2; iter++) | |
550 | { | |
551 | counter = n = 0; | |
552 | HASH_ITER(hh,Jumblrs,ptr,tmp) | |
553 | { | |
554 | if ( jumblr_addresstype(ptr->src) == 'z' && jumblr_addresstype(ptr->dest) == 'z' ) | |
555 | { | |
556 | if ( ptr->spent == 0 && (total= jumblr_balance(ptr->dest)) >= (fee + JUMBLR_FEE)*SATOSHIDEN ) | |
557 | { | |
558 | if ( iter == 1 && n == chosen_one ) | |
559 | { | |
560 | Jumblr_secretaddr(secretaddr); | |
561 | if ( (retstr= jumblr_sendz_to_t(ptr->dest,secretaddr,dstr(total))) != 0 ) | |
562 | { | |
563 | printf("sendz_to_t.(%s)\n",retstr); | |
564 | free(retstr); | |
565 | } | |
566 | ptr->spent = (uint32_t)time(NULL); | |
567 | break; | |
568 | } | |
569 | counter++; | |
570 | } | |
571 | } | |
572 | n++; | |
573 | } | |
574 | if ( counter == 0 ) | |
575 | break; | |
576 | if ( iter == 0 ) | |
577 | { | |
578 | OS_randombytes((uint8_t *)&chosen_one,sizeof(chosen_one)); | |
579 | if ( chosen_one < 0 ) | |
580 | chosen_one = -chosen_one; | |
581 | chosen_one %= counter; | |
582 | printf("jumblr z->t chosen_one.%d of %d, from %d\n",chosen_one,counter,n); | |
583 | } | |
584 | } | |
585 | } | |
586 | break; | |
587 | } | |
588 | } | |
589 |