@taquito/timelock 24.3.1-beta.1 → 25.0.0-beta.1

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.
@@ -5,22 +5,62 @@
5
5
  })(this, (function (exports, nacl, bigInt) { 'use strict';
6
6
 
7
7
  /**
8
- * Utilities for hex, bytes, CSPRNG.
9
- * @module
8
+ * Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
9
+ * @param a - value to test
10
+ * @returns `true` when the value is a Uint8Array-compatible view.
11
+ * @example
12
+ * Check whether a value is a Uint8Array-compatible view.
13
+ * ```ts
14
+ * isBytes(new Uint8Array([1, 2, 3]));
15
+ * ```
10
16
  */
11
- /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
12
- /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
13
17
  function isBytes(a) {
14
- return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
18
+ // Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
19
+ // The fallback still requires a real ArrayBuffer view, so plain
20
+ // JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
21
+ // `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
22
+ return (a instanceof Uint8Array ||
23
+ (ArrayBuffer.isView(a) &&
24
+ a.constructor.name === 'Uint8Array' &&
25
+ 'BYTES_PER_ELEMENT' in a &&
26
+ a.BYTES_PER_ELEMENT === 1));
15
27
  }
16
- /** Asserts something is positive integer. */
28
+ /**
29
+ * Asserts something is a non-negative integer.
30
+ * @param n - number to validate
31
+ * @param title - label included in thrown errors
32
+ * @throws On wrong argument types. {@link TypeError}
33
+ * @throws On wrong argument ranges or values. {@link RangeError}
34
+ * @example
35
+ * Validate a non-negative integer option.
36
+ * ```ts
37
+ * anumber(32, 'length');
38
+ * ```
39
+ */
17
40
  function anumber(n, title = '') {
41
+ if (typeof n !== 'number') {
42
+ const prefix = title && `"${title}" `;
43
+ throw new TypeError(`${prefix}expected number, got ${typeof n}`);
44
+ }
18
45
  if (!Number.isSafeInteger(n) || n < 0) {
19
46
  const prefix = title && `"${title}" `;
20
- throw new Error(`${prefix}expected integer >= 0, got ${n}`);
47
+ throw new RangeError(`${prefix}expected integer >= 0, got ${n}`);
21
48
  }
22
49
  }
23
- /** Asserts something is Uint8Array. */
50
+ /**
51
+ * Asserts something is Uint8Array.
52
+ * @param value - value to validate
53
+ * @param length - optional exact length constraint
54
+ * @param title - label included in thrown errors
55
+ * @returns The validated byte array.
56
+ * @throws On wrong argument types. {@link TypeError}
57
+ * @throws On wrong argument ranges or values. {@link RangeError}
58
+ * @example
59
+ * Validate that a value is a byte array.
60
+ * ```ts
61
+ * abytes(new Uint8Array([1, 2, 3]));
62
+ * ```
63
+ */
24
64
  function abytes(value, length, title = '') {
25
65
  const bytes = isBytes(value);
26
66
  const len = value?.length;
@@ -29,64 +69,171 @@
29
69
  const prefix = title && `"${title}" `;
30
70
  const ofLen = needsLen ? ` of length ${length}` : '';
31
71
  const got = bytes ? `length=${len}` : `type=${typeof value}`;
32
- throw new Error(prefix + 'expected Uint8Array' + ofLen + ', got ' + got);
72
+ const message = prefix + 'expected Uint8Array' + ofLen + ', got ' + got;
73
+ if (!bytes)
74
+ throw new TypeError(message);
75
+ throw new RangeError(message);
33
76
  }
34
77
  return value;
35
78
  }
36
- /** Asserts a hash instance has not been destroyed / finished */
79
+ /**
80
+ * Asserts a hash instance has not been destroyed or finished.
81
+ * @param instance - hash instance to validate
82
+ * @param checkFinished - whether to reject finalized instances
83
+ * @throws If the hash instance has already been destroyed or finalized. {@link Error}
84
+ * @example
85
+ * Validate that a hash instance is still usable.
86
+ * ```ts
87
+ * import { aexists } from '@noble/hashes/utils.js';
88
+ * import { sha256 } from '@noble/hashes/sha2.js';
89
+ * const hash = sha256.create();
90
+ * aexists(hash);
91
+ * ```
92
+ */
37
93
  function aexists(instance, checkFinished = true) {
38
94
  if (instance.destroyed)
39
95
  throw new Error('Hash instance has been destroyed');
40
96
  if (checkFinished && instance.finished)
41
97
  throw new Error('Hash#digest() has already been called');
42
98
  }
43
- /** Asserts output is properly-sized byte array */
99
+ /**
100
+ * Asserts output is a sufficiently-sized byte array.
101
+ * @param out - destination buffer
102
+ * @param instance - hash instance providing output length
103
+ * Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
104
+ * @throws On wrong argument types. {@link TypeError}
105
+ * @throws On wrong argument ranges or values. {@link RangeError}
106
+ * @example
107
+ * Validate a caller-provided digest buffer.
108
+ * ```ts
109
+ * import { aoutput } from '@noble/hashes/utils.js';
110
+ * import { sha256 } from '@noble/hashes/sha2.js';
111
+ * const hash = sha256.create();
112
+ * aoutput(new Uint8Array(hash.outputLen), hash);
113
+ * ```
114
+ */
44
115
  function aoutput(out, instance) {
45
116
  abytes(out, undefined, 'digestInto() output');
46
117
  const min = instance.outputLen;
47
118
  if (out.length < min) {
48
- throw new Error('"digestInto() output" expected to be of length >=' + min);
119
+ throw new RangeError('"digestInto() output" expected to be of length >=' + min);
49
120
  }
50
121
  }
51
- /** Cast u8 / u16 / u32 to u32. */
122
+ /**
123
+ * Casts a typed array view to Uint32Array.
124
+ * `arr.byteOffset` must already be 4-byte aligned or the platform
125
+ * Uint32Array constructor will throw.
126
+ * @param arr - source typed array
127
+ * @returns Uint32Array view over the same buffer.
128
+ * @example
129
+ * Reinterpret a byte array as 32-bit words.
130
+ * ```ts
131
+ * u32(new Uint8Array(8));
132
+ * ```
133
+ */
52
134
  function u32(arr) {
53
135
  return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
54
136
  }
55
- /** Zeroize a byte array. Warning: JS provides no guarantees. */
137
+ /**
138
+ * Zeroizes typed arrays in place. Warning: JS provides no guarantees.
139
+ * @param arrays - arrays to overwrite with zeros
140
+ * @example
141
+ * Zeroize sensitive buffers in place.
142
+ * ```ts
143
+ * clean(new Uint8Array([1, 2, 3]));
144
+ * ```
145
+ */
56
146
  function clean(...arrays) {
57
147
  for (let i = 0; i < arrays.length; i++) {
58
148
  arrays[i].fill(0);
59
149
  }
60
150
  }
61
- /** Is current platform little-endian? Most are. Big-Endian platform: IBM */
151
+ /** Whether the current platform is little-endian. */
62
152
  const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
63
- /** The byte swap operation for uint32 */
153
+ /**
154
+ * Byte-swap operation for uint32 values.
155
+ * @param word - source word
156
+ * @returns Word with reversed byte order.
157
+ * @example
158
+ * Reverse the byte order of a 32-bit word.
159
+ * ```ts
160
+ * byteSwap(0x11223344);
161
+ * ```
162
+ */
64
163
  function byteSwap(word) {
65
164
  return (((word << 24) & 0xff000000) |
66
165
  ((word << 8) & 0xff0000) |
67
166
  ((word >>> 8) & 0xff00) |
68
167
  ((word >>> 24) & 0xff));
69
168
  }
70
- /** Conditionally byte swap if on a big-endian platform */
169
+ /**
170
+ * Conditionally byte-swaps one 32-bit word on big-endian platforms.
171
+ * @param n - source word
172
+ * @returns Original or byte-swapped word depending on platform endianness.
173
+ * @example
174
+ * Normalize a 32-bit word for host endianness.
175
+ * ```ts
176
+ * swap8IfBE(0x11223344);
177
+ * ```
178
+ */
71
179
  const swap8IfBE = isLE
72
180
  ? (n) => n
73
- : (n) => byteSwap(n);
74
- /** In place byte swap for Uint32Array */
181
+ : (n) => byteSwap(n) >>> 0;
182
+ /**
183
+ * Byte-swaps every word of a Uint32Array in place.
184
+ * @param arr - array to mutate
185
+ * @returns The same array after mutation; callers pass live state arrays here.
186
+ * @example
187
+ * Reverse the byte order of every word in place.
188
+ * ```ts
189
+ * byteSwap32(new Uint32Array([0x11223344]));
190
+ * ```
191
+ */
75
192
  function byteSwap32(arr) {
76
193
  for (let i = 0; i < arr.length; i++) {
77
194
  arr[i] = byteSwap(arr[i]);
78
195
  }
79
196
  return arr;
80
197
  }
198
+ /**
199
+ * Conditionally byte-swaps a Uint32Array on big-endian platforms.
200
+ * @param u - array to normalize for host endianness
201
+ * @returns Original or byte-swapped array depending on platform endianness.
202
+ * On big-endian runtimes this mutates `u` in place via `byteSwap32(...)`.
203
+ * @example
204
+ * Normalize a word array for host endianness.
205
+ * ```ts
206
+ * swap32IfBE(new Uint32Array([0x11223344]));
207
+ * ```
208
+ */
81
209
  const swap32IfBE = isLE
82
210
  ? (u) => u
83
211
  : byteSwap32;
84
- /** Creates function with outputLen, blockLen, create properties from a class constructor. */
212
+ /**
213
+ * Creates a callable hash function from a stateful class constructor.
214
+ * @param hashCons - hash constructor or factory
215
+ * @param info - optional metadata such as DER OID
216
+ * @returns Frozen callable hash wrapper with `.create()`.
217
+ * Wrapper construction eagerly calls `hashCons(undefined)` once to read
218
+ * `outputLen` / `blockLen`, so constructor side effects happen at module
219
+ * init time.
220
+ * @example
221
+ * Wrap a stateful hash constructor into a callable helper.
222
+ * ```ts
223
+ * import { createHasher } from '@noble/hashes/utils.js';
224
+ * import { sha256 } from '@noble/hashes/sha2.js';
225
+ * const wrapped = createHasher(sha256.create, { oid: sha256.oid });
226
+ * wrapped(new Uint8Array([1]));
227
+ * ```
228
+ */
85
229
  function createHasher(hashCons, info = {}) {
86
- const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
230
+ const hashC = (msg, opts) => hashCons(opts)
231
+ .update(msg)
232
+ .digest();
87
233
  const tmp = hashCons(undefined);
88
234
  hashC.outputLen = tmp.outputLen;
89
235
  hashC.blockLen = tmp.blockLen;
236
+ hashC.canXOF = tmp.canXOF;
90
237
  hashC.create = (opts) => hashCons(opts);
91
238
  Object.assign(hashC, info);
92
239
  return Object.freeze(hashC);
@@ -97,8 +244,10 @@
97
244
  * @module
98
245
  */
99
246
  /**
100
- * Internal blake variable.
101
- * For BLAKE2b, the two extra permutations for rounds 10 and 11 are SIGMA[10..11] = SIGMA[0..1].
247
+ * Internal blake permutation table.
248
+ * Rows `0..9` serve BLAKE2s, rows `0..11` serve BLAKE2b with `10..11 = 0..1`, and Blake1 also
249
+ * reuses the later rows shown below. Blake1 expands rounds `10..15` as `SIGMA[i % 10]`, so rows
250
+ * `10..15` intentionally repeat rows `0..5` for the 14-round (256) and 16-round (512) variants.
102
251
  */
103
252
  // prettier-ignore
104
253
  const BSIGMA = /* @__PURE__ */ Uint8Array.from([
@@ -121,35 +270,38 @@
121
270
  2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
122
271
  ]);
123
272
 
124
- /**
125
- * Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
126
- * @todo re-check https://issues.chromium.org/issues/42212588
127
- * @module
128
- */
129
273
  const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
130
274
  const _32n = /* @__PURE__ */ BigInt(32);
275
+ // Split bigint into two 32-bit halves. With `le=true`, returned fields become `{ h: low, l: high
276
+ // }` to match little-endian word order rather than the property names.
131
277
  function fromBig(n, le = false) {
132
278
  if (le)
133
279
  return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
134
280
  return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
135
281
  }
136
- // Right rotate for Shift in [1, 32)
282
+ // High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
137
283
  const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
284
+ // Low 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
138
285
  const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
139
- // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
286
+ // High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
140
287
  const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
288
+ // Low 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
141
289
  const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
142
- // Right rotate for shift===32 (just swaps l&h)
290
+ // High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
143
291
  const rotr32H = (_h, l) => l;
292
+ // Low 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped high half.
144
293
  const rotr32L = (h, _l) => h;
145
- // JS uses 32-bit signed integers for bitwise operations which means we cannot
146
- // simple take carry out of low bit sum by shift, we need to use division.
294
+ // Add two split 64-bit words and return the split `{ h, l }` sum.
295
+ // JS uses 32-bit signed integers for bitwise operations, so we cannot simply shift the carry out
296
+ // of the low sum and instead use division.
147
297
  function add(Ah, Al, Bh, Bl) {
148
298
  const l = (Al >>> 0) + (Bl >>> 0);
149
299
  return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
150
300
  }
151
301
  // Addition with more than 2 elements
302
+ // Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
152
303
  const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
304
+ // High-word finalize step for 3-way addition; `low` must be the untruncated output of `add3L(...)`.
153
305
  const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
154
306
 
155
307
  /**
@@ -157,14 +309,15 @@
157
309
  * b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
158
310
  * @module
159
311
  */
160
- // Same as SHA512_IV, but swapped endianness: LE instead of BE. iv[1] is iv[0], etc.
312
+ // Same IV words as `SHA512_IV`, but endian-swapped into LE u32 low/high halves
313
+ // for the BLAKE2b u64 helpers below.
161
314
  const B2B_IV = /* @__PURE__ */ Uint32Array.from([
162
315
  0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
163
316
  0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
164
317
  ]);
165
- // Temporary buffer
318
+ // Shared synchronous BLAKE2b work vector as LE u32 low/high halves.
166
319
  const BBUF = /* @__PURE__ */ new Uint32Array(32);
167
- // Mixing function G splitted in two halfs
320
+ // BLAKE2b G mix split into two half-rounds over LE u32 low/high limbs.
168
321
  function G1b(a, b, c, d, msg, x) {
169
322
  // NOTE: V is LE here
170
323
  const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
@@ -189,6 +342,7 @@
189
342
  ((BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch));
190
343
  ((BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh));
191
344
  }
345
+ // Second half-round of the same LE-limb BLAKE2b G mix; `x` is the message word offset.
192
346
  function G2b(a, b, c, d, msg, x) {
193
347
  // NOTE: V is LE here
194
348
  const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
@@ -215,9 +369,11 @@
215
369
  }
216
370
  function checkBlake2Opts(outputLen, opts = {}, keyLen, saltLen, persLen) {
217
371
  anumber(keyLen);
218
- if (outputLen < 0 || outputLen > keyLen)
372
+ // RFC 7693 §2.1 requires digest length nn in 1..keyLen.
373
+ if (outputLen <= 0 || outputLen > keyLen)
219
374
  throw new Error('outputLen bigger than keyLen');
220
375
  const { key, salt, personalization } = opts;
376
+ // This API uses `undefined` for the RFC 7693 `kk = 0` case, so a provided key must be non-empty.
221
377
  if (key !== undefined && (key.length < 1 || key.length > keyLen))
222
378
  throw new Error('"key" expected to be undefined or of length=1..' + keyLen);
223
379
  if (salt !== undefined)
@@ -235,6 +391,7 @@
235
391
  pos = 0;
236
392
  blockLen;
237
393
  outputLen;
394
+ canXOF = false;
238
395
  constructor(blockLen, outputLen) {
239
396
  anumber(blockLen);
240
397
  anumber(outputLen);
@@ -264,7 +421,7 @@
264
421
  }
265
422
  const take = Math.min(blockLen - this.pos, len - pos);
266
423
  const dataOffset = offset + pos;
267
- // full block && aligned to 4 bytes && not last in input
424
+ // Zero-copy only for full, 4-byte-aligned, non-final blocks.
268
425
  if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
269
426
  const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
270
427
  swap32IfBE(data32);
@@ -292,18 +449,33 @@
292
449
  swap32IfBE(buffer32);
293
450
  this.compress(buffer32, 0, true);
294
451
  swap32IfBE(buffer32);
452
+ // Reject unaligned views explicitly instead of hiding them behind a full scratch copy.
453
+ if (out.byteOffset & 3)
454
+ throw new RangeError('"digestInto() output" expected 4-byte aligned byteOffset, got ' + out.byteOffset);
455
+ const state = this.get();
295
456
  const out32 = u32(out);
296
- this.get().forEach((v, i) => (out32[i] = swap8IfBE(v)));
457
+ const full = Math.floor(this.outputLen / 4);
458
+ for (let i = 0; i < full; i++)
459
+ out32[i] = swap8IfBE(state[i]);
460
+ const tail = this.outputLen % 4;
461
+ if (!tail)
462
+ return;
463
+ const off = full * 4;
464
+ const word = state[full];
465
+ for (let i = 0; i < tail; i++)
466
+ out[off + i] = word >>> (8 * i);
297
467
  }
298
468
  digest() {
299
469
  const { buffer, outputLen } = this;
300
470
  this.digestInto(buffer);
471
+ // Return a copy so callers do not alias the instance scratch buffer used during finalization.
301
472
  const res = buffer.slice(0, outputLen);
302
473
  this.destroy();
303
474
  return res;
304
475
  }
305
476
  _cloneInto(to) {
306
477
  const { buffer, length, finished, destroyed, outputLen, pos } = this;
478
+ // Recreate only `dkLen`; key/salt/personalization are already absorbed into the copied state.
307
479
  to ||= new this.constructor({ dkLen: outputLen });
308
480
  to.set(...this.get());
309
481
  to.buffer.set(buffer);
@@ -319,9 +491,9 @@
319
491
  return this._cloneInto();
320
492
  }
321
493
  }
322
- /** Internal blake2b hash class. */
494
+ /** Internal blake2b hash class with state stored as LE u32 low/high halves. */
323
495
  class _BLAKE2b extends _BLAKE2 {
324
- // Same as SHA-512, but LE
496
+ // Same IV words as SHA-512 / BLAKE2b, encoded as LE u32 low/high halves.
325
497
  v0l = B2B_IV[0] | 0;
326
498
  v0h = B2B_IV[1] | 0;
327
499
  v1l = B2B_IV[2] | 0;
@@ -348,6 +520,8 @@
348
520
  abytes(key, undefined, 'key');
349
521
  keyLength = key.length;
350
522
  }
523
+ // RFC 7693 §2.5: xor `p[0] = 0x0101kknn` into the low 32 bits of `h[0]`;
524
+ // the high 32 bits stay at `IV[0]`.
351
525
  this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
352
526
  if (salt !== undefined) {
353
527
  abytes(salt, undefined, 'salt');
@@ -409,6 +583,8 @@
409
583
  }
410
584
  let j = 0;
411
585
  const s = BSIGMA;
586
+ // SIGMA selects 64-bit message words; multiply by 2 because `msg` stores
587
+ // each word as [low32, high32].
412
588
  for (let i = 0; i < 12; i++) {
413
589
  G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
414
590
  G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
@@ -454,7 +630,15 @@
454
630
  /**
455
631
  * Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
456
632
  * @param msg - message that would be hashed
457
- * @param opts - dkLen output length, key for MAC mode, salt, personalization
633
+ * @param opts - Optional output, MAC, salt, and personalization settings.
634
+ * `dkLen` must be 1..64 bytes; `salt` and `personalization`, if present,
635
+ * must be 16 bytes each. See {@link Blake2Opts}.
636
+ * @returns Digest bytes.
637
+ * @example
638
+ * Hash a message with Blake2b.
639
+ * ```ts
640
+ * blake2b(new Uint8Array([97, 98, 99]));
641
+ * ```
458
642
  */
459
643
  const blake2b = /* @__PURE__ */ createHasher((opts) => new _BLAKE2b(opts));
460
644