]> Git Repo - VerusCoin.git/blame - src/komodo_utils.h
Reset array count to zero
[VerusCoin.git] / src / komodo_utils.h
CommitLineData
d019c447 1/******************************************************************************
713c2a94 2 * Copyright © 2014-2018 The SuperNET Developers. *
d019c447 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 ******************************************************************************/
5416af1d 15#include "komodo_defs.h"
42181656 16#include <string.h>
5416af1d 17
9d365796 18#ifdef _WIN32
19#include <sodium.h>
20#include <boost/date_time/posix_time/posix_time.hpp>
21#include <boost/thread.hpp>
22#endif
3eea72f2 23
453e1052 24#define SATOSHIDEN ((uint64_t)100000000L)
32b1b443 25#define dstr(x) ((double)(x) / SATOSHIDEN)
b67b154f 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
32b1b443 30
2552dbb4 31struct allocitem { uint32_t allocsize,type; };
0a912c89 32struct queueitem { struct queueitem *next,*prev; uint32_t allocsize,type; };
33
a18fbdc5 34typedef struct queue
35{
36 struct queueitem *list;
0a912c89 37 pthread_mutex_t mutex;
a18fbdc5 38 char name[64],initflag;
39} queue_t;
40
d019c447 41#include "mini-gmp.c"
3eea72f2 42
43#define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9"
b62d7030 44#define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA"
878412df 45#define CRYPTO777_RMD160STR "f1dce4182fce875748c4986b240ff7d7bc3fffb0"
b62d7030 46
d019c447 47#define KOMODO_PUBTYPE 60
48
d019c447 49struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; };
50struct 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
110static 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];
9d365796 119
d019c447 120#define RND(a,b,c,d,e,f,g,h,i,ki) \
121t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
122t1 = Sigma0(a) + Maj(a, b, c); \
123d += t0; \
124h = t0 + t1;
9d365796 125
d019c447 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
206static 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
220static 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
250static 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
274void 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
282bits256 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
52198432 292
d019c447 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
352static 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;
9d365796 356
d019c447 357 /* load words X */
358 for (i = 0; i < 16; i++){
359 LOAD32L(X[i], buf + (4 * i));
360 }
9d365796 361
d019c447 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];
9d365796 368
d019c447 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);
9d365796 386
d019c447 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);
9d365796 404
d019c447 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);
9d365796 422
d019c447 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);
9d365796 440
d019c447 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);
9d365796 458
d019c447 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);
9d365796 476
d019c447 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);
9d365796 494
d019c447 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);
9d365796 512
d019c447 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);
9d365796 530
d019c447 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);
9d365796 548
d019c447 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;
9d365796 556
d019c447 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 */
565int 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) \
577int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen) \
578{ \
579unsigned long n; \
580int err; \
581if (md->curlen > sizeof(md->buf)) { \
582return -1; \
583} \
584while (inlen > 0) { \
585if (md->curlen == 0 && inlen >= block_size) { \
586if ((err = compress_name (md, (unsigned char *)in)) != 0) { \
587return err; \
588} \
589md->length += block_size * 8; \
590in += block_size; \
591inlen -= block_size; \
592} else { \
593n = MIN(inlen, (block_size - md->curlen)); \
594memcpy(md->buf + md->curlen, in, (size_t)n); \
595md->curlen += n; \
596in += n; \
597inlen -= n; \
598if (md->curlen == block_size) { \
599if ((err = compress_name (md, md->buf)) != 0) { \
600return err; \
601} \
602md->length += 8*block_size; \
603md->curlen = 0; \
604} \
605} \
606} \
607return 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 */
617HASH_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 */
625int 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;
9d365796 633
d019c447 634 /* append the '1' bit */
635 md->buf[md->curlen++] = (unsigned char)0x80;
9d365796 636
d019c447 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
662void 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}
52198432 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
3eea72f2 685
1e81ccb7 686static 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
732uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size)
733{
734 const uint8_t *p;
9d365796 735
1e81ccb7 736 p = (const uint8_t *)buf;
737 crc = crc ^ ~0U;
9d365796 738
1e81ccb7 739 while (size--)
740 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
9d365796 741
1e81ccb7 742 return crc ^ ~0U;
743}
744
d019c447 745void 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
752int32_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]);
7637aa7f 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]);
d019c447 779 }
780 }
781 return(0);
782}
783
784char *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
8e3430ee 805int32_t komodo_is_issuer()
806{
654dfaaf 807 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) >= 0 )
8e3430ee 808 return(1);
809 else return(0);
810}
811
d2a115d8 812int32_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
1e81ccb7 821int32_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
832int32_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
849int32_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
859unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); }
860
861int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex)
862{
863 int32_t adjust,i = 0;
864 //printf("decode.(%s)\n",hex);
4f569c79 865 if ( is_hexstr(hex,n) <= 0 )
1e81ccb7 866 {
867 memset(bytes,0,n);
868 return(n);
869 }
e37f280f 870 if ( hex[n-1] == '\n' || hex[n-1] == '\r' )
4f569c79 871 hex[--n] = 0;
1e81ccb7 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
32b1b443 892char 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
902int32_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
921char *bits256_str(char hexstr[65],bits256 x)
922{
923 init_hexbytes_noT(hexstr,x.bytes,sizeof(x));
924 return(hexstr);
925}
926
1e81ccb7 927int32_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
962int32_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
64bb0834 978int32_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 {
4206dee5 987 opretlen = script[len] + (script[len+1] << 8);
988 len += 2;
989 //opretlen = script[len++];
990 //opretlen = (opretlen << 8) | script[len++];
64bb0834 991 }
992 }
993 *opretlenp = opretlen;
994 return(len);
995}
996
1e81ccb7 997int32_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++;
29bd53a1 1002 if ( opretlen >= 0x4c )
1e81ccb7 1003 {
1004 if ( opretlen > 0xff )
1005 {
29bd53a1 1006 script[offset++] = 0x4d;
1e81ccb7 1007 script[offset++] = opretlen & 0xff;
1008 script[offset++] = (opretlen >> 8) & 0xff;
1009 }
1010 else
1011 {
29bd53a1 1012 script[offset++] = 0x4c;
1e81ccb7 1013 script[offset++] = opretlen;
1014 }
1015 } else script[offset++] = opretlen;
795751ec 1016 script[offset++] = type; // covered by opretlen
1017 memcpy(&script[offset],opret,opretlen-1);
1018 return(offset + opretlen - 1);
1e81ccb7 1019}
438d3f35 1020
dba84931
MT
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
1f722359 1023uint64_t komodo_block_prg(uint32_t nHeight)
dba84931
MT
1024{
1025 int i;
1026 uint8_t hashSrc[8];
a0dd01bc 1027 uint64_t result, hashSrc64 = (uint64_t)ASSETCHAINS_MAGIC << 32 + nHeight;
dba84931
MT
1028 bits256 hashResult;
1029
1030 for ( i = 0; i < sizeof(hashSrc); i++ )
1031 {
1032 hashSrc[i] = hashSrc64 & 0xff;
1033 hashSrc64 >>= 8;
1034 }
a0dd01bc 1035 vcalc_sha256(0, hashResult.bytes, hashSrc, sizeof(hashSrc));
dba84931 1036
a0dd01bc 1037 for ( i = 0; i < 8; i++ )
dba84931 1038 {
a0dd01bc 1039 result = (result << 8) + hashResult.bytes[i];
dba84931
MT
1040 }
1041 return(result);
1042}
1043
abb90a89
MT
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
29bd53a1 1047int64_t komodo_block_unlocktime(uint32_t nHeight)
dba84931 1048{
abb90a89 1049 uint64_t fromTime, toTime, unlocktime;
dba84931 1050
abb90a89
MT
1051 if ( ASSETCHAINS_TIMEUNLOCKFROM == ASSETCHAINS_TIMEUNLOCKTO )
1052 unlocktime = ASSETCHAINS_TIMEUNLOCKTO;
dba84931
MT
1053 else
1054 {
74329e19
MT
1055 if (strcmp(ASSETCHAINS_SYMBOL, "VRSC") != 0 || nHeight >= 12800)
1056 {
1057 unlocktime = komodo_block_prg(nHeight) % (ASSETCHAINS_TIMEUNLOCKTO - ASSETCHAINS_TIMEUNLOCKFROM);
1058 // boundary and power of 2 can make it exceed to time by 1
1059 unlocktime = unlocktime + ASSETCHAINS_TIMEUNLOCKFROM;
1060 }
1061 else
1062 {
1063 unlocktime = komodo_block_prg(nHeight) / (0xffffffffffffffff / ((ASSETCHAINS_TIMEUNLOCKTO - ASSETCHAINS_TIMEUNLOCKFROM) + 1));
1064 // boundary and power of 2 can make it exceed to time by 1
1065 unlocktime = unlocktime + ASSETCHAINS_TIMEUNLOCKFROM;
1066 if (unlocktime > ASSETCHAINS_TIMEUNLOCKTO)
1067 unlocktime--;
1068 }
dba84931 1069 }
29bd53a1 1070 return ((int64_t)unlocktime);
992fdc3b
MT
1071}
1072
fd1799a6 1073long _stripwhite(char *buf,int accept)
1074{
1075 int32_t i,j,c;
1076 if ( buf == 0 || buf[0] == 0 )
1077 return(0);
1078 for (i=j=0; buf[i]!=0; i++)
1079 {
1080 buf[j] = c = buf[i];
1081 if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') )
1082 j++;
1083 }
1084 buf[j] = 0;
1085 return(j);
1086}
1087
2c36c760 1088char *clonestr(char *str)
1089{
1090 char *clone;
1091 if ( str == 0 || str[0] == 0 )
1092 {
1093 printf("warning cloning nullstr.%p\n",str);
1094#ifdef __APPLE__
1095 while ( 1 ) sleep(1);
1096#endif
1097 str = (char *)"<nullstr>";
1098 }
1099 clone = (char *)malloc(strlen(str)+16);
1100 strcpy(clone,str);
1101 return(clone);
1102}
1103
f3d5295c 1104int32_t safecopy(char *dest,char *src,long len)
1105{
1106 int32_t i = -1;
1107 if ( src != 0 && dest != 0 && src != dest )
1108 {
1109 if ( dest != 0 )
1110 memset(dest,0,len);
1111 for (i=0; i<len&&src[i]!=0; i++)
1112 dest[i] = src[i];
1113 if ( i == len )
1114 {
1115 printf("safecopy: %s too long %ld\n",src,len);
1116#ifdef __APPLE__
1117 //getchar();
1118#endif
1119 return(-1);
1120 }
1121 dest[i] = 0;
1122 }
1123 return(i);
1124}
1125
fd1799a6 1126char *parse_conf_line(char *line,char *field)
1127{
1128 line += strlen(field);
1129 for (; *line!='='&&*line!=0; line++)
1130 break;
1131 if ( *line == 0 )
1132 return(0);
1133 if ( *line == '=' )
1134 line++;
83c2d095 1135 while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' )
5ae7efc1 1136 line[strlen(line)-1] = 0;
e78fb5d3 1137 //printf("LINE.(%s)\n",line);
fd1799a6 1138 _stripwhite(line,0);
1139 return(clonestr(line));
1140}
1141
38550ae9 1142double OS_milliseconds()
1143{
1144 struct timeval tv; double millis;
1145 gettimeofday(&tv,NULL);
1146 millis = ((double)tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.);
1147 //printf("tv_sec.%ld usec.%d %f\n",tv.tv_sec,tv.tv_usec,millis);
1148 return(millis);
1149}
1150
9d365796 1151#ifndef _WIN32
f9557624 1152void OS_randombytes(unsigned char *x,long xlen)
5e39a3aa 1153{
1154 static int fd = -1;
1155 int32_t i;
1156 if (fd == -1) {
1157 for (;;) {
1158 fd = open("/dev/urandom",O_RDONLY);
1159 if (fd != -1) break;
1160 sleep(1);
1161 }
1162 }
1163 while (xlen > 0) {
1164 if (xlen < 1048576) i = (int32_t)xlen; else i = 1048576;
1165 i = (int32_t)read(fd,x,i);
1166 if (i < 1) {
1167 sleep(1);
1168 continue;
1169 }
1170 if ( 0 )
1171 {
1172 int32_t j;
1173 for (j=0; j<i; j++)
1174 printf("%02x ",x[j]);
1175 printf("-> %p\n",x);
1176 }
1177 x += i;
1178 xlen -= i;
1179 }
1180}
9d365796 1181#endif
5e39a3aa 1182
429dabb5 1183void lock_queue(queue_t *queue)
1184{
1185 if ( queue->initflag == 0 )
1186 {
1187 portable_mutex_init(&queue->mutex);
1188 queue->initflag = 1;
1189 }
1190 portable_mutex_lock(&queue->mutex);
1191}
1192
5fa92ab2 1193void queue_enqueue(char *name,queue_t *queue,struct queueitem *item)
429dabb5 1194{
429dabb5 1195 if ( queue->name[0] == 0 && name != 0 && name[0] != 0 )
5fa92ab2 1196 strcpy(queue->name,name);
1197 if ( item == 0 )
429dabb5 1198 {
5fa92ab2 1199 printf("FATAL type error: queueing empty value\n");
429dabb5 1200 return;
1201 }
429dabb5 1202 lock_queue(queue);
429dabb5 1203 DL_APPEND(queue->list,item);
1204 portable_mutex_unlock(&queue->mutex);
429dabb5 1205}
1206
5fa92ab2 1207struct queueitem *queue_dequeue(queue_t *queue)
429dabb5 1208{
1209 struct queueitem *item = 0;
1210 lock_queue(queue);
1211 if ( queue->list != 0 )
1212 {
1213 item = queue->list;
429dabb5 1214 DL_DELETE(queue->list,item);
1215 }
1216 portable_mutex_unlock(&queue->mutex);
5fa92ab2 1217 return(item);
429dabb5 1218}
1219
405e2bdd 1220void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize)
429dabb5 1221{
429dabb5 1222 struct queueitem *item = 0;
1223 lock_queue(queue);
1224 if ( queue->list != 0 )
1225 {
1226 DL_FOREACH(queue->list,item)
1227 {
9d365796 1228 #ifdef _WIN32
1229 if ( item == copy || (item->allocsize == copysize && memcmp((void *)((intptr_t)item + sizeof(struct queueitem)),(void *)((intptr_t)copy + sizeof(struct queueitem)),copysize) == 0) )
1230 #else
5fa92ab2 1231 if ( item == copy || (item->allocsize == copysize && memcmp((void *)((long)item + sizeof(struct queueitem)),(void *)((long)copy + sizeof(struct queueitem)),copysize) == 0) )
9d365796 1232 #endif
429dabb5 1233 {
1234 DL_DELETE(queue->list,item);
1235 portable_mutex_unlock(&queue->mutex);
5fa92ab2 1236 printf("name.(%s) deleted item.%p list.%p\n",queue->name,item,queue->list);
429dabb5 1237 return(item);
1238 }
1239 }
1240 }
1241 portable_mutex_unlock(&queue->mutex);
1242 return(0);
1243}
1244
1245void *queue_free(queue_t *queue)
1246{
1247 struct queueitem *item = 0;
1248 lock_queue(queue);
1249 if ( queue->list != 0 )
1250 {
1251 DL_FOREACH(queue->list,item)
1252 {
1253 DL_DELETE(queue->list,item);
5fa92ab2 1254 free(item);
429dabb5 1255 }
1256 //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1257 }
1258 portable_mutex_unlock(&queue->mutex);
1259 return(0);
1260}
1261
1262void *queue_clone(queue_t *clone,queue_t *queue,int32_t size)
1263{
1264 struct queueitem *ptr,*item = 0;
1265 lock_queue(queue);
1266 if ( queue->list != 0 )
1267 {
1268 DL_FOREACH(queue->list,item)
1269 {
5fa92ab2 1270 ptr = (struct queueitem *)calloc(1,sizeof(*ptr));
429dabb5 1271 memcpy(ptr,item,size);
a856c56a 1272 queue_enqueue(queue->name,clone,ptr);
429dabb5 1273 }
1274 //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1275 }
1276 portable_mutex_unlock(&queue->mutex);
1277 return(0);
1278}
1279
1280int32_t queue_size(queue_t *queue)
1281{
1282 int32_t count = 0;
1283 struct queueitem *tmp;
1284 lock_queue(queue);
1285 DL_COUNT(queue->list,tmp,count);
1286 portable_mutex_unlock(&queue->mutex);
1287 return count;
1288}
a18fbdc5 1289
1290void iguana_initQ(queue_t *Q,char *name)
1291{
25314868 1292 struct queueitem *item,*I;
a18fbdc5 1293 memset(Q,0,sizeof(*Q));
96d1da77 1294 I = (struct queueitem *)calloc(1,sizeof(*I));
a18fbdc5 1295 strcpy(Q->name,name);
25314868 1296 queue_enqueue(name,Q,I);
5fa92ab2 1297 if ( (item= queue_dequeue(Q)) != 0 )
1298 free(item);
a18fbdc5 1299}
561ff778 1300
34775d99 1301uint16_t _komodo_userpass(char *username,char *password,FILE *fp)
561ff778 1302{
05e307ec 1303 char *rpcuser,*rpcpassword,*str,line[8192]; uint16_t port = 0;
561ff778 1304 rpcuser = rpcpassword = 0;
1305 username[0] = password[0] = 0;
1306 while ( fgets(line,sizeof(line),fp) != 0 )
1307 {
1308 if ( line[0] == '#' )
1309 continue;
1310 //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword"));
1311 if ( (str= strstr(line,(char *)"rpcuser")) != 0 )
1312 rpcuser = parse_conf_line(str,(char *)"rpcuser");
1313 else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 )
1314 rpcpassword = parse_conf_line(str,(char *)"rpcpassword");
05e307ec 1315 else if ( (str= strstr(line,(char *)"rpcport")) != 0 )
1316 {
1317 port = atoi(parse_conf_line(str,(char *)"rpcport"));
9c117b3d 1318 //fprintf(stderr,"rpcport.%u in file\n",port);
05e307ec 1319 }
561ff778 1320 }
1321 if ( rpcuser != 0 && rpcpassword != 0 )
1322 {
1323 strcpy(username,rpcuser);
1324 strcpy(password,rpcpassword);
1325 }
1326 //printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port);
1327 if ( rpcuser != 0 )
1328 free(rpcuser);
1329 if ( rpcpassword != 0 )
1330 free(rpcpassword);
05e307ec 1331 return(port);
561ff778 1332}
1333
0ded57c8 1334void komodo_statefname(char *fname,char *symbol,char *str)
ab918767 1335{
1336 int32_t n,len;
1337 sprintf(fname,"%s",GetDataDir(false).string().c_str());
4eb91c7a 1338 if ( (n= (int32_t)strlen(ASSETCHAINS_SYMBOL)) != 0 )
ab918767 1339 {
1340 len = (int32_t)strlen(fname);
29255d5f 1341 if ( strcmp(ASSETCHAINS_SYMBOL,&fname[len - n]) == 0 )
ab918767 1342 fname[len - n] = 0;
ab918767 1343 else
1344 {
a4ddd789 1345 printf("unexpected fname.(%s) vs %s [%s] n.%d len.%d (%s)\n",fname,symbol,ASSETCHAINS_SYMBOL,n,len,&fname[len - n]);
ab918767 1346 return;
1347 }
1348 }
643d946e 1349 else
1350 {
9cb1ec9c 1351#ifdef _WIN32
643d946e 1352 strcat(fname,"\\");
20533bd8 1353#else
643d946e 1354 strcat(fname,"/");
20533bd8 1355#endif
643d946e 1356 }
1357 if ( symbol != 0 && symbol[0] != 0 && strcmp("KMD",symbol) != 0 )
1358 {
413d04bf 1359 strcat(fname,symbol);
643d946e 1360 //printf("statefname.(%s) -> (%s)\n",symbol,fname);
9cb1ec9c 1361#ifdef _WIN32
643d946e 1362 strcat(fname,"\\");
1740e3aa 1363#else
643d946e 1364 strcat(fname,"/");
1740e3aa 1365#endif
643d946e 1366 }
0ded57c8 1367 strcat(fname,str);
643d946e 1368 //printf("test.(%s) -> [%s] statename.(%s) %s\n",test,ASSETCHAINS_SYMBOL,symbol,fname);
ab918767 1369}
1370
05c2ba63 1371void komodo_configfile(char *symbol,uint16_t rpcport)
561ff778 1372{
1373 static char myusername[512],mypassword[8192];
9850ab03 1374 FILE *fp; uint16_t kmdport; uint8_t buf2[33]; char fname[512],buf[128],username[512],password[8192]; uint32_t crc,r,r2,i;
3ede6dd3 1375 if ( symbol != 0 && rpcport != 0 )
347acd7f 1376 {
1377 r = (uint32_t)time(NULL);
1378 r2 = OS_milliseconds();
1379 memcpy(buf,&r,sizeof(r));
1380 memcpy(&buf[sizeof(r)],&r2,sizeof(r2));
1381 memcpy(&buf[sizeof(r)+sizeof(r2)],symbol,strlen(symbol));
1382 crc = calc_crc32(0,(uint8_t *)buf,(int32_t)(sizeof(r)+sizeof(r2)+strlen(symbol)));
9d365796 1383 #ifdef _WIN32
1384 randombytes_buf(buf2,sizeof(buf2));
1385 #else
347acd7f 1386 OS_randombytes(buf2,sizeof(buf2));
9d365796 1387 #endif
347acd7f 1388 for (i=0; i<sizeof(buf2); i++)
1389 sprintf(&password[i*2],"%02x",buf2[i]);
1390 password[i*2] = 0;
1391 sprintf(buf,"%s.conf",symbol);
346d96b9 1392 BITCOIND_RPCPORT = rpcport;
9cb1ec9c 1393#ifdef _WIN32
c1428363 1394 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),buf);
561ff778 1395#else
c1428363 1396 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),buf);
561ff778 1397#endif
347acd7f 1398 if ( (fp= fopen(fname,"rb")) == 0 )
1399 {
95c46396 1400#ifndef FROM_CLI
347acd7f 1401 if ( (fp= fopen(fname,"wb")) != 0 )
1402 {
346d96b9 1403 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);
347acd7f 1404 fclose(fp);
1405 printf("Created (%s)\n",fname);
1406 } else printf("Couldnt create (%s)\n",fname);
95c46396 1407#endif
347acd7f 1408 }
1409 else
561ff778 1410 {
34775d99 1411 _komodo_userpass(myusername,mypassword,fp);
347acd7f 1412 mapArgs["-rpcpassword"] = mypassword;
1413 mapArgs["-rpcusername"] = myusername;
1414 //fprintf(stderr,"myusername.(%s)\n",myusername);
561ff778 1415 fclose(fp);
347acd7f 1416 }
561ff778 1417 }
cad9f4c8 1418 strcpy(fname,GetDataDir().string().c_str());
9cb1ec9c 1419#ifdef _WIN32
561ff778 1420 while ( fname[strlen(fname)-1] != '\\' )
1421 fname[strlen(fname)-1] = 0;
323832c3 1422 strcat(fname,"komodo.conf");
561ff778 1423#else
1424 while ( fname[strlen(fname)-1] != '/' )
1425 fname[strlen(fname)-1] = 0;
b55c2367 1426#ifdef __APPLE__
1427 strcat(fname,"Komodo.conf");
1428#else
323832c3 1429 strcat(fname,"komodo.conf");
b55c2367 1430#endif
561ff778 1431#endif
561ff778 1432 if ( (fp= fopen(fname,"rb")) != 0 )
1433 {
34775d99 1434 if ( (kmdport= _komodo_userpass(username,password,fp)) != 0 )
9850ab03 1435 KMD_PORT = kmdport;
561ff778 1436 sprintf(KMDUSERPASS,"%s:%s",username,password);
1437 fclose(fp);
43cc3df6 1438//printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS);
6ec3d43c 1439 } //else printf("couldnt open.(%s)\n",fname);
561ff778 1440}
1441
05e307ec 1442uint16_t komodo_userpass(char *userpass,char *symbol)
0ded57c8 1443{
7c130297 1444 FILE *fp; uint16_t port = 0; char fname[512],username[512],password[512],confname[KOMODO_ASSETCHAIN_MAXLEN];
0ded57c8 1445 userpass[0] = 0;
609fbcc2 1446 if ( strcmp("KMD",symbol) == 0 )
b55c2367 1447 {
1448#ifdef __APPLE__
1449 sprintf(confname,"Komodo.conf");
1450#else
609fbcc2 1451 sprintf(confname,"komodo.conf");
b55c2367 1452#endif
1453 }
609fbcc2 1454 else sprintf(confname,"%s.conf",symbol);
0ded57c8 1455 komodo_statefname(fname,symbol,confname);
1456 if ( (fp= fopen(fname,"rb")) != 0 )
1457 {
34775d99 1458 port = _komodo_userpass(username,password,fp);
0ded57c8 1459 sprintf(userpass,"%s:%s",username,password);
a643c078 1460 if ( strcmp(symbol,ASSETCHAINS_SYMBOL) == 0 )
1461 strcpy(ASSETCHAINS_USERPASS,userpass);
0ded57c8 1462 fclose(fp);
0ded57c8 1463 }
05e307ec 1464 return(port);
0ded57c8 1465}
1466
8683bd8d 1467uint32_t komodo_assetmagic(char *symbol,uint64_t supply,uint8_t *extraptr,int32_t extralen)
3aca2705 1468{
8683bd8d 1469 uint8_t buf[512]; uint32_t crc0=0; int32_t len = 0; bits256 hash;
550824a8 1470 if ( strcmp(symbol,"KMD") == 0 )
1471 return(0x8de4eef9);
3aca2705 1472 len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply);
1473 strcpy((char *)&buf[len],symbol);
1474 len += strlen(symbol);
8683bd8d 1475 if ( extraptr != 0 && extralen != 0 )
1476 {
1477 vcalc_sha256(0,hash.bytes,extraptr,extralen);
1478 crc0 = hash.uints[0];
1479 }
1480 return(calc_crc32(crc0,buf,len));
3aca2705 1481}
1482
2cd57608 1483uint16_t komodo_assetport(uint32_t magic,int32_t extralen)
3aca2705 1484{
550824a8 1485 if ( magic == 0x8de4eef9 )
950baa0e 1486 return(7770);
2cd57608 1487 else if ( extralen == 0 )
1488 return(8000 + (magic % 7777));
1489 else return(16000 + (magic % 49500));
3aca2705 1490}
1491
8683bd8d 1492uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp,uint8_t *extraptr,int32_t extralen)
3aca2705 1493{
550824a8 1494 if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 )
1495 {
1496 *magicp = 0x8de4eef9;
950baa0e 1497 return(7770);
550824a8 1498 }
8683bd8d 1499 *magicp = komodo_assetmagic(symbol,supply,extraptr,extralen);
2cd57608 1500 return(komodo_assetport(*magicp,extralen));
3aca2705 1501}
1502
8683bd8d 1503/*void komodo_ports(uint16_t ports[MAX_CURRENCIES])
3aca2705 1504{
e7a90cf9 1505 int32_t i; uint32_t magic;
3aca2705 1506 for (i=0; i<MAX_CURRENCIES; i++)
1507 {
e7a90cf9 1508 ports[i] = komodo_port(CURRENCIES[i],10,&magic);
3aca2705 1509 printf("%u ",ports[i]);
1510 }
1511 printf("ports\n");
8683bd8d 1512}*/
3aca2705 1513
69a1f515 1514char *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\\\"}\"";
7b7049d3 1515
ab8ef450 1516
8683bd8d 1517int32_t komodo_whoami(char *pubkeystr,int32_t height,uint32_t timestamp)
58033467 1518{
9d365796 1519 int32_t i,notaryid;
58033467 1520 for (i=0; i<33; i++)
c96e160a 1521 sprintf(&pubkeystr[i<<1],"%02x",NOTARY_PUBKEY33[i]);
58033467 1522 pubkeystr[66] = 0;
8683bd8d 1523 komodo_chosennotary(&notaryid,height,NOTARY_PUBKEY33,timestamp);
58033467 1524 return(notaryid);
1525}
1526
34017212 1527char *argv0suffix[] =
1528{
0b7ef289 1529 (char *)"mnzd", (char *)"mnz-cli", (char *)"mnzd.exe", (char *)"mnz-cli.exe", (char *)"btchd", (char *)"btch-cli", (char *)"btchd.exe", (char *)"btch-cli.exe"
34017212 1530};
1531
1532char *argv0names[] =
1533{
0b7ef289 1534 (char *)"MNZ", (char *)"MNZ", (char *)"MNZ", (char *)"MNZ", (char *)"BTCH", (char *)"BTCH", (char *)"BTCH", (char *)"BTCH"
34017212 1535};
1536
36647d52
MT
1537int64_t komodo_max_money()
1538{
48d800c2 1539 return komodo_current_supply(10000000);
36647d52
MT
1540}
1541
1542uint64_t komodo_ac_block_subsidy(int nHeight)
1543{
1544 // we have to find our era, start from beginning reward, and determine current subsidy
a0dd01bc 1545 int64_t numerator, denominator, subsidy = 0;
3e312ace 1546 int64_t subsidyDifference;
a0dd01bc 1547 int32_t numhalvings, curEra = 0, sign = 1;
36647d52
MT
1548 static uint64_t cached_subsidy; static int32_t cached_numhalvings; static int cached_era;
1549
3e312ace 1550 // check for backwards compat, older chains with no explicit rewards had 0.0001 block reward
18ec2a38 1551 if ( ASSETCHAINS_ENDSUBSIDY[0] == 0 && ASSETCHAINS_REWARD[0] == 0 )
3e312ace 1552 subsidy = 10000;
18ec2a38 1553 else if ( (ASSETCHAINS_ENDSUBSIDY[0] == 0 && ASSETCHAINS_REWARD[0] != 0) || ASSETCHAINS_ENDSUBSIDY[0] != 0 )
36647d52
MT
1554 {
1555 // if we have an end block in the first era, find our current era
1556 if ( ASSETCHAINS_ENDSUBSIDY[0] != 0 )
1557 {
6b78e01d 1558 for ( curEra = 0; curEra <= ASSETCHAINS_LASTERA; curEra++ )
36647d52
MT
1559 {
1560 if ( ASSETCHAINS_ENDSUBSIDY[curEra] > nHeight || ASSETCHAINS_ENDSUBSIDY[curEra] == 0 )
1561 break;
1562 }
1563 }
6b78e01d 1564 if ( curEra <= ASSETCHAINS_LASTERA )
36647d52 1565 {
48d800c2 1566 int64_t nStart = curEra ? ASSETCHAINS_ENDSUBSIDY[curEra - 1] : 0;
3e312ace 1567 subsidy = (int64_t)ASSETCHAINS_REWARD[curEra];
a0dd01bc 1568 if ( subsidy || (curEra != ASSETCHAINS_LASTERA && ASSETCHAINS_REWARD[curEra + 1] != 0) )
36647d52
MT
1569 {
1570 if ( ASSETCHAINS_HALVING[curEra] != 0 )
1571 {
1572 if ( (numhalvings = ((nHeight - nStart) / ASSETCHAINS_HALVING[curEra])) > 0 )
1573 {
1574 if ( ASSETCHAINS_DECAY[curEra] == 0 )
3e312ace 1575 subsidy >>= numhalvings;
36647d52
MT
1576 else if ( ASSETCHAINS_DECAY[curEra] == 100000000 && ASSETCHAINS_ENDSUBSIDY[curEra] != 0 )
1577 {
6b78e01d 1578 if ( curEra == ASSETCHAINS_LASTERA )
3e312ace
MT
1579 {
1580 subsidyDifference = subsidy;
1581 }
1582 else
1583 {
1584 // Ex: -ac_eras=3 -ac_reward=0,384,24 -ac_end=1440,260640,0 -ac_halving=1,1440,2103840 -ac_decay 100000000,97750000,0
1585 subsidyDifference = subsidy - ASSETCHAINS_REWARD[curEra + 1];
a0dd01bc 1586 if (subsidyDifference < 0)
1587 {
1588 sign = -1;
1589 subsidyDifference *= sign;
1590 }
3e312ace 1591 }
a0dd01bc 1592 denominator = ASSETCHAINS_ENDSUBSIDY[curEra] - nStart;
1593 numerator = denominator - ((ASSETCHAINS_ENDSUBSIDY[curEra] - nHeight) + ((nHeight - nStart) % ASSETCHAINS_HALVING[curEra]));
1594 subsidy = subsidy - sign * ((subsidyDifference * numerator) / denominator);
36647d52
MT
1595 }
1596 else
1597 {
1598 if ( cached_subsidy > 0 && cached_era == curEra && cached_numhalvings == numhalvings )
3e312ace 1599 subsidy = cached_subsidy;
36647d52
MT
1600 else
1601 {
3e312ace
MT
1602 for (int i=0; i < numhalvings && subsidy != 0; i++)
1603 subsidy = (subsidy * ASSETCHAINS_DECAY[curEra]) / 100000000;
1604 cached_subsidy = subsidy;
36647d52
MT
1605 cached_numhalvings = numhalvings;
1606 cached_era = curEra;
1607 }
1608 }
1609 }
1610 }
1611 }
1612 }
1613 }
1f722359
MT
1614 if ( nHeight == 1 )
1615 if ( ASSETCHAINS_LASTERA == 0 )
1616 subsidy = ASSETCHAINS_SUPPLY * SATOSHIDEN + (ASSETCHAINS_MAGIC & 0xffffff);
1617 else
1618 subsidy += ASSETCHAINS_SUPPLY * SATOSHIDEN + (ASSETCHAINS_MAGIC & 0xffffff);
1619
8c30b1b6 1620 return(subsidy);
36647d52
MT
1621}
1622
34017212 1623void komodo_args(char *argv0)
561ff778 1624{
6f74fa28 1625 extern int64_t MAX_MONEY;
3a19bc18 1626 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;
0e1af8a8 1627 IS_KOMODO_NOTARY = GetBoolArg("-notary", false);
2626c7e3 1628 if ( (KOMODO_EXCHANGEWALLET= GetBoolArg("-exchange", false)) != 0 )
1629 fprintf(stderr,"KOMODO_EXCHANGEWALLET mode active\n");
0e1af8a8 1630 NOTARY_PUBKEY = GetArg("-pubkey", "");
1631 if ( strlen(NOTARY_PUBKEY.c_str()) == 66 )
6cd01056 1632 {
0e1af8a8 1633 USE_EXTERNAL_PUBKEY = 1;
8683bd8d 1634 //KOMODO_PAX = 1;
1635 } //else KOMODO_PAX = GetArg("-pax",0);
0e1af8a8 1636 name = GetArg("-ac_name","");
89559d85 1637 if ( argv0 != 0 )
1638 {
1639 len = (int32_t)strlen(argv0);
1640 for (i=0; i<sizeof(argv0suffix)/sizeof(*argv0suffix); i++)
1641 {
1642 n = (int32_t)strlen(argv0suffix[i]);
1643 if ( strcmp(&argv0[len - n],argv0suffix[i]) == 0 )
1644 {
1645 //printf("ARGV0.(%s) -> matches suffix (%s) -> ac_name.(%s)\n",argv0,argv0suffix[i],argv0names[i]);
1646 name = argv0names[i];
1647 break;
34017212 1648 }
1649 }
1650 }
713c2a94 1651 ASSETCHAINS_CC = GetArg("-ac_cc",0);
acad2983 1652 if ( (KOMODO_REWIND= GetArg("-rewind",0)) != 0 )
4476ddfa 1653 {
1654 printf("KOMODO_REWIND %d\n",KOMODO_REWIND);
acad2983 1655 }
0e1af8a8 1656 if ( name.c_str()[0] != 0 )
561ff778 1657 {
c62d1154 1658 std::string selectedAlgo = GetArg("-ac_algo", std::string(ASSETCHAINS_ALGORITHMS[0]));
42181656 1659
1660 for ( int i = 0; i < ASSETCHAINS_NUMALGOS; i++ )
1661 {
1662 if (std::string(ASSETCHAINS_ALGORITHMS[i]) == selectedAlgo)
1663 {
1664 ASSETCHAINS_ALGO = i;
19120838 1665 // only worth mentioning if it's not equihash
1666 if (ASSETCHAINS_ALGO != ASSETCHAINS_EQUIHASH)
1667 printf("ASSETCHAINS_ALGO, algorithm set to %s\n", selectedAlgo.c_str());
42181656 1668 break;
1669 }
1670 }
1671 if (i == ASSETCHAINS_NUMALGOS)
1672 {
c71dcc71 1673 printf("ASSETCHAINS_ALGO, %s not supported. using equihash\n", selectedAlgo.c_str());
42181656 1674 }
1675
6b78e01d
MT
1676 ASSETCHAINS_LASTERA = GetArg("-ac_eras", 1);
1677 if ( ASSETCHAINS_LASTERA < 1 || ASSETCHAINS_LASTERA > ASSETCHAINS_MAX_ERAS )
8683bd8d 1678 {
6b78e01d
MT
1679 ASSETCHAINS_LASTERA = 1;
1680 printf("ASSETCHAINS_LASTERA, if specified, must be between 1 and %u. ASSETCHAINS_LASTERA set to %u\n", ASSETCHAINS_MAX_ERAS, ASSETCHAINS_LASTERA);
8683bd8d 1681 }
6b78e01d 1682 ASSETCHAINS_LASTERA -= 1;
36647d52 1683
7daf6567
MT
1684 ASSETCHAINS_TIMELOCKGTE = GetArg("-ac_timelockgte", _ASSETCHAINS_TIMELOCKOFF);
1685 ASSETCHAINS_TIMEUNLOCKFROM = GetArg("-ac_timeunlockfrom", 0);
1686 ASSETCHAINS_TIMEUNLOCKTO = GetArg("-ac_timeunlockto", 0);
19120838 1687 if ( ASSETCHAINS_TIMEUNLOCKFROM > ASSETCHAINS_TIMEUNLOCKTO )
a0dd01bc 1688 {
1689 printf("ASSETCHAINS_TIMELOCKGTE - must specify valid ac_timeunlockfrom and ac_timeunlockto\n");
1690 ASSETCHAINS_TIMELOCKGTE = _ASSETCHAINS_TIMELOCKOFF;
1691 ASSETCHAINS_TIMEUNLOCKFROM = ASSETCHAINS_TIMEUNLOCKTO = 0;
1692 }
da94ffe5 1693
26a8d194
MT
1694 Split(GetArg("-ac_end",""), ASSETCHAINS_ENDSUBSIDY, 0);
1695 Split(GetArg("-ac_reward",""), ASSETCHAINS_REWARD, 0);
1696 Split(GetArg("-ac_halving",""), ASSETCHAINS_HALVING, 0);
1697 Split(GetArg("-ac_decay",""), ASSETCHAINS_DECAY, 0);
36647d52 1698
26a8d194 1699 for ( int i = 0; i < ASSETCHAINS_MAX_ERAS; i++ )
8683bd8d 1700 {
36647d52
MT
1701 if ( ASSETCHAINS_DECAY[i] == 100000000 && ASSETCHAINS_ENDSUBSIDY == 0 )
1702 {
1703 ASSETCHAINS_DECAY[i] = 0;
1704 printf("ERA%u: ASSETCHAINS_DECAY of 100000000 means linear and that needs ASSETCHAINS_ENDSUBSIDY\n", i);
1705 }
1706 else if ( ASSETCHAINS_DECAY[i] > 100000000 )
1707 {
1708 ASSETCHAINS_DECAY[i] = 0;
1709 printf("ERA%u: ASSETCHAINS_DECAY cant be more than 100000000\n", i);
1710 }
8683bd8d 1711 }
36647d52 1712
48d800c2 1713 MAX_BLOCK_SIGOPS = 60000;
36647d52
MT
1714 ASSETCHAINS_SUPPLY = GetArg("-ac_supply",10);
1715 ASSETCHAINS_COMMISSION = GetArg("-ac_perc",0);
1716 ASSETCHAINS_OVERRIDE_PUBKEY = GetArg("-ac_pubkey","");
48d800c2 1717 if ( (ASSETCHAINS_STAKED= GetArg("-ac_staked",0)) > 100 )
1718 ASSETCHAINS_STAKED = 100;
1f722359
MT
1719 if ( (ASSETCHAINS_LWMAPOS = GetArg("-ac_veruspos",0)) > 100 )
1720 ASSETCHAINS_LWMAPOS = 100;
36647d52 1721
d209491a 1722 if ( strlen(ASSETCHAINS_OVERRIDE_PUBKEY.c_str()) == 66 && ASSETCHAINS_COMMISSION > 0 && ASSETCHAINS_COMMISSION <= 100000000 )
8683bd8d 1723 decode_hex(ASSETCHAINS_OVERRIDE_PUBKEY33,33,(char *)ASSETCHAINS_OVERRIDE_PUBKEY.c_str());
e8a05f61 1724 else if ( ASSETCHAINS_COMMISSION != 0 )
8683bd8d 1725 {
1726 ASSETCHAINS_COMMISSION = 0;
36647d52 1727 printf("ASSETCHAINS_COMMISSION needs an ASSETCHAINS_OVERRIDE_PUBKEY and cant be more than 100000000 (100%%)\n");
8683bd8d 1728 }
36647d52
MT
1729
1730 if ( ASSETCHAINS_ENDSUBSIDY[0] != 0 || ASSETCHAINS_REWARD[0] != 0 || ASSETCHAINS_HALVING[0] != 0 || ASSETCHAINS_DECAY[0] != 0 || ASSETCHAINS_COMMISSION != 0 )
8683bd8d 1731 {
36647d52
MT
1732 printf("perc.%llu\n",(long long)ASSETCHAINS_COMMISSION);
1733
8683bd8d 1734 extraptr = extrabuf;
1735 memcpy(extraptr,ASSETCHAINS_OVERRIDE_PUBKEY33,33), extralen = 33;
36647d52
MT
1736
1737 // if we have one era, this should create the same data structure as it used to, same if we increase _MAX_ERAS
6b78e01d 1738 for ( int i = 0; i <= ASSETCHAINS_LASTERA; i++ )
36647d52
MT
1739 {
1740 printf("ERA%u: end.%llu reward.%llu halving.%llu decay.%llu\n", i,
1741 (long long)ASSETCHAINS_ENDSUBSIDY[i],
1742 (long long)ASSETCHAINS_REWARD[i],
1743 (long long)ASSETCHAINS_HALVING[i],
1744 (long long)ASSETCHAINS_DECAY[i]);
1745
1746 // TODO: Verify that we don't overrun extrabuf here, which is a 256 byte buffer
1747 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_ENDSUBSIDY[i]),(void *)&ASSETCHAINS_ENDSUBSIDY[i]);
1748 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_REWARD[i]),(void *)&ASSETCHAINS_REWARD[i]);
1749 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_HALVING[i]),(void *)&ASSETCHAINS_HALVING[i]);
1750 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_DECAY[i]),(void *)&ASSETCHAINS_DECAY[i]);
1751 }
da94ffe5 1752
e980a26d 1753 if (ASSETCHAINS_LASTERA > 0)
1754 {
1755 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_LASTERA),(void *)&ASSETCHAINS_LASTERA);
1756 }
1757
da94ffe5
MT
1758 // hash in lock above for time locked coinbase transactions above a certain reward value only if the lock above
1759 // param was specified, otherwise, for compatibility, do nothing
7daf6567 1760 if ( ASSETCHAINS_TIMELOCKGTE != _ASSETCHAINS_TIMELOCKOFF )
da94ffe5 1761 {
7daf6567
MT
1762 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_TIMELOCKGTE),(void *)&ASSETCHAINS_TIMELOCKGTE);
1763 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_TIMEUNLOCKFROM),(void *)&ASSETCHAINS_TIMEUNLOCKFROM);
1764 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_TIMEUNLOCKTO),(void *)&ASSETCHAINS_TIMEUNLOCKTO);
da94ffe5
MT
1765 }
1766
42181656 1767 if ( ASSETCHAINS_ALGO != ASSETCHAINS_EQUIHASH )
1768 {
1769 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_ALGO),(void *)&ASSETCHAINS_ALGO);
1770 }
1771
1f722359
MT
1772 if ( ASSETCHAINS_LWMAPOS != 0 )
1773 {
1774 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(ASSETCHAINS_LWMAPOS),(void *)&ASSETCHAINS_LWMAPOS);
1775 }
1776
713c2a94 1777 val = ASSETCHAINS_COMMISSION | (((uint64_t)ASSETCHAINS_STAKED & 0xff) << 32) | (((uint64_t)ASSETCHAINS_CC & 0xffffff) << 40);
ce5dd547 1778 extralen += iguana_rwnum(1,&extraptr[extralen],sizeof(val),(void *)&val);
8683bd8d 1779 }
36647d52 1780
6ed911ff 1781 addn = GetArg("-seednode","");
1782 if ( strlen(addn.c_str()) > 0 )
b6a7cb2c 1783 ASSETCHAINS_SEED = 1;
0e1af8a8 1784 strncpy(ASSETCHAINS_SYMBOL,name.c_str(),sizeof(ASSETCHAINS_SYMBOL)-1);
36647d52
MT
1785
1786 MAX_MONEY = komodo_max_money();
1787
9cdcf7ab 1788 //printf("baseid.%d MAX_MONEY.%s %.8f\n",baseid,ASSETCHAINS_SYMBOL,(double)MAX_MONEY/SATOSHIDEN);
05c2ba63 1789 ASSETCHAINS_P2PPORT = komodo_port(ASSETCHAINS_SYMBOL,ASSETCHAINS_SUPPLY,&ASSETCHAINS_MAGIC,extraptr,extralen);
0e1af8a8 1790 while ( (dirname= (char *)GetDataDir(false).string().c_str()) == 0 || dirname[0] == 0 )
1791 {
1792 fprintf(stderr,"waiting for datadir\n");
9d365796 1793 #ifndef _WIN32
0e1af8a8 1794 sleep(3);
9d365796 1795 #else
1796 boost::this_thread::sleep(boost::posix_time::milliseconds(3000));
1797 #endif
0e1af8a8 1798 }
935fc055 1799 //fprintf(stderr,"Got datadir.(%s)\n",dirname);
0e1af8a8 1800 if ( ASSETCHAINS_SYMBOL[0] != 0 )
eda3ca93 1801 {
6051a4bc 1802 int32_t komodo_baseid(char *origbase);
eda3ca93 1803 extern int COINBASE_MATURITY;
3a19bc18 1804 if ( (port= komodo_userpass(ASSETCHAINS_USERPASS,ASSETCHAINS_SYMBOL)) != 0 )
05c2ba63 1805 ASSETCHAINS_RPCPORT = port;
1806 else komodo_configfile(ASSETCHAINS_SYMBOL,ASSETCHAINS_P2PPORT + 1);
e980a26d 1807 if (ASSETCHAINS_LASTERA == 0 && ASSETCHAINS_REWARD[0] == 0)
1808 COINBASE_MATURITY = 1;
05c2ba63 1809 //fprintf(stderr,"ASSETCHAINS_RPCPORT (%s) %u\n",ASSETCHAINS_SYMBOL,ASSETCHAINS_RPCPORT);
dab92e65 1810 }
05c2ba63 1811 if ( ASSETCHAINS_RPCPORT == 0 )
1812 ASSETCHAINS_RPCPORT = ASSETCHAINS_P2PPORT + 1;
8683bd8d 1813 //ASSETCHAINS_NOTARIES = GetArg("-ac_notaries","");
1814 //komodo_assetchain_pubkeys((char *)ASSETCHAINS_NOTARIES.c_str());
6e575e43 1815 iguana_rwnum(1,magic,sizeof(ASSETCHAINS_MAGIC),(void *)&ASSETCHAINS_MAGIC);
1816 for (i=0; i<4; i++)
1817 sprintf(&magicstr[i<<1],"%02x",magic[i]);
1818 magicstr[8] = 0;
95c46396 1819#ifndef FROM_CLI
69a1f515 1820 sprintf(fname,"%s_7776",ASSETCHAINS_SYMBOL);
6e575e43 1821 if ( (fp= fopen(fname,"wb")) != 0 )
1822 {
05c2ba63 1823 fprintf(fp,iguanafmtstr,name.c_str(),name.c_str(),name.c_str(),name.c_str(),magicstr,ASSETCHAINS_P2PPORT,ASSETCHAINS_RPCPORT,"78.47.196.146");
6e575e43 1824 fclose(fp);
b6a7cb2c 1825 //printf("created (%s)\n",fname);
6e575e43 1826 } else printf("error creating (%s)\n",fname);
95c46396 1827#endif
935fc055 1828 }
1829 else
1830 {
523a5dc7 1831 char fname[512],username[512],password[4096]; int32_t iter; FILE *fp;
05c2ba63 1832 ASSETCHAINS_P2PPORT = 7770;
1833 ASSETCHAINS_RPCPORT = 7771;
b55c2367 1834 for (iter=0; iter<2; iter++)
1835 {
1836 strcpy(fname,GetDataDir().string().c_str());
9cb1ec9c 1837#ifdef _WIN32
b55c2367 1838 while ( fname[strlen(fname)-1] != '\\' )
1839 fname[strlen(fname)-1] = 0;
1840 if ( iter == 0 )
1248a49e 1841 strcat(fname,"Komodo\\komodo.conf");
1842 else strcat(fname,"Bitcoin\\bitcoin.conf");
7cce5f6a 1843#else
b55c2367 1844 while ( fname[strlen(fname)-1] != '/' )
1845 fname[strlen(fname)-1] = 0;
1846#ifdef __APPLE__
1847 if ( iter == 0 )
1848 strcat(fname,"Komodo/Komodo.conf");
1849 else strcat(fname,"Bitcoin/Bitcoin.conf");
1850#else
1851 if ( iter == 0 )
1852 strcat(fname,".komodo/komodo.conf");
1853 else strcat(fname,".bitcoin/bitcoin.conf");
7cce5f6a 1854#endif
b55c2367 1855#endif
1856 if ( (fp= fopen(fname,"rb")) != 0 )
1857 {
59490d6f 1858 _komodo_userpass(username,password,fp);
b55c2367 1859 sprintf(iter == 0 ? KMDUSERPASS : BTCUSERPASS,"%s:%s",username,password);
1860 fclose(fp);
1861 //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS);
6ec3d43c 1862 } //else printf("couldnt open.(%s)\n",fname);
eea23236 1863 if ( IS_KOMODO_NOTARY == 0 )
1864 break;
b55c2367 1865 }
935fc055 1866 }
9edf27ec 1867 if ( ASSETCHAINS_SYMBOL[0] != 0 )
3a19bc18 1868 {
05c2ba63 1869 BITCOIND_RPCPORT = GetArg("-rpcport", ASSETCHAINS_RPCPORT);
1870 //fprintf(stderr,"(%s) port.%u chain params initialized\n",ASSETCHAINS_SYMBOL,BITCOIND_RPCPORT);
1871 } else BITCOIND_RPCPORT = GetArg("-rpcport", BaseParams().RPCPort());
1bfdde1d 1872}
1873
ab918767 1874void komodo_nameset(char *symbol,char *dest,char *source)
1bfdde1d 1875{
ab918767 1876 if ( source[0] == 0 )
1bfdde1d 1877 {
8a7c9241 1878 strcpy(symbol,(char *)"KMD");
1879 strcpy(dest,(char *)"BTC");
1bfdde1d 1880 }
1881 else
1882 {
ab918767 1883 strcpy(symbol,source);
8a7c9241 1884 strcpy(dest,(char *)"KMD");
ab918767 1885 }
1886}
1887
ab918767 1888struct komodo_state *komodo_stateptrget(char *base)
1889{
1890 int32_t baseid;
8a7c9241 1891 if ( base == 0 || base[0] == 0 || strcmp(base,(char *)"KMD") == 0 )
2d116f01 1892 return(&KOMODO_STATES[33]);
ab918767 1893 else if ( (baseid= komodo_baseid(base)) >= 0 )
1894 return(&KOMODO_STATES[baseid+1]);
2d116f01 1895 else return(&KOMODO_STATES[0]);
1896}
1897
1898struct komodo_state *komodo_stateptr(char *symbol,char *dest)
1899{
1900 int32_t baseid;
1901 komodo_nameset(symbol,dest,ASSETCHAINS_SYMBOL);
1902 return(komodo_stateptrget(symbol));
ab918767 1903}
cf53fd7c 1904
1905void komodo_prefetch(FILE *fp)
1906{
1907 long fsize,fpos; int32_t incr = 16*1024*1024;
1908 fpos = ftell(fp);
1909 fseek(fp,0,SEEK_END);
1910 fsize = ftell(fp);
1911 if ( fsize > incr )
1912 {
1913 char *ignore = (char *)malloc(incr);
1914 if ( ignore != 0 )
1915 {
1916 rewind(fp);
1917 while ( fread(ignore,1,incr,fp) == incr ) // prefetch
1918 fprintf(stderr,".");
1919 free(ignore);
1920 }
1921 }
1922 fseek(fp,fpos,SEEK_SET);
1923}
1924
This page took 0.57444 seconds and 4 git commands to generate.