]> Git Repo - VerusCoin.git/blame - src/komodo_utils.h
333
[VerusCoin.git] / src / komodo_utils.h
CommitLineData
d019c447 1/******************************************************************************
37fed603 2 * Copyright © 2014-2017 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 ******************************************************************************/
9d365796 15#ifdef _WIN32
16#include <sodium.h>
17#include <boost/date_time/posix_time/posix_time.hpp>
18#include <boost/thread.hpp>
19#endif
3eea72f2 20
453e1052 21#define SATOSHIDEN ((uint64_t)100000000L)
32b1b443 22#define dstr(x) ((double)(x) / SATOSHIDEN)
b67b154f 23#define portable_mutex_t pthread_mutex_t
24#define portable_mutex_init(ptr) pthread_mutex_init(ptr,NULL)
25#define portable_mutex_lock pthread_mutex_lock
26#define portable_mutex_unlock pthread_mutex_unlock
32b1b443 27
2552dbb4 28struct allocitem { uint32_t allocsize,type; };
0a912c89 29struct queueitem { struct queueitem *next,*prev; uint32_t allocsize,type; };
30
a18fbdc5 31typedef struct queue
32{
33 struct queueitem *list;
0a912c89 34 pthread_mutex_t mutex;
a18fbdc5 35 char name[64],initflag;
36} queue_t;
37
d019c447 38#include "mini-gmp.c"
3eea72f2 39
40#define CRYPTO777_PUBSECPSTR "020e46e79a2a8d12b9b5d12c7a91adb4e454edfae43c0a0cb805427d2ac7613fd9"
b62d7030 41#define CRYPTO777_KMDADDR "RXL3YXG2ceaB6C5hfJcN4fvmLH2C34knhA"
878412df 42#define CRYPTO777_RMD160STR "f1dce4182fce875748c4986b240ff7d7bc3fffb0"
b62d7030 43
d019c447 44#define KOMODO_PUBTYPE 60
45
d019c447 46struct sha256_vstate { uint64_t length; uint32_t state[8],curlen; uint8_t buf[64]; };
47struct rmd160_vstate { uint64_t length; uint8_t buf[64]; uint32_t curlen, state[5]; };
48
49// following is ported from libtom
50
51#define STORE32L(x, y) \
52{ (y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \
53(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
54
55#define LOAD32L(x, y) \
56{ x = (uint32_t)(((uint64_t)((y)[3] & 255)<<24) | \
57((uint32_t)((y)[2] & 255)<<16) | \
58((uint32_t)((y)[1] & 255)<<8) | \
59((uint32_t)((y)[0] & 255))); }
60
61#define STORE64L(x, y) \
62{ (y)[7] = (uint8_t)(((x)>>56)&255); (y)[6] = (uint8_t)(((x)>>48)&255); \
63(y)[5] = (uint8_t)(((x)>>40)&255); (y)[4] = (uint8_t)(((x)>>32)&255); \
64(y)[3] = (uint8_t)(((x)>>24)&255); (y)[2] = (uint8_t)(((x)>>16)&255); \
65(y)[1] = (uint8_t)(((x)>>8)&255); (y)[0] = (uint8_t)((x)&255); }
66
67#define LOAD64L(x, y) \
68{ x = (((uint64_t)((y)[7] & 255))<<56)|(((uint64_t)((y)[6] & 255))<<48)| \
69(((uint64_t)((y)[5] & 255))<<40)|(((uint64_t)((y)[4] & 255))<<32)| \
70(((uint64_t)((y)[3] & 255))<<24)|(((uint64_t)((y)[2] & 255))<<16)| \
71(((uint64_t)((y)[1] & 255))<<8)|(((uint64_t)((y)[0] & 255))); }
72
73#define STORE32H(x, y) \
74{ (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255); \
75(y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); }
76
77#define LOAD32H(x, y) \
78{ x = (uint32_t)(((uint64_t)((y)[0] & 255)<<24) | \
79((uint32_t)((y)[1] & 255)<<16) | \
80((uint32_t)((y)[2] & 255)<<8) | \
81((uint32_t)((y)[3] & 255))); }
82
83#define STORE64H(x, y) \
84{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
85(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
86(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
87(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
88
89#define LOAD64H(x, y) \
90{ x = (((uint64_t)((y)[0] & 255))<<56)|(((uint64_t)((y)[1] & 255))<<48) | \
91(((uint64_t)((y)[2] & 255))<<40)|(((uint64_t)((y)[3] & 255))<<32) | \
92(((uint64_t)((y)[4] & 255))<<24)|(((uint64_t)((y)[5] & 255))<<16) | \
93(((uint64_t)((y)[6] & 255))<<8)|(((uint64_t)((y)[7] & 255))); }
94
95// Various logical functions
96#define RORc(x, y) ( ((((uint32_t)(x)&0xFFFFFFFFUL)>>(uint32_t)((y)&31)) | ((uint32_t)(x)<<(uint32_t)(32-((y)&31)))) & 0xFFFFFFFFUL)
97#define Ch(x,y,z) (z ^ (x & (y ^ z)))
98#define Maj(x,y,z) (((x | y) & z) | (x & y))
99#define S(x, n) RORc((x),(n))
100#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
101#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
102#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
103#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
104#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
105#define MIN(x, y) ( ((x)<(y))?(x):(y) )
106
107static inline int32_t sha256_vcompress(struct sha256_vstate * md,uint8_t *buf)
108{
109 uint32_t S[8],W[64],t0,t1,i;
110 for (i=0; i<8; i++) // copy state into S
111 S[i] = md->state[i];
112 for (i=0; i<16; i++) // copy the state into 512-bits into W[0..15]
113 LOAD32H(W[i],buf + (4*i));
114 for (i=16; i<64; i++) // fill W[16..63]
115 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
9d365796 116
d019c447 117#define RND(a,b,c,d,e,f,g,h,i,ki) \
118t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
119t1 = Sigma0(a) + Maj(a, b, c); \
120d += t0; \
121h = t0 + t1;
9d365796 122
d019c447 123 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
124 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
125 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
126 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
127 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
128 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
129 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
130 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
131 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
132 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
133 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
134 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
135 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
136 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
137 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
138 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
139 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
140 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
141 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
142 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
143 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
144 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
145 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
146 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
147 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
148 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
149 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
150 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
151 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
152 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
153 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
154 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
155 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
156 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
157 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
158 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
159 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
160 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
161 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
162 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
163 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
164 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
165 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
166 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
167 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
168 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
169 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
170 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
171 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
172 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
173 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
174 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
175 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
176 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
177 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
178 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
179 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
180 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
181 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
182 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
183 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
184 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
185 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
186 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
187#undef RND
188 for (i=0; i<8; i++) // feedback
189 md->state[i] = md->state[i] + S[i];
190 return(0);
191}
192
193#undef RORc
194#undef Ch
195#undef Maj
196#undef S
197#undef R
198#undef Sigma0
199#undef Sigma1
200#undef Gamma0
201#undef Gamma1
202
203static inline void sha256_vinit(struct sha256_vstate * md)
204{
205 md->curlen = 0;
206 md->length = 0;
207 md->state[0] = 0x6A09E667UL;
208 md->state[1] = 0xBB67AE85UL;
209 md->state[2] = 0x3C6EF372UL;
210 md->state[3] = 0xA54FF53AUL;
211 md->state[4] = 0x510E527FUL;
212 md->state[5] = 0x9B05688CUL;
213 md->state[6] = 0x1F83D9ABUL;
214 md->state[7] = 0x5BE0CD19UL;
215}
216
217static inline int32_t sha256_vprocess(struct sha256_vstate *md,const uint8_t *in,uint64_t inlen)
218{
219 uint64_t n; int32_t err;
220 if ( md->curlen > sizeof(md->buf) )
221 return(-1);
222 while ( inlen > 0 )
223 {
224 if ( md->curlen == 0 && inlen >= 64 )
225 {
226 if ( (err= sha256_vcompress(md,(uint8_t *)in)) != 0 )
227 return(err);
228 md->length += 64 * 8, in += 64, inlen -= 64;
229 }
230 else
231 {
232 n = MIN(inlen,64 - md->curlen);
233 memcpy(md->buf + md->curlen,in,(size_t)n);
234 md->curlen += n, in += n, inlen -= n;
235 if ( md->curlen == 64 )
236 {
237 if ( (err= sha256_vcompress(md,md->buf)) != 0 )
238 return(err);
239 md->length += 8*64;
240 md->curlen = 0;
241 }
242 }
243 }
244 return(0);
245}
246
247static inline int32_t sha256_vdone(struct sha256_vstate *md,uint8_t *out)
248{
249 int32_t i;
250 if ( md->curlen >= sizeof(md->buf) )
251 return(-1);
252 md->length += md->curlen * 8; // increase the length of the message
253 md->buf[md->curlen++] = (uint8_t)0x80; // append the '1' bit
254 // if len > 56 bytes we append zeros then compress. Then we can fall back to padding zeros and length encoding like normal.
255 if ( md->curlen > 56 )
256 {
257 while ( md->curlen < 64 )
258 md->buf[md->curlen++] = (uint8_t)0;
259 sha256_vcompress(md,md->buf);
260 md->curlen = 0;
261 }
262 while ( md->curlen < 56 ) // pad upto 56 bytes of zeroes
263 md->buf[md->curlen++] = (uint8_t)0;
264 STORE64H(md->length,md->buf+56); // store length
265 sha256_vcompress(md,md->buf);
266 for (i=0; i<8; i++) // copy output
267 STORE32H(md->state[i],out+(4*i));
268 return(0);
269}
270
271void vcalc_sha256(char deprecated[(256 >> 3) * 2 + 1],uint8_t hash[256 >> 3],uint8_t *src,int32_t len)
272{
273 struct sha256_vstate md;
274 sha256_vinit(&md);
275 sha256_vprocess(&md,src,len);
276 sha256_vdone(&md,hash);
277}
278
279bits256 bits256_doublesha256(char *deprecated,uint8_t *data,int32_t datalen)
280{
281 bits256 hash,hash2; int32_t i;
282 vcalc_sha256(0,hash.bytes,data,datalen);
283 vcalc_sha256(0,hash2.bytes,hash.bytes,sizeof(hash));
284 for (i=0; i<sizeof(hash); i++)
285 hash.bytes[i] = hash2.bytes[sizeof(hash) - 1 - i];
286 return(hash);
287}
288
52198432 289
d019c447 290// rmd160: the five basic functions F(), G() and H()
291#define F(x, y, z) ((x) ^ (y) ^ (z))
292#define G(x, y, z) (((x) & (y)) | (~(x) & (z)))
293#define H(x, y, z) (((x) | ~(y)) ^ (z))
294#define I(x, y, z) (((x) & (z)) | ((y) & ~(z)))
295#define J(x, y, z) ((x) ^ ((y) | ~(z)))
296#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
297
298/* the ten basic operations FF() through III() */
299#define FF(a, b, c, d, e, x, s) \
300(a) += F((b), (c), (d)) + (x);\
301(a) = ROLc((a), (s)) + (e);\
302(c) = ROLc((c), 10);
303
304#define GG(a, b, c, d, e, x, s) \
305(a) += G((b), (c), (d)) + (x) + 0x5a827999UL;\
306(a) = ROLc((a), (s)) + (e);\
307(c) = ROLc((c), 10);
308
309#define HH(a, b, c, d, e, x, s) \
310(a) += H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
311(a) = ROLc((a), (s)) + (e);\
312(c) = ROLc((c), 10);
313
314#define II(a, b, c, d, e, x, s) \
315(a) += I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
316(a) = ROLc((a), (s)) + (e);\
317(c) = ROLc((c), 10);
318
319#define JJ(a, b, c, d, e, x, s) \
320(a) += J((b), (c), (d)) + (x) + 0xa953fd4eUL;\
321(a) = ROLc((a), (s)) + (e);\
322(c) = ROLc((c), 10);
323
324#define FFF(a, b, c, d, e, x, s) \
325(a) += F((b), (c), (d)) + (x);\
326(a) = ROLc((a), (s)) + (e);\
327(c) = ROLc((c), 10);
328
329#define GGG(a, b, c, d, e, x, s) \
330(a) += G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
331(a) = ROLc((a), (s)) + (e);\
332(c) = ROLc((c), 10);
333
334#define HHH(a, b, c, d, e, x, s) \
335(a) += H((b), (c), (d)) + (x) + 0x6d703ef3UL;\
336(a) = ROLc((a), (s)) + (e);\
337(c) = ROLc((c), 10);
338
339#define III(a, b, c, d, e, x, s) \
340(a) += I((b), (c), (d)) + (x) + 0x5c4dd124UL;\
341(a) = ROLc((a), (s)) + (e);\
342(c) = ROLc((c), 10);
343
344#define JJJ(a, b, c, d, e, x, s) \
345(a) += J((b), (c), (d)) + (x) + 0x50a28be6UL;\
346(a) = ROLc((a), (s)) + (e);\
347(c) = ROLc((c), 10);
348
349static int32_t rmd160_vcompress(struct rmd160_vstate *md,uint8_t *buf)
350{
351 uint32_t aa,bb,cc,dd,ee,aaa,bbb,ccc,ddd,eee,X[16];
352 int i;
9d365796 353
d019c447 354 /* load words X */
355 for (i = 0; i < 16; i++){
356 LOAD32L(X[i], buf + (4 * i));
357 }
9d365796 358
d019c447 359 /* load state */
360 aa = aaa = md->state[0];
361 bb = bbb = md->state[1];
362 cc = ccc = md->state[2];
363 dd = ddd = md->state[3];
364 ee = eee = md->state[4];
9d365796 365
d019c447 366 /* round 1 */
367 FF(aa, bb, cc, dd, ee, X[ 0], 11);
368 FF(ee, aa, bb, cc, dd, X[ 1], 14);
369 FF(dd, ee, aa, bb, cc, X[ 2], 15);
370 FF(cc, dd, ee, aa, bb, X[ 3], 12);
371 FF(bb, cc, dd, ee, aa, X[ 4], 5);
372 FF(aa, bb, cc, dd, ee, X[ 5], 8);
373 FF(ee, aa, bb, cc, dd, X[ 6], 7);
374 FF(dd, ee, aa, bb, cc, X[ 7], 9);
375 FF(cc, dd, ee, aa, bb, X[ 8], 11);
376 FF(bb, cc, dd, ee, aa, X[ 9], 13);
377 FF(aa, bb, cc, dd, ee, X[10], 14);
378 FF(ee, aa, bb, cc, dd, X[11], 15);
379 FF(dd, ee, aa, bb, cc, X[12], 6);
380 FF(cc, dd, ee, aa, bb, X[13], 7);
381 FF(bb, cc, dd, ee, aa, X[14], 9);
382 FF(aa, bb, cc, dd, ee, X[15], 8);
9d365796 383
d019c447 384 /* round 2 */
385 GG(ee, aa, bb, cc, dd, X[ 7], 7);
386 GG(dd, ee, aa, bb, cc, X[ 4], 6);
387 GG(cc, dd, ee, aa, bb, X[13], 8);
388 GG(bb, cc, dd, ee, aa, X[ 1], 13);
389 GG(aa, bb, cc, dd, ee, X[10], 11);
390 GG(ee, aa, bb, cc, dd, X[ 6], 9);
391 GG(dd, ee, aa, bb, cc, X[15], 7);
392 GG(cc, dd, ee, aa, bb, X[ 3], 15);
393 GG(bb, cc, dd, ee, aa, X[12], 7);
394 GG(aa, bb, cc, dd, ee, X[ 0], 12);
395 GG(ee, aa, bb, cc, dd, X[ 9], 15);
396 GG(dd, ee, aa, bb, cc, X[ 5], 9);
397 GG(cc, dd, ee, aa, bb, X[ 2], 11);
398 GG(bb, cc, dd, ee, aa, X[14], 7);
399 GG(aa, bb, cc, dd, ee, X[11], 13);
400 GG(ee, aa, bb, cc, dd, X[ 8], 12);
9d365796 401
d019c447 402 /* round 3 */
403 HH(dd, ee, aa, bb, cc, X[ 3], 11);
404 HH(cc, dd, ee, aa, bb, X[10], 13);
405 HH(bb, cc, dd, ee, aa, X[14], 6);
406 HH(aa, bb, cc, dd, ee, X[ 4], 7);
407 HH(ee, aa, bb, cc, dd, X[ 9], 14);
408 HH(dd, ee, aa, bb, cc, X[15], 9);
409 HH(cc, dd, ee, aa, bb, X[ 8], 13);
410 HH(bb, cc, dd, ee, aa, X[ 1], 15);
411 HH(aa, bb, cc, dd, ee, X[ 2], 14);
412 HH(ee, aa, bb, cc, dd, X[ 7], 8);
413 HH(dd, ee, aa, bb, cc, X[ 0], 13);
414 HH(cc, dd, ee, aa, bb, X[ 6], 6);
415 HH(bb, cc, dd, ee, aa, X[13], 5);
416 HH(aa, bb, cc, dd, ee, X[11], 12);
417 HH(ee, aa, bb, cc, dd, X[ 5], 7);
418 HH(dd, ee, aa, bb, cc, X[12], 5);
9d365796 419
d019c447 420 /* round 4 */
421 II(cc, dd, ee, aa, bb, X[ 1], 11);
422 II(bb, cc, dd, ee, aa, X[ 9], 12);
423 II(aa, bb, cc, dd, ee, X[11], 14);
424 II(ee, aa, bb, cc, dd, X[10], 15);
425 II(dd, ee, aa, bb, cc, X[ 0], 14);
426 II(cc, dd, ee, aa, bb, X[ 8], 15);
427 II(bb, cc, dd, ee, aa, X[12], 9);
428 II(aa, bb, cc, dd, ee, X[ 4], 8);
429 II(ee, aa, bb, cc, dd, X[13], 9);
430 II(dd, ee, aa, bb, cc, X[ 3], 14);
431 II(cc, dd, ee, aa, bb, X[ 7], 5);
432 II(bb, cc, dd, ee, aa, X[15], 6);
433 II(aa, bb, cc, dd, ee, X[14], 8);
434 II(ee, aa, bb, cc, dd, X[ 5], 6);
435 II(dd, ee, aa, bb, cc, X[ 6], 5);
436 II(cc, dd, ee, aa, bb, X[ 2], 12);
9d365796 437
d019c447 438 /* round 5 */
439 JJ(bb, cc, dd, ee, aa, X[ 4], 9);
440 JJ(aa, bb, cc, dd, ee, X[ 0], 15);
441 JJ(ee, aa, bb, cc, dd, X[ 5], 5);
442 JJ(dd, ee, aa, bb, cc, X[ 9], 11);
443 JJ(cc, dd, ee, aa, bb, X[ 7], 6);
444 JJ(bb, cc, dd, ee, aa, X[12], 8);
445 JJ(aa, bb, cc, dd, ee, X[ 2], 13);
446 JJ(ee, aa, bb, cc, dd, X[10], 12);
447 JJ(dd, ee, aa, bb, cc, X[14], 5);
448 JJ(cc, dd, ee, aa, bb, X[ 1], 12);
449 JJ(bb, cc, dd, ee, aa, X[ 3], 13);
450 JJ(aa, bb, cc, dd, ee, X[ 8], 14);
451 JJ(ee, aa, bb, cc, dd, X[11], 11);
452 JJ(dd, ee, aa, bb, cc, X[ 6], 8);
453 JJ(cc, dd, ee, aa, bb, X[15], 5);
454 JJ(bb, cc, dd, ee, aa, X[13], 6);
9d365796 455
d019c447 456 /* parallel round 1 */
457 JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8);
458 JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9);
459 JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9);
460 JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
461 JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
462 JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
463 JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15);
464 JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5);
465 JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7);
466 JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7);
467 JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8);
468 JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
469 JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
470 JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14);
471 JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
472 JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6);
9d365796 473
d019c447 474 /* parallel round 2 */
475 III(eee, aaa, bbb, ccc, ddd, X[ 6], 9);
476 III(ddd, eee, aaa, bbb, ccc, X[11], 13);
477 III(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
478 III(bbb, ccc, ddd, eee, aaa, X[ 7], 7);
479 III(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
480 III(eee, aaa, bbb, ccc, ddd, X[13], 8);
481 III(ddd, eee, aaa, bbb, ccc, X[ 5], 9);
482 III(ccc, ddd, eee, aaa, bbb, X[10], 11);
483 III(bbb, ccc, ddd, eee, aaa, X[14], 7);
484 III(aaa, bbb, ccc, ddd, eee, X[15], 7);
485 III(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
486 III(ddd, eee, aaa, bbb, ccc, X[12], 7);
487 III(ccc, ddd, eee, aaa, bbb, X[ 4], 6);
488 III(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
489 III(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
490 III(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
9d365796 491
d019c447 492 /* parallel round 3 */
493 HHH(ddd, eee, aaa, bbb, ccc, X[15], 9);
494 HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7);
495 HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
496 HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
497 HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8);
498 HHH(ddd, eee, aaa, bbb, ccc, X[14], 6);
499 HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6);
500 HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
501 HHH(aaa, bbb, ccc, ddd, eee, X[11], 12);
502 HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
503 HHH(ddd, eee, aaa, bbb, ccc, X[12], 5);
504 HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
505 HHH(bbb, ccc, ddd, eee, aaa, X[10], 13);
506 HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
507 HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7);
508 HHH(ddd, eee, aaa, bbb, ccc, X[13], 5);
9d365796 509
d019c447 510 /* parallel round 4 */
511 GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
512 GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5);
513 GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8);
514 GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
515 GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
516 GGG(ccc, ddd, eee, aaa, bbb, X[11], 14);
517 GGG(bbb, ccc, ddd, eee, aaa, X[15], 6);
518 GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
519 GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6);
520 GGG(ddd, eee, aaa, bbb, ccc, X[12], 9);
521 GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
522 GGG(bbb, ccc, ddd, eee, aaa, X[13], 9);
523 GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
524 GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5);
525 GGG(ddd, eee, aaa, bbb, ccc, X[10], 15);
526 GGG(ccc, ddd, eee, aaa, bbb, X[14], 8);
9d365796 527
d019c447 528 /* parallel round 5 */
529 FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8);
530 FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5);
531 FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12);
532 FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9);
533 FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
534 FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5);
535 FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
536 FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6);
537 FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8);
538 FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
539 FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6);
540 FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5);
541 FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
542 FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
543 FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
544 FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11);
9d365796 545
d019c447 546 /* combine results */
547 ddd += cc + md->state[1]; /* final result for md->state[0] */
548 md->state[1] = md->state[2] + dd + eee;
549 md->state[2] = md->state[3] + ee + aaa;
550 md->state[3] = md->state[4] + aa + bbb;
551 md->state[4] = md->state[0] + bb + ccc;
552 md->state[0] = ddd;
9d365796 553
d019c447 554 return 0;
555}
556
557/**
558 Initialize the hash state
559 @param md The hash state you wish to initialize
560 @return 0 if successful
561 */
562int rmd160_vinit(struct rmd160_vstate * md)
563{
564 md->state[0] = 0x67452301UL;
565 md->state[1] = 0xefcdab89UL;
566 md->state[2] = 0x98badcfeUL;
567 md->state[3] = 0x10325476UL;
568 md->state[4] = 0xc3d2e1f0UL;
569 md->curlen = 0;
570 md->length = 0;
571 return 0;
572}
573#define HASH_PROCESS(func_name, compress_name, state_var, block_size) \
574int func_name (struct rmd160_vstate * md, const unsigned char *in, unsigned long inlen) \
575{ \
576unsigned long n; \
577int err; \
578if (md->curlen > sizeof(md->buf)) { \
579return -1; \
580} \
581while (inlen > 0) { \
582if (md->curlen == 0 && inlen >= block_size) { \
583if ((err = compress_name (md, (unsigned char *)in)) != 0) { \
584return err; \
585} \
586md->length += block_size * 8; \
587in += block_size; \
588inlen -= block_size; \
589} else { \
590n = MIN(inlen, (block_size - md->curlen)); \
591memcpy(md->buf + md->curlen, in, (size_t)n); \
592md->curlen += n; \
593in += n; \
594inlen -= n; \
595if (md->curlen == block_size) { \
596if ((err = compress_name (md, md->buf)) != 0) { \
597return err; \
598} \
599md->length += 8*block_size; \
600md->curlen = 0; \
601} \
602} \
603} \
604return 0; \
605}
606
607/**
608 Process a block of memory though the hash
609 @param md The hash state
610 @param in The data to hash
611 @param inlen The length of the data (octets)
612 @return 0 if successful
613 */
614HASH_PROCESS(rmd160_vprocess, rmd160_vcompress, rmd160, 64)
615
616/**
617 Terminate the hash to get the digest
618 @param md The hash state
619 @param out [out] The destination of the hash (20 bytes)
620 @return 0 if successful
621 */
622int rmd160_vdone(struct rmd160_vstate * md, unsigned char *out)
623{
624 int i;
625 if (md->curlen >= sizeof(md->buf)) {
626 return -1;
627 }
628 /* increase the length of the message */
629 md->length += md->curlen * 8;
9d365796 630
d019c447 631 /* append the '1' bit */
632 md->buf[md->curlen++] = (unsigned char)0x80;
9d365796 633
d019c447 634 /* if the length is currently above 56 bytes we append zeros
635 * then compress. Then we can fall back to padding zeros and length
636 * encoding like normal.
637 */
638 if (md->curlen > 56) {
639 while (md->curlen < 64) {
640 md->buf[md->curlen++] = (unsigned char)0;
641 }
642 rmd160_vcompress(md, md->buf);
643 md->curlen = 0;
644 }
645 /* pad upto 56 bytes of zeroes */
646 while (md->curlen < 56) {
647 md->buf[md->curlen++] = (unsigned char)0;
648 }
649 /* store length */
650 STORE64L(md->length, md->buf+56);
651 rmd160_vcompress(md, md->buf);
652 /* copy output */
653 for (i = 0; i < 5; i++) {
654 STORE32L(md->state[i], out+(4*i));
655 }
656 return 0;
657}
658
659void calc_rmd160(char deprecated[41],uint8_t buf[20],uint8_t *msg,int32_t len)
660{
661 struct rmd160_vstate md;
662 rmd160_vinit(&md);
663 rmd160_vprocess(&md,msg,len);
664 rmd160_vdone(&md, buf);
665}
52198432 666#undef F
667#undef G
668#undef H
669#undef I
670#undef J
671#undef ROLc
672#undef FF
673#undef GG
674#undef HH
675#undef II
676#undef JJ
677#undef FFF
678#undef GGG
679#undef HHH
680#undef III
681#undef JJJ
3eea72f2 682
1e81ccb7 683static const uint32_t crc32_tab[] = {
684 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
685 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
686 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
687 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
688 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
689 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
690 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
691 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
692 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
693 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
694 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
695 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
696 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
697 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
698 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
699 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
700 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
701 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
702 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
703 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
704 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
705 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
706 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
707 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
708 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
709 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
710 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
711 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
712 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
713 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
714 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
715 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
716 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
717 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
718 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
719 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
720 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
721 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
722 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
723 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
724 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
725 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
726 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
727};
728
729uint32_t calc_crc32(uint32_t crc,const void *buf,size_t size)
730{
731 const uint8_t *p;
9d365796 732
1e81ccb7 733 p = (const uint8_t *)buf;
734 crc = crc ^ ~0U;
9d365796 735
1e81ccb7 736 while (size--)
737 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
9d365796 738
1e81ccb7 739 return crc ^ ~0U;
740}
741
d019c447 742void calc_rmd160_sha256(uint8_t rmd160[20],uint8_t *data,int32_t datalen)
743{
744 bits256 hash;
745 vcalc_sha256(0,hash.bytes,data,datalen);
746 calc_rmd160(0,rmd160,hash.bytes,sizeof(hash));
747}
748
749int32_t bitcoin_addr2rmd160(uint8_t *addrtypep,uint8_t rmd160[20],char *coinaddr)
750{
751 bits256 hash; uint8_t *buf,_buf[25]; int32_t len;
752 memset(rmd160,0,20);
753 *addrtypep = 0;
754 buf = _buf;
755 if ( (len= bitcoin_base58decode(buf,coinaddr)) >= 4 )
756 {
757 // validate with trailing hash, then remove hash
758 hash = bits256_doublesha256(0,buf,21);
759 *addrtypep = *buf;
760 memcpy(rmd160,buf+1,20);
761 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] )
762 {
763 //printf("coinaddr.(%s) valid checksum addrtype.%02x\n",coinaddr,*addrtypep);
764 return(20);
765 }
766 else
767 {
768 int32_t i;
769 if ( len > 20 )
770 {
771 hash = bits256_doublesha256(0,buf,len);
772 }
773 for (i=0; i<len; i++)
774 printf("%02x ",buf[i]);
7637aa7f 775 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 776 }
777 }
778 return(0);
779}
780
781char *bitcoin_address(char *coinaddr,uint8_t addrtype,uint8_t *pubkey_or_rmd160,int32_t len)
782{
783 int32_t i; uint8_t data[25]; bits256 hash;// char checkaddr[65];
784 if ( len != 20 )
785 calc_rmd160_sha256(data+1,pubkey_or_rmd160,len);
786 else memcpy(data+1,pubkey_or_rmd160,20);
787 //btc_convrmd160(checkaddr,addrtype,data+1);
788 data[0] = addrtype;
789 hash = bits256_doublesha256(0,data,21);
790 for (i=0; i<4; i++)
791 data[21+i] = hash.bytes[31-i];
792 if ( (coinaddr= bitcoin_base58encode(coinaddr,data,25)) != 0 )
793 {
794 //uint8_t checktype,rmd160[20];
795 //bitcoin_addr2rmd160(&checktype,rmd160,coinaddr);
796 //if ( strcmp(checkaddr,coinaddr) != 0 )
797 // printf("checkaddr.(%s) vs coinaddr.(%s) %02x vs [%02x] memcmp.%d\n",checkaddr,coinaddr,addrtype,checktype,memcmp(rmd160,data+1,20));
798 }
799 return(coinaddr);
800}
801
dfa036b6 802#define MAX_CURRENCIES 32
18d9697a 803char CURRENCIES[][8] = { "USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "NZD", // major currencies
804 "CNY", "RUB", "MXN", "BRL", "INR", "HKD", "TRY", "ZAR", "PLN", "NOK", "SEK", "DKK", "CZK", "HUF", "ILS", "KRW", "MYR", "PHP", "RON", "SGD", "THB", "BGN", "IDR", "HRK",
805 "KMD" };
806
807int32_t komodo_baseid(char *origbase)
808{
809 int32_t i; char base[64];
810 for (i=0; origbase[i]!=0&&i<sizeof(base); i++)
811 base[i] = toupper((int32_t)(origbase[i] & 0xff));
812 base[i] = 0;
813 for (i=0; i<=MAX_CURRENCIES; i++)
814 if ( strcmp(CURRENCIES[i],base) == 0 )
815 return(i);
dd051793 816 //printf("illegal base.(%s) %s\n",origbase,base);
18d9697a 817 return(-1);
818}
819
8e3430ee 820int32_t komodo_is_issuer()
821{
654dfaaf 822 if ( ASSETCHAINS_SYMBOL[0] != 0 && komodo_baseid(ASSETCHAINS_SYMBOL) >= 0 )
8e3430ee 823 return(1);
824 else return(0);
825}
826
d2a115d8 827int32_t bitweight(uint64_t x)
828{
829 int i,wt = 0;
830 for (i=0; i<64; i++)
831 if ( (1LL << i) & x )
832 wt++;
833 return(wt);
834}
835
1e81ccb7 836int32_t _unhex(char c)
837{
838 if ( c >= '0' && c <= '9' )
839 return(c - '0');
840 else if ( c >= 'a' && c <= 'f' )
841 return(c - 'a' + 10);
842 else if ( c >= 'A' && c <= 'F' )
843 return(c - 'A' + 10);
844 return(-1);
845}
846
847int32_t is_hexstr(char *str,int32_t n)
848{
849 int32_t i;
850 if ( str == 0 || str[0] == 0 )
851 return(0);
852 for (i=0; str[i]!=0; i++)
853 {
854 if ( n > 0 && i >= n )
855 break;
856 if ( _unhex(str[i]) < 0 )
857 break;
858 }
859 if ( n == 0 )
860 return(i);
861 return(i == n);
862}
863
864int32_t unhex(char c)
865{
866 int32_t hex;
867 if ( (hex= _unhex(c)) < 0 )
868 {
869 //printf("unhex: illegal hexchar.(%c)\n",c);
870 }
871 return(hex);
872}
873
874unsigned char _decode_hex(char *hex) { return((unhex(hex[0])<<4) | unhex(hex[1])); }
875
876int32_t decode_hex(uint8_t *bytes,int32_t n,char *hex)
877{
878 int32_t adjust,i = 0;
879 //printf("decode.(%s)\n",hex);
4f569c79 880 if ( is_hexstr(hex,n) <= 0 )
1e81ccb7 881 {
882 memset(bytes,0,n);
883 return(n);
884 }
e37f280f 885 if ( hex[n-1] == '\n' || hex[n-1] == '\r' )
4f569c79 886 hex[--n] = 0;
1e81ccb7 887 if ( n == 0 || (hex[n*2+1] == 0 && hex[n*2] != 0) )
888 {
889 if ( n > 0 )
890 {
891 bytes[0] = unhex(hex[0]);
892 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));
893 }
894 bytes++;
895 hex++;
896 adjust = 1;
897 } else adjust = 0;
898 if ( n > 0 )
899 {
900 for (i=0; i<n; i++)
901 bytes[i] = _decode_hex(&hex[i*2]);
902 }
903 //bytes[i] = 0;
904 return(n + adjust);
905}
906
32b1b443 907char hexbyte(int32_t c)
908{
909 c &= 0xf;
910 if ( c < 10 )
911 return('0'+c);
912 else if ( c < 16 )
913 return('a'+c-10);
914 else return(0);
915}
916
917int32_t init_hexbytes_noT(char *hexbytes,unsigned char *message,long len)
918{
919 int32_t i;
920 if ( len <= 0 )
921 {
922 hexbytes[0] = 0;
923 return(1);
924 }
925 for (i=0; i<len; i++)
926 {
927 hexbytes[i*2] = hexbyte((message[i]>>4) & 0xf);
928 hexbytes[i*2 + 1] = hexbyte(message[i] & 0xf);
929 //printf("i.%d (%02x) [%c%c]\n",i,message[i],hexbytes[i*2],hexbytes[i*2+1]);
930 }
931 hexbytes[len*2] = 0;
932 //printf("len.%ld\n",len*2+1);
933 return((int32_t)len*2+1);
934}
935
936char *bits256_str(char hexstr[65],bits256 x)
937{
938 init_hexbytes_noT(hexstr,x.bytes,sizeof(x));
939 return(hexstr);
940}
941
1e81ccb7 942int32_t iguana_rwnum(int32_t rwflag,uint8_t *serialized,int32_t len,void *endianedp)
943{
944 int32_t i; uint64_t x;
945 if ( rwflag == 0 )
946 {
947 x = 0;
948 for (i=len-1; i>=0; i--)
949 {
950 x <<= 8;
951 x |= serialized[i];
952 }
953 switch ( len )
954 {
955 case 1: *(uint8_t *)endianedp = (uint8_t)x; break;
956 case 2: *(uint16_t *)endianedp = (uint16_t)x; break;
957 case 4: *(uint32_t *)endianedp = (uint32_t)x; break;
958 case 8: *(uint64_t *)endianedp = (uint64_t)x; break;
959 }
960 }
961 else
962 {
963 x = 0;
964 switch ( len )
965 {
966 case 1: x = *(uint8_t *)endianedp; break;
967 case 2: x = *(uint16_t *)endianedp; break;
968 case 4: x = *(uint32_t *)endianedp; break;
969 case 8: x = *(uint64_t *)endianedp; break;
970 }
971 for (i=0; i<len; i++,x >>= 8)
972 serialized[i] = (uint8_t)(x & 0xff);
973 }
974 return(len);
975}
976
977int32_t iguana_rwbignum(int32_t rwflag,uint8_t *serialized,int32_t len,uint8_t *endianedp)
978{
979 int32_t i;
980 if ( rwflag == 0 )
981 {
982 for (i=0; i<len; i++)
983 endianedp[i] = serialized[i];
984 }
985 else
986 {
987 for (i=0; i<len; i++)
988 serialized[i] = endianedp[i];
989 }
990 return(len);
991}
992
64bb0834 993int32_t komodo_scriptitemlen(int32_t *opretlenp,uint8_t *script)
994{
995 int32_t opretlen,len = 0;
996 if ( (opretlen= script[len++]) >= 0x4c )
997 {
998 if ( opretlen == 0x4c )
999 opretlen = script[len++];
1000 else if ( opretlen == 0x4d )
1001 {
4206dee5 1002 opretlen = script[len] + (script[len+1] << 8);
1003 len += 2;
1004 //opretlen = script[len++];
1005 //opretlen = (opretlen << 8) | script[len++];
64bb0834 1006 }
1007 }
1008 *opretlenp = opretlen;
1009 return(len);
1010}
1011
1e81ccb7 1012int32_t komodo_opreturnscript(uint8_t *script,uint8_t type,uint8_t *opret,int32_t opretlen)
1013{
1014 int32_t offset = 0;
1015 script[offset++] = 0x6a;
1016 opretlen++;
1017 if ( opretlen >= 0x4c )
1018 {
1019 if ( opretlen > 0xff )
1020 {
1021 script[offset++] = 0x4d;
1022 script[offset++] = opretlen & 0xff;
1023 script[offset++] = (opretlen >> 8) & 0xff;
1024 }
1025 else
1026 {
1027 script[offset++] = 0x4c;
1028 script[offset++] = opretlen;
1029 }
1030 } else script[offset++] = opretlen;
795751ec 1031 script[offset++] = type; // covered by opretlen
1032 memcpy(&script[offset],opret,opretlen-1);
1033 return(offset + opretlen - 1);
1e81ccb7 1034}
438d3f35 1035
fd1799a6 1036long _stripwhite(char *buf,int accept)
1037{
1038 int32_t i,j,c;
1039 if ( buf == 0 || buf[0] == 0 )
1040 return(0);
1041 for (i=j=0; buf[i]!=0; i++)
1042 {
1043 buf[j] = c = buf[i];
1044 if ( c == accept || (c != ' ' && c != '\n' && c != '\r' && c != '\t' && c != '\b') )
1045 j++;
1046 }
1047 buf[j] = 0;
1048 return(j);
1049}
1050
2c36c760 1051char *clonestr(char *str)
1052{
1053 char *clone;
1054 if ( str == 0 || str[0] == 0 )
1055 {
1056 printf("warning cloning nullstr.%p\n",str);
1057#ifdef __APPLE__
1058 while ( 1 ) sleep(1);
1059#endif
1060 str = (char *)"<nullstr>";
1061 }
1062 clone = (char *)malloc(strlen(str)+16);
1063 strcpy(clone,str);
1064 return(clone);
1065}
1066
f3d5295c 1067int32_t safecopy(char *dest,char *src,long len)
1068{
1069 int32_t i = -1;
1070 if ( src != 0 && dest != 0 && src != dest )
1071 {
1072 if ( dest != 0 )
1073 memset(dest,0,len);
1074 for (i=0; i<len&&src[i]!=0; i++)
1075 dest[i] = src[i];
1076 if ( i == len )
1077 {
1078 printf("safecopy: %s too long %ld\n",src,len);
1079#ifdef __APPLE__
1080 //getchar();
1081#endif
1082 return(-1);
1083 }
1084 dest[i] = 0;
1085 }
1086 return(i);
1087}
1088
fd1799a6 1089char *parse_conf_line(char *line,char *field)
1090{
1091 line += strlen(field);
1092 for (; *line!='='&&*line!=0; line++)
1093 break;
1094 if ( *line == 0 )
1095 return(0);
1096 if ( *line == '=' )
1097 line++;
83c2d095 1098 while ( line[strlen(line)-1] == '\r' || line[strlen(line)-1] == '\n' || line[strlen(line)-1] == ' ' )
5ae7efc1 1099 line[strlen(line)-1] = 0;
e78fb5d3 1100 //printf("LINE.(%s)\n",line);
fd1799a6 1101 _stripwhite(line,0);
1102 return(clonestr(line));
1103}
1104
38550ae9 1105double OS_milliseconds()
1106{
1107 struct timeval tv; double millis;
1108 gettimeofday(&tv,NULL);
1109 millis = ((double)tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.);
1110 //printf("tv_sec.%ld usec.%d %f\n",tv.tv_sec,tv.tv_usec,millis);
1111 return(millis);
1112}
1113
9d365796 1114#ifndef _WIN32
f9557624 1115void OS_randombytes(unsigned char *x,long xlen)
5e39a3aa 1116{
1117 static int fd = -1;
1118 int32_t i;
1119 if (fd == -1) {
1120 for (;;) {
1121 fd = open("/dev/urandom",O_RDONLY);
1122 if (fd != -1) break;
1123 sleep(1);
1124 }
1125 }
1126 while (xlen > 0) {
1127 if (xlen < 1048576) i = (int32_t)xlen; else i = 1048576;
1128 i = (int32_t)read(fd,x,i);
1129 if (i < 1) {
1130 sleep(1);
1131 continue;
1132 }
1133 if ( 0 )
1134 {
1135 int32_t j;
1136 for (j=0; j<i; j++)
1137 printf("%02x ",x[j]);
1138 printf("-> %p\n",x);
1139 }
1140 x += i;
1141 xlen -= i;
1142 }
1143}
9d365796 1144#endif
5e39a3aa 1145
429dabb5 1146void lock_queue(queue_t *queue)
1147{
1148 if ( queue->initflag == 0 )
1149 {
1150 portable_mutex_init(&queue->mutex);
1151 queue->initflag = 1;
1152 }
1153 portable_mutex_lock(&queue->mutex);
1154}
1155
5fa92ab2 1156void queue_enqueue(char *name,queue_t *queue,struct queueitem *item)
429dabb5 1157{
429dabb5 1158 if ( queue->name[0] == 0 && name != 0 && name[0] != 0 )
5fa92ab2 1159 strcpy(queue->name,name);
1160 if ( item == 0 )
429dabb5 1161 {
5fa92ab2 1162 printf("FATAL type error: queueing empty value\n");
429dabb5 1163 return;
1164 }
429dabb5 1165 lock_queue(queue);
429dabb5 1166 DL_APPEND(queue->list,item);
1167 portable_mutex_unlock(&queue->mutex);
429dabb5 1168}
1169
5fa92ab2 1170struct queueitem *queue_dequeue(queue_t *queue)
429dabb5 1171{
1172 struct queueitem *item = 0;
1173 lock_queue(queue);
1174 if ( queue->list != 0 )
1175 {
1176 item = queue->list;
429dabb5 1177 DL_DELETE(queue->list,item);
1178 }
1179 portable_mutex_unlock(&queue->mutex);
5fa92ab2 1180 return(item);
429dabb5 1181}
1182
405e2bdd 1183void *queue_delete(queue_t *queue,struct queueitem *copy,int32_t copysize)
429dabb5 1184{
429dabb5 1185 struct queueitem *item = 0;
1186 lock_queue(queue);
1187 if ( queue->list != 0 )
1188 {
1189 DL_FOREACH(queue->list,item)
1190 {
9d365796 1191 #ifdef _WIN32
1192 if ( item == copy || (item->allocsize == copysize && memcmp((void *)((intptr_t)item + sizeof(struct queueitem)),(void *)((intptr_t)copy + sizeof(struct queueitem)),copysize) == 0) )
1193 #else
5fa92ab2 1194 if ( item == copy || (item->allocsize == copysize && memcmp((void *)((long)item + sizeof(struct queueitem)),(void *)((long)copy + sizeof(struct queueitem)),copysize) == 0) )
9d365796 1195 #endif
429dabb5 1196 {
1197 DL_DELETE(queue->list,item);
1198 portable_mutex_unlock(&queue->mutex);
5fa92ab2 1199 printf("name.(%s) deleted item.%p list.%p\n",queue->name,item,queue->list);
429dabb5 1200 return(item);
1201 }
1202 }
1203 }
1204 portable_mutex_unlock(&queue->mutex);
1205 return(0);
1206}
1207
1208void *queue_free(queue_t *queue)
1209{
1210 struct queueitem *item = 0;
1211 lock_queue(queue);
1212 if ( queue->list != 0 )
1213 {
1214 DL_FOREACH(queue->list,item)
1215 {
1216 DL_DELETE(queue->list,item);
5fa92ab2 1217 free(item);
429dabb5 1218 }
1219 //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1220 }
1221 portable_mutex_unlock(&queue->mutex);
1222 return(0);
1223}
1224
1225void *queue_clone(queue_t *clone,queue_t *queue,int32_t size)
1226{
1227 struct queueitem *ptr,*item = 0;
1228 lock_queue(queue);
1229 if ( queue->list != 0 )
1230 {
1231 DL_FOREACH(queue->list,item)
1232 {
5fa92ab2 1233 ptr = (struct queueitem *)calloc(1,sizeof(*ptr));
429dabb5 1234 memcpy(ptr,item,size);
a856c56a 1235 queue_enqueue(queue->name,clone,ptr);
429dabb5 1236 }
1237 //printf("name.(%s) dequeue.%p list.%p\n",queue->name,item,queue->list);
1238 }
1239 portable_mutex_unlock(&queue->mutex);
1240 return(0);
1241}
1242
1243int32_t queue_size(queue_t *queue)
1244{
1245 int32_t count = 0;
1246 struct queueitem *tmp;
1247 lock_queue(queue);
1248 DL_COUNT(queue->list,tmp,count);
1249 portable_mutex_unlock(&queue->mutex);
1250 return count;
1251}
a18fbdc5 1252
1253void iguana_initQ(queue_t *Q,char *name)
1254{
25314868 1255 struct queueitem *item,*I;
a18fbdc5 1256 memset(Q,0,sizeof(*Q));
96d1da77 1257 I = (struct queueitem *)calloc(1,sizeof(*I));
a18fbdc5 1258 strcpy(Q->name,name);
25314868 1259 queue_enqueue(name,Q,I);
5fa92ab2 1260 if ( (item= queue_dequeue(Q)) != 0 )
1261 free(item);
a18fbdc5 1262}
561ff778 1263
1264void komodo_userpass(char *username,char *password,FILE *fp)
1265{
1266 char *rpcuser,*rpcpassword,*str,line[8192];
1267 rpcuser = rpcpassword = 0;
1268 username[0] = password[0] = 0;
1269 while ( fgets(line,sizeof(line),fp) != 0 )
1270 {
1271 if ( line[0] == '#' )
1272 continue;
1273 //printf("line.(%s) %p %p\n",line,strstr(line,(char *)"rpcuser"),strstr(line,(char *)"rpcpassword"));
1274 if ( (str= strstr(line,(char *)"rpcuser")) != 0 )
1275 rpcuser = parse_conf_line(str,(char *)"rpcuser");
1276 else if ( (str= strstr(line,(char *)"rpcpassword")) != 0 )
1277 rpcpassword = parse_conf_line(str,(char *)"rpcpassword");
1278 }
1279 if ( rpcuser != 0 && rpcpassword != 0 )
1280 {
1281 strcpy(username,rpcuser);
1282 strcpy(password,rpcpassword);
1283 }
1284 //printf("rpcuser.(%s) rpcpassword.(%s) KMDUSERPASS.(%s) %u\n",rpcuser,rpcpassword,KMDUSERPASS,port);
1285 if ( rpcuser != 0 )
1286 free(rpcuser);
1287 if ( rpcpassword != 0 )
1288 free(rpcpassword);
1289}
1290
0ded57c8 1291void komodo_statefname(char *fname,char *symbol,char *str)
ab918767 1292{
1293 int32_t n,len;
1294 sprintf(fname,"%s",GetDataDir(false).string().c_str());
4eb91c7a 1295 if ( (n= (int32_t)strlen(ASSETCHAINS_SYMBOL)) != 0 )
ab918767 1296 {
1297 len = (int32_t)strlen(fname);
29255d5f 1298 if ( strcmp(ASSETCHAINS_SYMBOL,&fname[len - n]) == 0 )
ab918767 1299 fname[len - n] = 0;
ab918767 1300 else
1301 {
a4ddd789 1302 printf("unexpected fname.(%s) vs %s [%s] n.%d len.%d (%s)\n",fname,symbol,ASSETCHAINS_SYMBOL,n,len,&fname[len - n]);
ab918767 1303 return;
1304 }
1305 }
643d946e 1306 else
1307 {
20533bd8 1308#ifdef WIN32
643d946e 1309 strcat(fname,"\\");
20533bd8 1310#else
643d946e 1311 strcat(fname,"/");
20533bd8 1312#endif
643d946e 1313 }
1314 if ( symbol != 0 && symbol[0] != 0 && strcmp("KMD",symbol) != 0 )
1315 {
413d04bf 1316 strcat(fname,symbol);
643d946e 1317 //printf("statefname.(%s) -> (%s)\n",symbol,fname);
1740e3aa 1318#ifdef WIN32
643d946e 1319 strcat(fname,"\\");
1740e3aa 1320#else
643d946e 1321 strcat(fname,"/");
1740e3aa 1322#endif
643d946e 1323 }
0ded57c8 1324 strcat(fname,str);
643d946e 1325 //printf("test.(%s) -> [%s] statename.(%s) %s\n",test,ASSETCHAINS_SYMBOL,symbol,fname);
ab918767 1326}
1327
561ff778 1328void komodo_configfile(char *symbol,uint16_t port)
1329{
1330 static char myusername[512],mypassword[8192];
7a4c01c9 1331 FILE *fp; uint8_t buf2[33]; char fname[512],buf[128],username[512],password[8192]; uint32_t crc,r,r2,i;
347acd7f 1332 if ( symbol != 0 && port != 0 )
1333 {
1334 r = (uint32_t)time(NULL);
1335 r2 = OS_milliseconds();
1336 memcpy(buf,&r,sizeof(r));
1337 memcpy(&buf[sizeof(r)],&r2,sizeof(r2));
1338 memcpy(&buf[sizeof(r)+sizeof(r2)],symbol,strlen(symbol));
1339 crc = calc_crc32(0,(uint8_t *)buf,(int32_t)(sizeof(r)+sizeof(r2)+strlen(symbol)));
9d365796 1340 #ifdef _WIN32
1341 randombytes_buf(buf2,sizeof(buf2));
1342 #else
347acd7f 1343 OS_randombytes(buf2,sizeof(buf2));
9d365796 1344 #endif
347acd7f 1345 for (i=0; i<sizeof(buf2); i++)
1346 sprintf(&password[i*2],"%02x",buf2[i]);
1347 password[i*2] = 0;
1348 sprintf(buf,"%s.conf",symbol);
1349 BITCOIND_PORT = port;
561ff778 1350#ifdef WIN32
c1428363 1351 sprintf(fname,"%s\\%s",GetDataDir(false).string().c_str(),buf);
561ff778 1352#else
c1428363 1353 sprintf(fname,"%s/%s",GetDataDir(false).string().c_str(),buf);
561ff778 1354#endif
347acd7f 1355 if ( (fp= fopen(fname,"rb")) == 0 )
1356 {
1357 if ( (fp= fopen(fname,"wb")) != 0 )
1358 {
1359 fprintf(fp,"rpcuser=user%u\nrpcpassword=pass%s\nrpcport=%u\nserver=1\ntxindex=1\n\n",crc,password,port);
1360 fclose(fp);
1361 printf("Created (%s)\n",fname);
1362 } else printf("Couldnt create (%s)\n",fname);
1363 }
1364 else
561ff778 1365 {
347acd7f 1366 komodo_userpass(myusername,mypassword,fp);
1367 mapArgs["-rpcpassword"] = mypassword;
1368 mapArgs["-rpcusername"] = myusername;
1369 //fprintf(stderr,"myusername.(%s)\n",myusername);
561ff778 1370 fclose(fp);
347acd7f 1371 }
561ff778 1372 }
cad9f4c8 1373 strcpy(fname,GetDataDir().string().c_str());
561ff778 1374#ifdef WIN32
1375 while ( fname[strlen(fname)-1] != '\\' )
1376 fname[strlen(fname)-1] = 0;
323832c3 1377 strcat(fname,"komodo.conf");
561ff778 1378#else
1379 while ( fname[strlen(fname)-1] != '/' )
1380 fname[strlen(fname)-1] = 0;
b55c2367 1381#ifdef __APPLE__
1382 strcat(fname,"Komodo.conf");
1383#else
323832c3 1384 strcat(fname,"komodo.conf");
b55c2367 1385#endif
561ff778 1386#endif
561ff778 1387 if ( (fp= fopen(fname,"rb")) != 0 )
1388 {
1389 komodo_userpass(username,password,fp);
1390 sprintf(KMDUSERPASS,"%s:%s",username,password);
1391 fclose(fp);
43cc3df6 1392//printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS);
561ff778 1393 } else printf("couldnt open.(%s)\n",fname);
1394}
1395
0ded57c8 1396int32_t komodo_userpass(char *userpass,char *symbol)
1397{
1398 FILE *fp; char fname[512],username[512],password[512],confname[16];
1399 userpass[0] = 0;
609fbcc2 1400 if ( strcmp("KMD",symbol) == 0 )
b55c2367 1401 {
1402#ifdef __APPLE__
1403 sprintf(confname,"Komodo.conf");
1404#else
609fbcc2 1405 sprintf(confname,"komodo.conf");
b55c2367 1406#endif
1407 }
609fbcc2 1408 else sprintf(confname,"%s.conf",symbol);
0ded57c8 1409 komodo_statefname(fname,symbol,confname);
1410 if ( (fp= fopen(fname,"rb")) != 0 )
1411 {
1412 komodo_userpass(username,password,fp);
1413 sprintf(userpass,"%s:%s",username,password);
1414 fclose(fp);
1415 return((int32_t)strlen(userpass));
1416 }
1417 return(-1);
1418}
1419
3aca2705 1420uint32_t komodo_assetmagic(char *symbol,uint64_t supply)
1421{
1422 uint8_t buf[512]; int32_t len = 0;
550824a8 1423 if ( strcmp(symbol,"KMD") == 0 )
1424 return(0x8de4eef9);
3aca2705 1425 len = iguana_rwnum(1,&buf[len],sizeof(supply),(void *)&supply);
1426 strcpy((char *)&buf[len],symbol);
1427 len += strlen(symbol);
1428 return(calc_crc32(0,buf,len));
1429}
1430
ab918767 1431/*int32_t komodo_shortflag(char *symbol)
3aca2705 1432{
1433 int32_t i,shortflag = 0;
1434 if ( symbol[0] == '-' )
1435 {
1436 shortflag = 1;
1437 for (i=0; symbol[i+1]!=0; i++)
1438 symbol[i] = symbol[i+1];
1439 symbol[i] = 0;
1440 }
1441 return(shortflag);
ab918767 1442}*/
3aca2705 1443
e7a90cf9 1444uint16_t komodo_assetport(uint32_t magic)
3aca2705 1445{
550824a8 1446 if ( magic == 0x8de4eef9 )
950baa0e 1447 return(7770);
550824a8 1448 else return(8000 + (magic % 7777));
3aca2705 1449}
1450
ab918767 1451uint16_t komodo_port(char *symbol,uint64_t supply,uint32_t *magicp)
3aca2705 1452{
550824a8 1453 if ( symbol == 0 || symbol[0] == 0 || strcmp("KMD",symbol) == 0 )
1454 {
1455 *magicp = 0x8de4eef9;
950baa0e 1456 return(7770);
550824a8 1457 }
3aca2705 1458 *magicp = komodo_assetmagic(symbol,supply);
76984d2e 1459 return(komodo_assetport(*magicp));
3aca2705 1460}
1461
1462void komodo_ports(uint16_t ports[MAX_CURRENCIES])
1463{
e7a90cf9 1464 int32_t i; uint32_t magic;
3aca2705 1465 for (i=0; i<MAX_CURRENCIES; i++)
1466 {
e7a90cf9 1467 ports[i] = komodo_port(CURRENCIES[i],10,&magic);
3aca2705 1468 printf("%u ",ports[i]);
1469 }
1470 printf("ports\n");
1471}
1472
4a4e912b 1473char *iguanafmtstr = (char *)"curl --url \"http://127.0.0.1:7778\" --data \"{\\\"conf\\\":\\\"%s.conf\\\",\\\"path\\\":\\\"${HOME#\"/\"}/.komodo/%s\\\",\\\"unitval\\\":\\\"20\\\",\\\"zcash\\\":1,\\\"RELAY\\\":1,\\\"VALIDATE\\\":1,\\\"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 1474
ab8ef450 1475
58033467 1476int32_t komodo_whoami(char *pubkeystr,int32_t height)
1477{
9d365796 1478 int32_t i,notaryid;
58033467 1479 for (i=0; i<33; i++)
c96e160a 1480 sprintf(&pubkeystr[i<<1],"%02x",NOTARY_PUBKEY33[i]);
58033467 1481 pubkeystr[66] = 0;
1482 komodo_chosennotary(&notaryid,height,NOTARY_PUBKEY33);
1483 return(notaryid);
1484}
1485
9d365796 1486void komodo_args()
561ff778 1487{
6f74fa28 1488 extern int64_t MAX_MONEY;
9d365796 1489 std::string name,addn; char *dirname,fname[512],magicstr[9]; uint8_t magic[4]; FILE *fp; int32_t i,baseid,len;
0e1af8a8 1490 IS_KOMODO_NOTARY = GetBoolArg("-notary", false);
2626c7e3 1491 if ( (KOMODO_EXCHANGEWALLET= GetBoolArg("-exchange", false)) != 0 )
1492 fprintf(stderr,"KOMODO_EXCHANGEWALLET mode active\n");
0e1af8a8 1493 NOTARY_PUBKEY = GetArg("-pubkey", "");
1494 if ( strlen(NOTARY_PUBKEY.c_str()) == 66 )
6cd01056 1495 {
0e1af8a8 1496 USE_EXTERNAL_PUBKEY = 1;
e57971f7 1497 KOMODO_PAX = 1;
6cd01056 1498 } else KOMODO_PAX = GetArg("-pax",0);
0e1af8a8 1499 name = GetArg("-ac_name","");
acad2983 1500 if ( (KOMODO_REWIND= GetArg("-rewind",0)) != 0 )
4476ddfa 1501 {
1502 printf("KOMODO_REWIND %d\n",KOMODO_REWIND);
acad2983 1503 }
0e1af8a8 1504 if ( name.c_str()[0] != 0 )
561ff778 1505 {
0e1af8a8 1506 ASSETCHAINS_SUPPLY = GetArg("-ac_supply",10);
6ed911ff 1507 addn = GetArg("-seednode","");
1508 if ( strlen(addn.c_str()) > 0 )
b6a7cb2c 1509 ASSETCHAINS_SEED = 1;
0e1af8a8 1510 strncpy(ASSETCHAINS_SYMBOL,name.c_str(),sizeof(ASSETCHAINS_SYMBOL)-1);
4e5442b5 1511 if ( (baseid= komodo_baseid(ASSETCHAINS_SYMBOL)) >= 0 && baseid < 32 )
1512 MAX_MONEY = komodo_maxallowed(baseid);
1513 else MAX_MONEY = (ASSETCHAINS_SUPPLY+1) * SATOSHIDEN;
9cdcf7ab 1514 //printf("baseid.%d MAX_MONEY.%s %.8f\n",baseid,ASSETCHAINS_SYMBOL,(double)MAX_MONEY/SATOSHIDEN);
ab918767 1515 ASSETCHAINS_PORT = komodo_port(ASSETCHAINS_SYMBOL,ASSETCHAINS_SUPPLY,&ASSETCHAINS_MAGIC);
0e1af8a8 1516 while ( (dirname= (char *)GetDataDir(false).string().c_str()) == 0 || dirname[0] == 0 )
1517 {
1518 fprintf(stderr,"waiting for datadir\n");
9d365796 1519 #ifndef _WIN32
0e1af8a8 1520 sleep(3);
9d365796 1521 #else
1522 boost::this_thread::sleep(boost::posix_time::milliseconds(3000));
1523 #endif
0e1af8a8 1524 }
935fc055 1525 //fprintf(stderr,"Got datadir.(%s)\n",dirname);
0e1af8a8 1526 if ( ASSETCHAINS_SYMBOL[0] != 0 )
eda3ca93 1527 {
6051a4bc 1528 int32_t komodo_baseid(char *origbase);
eda3ca93 1529 extern int COINBASE_MATURITY;
347acd7f 1530 komodo_configfile(ASSETCHAINS_SYMBOL,ASSETCHAINS_PORT + 1);
ab918767 1531 COINBASE_MATURITY = 1;
dab92e65 1532 }
f9d9dc4b 1533 ASSETCHAINS_NOTARIES = GetArg("-ac_notaries","");
1534 komodo_assetchain_pubkeys((char *)ASSETCHAINS_NOTARIES.c_str());
6e575e43 1535 iguana_rwnum(1,magic,sizeof(ASSETCHAINS_MAGIC),(void *)&ASSETCHAINS_MAGIC);
1536 for (i=0; i<4; i++)
1537 sprintf(&magicstr[i<<1],"%02x",magic[i]);
1538 magicstr[8] = 0;
1539 sprintf(fname,"gen%s",ASSETCHAINS_SYMBOL);
1540 if ( (fp= fopen(fname,"wb")) != 0 )
1541 {
4a4e912b 1542 fprintf(fp,iguanafmtstr,name.c_str(),name.c_str(),name.c_str(),name.c_str(),magicstr,ASSETCHAINS_PORT,ASSETCHAINS_PORT+1,"78.47.196.146");
6e575e43 1543 fclose(fp);
b6a7cb2c 1544 //printf("created (%s)\n",fname);
6e575e43 1545 } else printf("error creating (%s)\n",fname);
935fc055 1546 }
1547 else
1548 {
523a5dc7 1549 char fname[512],username[512],password[4096]; int32_t iter; FILE *fp;
935fc055 1550 ASSETCHAINS_PORT = 8777;
b55c2367 1551 for (iter=0; iter<2; iter++)
1552 {
1553 strcpy(fname,GetDataDir().string().c_str());
7cce5f6a 1554#ifdef WIN32
b55c2367 1555 while ( fname[strlen(fname)-1] != '\\' )
1556 fname[strlen(fname)-1] = 0;
1557 if ( iter == 0 )
1558 strcat(fname,".komodo\\komodo.conf");
1559 else strcat(fname,".bitcoin\\bitcoin.conf");
7cce5f6a 1560#else
b55c2367 1561 while ( fname[strlen(fname)-1] != '/' )
1562 fname[strlen(fname)-1] = 0;
1563#ifdef __APPLE__
1564 if ( iter == 0 )
1565 strcat(fname,"Komodo/Komodo.conf");
1566 else strcat(fname,"Bitcoin/Bitcoin.conf");
1567#else
1568 if ( iter == 0 )
1569 strcat(fname,".komodo/komodo.conf");
1570 else strcat(fname,".bitcoin/bitcoin.conf");
7cce5f6a 1571#endif
b55c2367 1572#endif
1573 if ( (fp= fopen(fname,"rb")) != 0 )
1574 {
1575 komodo_userpass(username,password,fp);
1576 sprintf(iter == 0 ? KMDUSERPASS : BTCUSERPASS,"%s:%s",username,password);
1577 fclose(fp);
1578 //printf("KOMODO.(%s) -> userpass.(%s)\n",fname,KMDUSERPASS);
1579 } else printf("couldnt open.(%s)\n",fname);
eea23236 1580 if ( IS_KOMODO_NOTARY == 0 )
1581 break;
b55c2367 1582 }
935fc055 1583 }
9d365796 1584 //fprintf(stderr,"%s chain params initialized\n",ASSETCHAINS_SYMBOL);
1bfdde1d 1585}
1586
ab918767 1587void komodo_nameset(char *symbol,char *dest,char *source)
1bfdde1d 1588{
ab918767 1589 if ( source[0] == 0 )
1bfdde1d 1590 {
8a7c9241 1591 strcpy(symbol,(char *)"KMD");
1592 strcpy(dest,(char *)"BTC");
1bfdde1d 1593 }
1594 else
1595 {
ab918767 1596 strcpy(symbol,source);
8a7c9241 1597 strcpy(dest,(char *)"KMD");
ab918767 1598 }
1599}
1600
ab918767 1601struct komodo_state *komodo_stateptrget(char *base)
1602{
1603 int32_t baseid;
8a7c9241 1604 if ( base == 0 || base[0] == 0 || strcmp(base,(char *)"KMD") == 0 )
2d116f01 1605 return(&KOMODO_STATES[33]);
ab918767 1606 else if ( (baseid= komodo_baseid(base)) >= 0 )
1607 return(&KOMODO_STATES[baseid+1]);
2d116f01 1608 else return(&KOMODO_STATES[0]);
1609}
1610
1611struct komodo_state *komodo_stateptr(char *symbol,char *dest)
1612{
1613 int32_t baseid;
1614 komodo_nameset(symbol,dest,ASSETCHAINS_SYMBOL);
1615 return(komodo_stateptrget(symbol));
ab918767 1616}
This page took 0.443392 seconds and 4 git commands to generate.