@leofcoin/chain 1.10.15 → 1.10.16

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.
Files changed (22) hide show
  1. package/exports/browser/beacon-wire.js +2 -2
  2. package/exports/browser/{browser-DvU1xNFS-DmmNUdE-.js → browser-CCfR9KOM-CvhDZrGu.js} +3 -3
  3. package/exports/browser/{browser-crypto-B9N9KlL4.js → browser-crypto-CeE-yId5.js} +283 -158
  4. package/exports/browser/chain.js +28 -11
  5. package/exports/browser/{client-DDC8C510-3uaB70KL.js → client-CAC7RVMf-Dmz_2A_5.js} +4 -4
  6. package/exports/browser/{connection-monitor-GPztgSSJ.js → connection-monitor-Cmv00mwl.js} +4 -4
  7. package/exports/browser/{index-Ccn5sExe.js → index-BC-KRBgE.js} +55 -52
  8. package/exports/browser/{index-Bxr5Iztg-WbFIosyf.js → index-dTQVgkpf-CuHJWO2l.js} +3 -3
  9. package/exports/browser/{messages-DH0tgNM9-C2CtyLsf.js → messages-Dw86z0zq-DrPw_nlZ.js} +9 -4
  10. package/exports/browser/{node-browser-BYi7D_oa.js → node-browser-DWSdi-XP.js} +950 -482
  11. package/exports/browser/node-browser.js +3 -3
  12. package/exports/browser/workers/block-worker.js +5 -3
  13. package/exports/browser/workers/machine-worker.js +8 -8
  14. package/exports/{workers/worker-DMCj1e6z-CTYVa2yX.js → browser/workers/worker-BDTWOCzS-COrAszCt.js} +304 -168
  15. package/exports/chain.js +10 -5
  16. package/exports/{constants-CMYKv-Rt.js → constants-Dy9i9S_o.js} +1 -1
  17. package/exports/hash-format-state.js +17 -0
  18. package/exports/node.js +1 -1
  19. package/exports/workers/block-worker.js +5 -3
  20. package/exports/workers/machine-worker.js +8 -8
  21. package/exports/{browser/workers/worker-DMCj1e6z-CTYVa2yX.js → workers/worker-BDTWOCzS-COrAszCt.js} +304 -168
  22. package/package.json +2 -2
@@ -1,6 +1,6 @@
1
- import { s as signTransaction, c as MultiWallet, d as createTransactionHash } from './index-Ccn5sExe.js';
1
+ import { s as signTransaction, c as MultiWallet, d as createTransactionHash } from './index-BC-KRBgE.js';
2
2
  import { f as fromBase58 } from './index-CvKt4UDE.js';
3
- import './browser-crypto-B9N9KlL4.js';
3
+ import './browser-crypto-CeE-yId5.js';
4
4
  import './sha2-piimb6xL.js';
5
5
 
