]> Git Repo - VerusCoin.git/blob - src/komodo_utils.h
Enable time locked coin bases to be used as normal coinbase transactions with longer...
[VerusCoin.git] / src / komodo_utils.h
1 /******************************************************************************
2  * Copyright © 2014-2018 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 #include "komodo_defs.h"
16 #include <string.h>
17
18 #ifdef _WIN32
19 #include <sodium.h>
20 #include <boost/date_time/posix_time/posix_time.hpp>
21 #include <boost/thread.hpp>
22 #endif
23
24 #define SATOSHIDEN ((uint64_t)100000000L)
25 #define dstr(x) ((double)(x) / SATOSHIDEN)
26 #define portable_mutex_t pthread_mutex_t
27 #define portable_mutex_init(ptr) pthread_mutex_init(ptr,NULL)
28 #define portable_mutex_lock pthread_mutex_lock
29 #define portable_mutex_unlock pthread_mutex_unlock
30
31 struct allocitem { uint32_t allocsize,type; };
32 struct queueitem { struct queueitem *next,*prev; uint32_t allocsize,type;  };
33
34 typedef struct queue
35 {
36         struct queueitem *list;
37         pthread_mutex_t mutex;
38     char name[64],initflag;
39 } queue_t;
40
41 #include "mini-gmp.c"
42
43 #define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9"
44 #define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA"
45 #define CRYPTO777_RMD160STR "f1dce4182fce875748c4986b240ff7d7bc3fffb0"
46
47 #define KOMODO_PUBTYPE 60
48
49 struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; };
50 struct rmd160_vstate { uint64_t length; uint8_t buf[64]; uint32_t curlen, state[5]; };
51
52 // following is ported from libtom
53
54 #define STORE32L(x, y)                                                                     \
55 { (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255);   \
56 (y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
57
58 #define LOAD32L(x, y)                            \
59 { x = (uint32_t)(((uint64_t)((y)[3] & 255)<<24) | \
60 ((uint32_t)((y)[2] & 255)<<16) | \
61 ((uint32_t)((y)[1] & 255)<<8)  | \
62 ((uint32_t)((y)[0] & 255))); }
63
64 #define STORE64L(x, y)                                                                     \
65 { (y)[7] = (uint8_t)(((x)>>56)&255); (y)[6] = (uint8_t)(((x)>>48)&255);   \
66 (y)[5] = (uint8_t)(((x)>>40)&255); (y)[4] = (uint8_t)(((x)>>32)&255);   \
67 (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255);   \
68 (y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
69
70 #define LOAD64L(x, y)                                                       \
71 { x = (((uint64_t)((y)[7] & 255))<<56)|(((uint64_t)((y)[6] & 255))<<48)| \
72 (((uint64_t)((y)[5] & 255))<<40)|(((uint64_t)((y)[4] & 255))<<32)| \
73 (((uint64_t)((y)[3] & 255))<<24)|(((uint64_t)((y)[2] & 255))<<16)| \
74 (((uint64_t)((y)[1] & 255))<<8)|(((uint64_t)((y)[0] & 255))); }
75
76 #define STORE32H(x, y)                                                                     \
77 { (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255);   \
78 (y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); }
79
80 #define LOAD32H(x, y)                            \
81 { x = (uint32_t)(((uint64_t)((y)[0] & 255)<<24) | \
82 ((uint32_t)((y)[1] & 255)<<16) | \
83 ((uint32_t)((y)[2] & 255)<<8)  | \
84 ((uint32_t)((y)[3] & 255))); }
85
86 #define STORE64H(x, y)                                                                     \
87 { (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255);     \
88 (y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255);     \
89 (y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255);     \
90 (y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
91
92 #define LOAD64H(x, y)                                                      \
93 { x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \
94 (((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \
95 (((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \
96 (((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); }
97
98 // Various logical functions
99 #define RORc(x, y) ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL)
100 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
101 #define Maj(x,y,z)      (((x | y) & z) | (x & y))
102 #define S(x, n)         RORc((x),(n))
103 #define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
104 #define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
105 #define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
106 #define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
107 #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
108 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
109
110 static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf)
111 {
112     uint32_t S[8],W[64],t0,t1,i;
113     for (i=0; i<8; i++) // copy state into S
114         S[i] = md->state[i];
115     for (i=0; i<16; i++) // copy the state into 512-bits into W[0..15]
116         LOAD32H(W[i],buf + (4*i));
117     for (i=16; i<64; i++) // fill W[16..63]
118         W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
119
120 #define RND(a,b,c,d,e,f,g,h,i,ki)                    \
121 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i];   \
122 t1 = Sigma0(a) + Maj(a, b, c);                  \
123 d += t0;                                        \
124 h  = t0 + t1;
125
126     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
127     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
128     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
129     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
130     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
131     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
132     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
133     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
134     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
135     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
136     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
137     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
138     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
139     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
140     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
141     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
142     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
143     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
144     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
145     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
146     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
147     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
148     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
149     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
150     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
151     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
152     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
153     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
154     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
155     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
156     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
157     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
158     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
159     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
160     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
161     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
162     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
163     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
164     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
165     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
166     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
167     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
168     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
169     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
170     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
171     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
172     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
173     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
174     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
175     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
176     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
177     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
178     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
179     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
180     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
181     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
182     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
183     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
184     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
185     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
186     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
187     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
188     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
189     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
190 #undef RND
191     for (i=0; i<8; i++) // feedback
192         md->state[i] = md->state[i] + S[i];
193     return(0);
194 }
195
196 #undef RORc
197 #undef Ch
198 #undef Maj
199 #undef S
200 #undef R
201 #undef Sigma0
202 #undef Sigma1
203 #undef Gamma0
204 #undef Gamma1
205
206 static inline void sha256_vinit(struct sha256_vstate * md)
207 {
208     md->curlen = 0;
209     md->length = 0;
210     md->state[0] = 0x6A09E667UL;
211     md->state[1] = 0xBB67AE85UL;
212     md->state[2] = 0x3C6EF372UL;
213     md->state[3] = 0xA54FF53AUL;
214     md->state[4] = 0x510E527FUL;
215     md->state[5] = 0x9B05688CUL;
216     md->state[6] = 0x1F83D9ABUL;
217     md->state[7] = 0x5BE0CD19UL;
218 }
219
220 static inline int32_t sha256_vprocess(struct sha256_vstate *md,const uint8_t *in,uint64_t inlen)
221 {
222     uint64_t n; int32_t err;
223     if ( md->curlen > sizeof(md->buf) )
224         return(-1);
225     while ( inlen > 0 )
226     {
227         if ( md->curlen == 0 && inlen >= 64 )
228         {
229             if ( (err= sha256_vcompress(md,(uint8_t *)in)) != 0 )
230                 return(err);
231             md->length += 64 * 8, in += 64, inlen -= 64;
232         }
233         else
234         {
235             n = MIN(inlen,64 - md->curlen);
236             memcpy(md->buf + md->curlen,in,(size_t)n);
237             md->curlen += n, in += n, inlen -= n;
238             if ( md->curlen == 64 )
239             {
240                 if ( (err= sha256_vcompress(md,md->buf)) != 0 )
241                     return(err);
242                 md->length += 8*64;
243                 md->curlen = 0;
244             }
245         }
246     }
247     return(0);
248 }
249
250 static inline int32_t sha256_vdone(struct sha256_vstate *md,uint8_t *out)
251 {
252     int32_t i;
253     if ( md->curlen >= sizeof(md->buf) )
254         return(-1);
255     md->length += md->curlen * 8; // increase the length of the message
256     md->buf[md->curlen++] = (uint8_t)0x80; // append the '1' bit
257     // if len > 56 bytes we append zeros then compress.  Then we can fall back to padding zeros and length encoding like normal.
258     if ( md->curlen > 56 )
259     {
260         while ( md->curlen < 64 )
261             md->buf[md->curlen++] = (uint8_t)0;
262         sha256_vcompress(md,md->buf);
263         md->curlen = 0;
264     }
265     while ( md->curlen < 56 ) // pad upto 56 bytes of zeroes
266         md->buf[md->curlen++] = (uint8_t)0;
267     STORE64H(md->length,md->buf+56); // store length
268     sha256_vcompress(md,md->buf);
269     for (i=0; i<8; i++) // copy output
270         STORE32H(md->state[i],out+(4*i));
271     return(0);
272 }
273
274 void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len)
275 {
276     struct sha256_vstate md;
277     sha256_vinit(&md);
278     sha256_vprocess(&md,src,len);
279     sha256_vdone(&md,hash);
280 }
281
282 bits256 bits256_doublesha256(char *deprecated,uint8_t *data,int32_t datalen)
283 {
284     bits256 hash,hash2; int32_t i;
285     vcalc_sha256(0,hash.bytes,data,datalen);
286     vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash));
287     for (i=0; i<sizeof(hash); i++)
288         hash.bytes[i] = hash2.bytes[sizeof(hash) - 1 - i];
289     return(hash);
290 }
291
292
293 // rmd160: the five basic functions F(), G() and H()
294 #define F(x, y, z)        ((x) ^ (y) ^ (z))
295 #define G(x, y, z)        (((x) & (y)) | (~(x) & (z)))
296 #define H(x, y, z)        (((x) | ~(y)) ^ (z))
297 #define I(x, y, z)        (((x) & (z)) | ((y) & ~(z)))
298 #define J(x, y, z)        ((x) ^ ((y) | ~(z)))
299 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
300
301 /* the ten basic operations FF() through III() */
302 #define FF(a, b, c, d, e, x, s)        \
303 (a) += F((b), (c), (d)) + (x);\
304 (a) = ROLc((a), (s)) + (e);\
305 (c) = ROLc((c), 10);
306
307 #define GG(a, b, c, d, e, x, s)        \
308 (a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
309 (a) = ROLc((a), (s)) + (e);\
310 (c) = ROLc((c), 10);
311
312 #define HH(a, b, c, d, e, x, s)        \
313 (a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
314 (a) = ROLc((a), (s)) + (e);\
315 (c) = ROLc((c), 10);
316
317 #define II(a, b, c, d, e, x, s)        \
318 (a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
319 (a) = ROLc((a), (s)) + (e);\
320 (c) = ROLc((c), 10);
321
322 #define JJ(a, b, c, d, e, x, s)        \
323 (a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
324 (a) = ROLc((a), (s)) + (e);\
325 (c) = ROLc((c), 10);
326
327 #define FFF(a, b, c, d, e, x, s)        \
328 (a) += F((b), (c), (d)) + (x);\
329 (a) = ROLc((a), (s)) + (e);\
330 (c) = ROLc((c), 10);
331
332 #define GGG(a, b, c, d, e, x, s)        \
333 (a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
334 (a) = ROLc((a), (s)) + (e);\
335 (c) = ROLc((c), 10);
336
337 #define HHH(a, b, c, d, e, x, s)        \
338 (a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
339 (a) = ROLc((a), (s)) + (e);\
340 (c) = ROLc((c), 10);
341
342 #define III(a, b, c, d, e, x, s)        \
343 (a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
344 (a) = ROLc((a), (s)) + (e);\
345 (c) = ROLc((c), 10);
346
347 #define JJJ(a, b, c, d, e, x, s)        \
348 (a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
349 (a) = ROLc((a), (s)) + (e);\
350 (c) = ROLc((c), 10);
351
352 static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf)
353 {
354     uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16];
355     int i;
356
357     /* load words X */
358     for (i = 0; i < 16; i++){
359         LOAD32L(X[i], buf + (4 * i));
360     }
361
362     /* load state */
363     aa = aaa = md->state[0];
364     bb = bbb = md->state[1];
365     cc = ccc = md->state[2];
366     dd = ddd = md->state[3];
367     ee = eee = md->state[4];
368
369     /* round 1 */
370     FF(aa, bb, cc, dd, ee, X[ 0], 11);
371     FF(ee, aa, bb, cc, dd, X[ 1], 14);
372     FF(dd, ee, aa, bb, cc, X[ 2], 15);
373     FF(cc, dd, ee, aa, bb, X[ 3], 12);
374     FF(bb, cc, dd, ee, aa, X[ 4],  5);
375     FF(aa, bb, cc, dd, ee, X[ 5],  8);
376     FF(ee, aa, bb, cc, dd, X[ 6],  7);
377     FF(dd, ee, aa, bb, cc, X[ 7],  9);
378     FF(cc, dd, ee, aa, bb, X[ 8], 11);
379     FF(bb, cc, dd, ee, aa, X[ 9], 13);
380     FF(aa, bb, cc, dd, ee, X[10], 14);
381     FF(ee, aa, bb, cc, dd, X[11], 15);
382     FF(dd, ee, aa, bb, cc, X[12],  6);
383     FF(cc, dd, ee, aa, bb, X[13],  7);
384     FF(bb, cc, dd, ee, aa, X[14],  9);
385     FF(aa, bb, cc, dd, ee, X[15],  8);
386
387     /* round 2 */
388     GG(ee, aa, bb, cc, dd, X[ 7],  7);
389     GG(dd, ee, aa, bb, cc, X[ 4],  6);
390     GG(cc, dd, ee, aa, bb, X[13],  8);
391     GG(bb, cc, dd, ee, aa, X[ 1], 13);
392     GG(aa, bb, cc, dd, ee, X[10], 11);
393     GG(ee, aa, bb, cc, dd, X[ 6],  9);
394     GG(dd, ee, aa, bb, cc, X[15],  7);
395     GG(cc, dd, ee, aa, bb, X[ 3], 15);
396     GG(bb, cc, dd, ee, aa, X[12],  7);
397     GG(aa, bb, cc, dd, ee, X[ 0], 12);
398     GG(ee, aa, bb, cc, dd, X[ 9], 15);
399     GG(dd, ee, aa, bb, cc, X[ 5],  9);
400     GG(cc, dd, ee, aa, bb, X[ 2], 11);
401     GG(bb, cc, dd, ee, aa, X[14],  7);
402     GG(aa, bb, cc, dd, ee, X[11], 13);
403     GG(ee, aa, bb, cc, dd, X[ 8], 12);
404
405     /* round 3 */
406     HH(dd, ee, aa, bb, cc, X[ 3], 11);
407     HH(cc, dd, ee, aa, bb, X[10], 13);
408     HH(bb, cc, dd, ee, aa, X[14],  6);
409     HH(aa, bb, cc, dd, ee, X[ 4],  7);
410     HH(ee, aa, bb, cc, dd, X[ 9], 14);
411     HH(dd, ee, aa, bb, cc, X[15],  9);
412     HH(cc, dd, ee, aa, bb, X[ 8], 13);
413     HH(bb, cc, dd, ee, aa, X[ 1], 15);
414     HH(aa, bb, cc, dd, ee, X[ 2], 14);
415     HH(ee, aa, bb, cc, dd, X[ 7],  8);
416     HH(dd, ee, aa, bb, cc, X[ 0], 13);
417     HH(cc, dd, ee, aa, bb, X[ 6],  6);
418     HH(bb, cc, dd, ee, aa, X[13],  5);
419     HH(aa, bb, cc, dd, ee, X[11], 12);
420     HH(ee, aa, bb, cc, dd, X[ 5],  7);
421     HH(dd, ee, aa, bb, cc, X[12],  5);
422
423     /* round 4 */
424     II(cc, dd, ee, aa, bb, X[ 1], 11);
425     II(bb, cc, dd, ee, aa, X[ 9], 12);
426     II(aa, bb, cc, dd, ee, X[11], 14);
427     II(ee, aa, bb, cc, dd, X[10], 15);
428     II(dd, ee, aa, bb, cc, X[ 0], 14);
429     II(cc, dd, ee, aa, bb, X[ 8], 15);
430     II(bb, cc, dd, ee, aa, X[12],  9);
431     II(aa, bb, cc, dd, ee, X[ 4],  8);
432     II(ee, aa, bb, cc, dd, X[13],  9);
433     II(dd, ee, aa, bb, cc, X[ 3], 14);
434     II(cc, dd, ee, aa, bb, X[ 7],  5);
435     II(bb, cc, dd, ee, aa, X[15],  6);
436     II(aa, bb, cc, dd, ee, X[14],  8);
437     II(ee, aa, bb, cc, dd, X[ 5],  6);
438     II(dd, ee, aa, bb, cc, X[ 6],  5);
439     II(cc, dd, ee, aa, bb, X[ 2], 12);
440
441     /* round 5 */
442     JJ(bb, cc, dd, ee, aa, X[ 4],  9);
443     JJ(aa, bb, cc, dd, ee, X[ 0], 15);
444     JJ(ee, aa, bb, cc, dd, X[ 5],  5);
445     JJ(dd, ee, aa, bb, cc, X[ 9], 11);
446     JJ(cc, dd, ee, aa, bb, X[ 7],  6);
447     JJ(bb, cc, dd, ee, aa, X[12],  8);
448     JJ(aa, bb, cc, dd, ee, X[ 2], 13);
449     JJ(ee, aa, bb, cc, dd, X[10], 12);
450     JJ(dd, ee, aa, bb, cc, X[14],  5);
451     JJ(cc, dd, ee, aa, bb, X[ 1], 12);
452     JJ(bb, cc, dd, ee, aa, X[ 3], 13);
453     JJ(aa, bb, cc, dd, ee, X[ 8], 14);
454     JJ(ee, aa, bb, cc, dd, X[11], 11);
455     JJ(dd, ee, aa, bb, cc, X[ 6],  8);
456     JJ(cc, dd, ee, aa, bb, X[15],  5);
457     JJ(bb, cc, dd, ee, aa, X[13],  6);
458
459     /* parallel round 1 */
460     JJJ(aaa, bbb, ccc, ddd, eee, X[ 5],  8);
461     JJJ(eee, aaa, bbb, ccc, ddd, X[14],  9);
462     JJJ(ddd, eee, aaa, bbb, ccc, X[ 7],  9);
463     JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
464     JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
465     JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
466     JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
467     JJJ(ddd, eee, aaa, bbb, ccc, X[ 4],  5);
468     JJJ(ccc, ddd, eee, aaa, bbb, X[13],  7);
469     JJJ(bbb, ccc, ddd, eee, aaa, X[ 6],  7);
470     JJJ(aaa, bbb, ccc, ddd, eee, X[15],  8);
471     JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
472     JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
473     JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
474     JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
475     JJJ(aaa, bbb, ccc, ddd, eee, X[12],  6);
476
477     /* parallel round 2 */
478     III(eee, aaa, bbb, ccc, ddd, X[ 6],  9);
479     III(ddd, eee, aaa, bbb, ccc, X[11], 13);
480     III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
481     III(bbb, ccc, ddd, eee, aaa, X[ 7],  7);
482     III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
483     III(eee, aaa, bbb, ccc, ddd, X[13],  8);
484     III(ddd, eee, aaa, bbb, ccc, X[ 5],  9);
485     III(ccc, ddd, eee, aaa, bbb, X[10], 11);
486     III(bbb, ccc, ddd, eee, aaa, X[14],  7);
487     III(aaa, bbb, ccc, ddd, eee, X[15],  7);
488     III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
489     III(ddd, eee, aaa, bbb, ccc, X[12],  7);
490     III(ccc, ddd, eee, aaa, bbb, X[ 4],  6);
491     III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
492     III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
493     III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
494
495     /* parallel round 3 */
496     HHH(ddd, eee, aaa, bbb, ccc, X[15],  9);
497     HHH(ccc, ddd, eee, aaa, bbb, X[ 5],  7);
498     HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
499     HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
500     HHH(eee, aaa, bbb, ccc, ddd, X[ 7],  8);
501     HHH(ddd, eee, aaa, bbb, ccc, X[14],  6);
502     HHH(ccc, ddd, eee, aaa, bbb, X[ 6],  6);
503     HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
504     HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
505     HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
506     HHH(ddd, eee, aaa, bbb, ccc, X[12],  5);
507     HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
508     HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
509     HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
510     HHH(eee, aaa, bbb, ccc, ddd, X[ 4],  7);
511     HHH(ddd, eee, aaa, bbb, ccc, X[13],  5);
512
513     /* parallel round 4 */
514     GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
515     GGG(bbb, ccc, ddd, eee, aaa, X[ 6],  5);
516     GGG(aaa, bbb, ccc, ddd, eee, X[ 4],  8);
517     GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
518     GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
519     GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
520     GGG(bbb, ccc, ddd, eee, aaa, X[15],  6);
521     GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
522     GGG(eee, aaa, bbb, ccc, ddd, X[ 5],  6);
523     GGG(ddd, eee, aaa, bbb, ccc, X[12],  9);
524     GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
525     GGG(bbb, ccc, ddd, eee, aaa, X[13],  9);
526     GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
527     GGG(eee, aaa, bbb, ccc, ddd, X[ 7],  5);
528     GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
529     GGG(ccc, ddd, eee, aaa, bbb, X[14],  8);
530
531     /* parallel round 5 */
532     FFF(bbb, ccc, ddd, eee, aaa, X[12] ,  8);
533     FFF(aaa, bbb, ccc, ddd, eee, X[15] ,  5);
534     FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
535     FFF(ddd, eee, aaa, bbb, ccc, X[ 4] ,  9);
536     FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
537     FFF(bbb, ccc, ddd, eee, aaa, X[ 5] ,  5);
538     FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
539     FFF(eee, aaa, bbb, ccc, ddd, X[ 7] ,  6);
540     FFF(ddd, eee, aaa, bbb, ccc, X[ 6] ,  8);
541     FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
542     FFF(bbb, ccc, ddd, eee, aaa, X[13] ,  6);
543     FFF(aaa, bbb, ccc, ddd, eee, X[14] ,  5);
544     FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
545     FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
546     FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
547     FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
548
549     /* combine results */
550     ddd += cc + md->state[1];               /* final result for md->state[0] */
551     md->state[1] = md->state[2] + dd + eee;
552     md->state[2] = md->state[3] + ee + aaa;
553     md->state[3] = md->state[4] + aa + bbb;
554     md->state[4] = md->state[0] + bb + ccc;
555     md->state[0] = ddd;
556
557     return 0;
558 }
559
560 /**
561  Initialize the hash state
562  @param md   The hash state you wish to initialize
563  @return 0 if successful
564  */
565 int rmd160_vinit(struct rmd160_vstate * md)
566 {
567     md->state[0] = 0x67452301UL;
568     md->state[1] = 0xefcdab89UL;
569     md->state[2] = 0x98badcfeUL;
570     md->state[3] = 0x10325476UL;
571     md->state[4] = 0xc3d2e1f0UL;
572     md->curlen   = 0;
573     md->length   = 0;
574     return 0;
575 }
576 #define HASH_PROCESS(func_name, compress_name, state_var, block_size)                       \
577 int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen)               \
578 {                                                                                           \
579 unsigned long n;                                                                        \
580 int           err;                                                                      \
581 if (md->curlen > sizeof(md->buf)) {                             \
582 return -1;                                                            \
583 }                                                                                       \
584 while (inlen > 0) {                                                                     \
585 if (md->curlen == 0 && inlen >= block_size) {                           \
586 if ((err = compress_name (md, (unsigned char *)in)) != 0) {               \
587 return err;                                                                   \
588 }                                                                                \
589 md->length += block_size * 8;                                        \
590 in             += block_size;                                                    \
591 inlen          -= block_size;                                                    \
592 } else {                                                                            \
593 n = MIN(inlen, (block_size - md->curlen));                           \
594 memcpy(md->buf + md->curlen, in, (size_t)n);              \
595 md->curlen += n;                                                     \
596 in             += n;                                                             \
597 inlen          -= n;                                                             \
598 if (md->curlen == block_size) {                                      \
599 if ((err = compress_name (md, md->buf)) != 0) {            \
600 return err;                                                                \
601 }                                                                             \
602 md->length += 8*block_size;                                       \
603 md->curlen = 0;                                                   \
604 }                                                                                \
605 }                                                                                    \
606 }                                                                                       \
607 return 0;                                                                        \
608 }
609
610 /**
611  Process a block of memory though the hash
612  @param md     The hash state
613  @param in     The data to hash
614  @param inlen  The length of the data (octets)
615  @return 0 if successful
616  */
617 HASH_PROCESS(rmd160_vprocess, rmd160_vcompress, rmd160, 64)
618
619 /**
620  Terminate the hash to get the digest
621  @param md  The hash state
622  @param out [out] The destination of the hash (20 bytes)
623  @return 0 if successful
624  */
625 int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out)
626 {
627     int i;
628     if (md->curlen >= sizeof(md->buf)) {
629         return -1;
630     }
631     /* increase the length of the message */
632     md->length += md->curlen * 8;
633
634     /* append the '1' bit */
635     md->buf[md->curlen++] = (unsigned char)0x80;
636
637     /* if the length is currently above 56 bytes we append zeros
638      * then compress.  Then we can fall back to padding zeros and length
639      * encoding like normal.
640      */
641     if (md->curlen > 56) {
642         while (md->curlen < 64) {
643             md->buf[md->curlen++] = (unsigned char)0;
644         }
645         rmd160_vcompress(md, md->buf);
646         md->curlen = 0;
647     }
648     /* pad upto 56 bytes of zeroes */
649     while (md->curlen < 56) {
650         md->buf[md->curlen++] = (unsigned char)0;
651     }
652     /* store length */
653     STORE64L(md->length, md->buf+56);
654     rmd160_vcompress(md, md->buf);
655     /* copy output */
656     for (i = 0; i < 5; i++) {
657         STORE32L(md->state[i], out+(4*i));
658     }
659     return 0;
660 }
661
662 void calc_rmd160(char deprecated[41],uint8_t buf[20],uint8_t *msg,int32_t len)
663 {
664     struct rmd160_vstate md;
665     rmd160_vinit(&md);
666     rmd160_vprocess(&md,msg,len);
667     rmd160_vdone(&md, buf);
668 }
669 #undef F
670 #undef G
671 #undef H
672 #undef I
673 #undef J
674 #undef ROLc
675 #undef FF
676 #undef GG
677 #undef HH
678 #undef II
679 #undef JJ
680 #undef FFF
681 #undef GGG
682 #undef HHH
683 #undef III
684 #undef JJJ
685
686 static const uint32_t crc32_tab[] = {
687         0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
688         0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
689         0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
690         0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
691         0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
692         0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
693         0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
694         0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
695         0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
696         0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
697         0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
698         0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
699         0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
700         0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
701         0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
702         0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
703         0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
704         0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
705         0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
706         0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
707         0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
708         0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
709         0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
710         0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
711         0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
712         0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
713         0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
714         0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
715         0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
716         0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
717         0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
718         0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
719         0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
720         0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
721         0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
722         0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
723         0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
724         0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
725         0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
726         0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
727         0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
728         0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
729         0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
730 };
731
732 uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size)
733 {
734         const uint8_t *p;
735
736         p = (const uint8_t *)buf;
737         crc = crc ^ ~0U;
738
739         while (size--)
740                 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
741
742         return crc ^ ~0U;
743 }
744
745 void calc_rmd160_sha256(uint8_t rmd160[20],uint8_t *data,int32_t datalen)
746 {
747     bits256 hash;
748     vcalc_sha256(0,hash.bytes,data,datalen);
749     calc_rmd160(0,rmd160,hash.bytes,sizeof(hash));
750 }
751
752 int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr)
753 {
754     bits256 hash; uint8_t *buf,_buf[25]; int32_t len;
755     memset(rmd160,0,20);
756     *addrtypep = 0;
757     buf = _buf;
758     if ( (len= bitcoin_base58decode(buf,coinaddr)) >= 4 )
759     {
760         // validate with trailing hash, then remove hash
761         hash = bits256_doublesha256(0,buf,21);
762         *addrtypep = *buf;
763         memcpy(rmd160,buf+1,20);
764         if ( (buf[21]&0xff) == hash.bytes[31] && (buf[22]&0xff) == hash.bytes[30] &&(buf[23]&0xff) == hash.bytes[29] && (buf[24]&0xff) == hash.bytes[28] )
765         {
766             //printf("coinaddr.(%s) valid checksum addrtype.%02x\n",coinaddr,*addrtypep);
767             return(20);
768         }
769         else
770         {
771             int32_t i;
772             if ( len > 20 )
773             {
774                 hash = bits256_doublesha256(0,buf,len);
775             }
776             for (i=0; i<len; i++)
777                 printf("%02x ",buf[i]);
778             printf("\nhex checkhash.(%s) len.%d mismatch %02x %02x %02x %02x vs %02x %02x %02x %02x\n",coinaddr,len,buf[len-1]&0xff,buf[len-2]&0xff,buf[len-3]&0xff,buf[len-4]&0xff,hash.bytes[31],hash.bytes[30],hash.bytes[29],hash.bytes[28]);
779         }
780     }
781         return(0);
782 }
783
784 char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len)
785 {
786     int32_t i; uint8_t data[25]; bits256 hash;// char checkaddr[65];
787     if ( len != 20 )
788         calc_rmd160_sha256(data+1,pubkey_or_rmd160,len);
789     else memcpy(data+1,pubkey_or_rmd160,20);
790     //btc_convrmd160(checkaddr,addrtype,data+1);
791     data[0] = addrtype;
792     hash = bits256_doublesha256(0,data,21);
793     for (i=0; i<4; i++)
794         data[21+i] = hash.bytes[31-i];
795     if ( (coinaddr= bitcoin_base58encode(coinaddr,data,25)) != 0 )
796     {
797         //uint8_t checktype,rmd160[20];
798         //bitcoin_addr2rmd160(&checktype,rmd160,coinaddr);
799         //if ( strcmp(checkaddr,coinaddr) != 0 )
800         //    printf("checkaddr.(%s) vs coinaddr.(%s) %02x vs [%02x] memcmp.%d\n",checkaddr,coinaddr,addrtype,checktype,memcmp(rmd160,data+1,20));
801     }
802     return(coinaddr);
803 }
804
805 int32_t komodo_is_issuer()
806 {
807     if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) >= 0 )
808         return(1);
809     else return(0);
810 }
811
812 int32_t bitweight(uint64_t x)
813 {
814     int i,wt = 0;
815     for (i=0; i<64; i++)
816         if ( (1LL << i) & x )
817             wt++;
818     return(wt);
819 }
820
821 int32_t _unhex(char c)
822 {
823     if ( c >= '0' && c <= '9' )
824         return(c - '0');
825     else if ( c >= 'a' && c <= 'f' )
826         return(c - 'a' + 10);
827     else if ( c >= 'A' && c <= 'F' )
828         return(c - 'A' + 10);
829     return(-1);
830 }
831
832 int32_t is_hexstr(char *str,int32_t n)
833 {
834     int32_t i;
835     if ( str == 0 || str[0] == 0 )
836         return(0);
837     for (i=0; str[i]!=0; i++)
838     {
839         if ( n > 0 && i >= n )
840             break;
841         if ( _unhex(str[i]) < 0 )
842             break;
843     }
844     if ( n == 0 )
845         return(i);
846     return(i == n);
847 }
848
849 int32_t unhex(char c)
850 {
851     int32_t hex;
852     if ( (hex= _unhex(c)) < 0 )
853     {
854         //printf("unhex: illegal hexchar.(%c)\n",c);
855     }
856     return(hex);
857 }
858
859 unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); }
860
861 int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex)
862 {
863     int32_t adjust,i = 0;
864     //printf("decode.(%s)\n",hex);
865     if ( is_hexstr(hex,n) <= 0 )
866     {
867         memset(bytes,0,n);
868         return(n);
869     }
870     if ( hex[n-1] == '\n' || hex[n-1] == '\r' )
871         hex[--n] = 0;
872     if ( n == 0 || (hex[n*2+1] == 0 && hex[n*2] != 0) )
873     {
874         if ( n > 0 )
875         {
876             bytes[0] = unhex(hex[0]);
877             printf("decode_hex n.%d hex[0] (%c) -> %d hex.(%s) [n*2+1: %d] [n*2: %d %c] len.%ld\n",n,hex[0],bytes[0],hex,hex[n*2+1],hex[n*2],hex[n*2],(long)strlen(hex));
878         }
879         bytes++;
880         hex++;
881         adjust = 1;
882     } else adjust = 0;
883     if ( n > 0 )
884     {
885         for (i=0; i<n; i++)
886             bytes[i] = _decode_hex(&hex[i*2]);
887     }
888     //bytes[i] = 0;
889     return(n + adjust);
890 }
891
892 char hexbyte(int32_t c)
893 {
894     c &= 0xf;
895     if ( c < 10 )
896         return('0'+c);
897     else if ( c < 16 )
898         return('a'+c-10);
899     else return(0);
900 }
901
902 int32_t init_hexbytes_noT(char *hexbytes,unsigned char *message,long len)
903 {
904     int32_t i;
905     if ( len <= 0 )
906     {
907         hexbytes[0] = 0;
908         return(1);
909     }
910     for (i=0; i<len; i++)
911     {
912         hexbytes[i*2] = hexbyte((message[i]>>4) & 0xf);
913         hexbytes[i*2 + 1] = hexbyte(message[i] & 0xf);
914         //printf("i.%d (%02x) [%c%c]\n",i,message[i],hexbytes[i*2],hexbytes[i*2+1]);
915     }
916     hexbytes[len*2] = 0;
917     //printf("len.%ld\n",len*2+1);
918     return((int32_t)len*2+1);
919 }
920
921 char *bits256_str(char hexstr[65],bits256 x)
922 {
923     init_hexbytes_noT(hexstr,x.bytes,sizeof(x));
924     return(hexstr);
925 }
926
927 int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp)
928 {
929     int32_t i; uint64_t x;
930     if ( rwflag == 0 )
931     {
932         x = 0;
933         for (i=len-1; i>=0; i--)
934         {
935             x <<= 8;
936             x |= serialized[i];
937         }
938         switch ( len )
939         {
940             case 1: *(uint8_t *)endianedp = (uint8_t)x; break;
941             case 2: *(uint16_t *)endianedp = (uint16_t)x; break;
942             case 4: *(uint32_t *)endianedp = (uint32_t)x; break;
943             case 8: *(uint64_t *)endianedp = (uint64_t)x; break;
944         }
945     }
946     else
947     {
948         x = 0;
949         switch ( len )
950         {
951             case 1: x = *(uint8_t *)endianedp; break;
952             case 2: x = *(uint16_t *)endianedp; break;
953             case 4: x = *(uint32_t *)endianedp; break;
954             case 8: x = *(uint64_t *)endianedp; break;
955         }
956         for (i=0; i<len; i++,x >>= 8)
957             serialized[i] = (uint8_t)(x & 0xff);
958     }
959     return(len);
960 }
961
962 int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp)
963 {
964     int32_t i;
965     if ( rwflag == 0 )
966     {
967         for (i=0; i<len; i++)
968             endianedp[i] = serialized[i];
969     }
970     else
971     {
972         for (i=0; i<len; i++)
973             serialized[i] = endianedp[i];
974     }
975     return(len);
976 }
977
978 int32_t komodo_scriptitemlen(int32_t *opretlenp,uint8_t *script)
979 {
980     int32_t opretlen,len = 0;
981     if ( (opretlen= script[len++]) >= 0x4c )
982     {
983         if ( opretlen == 0x4c )
984             opretlen = script[len++];
985         else if ( opretlen == 0x4d )
986         {
987             opretlen = script[len] + (script[len+1] << 8);
988             len += 2;
989             //opretlen = script[len++];
990             //opretlen = (opretlen << 8) | script[len++];
991         }
992     }
993     *opretlenp = opretlen;
994     return(len);
995 }
996
997 int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_t opretlen)
998 {
999     int32_t offset = 0;
1000     script[offset++] = 0x6a;
1001     opretlen++;
1002     if ( opretlen >= 0x4c )
1003     {
1004         if ( opretlen > 0xff )
1005         {
1006             script[offset++] = 0x4d;
1007             script[offset++] = opretlen & 0xff;
1008             script[offset++] = (opretlen >> 8) & 0xff;
1009         }
1010         else
1011         {
1012             script[offset++] = 0x4c;
1013             script[offset++] = opretlen;
1014         }
1015     } else script[offset++] = opretlen;
1016     script[offset++] = type; // covered by opretlen
1017     memcpy(&script[offset],opret,opretlen-1);
1018     return(offset + opretlen - 1);
1019 }
1020
1021 // get a pseudo random number that is the same for each block individually at all times and different
1022 // from all other blocks. the sequence is extremely likely, but not guaranteed to be unique for each block chain
1023 uint64_t blockPRG(uint32_t nHeight)
1024 {
1025     int i;
1026     uint8_t hashSrc[8];
1027     uint64_t result, hashSrc64 = (uint64_t)ASSETCHAINS_MAGIC << 32 + nHeight;
1028     bits256 hashResult;
1029
1030     for ( i = 0; i < sizeof(hashSrc); i++ )
1031     {
1032         hashSrc[i] = hashSrc64 & 0xff;
1033         hashSrc64 >>= 8;
1034     }
1035     vcalc_sha256(0, hashResult.bytes, hashSrc, sizeof(hashSrc));
1036
1037     for ( i = 0; i < 8; i++ )
1038     {
1039         result = (result << 8) + hashResult.bytes[i];
1040     }
1041     return(result);
1042 }
1043
1044 // given a block height, this returns the unlock time for that block height, derived from
1045 // the ASSETCHAINS_MAGIC number as well as the block height, providing different random numbers
1046 // for corresponding blocks across chains, but the same sequence in each chain 
1047 int64_t komodo_block_unlocktime(uint32_t nHeight)
1048 {
1049     uint64_t fromTime, toTime, unlocktime;
1050
1051     if ( ASSETCHAINS_TIMEUNLOCKFROM == ASSETCHAINS_TIMEUNLOCKTO )
1052         unlocktime = ASSETCHAINS_TIMEUNLOCKTO;
1053     else
1054     {
1055         unlocktime = blockPRG(nHeight) / (0xffffffffffffffff / ((ASSETCHAINS_TIMEUNLOCKTO - ASSETCHAINS_TIMEUNLOCKFROM) + 1));
1056         // boundary and power of 2 can make it exceed to time by 1
1057         unlocktime = unlocktime + ASSETCHAINS_TIMEUNLOCKFROM;
1058         if (unlocktime > ASSETCHAINS_TIMEUNLOCKTO)
1059             unlocktime--;
1060     }
1061     return ((int64_t)unlocktime);
1062 }
1063
1064 long _stripwhite(char *buf,int accept)
1065 {
1066     int32_t i,j,c;
1067     if ( buf == 0 || buf[0] == 0 )
1068         return(0);
1069     for (i=j=0; buf[i]!=0; i++)
1070     {
1071         buf[j] = c = buf[i];
1072         if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') )
1073             j++;
1074     }
1075     buf[j] = 0;
1076     return(j);
1077 }
1078
1079 char *clonestr(char *str)
1080 {
1081     char *clone;
1082     if ( str == 0 || str[0] == 0 )
1083     {
1084         printf("warning cloning nullstr.%p\n",str);
1085 #ifdef __APPLE__
1086         while ( 1 ) sleep(1);
1087 #endif
1088         str = (char *)"<nullstr>";
1089     }
1090     clone = (char *)malloc(strlen(str)+16);
1091     strcpy(clone,str);
1092     return(clone);
1093 }
1094
1095 int32_t safecopy(char *dest,char *src,long len)
1096 {
1097     int32_t i = -1;
1098     if ( src != 0 && dest != 0 && src != dest )
1099     {
1100         if ( dest != 0 )
1101             memset(dest,0,len);
1102         for (i=0; i<len&&src[i]!=0; i++)
1103             dest[i] = src[i];
1104         if ( i == len )
1105         {
1106             printf("safecopy: %s too long %ld\n",src,len);
1107 #ifdef __APPLE__
1108             //getchar();
1109 #endif
1110             return(-1);
1111         }
1112         dest[i] = 0;
1113     }
1114     return(i);
1115 }
1116
1117 char *parse_conf_line(char *line,char *field)
1118 {
1119     line += strlen(field);
1120     for (; *line!='='&&*line!=0; line++)
1121         break;
1122     if ( *line == 0 )
1123         return(0);
1124     if ( *line == '=' )
1125         line++;
1126     while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' )
1127         line[strlen(line)-1] = 0;
1128     //printf("LINE.(%s)\n",line);
1129     _stripwhite(line,0);
1130     return(clonestr(line));
1131 }
1132
1133 double OS_milliseconds()
1134 {
1135     struct timeval tv; double millis;
1136     gettimeofday(&tv,NULL);
1137     millis = ((double)tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.);
1138     //printf("tv_sec.%ld usec.%d %f\n",tv.tv_sec,tv.tv_usec,millis);
1139     return(millis);
1140 }
1141
1142 #ifndef _WIN32
1143 void OS_randombytes(unsigned char *x,long xlen)
1144 {
1145     static int fd = -1;
1146     int32_t i;
1147     if (fd == -1) {
1148         for (;;) {
1149             fd = open("/dev/urandom",O_RDONLY);
1150             if (fd != -1) break;
1151             sleep(1);
1152         }
1153     }
1154     while (xlen > 0) {
1155         if (xlen < 1048576) i = (int32_t)xlen; else i = 1048576;
1156         i = (int32_t)read(fd,x,i);
1157         if (i < 1) {
1158             sleep(1);
1159             continue;
1160         }
1161         if ( 0 )
1162         {
1163             int32_t j;
1164             for (j=0; j<i; j++)
1165                 printf("%02x ",x[j]);
1166             printf("-> %p\n",x);
1167         }
1168         x += i;
1169         xlen -= i;
1170     }
1171 }
1172 #endif
1173
1174 void lock_queue(queue_t *queue)
1175 {
1176     if ( queue->initflag == 0 )
1177     {
1178         portable_mutex_init(&queue->mutex);
1179         queue->initflag = 1;
1180     }
1181         portable_mutex_lock(&queue->mutex);
1182 }
1183
1184 void queue_enqueue(char *name,queue_t *queue,struct queueitem *item)
1185 {
1186     if ( queue->name[0] == 0 && name != 0 && name[0] != 0 )
1187         strcpy(queue->name,name);
1188     if ( item == 0 )
1189     {
1190         printf("FATAL type error: queueing empty value\n");
1191         return;
1192     }
1193     lock_queue(queue);
1194     DL_APPEND(queue->list,item);
1195     portable_mutex_unlock(&queue->mutex);
1196 }
1197
1198 struct queueitem *queue_dequeue(queue_t *queue)
1199 {
1200     struct queueitem *item = 0;
1201     lock_queue(queue);
1202     if ( queue->list != 0 )
1203     {
1204         item = queue->list;
1205         DL_DELETE(queue->list,item);
1206     }
1207         portable_mutex_unlock(&queue->mutex);
1208     return(item);
1209 }
1210
1211 void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize)
1212 {
1213     struct queueitem *item = 0;
1214     lock_queue(queue);
1215     if ( queue->list != 0 )
1216     {
1217         DL_FOREACH(queue->list,item)
1218         {
1219                                                 #ifdef _WIN32
1220                                                 if ( item == copy || (item->allocsize == copysize && memcmp((void *)((intptr_t)item + sizeof(struct queueitem)),(void *)((intptr_t)copy + sizeof(struct queueitem)),copysize) == 0) )
1221                                                 #else
1222             if ( item == copy || (item->allocsize == copysize && memcmp((void *)((long)item + sizeof(struct queueitem)),(void *)((long)copy + sizeof(struct queueitem)),copysize) == 0) )
1223                                                 #endif
1224             {
1225                 DL_DELETE(queue->list,item);
1226                 portable_mutex_unlock(&queue->mutex);
1227                 printf("name.(%s) deleted item.%p list.%p\n",queue->name,item,queue->list);
1228                 return(item);
1229             }
1230         }
1231     }
1232         portable_mutex_unlock(&queue->mutex);
1233     return(0);
1234 }
1235
1236 void *queue_free(queue_t *queue)
1237 {
1238     struct queueitem *item = 0;
1239     lock_queue(queue);
1240     if ( queue->list != 0 )
1241     {
1242         DL_FOREACH(queue->list,item)
1243         {
1244             DL_DELETE(queue->list,item);
1245             free(item);
1246         }
1247         //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1248     }
1249         portable_mutex_unlock(&queue->mutex);
1250     return(0);
1251 }
1252
1253 void *queue_clone(queue_t *clone,queue_t *queue,int32_t size)
1254 {
1255     struct queueitem *ptr,*item = 0;
1256     lock_queue(queue);
1257     if ( queue->list != 0 )
1258     {
1259         DL_FOREACH(queue->list,item)
1260         {
1261             ptr = (struct queueitem *)calloc(1,sizeof(*ptr));
1262             memcpy(ptr,item,size);
1263             queue_enqueue(queue->name,clone,ptr);
1264         }
1265         //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1266     }
1267         portable_mutex_unlock(&queue->mutex);
1268     return(0);
1269 }
1270
1271 int32_t queue_size(queue_t *queue)
1272 {
1273     int32_t count = 0;
1274     struct queueitem *tmp;
1275     lock_queue(queue);
1276     DL_COUNT(queue->list,tmp,count);
1277     portable_mutex_unlock(&queue->mutex);
1278         return count;
1279 }
1280
1281 void iguana_initQ(queue_t *Q,char *name)
1282 {
1283     struct queueitem *item,*I;
1284     memset(Q,0,sizeof(*Q));
1285     I = (struct queueitem *)calloc(1,sizeof(*I));
1286     strcpy(Q->name,name);
1287     queue_enqueue(name,Q,I);
1288     if ( (item= queue_dequeue(Q)) != 0 )
1289         free(item);
1290 }
1291
1292 uint16_t _komodo_userpass(char *username,char *password,FILE *fp)
1293 {
1294     char *rpcuser,*rpcpassword,*str,line[8192]; uint16_t port = 0;
1295     rpcuser = rpcpassword = 0;
1296     username[0] = password[0] = 0;
1297     while ( fgets(line,sizeof(line),fp) != 0 )
1298     {
1299         if ( line[0] == '#' )
1300             continue;
1301         //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword"));
1302         if ( (str= strstr(line,(char *)"rpcuser")) != 0 )
1303             rpcuser = parse_conf_line(str,(char *)"rpcuser");
1304         else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 )
1305             rpcpassword = parse_conf_line(str,(char *)"rpcpassword");
1306         else if ( (str= strstr(line,(char *)"rpcport")) != 0 )
1307         {
1308             port = atoi(parse_conf_line(str,(char *)"rpcport"));
1309             //fprintf(stderr,"rpcport.%u in file\n",port);
1310         }
1311     }
1312     if ( rpcuser != 0 && rpcpassword != 0 )
1313     {
1314         strcpy(username,rpcuser);
1315         strcpy(password,rpcpassword);
1316     }
1317     //printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port);
1318     if ( rpcuser != 0 )
1319         free(rpcuser);
1320     if ( rpcpassword != 0 )
1321         free(rpcpassword);
1322     return(port);
1323 }
1324
1325 void komodo_statefname(char *fname,char *symbol,char *str)
1326 {
1327     int32_t n,len;
1328     sprintf(fname,"%s",GetDataDir(false).string().c_str());
1329     if ( (n= (int32_t)strlen(ASSETCHAINS_SYMBOL)) != 0 )
1330     {
1331         len = (int32_t)strlen(fname);
1332         if ( strcmp(ASSETCHAINS_SYMBOL,&fname[len - n]) == 0 )
1333             fname[len - n] = 0;
1334         else
1335         {
1336             printf("unexpected fname.(%s) vs %s [%s] n.%d len.%d (%s)\n",fname,symbol,ASSETCHAINS_SYMBOL,n,len,&fname[len - n]);
1337             return;
1338         }
1339     }
1340     else
1341     {
1342 #ifdef _WIN32
1343         strcat(fname,"\\");
1344 #else
1345         strcat(fname,"/");
1346 #endif
1347     }
1348     if ( symbol != 0 && symbol[0] != 0 && strcmp("KMD",symbol) != 0 )
1349     {
1350         strcat(fname,symbol);
1351         //printf("statefname.(%s) -> (%s)\n",symbol,fname);
1352 #ifdef _WIN32
1353         strcat(fname,"\\");
1354 #else
1355         strcat(fname,"/");
1356 #endif
1357     }
1358     strcat(fname,str);
1359     //printf("test.(%s) -> [%s] statename.(%s) %s\n",test,ASSETCHAINS_SYMBOL,symbol,fname);
1360 }
1361
1362 void komodo_configfile(char *symbol,uint16_t rpcport)
1363 {
1364     static char myusername[512],mypassword[8192];
1365     FILE *fp; uint16_t kmdport; uint8_t buf2[33]; char fname[512],buf[128],username[512],password[8192]; uint32_t crc,r,r2,i;
1366     if ( symbol != 0 && rpcport != 0 )
1367     {
1368         r = (uint32_t)time(NULL);
1369         r2 = OS_milliseconds();
1370         memcpy(buf,&r,sizeof(r));
1371         memcpy(&buf[sizeof(r)],&r2,sizeof(r2));
1372         memcpy(&buf[sizeof(r)+sizeof(r2)],symbol,strlen(symbol));
1373         crc = calc_crc32(0,(uint8_t *)buf,(int32_t)(sizeof(r)+sizeof(r2)+strlen(symbol)));
1374                                 #ifdef _WIN32
1375                                 randombytes_buf(buf2,sizeof(buf2));
1376                                 #else
1377         OS_randombytes(buf2,sizeof(buf2));
1378                                 #endif
1379         for (i=0; i<sizeof(buf2); i++)
1380             sprintf(&password[i*2],"%02x",buf2[i]);
1381         password[i*2] = 0;
1382         sprintf(buf,"%s.conf",symbol);
1383         BITCOIND_RPCPORT = rpcport;
1384 #ifdef _WIN32
1385         sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),buf);
1386 #else
1387         sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),buf);
1388 #endif
1389         if ( (fp= fopen(fname,"rb")) == 0 )
1390         {
1391 #ifndef FROM_CLI
1392             if ( (fp= fopen(fname,"wb")) != 0 )
1393             {
1394                 fprintf(fp,"rpcuser=user%u\nrpcpassword=pass%s\nrpcport=%u\nserver=1\ntxindex=1\nrpcworkqueue=256\nrpcallowip=127.0.0.1\n",crc,password,rpcport);
1395                 fclose(fp);
1396                 printf("Created (%s)\n",fname);
1397             } else printf("Couldnt create (%s)\n",fname);
1398 #endif
1399         }
1400         else
1401         {
1402             _komodo_userpass(myusername,mypassword,fp);
1403             mapArgs["-rpcpassword"] = mypassword;
1404             mapArgs["-rpcusername"] = myusername;
1405             //fprintf(stderr,"myusername.(%s)\n",myusername);
1406             fclose(fp);
1407         }
1408     }
1409     strcpy(fname,GetDataDir().string().c_str());
1410 #ifdef _WIN32
1411     while ( fname[strlen(fname)-1] != '\\' )
1412         fname[strlen(fname)-1] = 0;
1413     strcat(fname,"komodo.conf");
1414 #else
1415     while ( fname[strlen(fname)-1] != '/' )
1416         fname[strlen(fname)-1] = 0;
1417 #ifdef __APPLE__
1418     strcat(fname,"Komodo.conf");
1419 #else
1420     strcat(fname,"komodo.conf");
1421 #endif
1422 #endif
1423     if ( (fp= fopen(fname,"rb")) != 0 )
1424     {
1425         if ( (kmdport= _komodo_userpass(username,password,fp)) != 0 )
1426             KMD_PORT = kmdport;
1427         sprintf(KMDUSERPASS,"%s:%s",username,password);
1428         fclose(fp);
1429 //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS);
1430     } //else printf("couldnt open.(%s)\n",fname);
1431 }
1432
1433 uint16_t komodo_userpass(char *userpass,char *symbol)
1434 {
1435     FILE *fp; uint16_t port = 0; char fname[512],username[512],password[512],confname[KOMODO_ASSETCHAIN_MAXLEN];
1436     userpass[0] = 0;
1437     if ( strcmp("KMD",symbol) == 0 )
1438     {
1439 #ifdef __APPLE__
1440         sprintf(confname,"Komodo.conf");
1441 #else
1442         sprintf(confname,"komodo.conf");
1443 #endif
1444     }
1445     else sprintf(confname,"%s.conf",symbol);
1446     komodo_statefname(fname,symbol,confname);
1447     if ( (fp= fopen(fname,"rb")) != 0 )
1448     {
1449         port = _komodo_userpass(username,password,fp);
1450         sprintf(userpass,"%s:%s",username,password);
1451         if ( strcmp(symbol,ASSETCHAINS_SYMBOL) == 0 )
1452             strcpy(ASSETCHAINS_USERPASS,userpass);
1453         fclose(fp);
1454     }
1455     return(port);
1456 }
1457
1458 uint32_t komodo_assetmagic(char *symbol,uint64_t supply,uint8_t *extraptr,int32_t extralen)
1459 {
1460     uint8_t buf[512]; uint32_t crc0=0; int32_t len = 0; bits256 hash;
1461     if ( strcmp(symbol,"KMD") == 0 )
1462         return(0x8de4eef9);
1463     len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply);
1464     strcpy((char *)&buf[len],symbol);
1465     len += strlen(symbol);
1466     if ( extraptr != 0 && extralen != 0 )
1467     {
1468         vcalc_sha256(0,hash.bytes,extraptr,extralen);
1469         crc0 = hash.uints[0];
1470     }
1471     return(calc_crc32(crc0,buf,len));
1472 }
1473
1474 uint16_t komodo_assetport(uint32_t magic,int32_t extralen)
1475 {
1476     if ( magic == 0x8de4eef9 )
1477         return(7770);
1478     else if ( extralen == 0 )
1479         return(8000 + (magic % 7777));
1480     else return(16000 + (magic % 49500));
1481 }
1482
1483 uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp,uint8_t *extraptr,int32_t extralen)
1484 {
1485     if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 )
1486     {
1487         *magicp = 0x8de4eef9;
1488         return(7770);
1489     }
1490     *magicp = komodo_assetmagic(symbol,supply,extraptr,extralen);
1491     return(komodo_assetport(*magicp,extralen));
1492 }
1493
1494 /*void komodo_ports(uint16_t ports[MAX_CURRENCIES])
1495 {
1496     int32_t i; uint32_t magic;
1497     for (i=0; i<MAX_CURRENCIES; i++)
1498     {
1499         ports[i] = komodo_port(CURRENCIES[i],10,&magic);
1500         printf("%u ",ports[i]);
1501     }
1502     printf("ports\n");
1503 }*/
1504
1505 char *iguanafmtstr = (char *)"curl --url \"http://127.0.0.1:7776\" --data \"{\\\"conf\\\":\\\"%s.conf\\\",\\\"path\\\":\\\"${HOME#\"/\"}/.komodo/%s\\\",\\\"unitval\\\":\\\"20\\\",\\\"zcash\\\":1,\\\"RELAY\\\":-1,\\\"VALIDATE\\\":0,\\\"prefetchlag\\\":-1,\\\"poll\\\":100,\\\"active\\\":1,\\\"agent\\\":\\\"iguana\\\",\\\"method\\\":\\\"addcoin\\\",\\\"startpend\\\":4,\\\"endpend\\\":4,\\\"services\\\":129,\\\"maxpeers\\\":8,\\\"newcoin\\\":\\\"%s\\\",\\\"name\\\":\\\"%s\\\",\\\"hasheaders\\\":1,\\\"useaddmultisig\\\":0,\\\"netmagic\\\":\\\"%s\\\",\\\"p2p\\\":%u,\\\"rpc\\\":%u,\\\"pubval\\\":60,\\\"p2shval\\\":85,\\\"wifval\\\":188,\\\"txfee_satoshis\\\":\\\"10000\\\",\\\"isPoS\\\":0,\\\"minoutput\\\":10000,\\\"minconfirms\\\":2,\\\"genesishash\\\":\\\"027e3758c3a65b12aa1046462b486d0a63bfa1beae327897f56c5cfb7daaae71\\\",\\\"protover\\\":170002,\\\"genesisblock\\\":\\\"0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a000000000000000000000000000000000000000000000000000000000000000029ab5f490f0f0f200b00000000000000000000000000000000000000000000000000000000000000fd4005000d5ba7cda5d473947263bf194285317179d2b0d307119c2e7cc4bd8ac456f0774bd52b0cd9249be9d40718b6397a4c7bbd8f2b3272fed2823cd2af4bd1632200ba4bf796727d6347b225f670f292343274cc35099466f5fb5f0cd1c105121b28213d15db2ed7bdba490b4cedc69742a57b7c25af24485e523aadbb77a0144fc76f79ef73bd8530d42b9f3b9bed1c135ad1fe152923fafe98f95f76f1615e64c4abb1137f4c31b218ba2782bc15534788dda2cc08a0ee2987c8b27ff41bd4e31cd5fb5643dfe862c9a02ca9f90c8c51a6671d681d04ad47e4b53b1518d4befafefe8cadfb912f3d03051b1efbf1dfe37b56e93a741d8dfd80d576ca250bee55fab1311fc7b3255977558cdda6f7d6f875306e43a14413facdaed2f46093e0ef1e8f8a963e1632dcbeebd8e49fd16b57d49b08f9762de89157c65233f60c8e38a1f503a48c555f8ec45dedecd574a37601323c27be597b956343107f8bd80f3a925afaf30811df83c402116bb9c1e5231c70fff899a7c82f73c902ba54da53cc459b7bf1113db65cc8f6914d3618560ea69abd13658fa7b6af92d374d6eca9529f8bd565166e4fcbf2a8dfb3c9b69539d4d2ee2e9321b85b331925df195915f2757637c2805e1d4131e1ad9ef9bc1bb1c732d8dba4738716d351ab30c996c8657bab39567ee3b29c6d054b711495c0d52e1cd5d8e55b4f0f0325b97369280755b46a02afd54be4ddd9f77c22272b8bbb17ff5118fedbae2564524e797bd28b5f74f7079d532ccc059807989f94d267f47e724b3f1ecfe00ec9e6541c961080d8891251b84b4480bc292f6a180bea089fef5bbda56e1e41390d7c0e85ba0ef530f7177413481a226465a36ef6afe1e2bca69d2078712b3912bba1a99b1fbff0d355d6ffe726d2bb6fbc103c4ac5756e5bee6e47e17424ebcbf1b63d8cb90ce2e40198b4f4198689daea254307e52a25562f4c1455340f0ffeb10f9d8e914775e37d0edca019fb1b9c6ef81255ed86bc51c5391e0591480f66e2d88c5f4fd7277697968656a9b113ab97f874fdd5f2465e5559533e01ba13ef4a8f7a21d02c30c8ded68e8c54603ab9c8084ef6d9eb4e92c75b078539e2ae786ebab6dab73a09e0aa9ac575bcefb29e930ae656e58bcb513f7e3c17e079dce4f05b5dbc18c2a872b22509740ebe6a3903e00ad1abc55076441862643f93606e3dc35e8d9f2caef3ee6be14d513b2e062b21d0061de3bd56881713a1a5c17f5ace05e1ec09da53f99442df175a49bd154aa96e4949decd52fed79ccf7ccbce32941419c314e374e4a396ac553e17b5340336a1a25c22f9e42a243ba5404450b650acfc826a6e432971ace776e15719515e1634ceb9a4a35061b668c74998d3dfb5827f6238ec015377e6f9c94f38108768cf6e5c8b132e0303fb5a200368f845ad9d46343035a6ff94031df8d8309415bb3f6cd5ede9c135fdabcc030599858d803c0f85be7661c88984d88faa3d26fb0e9aac0056a53f1b5d0baed713c853c4a2726869a0a124a8a5bbc0fc0ef80c8ae4cb53636aa02503b86a1eb9836fcc259823e2692d921d88e1ffc1e6cb2bde43939ceb3f32a611686f539f8f7c9f0bf00381f743607d40960f06d347d1cd8ac8a51969c25e37150efdf7aa4c2037a2fd0516fb444525ab157a0ed0a7412b2fa69b217fe397263153782c0f64351fbdf2678fa0dc8569912dcd8e3ccad38f34f23bbbce14c6a26ac24911b308b82c7e43062d180baeac4ba7153858365c72c63dcf5f6a5b08070b730adb017aeae925b7d0439979e2679f45ed2f25a7edcfd2fb77a8794630285ccb0a071f5cce410b46dbf9750b0354aae8b65574501cc69efb5b6a43444074fee116641bb29da56c2b4a7f456991fc92b2\\\",\\\"debug\\\":0,\\\"seedipaddr\\\":\\\"%s\\\"}\"";
1506
1507
1508 int32_t komodo_whoami(char *pubkeystr,int32_t height,uint32_t timestamp)
1509 {
1510     int32_t i,notaryid;
1511     for (i=0; i<33; i++)
1512         sprintf(&pubkeystr[i<<1],"%02x",NOTARY_PUBKEY33[i]);
1513     pubkeystr[66] = 0;
1514     komodo_chosennotary(&notaryid,height,NOTARY_PUBKEY33,timestamp);
1515     return(notaryid);
1516 }
1517
1518 char *argv0suffix[] =
1519 {
1520     (char *)"mnzd", (char *)"mnz-cli", (char *)"mnzd.exe", (char *)"mnz-cli.exe", (char *)"btchd", (char *)"btch-cli", (char *)"btchd.exe", (char *)"btch-cli.exe"
1521 };
1522
1523 char *argv0names[] =
1524 {
1525     (char *)"MNZ", (char *)"MNZ", (char *)"MNZ", (char *)"MNZ", (char *)"BTCH", (char *)"BTCH", (char *)"BTCH", (char *)"BTCH"
1526 };
1527
1528 int64_t komodo_max_money()
1529 {
1530     return komodo_current_supply(10000000);
1531 }
1532
1533 uint64_t komodo_ac_block_subsidy(int nHeight)
1534 {
1535     // we have to find our era, start from beginning reward, and determine current subsidy
1536     int64_t numerator, denominator, subsidy = 0;
1537     int64_t subsidyDifference;
1538     int32_t numhalvings, curEra = 0, sign = 1;
1539     static uint64_t cached_subsidy; static int32_t cached_numhalvings; static int cached_era;
1540
1541     // check for backwards compat, older chains with no explicit rewards had 0.0001 block reward
1542     if ( ASSETCHAINS_ENDSUBSIDY[0] == 0 && ASSETCHAINS_REWARD[0] == 0 )
1543         subsidy = 10000;
1544     else if ( (ASSETCHAINS_ENDSUBSIDY[0] == 0 && ASSETCHAINS_REWARD[0] != 0) || ASSETCHAINS_ENDSUBSIDY[0] != 0 )
1545     {
1546         // if we have an end block in the first era, find our current era
1547         if ( ASSETCHAINS_ENDSUBSIDY[0] != 0 )
1548         {
1549             for ( curEra = 0; curEra <= ASSETCHAINS_LASTERA; curEra++ )
1550             {
1551                 if ( ASSETCHAINS_ENDSUBSIDY[curEra] > nHeight || ASSETCHAINS_ENDSUBSIDY[curEra] == 0 )
1552                     break;
1553             }
1554         }
1555         if ( curEra <= ASSETCHAINS_LASTERA )
1556         {
1557             int64_t nStart = curEra ? ASSETCHAINS_ENDSUBSIDY[curEra - 1] : 0;
1558             subsidy = (int64_t)ASSETCHAINS_REWARD[curEra];
1559             if ( subsidy || (curEra != ASSETCHAINS_LASTERA && ASSETCHAINS_REWARD[curEra + 1] != 0) )
1560             {
1561                 if ( ASSETCHAINS_HALVING[curEra] != 0 )
1562                 {
1563                     if ( (numhalvings = ((nHeight - nStart) / ASSETCHAINS_HALVING[curEra])) > 0 )
1564                     {
1565                         if ( ASSETCHAINS_DECAY[curEra] == 0 )
1566                             subsidy >>= numhalvings;
1567                         else if ( ASSETCHAINS_DECAY[curEra] == 100000000 && ASSETCHAINS_ENDSUBSIDY[curEra] != 0 )
1568                         {
1569                             if ( curEra == ASSETCHAINS_LASTERA )
1570                             {
1571                                 subsidyDifference = subsidy;
1572                             }
1573                             else
1574                             {    
1575                                 // Ex: -ac_eras=3 -ac_reward=0,384,24 -ac_end=1440,260640,0 -ac_halving=1,1440,2103840 -ac_decay 100000000,97750000,0
1576                                 subsidyDifference = subsidy - ASSETCHAINS_REWARD[curEra + 1];
1577                                 if (subsidyDifference < 0)
1578                                 {
1579                                     sign = -1;
1580                                     subsidyDifference *= sign;
1581                                 }
1582                             }
1583                             denominator = ASSETCHAINS_ENDSUBSIDY[curEra] - nStart;
1584                             numerator = denominator - ((ASSETCHAINS_ENDSUBSIDY[curEra] - nHeight) + ((nHeight - nStart) % ASSETCHAINS_HALVING[curEra]));
1585                             subsidy = subsidy - sign * ((subsidyDifference * numerator) / denominator);
1586                         }
1587                         else
1588                         {
1589                             if ( cached_subsidy > 0 && cached_era == curEra && cached_numhalvings == numhalvings )
1590                                 subsidy = cached_subsidy;
1591                             else
1592                             {
1593                                 for (int i=0; i < numhalvings && subsidy != 0; i++)
1594                                     subsidy = (subsidy * ASSETCHAINS_DECAY[curEra]) / 100000000;
1595                                 cached_subsidy = subsidy;
1596                                 cached_numhalvings = numhalvings;
1597                                 cached_era = curEra;
1598                             }
1599                         }
1600                     }
1601                 }
1602             }
1603         }
1604     }
1605     return(subsidy);
1606 }
1607
1608 void komodo_args(char *argv0)
1609 {
1610     extern int64_t MAX_MONEY;
1611     std::string name,addn; char *dirname,fname[512],arg0str[64],magicstr[9]; uint8_t magic[4],extrabuf[256],*extraptr=0; FILE *fp; uint64_t val; uint16_t port; int32_t i,baseid,len,n,extralen = 0;
1612     IS_KOMODO_NOTARY = GetBoolArg("-notary", false);
1613     if ( (KOMODO_EXCHANGEWALLET= GetBoolArg("-exchange", false)) != 0 )
1614         fprintf(stderr,"KOMODO_EXCHANGEWALLET mode active\n");
1615     NOTARY_PUBKEY = GetArg("-pubkey", "");
1616     if ( strlen(NOTARY_PUBKEY.c_str()) == 66 )
1617     {
1618         USE_EXTERNAL_PUBKEY = 1;
1619         //KOMODO_PAX = 1;
1620     } //else KOMODO_PAX = GetArg("-pax",0);
1621     name = GetArg("-ac_name","");
1622     if ( argv0 != 0 )
1623     {
1624         len = (int32_t)strlen(argv0);
1625         for (i=0; i<sizeof(argv0suffix)/sizeof(*argv0suffix); i++)
1626         {
1627             n = (int32_t)strlen(argv0suffix[i]);
1628             if ( strcmp(&argv0[len - n],argv0suffix[i]) == 0 )
1629             {
1630                 //printf("ARGV0.(%s) -> matches suffix (%s) -> ac_name.(%s)\n",argv0,argv0suffix[i],argv0names[i]);
1631                 name = argv0names[i];
1632                 break;
1633             }
1634         }
1635     }
1636     ASSETCHAINS_CC = GetArg("-ac_cc",0);
1637     if ( (KOMODO_REWIND= GetArg("-rewind",0)) != 0 )
1638     {
1639         printf("KOMODO_REWIND %d\n",KOMODO_REWIND);
1640     }
1641     if ( name.c_str()[0] != 0 )
1642     {
1643         std::string selectedAlgo = GetArg("-ac_algo", std::string(ASSETCHAINS_ALGORITHMS[0]));
1644
1645         for ( int i = 0; i < ASSETCHAINS_NUMALGOS; i++ )
1646         {
1647             if (std::string(ASSETCHAINS_ALGORITHMS[i]) == selectedAlgo)
1648             {
1649                 ASSETCHAINS_ALGO = i;
1650                 // only worth mentioning if it's not equihash
1651                 if (ASSETCHAINS_ALGO != ASSETCHAINS_EQUIHASH)
1652                     printf("ASSETCHAINS_ALGO, algorithm set to %s\n", selectedAlgo.c_str());
1653                 break;
1654             }
1655         }
1656         if (i == ASSETCHAINS_NUMALGOS)
1657         {
1658             printf("ASSETCHAINS_ALGO, %s not supported. using equihash\n", selectedAlgo.c_str());
1659         }
1660
1661         ASSETCHAINS_LASTERA = GetArg("-ac_eras", 1);
1662         if ( ASSETCHAINS_LASTERA < 1 || ASSETCHAINS_LASTERA > ASSETCHAINS_MAX_ERAS )
1663         {
1664             ASSETCHAINS_LASTERA = 1;
1665             printf("ASSETCHAINS_LASTERA, if specified, must be between 1 and %u. ASSETCHAINS_LASTERA set to %u\n", ASSETCHAINS_MAX_ERAS, ASSETCHAINS_LASTERA);
1666         }
1667         ASSETCHAINS_LASTERA -= 1;
1668
1669         ASSETCHAINS_TIMELOCKGTE = GetArg("-ac_timelockgte", _ASSETCHAINS_TIMELOCKOFF);
1670         ASSETCHAINS_TIMEUNLOCKFROM = GetArg("-ac_timeunlockfrom", 0);
1671         ASSETCHAINS_TIMEUNLOCKTO = GetArg("-ac_timeunlockto", 0);
1672         if ( ASSETCHAINS_TIMEUNLOCKFROM > ASSETCHAINS_TIMEUNLOCKTO )
1673         {
1674             printf("ASSETCHAINS_TIMELOCKGTE - must specify valid ac_timeunlockfrom and ac_timeunlockto\n");
1675             ASSETCHAINS_TIMELOCKGTE = _ASSETCHAINS_TIMELOCKOFF;
1676             ASSETCHAINS_TIMEUNLOCKFROM = ASSETCHAINS_TIMEUNLOCKTO = 0;
1677         }
1678
1679         Split(GetArg("-ac_end",""),  ASSETCHAINS_ENDSUBSIDY, 0);
1680         Split(GetArg("-ac_reward",""),  ASSETCHAINS_REWARD, 0);
1681         Split(GetArg("-ac_halving",""),  ASSETCHAINS_HALVING, 0);
1682         Split(GetArg("-ac_decay",""),  ASSETCHAINS_DECAY, 0);
1683
1684         for ( int i = 0; i < ASSETCHAINS_MAX_ERAS; i++ )
1685         {
1686             if ( ASSETCHAINS_DECAY[i] == 100000000 && ASSETCHAINS_ENDSUBSIDY == 0 )
1687             {
1688                 ASSETCHAINS_DECAY[i] = 0;
1689                 printf("ERA%u: ASSETCHAINS_DECAY of 100000000 means linear and that needs ASSETCHAINS_ENDSUBSIDY\n", i);
1690             }
1691             else if ( ASSETCHAINS_DECAY[i] > 100000000 )
1692             {
1693                 ASSETCHAINS_DECAY[i] = 0;
1694                 printf("ERA%u: ASSETCHAINS_DECAY cant be more than 100000000\n", i);
1695             }
1696         }
1697
1698         MAX_BLOCK_SIGOPS = 60000;
1699         ASSETCHAINS_SUPPLY = GetArg("-ac_supply",10);
1700         ASSETCHAINS_COMMISSION = GetArg("-ac_perc",0);
1701         ASSETCHAINS_OVERRIDE_PUBKEY = GetArg("-ac_pubkey","");
1702         if ( (ASSETCHAINS_STAKED= GetArg("-ac_staked",0)) > 100 )
1703             ASSETCHAINS_STAKED = 100;
1704
1705         if ( strlen(ASSETCHAINS_OVERRIDE_PUBKEY.c_str()) == 66 && ASSETCHAINS_COMMISSION > 0 && ASSETCHAINS_COMMISSION <= 100000000 )
1706             decode_hex(ASSETCHAINS_OVERRIDE_PUBKEY33,33,(char *)ASSETCHAINS_OVERRIDE_PUBKEY.c_str());
1707         else if ( ASSETCHAINS_COMMISSION != 0 )
1708         {
1709             ASSETCHAINS_COMMISSION = 0;
1710             printf("ASSETCHAINS_COMMISSION needs an ASSETCHAINS_OVERRIDE_PUBKEY and cant be more than 100000000 (100%%)\n");
1711         }
1712
1713         if ( ASSETCHAINS_ENDSUBSIDY[0] != 0 || ASSETCHAINS_REWARD[0] != 0 || ASSETCHAINS_HALVING[0] != 0 || ASSETCHAINS_DECAY[0] != 0 || ASSETCHAINS_COMMISSION != 0 )
1714         {
1715             printf("perc.%llu\n",(long long)ASSETCHAINS_COMMISSION);
1716
1717             extraptr = extrabuf;
1718             memcpy(extraptr,ASSETCHAINS_OVERRIDE_PUBKEY33,33), extralen = 33;
1719
1720             // if we have one era, this should create the same data structure as it used to, same if we increase _MAX_ERAS
1721             for ( int i = 0; i <= ASSETCHAINS_LASTERA; i++ )
1722             {
1723                 printf("ERA%u: end.%llu reward.%llu halving.%llu decay.%llu\n", i,
1724                        (long long)ASSETCHAINS_ENDSUBSIDY[i],
1725                        (long long)ASSETCHAINS_REWARD[i],
1726                        (long long)ASSETCHAINS_HALVING[i],
1727                        (long long)ASSETCHAINS_DECAY[i]);
1728
1729                 // TODO: Verify that we don't overrun extrabuf here, which is a 256 byte buffer
1730                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_ENDSUBSIDY[i]),(void *)&ASSETCHAINS_ENDSUBSIDY[i]);
1731                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_REWARD[i]),(void *)&ASSETCHAINS_REWARD[i]);
1732                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_HALVING[i]),(void *)&ASSETCHAINS_HALVING[i]);
1733                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_DECAY[i]),(void *)&ASSETCHAINS_DECAY[i]);
1734             }
1735
1736             if (ASSETCHAINS_LASTERA > 0)
1737             {
1738                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_LASTERA),(void *)&ASSETCHAINS_LASTERA);
1739             }
1740
1741             // hash in lock above for time locked coinbase transactions above a certain reward value only if the lock above
1742             // param was specified, otherwise, for compatibility, do nothing
1743             if ( ASSETCHAINS_TIMELOCKGTE != _ASSETCHAINS_TIMELOCKOFF )
1744             {
1745                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_TIMELOCKGTE),(void *)&ASSETCHAINS_TIMELOCKGTE);
1746                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_TIMEUNLOCKFROM),(void *)&ASSETCHAINS_TIMEUNLOCKFROM);
1747                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_TIMEUNLOCKTO),(void *)&ASSETCHAINS_TIMEUNLOCKTO);
1748             }
1749
1750             if ( ASSETCHAINS_ALGO != ASSETCHAINS_EQUIHASH )
1751             {
1752                 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_ALGO),(void *)&ASSETCHAINS_ALGO);
1753             }
1754
1755             val = ASSETCHAINS_COMMISSION | (((uint64_t)ASSETCHAINS_STAKED & 0xff) << 32) | (((uint64_t)ASSETCHAINS_CC & 0xffffff) << 40);
1756             extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(val),(void *)&val);
1757         }
1758
1759         addn = GetArg("-seednode","");
1760         if ( strlen(addn.c_str()) > 0 )
1761             ASSETCHAINS_SEED = 1;
1762         strncpy(ASSETCHAINS_SYMBOL,name.c_str(),sizeof(ASSETCHAINS_SYMBOL)-1);
1763
1764         MAX_MONEY = komodo_max_money();
1765
1766         //printf("baseid.%d MAX_MONEY.%s %.8f\n",baseid,ASSETCHAINS_SYMBOL,(double)MAX_MONEY/SATOSHIDEN);
1767         ASSETCHAINS_P2PPORT = komodo_port(ASSETCHAINS_SYMBOL,ASSETCHAINS_SUPPLY,&ASSETCHAINS_MAGIC,extraptr,extralen);
1768         while ( (dirname= (char *)GetDataDir(false).string().c_str()) == 0 || dirname[0] == 0 )
1769         {
1770             fprintf(stderr,"waiting for datadir\n");
1771                                                 #ifndef _WIN32
1772             sleep(3);
1773                                                 #else
1774                                                 boost::this_thread::sleep(boost::posix_time::milliseconds(3000));
1775                                                 #endif
1776         }
1777         //fprintf(stderr,"Got datadir.(%s)\n",dirname);
1778         if ( ASSETCHAINS_SYMBOL[0] != 0 )
1779         {
1780             int32_t komodo_baseid(char *origbase);
1781             extern int COINBASE_MATURITY;
1782             if ( (port= komodo_userpass(ASSETCHAINS_USERPASS,ASSETCHAINS_SYMBOL)) != 0 )
1783                 ASSETCHAINS_RPCPORT = port;
1784             else komodo_configfile(ASSETCHAINS_SYMBOL,ASSETCHAINS_P2PPORT + 1);
1785             if (ASSETCHAINS_LASTERA == 0 && ASSETCHAINS_REWARD[0] == 0)
1786                 COINBASE_MATURITY = 1;
1787             //fprintf(stderr,"ASSETCHAINS_RPCPORT (%s) %u\n",ASSETCHAINS_SYMBOL,ASSETCHAINS_RPCPORT);
1788         }
1789         if ( ASSETCHAINS_RPCPORT == 0 )
1790             ASSETCHAINS_RPCPORT = ASSETCHAINS_P2PPORT + 1;
1791         //ASSETCHAINS_NOTARIES = GetArg("-ac_notaries","");
1792         //komodo_assetchain_pubkeys((char *)ASSETCHAINS_NOTARIES.c_str());
1793         iguana_rwnum(1,magic,sizeof(ASSETCHAINS_MAGIC),(void *)&ASSETCHAINS_MAGIC);
1794         for (i=0; i<4; i++)
1795             sprintf(&magicstr[i<<1],"%02x",magic[i]);
1796         magicstr[8] = 0;
1797 #ifndef FROM_CLI
1798         sprintf(fname,"%s_7776",ASSETCHAINS_SYMBOL);
1799         if ( (fp= fopen(fname,"wb")) != 0 )
1800         {
1801             fprintf(fp,iguanafmtstr,name.c_str(),name.c_str(),name.c_str(),name.c_str(),magicstr,ASSETCHAINS_P2PPORT,ASSETCHAINS_RPCPORT,"78.47.196.146");
1802             fclose(fp);
1803             //printf("created (%s)\n",fname);
1804         } else printf("error creating (%s)\n",fname);
1805 #endif
1806     }
1807     else
1808     {
1809         char fname[512],username[512],password[4096]; int32_t iter; FILE *fp;
1810         ASSETCHAINS_P2PPORT = 7770;
1811         ASSETCHAINS_RPCPORT = 7771;
1812         for (iter=0; iter<2; iter++)
1813         {
1814             strcpy(fname,GetDataDir().string().c_str());
1815 #ifdef _WIN32
1816             while ( fname[strlen(fname)-1] != '\\' )
1817                 fname[strlen(fname)-1] = 0;
1818             if ( iter == 0 )
1819                 strcat(fname,"Komodo\\komodo.conf");
1820             else strcat(fname,"Bitcoin\\bitcoin.conf");
1821 #else
1822             while ( fname[strlen(fname)-1] != '/' )
1823                 fname[strlen(fname)-1] = 0;
1824 #ifdef __APPLE__
1825             if ( iter == 0 )
1826                 strcat(fname,"Komodo/Komodo.conf");
1827             else strcat(fname,"Bitcoin/Bitcoin.conf");
1828 #else
1829             if ( iter == 0 )
1830                 strcat(fname,".komodo/komodo.conf");
1831             else strcat(fname,".bitcoin/bitcoin.conf");
1832 #endif
1833 #endif
1834             if ( (fp= fopen(fname,"rb")) != 0 )
1835             {
1836                 _komodo_userpass(username,password,fp);
1837                 sprintf(iter == 0 ? KMDUSERPASS : BTCUSERPASS,"%s:%s",username,password);
1838                 fclose(fp);
1839                 //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS);
1840             } //else printf("couldnt open.(%s)\n",fname);
1841             if ( IS_KOMODO_NOTARY == 0 )
1842                 break;
1843         }
1844     }
1845     if ( ASSETCHAINS_SYMBOL[0] != 0 )
1846     {
1847         BITCOIND_RPCPORT = GetArg("-rpcport", ASSETCHAINS_RPCPORT);
1848         //fprintf(stderr,"(%s) port.%u chain params initialized\n",ASSETCHAINS_SYMBOL,BITCOIND_RPCPORT);
1849     } else BITCOIND_RPCPORT = GetArg("-rpcport", BaseParams().RPCPort());
1850 }
1851
1852 void komodo_nameset(char *symbol,char *dest,char *source)
1853 {
1854     if ( source[0] == 0 )
1855     {
1856         strcpy(symbol,(char *)"KMD");
1857         strcpy(dest,(char *)"BTC");
1858     }
1859     else
1860     {
1861         strcpy(symbol,source);
1862         strcpy(dest,(char *)"KMD");
1863     }
1864 }
1865
1866 struct komodo_state *komodo_stateptrget(char *base)
1867 {
1868     int32_t baseid;
1869     if ( base == 0 || base[0] == 0 || strcmp(base,(char *)"KMD") == 0 )
1870         return(&KOMODO_STATES[33]);
1871     else if ( (baseid= komodo_baseid(base)) >= 0 )
1872         return(&KOMODO_STATES[baseid+1]);
1873     else return(&KOMODO_STATES[0]);
1874 }
1875
1876 struct komodo_state *komodo_stateptr(char *symbol,char *dest)
1877 {
1878     int32_t baseid;
1879     komodo_nameset(symbol,dest,ASSETCHAINS_SYMBOL);
1880     return(komodo_stateptrget(symbol));
1881 }
1882
1883 void komodo_prefetch(FILE *fp)
1884 {
1885     long fsize,fpos; int32_t incr = 16*1024*1024;
1886     fpos = ftell(fp);
1887     fseek(fp,0,SEEK_END);
1888     fsize = ftell(fp);
1889     if ( fsize > incr )
1890     {
1891         char *ignore = (char *)malloc(incr);
1892         if ( ignore != 0 )
1893         {
1894             rewind(fp);
1895             while ( fread(ignore,1,incr,fp) == incr ) // prefetch
1896                 fprintf(stderr,".");
1897             free(ignore);
1898         }
1899     }
1900     fseek(fp,fpos,SEEK_SET);
1901 }
1902
This page took 0.13366 seconds and 4 git commands to generate.