@otskit/client 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -392,16 +392,1453 @@ var ResilientNetworkLayer = class {
392
392
  }
393
393
  };
394
394
 
395
- // src/core/orchestration.ts
396
- import {
397
- DetachedTimestampFile,
398
- OpSHA256,
399
- OpAppend,
400
- makeMerkleTree
401
- } from "@otskit/core";
395
+ // node_modules/.pnpm/@otskit+core@0.1.0/node_modules/@otskit/core/dist/index.js
396
+ var DeserializationError = class extends Error {
397
+ constructor(message) {
398
+ super(message);
399
+ this.name = new.target.name;
400
+ Object.setPrototypeOf(this, new.target.prototype);
401
+ }
402
+ };
403
+ var BadMagicError = class extends DeserializationError {
404
+ };
405
+ var TruncatedStreamError = class extends DeserializationError {
406
+ };
407
+ var OversizedDataError = class extends DeserializationError {
408
+ };
409
+ var VaruintOverflowError = class extends DeserializationError {
410
+ };
411
+ var TrailingGarbageError = class extends DeserializationError {
412
+ };
413
+ var UnknownOperationError = class extends DeserializationError {
414
+ };
415
+ var OpExecutionError = class extends Error {
416
+ constructor(message) {
417
+ super(message);
418
+ this.name = new.target.name;
419
+ Object.setPrototypeOf(this, new.target.prototype);
420
+ }
421
+ };
422
+ var MessageTooLongError = class extends OpExecutionError {
423
+ };
424
+ var ResultTooLongError = class extends OpExecutionError {
425
+ };
426
+ var InvalidUriError = class extends DeserializationError {
427
+ };
428
+ var VerificationError = class extends Error {
429
+ constructor(message) {
430
+ super(message);
431
+ this.name = new.target.name;
432
+ Object.setPrototypeOf(this, new.target.prototype);
433
+ }
434
+ };
435
+ var EmptyTimestampError = class extends Error {
436
+ constructor(message) {
437
+ super(message);
438
+ this.name = new.target.name;
439
+ Object.setPrototypeOf(this, new.target.prototype);
440
+ }
441
+ };
442
+ var MergeError = class extends Error {
443
+ constructor(message) {
444
+ super(message);
445
+ this.name = new.target.name;
446
+ Object.setPrototypeOf(this, new.target.prototype);
447
+ }
448
+ };
449
+ var EmptyMerkleTreeError = class extends Error {
450
+ constructor(message) {
451
+ super(message);
452
+ this.name = new.target.name;
453
+ Object.setPrototypeOf(this, new.target.prototype);
454
+ }
455
+ };
456
+ var UnsupportedVersionError = class extends DeserializationError {
457
+ };
458
+ var HEX_RE = /^[0-9a-fA-F]*$/;
459
+ function hexToBytes(hex) {
460
+ if (hex.length % 2 !== 0) {
461
+ throw new Error(`hex string must have even length; got ${hex.length}`);
462
+ }
463
+ if (!HEX_RE.test(hex)) {
464
+ throw new Error("hex string contains non-hex characters");
465
+ }
466
+ const out = new Uint8Array(hex.length / 2);
467
+ for (let i = 0; i < out.length; i++) {
468
+ out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
469
+ }
470
+ return out;
471
+ }
472
+ var HEX_TABLE = Array.from({ length: 256 }, (_, b) => b.toString(16).padStart(2, "0"));
473
+ function bytesToHex(bytes) {
474
+ let s = "";
475
+ for (let i = 0; i < bytes.length; i++) {
476
+ s += HEX_TABLE[bytes[i]];
477
+ }
478
+ return s;
479
+ }
480
+ var encoder = new TextEncoder();
481
+ var decoder = new TextDecoder("utf-8", { fatal: true });
482
+ function textToBytes(text) {
483
+ return encoder.encode(text);
484
+ }
485
+ function bytesEqual(a, b) {
486
+ if (a.length !== b.length) return false;
487
+ for (let i = 0; i < a.length; i++) {
488
+ if (a[i] !== b[i]) return false;
489
+ }
490
+ return true;
491
+ }
492
+ function compareBytes(a, b) {
493
+ const min = Math.min(a.length, b.length);
494
+ for (let i = 0; i < min; i++) {
495
+ const d = a[i] - b[i];
496
+ if (d !== 0) return d;
497
+ }
498
+ return a.length - b.length;
499
+ }
500
+ var StreamDeserializationContext = class {
501
+ #buffer;
502
+ #counter = 0;
503
+ constructor(stream) {
504
+ if (!(stream instanceof Uint8Array)) {
505
+ throw new TypeError("StreamDeserializationContext expects a Uint8Array");
506
+ }
507
+ this.#buffer = stream;
508
+ }
509
+ get counter() {
510
+ return this.#counter;
511
+ }
512
+ /** Lee `length` bytes. Lanza si el stream no tiene suficientes. */
513
+ read(length) {
514
+ if (length < 0) throw new RangeError("read length must be >= 0");
515
+ if (this.#counter + length > this.#buffer.length) {
516
+ throw new TruncatedStreamError(
517
+ `attempted to read ${length} bytes at offset ${this.#counter}, only ${this.#buffer.length - this.#counter} available`
518
+ );
519
+ }
520
+ const slice = this.#buffer.subarray(this.#counter, this.#counter + length);
521
+ this.#counter += length;
522
+ return slice;
523
+ }
524
+ /** Lee un único byte. */
525
+ readByte() {
526
+ return this.read(1)[0];
527
+ }
528
+ /** Varuint LEB128: 7 bits por byte, bit 7 = continuación. */
529
+ readVaruint() {
530
+ let value = 0;
531
+ let shift = 0;
532
+ let byte;
533
+ do {
534
+ if (shift > 56) {
535
+ throw new VaruintOverflowError("varuint exceeds Number.MAX_SAFE_INTEGER");
536
+ }
537
+ byte = this.readByte();
538
+ value += (byte & 127) * 2 ** shift;
539
+ if (!Number.isSafeInteger(value)) {
540
+ throw new VaruintOverflowError("varuint exceeds Number.MAX_SAFE_INTEGER");
541
+ }
542
+ shift += 7;
543
+ } while (byte & 128);
544
+ return value;
545
+ }
546
+ /** Lee un bloque varbytes. `maxLen` es obligatorio (defensa DoS). */
547
+ readVarbytes(maxLen, minLen = 0) {
548
+ const length = this.readVaruint();
549
+ if (length > maxLen) {
550
+ throw new OversizedDataError(`varbytes length ${length} exceeds maxLen ${maxLen}`);
551
+ }
552
+ if (length < minLen) {
553
+ throw new OversizedDataError(`varbytes length ${length} below minLen ${minLen}`);
554
+ }
555
+ return this.read(length);
556
+ }
557
+ /** Verifica el número mágico de cabecera. */
558
+ assertMagic(expectedMagic) {
559
+ const actual = this.read(expectedMagic.length);
560
+ if (!bytesEqual(expectedMagic, actual)) {
561
+ throw new BadMagicError("header magic mismatch");
562
+ }
563
+ }
564
+ /** Exige que no queden bytes sin consumir. */
565
+ assertEof() {
566
+ if (this.#counter < this.#buffer.length) {
567
+ throw new TrailingGarbageError("trailing garbage after end of deserialized data");
568
+ }
569
+ }
570
+ };
571
+ var StreamSerializationContext = class {
572
+ #buffer = new Uint8Array(4096);
573
+ #length = 0;
574
+ get length() {
575
+ return this.#length;
576
+ }
577
+ getOutput() {
578
+ return this.#buffer.slice(0, this.#length);
579
+ }
580
+ writeByte(value) {
581
+ if (!Number.isInteger(value) || value < 0 || value > 255) {
582
+ throw new RangeError(`writeByte expects a byte 0..255; got ${value}`);
583
+ }
584
+ if (this.#length >= this.#buffer.length) {
585
+ const grown = new Uint8Array(this.#buffer.length * 2);
586
+ grown.set(this.#buffer, 0);
587
+ this.#buffer = grown;
588
+ }
589
+ this.#buffer[this.#length] = value;
590
+ this.#length += 1;
591
+ }
592
+ writeBytes(value) {
593
+ for (let i = 0; i < value.length; i++) {
594
+ this.writeByte(value[i]);
595
+ }
596
+ }
597
+ /** Codifica un varuint LEB128. */
598
+ writeVaruint(value) {
599
+ if (!Number.isSafeInteger(value) || value < 0) {
600
+ throw new RangeError(`writeVaruint expects a safe non-negative integer; got ${value}`);
601
+ }
602
+ do {
603
+ let byte = value % 128;
604
+ value = Math.floor(value / 128);
605
+ if (value > 0) byte |= 128;
606
+ this.writeByte(byte);
607
+ } while (value > 0);
608
+ }
609
+ writeVarbytes(value) {
610
+ this.writeVaruint(value.length);
611
+ this.writeBytes(value);
612
+ }
613
+ };
614
+ var rotl = (x, n) => x << n | x >>> 32 - n;
615
+ function sha1(msg) {
616
+ let h0 = 1732584193, h1 = 4023233417, h2 = 2562383102, h3 = 271733878, h4 = 3285377520;
617
+ const bitLen = msg.length * 8;
618
+ const withOne = msg.length + 1;
619
+ const padded = new Uint8Array(Math.ceil((withOne + 8) / 64) * 64);
620
+ padded.set(msg, 0);
621
+ padded[msg.length] = 128;
622
+ const dv = new DataView(padded.buffer);
623
+ dv.setUint32(padded.length - 4, bitLen >>> 0, false);
624
+ dv.setUint32(padded.length - 8, Math.floor(bitLen / 2 ** 32), false);
625
+ const w = new Uint32Array(80);
626
+ for (let off = 0; off < padded.length; off += 64) {
627
+ for (let i = 0; i < 16; i++) w[i] = dv.getUint32(off + i * 4, false);
628
+ for (let i = 16; i < 80; i++) w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
629
+ let a = h0, b = h1, c = h2, d = h3, e = h4;
630
+ for (let i = 0; i < 80; i++) {
631
+ let f2, k;
632
+ if (i < 20) {
633
+ f2 = b & c | ~b & d;
634
+ k = 1518500249;
635
+ } else if (i < 40) {
636
+ f2 = b ^ c ^ d;
637
+ k = 1859775393;
638
+ } else if (i < 60) {
639
+ f2 = b & c | b & d | c & d;
640
+ k = 2400959708;
641
+ } else {
642
+ f2 = b ^ c ^ d;
643
+ k = 3395469782;
644
+ }
645
+ const t = rotl(a, 5) + f2 + e + k + w[i] | 0;
646
+ e = d;
647
+ d = c;
648
+ c = rotl(b, 30);
649
+ b = a;
650
+ a = t;
651
+ }
652
+ h0 = h0 + a | 0;
653
+ h1 = h1 + b | 0;
654
+ h2 = h2 + c | 0;
655
+ h3 = h3 + d | 0;
656
+ h4 = h4 + e | 0;
657
+ }
658
+ const out = new Uint8Array(20);
659
+ const odv = new DataView(out.buffer);
660
+ odv.setUint32(0, h0, false);
661
+ odv.setUint32(4, h1, false);
662
+ odv.setUint32(8, h2, false);
663
+ odv.setUint32(12, h3, false);
664
+ odv.setUint32(16, h4, false);
665
+ return out;
666
+ }
667
+ var K = new Uint32Array([
668
+ 1116352408,
669
+ 1899447441,
670
+ 3049323471,
671
+ 3921009573,
672
+ 961987163,
673
+ 1508970993,
674
+ 2453635748,
675
+ 2870763221,
676
+ 3624381080,
677
+ 310598401,
678
+ 607225278,
679
+ 1426881987,
680
+ 1925078388,
681
+ 2162078206,
682
+ 2614888103,
683
+ 3248222580,
684
+ 3835390401,
685
+ 4022224774,
686
+ 264347078,
687
+ 604807628,
688
+ 770255983,
689
+ 1249150122,
690
+ 1555081692,
691
+ 1996064986,
692
+ 2554220882,
693
+ 2821834349,
694
+ 2952996808,
695
+ 3210313671,
696
+ 3336571891,
697
+ 3584528711,
698
+ 113926993,
699
+ 338241895,
700
+ 666307205,
701
+ 773529912,
702
+ 1294757372,
703
+ 1396182291,
704
+ 1695183700,
705
+ 1986661051,
706
+ 2177026350,
707
+ 2456956037,
708
+ 2730485921,
709
+ 2820302411,
710
+ 3259730800,
711
+ 3345764771,
712
+ 3516065817,
713
+ 3600352804,
714
+ 4094571909,
715
+ 275423344,
716
+ 430227734,
717
+ 506948616,
718
+ 659060556,
719
+ 883997877,
720
+ 958139571,
721
+ 1322822218,
722
+ 1537002063,
723
+ 1747873779,
724
+ 1955562222,
725
+ 2024104815,
726
+ 2227730452,
727
+ 2361852424,
728
+ 2428436474,
729
+ 2756734187,
730
+ 3204031479,
731
+ 3329325298
732
+ ]);
733
+ var rotr = (x, n) => x >>> n | x << 32 - n;
734
+ function sha256(msg) {
735
+ const h = new Uint32Array([
736
+ 1779033703,
737
+ 3144134277,
738
+ 1013904242,
739
+ 2773480762,
740
+ 1359893119,
741
+ 2600822924,
742
+ 528734635,
743
+ 1541459225
744
+ ]);
745
+ const bitLen = msg.length * 8;
746
+ const withOne = msg.length + 1;
747
+ const padded = new Uint8Array(Math.ceil((withOne + 8) / 64) * 64);
748
+ padded.set(msg, 0);
749
+ padded[msg.length] = 128;
750
+ const dv = new DataView(padded.buffer);
751
+ dv.setUint32(padded.length - 4, bitLen >>> 0, false);
752
+ dv.setUint32(padded.length - 8, Math.floor(bitLen / 2 ** 32), false);
753
+ const w = new Uint32Array(64);
754
+ for (let off = 0; off < padded.length; off += 64) {
755
+ for (let i = 0; i < 16; i++) w[i] = dv.getUint32(off + i * 4, false);
756
+ for (let i = 16; i < 64; i++) {
757
+ const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ w[i - 15] >>> 3;
758
+ const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ w[i - 2] >>> 10;
759
+ w[i] = w[i - 16] + s0 + w[i - 7] + s1 | 0;
760
+ }
761
+ let [a, b, c, d, e, f2, g, hh] = h;
762
+ for (let i = 0; i < 64; i++) {
763
+ const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
764
+ const ch = e & f2 ^ ~e & g;
765
+ const t1 = hh + S1 + ch + K[i] + w[i] | 0;
766
+ const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
767
+ const maj = a & b ^ a & c ^ b & c;
768
+ const t2 = S0 + maj | 0;
769
+ hh = g;
770
+ g = f2;
771
+ f2 = e;
772
+ e = d + t1 | 0;
773
+ d = c;
774
+ c = b;
775
+ b = a;
776
+ a = t1 + t2 | 0;
777
+ }
778
+ h[0] = h[0] + a | 0;
779
+ h[1] = h[1] + b | 0;
780
+ h[2] = h[2] + c | 0;
781
+ h[3] = h[3] + d | 0;
782
+ h[4] = h[4] + e | 0;
783
+ h[5] = h[5] + f2 | 0;
784
+ h[6] = h[6] + g | 0;
785
+ h[7] = h[7] + hh | 0;
786
+ }
787
+ const out = new Uint8Array(32);
788
+ const odv = new DataView(out.buffer);
789
+ for (let i = 0; i < 8; i++) odv.setUint32(i * 4, h[i], false);
790
+ return out;
791
+ }
792
+ var rol = (x, n) => x << n | x >>> 32 - n;
793
+ var ZL = [
794
+ 0,
795
+ 1,
796
+ 2,
797
+ 3,
798
+ 4,
799
+ 5,
800
+ 6,
801
+ 7,
802
+ 8,
803
+ 9,
804
+ 10,
805
+ 11,
806
+ 12,
807
+ 13,
808
+ 14,
809
+ 15,
810
+ 7,
811
+ 4,
812
+ 13,
813
+ 1,
814
+ 10,
815
+ 6,
816
+ 15,
817
+ 3,
818
+ 12,
819
+ 0,
820
+ 9,
821
+ 5,
822
+ 2,
823
+ 14,
824
+ 11,
825
+ 8,
826
+ 3,
827
+ 10,
828
+ 14,
829
+ 4,
830
+ 9,
831
+ 15,
832
+ 8,
833
+ 1,
834
+ 2,
835
+ 7,
836
+ 0,
837
+ 6,
838
+ 13,
839
+ 11,
840
+ 5,
841
+ 12,
842
+ 1,
843
+ 9,
844
+ 11,
845
+ 10,
846
+ 0,
847
+ 8,
848
+ 12,
849
+ 4,
850
+ 13,
851
+ 3,
852
+ 7,
853
+ 15,
854
+ 14,
855
+ 5,
856
+ 6,
857
+ 2,
858
+ 4,
859
+ 0,
860
+ 5,
861
+ 9,
862
+ 7,
863
+ 12,
864
+ 2,
865
+ 10,
866
+ 14,
867
+ 1,
868
+ 3,
869
+ 8,
870
+ 11,
871
+ 6,
872
+ 15,
873
+ 13
874
+ ];
875
+ var ZR = [
876
+ 5,
877
+ 14,
878
+ 7,
879
+ 0,
880
+ 9,
881
+ 2,
882
+ 11,
883
+ 4,
884
+ 13,
885
+ 6,
886
+ 15,
887
+ 8,
888
+ 1,
889
+ 10,
890
+ 3,
891
+ 12,
892
+ 6,
893
+ 11,
894
+ 3,
895
+ 7,
896
+ 0,
897
+ 13,
898
+ 5,
899
+ 10,
900
+ 14,
901
+ 15,
902
+ 8,
903
+ 12,
904
+ 4,
905
+ 9,
906
+ 1,
907
+ 2,
908
+ 15,
909
+ 5,
910
+ 1,
911
+ 3,
912
+ 7,
913
+ 14,
914
+ 6,
915
+ 9,
916
+ 11,
917
+ 8,
918
+ 12,
919
+ 2,
920
+ 10,
921
+ 0,
922
+ 4,
923
+ 13,
924
+ 8,
925
+ 6,
926
+ 4,
927
+ 1,
928
+ 3,
929
+ 11,
930
+ 15,
931
+ 0,
932
+ 5,
933
+ 12,
934
+ 2,
935
+ 13,
936
+ 9,
937
+ 7,
938
+ 10,
939
+ 14,
940
+ 12,
941
+ 15,
942
+ 10,
943
+ 4,
944
+ 1,
945
+ 5,
946
+ 8,
947
+ 7,
948
+ 6,
949
+ 2,
950
+ 13,
951
+ 14,
952
+ 0,
953
+ 3,
954
+ 9,
955
+ 11
956
+ ];
957
+ var SL = [
958
+ 11,
959
+ 14,
960
+ 15,
961
+ 12,
962
+ 5,
963
+ 8,
964
+ 7,
965
+ 9,
966
+ 11,
967
+ 13,
968
+ 14,
969
+ 15,
970
+ 6,
971
+ 7,
972
+ 9,
973
+ 8,
974
+ 7,
975
+ 6,
976
+ 8,
977
+ 13,
978
+ 11,
979
+ 9,
980
+ 7,
981
+ 15,
982
+ 7,
983
+ 12,
984
+ 15,
985
+ 9,
986
+ 11,
987
+ 7,
988
+ 13,
989
+ 12,
990
+ 11,
991
+ 13,
992
+ 6,
993
+ 7,
994
+ 14,
995
+ 9,
996
+ 13,
997
+ 15,
998
+ 14,
999
+ 8,
1000
+ 13,
1001
+ 6,
1002
+ 5,
1003
+ 12,
1004
+ 7,
1005
+ 5,
1006
+ 11,
1007
+ 12,
1008
+ 14,
1009
+ 15,
1010
+ 14,
1011
+ 15,
1012
+ 9,
1013
+ 8,
1014
+ 9,
1015
+ 14,
1016
+ 5,
1017
+ 6,
1018
+ 8,
1019
+ 6,
1020
+ 5,
1021
+ 12,
1022
+ 9,
1023
+ 15,
1024
+ 5,
1025
+ 11,
1026
+ 6,
1027
+ 8,
1028
+ 13,
1029
+ 12,
1030
+ 5,
1031
+ 12,
1032
+ 13,
1033
+ 14,
1034
+ 11,
1035
+ 8,
1036
+ 5,
1037
+ 6
1038
+ ];
1039
+ var SR = [
1040
+ 8,
1041
+ 9,
1042
+ 9,
1043
+ 11,
1044
+ 13,
1045
+ 15,
1046
+ 15,
1047
+ 5,
1048
+ 7,
1049
+ 7,
1050
+ 8,
1051
+ 11,
1052
+ 14,
1053
+ 14,
1054
+ 12,
1055
+ 6,
1056
+ 9,
1057
+ 13,
1058
+ 15,
1059
+ 7,
1060
+ 12,
1061
+ 8,
1062
+ 9,
1063
+ 11,
1064
+ 7,
1065
+ 7,
1066
+ 12,
1067
+ 7,
1068
+ 6,
1069
+ 15,
1070
+ 13,
1071
+ 11,
1072
+ 9,
1073
+ 7,
1074
+ 15,
1075
+ 11,
1076
+ 8,
1077
+ 6,
1078
+ 6,
1079
+ 14,
1080
+ 12,
1081
+ 13,
1082
+ 5,
1083
+ 14,
1084
+ 13,
1085
+ 13,
1086
+ 7,
1087
+ 5,
1088
+ 15,
1089
+ 5,
1090
+ 8,
1091
+ 11,
1092
+ 14,
1093
+ 14,
1094
+ 6,
1095
+ 14,
1096
+ 6,
1097
+ 9,
1098
+ 12,
1099
+ 9,
1100
+ 12,
1101
+ 5,
1102
+ 15,
1103
+ 8,
1104
+ 8,
1105
+ 5,
1106
+ 12,
1107
+ 9,
1108
+ 12,
1109
+ 5,
1110
+ 14,
1111
+ 6,
1112
+ 8,
1113
+ 13,
1114
+ 6,
1115
+ 5,
1116
+ 15,
1117
+ 13,
1118
+ 11,
1119
+ 11
1120
+ ];
1121
+ var KL = [0, 1518500249, 1859775393, 2400959708, 2840853838];
1122
+ var KR = [1352829926, 1548603684, 1836072691, 2053994217, 0];
1123
+ var f = (j, x, y, z) => {
1124
+ if (j < 16) return x ^ y ^ z;
1125
+ if (j < 32) return x & y | ~x & z;
1126
+ if (j < 48) return (x | ~y) ^ z;
1127
+ if (j < 64) return x & z | y & ~z;
1128
+ return x ^ (y | ~z);
1129
+ };
1130
+ function ripemd160(msg) {
1131
+ let h0 = 1732584193, h1 = 4023233417, h2 = 2562383102, h3 = 271733878, h4 = 3285377520;
1132
+ const bitLen = msg.length * 8;
1133
+ const withOne = msg.length + 1;
1134
+ const padded = new Uint8Array(Math.ceil((withOne + 8) / 64) * 64);
1135
+ padded.set(msg, 0);
1136
+ padded[msg.length] = 128;
1137
+ const dv = new DataView(padded.buffer);
1138
+ dv.setUint32(padded.length - 8, bitLen >>> 0, true);
1139
+ dv.setUint32(padded.length - 4, Math.floor(bitLen / 2 ** 32), true);
1140
+ const x = new Uint32Array(16);
1141
+ for (let off = 0; off < padded.length; off += 64) {
1142
+ for (let i = 0; i < 16; i++) x[i] = dv.getUint32(off + i * 4, true);
1143
+ let al = h0, bl = h1, cl = h2, dl = h3, el = h4;
1144
+ let ar = h0, br = h1, cr = h2, dr = h3, er = h4;
1145
+ for (let j = 0; j < 80; j++) {
1146
+ const round = Math.floor(j / 16);
1147
+ let t2 = al + f(j, bl, cl, dl) + x[ZL[j]] + KL[round] | 0;
1148
+ t2 = rol(t2, SL[j]) + el | 0;
1149
+ al = el;
1150
+ el = dl;
1151
+ dl = rol(cl, 10);
1152
+ cl = bl;
1153
+ bl = t2;
1154
+ t2 = ar + f(79 - j, br, cr, dr) + x[ZR[j]] + KR[round] | 0;
1155
+ t2 = rol(t2, SR[j]) + er | 0;
1156
+ ar = er;
1157
+ er = dr;
1158
+ dr = rol(cr, 10);
1159
+ cr = br;
1160
+ br = t2;
1161
+ }
1162
+ const t = h1 + cl + dr | 0;
1163
+ h1 = h2 + dl + er | 0;
1164
+ h2 = h3 + el + ar | 0;
1165
+ h3 = h4 + al + br | 0;
1166
+ h4 = h0 + bl + cr | 0;
1167
+ h0 = t;
1168
+ }
1169
+ const out = new Uint8Array(20);
1170
+ const odv = new DataView(out.buffer);
1171
+ odv.setUint32(0, h0, true);
1172
+ odv.setUint32(4, h1, true);
1173
+ odv.setUint32(8, h2, true);
1174
+ odv.setUint32(12, h3, true);
1175
+ odv.setUint32(16, h4, true);
1176
+ return out;
1177
+ }
1178
+ var MAX_RESULT_LENGTH = 4096;
1179
+ var MAX_MSG_LENGTH = 4096;
1180
+ function concatBytes(a, b) {
1181
+ const out = new Uint8Array(a.length + b.length);
1182
+ out.set(a, 0);
1183
+ out.set(b, a.length);
1184
+ return out;
1185
+ }
1186
+ var Op = class _Op {
1187
+ static MAX_RESULT_LENGTH = MAX_RESULT_LENGTH;
1188
+ static MAX_MSG_LENGTH = MAX_MSG_LENGTH;
1189
+ /** Deserializa una operación leyendo su tag y despachando a la factoría correcta. */
1190
+ static deserialize(ctx) {
1191
+ return _Op.deserializeFromTag(ctx, ctx.readByte());
1192
+ }
1193
+ /** Igual que `deserialize`, pero con el tag ya leído del stream (lo usa el árbol Timestamp). */
1194
+ static deserializeFromTag(ctx, tag) {
1195
+ const factory = OP_BY_TAG.get(tag);
1196
+ if (factory === void 0) {
1197
+ throw new UnknownOperationError(`unknown operation tag 0x${tag.toString(16).padStart(2, "0")}`);
1198
+ }
1199
+ return factory(ctx);
1200
+ }
1201
+ get maxMsgLength() {
1202
+ return MAX_MSG_LENGTH;
1203
+ }
1204
+ checkMsg(msg) {
1205
+ if (msg.length > this.maxMsgLength) {
1206
+ throw new MessageTooLongError(`message length ${msg.length} exceeds ${this.maxMsgLength}`);
1207
+ }
1208
+ }
1209
+ checkResult(result) {
1210
+ if (result.length > MAX_RESULT_LENGTH) {
1211
+ throw new ResultTooLongError(`result length ${result.length} exceeds ${MAX_RESULT_LENGTH}`);
1212
+ }
1213
+ return result;
1214
+ }
1215
+ };
1216
+ var OpBinary = class extends Op {
1217
+ arg;
1218
+ constructor(arg) {
1219
+ super();
1220
+ if (!(arg instanceof Uint8Array)) {
1221
+ throw new TypeError("OpBinary arg must be a Uint8Array");
1222
+ }
1223
+ this.arg = arg.slice();
1224
+ }
1225
+ serialize(ctx) {
1226
+ ctx.writeByte(this.tag);
1227
+ ctx.writeVarbytes(this.arg);
1228
+ }
1229
+ };
1230
+ var OpAppend = class _OpAppend extends OpBinary {
1231
+ static TAG = 240;
1232
+ tag = _OpAppend.TAG;
1233
+ tagName = "append";
1234
+ call(msg) {
1235
+ this.checkMsg(msg);
1236
+ return this.checkResult(concatBytes(msg, this.arg));
1237
+ }
1238
+ equals(other) {
1239
+ return other instanceof _OpAppend && bytesEqual(this.arg, other.arg);
1240
+ }
1241
+ };
1242
+ var OpPrepend = class _OpPrepend extends OpBinary {
1243
+ static TAG = 241;
1244
+ tag = _OpPrepend.TAG;
1245
+ tagName = "prepend";
1246
+ call(msg) {
1247
+ this.checkMsg(msg);
1248
+ return this.checkResult(concatBytes(this.arg, msg));
1249
+ }
1250
+ equals(other) {
1251
+ return other instanceof _OpPrepend && bytesEqual(this.arg, other.arg);
1252
+ }
1253
+ };
1254
+ var OpUnary = class extends Op {
1255
+ serialize(ctx) {
1256
+ ctx.writeByte(this.tag);
1257
+ }
1258
+ };
1259
+ var OpReverse = class _OpReverse extends OpUnary {
1260
+ static TAG = 242;
1261
+ tag = _OpReverse.TAG;
1262
+ tagName = "reverse";
1263
+ call(msg) {
1264
+ this.checkMsg(msg);
1265
+ const r = new Uint8Array(msg.length);
1266
+ for (let i = 0; i < msg.length; i++) r[i] = msg[msg.length - 1 - i];
1267
+ return this.checkResult(r);
1268
+ }
1269
+ equals(other) {
1270
+ return other instanceof _OpReverse;
1271
+ }
1272
+ };
1273
+ var OpHexlify = class _OpHexlify extends OpUnary {
1274
+ static TAG = 243;
1275
+ tag = _OpHexlify.TAG;
1276
+ tagName = "hexlify";
1277
+ // El resultado mide el doble que el mensaje; el límite de mensaje es la mitad.
1278
+ get maxMsgLength() {
1279
+ return MAX_RESULT_LENGTH / 2;
1280
+ }
1281
+ call(msg) {
1282
+ this.checkMsg(msg);
1283
+ return this.checkResult(textToBytes(bytesToHex(msg)));
1284
+ }
1285
+ equals(other) {
1286
+ return other instanceof _OpHexlify;
1287
+ }
1288
+ };
1289
+ var CryptOp = class extends OpUnary {
1290
+ call(msg) {
1291
+ this.checkMsg(msg);
1292
+ return this.hash(msg);
1293
+ }
1294
+ /**
1295
+ * Hashea el contenido COMPLETO de un fichero (longitud arbitraria) con el algoritmo
1296
+ * de esta operación. A diferencia de `call`, NO aplica el límite `MAX_MSG_LENGTH`:
1297
+ * `call` transforma digests dentro del árbol de prueba (≤ 4096 bytes), mientras que
1298
+ * `hashFile` recibe el contenido íntegro del fichero a sellar, que puede ser de
1299
+ * cualquier tamaño. Lo usa `DetachedTimestampFile.fromBytes`.
1300
+ */
1301
+ hashFile(data) {
1302
+ if (!(data instanceof Uint8Array)) {
1303
+ throw new TypeError("hashFile expects a Uint8Array");
1304
+ }
1305
+ return this.hash(data);
1306
+ }
1307
+ };
1308
+ var OpSHA1 = class _OpSHA1 extends CryptOp {
1309
+ static TAG = 2;
1310
+ tag = _OpSHA1.TAG;
1311
+ tagName = "sha1";
1312
+ digestLength = 20;
1313
+ hash(msg) {
1314
+ return sha1(msg);
1315
+ }
1316
+ equals(other) {
1317
+ return other instanceof _OpSHA1;
1318
+ }
1319
+ };
1320
+ var OpRIPEMD160 = class _OpRIPEMD160 extends CryptOp {
1321
+ static TAG = 3;
1322
+ tag = _OpRIPEMD160.TAG;
1323
+ tagName = "ripemd160";
1324
+ digestLength = 20;
1325
+ hash(msg) {
1326
+ return ripemd160(msg);
1327
+ }
1328
+ equals(other) {
1329
+ return other instanceof _OpRIPEMD160;
1330
+ }
1331
+ };
1332
+ var OpSHA256 = class _OpSHA256 extends CryptOp {
1333
+ static TAG = 8;
1334
+ tag = _OpSHA256.TAG;
1335
+ tagName = "sha256";
1336
+ digestLength = 32;
1337
+ hash(msg) {
1338
+ return sha256(msg);
1339
+ }
1340
+ equals(other) {
1341
+ return other instanceof _OpSHA256;
1342
+ }
1343
+ };
1344
+ var unary = (ctor) => () => new ctor();
1345
+ var binary = (ctor) => (ctx) => new ctor(ctx.readVarbytes(MAX_RESULT_LENGTH, 1));
1346
+ function buildTagTable(entries) {
1347
+ const map = /* @__PURE__ */ new Map();
1348
+ for (const [tag, factory] of entries) {
1349
+ if (map.has(tag)) {
1350
+ throw new Error(`duplicate operation tag 0x${tag.toString(16)}`);
1351
+ }
1352
+ map.set(tag, factory);
1353
+ }
1354
+ return map;
1355
+ }
1356
+ var OP_BY_TAG = buildTagTable([
1357
+ [OpAppend.TAG, binary(OpAppend)],
1358
+ [OpPrepend.TAG, binary(OpPrepend)],
1359
+ [OpReverse.TAG, unary(OpReverse)],
1360
+ [OpHexlify.TAG, unary(OpHexlify)],
1361
+ [OpSHA1.TAG, unary(OpSHA1)],
1362
+ [OpRIPEMD160.TAG, unary(OpRIPEMD160)],
1363
+ [OpSHA256.TAG, unary(OpSHA256)]
1364
+ ]);
1365
+ var TAG_SIZE = 8;
1366
+ var MAX_PAYLOAD_SIZE = 8192;
1367
+ var MAX_URI_LENGTH = 1e3;
1368
+ var ALLOWED_URI_CHARS = new Set(
1369
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._/:"
1370
+ );
1371
+ var PENDING_TAG = new Uint8Array([131, 223, 227, 13, 46, 249, 12, 142]);
1372
+ var BITCOIN_TAG = new Uint8Array([5, 136, 150, 13, 115, 215, 25, 1]);
1373
+ var LITECOIN_TAG = new Uint8Array([6, 134, 154, 13, 115, 215, 27, 69]);
1374
+ function decodeAndValidateUri(bytes) {
1375
+ if (bytes.length === 0) {
1376
+ throw new InvalidUriError("pending attestation URI is empty");
1377
+ }
1378
+ if (bytes.length > MAX_URI_LENGTH) {
1379
+ throw new InvalidUriError(`pending attestation URI exceeds ${MAX_URI_LENGTH} bytes`);
1380
+ }
1381
+ let uri = "";
1382
+ for (let i = 0; i < bytes.length; i++) {
1383
+ const byte = bytes[i];
1384
+ const char = String.fromCharCode(byte);
1385
+ if (!ALLOWED_URI_CHARS.has(char)) {
1386
+ throw new InvalidUriError(
1387
+ `pending attestation URI contains invalid byte 0x${byte.toString(16).padStart(2, "0")}`
1388
+ );
1389
+ }
1390
+ uri += char;
1391
+ }
1392
+ return uri;
1393
+ }
1394
+ function deserializeAttestation(ctx) {
1395
+ const tag = ctx.read(TAG_SIZE).slice();
1396
+ const payload = ctx.readVarbytes(MAX_PAYLOAD_SIZE);
1397
+ const payloadCtx = new StreamDeserializationContext(payload);
1398
+ let attestation;
1399
+ if (bytesEqual(tag, PENDING_TAG)) {
1400
+ const uriBytes = payloadCtx.readVarbytes(MAX_URI_LENGTH).slice();
1401
+ attestation = { kind: "pending", tag, uri: decodeAndValidateUri(uriBytes), uriBytes };
1402
+ } else if (bytesEqual(tag, BITCOIN_TAG)) {
1403
+ attestation = { kind: "bitcoin", tag, height: payloadCtx.readVaruint() };
1404
+ } else if (bytesEqual(tag, LITECOIN_TAG)) {
1405
+ attestation = { kind: "litecoin", tag, height: payloadCtx.readVaruint() };
1406
+ } else {
1407
+ return { kind: "unknown", tag, payload: payload.slice() };
1408
+ }
1409
+ payloadCtx.assertEof();
1410
+ return attestation;
1411
+ }
1412
+ function serializePayload(ctx, att) {
1413
+ switch (att.kind) {
1414
+ case "pending":
1415
+ ctx.writeVarbytes(att.uriBytes);
1416
+ return;
1417
+ case "bitcoin":
1418
+ case "litecoin":
1419
+ ctx.writeVaruint(att.height);
1420
+ return;
1421
+ case "unknown":
1422
+ ctx.writeBytes(att.payload);
1423
+ return;
1424
+ }
1425
+ }
1426
+ function serializeAttestation(ctx, att) {
1427
+ ctx.writeBytes(att.tag);
1428
+ const payloadCtx = new StreamSerializationContext();
1429
+ serializePayload(payloadCtx, att);
1430
+ ctx.writeVarbytes(payloadCtx.getOutput());
1431
+ }
1432
+ function compareAttestations(a, b) {
1433
+ const deltaTag = compareBytes(a.tag, b.tag);
1434
+ if (deltaTag !== 0) {
1435
+ return deltaTag;
1436
+ }
1437
+ switch (a.kind) {
1438
+ case "pending":
1439
+ return compareBytes(a.uriBytes, b.uriBytes);
1440
+ case "bitcoin":
1441
+ case "litecoin":
1442
+ return a.height - b.height;
1443
+ case "unknown":
1444
+ return compareBytes(a.payload, b.payload);
1445
+ }
1446
+ }
1447
+ function attestationsEqual(a, b) {
1448
+ if (a.kind !== b.kind || !bytesEqual(a.tag, b.tag)) {
1449
+ return false;
1450
+ }
1451
+ switch (a.kind) {
1452
+ case "pending":
1453
+ return bytesEqual(a.uriBytes, b.uriBytes);
1454
+ case "bitcoin":
1455
+ case "litecoin":
1456
+ return a.height === b.height;
1457
+ case "unknown":
1458
+ return bytesEqual(a.payload, b.payload);
1459
+ }
1460
+ }
1461
+ var MERKLEROOT_RE = /^[0-9a-fA-F]{64}$/;
1462
+ function verifyAgainstBlockheader(digest, block) {
1463
+ if (digest.length !== 32) {
1464
+ throw new VerificationError(`expected digest of 32 bytes; got ${digest.length}`);
1465
+ }
1466
+ if (typeof block.merkleroot !== "string" || !MERKLEROOT_RE.test(block.merkleroot)) {
1467
+ throw new VerificationError("block merkleroot is not a 64-char hex string");
1468
+ }
1469
+ if (!Number.isInteger(block.time) || block.time <= 0) {
1470
+ throw new VerificationError("block time is not a positive integer");
1471
+ }
1472
+ if (!bytesEqual(digest, hexToBytes(block.merkleroot))) {
1473
+ throw new VerificationError("digest does not match block merkleroot");
1474
+ }
1475
+ return block.time;
1476
+ }
1477
+ var MAX_TREE_DEPTH = 256;
1478
+ function opToBytes(op) {
1479
+ const ctx = new StreamSerializationContext();
1480
+ op.serialize(ctx);
1481
+ return ctx.getOutput();
1482
+ }
1483
+ function opKey(op) {
1484
+ return bytesToHex(opToBytes(op));
1485
+ }
1486
+ var Timestamp = class _Timestamp {
1487
+ /** Digest de este nodo (copia defensiva, no comparte memoria con la entrada). */
1488
+ msg;
1489
+ /** Sellos directos sobre `msg`. El cliente puede añadir con `.push()`. */
1490
+ attestations = [];
1491
+ /** Ramas indexadas por la serialización canónica (hex) de su op. */
1492
+ #ops = /* @__PURE__ */ new Map();
1493
+ constructor(msg) {
1494
+ if (!(msg instanceof Uint8Array)) {
1495
+ throw new TypeError("Timestamp msg must be a Uint8Array");
1496
+ }
1497
+ if (msg.length > Op.MAX_MSG_LENGTH) {
1498
+ throw new TypeError(`Timestamp msg length ${msg.length} exceeds ${Op.MAX_MSG_LENGTH}`);
1499
+ }
1500
+ this.msg = msg.slice();
1501
+ }
1502
+ /** El digest de este nodo (copia: mutar el resultado no afecta al árbol). */
1503
+ getDigest() {
1504
+ return this.msg.slice();
1505
+ }
1506
+ /** Las ramas (op + sub-timestamp) de este nodo, como array de solo lectura. */
1507
+ get branches() {
1508
+ return [...this.#ops.values()];
1509
+ }
1510
+ /**
1511
+ * Deserializa un Timestamp. El formato no incluye el mensaje sobre el que opera,
1512
+ * así que hay que aportarlo (`initialMsg`) para recalcular los resultados de las ops.
1513
+ * @param depth profundidad actual; protege contra árboles maliciosamente profundos.
1514
+ */
1515
+ static deserialize(ctx, initialMsg, depth = 0) {
1516
+ if (depth > MAX_TREE_DEPTH) {
1517
+ throw new OversizedDataError(`timestamp tree exceeds max depth ${MAX_TREE_DEPTH}`);
1518
+ }
1519
+ const self = new _Timestamp(initialMsg);
1520
+ let tag = ctx.readByte();
1521
+ while (tag === 255) {
1522
+ self.#deserializeElement(ctx, ctx.readByte(), depth);
1523
+ tag = ctx.readByte();
1524
+ }
1525
+ self.#deserializeElement(ctx, tag, depth);
1526
+ return self;
1527
+ }
1528
+ #deserializeElement(ctx, tag, depth) {
1529
+ if (tag === 0) {
1530
+ this.attestations.push(deserializeAttestation(ctx));
1531
+ return;
1532
+ }
1533
+ const op = Op.deserializeFromTag(ctx, tag);
1534
+ let result;
1535
+ try {
1536
+ result = op.call(this.msg);
1537
+ } catch (err) {
1538
+ throw new DeserializationError(
1539
+ `operation failed during deserialization: ${err.message}`
1540
+ );
1541
+ }
1542
+ const stamp = _Timestamp.deserialize(ctx, result, depth + 1);
1543
+ this.#ops.set(opKey(op), { op, stamp });
1544
+ }
1545
+ /** Serializa este nodo en orden canónico (determinista byte-a-byte). */
1546
+ serialize(ctx) {
1547
+ const attestations = [...this.attestations].sort(compareAttestations);
1548
+ const branches = [...this.#ops.values()].sort(
1549
+ (a, b) => compareBytes(opToBytes(a.op), opToBytes(b.op))
1550
+ );
1551
+ const total = attestations.length + branches.length;
1552
+ if (total === 0) {
1553
+ throw new EmptyTimestampError("an empty timestamp cannot be serialized");
1554
+ }
1555
+ let index = 0;
1556
+ for (const attestation of attestations) {
1557
+ if (index < total - 1) ctx.writeByte(255);
1558
+ ctx.writeByte(0);
1559
+ serializeAttestation(ctx, attestation);
1560
+ index++;
1561
+ }
1562
+ for (const { op, stamp } of branches) {
1563
+ if (index < total - 1) ctx.writeByte(255);
1564
+ op.serialize(ctx);
1565
+ stamp.serialize(ctx);
1566
+ index++;
1567
+ }
1568
+ }
1569
+ /**
1570
+ * Añade una op a este nodo y devuelve el sub-timestamp de su resultado.
1571
+ * Si la op (por contenido) ya existe, devuelve la rama existente.
1572
+ */
1573
+ add(op) {
1574
+ const key = opKey(op);
1575
+ const existing = this.#ops.get(key);
1576
+ if (existing !== void 0) {
1577
+ return existing.stamp;
1578
+ }
1579
+ const stamp = new _Timestamp(op.call(this.msg));
1580
+ this.#ops.set(key, { op, stamp });
1581
+ return stamp;
1582
+ }
1583
+ /**
1584
+ * Vincula `op` a un sub-timestamp YA EXISTENTE, compartiendo el objeto (no crea uno nuevo).
1585
+ * A diferencia de `add`, hace que esta rama apunte al mismo `Timestamp` que otra rama
1586
+ * (de otro nodo) ya construyó, de modo que las attestations añadidas más arriba sean
1587
+ * alcanzables desde ambos caminos. Lo usa el árbol Merkle para el cross-link izquierda/derecha.
1588
+ * Falla (fail-closed) si `stamp` no es un Timestamp o si `op.call(this.msg)` no coincide con `stamp.msg`.
1589
+ */
1590
+ addExisting(op, stamp) {
1591
+ if (!(stamp instanceof _Timestamp)) {
1592
+ throw new TypeError("addExisting requires a Timestamp");
1593
+ }
1594
+ if (!bytesEqual(op.call(this.msg), stamp.msg)) {
1595
+ throw new MergeError("operation result does not match the existing timestamp message");
1596
+ }
1597
+ this.#ops.set(opKey(op), { op, stamp });
1598
+ return stamp;
1599
+ }
1600
+ /** Incorpora las attestations y ramas de `other` (mismo `msg`) en este timestamp. */
1601
+ merge(other) {
1602
+ if (!(other instanceof _Timestamp)) {
1603
+ throw new MergeError("can only merge Timestamps together");
1604
+ }
1605
+ if (!bytesEqual(this.msg, other.msg)) {
1606
+ throw new MergeError("cannot merge timestamps for different messages");
1607
+ }
1608
+ for (const attestation of other.attestations) {
1609
+ if (!this.attestations.some((existing) => attestationsEqual(existing, attestation))) {
1610
+ this.attestations.push(attestation);
1611
+ }
1612
+ }
1613
+ for (const { op, stamp } of other.#ops.values()) {
1614
+ const key = opKey(op);
1615
+ let branch = this.#ops.get(key);
1616
+ if (branch === void 0) {
1617
+ branch = { op, stamp: new _Timestamp(op.call(this.msg)) };
1618
+ this.#ops.set(key, branch);
1619
+ }
1620
+ branch.stamp.merge(stamp);
1621
+ }
1622
+ }
1623
+ /** Todas las attestations del árbol con el msg de su nodo (sin pérdida de datos). */
1624
+ allAttestations() {
1625
+ const result = [];
1626
+ for (const attestation of this.attestations) {
1627
+ result.push({ msg: this.msg.slice(), attestation });
1628
+ }
1629
+ for (const { stamp } of this.#ops.values()) {
1630
+ result.push(...stamp.allAttestations());
1631
+ }
1632
+ return result;
1633
+ }
1634
+ /** Todas las attestations del árbol (sin el msg asociado). */
1635
+ getAttestations() {
1636
+ return this.allAttestations().map((entry) => entry.attestation);
1637
+ }
1638
+ /** Verdadero si el árbol contiene una attestation verificable localmente (Bitcoin/Litecoin). */
1639
+ isTimestampComplete() {
1640
+ return this.allAttestations().some(
1641
+ ({ attestation }) => attestation.kind === "bitcoin" || attestation.kind === "litecoin"
1642
+ );
1643
+ }
1644
+ /** Sub-timestamps que tienen attestations directas. */
1645
+ directlyVerified() {
1646
+ if (this.attestations.length > 0) {
1647
+ return [this];
1648
+ }
1649
+ const result = [];
1650
+ for (const { stamp } of this.#ops.values()) {
1651
+ result.push(...stamp.directlyVerified());
1652
+ }
1653
+ return result;
1654
+ }
1655
+ /** Los mensajes de las hojas del árbol (nodos sin ops). */
1656
+ allTips() {
1657
+ if (this.#ops.size === 0) {
1658
+ return [this.msg.slice()];
1659
+ }
1660
+ const result = [];
1661
+ for (const { stamp } of this.#ops.values()) {
1662
+ result.push(...stamp.allTips());
1663
+ }
1664
+ return result;
1665
+ }
1666
+ /** Igualdad estructural recursiva con otro timestamp. */
1667
+ equals(other) {
1668
+ if (!(other instanceof _Timestamp)) {
1669
+ return false;
1670
+ }
1671
+ if (!bytesEqual(this.msg, other.msg)) {
1672
+ return false;
1673
+ }
1674
+ if (this.attestations.length !== other.attestations.length) {
1675
+ return false;
1676
+ }
1677
+ const ours = [...this.attestations].sort(compareAttestations);
1678
+ const theirs = [...other.attestations].sort(compareAttestations);
1679
+ for (let i = 0; i < ours.length; i++) {
1680
+ if (!attestationsEqual(ours[i], theirs[i])) {
1681
+ return false;
1682
+ }
1683
+ }
1684
+ if (this.#ops.size !== other.#ops.size) {
1685
+ return false;
1686
+ }
1687
+ for (const [key, branch] of this.#ops) {
1688
+ const otherBranch = other.#ops.get(key);
1689
+ if (otherBranch === void 0) {
1690
+ return false;
1691
+ }
1692
+ if (!branch.stamp.equals(otherBranch.stamp)) {
1693
+ return false;
1694
+ }
1695
+ }
1696
+ return true;
1697
+ }
1698
+ };
1699
+ function catSha256(left, right) {
1700
+ if (!(left instanceof Timestamp) || !(right instanceof Timestamp)) {
1701
+ throw new TypeError("catSha256 requires two Timestamps");
1702
+ }
1703
+ const concat = right.add(new OpPrepend(left.msg));
1704
+ left.addExisting(new OpAppend(right.msg), concat);
1705
+ return concat.add(new OpSHA256());
1706
+ }
1707
+ function makeMerkleTree(timestamps) {
1708
+ if (timestamps.length === 0) {
1709
+ throw new EmptyMerkleTreeError("makeMerkleTree requires at least one timestamp");
1710
+ }
1711
+ for (const stamp of timestamps) {
1712
+ if (!(stamp instanceof Timestamp)) {
1713
+ throw new TypeError("makeMerkleTree requires an array of Timestamps");
1714
+ }
1715
+ }
1716
+ let round = [...timestamps];
1717
+ while (round.length > 1) {
1718
+ const next = [];
1719
+ for (let i = 0; i < round.length; i += 2) {
1720
+ if (i + 1 < round.length) {
1721
+ next.push(catSha256(round[i], round[i + 1]));
1722
+ } else {
1723
+ next.push(round[i]);
1724
+ }
1725
+ }
1726
+ round = next;
1727
+ }
1728
+ return round[0];
1729
+ }
1730
+ var HEADER_MAGIC = new Uint8Array([
1731
+ 0,
1732
+ 79,
1733
+ 112,
1734
+ 101,
1735
+ 110,
1736
+ 84,
1737
+ 105,
1738
+ 109,
1739
+ 101,
1740
+ 115,
1741
+ 116,
1742
+ 97,
1743
+ 109,
1744
+ 112,
1745
+ 115,
1746
+ 0,
1747
+ 0,
1748
+ 80,
1749
+ 114,
1750
+ 111,
1751
+ 111,
1752
+ 102,
1753
+ 0,
1754
+ 191,
1755
+ 137,
1756
+ 226,
1757
+ 232,
1758
+ 132,
1759
+ 232,
1760
+ 146,
1761
+ 148
1762
+ ]);
1763
+ var MAJOR_VERSION = 1;
1764
+ var DetachedTimestampFile = class _DetachedTimestampFile {
1765
+ fileHashOp;
1766
+ timestamp;
1767
+ constructor(fileHashOp, timestamp) {
1768
+ if (!(fileHashOp instanceof CryptOp)) {
1769
+ throw new TypeError("DetachedTimestampFile: fileHashOp must be a CryptOp");
1770
+ }
1771
+ if (!(timestamp instanceof Timestamp)) {
1772
+ throw new TypeError("DetachedTimestampFile: timestamp must be a Timestamp");
1773
+ }
1774
+ if (timestamp.msg.length !== fileHashOp.digestLength) {
1775
+ throw new TypeError(
1776
+ `DetachedTimestampFile: timestamp message length ${timestamp.msg.length} does not match ${fileHashOp.tagName} digest length ${fileHashOp.digestLength}`
1777
+ );
1778
+ }
1779
+ this.fileHashOp = fileHashOp;
1780
+ this.timestamp = timestamp;
1781
+ }
1782
+ /** Digest del fichero sellado (copia defensiva: mutarla no afecta al objeto). */
1783
+ fileDigest() {
1784
+ return this.timestamp.getDigest();
1785
+ }
1786
+ /** Escribe el fichero `.ots` en el contexto: magic → versión → op → digest → árbol. */
1787
+ serialize(ctx) {
1788
+ ctx.writeBytes(HEADER_MAGIC);
1789
+ ctx.writeVaruint(MAJOR_VERSION);
1790
+ this.fileHashOp.serialize(ctx);
1791
+ ctx.writeBytes(this.timestamp.msg);
1792
+ this.timestamp.serialize(ctx);
1793
+ }
1794
+ /** Serializa el fichero `.ots` completo a bytes. */
1795
+ serializeToBytes() {
1796
+ const ctx = new StreamSerializationContext();
1797
+ this.serialize(ctx);
1798
+ return ctx.getOutput();
1799
+ }
1800
+ /**
1801
+ * Lee un fichero `.ots` desde bytes. Único tipo de entrada: `Uint8Array` (fail-closed;
1802
+ * elimina los 4 tipos del original y el bug `Array.from(ArrayBuffer) → []`).
1803
+ */
1804
+ static deserialize(input) {
1805
+ if (!(input instanceof Uint8Array)) {
1806
+ throw new TypeError("DetachedTimestampFile.deserialize expects a Uint8Array");
1807
+ }
1808
+ const ctx = new StreamDeserializationContext(input);
1809
+ ctx.assertMagic(HEADER_MAGIC);
1810
+ const major = ctx.readVaruint();
1811
+ if (major !== MAJOR_VERSION) {
1812
+ throw new UnsupportedVersionError(`unsupported .ots major version ${major}`);
1813
+ }
1814
+ const op = Op.deserialize(ctx);
1815
+ if (!(op instanceof CryptOp)) {
1816
+ throw new DeserializationError("file hash operation must be a cryptographic hash");
1817
+ }
1818
+ const fileHash = ctx.read(op.digestLength);
1819
+ const timestamp = Timestamp.deserialize(ctx, fileHash);
1820
+ ctx.assertEof();
1821
+ return new _DetachedTimestampFile(op, timestamp);
1822
+ }
1823
+ /** Crea un `.ots` nuevo hasheando el contenido completo de un fichero. */
1824
+ static fromBytes(fileHashOp, fileContent) {
1825
+ if (!(fileHashOp instanceof CryptOp)) {
1826
+ throw new TypeError("DetachedTimestampFile.fromBytes: fileHashOp must be a CryptOp");
1827
+ }
1828
+ const digest = fileHashOp.hashFile(fileContent);
1829
+ return new _DetachedTimestampFile(fileHashOp, new Timestamp(digest));
1830
+ }
1831
+ /** Crea un `.ots` nuevo a partir de un digest ya calculado del fichero. */
1832
+ static fromHash(fileHashOp, fileDigest) {
1833
+ return new _DetachedTimestampFile(fileHashOp, new Timestamp(fileDigest));
1834
+ }
1835
+ /** Igualdad estructural con otro fichero `.ots`. */
1836
+ equals(other) {
1837
+ return other instanceof _DetachedTimestampFile && this.fileHashOp.equals(other.fileHashOp) && this.timestamp.equals(other.timestamp);
1838
+ }
1839
+ };
402
1840
 
403
1841
  // src/network/calendar.ts
404
- import { Timestamp, StreamDeserializationContext, bytesToHex } from "@otskit/core";
405
1842
  var MAX_CALENDAR_RESPONSE_SIZE = 1e4;
406
1843
  function assertCommitment(bytes) {
407
1844
  if (!(bytes instanceof Uint8Array)) {
@@ -523,7 +1960,6 @@ var DEFAULT_AGGREGATORS = [
523
1960
  ];
524
1961
 
525
1962
  // src/network/esplora.ts
526
- import { verifyAgainstBlockheader, VerificationError } from "@otskit/core";
527
1963
  var PUBLIC_ESPLORA_URL = "https://blockstream.info/api";
528
1964
  var MAX_ESPLORA_RESPONSE_SIZE = 1e5;
529
1965
  var HEX64_RE = /^[0-9a-f]{64}$/i;
@@ -931,10 +2367,6 @@ var OpenTimestampsClient = class {
931
2367
  this.networkLayer.resetAllCircuits();
932
2368
  }
933
2369
  };
934
-
935
- // src/index.ts
936
- import { DetachedTimestampFile as DetachedTimestampFile2, Timestamp as Timestamp2 } from "@otskit/core";
937
- import { verifyAgainstBlockheader as verifyAgainstBlockheader2 } from "@otskit/core";
938
2370
  export {
939
2371
  CalendarClient,
940
2372
  CalendarResponseTooLargeError,
@@ -945,7 +2377,7 @@ export {
945
2377
  DEFAULT_CALENDARS,
946
2378
  DEFAULT_CALENDAR_WHITELIST,
947
2379
  DEFAULT_RESILIENCE,
948
- DetachedTimestampFile2 as DetachedTimestampFile,
2380
+ DetachedTimestampFile,
949
2381
  EsploraClient,
950
2382
  EsploraResponseError,
951
2383
  MAX_CALENDAR_RESPONSE_SIZE,
@@ -956,10 +2388,10 @@ export {
956
2388
  PUBLIC_ESPLORA_URL,
957
2389
  ResilientNetworkLayer,
958
2390
  StampError,
959
- Timestamp2 as Timestamp,
2391
+ Timestamp,
960
2392
  UpgradeError,
961
2393
  UrlWhitelist,
962
2394
  ValidationError,
963
- verifyAgainstBlockheader2 as verifyAgainstBlockheader,
2395
+ verifyAgainstBlockheader,
964
2396
  verifyTimestampAttestation
965
2397
  };