6
6
  const validInteger = (value, minimum = 0n) => {
@@ -1,8 +1,8 @@
1
- import { g as getDefaultExportFromCjs } from './node-browser-BYi7D_oa.js';
2
- import './browser-crypto-B9N9KlL4.js';
1
+ import { g as getDefaultExportFromCjs } from './node-browser-DWSdi-XP.js';
2
+ import './browser-crypto-CeE-yId5.js';
3
3
  import './index-CvKt4UDE.js';
4
4
  import './sha2-piimb6xL.js';
5
- import './connection-monitor-GPztgSSJ.js';
5
+ import './connection-monitor-Cmv00mwl.js';
6
6
 
7
7
  function _mergeNamespaces$1(n, m) {
8
8
  m.forEach(function (e) {
@@ -1,4 +1,4 @@
1
- import { a as index$4, b as base58$1, i as index$5, f as fromBase58, c as fromHex, d as fromArrayLike, e as toBase32, t as toBase58, g as toHex } from './index-CvKt4UDE.js';
1
+ import { a as index$3, b as base58$1, i as index$4, f as fromBase58, c as fromHex, d as fromArrayLike, e as toBase32, t as toBase58, g as toHex } from './index-CvKt4UDE.js';
2
2
  import { s as sha256 } from './sha2-piimb6xL.js';
3
3
 
4
4
  var isHex = string => /^[A-F0-9]+$/i.test(
@@ -7,100 +7,217 @@ var isHex = string => /^[A-F0-9]+$/i.test(
7
7
  string
8
8
  );
9
9
 
10
- const MSB$1 = 0x80;
11
- const MSBALL = -128;
12
- const INT = Math.pow(2, 31);
13
- const encode$1 = (num, out, offset) => {
14
- if (Number.MAX_SAFE_INTEGER && num > Number.MAX_SAFE_INTEGER) {
15
- encode$1.bytes = 0;
16
- throw new RangeError('Could not encode varint');
17
- }
18
- out = out || [];
19
- offset = offset || 0;
20
- const oldOffset = offset;
21
- while (num >= INT) {
22
- out[offset++] = (num & 0xFF) | MSB$1;
23
- num /= 128;
24
- }
25
- while (num & MSBALL) {
26
- out[offset++] = (num & 0xFF) | MSB$1;
27
- num >>>= 7;
28
- }
29
- out[offset] = num | 0;
30
- encode$1.bytes = offset - oldOffset + 1;
31
- return out;
32
- };
33
-
34
- const MSB = 0x80;
35
- const REST = 0x7F;
36
- const decode$1 = (buf, offset) => {
37
- offset = offset || 0;
38
- const l = buf.length;
39
- let counter = offset;
40
- let result = 0;
41
- let shift = 0;
42
- let b;
10
+ const MAX_BIGINT = (1n << 256n) - 1n;
11
+ function size(value) {
12
+ if (!Number.isSafeInteger(value) || value < 0)
13
+ throw new RangeError("Expected an unsigned safe integer");
14
+ }
15
+ function bytes(value) {
16
+ if (!(value instanceof Uint8Array))
17
+ throw new TypeError("Expected Uint8Array");
18
+ }
19
+ function varUintLength(value) {
20
+ size(value);
21
+ let length = 1;
22
+ while (value >= 128) {
23
+ value = Math.floor(value / 128);
24
+ length++;
25
+ }
26
+ return length;
27
+ }
28
+ function varBigIntLength(value) {
29
+ if (typeof value !== "bigint")
30
+ throw new TypeError("Expected BigInt");
31
+ if (value < 0n || value > MAX_BIGINT)
32
+ throw new RangeError("Expected an unsigned 256-bit integer");
33
+ let length = 1;
34
+ while (value >= 128n) {
35
+ value >>= 7n;
36
+ length++;
37
+ }
38
+ return length;
39
+ }
40
+ class BinaryWriter {
41
+ #buffer;
42
+ #position = 0;
43
+ constructor(capacity = 64) {
44
+ if (typeof capacity === "number") {
45
+ size(capacity);
46
+ this.#buffer = new Uint8Array(capacity);
47
+ } else {
48
+ bytes(capacity);
49
+ this.#buffer = capacity;
50
+ }
51
+ }
52
+ get position() {
53
+ return this.#position;
54
+ }
55
+ get remaining() {
56
+ return this.#buffer.length - this.#position;
57
+ }
58
+ #require(length) {
59
+ size(length);
60
+ if (length > this.remaining)
61
+ throw new RangeError("Binary output buffer is too small");
62
+ }
63
+ writeVarUint(value) {
64
+ this.#require(varUintLength(value));
65
+ this.#writeVarUint(value);
66
+ return this;
67
+ }
68
+ #writeVarUint(value) {
69
+ while (value >= 128) {
70
+ this.#buffer[this.#position++] = value % 128 | 128;
71
+ value = Math.floor(value / 128);
72
+ }
73
+ this.#buffer[this.#position++] = value;
74
+ }
75
+ writeVarBigInt(value) {
76
+ this.#require(varBigIntLength(value));
43
77
  do {
44
- if (counter >= l || shift > 49) {
45
- decode$1.bytes = 0;
46
- throw new RangeError('Could not decode varint');
47
- }
48
- b = buf[counter++];
49
- result += shift < 28
50
- ? (b & REST) << shift
51
- : (b & REST) * Math.pow(2, shift);
52
- shift += 7;
53
- } while (b >= MSB);
54
- decode$1.bytes = counter - offset;
55
- return result;
56
- };
57
-
58
- const N1 = Math.pow(2, 7);
59
- const N2 = Math.pow(2, 14);
60
- const N3 = Math.pow(2, 21);
61
- const N4 = Math.pow(2, 28);
62
- const N5 = Math.pow(2, 35);
63
- const N6 = Math.pow(2, 42);
64
- const N7 = Math.pow(2, 49);
65
- const N8 = Math.pow(2, 56);
66
- const N9 = Math.pow(2, 63);
67
- var encodingLength = (value) => (value < N1 ? 1
68
- : value < N2 ? 2
69
- : value < N3 ? 3
70
- : value < N4 ? 4
71
- : value < N5 ? 5
72
- : value < N6 ? 6
73
- : value < N7 ? 7
74
- : value < N8 ? 8
75
- : value < N9 ? 9
76
- : 10);
77
-
78
- var index$3 = {
79
- encode: encode$1,
80
- decode: decode$1,
81
- encodingLength
82
- };
83
-
84
- var index$2 = (input, prefix) => {
85
- const encodedArray = [];
86
- const length = input.reduce((total, current) => {
87
- const encoded = index$3.encode(current.length);
88
- encodedArray.push(encoded);
89
- total += current.length + encoded.length;
90
- return total;
91
- }, 0);
92
- const typedArray = new Uint8Array(length);
93
- let currentIndex = 0;
94
- let index = 0;
95
- for (const source of input) {
96
- typedArray.set(encodedArray[index], currentIndex);
97
- currentIndex += encodedArray[index].length;
98
- typedArray.set(source, currentIndex);
99
- currentIndex += source.length;
100
- index += 1;
101
- }
102
- return typedArray;
78
+ const byte = Number(value & 127n);
79
+ value >>= 7n;
80
+ this.#buffer[this.#position++] = byte | (value ? 128 : 0);
81
+ } while (value);
82
+ return this;
83
+ }
84
+ writeBytes(value) {
85
+ bytes(value);
86
+ this.#require(value.length);
87
+ this.#buffer.set(value, this.#position);
88
+ this.#position += value.length;
89
+ return this;
90
+ }
91
+ writeFrame(value) {
92
+ bytes(value);
93
+ const prefixLength = varUintLength(value.length);
94
+ this.#require(prefixLength + value.length);
95
+ const prefixStart = this.#buffer.byteOffset + this.#position;
96
+ if (value.buffer === this.#buffer.buffer && value.byteOffset < prefixStart + prefixLength && value.byteOffset + value.length > prefixStart)
97
+ throw new RangeError("Frame source overlaps its length prefix");
98
+ this.#writeVarUint(value.length);
99
+ this.#buffer.set(value, this.#position);
100
+ this.#position += value.length;
101
+ return this;
102
+ }
103
+ reset() {
104
+ this.#position = 0;
105
+ return this;
106
+ }
107
+ finish(copy = false) {
108
+ const result = this.#buffer.subarray(0, this.#position);
109
+ return copy ? new Uint8Array(result) : result;
110
+ }
111
+ }
112
+ class BinaryReader {
113
+ #buffer;
114
+ #position = 0;
115
+ constructor(buffer) {
116
+ bytes(buffer);
117
+ this.#buffer = buffer;
118
+ }
119
+ get position() {
120
+ return this.#position;
121
+ }
122
+ get remaining() {
123
+ return this.#buffer.length - this.#position;
124
+ }
125
+ readVarUint(canonical = true) {
126
+ let result = 0;
127
+ let multiplier = 1;
128
+ for (let index = 0; index < 8; index++) {
129
+ const cursor = this.#position + index;
130
+ if (cursor >= this.#buffer.length)
131
+ throw new RangeError("Truncated varint");
132
+ const byte = this.#buffer[cursor];
133
+ const payload = byte & 127;
134
+ if (index === 7 && payload > Math.floor((Number.MAX_SAFE_INTEGER - result) / multiplier))
135
+ throw new RangeError("Varint exceeds safe integer range");
136
+ result += payload * multiplier;
137
+ if (byte < 128) {
138
+ if (canonical && index > 0 && payload === 0)
139
+ throw new RangeError("Non-canonical varint");
140
+ this.#position = cursor + 1;
141
+ return result;
142
+ }
143
+ multiplier *= 128;
144
+ }
145
+ throw new RangeError("Varint exceeds safe integer range");
146
+ }
147
+ readVarBigInt(canonical = true) {
148
+ let result = 0n;
149
+ for (let index = 0; index < 37; index++) {
150
+ const cursor = this.#position + index;
151
+ if (cursor >= this.#buffer.length)
152
+ throw new RangeError("Truncated varint");
153
+ const byte = this.#buffer[cursor];
154
+ result |= BigInt(byte & 127) << BigInt(index * 7);
155
+ if (result > MAX_BIGINT)
156
+ throw new RangeError("Varint exceeds 256 bits");
157
+ if (byte < 128) {
158
+ if (canonical && index > 0 && byte === 0)
159
+ throw new RangeError("Non-canonical varint");
160
+ this.#position = cursor + 1;
161
+ return result;
162
+ }
163
+ }
164
+ throw new RangeError("Varint exceeds 256 bits");
165
+ }
166
+ readBytes(length) {
167
+ size(length);
168
+ if (length > this.remaining)
169
+ throw new RangeError("Truncated binary field");
170
+ const start = this.#position;
171
+ this.#position += length;
172
+ return this.#buffer.subarray(start, this.#position);
173
+ }
174
+ readFrame(canonical = true) {
175
+ const start = this.#position;
176
+ try {
177
+ return this.readBytes(this.readVarUint(canonical));
178
+ } catch (error) {
179
+ this.#position = start;
180
+ throw error;
181
+ }
182
+ }
183
+ }
184
+ function encodeFrames(frames, prefix) {
185
+ if (!Array.isArray(frames))
186
+ throw new TypeError("Expected an array of byte frames");
187
+ let length = 0;
188
+ for (const frame of frames) {
189
+ bytes(frame);
190
+ length += varUintLength(frame.length) + frame.length;
191
+ size(length);
192
+ }
193
+ const writer = new BinaryWriter(length);
194
+ for (const frame of frames)
195
+ writer.writeFrame(frame);
196
+ return writer.finish();
197
+ }
198
+ function decodeFrames(buffer, prefixLength = 0, options = {}) {
199
+ const reader = new BinaryReader(buffer);
200
+ reader.readBytes(prefixLength);
201
+ const maxFrames = options.maxFrames ?? buffer.length;
202
+ size(maxFrames);
203
+ const frames = [];
204
+ while (reader.remaining) {
205
+ if (frames.length >= maxFrames)
206
+ throw new RangeError("Too many binary frames");
207
+ frames.push(reader.readFrame(options.canonical ?? true));
208
+ }
209
+ return frames;
210
+ }
211
+
212
+ const asBytes = (source) => {
213
+ if (source instanceof Uint8Array)
214
+ return source;
215
+ // Keep legacy ArrayLike coercion. Typed-array callers take the zero-copy path.
216
+ const bytes = new Uint8Array(source.length);
217
+ bytes.set(source);
218
+ return bytes;
103
219
  };
220
+ var index$2 = (input, prefix) => encodeFrames(input.map(asBytes));
104
221
 
105
222
  /*! pako 2.2.0 https://github.com/nodeca/pako @license (MIT AND Zlib) */
106
223
  // (C) 1995-2013 Jean-loup Gailly and Mark Adler
@@ -7098,24 +7215,9 @@ const encode = (proto, input, compress) => {
7098
7215
  return compress ? pako.deflate(index$2(set)) : index$2(set);
7099
7216
  };
7100
7217
 
7101
- var index$1 = (typedArray, prefix) => {
7102
- const set = [];
7103
- const varintAndSub = (typedArray) => {
7104
- const length = index$3.decode(typedArray);
7105
- // remove length
7106
- typedArray = typedArray.subarray(index$3.decode.bytes);
7107
- // push value
7108
- set.push(typedArray.subarray(0, length));
7109
- // remove value
7110
- typedArray = typedArray.subarray(length);
7111
- if (typedArray.length !== 0)
7112
- return varintAndSub(typedArray);
7113
- return set;
7114
- };
7115
- return varintAndSub(typedArray);
7116
- };
7117
-
7118
- const { toString } = index$4;
7218
+ var index$1 = (typedArray, prefix) => decodeFrames(typedArray, 0, { canonical: false });
7219
+
7220
+ const { toString } = index$3;
7119
7221
  const decoder = new TextDecoder();
7120
7222
  const decode = (proto, uint8Array, compressed) => {
7121
7223
  if (compressed)
@@ -7211,13 +7313,13 @@ class BasicInterface {
7211
7313
  this.fromBs58(value);
7212
7314
  }
7213
7315
  catch {
7214
- if (index$5.isBase32(value))
7316
+ if (index$4.isBase32(value))
7215
7317
  this.fromBs32(value);
7216
7318
  else
7217
7319
  throw new Error('Invalid base58/base32 input');
7218
7320
  }
7219
7321
  }
7220
- else if (index$5.isBase32(value))
7322
+ else if (index$4.isBase32(value))
7221
7323
  this.fromBs32(value);
7222
7324
  else
7223
7325
  this.fromString(value);
@@ -7285,13 +7387,13 @@ class BasicInterface {
7285
7387
  return isHex(string);
7286
7388
  }
7287
7389
  isBase32(string) {
7288
- return index$5.isBase32(string);
7390
+ return index$4.isBase32(string);
7289
7391
  }
7290
7392
  isBase58(string) {
7291
7393
  return base58$1.isBase58(string);
7292
7394
  }
7293
7395
  fromBs32(encoded) {
7294
- return this.decode(index$5.decode(encoded));
7396
+ return this.decode(index$4.decode(encoded));
7295
7397
  }
7296
7398
  fromBs58(encoded) {
7297
7399
  return this.decode(fromBase58(encoded));
@@ -7634,7 +7736,7 @@ let Codec$1 = class Codec extends BasicInterface {
7634
7736
  super();
7635
7737
  if (buffer) {
7636
7738
  if (buffer instanceof Uint8Array) {
7637
- const codec = index$3.decode(buffer);
7739
+ const codec = new BinaryReader(buffer).readVarUint(false);
7638
7740
  const name = this.getCodecName(codec);
7639
7741
  if (name) {
7640
7742
  this.name = name;
@@ -7646,11 +7748,12 @@ let Codec$1 = class Codec extends BasicInterface {
7646
7748
  }
7647
7749
  }
7648
7750
  else if (buffer instanceof ArrayBuffer) {
7649
- const codec = index$3.decode(buffer);
7751
+ const view = new Uint8Array(buffer);
7752
+ const codec = new BinaryReader(view).readVarUint(false);
7650
7753
  const name = this.getCodecName(codec);
7651
7754
  if (name) {
7652
7755
  this.name = name;
7653
- this.decode(buffer);
7756
+ this.decode(view);
7654
7757
  }
7655
7758
  else {
7656
7759
  this.encode(Number(new TextDecoder().decode(new Uint8Array(buffer))));
@@ -7675,7 +7778,7 @@ let Codec$1 = class Codec extends BasicInterface {
7675
7778
  }
7676
7779
  }
7677
7780
  fromEncoded(encoded) {
7678
- const codec = index$3.decode(encoded);
7781
+ const codec = new BinaryReader(encoded).readVarUint(false);
7679
7782
  const name = this.getCodecName(codec);
7680
7783
  this.name = name;
7681
7784
  this.encoded = encoded;
@@ -7694,7 +7797,7 @@ let Codec$1 = class Codec extends BasicInterface {
7694
7797
  this.name = this.getCodecName(codec);
7695
7798
  this.hashAlg = this.getHashAlg(this.name);
7696
7799
  this.codec = this.getCodec(this.name);
7697
- this.codecBuffer = index$3.encode(this.codec);
7800
+ this.codecBuffer = new BinaryWriter(varUintLength(this.codec)).writeVarUint(this.codec).finish();
7698
7801
  this.decoded = {
7699
7802
  name: this.name,
7700
7803
  hashAlg: this.hashAlg,
@@ -7707,7 +7810,7 @@ let Codec$1 = class Codec extends BasicInterface {
7707
7810
  this.name = name;
7708
7811
  this.codec = codec;
7709
7812
  this.hashAlg = this.getHashAlg(name);
7710
- this.codecBuffer = index$3.encode(this.codec);
7813
+ this.codecBuffer = new BinaryWriter(varUintLength(this.codec)).writeVarUint(this.codec).finish();
7711
7814
  this.decoded = {
7712
7815
  name: this.name,
7713
7816
  hashAlg: this.hashAlg,
@@ -7717,13 +7820,13 @@ let Codec$1 = class Codec extends BasicInterface {
7717
7820
  }
7718
7821
  decode(encoded) {
7719
7822
  encoded = encoded || this.encoded;
7720
- const codec = index$3.decode(encoded);
7823
+ const codec = new BinaryReader(encoded).readVarUint(false);
7721
7824
  this.fromCodec(codec);
7722
7825
  return this.decoded;
7723
7826
  }
7724
7827
  encode(codec) {
7725
7828
  codec = codec || this.codec;
7726
- this.encoded = index$3.encode(codec);
7829
+ this.encoded = new BinaryWriter(varUintLength(codec)).writeVarUint(codec).finish();
7727
7830
  return this.encoded;
7728
7831
  }
7729
7832
  };
@@ -8469,10 +8572,37 @@ const digestBuffer = async (hashVariant, buffer) => {
8469
8572
  const digest = hasher.digest('binary');
8470
8573
  return digest instanceof Uint8Array ? digest : new Uint8Array(digest);
8471
8574
  };
8575
+ const hashHeader = (buffer) => {
8576
+ if (!(buffer instanceof Uint8Array))
8577
+ throw new TypeError('CodecHash only supports Uint8Array input');
8578
+ const reader = new BinaryReader(buffer);
8579
+ const codec = reader.readVarUint();
8580
+ const name = utils.getCodecName(codec);
8581
+ if (!name)
8582
+ throw new Error('unknown hash codec');
8583
+ const size = reader.readVarUint();
8584
+ if (size !== reader.remaining)
8585
+ throw new Error('hash length inconsistent');
8586
+ return { name, size, offset: reader.position };
8587
+ };
8588
+ const hasHashHeader = (buffer) => {
8589
+ const reader = new BinaryReader(buffer);
8590
+ try {
8591
+ // Recognition is deliberately separate from strict parsing: a padded known
8592
+ // codec must reach decode() and fail, not silently become raw content.
8593
+ return Boolean(utils.getCodecName(reader.readVarUint(false)));
8594
+ }
8595
+ catch (error) {
8596
+ if (!(error instanceof RangeError))
8597
+ throw error;
8598
+ return false;
8599
+ }
8600
+ };
8472
8601
  class CodecHash extends BasicInterface {
8473
8602
  codec;
8474
8603
  discoCodec;
8475
8604
  size;
8605
+ digest;
8476
8606
  codecs;
8477
8607
  constructor(options = { name: 'disco-hash', codecs: {} }) {
8478
8608
  super();
@@ -8480,21 +8610,14 @@ class CodecHash extends BasicInterface {
8480
8610
  this.name = options.name;
8481
8611
  else
8482
8612
  this.name = 'disco-hash';
8483
- if (options?.codecs)
8484
- this.codecs = options.codecs;
8613
+ this.codecs = options?.codecs ?? {};
8485
8614
  }
8486
8615
  async init(uint8Array) {
8487
8616
  if (uint8Array) {
8488
8617
  if (!(uint8Array instanceof Uint8Array)) {
8489
8618
  throw new Error('CodecHash only supports Uint8Array input');
8490
8619
  }
8491
- // For large buffers, only check first bytes for codec prefix
8492
- // Codec prefixes are small (varint encoded), so 100 bytes is more than enough
8493
- const checkBuffer = uint8Array.length > 100 ? uint8Array.subarray(0, 100) : uint8Array;
8494
- this.discoCodec = new Codec$1(checkBuffer);
8495
- const name = this.discoCodec.name;
8496
- if (name) {
8497
- this.name = name;
8620
+ if (hasHashHeader(uint8Array)) {
8498
8621
  this.decode(uint8Array);
8499
8622
  }
8500
8623
  else {
@@ -8503,15 +8626,19 @@ class CodecHash extends BasicInterface {
8503
8626
  }
8504
8627
  return this;
8505
8628
  }
8629
+ #clearEncodingCache() {
8630
+ for (const key of ['_bs32', '_hex', '_string'])
8631
+ delete this[key];
8632
+ }
8506
8633
  get prefix() {
8507
8634
  const length = this.length;
8508
8635
  const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length);
8509
- uint8Array.set(length);
8510
- uint8Array.set(this.discoCodec.codecBuffer, length.length);
8636
+ uint8Array.set(this.discoCodec.codecBuffer);
8637
+ uint8Array.set(length, this.discoCodec.codecBuffer.length);
8511
8638
  return uint8Array;
8512
8639
  }
8513
8640
  get length() {
8514
- return index$3.encode(this.size);
8641
+ return new BinaryWriter(varUintLength(this.size)).writeVarUint(this.size).finish();
8515
8642
  }
8516
8643
  get buffer() {
8517
8644
  return this.encoded;
@@ -8538,6 +8665,8 @@ class CodecHash extends BasicInterface {
8538
8665
  const uint8Array = new Uint8Array(this.digest.length + this.prefix.length);
8539
8666
  uint8Array.set(this.prefix);
8540
8667
  uint8Array.set(this.digest, this.prefix.length);
8668
+ this.#clearEncodingCache();
8669
+ this.decoded = undefined;
8541
8670
  this.encoded = uint8Array;
8542
8671
  return this.encoded;
8543
8672
  }
@@ -8545,35 +8674,30 @@ class CodecHash extends BasicInterface {
8545
8674
  if (!(buffer instanceof Uint8Array)) {
8546
8675
  throw new Error('CodecHash only supports Uint8Array input');
8547
8676
  }
8548
- const codec = index$3.decode(buffer);
8549
- if (this.codecs[codec]) {
8550
- this.decode(buffer);
8551
- }
8552
- else {
8553
- await this.encode(buffer);
8554
- }
8677
+ await this.init(buffer);
8555
8678
  }
8556
8679
  decode(buffer) {
8680
+ buffer = buffer ?? this.encoded;
8681
+ // Validate completely before changing this instance, including cached encodings.
8682
+ const header = hashHeader(buffer);
8683
+ const codec = new Codec$1(header.name);
8684
+ const digest = new Uint8Array(buffer.subarray(header.offset));
8685
+ this.#clearEncodingCache();
8557
8686
  this.encoded = buffer;
8558
- const codec = index$3.decode(buffer);
8559
- this.discoCodec = new Codec$1(codec, this.codecs);
8560
- // TODO: validate codec
8561
- buffer = buffer.slice(index$3.decode.bytes);
8562
- this.size = index$3.decode(buffer);
8563
- this.digest = buffer.slice(index$3.decode.bytes);
8564
- if (this.digest.length !== this.size) {
8565
- throw new Error(`hash length inconsistent: 0x${this.encoded.toString()}`);
8566
- }
8567
- // const discoCodec = new Codec(codec, this.codecs)
8568
- this.name = this.discoCodec.name;
8569
- this.size = this.digest.length;
8570
- return {
8687
+ this.discoCodec = codec;
8688
+ this.codec = codec.codecBuffer;
8689
+ this.name = header.name;
8690
+ this.size = header.size;
8691
+ this.digest = digest;
8692
+ const decoded = {
8571
8693
  codec: this.codec,
8572
8694
  name: this.name,
8573
8695
  size: this.size,
8574
8696
  length: this.length,
8575
8697
  digest: this.digest
8576
8698
  };
8699
+ this.decoded = decoded;
8700
+ return decoded;
8577
8701
  }
8578
8702
  }
8579
8703
 
@@ -8686,13 +8810,14 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
8686
8810
  !Object.prototype.hasOwnProperty.call(this.decoded, 'hash')) {
8687
8811
  // @ts-ignore
8688
8812
  const hash = new CodecHash({ name: this.name });
8689
- return hash.init(this.contentBytes);
8813
+ // These are explicitly raw message bytes, even if they resemble a hash header.
8814
+ return hash.encode(this.contentBytes).then(() => hash);
8690
8815
  }
8691
8816
  // Fallback: must re-encode without hash property
8692
8817
  const decoded = this.beforeHashing({ ...this.decoded });
8693
8818
  // @ts-ignore
8694
8819
  const hash = new CodecHash({ name: this.name });
8695
- return hash.init(decoded);
8820
+ return hash.encode(this.protoEncode(decoded)).then(() => hash);
8696
8821
  }
8697
8822
  /**
8698
8823
  * @return {peernetHash}
@@ -8890,4 +9015,4 @@ const createHash = (algorithm) => {
8890
9015
  };
8891
9016
  };
8892
9017
 
8893
- export { ContractMessage as C, FormatInterface as F, TransactionMessage as T, proto$1 as a, proto as b, jsonParseBigInt$1 as c, Codec as d, proto$3 as e, index$1 as f, createRIPEMD160 as g, createHMAC as h, index$2 as i, jsonStringifyBigInt$1 as j, createSHA512 as k, createKeccak as l, index$3 as m, createHash as n, proto$7 as o, proto$2 as p, proto$8 as q, proto$5 as r };
9018
+ export { BinaryWriter as B, ContractMessage as C, FormatInterface as F, TransactionMessage as T, proto$1 as a, proto as b, jsonParseBigInt$1 as c, Codec as d, proto$3 as e, index$1 as f, createRIPEMD160 as g, createHMAC as h, index$2 as i, jsonStringifyBigInt$1 as j, createSHA512 as k, createKeccak as l, BinaryReader as m, createHash as n, proto$7 as o, proto$2 as p, proto$8 as q, encodeFrames as r, decodeFrames as s, proto$5 as t, varUintLength as v };