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