-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUID_fillCache.c
More file actions
566 lines (507 loc) · 17.8 KB
/
UID_fillCache.c
File metadata and controls
566 lines (507 loc) · 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
* Copyright (c) 2016-2018. Uniquid Inc. or its affiliates. All Rights Reserved.
*
* License is in the "LICENSE" file accompanying this file.
* See the License for the specific language governing permissions and limitations under the License.
*/
/*
* @file UID_fillCache.c
*
* @date 05/jan/2016
* @author M. Palumbi
*/
/**
* @file UID_fillCache.h
*
* The module implements the filling of cache crontracts from
* the blockchain
*
*/
#include <stdio.h>
#include <string.h>
#include "yajl/yajl_tree.h"
#include "yajl/yajl_parse.h"
#include "UID_fillCache.h"
#include "UID_utils.h"
#include "UID_identity.h"
#include "UID_dispatch.h"
#include "UID_log.h"
/**
* Base url of the Name Registry appliance<br>
* Defaults to http://appliance4.uniquid.co:8080/registry
*/
char *UID_pRegistryURL = UID_REGISTRY;
/**
* check the "n" vout to be a valid imprinting
*
* @return 0 not valid 1 valid
*/
static int check_vout(int n, yajl_val vout, UID_SecurityProfile *sp)
{
yajl_val addresses;
// check validity (vout n not spent)
const char * path[] = { "spentIndex", (const char *) 0, (const char *) 0 };
if (NULL != yajl_tree_get(vout->u.array.values[n], path, yajl_t_number)) {
UID_log(UID_LOG_INFO," ### spent!!\n");
return 0; // vout spent
}
UID_log(UID_LOG_INFO," ### not spent!!\n");
// get the addresses of the first vout (array)
path[0] = "scriptPubKey";
path[1] = "addresses";
addresses = yajl_tree_get(vout->u.array.values[n], path, yajl_t_array);
if (NULL == addresses) {
return 0;
}
// get the address
char *s = YAJL_GET_STRING(addresses->u.array.values[0]);
if (NULL == s) {
return 0;
}
// check if it match the address we are looking for
if (0 != strcmp(s, sp->serviceProviderAddress)) {
return 0;
}
UID_log(UID_LOG_INFO," %s %s\n", s, sp->serviceProviderAddress);
return 1;
}
/**
* Parse for a valid IMPRINTING transaction
*
* @return 1 if valid
*/
static int parse_imprinting(yajl_val jnode, UID_SecurityProfile *sp)
{
yajl_val vin, vout, str;
unsigned i;
// get the vout array
const char * path[] = { "vout", (const char *) 0, (const char *) 0 };
vout = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL == vout) {
return 0;
}
for (i=0; i<vout->u.array.len; i++) {
if ( 0 != check_vout(i, vout, sp) ) break;
}
if ( i >= vout->u.array.len) return 0;
// if ( 0 == check_vout(0, vout, sp) )
// if (0 == check_vout(1, vout, sp) )
// return 0;
// get the vin array
path[0] = "vin";
path[1] = NULL;
vin = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL == vin) {
return 0;
}
// get the addr of the first input (imprinting orchestrator)
path[0] = "addr";
str = yajl_tree_get(vin->u.array.values[0], path, yajl_t_string);
char *s = YAJL_GET_STRING(str);
if ( NULL == s ) {
return 0;
}
UID_log(UID_LOG_INFO," user: %s\n", s);
strncpy(sp->serviceUserAddress, s, sizeof(sp->serviceUserAddress));
memset(&(sp->profile), 0x00, sizeof(sp->profile));
memset(&(sp->profile.bit_mask), 0xFF, UID_RPC_RESERVED/8);
return 1;
}
/**
* Parse for a valid PROVIDER transaction
*
* @return 1 if valid
*/
static int parse_provider(yajl_val jnode, UID_SecurityProfile *sp)
{
yajl_val vin, vout, str, addresses;
// get the vin array
const char * path[] = { "vin", (const char *) 0, (const char *) 0 };
vin = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL == vin) {
return 0;
}
// get the addr of the first input (provider)
path[0] = "addr";
str = yajl_tree_get(vin->u.array.values[0], path, yajl_t_string);
char *s = YAJL_GET_STRING(str);
if ( NULL == s ) {
return 0;
}
// check if it match the address we are looking for
if (0 != strcmp(s, sp->serviceProviderAddress)) {
return 0;
}
UID_log(UID_LOG_INFO," %s %s\n", s, sp->serviceProviderAddress);
// get the vout array
path[0] = "vout";
vout = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL == vout) {
return 0;
}
// check validity (vout n 2 not spent)
path[0] = "spentIndex";
if (NULL != yajl_tree_get(vout->u.array.values[2], path, yajl_t_number)) {
UID_log(UID_LOG_INFO," ### spent!!\n");
return 0; // vout spent
}
UID_log(UID_LOG_INFO," ### not spent!!\n");
// get the addresses of the first vout (array)
path[0] = "scriptPubKey";
path[1] = "addresses";
addresses = yajl_tree_get(vout->u.array.values[0], path, yajl_t_array);
if (NULL == addresses) {
return 0;
}
// get the address of the user
s = YAJL_GET_STRING(addresses->u.array.values[0]);
if (NULL == s) {
return 0;
}
UID_log(UID_LOG_INFO," user: %s\n", s);
strncpy(sp->serviceUserAddress, s, sizeof(sp->serviceUserAddress));
// get the OP_RETURN (smart contract)
path[0] = "scriptPubKey";
path[1] = "hex";
s = YAJL_GET_STRING(yajl_tree_get(vout->u.array.values[1], path, yajl_t_string));
if ((NULL == s) || (166 != strlen(s))) {
return 0;
}
UID_log(UID_LOG_INFO," <%s>\n", s + 6);
fromhex(s + 6, (uint8_t *)&(sp->profile), sizeof(sp->profile));
return 1;
}
/**
* Parse for a valid USER transaction
*
* @return 1 if valid
*/
static int parse_user(yajl_val jnode, UID_ClientProfile *cp)
{
yajl_val vin, vout, str, addresses;
// get the vout array
const char * path[] = { "vout", (const char *) 0, (const char *) 0 };
vout = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL == vout) {
return 0;
}
// get the addresses of the first vout (array)
path[0] = "scriptPubKey";
path[1] = "addresses";
addresses = yajl_tree_get(vout->u.array.values[0], path, yajl_t_array);
if (NULL == addresses) {
return 0;
}
// get the address of the user
char *s = YAJL_GET_STRING(addresses->u.array.values[0]);
if (NULL == s) {
return 0;
}
UID_log(UID_LOG_INFO," user: %s\n", s);
// check if it match the address we are looking for
if (0 != strcmp(s, cp->serviceUserAddress)) {
return 0;
}
UID_log(UID_LOG_INFO," %s %s\n", s, cp->serviceUserAddress);
// get the OP_RETURN (smart contract)
path[0] = "scriptPubKey";
path[1] = "hex";
s = YAJL_GET_STRING(yajl_tree_get(vout->u.array.values[1], path, yajl_t_string));
if ((NULL == s) || (166 != strlen(s))) {
return 0;
}
UID_log(UID_LOG_INFO," <%s>\n", s + 6);
// check validity (vout n 2 not spent)
path[0] = "spentIndex";
path[1] = NULL;
if (NULL != yajl_tree_get(vout->u.array.values[2], path, yajl_t_number)) {
UID_log(UID_LOG_INFO," ### spent!!\n");
return 0; // vout spent
}
UID_log(UID_LOG_INFO," ### not spent!!\n");
// get the vin array
path[0] = "vin";
vin = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL == vin) {
return 0;
}
// get the addr of the first input (provider)
path[0] = "addr";
str = yajl_tree_get(vin->u.array.values[0], path, yajl_t_string);
s = YAJL_GET_STRING(str);
if ( NULL == s ) {
return 0;
}
strncpy(cp->serviceProviderAddress, s, sizeof(cp->serviceProviderAddress));
memset(cp->serviceProviderName, 0x00, sizeof(cp->serviceProviderName));
return 1;
}
static char curlbuffer[UID_CURL_BUFFER_SIZE];
#define USER 0
#define PROVIDER 1
#define IMPRINTING 2
/**
* Minimum confirmations required to accept the contract<br>
* Defaults to 1
*/
int UID_confirmations = 1;
/**
* check the tx for a valid contract of type "type"
* if found add it to the cache buffer
*
* @param[in] curl pointer to an initialized UID_HttpOBJ struct
* @param[out] secondb pointer to the contracts cache buffer
* to be filled
* @param[in] tx transaction to check
* @param[in] type type of contract to look for: PROVIDER IMPRINTING USER
* @param[in] address address we are looking for
* @param[in] bip32p Bip32 path of the address
*
* @return UID_CONTRACTS_OK no error <br>
* UID_CONTRACTS_SERV_ERROR error contacting the server
*/
static int check_contract(UID_HttpOBJ *curl, cache_buffer *secondb, char * tx, char *address, UID_Bip32Path *bip32p, int type)
{
yajl_val jnode, v;
char url[256];
snprintf(url, sizeof(url), UID_GETCONTRACT, tx);
if(UID_HTTP_OK != UID_httpget(curl, url, curlbuffer, sizeof(curlbuffer))) {
return UID_CONTRACTS_SERV_ERROR;
}
jnode = yajl_tree_parse(curlbuffer, NULL, 0);
if (NULL == jnode)
return UID_CONTRACTS_SERV_ERROR;
const char * path[] = { "confirmations", (const char *) 0, (const char *) 0 };
v = yajl_tree_get(jnode, path, yajl_t_number);
if (NULL == v) {
yajl_tree_free(jnode);
return UID_CONTRACTS_SERV_ERROR;
}
UID_log(UID_LOG_INFO," confirmations: %lld\n", YAJL_GET_INTEGER(v));
if(UID_confirmations <= YAJL_GET_INTEGER(v)) {
if (type == IMPRINTING) {
UID_SecurityProfile *sp = &(secondb->contractsCache)[secondb->validCacheEntries];
// fill the provider field
strncpy(sp->serviceProviderAddress, address, sizeof(sp->serviceProviderAddress));
memcpy(&(sp->path), bip32p, sizeof(sp->path));
if (parse_imprinting(jnode, sp))
secondb->validCacheEntries++;
}
if (type == PROVIDER) {
UID_SecurityProfile *sp = &(secondb->contractsCache)[secondb->validCacheEntries];
// fill the provider field
strncpy(sp->serviceProviderAddress, address, sizeof(sp->serviceProviderAddress));
memcpy(&(sp->path), bip32p, sizeof(sp->path));
if (parse_provider(jnode, sp))
secondb->validCacheEntries++;
}
if (type == USER) {
UID_ClientProfile *cp = &(secondb->clientCache)[secondb->validClientEntries];
strncpy(cp->serviceUserAddress, address, sizeof(cp->serviceUserAddress));
memcpy(&(cp->path), bip32p, sizeof(cp->path));
if (parse_user(jnode, cp))
secondb->validClientEntries++;
}
}
yajl_tree_free(jnode);
return UID_CONTRACTS_OK;
}
/**
* lookup the Block-Chain for a given address to see
* if it received transactions. If some TX exists for the address,
* call check_contract() to look for valid contracts
*
* @param[in] curl pointer to an initialized UID_HttpOBJ struct
* @param[out] secondb pointer to the contracts cache buffer
* to be filled
* @param[in] bip32p Bip32 path of the address to check
* @param[in] type type of contract to look for: PROVIDER IMPRINTING USER
*
* @return UID_CONTRACTS_OK transaction exists for the given address <br>
* UID_CONTRACTS_NO_TX no transactions for the given address <br>
* UID_CONTRACTS_SERV_ERROR error contacting the server
*/
static int check_address(UID_HttpOBJ *curl, cache_buffer *secondb, UID_Bip32Path *bip32p, int type)
{
char address[36];
int res;
unsigned i;
char url[256];
yajl_val jnode, transactions;//, v;
UID_getAddressAt(bip32p, address, sizeof(address));
UID_log(UID_LOG_INFO,"==>> %s\n", address);
snprintf(url, sizeof(url), UID_GETTXS, address);
if(UID_HTTP_OK != UID_httpget(curl, url, curlbuffer, sizeof(curlbuffer))) {
return UID_CONTRACTS_SERV_ERROR;
}
jnode = yajl_tree_parse(curlbuffer, NULL, 0);
if (NULL == jnode)
return UID_CONTRACTS_SERV_ERROR;
const char * path[] = { "transactions",(const char *) 0 };
transactions = yajl_tree_get(jnode, path, yajl_t_array);
if (NULL != transactions) {
res = UID_CONTRACTS_NO_TX;
if (type == IMPRINTING) {
UID_log(UID_LOG_INFO," @@ check for imprinting @@\n");
i = transactions->u.array.len;
if (i > 0) {
// first tx (last on the array)
char *str = YAJL_GET_STRING(transactions->u.array.values[i-1]);
UID_log(UID_LOG_INFO," ---> %s ---\n", str);
res = check_contract(curl, secondb, str, address, bip32p, type);
}
}
else {
for (i = 0; i < transactions->u.array.len; i++) {
char *str = YAJL_GET_STRING(transactions->u.array.values[i]);
UID_log(UID_LOG_INFO," ---> %s ---\n", str);
res = check_contract(curl, secondb, str, address, bip32p, type);
if (res != UID_CONTRACTS_OK)
break;
}
}
}
else {
res = UID_CONTRACTS_SERV_ERROR;
UID_log(UID_LOG_INFO," parse error, no transactions field\n");
}
yajl_tree_free(jnode);
return res;
}
/**
* Gets the providers name of the contracts from the Registry
*
* @param[in] curl pointer to an initialized UID_HttpOBJ struct
* @param[out] secondb pointer to the contracts cache buffer
* to be filled with the names
*/
static int get_providers_name(UID_HttpOBJ *curl, cache_buffer *secondb)
{
yajl_val jnode, v;
int i;
size_t j;
char *s;
char registryurl[256];
for (i=0; i<secondb->validClientEntries;i++) {
snprintf(registryurl, sizeof(registryurl),"%s?providerAddress=%s", UID_pRegistryURL,secondb->clientCache[i].serviceProviderAddress);
if(UID_HTTP_OK != UID_httpget(curl, registryurl, curlbuffer, sizeof(curlbuffer))) {
continue;
}
jnode = yajl_tree_parse(curlbuffer, NULL, 0);
UID_log(UID_LOG_DEBUG, "url=%s jnode=%p\n", registryurl, jnode);
if (NULL == jnode)
continue;
if (!YAJL_IS_ARRAY(jnode)) {
yajl_tree_free(jnode);
continue;
}
const char * path[] = { NULL, (const char *) 0 };
for (j=0; j<jnode->u.array.len; j++) {
path[0] = "provider_address";
v = yajl_tree_get(jnode->u.array.values[j], path, yajl_t_string);
s = YAJL_GET_STRING(v);
if (!strcmp(s, secondb->clientCache[i].serviceProviderAddress)) {
path[0] = "provider_name";
v = yajl_tree_get(jnode->u.array.values[j], path, yajl_t_string);
s = YAJL_GET_STRING(v);
strncpy(secondb->clientCache[i].serviceProviderName, s, sizeof(secondb->clientCache[i].serviceProviderName));
}
}
yajl_tree_free(jnode);
}
return 0;
}
/**
* Lookup all the Bip32 addresses looking for contracts
*
* - imprinting contract <br>
* - provider contracts <br>
* - user contracts <br>
*
* @param[in] curl pointer to an initialized UID_HttpOBJ struct
* @param[out] secondb pointer to the contracts cache buffer
* to be filled
*
* @return UID_CONTRACTS_OK no error <br>
* UID_CONTRACTS_SERV_ERROR error contacting the server
*
* @todo handle errors from get_providers_name
*/
int UID_fillCache(UID_HttpOBJ *curl, cache_buffer *secondb)
{
int res, gap;
UID_Bip32Path path;
(secondb->validCacheEntries) = 0; // void the cache
(secondb->validClientEntries) = 0; // void the cache
UID_log(UID_LOG_INFO,"================================================================\n");
// look for contract on the first external addresses
path.p_u = 0; //provider
path.account = 0;
path.n = 0;
res = check_address(curl, secondb, &path, PROVIDER);
if ( UID_CONTRACTS_SERV_ERROR == res )
return UID_CONTRACTS_SERV_ERROR;
UID_log(UID_LOG_INFO,"----------------------------------------------------------------\n");
// look for contracts on all internal addresses
path.p_u = 0; //provider
path.account = 1;
for (path.n = 0, gap = 0; gap < 5; path.n++) {
res = check_address(curl, secondb, &path, PROVIDER);
if ( UID_CONTRACTS_SERV_ERROR == res )
return UID_CONTRACTS_SERV_ERROR;
if ( UID_CONTRACTS_NO_TX == res )
gap ++;
else
gap = 0;
}
UID_log(UID_LOG_INFO,"----------------------------------------------------------------\n");
// look for imprinting on the first external addresses
if (0 == secondb->validCacheEntries) {
path.p_u = 0; //provider
path.account = 0;
path.n = 0;
res = check_address(curl, secondb, &path, IMPRINTING);
if ( UID_CONTRACTS_SERV_ERROR == res )
return UID_CONTRACTS_SERV_ERROR;
}
UID_log(UID_LOG_INFO,"----------------------------------------------------------------\n");
path.p_u = 1; //user
path.account = 0;
for (path.n = 0, gap = 0; gap < 5; path.n++) {
res = check_address(curl, secondb, &path, USER);
if ( UID_CONTRACTS_SERV_ERROR == res )
return UID_CONTRACTS_SERV_ERROR;
if ( UID_CONTRACTS_NO_TX == res )
gap ++;
else
gap = 0;
}
get_providers_name(curl, secondb);
return UID_CONTRACTS_OK;
}
#ifdef UID_IMPLEMENTSENDTX
/**
* Sends a signed transaction to the block-chain using
* Insight API service
*
* @param[in] signed_tx signed transaction as hex string (ascii)
* @param[out] ret string buffer to be filled with the
* result from Insight API. The txid if all OK, es: <br>
* {"txid":"3cd0f12a587945c75edde69e8989260fb4126b6ae803cb26de751e62a47137be"}
* @param[in] size size of ret buffer
*
* @return UID_HTTP_OK == no error
*/
int UID_sendTx(char *signed_tx, char *ret, size_t size)
{
UID_HttpOBJ *curl;
int res;
char url[256];
curl = UID_httpinit();
snprintf(url, sizeof(url), UID_SENDTX);
res = UID_httppost(curl, url, signed_tx, ret, size);
/* always cleanup */
UID_httpcleanup(curl);
return res;
}
#endif //UID_IMPLEMENTSENDTX