This repository was archived by the owner on Jul 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontinusec.js
More file actions
1949 lines (1769 loc) · 72.2 KB
/
Copy pathcontinusec.js
File metadata and controls
1949 lines (1769 loc) · 72.2 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
A JavaScript implementation of the SHA family of hashes, as
defined in FIPS PUB 180-2 as well as the corresponding HMAC implementation
as defined in FIPS PUB 198a
Copyright Brian Turek 2008-2015
Distributed under the BSD License
See http://caligatio.github.com/jsSHA/ for more information
Several functions taken from Paul Johnston
*/
'use strict';(function(H){function v(c,a,b){var g=0,d=[],f=0,e,h,n,l,m,F,r,p=!1,k=!1,q=[],t=[],u,y=!1;b=b||{};e=b.encoding||"UTF8";u=b.numRounds||1;n=z(a,e);if(u!==parseInt(u,10)||1>u)throw Error("numRounds must a integer >= 1");F=function(a,b){return A(a,b,c)};r=function(a,b,f,d){var g,e;if("SHA-224"===c||"SHA-256"===c)g=(b+65>>>9<<4)+15,e=16;else throw Error("Unexpected error in SHA-2 implementation");for(;a.length<=g;)a.push(0);a[b>>>5]|=128<<24-b%32;a[g]=b+f;f=a.length;for(b=0;b<f;b+=e)d=A(a.slice(b,
b+e),d,c);if("SHA-224"===c)a=[d[0],d[1],d[2],d[3],d[4],d[5],d[6]];else if("SHA-256"===c)a=d;else throw Error("Unexpected error in SHA-2 implementation");return a};if("SHA-224"===c)m=512,l=224;else if("SHA-256"===c)m=512,l=256;else throw Error("Chosen SHA variant is not supported");h=w(c);this.setHMACKey=function(a,b,d){var f;if(!0===k)throw Error("HMAC key already set");if(!0===p)throw Error("Cannot set HMAC key after finalizing hash");if(!0===y)throw Error("Cannot set HMAC key after calling update");
e=(d||{}).encoding||"UTF8";b=z(b,e)(a);a=b.binLen;b=b.value;f=m>>>3;d=f/4-1;if(f<a/8){for(b=r(b,a,0,w(c));b.length<=d;)b.push(0);b[d]&=4294967040}else if(f>a/8){for(;b.length<=d;)b.push(0);b[d]&=4294967040}for(a=0;a<=d;a+=1)q[a]=b[a]^909522486,t[a]=b[a]^1549556828;h=F(q,h);g=m;k=!0};this.update=function(a){var b,c,e,l=0,p=m>>>5;b=n(a,d,f);a=b.binLen;c=b.value;b=a>>>5;for(e=0;e<b;e+=p)l+m<=a&&(h=F(c.slice(e,e+p),h),l+=m);g+=l;d=c.slice(l>>>5);f=a%m;y=!0};this.getHash=function(a,b){var e,m,n;if(!0===
k)throw Error("Cannot call getHash after setting HMAC key");n=B(b);switch(a){case "HEX":e=function(a){return C(a,n)};break;case "B64":e=function(a){return D(a,n)};break;case "BYTES":e=E;break;default:throw Error("format must be HEX, B64, or BYTES");}if(!1===p)for(h=r(d,f,g,h),m=1;m<u;m+=1)h=r(h,l,0,w(c));p=!0;return e(h)};this.getHMAC=function(a,b){var e,n,q;if(!1===k)throw Error("Cannot call getHMAC without first setting HMAC key");q=B(b);switch(a){case "HEX":e=function(a){return C(a,q)};break;case "B64":e=
function(a){return D(a,q)};break;case "BYTES":e=E;break;default:throw Error("outputFormat must be HEX, B64, or BYTES");}!1===p&&(n=r(d,f,g,h),h=F(t,w(c)),h=r(n,l,m,h));p=!0;return e(h)}}function k(){}function I(c,a,b){var g=c.length,d,f,e,h,n;a=a||[0];b=b||0;n=b>>>3;if(0!==g%2)throw Error("String of HEX type must be in byte increments");for(d=0;d<g;d+=2){f=parseInt(c.substr(d,2),16);if(isNaN(f))throw Error("String of HEX type contains invalid characters");h=(d>>>1)+n;for(e=h>>>2;a.length<=e;)a.push(0);
a[e]|=f<<8*(3-h%4)}return{value:a,binLen:4*g+b}}function J(c,a,b){var g=[],d,f,e,h,g=a||[0];b=b||0;f=b>>>3;for(d=0;d<c.length;d+=1)a=c.charCodeAt(d),h=d+f,e=h>>>2,g.length<=e&&g.push(0),g[e]|=a<<8*(3-h%4);return{value:g,binLen:8*c.length+b}}function K(c,a,b){var g=[],d=0,f,e,h,n,l,m,g=a||[0];b=b||0;a=b>>>3;if(-1===c.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");e=c.indexOf("=");c=c.replace(/\=/g,"");if(-1!==e&&e<c.length)throw Error("Invalid '=' found in base-64 string");
for(e=0;e<c.length;e+=4){l=c.substr(e,4);for(h=n=0;h<l.length;h+=1)f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(l[h]),n|=f<<18-6*h;for(h=0;h<l.length-1;h+=1){m=d+a;for(f=m>>>2;g.length<=f;)g.push(0);g[f]|=(n>>>16-8*h&255)<<8*(3-m%4);d+=1}}return{value:g,binLen:8*d+b}}function C(c,a){var b="",g=4*c.length,d,f;for(d=0;d<g;d+=1)f=c[d>>>2]>>>8*(3-d%4),b+="0123456789abcdef".charAt(f>>>4&15)+"0123456789abcdef".charAt(f&15);return a.outputUpper?b.toUpperCase():b}function D(c,
a){var b="",g=4*c.length,d,f,e;for(d=0;d<g;d+=3)for(e=d+1>>>2,f=c.length<=e?0:c[e],e=d+2>>>2,e=c.length<=e?0:c[e],e=(c[d>>>2]>>>8*(3-d%4)&255)<<16|(f>>>8*(3-(d+1)%4)&255)<<8|e>>>8*(3-(d+2)%4)&255,f=0;4>f;f+=1)8*d+6*f<=32*c.length?b+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(e>>>6*(3-f)&63):b+=a.b64Pad;return b}function E(c){var a="",b=4*c.length,g,d;for(g=0;g<b;g+=1)d=c[g>>>2]>>>8*(3-g%4)&255,a+=String.fromCharCode(d);return a}function B(c){var a={outputUpper:!1,b64Pad:"="};
c=c||{};a.outputUpper=c.outputUpper||!1;a.b64Pad=c.b64Pad||"=";if("boolean"!==typeof a.outputUpper)throw Error("Invalid outputUpper formatting option");if("string"!==typeof a.b64Pad)throw Error("Invalid b64Pad formatting option");return a}function z(c,a){var b;switch(a){case "UTF8":case "UTF16BE":case "UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE");}switch(c){case "HEX":b=I;break;case "TEXT":b=function(b,c,f){var e=[],h=[],n=0,l,m,k,r,p,e=c||[0];c=f||0;k=c>>>3;if("UTF8"===
a)for(l=0;l<b.length;l+=1)for(f=b.charCodeAt(l),h=[],128>f?h.push(f):2048>f?(h.push(192|f>>>6),h.push(128|f&63)):55296>f||57344<=f?h.push(224|f>>>12,128|f>>>6&63,128|f&63):(l+=1,f=65536+((f&1023)<<10|b.charCodeAt(l)&1023),h.push(240|f>>>18,128|f>>>12&63,128|f>>>6&63,128|f&63)),m=0;m<h.length;m+=1){p=n+k;for(r=p>>>2;e.length<=r;)e.push(0);e[r]|=h[m]<<8*(3-p%4);n+=1}else if("UTF16BE"===a||"UTF16LE"===a)for(l=0;l<b.length;l+=1){f=b.charCodeAt(l);"UTF16LE"===a&&(m=f&255,f=m<<8|f>>>8);p=n+k;for(r=p>>>
2;e.length<=r;)e.push(0);e[r]|=f<<8*(2-p%4);n+=2}return{value:e,binLen:8*n+c}};break;case "B64":b=K;break;case "BYTES":b=J;break;default:throw Error("format must be HEX, TEXT, B64, or BYTES");}return b}function t(c,a){return c>>>a|c<<32-a}function L(c,a,b){return c&a^~c&b}function M(c,a,b){return c&a^c&b^a&b}function N(c){return t(c,2)^t(c,13)^t(c,22)}function O(c){return t(c,6)^t(c,11)^t(c,25)}function P(c){return t(c,7)^t(c,18)^c>>>3}function Q(c){return t(c,17)^t(c,19)^c>>>10}function R(c,a){var b=
(c&65535)+(a&65535);return((c>>>16)+(a>>>16)+(b>>>16)&65535)<<16|b&65535}function S(c,a,b,g){var d=(c&65535)+(a&65535)+(b&65535)+(g&65535);return((c>>>16)+(a>>>16)+(b>>>16)+(g>>>16)+(d>>>16)&65535)<<16|d&65535}function T(c,a,b,g,d){var f=(c&65535)+(a&65535)+(b&65535)+(g&65535)+(d&65535);return((c>>>16)+(a>>>16)+(b>>>16)+(g>>>16)+(d>>>16)+(f>>>16)&65535)<<16|f&65535}function w(c){var a,b;a=[3238371032,914150663,812702999,4144912697,4290775857,1750603025,1694076839,3204075428];b=[1779033703,3144134277,
1013904242,2773480762,1359893119,2600822924,528734635,1541459225];switch(c){case "SHA-224":c=a;break;case "SHA-256":c=b;break;case "SHA-384":c=[new k,new k,new k,new k,new k,new k,new k,new k];break;case "SHA-512":c=[new k,new k,new k,new k,new k,new k,new k,new k];break;default:throw Error("Unknown SHA variant");}return c}function A(c,a,b){var g,d,f,e,h,n,l,m,k,r,p,t,q,v,u,y,w,z,A,B,C,D,x=[],E;if("SHA-224"===b||"SHA-256"===b)r=64,t=1,D=Number,q=R,v=S,u=T,y=P,w=Q,z=N,A=O,C=M,B=L,E=G;else throw Error("Unexpected error in SHA-2 implementation");
b=a[0];g=a[1];d=a[2];f=a[3];e=a[4];h=a[5];n=a[6];l=a[7];for(p=0;p<r;p+=1)16>p?(k=p*t,m=c.length<=k?0:c[k],k=c.length<=k+1?0:c[k+1],x[p]=new D(m,k)):x[p]=v(w(x[p-2]),x[p-7],y(x[p-15]),x[p-16]),m=u(l,A(e),B(e,h,n),E[p],x[p]),k=q(z(b),C(b,g,d)),l=n,n=h,h=e,e=q(f,m),f=d,d=g,g=b,b=q(m,k);a[0]=q(b,a[0]);a[1]=q(g,a[1]);a[2]=q(d,a[2]);a[3]=q(f,a[3]);a[4]=q(e,a[4]);a[5]=q(h,a[5]);a[6]=q(n,a[6]);a[7]=q(l,a[7]);return a}var G;G=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,
3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,
1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298];"function"===typeof define&&define.amd?define(function(){return v}):"undefined"!==typeof exports?"undefined"!==typeof module&&module.exports?module.exports=exports=v:exports=v:H.jsSHA=v})(this);
/*
Copyright 2016 Continusec Pty Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Indicates network error.
*/
var CONTINUSEC_NETWORK_ERROR = "CONTINUSEC_NETWORK_ERROR";
/**
* Indicates invalid size or range in the request, e.g. tree size too large or small.
*/
var CONTINUSEC_INVALID_RANGE_ERROR = "CONTINUSEC_INVALID_RANGE_ERROR";
/**
* Indicates that either the wrong API Key is being used, or the account is suspended for other reasons (check billing status in console).
*/
var CONTINUSEC_UNAUTHORIZED_ERROR = "CONTINUSEC_UNAUTHORIZED_ERROR";
/**
* Indicates the object cannot be found.
*/
var CONTINUSEC_NOT_FOUND_ERROR = "CONTINUSEC_NOT_FOUND_ERROR";
/**
* Indicates internal error that occurred on the server.
*/
var CONTINUSEC_INTERNAL_ERROR = "CONTINUSEC_INTERNAL_ERROR";
/**
* Indicates that object being modified already exists.
*/
var CONTINUSEC_OBJECT_CONFLICT_ERROR = "CONTINUSEC_OBJECT_CONFLICT_ERROR";
/**
* Indicates the verification of a proof has failed.
*/
var CONTINUSEC_VERIFICATION_ERROR = "CONTINUSEC_VERIFICATION_ERROR";
/**
* Indicates that not all entries were returned. Typically due to requesting Json, but not
* storing as such.
*/
var CONTINUSEC_NOT_ALL_ENTRIES_RETURNED_ERROR = "CONTINUSEC_NOT_ALL_ENTRIES_RETURNED_ERROR";
/**
* HEAD can be substituted for tree size in requests for fetch tree hashes. Specifying
* this values means to fetch the latest tree hash present.
*/
var CONTINUSEC_HEAD = 0;
/**
* Create a ContinusecClient for a given account with specified API Key.
* baseURL is optional and normally only used for unit tests of the ContinusecClient API
* that may wish to use a custom URL to send API requests to.
*
* @param {string} account the account number, found on the "Settings" tab in the console.
* @param {string} apiKey the API Key, found on the "API Keys" tab in the console.
* @param {string} baseURL the base URL to send API requests to.
*
* @constructor
* @classdesc Main entry point for interacting with Continusec's Verifiable Data Structure APIs.
*/
var ContinusecClient = function (account, apiKey, baseURL) {
this.account = account;
this.apiKey = apiKey;
if (baseURL == undefined) {
this.baseURL = "https://api.continusec.com";
} else {
this.baseURL = baseURL;
}
};
/**
* Return a pointer to a verifiable map that belongs to this account.
*
* @param {string} name name of the map to access.
* @return {VerifiableMap} an object that allows manipulation of the specified map.
*/
ContinusecClient.prototype.getVerifiableMap = function (name) {
return new VerifiableMap(this, "/map/" + name);
};
/**
* Return a pointer to a verifiable log that belongs to this account.
*
* @param {string} name name of the log to access.
* @return {VerifiableLog} an object that allows manipulation of the specified log.
*/
ContinusecClient.prototype.getVerifiableLog = function (name) {
return new VerifiableLog(this, "/log/" + name);
};
/**
* Fetch the list of logs held by this account.
* @param {listLogsSuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
ContinusecClient.prototype.listLogs = function (success, failure) {
this.makeRequest("GET", "/logs", null, null, function (data) {
var obj = JSON.parse(data);
var rv = [];
for (var i = 0; i < obj.results.length; i++) {
rv.push(new LogInfo(obj.results[i].name));
}
success(rv);
}, failure);
};
/**
* Fetch the list of maps held by this account.
* @param {listMapsSuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
ContinusecClient.prototype.listMaps = function (success, failure) {
this.makeRequest("GET", "/maps", null, null, function (data) {
var obj = JSON.parse(data);
var rv = [];
for (var i = 0; i < obj.results.length; i++) {
rv.push(new MapInfo(obj.results[i].name));
}
success(rv);
}, failure);
};
/**
* @private
*/
ContinusecClient.prototype.makeRequest = function (method, path, data, extraHeaders, success, failure) {
var req = new XMLHttpRequest();
req.onload = function (evt) {
switch (req.status) {
case 200:
success(binaryArrayToString(new Uint8Array(req.response)), req);
break;
case 400:
failure(CONTINUSEC_INVALID_RANGE_ERROR);
break;
case 403:
failure(CONTINUSEC_UNAUTHORIZED_ERROR);
break;
case 404:
failure(CONTINUSEC_NOT_FOUND_ERROR);
break;
case 409:
failure(CONTINUSEC_OBJECT_CONFLICT_ERROR);
break;
default:
failure(CONTINUSEC_INTERNAL_ERROR);
}
};
req.onerror = function (evt) {
failure(CONTINUSEC_NETWORK_ERROR);
};
req.open(method, this.baseURL + "/v1/account/" + this.account + path, true);
req.responseType = "arraybuffer";
req.setRequestHeader("Authorization", 'Key ' + this.apiKey);
if (extraHeaders != null) {
for (var k in extraHeaders) {
req.setRequestHeader(k, extraHeaders[k]);
}
}
req.send(data);
};
/**
* Private constructor. Use {@link ContinusecClient#getVerifiableMap(String)} to instantiate.
* @constructor
* @classdesc Class to manage interactions with a Verifiable Map. Use {@link ContinusecClient#getVerifiableMap(String)} to instantiate.
*/
var VerifiableMap = function (client, path) {
this.client = client;
this.path = path;
};
/**
* Get a pointer to the mutation log that underlies this verifiable map. Since the mutation log
* is managed by the map, it cannot be directly modified, however all read operations are supported.
* Note that mutations themselves are stored as {@link JsonEntry} format, so {@link JsonEntryFactory#getInstance()} should
* be used for entry retrieval.
* @return {VerifiableLog} the mutation log.
*/
VerifiableMap.prototype.getMutationLog = function () {
return new VerifiableLog(this.client, this.path + "/log/mutation");
};
/**
* Get a pointer to the tree head log that contains all map root hashes produced by this map. Since the tree head log
* is managed by the map, it cannot be directly modified, however all read operations are supported.
* Note that tree heaads themselves are stored as {@link JsonEntry} format, so {@link JsonEntryFactory#getInstance()} should
* be used for entry retrieval.
* @return {VerifiableLog} the tree head log.
*/
VerifiableMap.prototype.getTreeHeadLog = function () {
return new VerifiableLog(this.client, this.path + "/log/treehead");
};
/**
* Failure callback is called upon error. Typically the reason passed is one of the
* error constants defined in this module.
*
* @callback failureCallback
* @param {string} reason
*/
/**
* Success callback is called upon success with no arguments passed to it.
* @callback emptySuccessCallback
*/
/**
* Success callback is called upon success with mapEntry passed to it.
* @param {MapEntryResponse} mapEntry
* @callback mapEntrySuccessCallback
*/
/**
* Success callback is called upon success with an entry passed to it.
* @param {VerifiableEntry} entry, ie once of RawDataEntry, JsonEntry, RedactedJsonEntry
* @callback verifiableEntrySuccessCallback
*/
/**
* Success callback is called upon success with an index and entry passed to it.
* @param {int} idx the index
* @param {VerifiableEntry} entry the value, of type RawDataEntry, JsonEntry or RedactedJsonEntry
* @callback verifiableEntryIndexSuccessCallback
*/
/**
* Success callback is called upon success with an entry passed to it.
* @param {AddEntryResponse} entry
* @callback addEntryResponseCallback
*/
/**
* Success callback is called upon success with a MapTreeHead passed to it.
* @param {MapTreeHead} entry
* @callback mapTreeHeadSuccessCallback
*/
/**
* Success callback is called upon success with a LogTreeHead passed to it.
* @param {LogTreeHead} entry
* @callback logTreeHeadSuccessCallback
*/
/**
* Success callback is called upon success with a MapTreeState passed to it.
* @param {MapTreeState} entry
* @callback mapTreeStateSuccessCallback
*/
/**
* Success callback is called upon success with a LogInclusionProof passed to it.
* @param {LogInclusionProof} entry
* @callback logInclusionProofSuccessCallback
*/
/**
* Success callback is called upon success with a LogConsistencyProof passed to it.
* @param {LogConsistencyProof} entry
* @callback logConsistencyProofSuccessCallback
*/
/**
* Success callback is called upon success with an array of LogInfo passed to it.
* @param {LogInfo[]} logs
* @callback listLogsSuccessCallback
*/
/**
* Success callback is called upon success with an array of MapInfo passed to it.
* @param {MapInfo[]} maps
* @callback listMapsSuccessCallback
*/
/**
* Send API call to create this map. This should only be called once, and subsequent
* calls will cause an exception to be generated.
* @param {emptySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.create = function (success, failure) {
this.client.makeRequest("PUT", this.path, null, null, function (data, req) {
success();
}, function (reason) {
failure(reason);
});
};
/**
* Destroy will send an API call to delete this map - this operation removes it permanently,
* and renders the name unusable again within the same account, so please use with caution.
* @param {emptySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.destroy = function (success, failure) {
this.client.makeRequest("DELETE", this.path, null, null, function (data, req) {
success();
}, function (reason) {
failure(reason);
});
};
/**
* For a given key, return the value and inclusion proof for the given treeSize.
* @param {string} key the key in the map.
* @param {int} treeSize the tree size.
* @param {VerifiableEntryFactory} factory the factory that should be used to instantiate the VerifiableEntry. Typically one of RawDataEntryFactory, JsonEntryFactory, RedactedJsonEntryFactory.
* @param {mapEntrySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.getValue = function (key, treeSize, factory, success, failure) {
this.client.makeRequest("GET", this.path + "/tree/" + treeSize + "/key/h/" + hexString(key) + factory.getFormat(), null, null, function (data, req) {
var verifiedTreeSize = req.getResponseHeader("X-Verified-Treesize");
if (verifiedTreeSize === null) {
failure(CONTINUSEC_NOT_FOUND_ERROR);
return;
}
verifiedTreeSize = Number(verifiedTreeSize);
var proof = req.getResponseHeader("X-Verified-Proof");
if (proof === null) {
proof = "";
}
var parts = proof.split(",");
var auditPath = [];
var i;
for (i = 0; i < 256; i++) {
auditPath.push(null);
}
for (i = 0; i < parts.length; i++) {
var pieces = parts[i].split("/");
if (pieces.length == 2) {
auditPath[Number(pieces[0].trim())] = decodeHex(pieces[1].trim());
}
}
success(new MapEntryResponse(key, factory.createFromBytes(data), verifiedTreeSize, auditPath));
}, function (reason) {
failure(reason);
});
};
/**
* For a given key, retrieve the value and inclusion proof, verify the proof, then return the value.
* @param {string} key the key in the map.
* @param {MapStateHead} mapState a map tree state as previously returned by {@link #getVerifiedMapState(MapTreeState,int)}
* @param {VerifiableEntryFactory} factory the factory that should be used to instantiate the VerifiableEntry. Typically one of RawDataEntryFactory, JsonEntryFactory, RedactedJsonEntryFactory.
* @param {verifiableEntrySuccessCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.getVerifiedValue = function (key, mapState, factory, success, failure) {
this.getValue(key, mapState.getTreeSize(), factory, function(mapResp) {
try {
mapResp.verify(mapState.getMapHead());
} catch (err) {
failure(err);
return;
}
success(mapResp.getValue());
}, function (reason) {
failure(reason);
});
};
/**
* Set the value for a given key in the map. Calling this has the effect of adding a mutation to the
* mutation log for the map, which then reflects in the root hash for the map. This occurs asynchronously.
* @param {string} key the key to set.
* @param {value} value the entry to set to key to. Typically one of {@link RawDataEntry}, {@link JsonEntry} or {@link RedactableJsonEntry}.
* @param {addEntryResponseCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.setValue = function (key, value, success, failure) {
this.client.makeRequest("PUT", this.path + "/key/h/" + hexString(key) + value.getFormat(), value.getDataForUpload(), null, function (data, req) {
var obj = JSON.parse(data);
success(new AddEntryResponse(atob(obj.leaf_hash)));
}, function (reason) {
failure(reason);
});
};
/**
* Set the value for a given key in the map, conditional on the previous leaf hash.
* Calling this has the effect of adding a mutation to the
* mutation log for the map, which then reflects in the root hash for the map. This occurs asynchronously.
* @param {string} key the key to set.
* @param {value} value the entry to set to key to. Typically one of {@link RawDataEntry}, {@link JsonEntry} or {@link RedactableJsonEntry}.
* @param {LeafHash} prevLeafHash the previous leaf hash - must implement getLeafHash(). Typically one of {@link RawDataEntry}, {@link JsonEntry}.
* @param {addEntryResponseCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.updateValue = function (key, value, prevLeafHash, success, failure) {
this.client.makeRequest("PUT", this.path + "/key/h/" + hexString(key) + value.getFormat(), value.getDataForUpload(), {
"X-Previous-LeafHash": hexString(prevLeafHash.getLeafHash()),
}, function (data, req) {
var obj = JSON.parse(data);
success(new AddEntryResponse(atob(obj.leaf_hash)));
}, function (reason) {
failure(reason);
});
};
/**
* Delete the value for a given key from the map. Calling this has the effect of adding a mutation to the
* mutation log for the map, which then reflects in the root hash for the map. This occurs asynchronously.
* @param {string} key the key to delete.
* @param {addEntryResponseCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.deleteValue = function (key, success, failure) {
this.client.makeRequest("DELETE", this.path + "/key/h/" + hexString(key), null, null, function (data, req) {
var obj = JSON.parse(data);
success(new AddEntryResponse(atob(obj.leaf_hash)));
}, function (reason) {
failure(reason);
});
};
/**
* Get the tree hash for given tree size.
*
* @param {int} treeSize the tree size to retrieve the hash for. Pass HEAD (0) to get the
* latest tree size.
* @param {mapTreeHeadSuccessCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.getTreeHead = function (treeSize, success, failure) {
this.client.makeRequest("GET", this.path + "/tree/" + treeSize, null, null, function (data, req) {
var obj = JSON.parse(data);
success(new MapTreeHead(new LogTreeHead(Number(obj.mutation_log.tree_size), atob(obj.mutation_log.tree_hash)), atob(obj.map_hash)));
}, function (reason) {
failure(reason);
});
};
/**
* Block until the map has caught up to a certain size.
* This polls getTreeHead(int) until
* such time as a new tree hash is produced that is of at least this size.
* This is intended for test use.
* @param {int} treeSize the tree size that we should wait for.
* @param {mapTreeHeadSuccessCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.blockUntilSize = function (treeSize, success, failure) {
doMapBlockRound(this, -1, 0, treeSize, success, failure);
}
function doMapBlockRound(log, lastHead, secsToSleep, treeSize, success, failure) {
log.getTreeHead(0, function (lth) {
if (lth.getTreeSize() > lastHead) {
if (lth.getTreeSize() >= treeSize) {
success(lth);
} else {
secsToSleep = 1;
setTimeout(function () { doMapBlockRound(log, lth.getTreeSize(), secsToSleep, treeSize, success, failure); }, secsToSleep * 1000);
}
} else {
secsToSleep *= 2
setTimeout(function () { doMapBlockRound(log, lastHead, secsToSleep, treeSize, success, failure); }, secsToSleep * 1000);
}
}, failure);
}
/**
* getVerifiedLatestMapState fetches the latest MapTreeState, verifies it is consistent with,
* and newer than, any previously passed state.
*
* @param {MapTreeState} prev previously held MapTreeState, may be null to skip consistency checks.
* @param {mapTreeStateSuccessCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.getVerifiedLatestMapState = function (prev, success, failure) {
this.getVerifiedMapState(prev, 0, function (head) {
if ((prev != null) && (head.getTreeSize() <= prev.getTreeSize())) {
success(prev);
} else {
success(head);
}
}, failure);
}
/**
* getVerifiedMapState returns a wrapper for the MapTreeHead for a given tree size, along with
* a LogTreeHead for the TreeHeadLog that has been verified to contain this map tree head.
* The value returned by this will have been proven to be consistent with any passed prev value.
* Note that the TreeHeadLogTreeHead returned may differ between calls, even for the same treeSize,
* as all future LogTreeHeads can also be proven to contain the MapTreeHead.
*
* Typical clients that only need to access current data will instead use getVerifiedLatestMapState()
* @param {MapTreeState} prev previously held MapTreeState, may be null to skip consistency checks.
* @param {treeSize} treeSize the tree size to retrieve the hash for. Pass HEAD (0) to get the
* latest tree size.
* @param {mapTreeStateSuccessCallback} success called upon success
* @param {failureCallback} failure called on failure
*/
VerifiableMap.prototype.getVerifiedMapState = function (prev, treeSize, success, failure) {
if ((treeSize != 0) && (prev != null) && (prev.getTreeSize() == treeSize)) {
success(prev);
} else {
var map = this;
map.getTreeHead(treeSize, function (mapHead) {
if (prev != null) {
map.getMutationLog().verifyConsistency(prev.getMapHead().getMutationLogTreeHead(), mapHead.getMutationLogTreeHead(), function () {
secondStageMapVerified(map, prev, mapHead, success, failure);
}, failure);
} else {
secondStageMapVerified(map, prev, mapHead, success, failure);
}
}, failure);
}
};
/**
* @private
*/
function secondStageMapVerified(map, prev, mapHead, success, failure) {
var prevThlth = null;
if (prev != null) {
prevThlth = prev.getTreeHeadLogTreeHead();
}
map.getTreeHeadLog().getVerifiedLatestTreeHead(prevThlth, function (thlth) {
map.getTreeHeadLog().verifyInclusion(thlth, mapHead, function () {
success(new MapTreeState(mapHead, thlth));
}, failure);
}, failure);
}
/**
* Private constructor. Use {@link ContinusecClient#getVerifiableLog(String)} to instantiate.
* @constructor
* @classdesc Class to interact with verifiable logs. Instantiate by callling {@link ContinusecClient#getVerifiableLog(String)} method
*/
var VerifiableLog = function (client, path) {
this.client = client;
this.path = path;
};
/**
* Send API call to create this log. This should only be called once, and subsequent
* calls will cause an exception to be generated.
* @param {emptySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.create = function (success, failure) {
this.client.makeRequest("PUT", this.path, null, null, function (data, req) {
success();
}, function (reason) {
failure(reason);
});
};
/**
* Destroy will send an API call to delete this log - this operation removes it permanently,
* and renders the name unusable again within the same account, so please use with caution.
* @param {emptySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.destroy = function (success, failure) {
this.client.makeRequest("DELETE", this.path, null, null, function (data, req) {
success();
}, function (reason) {
failure(reason);
});
};
/**
* Send API call to add an entry to the log. Note the entry is added asynchronously, so while
* the library will return as soon as the server acknowledges receipt of entry, it may not be
* reflected in the tree hash (or inclusion proofs) until the server has sequenced the entry.
*
* @param {VerifiableEntry} value the entry to add, often RawDataEntry, JsonEntry or RedactableJsonEntry.
* @param {addEntryResponseCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.add = function (value, success, failure) {
this.client.makeRequest("POST", this.path + "/entry" + value.getFormat(), value.getDataForUpload(), null, function (data, req) {
var obj = JSON.parse(data);
success(new AddEntryResponse(atob(obj.leaf_hash)));
}, function (reason) {
failure(reason);
});
};
/**
* Get the tree hash for given tree size.
*
* @param {int} treeSize the tree size to retrieve the hash for. Pass HEAD (0) to get the
* latest tree size.
* @param {logTreeHeadSuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getTreeHead = function (treeSize, success, failure) {
this.client.makeRequest("GET", this.path + "/tree/" + treeSize, null, null, function (data, req) {
var obj = JSON.parse(data);
success(new LogTreeHead(Number(obj.tree_size), obj.tree_hash === null ? null : atob(obj.tree_hash)));
}, function (reason) {
failure(reason);
});
};
/**
* Get the entry at the specified index.
*
* @param {int} idx the index to retrieve (starts at zero).
* @param {VerifiableEntryFactory} factory the type of entry to return, usually one of RawDataEntryFactory, JsonEntryFactory, RedactedJsonEntryFactory.
* @param {verifiableEntrySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getEntry = function (idx, factory, success, failure) {
this.client.makeRequest("GET", this.path + "/entry/" + idx + factory.getFormat(), null, null, function (data, req) {
success(factory.createFromBytes(data, idx));
}, function (reason) {
failure(reason);
});
};
/**
* Returns an iterator to efficiently fetch a contiguous set of entries. If for any
* reason not all entries are returned, the iterator will terminate early.
*
* @param {int} beginIdx the first entry to return
* @param {int} endIdx the last entry to return
* @param {VerifiableEntryFactory} factory the type of entry to return, usually one of RawDataEntryFactory, JsonEntryFactory, RedactedJsonEntryFactory.
* @param {verifiableEntryIndexSuccessCallback} each called for each entry
* @param {emptySuccessCallback} success called on success (after all values processed).
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getEntries = function (startIdx, endIdx, factory, each, success, failure) {
this.client.makeRequest("GET", this.path + "/entries/" + startIdx + "-" + endIdx + factory.getFormat(), null, null, function (data, req) {
try {
var obj = JSON.parse(data);
for (var i = 0; i < obj.entries.length; i++) {
each(startIdx + i, factory.createFromBytes(atob(obj.entries[i].leaf_data)));
}
} catch (err) {
failure(err);
return
}
success();
}, function (reason) {
failure(reason);
});
};
/**
* Utility method for auditors that wish to audit the full content of a log, as well as the log operation.
* This method will retrieve all entries in batch from the log, and ensure that the root hash in head can be confirmed to accurately represent the contents
* of all of the log entries. If prev is not null, then additionally it is proven that the root hash in head is consistent with the root hash in prev.
* @param {LogTreeHead} prev a previous LogTreeHead representing the set of entries that have been previously audited. To indicate this is has not previously been audited, pass null,
* @param {LogTreeHead} head the LogTreeHead up to which we wish to audit the log. Upon successful completion the caller should persist this for a future iteration.
* @param {VerifiableEntryFactory} factory the type of entry to return, usually one of RawDataEntryFactory, JsonEntryFactory, RedactedJsonEntryFactory.
* @param {verifiableEntryIndexSuccessCallback} each which is called sequentially for each log entry as it is encountered.
* @param {emptySuccessCallback} success called on success (after all values processed).
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.verifyEntries = function (prev, head, factory, each, success, failure) {
if ((prev == null) || (prev.getTreeSize() < head.getTreeSize())) {
var log = this;
var stack = [];
if ((prev != null) && (prev.getTreeSize() > 0)) {
this.getInclusionProofByIndex(prev.getTreeSize()+1, prev.getTreeSize(), function (proof) {
var firstHash = null;
for (var i = 0; i < proof.getAuditPath().length; i++) {
if (firstHash == null) {
firstHash = proof.getAuditPath()[i];
} else {
firstHash = nodeMerkleTreeHash(proof.getAuditPath()[i], firstHash);
}
}
if (firstHash != prev.getRootHash()) {
failure(CONTINUSEC_VERIFICATION_ERROR);
} else {
for (var i = proof.getAuditPath().length - 1; i >= 0; i--) {
stack.push(proof.getAuditPath()[i]);
}
secondStageVerifyEntries(stack, log, prev, head, factory, each, success, failure);
}
}, failure);
} else {
secondStageVerifyEntries(stack, log, prev, head, factory, each, success, failure);
}
} else {
success();
}
};
/**
* @private
*/
function secondStageVerifyEntries(stack, log, prev, head, factory, each, success, failure) {
var parIdx = 0;
if (prev != null) {
parIdx = prev.getTreeSize();
}
log.getEntries(parIdx, head.getTreeSize(), factory, function (idx, entry) {
each(idx, entry);
stack.push(entry.getLeafHash());
for (var z = idx; (z & 1) == 1; z >>= 1) {
var right = stack.pop();
var left = stack.pop();
stack.push(nodeMerkleTreeHash(left, right));
}
parIdx += 1;
}, function () {
if (parIdx != head.getTreeSize()) {
failure(CONTINUSEC_NOT_ALL_ENTRIES_RETURNED_ERROR);
} else {
var headHash = stack.pop();
while (stack.length > 0) {
headHash = nodeMerkleTreeHash(stack.pop(), headHash);
}
if (headHash != head.getRootHash()) {
failure(CONTINUSEC_VERIFICATION_ERROR);
} else {
success();
}
}
}, failure);
}
/**
* Get an inclusion proof for a given item for a specific tree size. Most clients will commonly use {@link #verifyInclusion(LogTreeHead,MerkleTreeLeaf)} instead.
* @param {int} treeSize the tree size for which the inclusion proof should be returned. This is usually as returned by {@link #getTreeHead(int)}.getTreeSize().
* @param {MerkleTreeLeaf} leaf the entry for which the inclusion proof should be returned. Note that AddEntryResponse and RawDataEntry/JsonEntry/RedactedJsonEntry each implement MerkleTreeLeaf.
* @param {logInclusionProofSuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getInclusionProof = function (treeSize, leaf, success, failure) {
var lh = leaf.getLeafHash();
this.client.makeRequest("GET", this.path + "/tree/" + treeSize + "/inclusion/h/" + hexString(lh), null, null, function (data, req) {
var obj = JSON.parse(data);
var auditPath = [];
for (var i = 0; i < obj.proof.length; i++) {
auditPath.push(atob(obj.proof[i]));
}
success(new LogInclusionProof(lh, Number(obj.tree_size), Number(obj.leaf_index), auditPath));
}, function (reason) {
failure(reason);
});
};
/**
* Get an inclusion proof for a specified tree size and leaf index. This is not used by typical clients,
* however it can be useful for audit operations and debugging tools. Typical clients will use {@link #verifyInclusion(LogTreeHead,MerkleTreeLeaf)}.
* @param {int} treeSize the tree size on which to base the proof.
* @param {int} leafIndex the leaf index for which to retrieve the inclusion proof.
* @param {logInclusionProofSuccessCallback} success called on success (note the proof is only partially filled in as it does not include the MerkleTreeLeaf hash for the item).
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getInclusionProofByIndex = function (treeSize, leafIndex, success, failure) {
this.client.makeRequest("GET", this.path + "/tree/" + treeSize + "/inclusion/" + leafIndex, null, null, function (data, req) {
var obj = JSON.parse(data);
var auditPath = [];
for (var i = 0; i < obj.proof.length; i++) {
auditPath.push(atob(obj.proof[i]));
}
success(new LogInclusionProof(null, Number(obj.tree_size), Number(obj.leaf_index), auditPath));
}, function (reason) {
failure(reason);
});
};
/**
* Get an inclusion proof for a given item and verify it.
* @param {LogTreeHead} treeHead the tree head for which the inclusion proof should be returned. This is usually as returned by {@link #getTreeHead(int)}.
* @param {MerkleTreeLeaf} leaf the entry for which the inclusion proof should be returned. Note that AddEntryResponse and RawDataEntry/JsonEntry/RedactedJsonEntry each implement MerkleTreeLeaf.
* @param {emptySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.verifyInclusion = function (head, leaf, success, failure) {
this.getInclusionProof(head.getTreeSize(), leaf, function (proof) {
try {
proof.verify(head);
} catch (err) {
failure(err);
return;
}
success();
}, function (reason) {
failure(reason);
});
}
/**
* ConsistencyProof returns an audit path which contains the set of Merkle Subtree hashes
* that demonstrate how the root hash is calculated for both the first and second tree sizes.
* @param {int} firstSize the size of the first tree.
* @param {int} secondSize the size of the second tree.
* @param {logConsistencyProofSuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getConsistencyProof = function (firstSize, secondSize, success, failure) {
this.client.makeRequest("GET", this.path + "/tree/" + secondSize + "/consistency/" + firstSize, null, null, function (data, req) {
var obj = JSON.parse(data);
var auditPath = [];
for (var i = 0; i < obj.proof.length; i++) {
auditPath.push(atob(obj.proof[i]));
}
success(new LogConsistencyProof(firstSize, secondSize, auditPath));
}, function (reason) {
failure(reason);
});
};
/**
* verifyConsistency takes two tree heads, retrieves a consistency proof and then verifies it.
* The two tree heads may be in either order (even equal), but both must be greater than zero and non-nil.
* @param {LogTreeHead} a one log tree head
* @param {LogTreeHead} b another log tree head
* @param {emptySuccessCallback} success called on success
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.verifyConsistency = function (a, b, success, failure) {
if (a.getTreeSize() <= 0) {
failure(CONTINUSEC_VERIFICATION_ERROR);
return;
}
if (b.getTreeSize() <= 0) {
failure(CONTINUSEC_VERIFICATION_ERROR);
return;
}
if (a.getTreeSize() == b.getTreeSize()) {
if (a.getRootHash() != b.getRootHash()) {
failure(CONTINUSEC_VERIFICATION_ERROR);
return;
}
success();
return;
}
if (a.getTreeSize() > b.getTreeSize()) {
var c = a;
a = b;
b = c;
}
this.getConsistencyProof(a.getTreeSize(), b.getTreeSize(), function (proof) {
try {
proof.verify(a, b);
} catch (err) {
failure(err);
return;
}
success();
}, function (reason) {
failure(reason);
});
}
/**
* getVerifiedLatestTreeHead calls getVerifiedTreeHead() with HEAD to fetch the latest tree head,
* and additionally verifies that it is newer than the previously passed tree head.
* For first use, pass null to skip consistency checking.
* @param {LogTreeHead} prev a previously persisted log tree head
* @param {logTreeHeadSuccessCallback} success called on success, with a new LogTreeHead which has been verified to be consistent with the past tree head, or if no newer one present, the same value as passed in.
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getVerifiedLatestTreeHead = function (prev, success, failure) {
this.getVerifiedTreeHead(prev, 0, function (head) {
if (prev != null) {
if (head.getTreeSize() <= prev.getTreeSize()) {
head = prev;
}
}
success(head);
}, failure);
}
/**
* getVerifiedTreeHead is a utility method to fetch a LogTreeHead and verifies that it is consistent with
* a tree head earlier fetched and persisted. For first use, pass null for prev, which will
* bypass consistency proof checking. Tree size may be older or newer than the previous head value.
* @param {LogTreeHead} prev a previously persisted log tree head
* @param {int} treeSize the tree size to fetch
* @param {logTreeHeadSuccessCallback} success called on success, with a LogTreeHead which has been verified to be consistent with the past tree head and matches the size specified.
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.getVerifiedTreeHead = function (prev, treeSize, success, failure) {
if ((treeSize != 0) && (prev != null) && (prev.getTreeSize() == treeSize)) {
success(prev);
} else {
var log = this;
this.getTreeHead(treeSize, function (head) {
if (prev == null) {
success(head);
} else {
log.verifyConsistency(prev, head, function () {
success(head);
}, failure);
}
}, failure);
}
}
/**
* verifySuppliedInclusionProof is a utility method that fetches any required tree heads that are needed
* to verify a supplied log inclusion proof. Additionally it will ensure that any fetched tree heads are consistent
* with any prior supplied LogTreeHead. For first use, pass null for prev, which will
* bypass consistency proof checking.
* @param {LogTreeHead} prev a previously persisted log tree head, or null
* @param {LogInclusionProof} proof an inclusion proof that may be for a different tree size than prev.getTreeSize()
* @param {logTreeHeadSuccessCallback} success called on success, with the verified (for consistency) LogTreeHead that was used for successful verification (of inclusion) of the supplied proof. This may be older than the LogTreeHead passed in.
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.verifySuppliedInclusionProof = function (prev, proof, success, failure) {
this.getVerifiedTreeHead(prev, proof.getTreeSize(), function (head) {
try {
proof.verify(head);
success(head);
} catch (err) {
failure(err);
}
}, failure);
};
/**
* Block until the log is able to produce a LogTreeHead that includes the specified MerkleTreeLeaf.
* This polls {@link #getTreeHead(int)} and {@link #verifyInclusion(LogTreeHead, MerkleTreeLeaf)} until
* such time as a new tree hash is produced that includes the given MerkleTreeLeaf. Exponential back-off
* is used when no tree hash is available. This is intended for test use - the returned tree head is not verified for consistency.
* @param {MerkleTreeLeaf} leaf the entry for which we should block until present. Note that AddEntryResponse and RawDataEntry/JsonEntry/RedactedJsonEntry each implement MerkleTreeLeaf.
* @param {logTreeHeadSuccessCallback} success called on success, with the LogTreeHead that this leaf is included in.
* @param {failureCallback} failure called on failure
*/
VerifiableLog.prototype.blockUntilPresent = function (leaf, success, failure) {
doBlockRound(this, -1, 0, leaf, success, failure);
}
/**
* @private
*/
function doBlockRound(log, lastHead, secsToSleep, leaf, success, failure) {
log.getTreeHead(0, function (lth) {
if (lth.getTreeSize() > lastHead) {
log.verifyInclusion(lth, leaf, function () {
success(lth);
}, function (reason) {
if (reason == CONTINUSEC_INVALID_RANGE_ERROR) {
secsToSleep = 1;
setTimeout(function () { doBlockRound(log, lth.getTreeSize(), secsToSleep, leaf, success, failure); }, secsToSleep * 1000);
} else {
failure(reason);
}
});
} else {
secsToSleep *= 2
setTimeout(function () { doBlockRound(log, lastHead, secsToSleep, leaf, success, failure); }, secsToSleep * 1000);
}
}, failure);
}
/**
* Package private constructor. Use VerifiableLog.add(UploadableEntry) to instantiate.