@lukso/web-components 1.147.0 → 1.148.0

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.
@@ -0,0 +1,831 @@
1
+ function isHex(value, { strict = true } = {}) {
2
+ if (!value)
3
+ return false;
4
+ if (typeof value !== 'string')
5
+ return false;
6
+ return strict ? /^0x[0-9a-fA-F]*$/.test(value) : value.startsWith('0x');
7
+ }
8
+
9
+ /**
10
+ * @description Retrieves the size of the value (in bytes).
11
+ *
12
+ * @param value The value (hex or byte array) to retrieve the size of.
13
+ * @returns The size of the value (in bytes).
14
+ */
15
+ function size(value) {
16
+ if (isHex(value, { strict: false }))
17
+ return Math.ceil((value.length - 2) / 2);
18
+ return value.length;
19
+ }
20
+
21
+ const version = '2.33.0';
22
+
23
+ let errorConfig = {
24
+ getDocsUrl: ({ docsBaseUrl, docsPath = '', docsSlug, }) => docsPath
25
+ ? `${docsBaseUrl ?? 'https://viem.sh'}${docsPath}${docsSlug ? `#${docsSlug}` : ''}`
26
+ : undefined,
27
+ version: `viem@${version}`,
28
+ };
29
+ class BaseError extends Error {
30
+ constructor(shortMessage, args = {}) {
31
+ const details = (() => {
32
+ if (args.cause instanceof BaseError)
33
+ return args.cause.details;
34
+ if (args.cause?.message)
35
+ return args.cause.message;
36
+ return args.details;
37
+ })();
38
+ const docsPath = (() => {
39
+ if (args.cause instanceof BaseError)
40
+ return args.cause.docsPath || args.docsPath;
41
+ return args.docsPath;
42
+ })();
43
+ const docsUrl = errorConfig.getDocsUrl?.({ ...args, docsPath });
44
+ const message = [
45
+ shortMessage || 'An error occurred.',
46
+ '',
47
+ ...(args.metaMessages ? [...args.metaMessages, ''] : []),
48
+ ...(docsUrl ? [`Docs: ${docsUrl}`] : []),
49
+ ...(details ? [`Details: ${details}`] : []),
50
+ ...(errorConfig.version ? [`Version: ${errorConfig.version}`] : []),
51
+ ].join('\n');
52
+ super(message, args.cause ? { cause: args.cause } : undefined);
53
+ Object.defineProperty(this, "details", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ Object.defineProperty(this, "docsPath", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ Object.defineProperty(this, "metaMessages", {
66
+ enumerable: true,
67
+ configurable: true,
68
+ writable: true,
69
+ value: void 0
70
+ });
71
+ Object.defineProperty(this, "shortMessage", {
72
+ enumerable: true,
73
+ configurable: true,
74
+ writable: true,
75
+ value: void 0
76
+ });
77
+ Object.defineProperty(this, "version", {
78
+ enumerable: true,
79
+ configurable: true,
80
+ writable: true,
81
+ value: void 0
82
+ });
83
+ Object.defineProperty(this, "name", {
84
+ enumerable: true,
85
+ configurable: true,
86
+ writable: true,
87
+ value: 'BaseError'
88
+ });
89
+ this.details = details;
90
+ this.docsPath = docsPath;
91
+ this.metaMessages = args.metaMessages;
92
+ this.name = args.name ?? this.name;
93
+ this.shortMessage = shortMessage;
94
+ this.version = version;
95
+ }
96
+ walk(fn) {
97
+ return walk(this, fn);
98
+ }
99
+ }
100
+ function walk(err, fn) {
101
+ if (fn?.(err))
102
+ return err;
103
+ if (err &&
104
+ typeof err === 'object' &&
105
+ 'cause' in err &&
106
+ err.cause !== undefined)
107
+ return walk(err.cause, fn);
108
+ return fn ? null : err;
109
+ }
110
+
111
+ class SizeExceedsPaddingSizeError extends BaseError {
112
+ constructor({ size, targetSize, type, }) {
113
+ super(`${type.charAt(0).toUpperCase()}${type
114
+ .slice(1)
115
+ .toLowerCase()} size (${size}) exceeds padding size (${targetSize}).`, { name: 'SizeExceedsPaddingSizeError' });
116
+ }
117
+ }
118
+
119
+ function pad(hexOrBytes, { dir, size = 32 } = {}) {
120
+ if (typeof hexOrBytes === 'string')
121
+ return padHex(hexOrBytes, { dir, size });
122
+ return padBytes(hexOrBytes, { dir, size });
123
+ }
124
+ function padHex(hex_, { dir, size = 32 } = {}) {
125
+ if (size === null)
126
+ return hex_;
127
+ const hex = hex_.replace('0x', '');
128
+ if (hex.length > size * 2)
129
+ throw new SizeExceedsPaddingSizeError({
130
+ size: Math.ceil(hex.length / 2),
131
+ targetSize: size,
132
+ type: 'hex',
133
+ });
134
+ return `0x${hex[dir === 'right' ? 'padEnd' : 'padStart'](size * 2, '0')}`;
135
+ }
136
+ function padBytes(bytes, { dir, size = 32 } = {}) {
137
+ if (size === null)
138
+ return bytes;
139
+ if (bytes.length > size)
140
+ throw new SizeExceedsPaddingSizeError({
141
+ size: bytes.length,
142
+ targetSize: size,
143
+ type: 'bytes',
144
+ });
145
+ const paddedBytes = new Uint8Array(size);
146
+ for (let i = 0; i < size; i++) {
147
+ const padEnd = dir === 'right';
148
+ paddedBytes[padEnd ? i : size - i - 1] =
149
+ bytes[padEnd ? i : bytes.length - i - 1];
150
+ }
151
+ return paddedBytes;
152
+ }
153
+
154
+ class IntegerOutOfRangeError extends BaseError {
155
+ constructor({ max, min, signed, size, value, }) {
156
+ super(`Number "${value}" is not in safe ${size ? `${size * 8}-bit ${signed ? 'signed' : 'unsigned'} ` : ''}integer range ${max ? `(${min} to ${max})` : `(above ${min})`}`, { name: 'IntegerOutOfRangeError' });
157
+ }
158
+ }
159
+ class SizeOverflowError extends BaseError {
160
+ constructor({ givenSize, maxSize }) {
161
+ super(`Size cannot exceed ${maxSize} bytes. Given size: ${givenSize} bytes.`, { name: 'SizeOverflowError' });
162
+ }
163
+ }
164
+
165
+ function assertSize(hexOrBytes, { size: size$1 }) {
166
+ if (size(hexOrBytes) > size$1)
167
+ throw new SizeOverflowError({
168
+ givenSize: size(hexOrBytes),
169
+ maxSize: size$1,
170
+ });
171
+ }
172
+
173
+ /**
174
+ * Encodes a number or bigint into a hex string
175
+ *
176
+ * - Docs: https://viem.sh/docs/utilities/toHex#numbertohex
177
+ *
178
+ * @param value Value to encode.
179
+ * @param opts Options.
180
+ * @returns Hex value.
181
+ *
182
+ * @example
183
+ * import { numberToHex } from 'viem'
184
+ * const data = numberToHex(420)
185
+ * // '0x1a4'
186
+ *
187
+ * @example
188
+ * import { numberToHex } from 'viem'
189
+ * const data = numberToHex(420, { size: 32 })
190
+ * // '0x00000000000000000000000000000000000000000000000000000000000001a4'
191
+ */
192
+ function numberToHex(value_, opts = {}) {
193
+ const { signed, size } = opts;
194
+ const value = BigInt(value_);
195
+ let maxValue;
196
+ if (size) {
197
+ if (signed)
198
+ maxValue = (1n << (BigInt(size) * 8n - 1n)) - 1n;
199
+ else
200
+ maxValue = 2n ** (BigInt(size) * 8n) - 1n;
201
+ }
202
+ else if (typeof value_ === 'number') {
203
+ maxValue = BigInt(Number.MAX_SAFE_INTEGER);
204
+ }
205
+ const minValue = typeof maxValue === 'bigint' && signed ? -maxValue - 1n : 0;
206
+ if ((maxValue && value > maxValue) || value < minValue) {
207
+ const suffix = typeof value_ === 'bigint' ? 'n' : '';
208
+ throw new IntegerOutOfRangeError({
209
+ max: maxValue ? `${maxValue}${suffix}` : undefined,
210
+ min: `${minValue}${suffix}`,
211
+ signed,
212
+ size,
213
+ value: `${value_}${suffix}`,
214
+ });
215
+ }
216
+ const hex = `0x${(signed && value < 0 ? (1n << BigInt(size * 8)) + BigInt(value) : value).toString(16)}`;
217
+ if (size)
218
+ return pad(hex, { size });
219
+ return hex;
220
+ }
221
+
222
+ const encoder = /*#__PURE__*/ new TextEncoder();
223
+ /**
224
+ * Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
225
+ *
226
+ * - Docs: https://viem.sh/docs/utilities/toBytes
227
+ * - Example: https://viem.sh/docs/utilities/toBytes#usage
228
+ *
229
+ * @param value Value to encode.
230
+ * @param opts Options.
231
+ * @returns Byte array value.
232
+ *
233
+ * @example
234
+ * import { toBytes } from 'viem'
235
+ * const data = toBytes('Hello world')
236
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
237
+ *
238
+ * @example
239
+ * import { toBytes } from 'viem'
240
+ * const data = toBytes(420)
241
+ * // Uint8Array([1, 164])
242
+ *
243
+ * @example
244
+ * import { toBytes } from 'viem'
245
+ * const data = toBytes(420, { size: 4 })
246
+ * // Uint8Array([0, 0, 1, 164])
247
+ */
248
+ function toBytes$1(value, opts = {}) {
249
+ if (typeof value === 'number' || typeof value === 'bigint')
250
+ return numberToBytes(value, opts);
251
+ if (typeof value === 'boolean')
252
+ return boolToBytes(value, opts);
253
+ if (isHex(value))
254
+ return hexToBytes(value, opts);
255
+ return stringToBytes(value, opts);
256
+ }
257
+ /**
258
+ * Encodes a boolean into a byte array.
259
+ *
260
+ * - Docs: https://viem.sh/docs/utilities/toBytes#booltobytes
261
+ *
262
+ * @param value Boolean value to encode.
263
+ * @param opts Options.
264
+ * @returns Byte array value.
265
+ *
266
+ * @example
267
+ * import { boolToBytes } from 'viem'
268
+ * const data = boolToBytes(true)
269
+ * // Uint8Array([1])
270
+ *
271
+ * @example
272
+ * import { boolToBytes } from 'viem'
273
+ * const data = boolToBytes(true, { size: 32 })
274
+ * // Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
275
+ */
276
+ function boolToBytes(value, opts = {}) {
277
+ const bytes = new Uint8Array(1);
278
+ bytes[0] = Number(value);
279
+ if (typeof opts.size === 'number') {
280
+ assertSize(bytes, { size: opts.size });
281
+ return pad(bytes, { size: opts.size });
282
+ }
283
+ return bytes;
284
+ }
285
+ // We use very optimized technique to convert hex string to byte array
286
+ const charCodeMap = {
287
+ zero: 48,
288
+ nine: 57,
289
+ A: 65,
290
+ F: 70,
291
+ a: 97,
292
+ f: 102,
293
+ };
294
+ function charCodeToBase16(char) {
295
+ if (char >= charCodeMap.zero && char <= charCodeMap.nine)
296
+ return char - charCodeMap.zero;
297
+ if (char >= charCodeMap.A && char <= charCodeMap.F)
298
+ return char - (charCodeMap.A - 10);
299
+ if (char >= charCodeMap.a && char <= charCodeMap.f)
300
+ return char - (charCodeMap.a - 10);
301
+ return undefined;
302
+ }
303
+ /**
304
+ * Encodes a hex string into a byte array.
305
+ *
306
+ * - Docs: https://viem.sh/docs/utilities/toBytes#hextobytes
307
+ *
308
+ * @param hex Hex string to encode.
309
+ * @param opts Options.
310
+ * @returns Byte array value.
311
+ *
312
+ * @example
313
+ * import { hexToBytes } from 'viem'
314
+ * const data = hexToBytes('0x48656c6c6f20776f726c6421')
315
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
316
+ *
317
+ * @example
318
+ * import { hexToBytes } from 'viem'
319
+ * const data = hexToBytes('0x48656c6c6f20776f726c6421', { size: 32 })
320
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
321
+ */
322
+ function hexToBytes(hex_, opts = {}) {
323
+ let hex = hex_;
324
+ if (opts.size) {
325
+ assertSize(hex, { size: opts.size });
326
+ hex = pad(hex, { dir: 'right', size: opts.size });
327
+ }
328
+ let hexString = hex.slice(2);
329
+ if (hexString.length % 2)
330
+ hexString = `0${hexString}`;
331
+ const length = hexString.length / 2;
332
+ const bytes = new Uint8Array(length);
333
+ for (let index = 0, j = 0; index < length; index++) {
334
+ const nibbleLeft = charCodeToBase16(hexString.charCodeAt(j++));
335
+ const nibbleRight = charCodeToBase16(hexString.charCodeAt(j++));
336
+ if (nibbleLeft === undefined || nibbleRight === undefined) {
337
+ throw new BaseError(`Invalid byte sequence ("${hexString[j - 2]}${hexString[j - 1]}" in "${hexString}").`);
338
+ }
339
+ bytes[index] = nibbleLeft * 16 + nibbleRight;
340
+ }
341
+ return bytes;
342
+ }
343
+ /**
344
+ * Encodes a number into a byte array.
345
+ *
346
+ * - Docs: https://viem.sh/docs/utilities/toBytes#numbertobytes
347
+ *
348
+ * @param value Number to encode.
349
+ * @param opts Options.
350
+ * @returns Byte array value.
351
+ *
352
+ * @example
353
+ * import { numberToBytes } from 'viem'
354
+ * const data = numberToBytes(420)
355
+ * // Uint8Array([1, 164])
356
+ *
357
+ * @example
358
+ * import { numberToBytes } from 'viem'
359
+ * const data = numberToBytes(420, { size: 4 })
360
+ * // Uint8Array([0, 0, 1, 164])
361
+ */
362
+ function numberToBytes(value, opts) {
363
+ const hex = numberToHex(value, opts);
364
+ return hexToBytes(hex);
365
+ }
366
+ /**
367
+ * Encodes a UTF-8 string into a byte array.
368
+ *
369
+ * - Docs: https://viem.sh/docs/utilities/toBytes#stringtobytes
370
+ *
371
+ * @param value String to encode.
372
+ * @param opts Options.
373
+ * @returns Byte array value.
374
+ *
375
+ * @example
376
+ * import { stringToBytes } from 'viem'
377
+ * const data = stringToBytes('Hello world!')
378
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
379
+ *
380
+ * @example
381
+ * import { stringToBytes } from 'viem'
382
+ * const data = stringToBytes('Hello world!', { size: 32 })
383
+ * // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
384
+ */
385
+ function stringToBytes(value, opts = {}) {
386
+ const bytes = encoder.encode(value);
387
+ if (typeof opts.size === 'number') {
388
+ assertSize(bytes, { size: opts.size });
389
+ return pad(bytes, { dir: 'right', size: opts.size });
390
+ }
391
+ return bytes;
392
+ }
393
+
394
+ /**
395
+ * Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
396
+ * @todo re-check https://issues.chromium.org/issues/42212588
397
+ * @module
398
+ */
399
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
400
+ const _32n = /* @__PURE__ */ BigInt(32);
401
+ function fromBig(n, le = false) {
402
+ if (le)
403
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
404
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
405
+ }
406
+ function split(lst, le = false) {
407
+ const len = lst.length;
408
+ let Ah = new Uint32Array(len);
409
+ let Al = new Uint32Array(len);
410
+ for (let i = 0; i < len; i++) {
411
+ const { h, l } = fromBig(lst[i], le);
412
+ [Ah[i], Al[i]] = [h, l];
413
+ }
414
+ return [Ah, Al];
415
+ }
416
+ // Left rotate for Shift in [1, 32)
417
+ const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
418
+ const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
419
+ // Left rotate for Shift in (32, 64), NOTE: 32 is special case.
420
+ const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
421
+ const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
422
+
423
+ /**
424
+ * Utilities for hex, bytes, CSPRNG.
425
+ * @module
426
+ */
427
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
428
+ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
429
+ // node.js versions earlier than v19 don't declare it in global scope.
430
+ // For node.js, package.json#exports field mapping rewrites import
431
+ // from `crypto` to `cryptoNode`, which imports native module.
432
+ // Makes the utils un-importable in browsers without a bundler.
433
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
434
+ /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
435
+ function isBytes(a) {
436
+ return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
437
+ }
438
+ /** Asserts something is positive integer. */
439
+ function anumber(n) {
440
+ if (!Number.isSafeInteger(n) || n < 0)
441
+ throw new Error('positive integer expected, got ' + n);
442
+ }
443
+ /** Asserts something is Uint8Array. */
444
+ function abytes(b, ...lengths) {
445
+ if (!isBytes(b))
446
+ throw new Error('Uint8Array expected');
447
+ if (lengths.length > 0 && !lengths.includes(b.length))
448
+ throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
449
+ }
450
+ /** Asserts a hash instance has not been destroyed / finished */
451
+ function aexists(instance, checkFinished = true) {
452
+ if (instance.destroyed)
453
+ throw new Error('Hash instance has been destroyed');
454
+ if (checkFinished && instance.finished)
455
+ throw new Error('Hash#digest() has already been called');
456
+ }
457
+ /** Asserts output is properly-sized byte array */
458
+ function aoutput(out, instance) {
459
+ abytes(out);
460
+ const min = instance.outputLen;
461
+ if (out.length < min) {
462
+ throw new Error('digestInto() expects output buffer of length at least ' + min);
463
+ }
464
+ }
465
+ /** Cast u8 / u16 / u32 to u32. */
466
+ function u32(arr) {
467
+ return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
468
+ }
469
+ /** Zeroize a byte array. Warning: JS provides no guarantees. */
470
+ function clean(...arrays) {
471
+ for (let i = 0; i < arrays.length; i++) {
472
+ arrays[i].fill(0);
473
+ }
474
+ }
475
+ /** Is current platform little-endian? Most are. Big-Endian platform: IBM */
476
+ const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
477
+ /** The byte swap operation for uint32 */
478
+ function byteSwap(word) {
479
+ return (((word << 24) & 0xff000000) |
480
+ ((word << 8) & 0xff0000) |
481
+ ((word >>> 8) & 0xff00) |
482
+ ((word >>> 24) & 0xff));
483
+ }
484
+ /** In place byte swap for Uint32Array */
485
+ function byteSwap32(arr) {
486
+ for (let i = 0; i < arr.length; i++) {
487
+ arr[i] = byteSwap(arr[i]);
488
+ }
489
+ return arr;
490
+ }
491
+ const swap32IfBE = isLE
492
+ ? (u) => u
493
+ : byteSwap32;
494
+ /**
495
+ * Converts string to bytes using UTF8 encoding.
496
+ * @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
497
+ */
498
+ function utf8ToBytes(str) {
499
+ if (typeof str !== 'string')
500
+ throw new Error('string expected');
501
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
502
+ }
503
+ /**
504
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
505
+ * Warning: when Uint8Array is passed, it would NOT get copied.
506
+ * Keep in mind for future mutable operations.
507
+ */
508
+ function toBytes(data) {
509
+ if (typeof data === 'string')
510
+ data = utf8ToBytes(data);
511
+ abytes(data);
512
+ return data;
513
+ }
514
+ /** For runtime check if class implements interface */
515
+ class Hash {
516
+ }
517
+ /** Wraps hash function, creating an interface on top of it */
518
+ function createHasher(hashCons) {
519
+ const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
520
+ const tmp = hashCons();
521
+ hashC.outputLen = tmp.outputLen;
522
+ hashC.blockLen = tmp.blockLen;
523
+ hashC.create = () => hashCons();
524
+ return hashC;
525
+ }
526
+
527
+ /**
528
+ * SHA3 (keccak) hash function, based on a new "Sponge function" design.
529
+ * Different from older hashes, the internal state is bigger than output size.
530
+ *
531
+ * Check out [FIPS-202](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf),
532
+ * [Website](https://keccak.team/keccak.html),
533
+ * [the differences between SHA-3 and Keccak](https://crypto.stackexchange.com/questions/15727/what-are-the-key-differences-between-the-draft-sha-3-standard-and-the-keccak-sub).
534
+ *
535
+ * Check out `sha3-addons` module for cSHAKE, k12, and others.
536
+ * @module
537
+ */
538
+ // No __PURE__ annotations in sha3 header:
539
+ // EVERYTHING is in fact used on every export.
540
+ // Various per round constants calculations
541
+ const _0n = BigInt(0);
542
+ const _1n = BigInt(1);
543
+ const _2n = BigInt(2);
544
+ const _7n = BigInt(7);
545
+ const _256n = BigInt(256);
546
+ const _0x71n = BigInt(0x71);
547
+ const SHA3_PI = [];
548
+ const SHA3_ROTL = [];
549
+ const _SHA3_IOTA = [];
550
+ for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
551
+ // Pi
552
+ [x, y] = [y, (2 * x + 3 * y) % 5];
553
+ SHA3_PI.push(2 * (5 * y + x));
554
+ // Rotational
555
+ SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
556
+ // Iota
557
+ let t = _0n;
558
+ for (let j = 0; j < 7; j++) {
559
+ R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
560
+ if (R & _2n)
561
+ t ^= _1n << ((_1n << /* @__PURE__ */ BigInt(j)) - _1n);
562
+ }
563
+ _SHA3_IOTA.push(t);
564
+ }
565
+ const IOTAS = split(_SHA3_IOTA, true);
566
+ const SHA3_IOTA_H = IOTAS[0];
567
+ const SHA3_IOTA_L = IOTAS[1];
568
+ // Left rotation (without 0, 32, 64)
569
+ const rotlH = (h, l, s) => (s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s));
570
+ const rotlL = (h, l, s) => (s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s));
571
+ /** `keccakf1600` internal function, additionally allows to adjust round count. */
572
+ function keccakP(s, rounds = 24) {
573
+ const B = new Uint32Array(5 * 2);
574
+ // NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
575
+ for (let round = 24 - rounds; round < 24; round++) {
576
+ // Theta θ
577
+ for (let x = 0; x < 10; x++)
578
+ B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
579
+ for (let x = 0; x < 10; x += 2) {
580
+ const idx1 = (x + 8) % 10;
581
+ const idx0 = (x + 2) % 10;
582
+ const B0 = B[idx0];
583
+ const B1 = B[idx0 + 1];
584
+ const Th = rotlH(B0, B1, 1) ^ B[idx1];
585
+ const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
586
+ for (let y = 0; y < 50; y += 10) {
587
+ s[x + y] ^= Th;
588
+ s[x + y + 1] ^= Tl;
589
+ }
590
+ }
591
+ // Rho (ρ) and Pi (π)
592
+ let curH = s[2];
593
+ let curL = s[3];
594
+ for (let t = 0; t < 24; t++) {
595
+ const shift = SHA3_ROTL[t];
596
+ const Th = rotlH(curH, curL, shift);
597
+ const Tl = rotlL(curH, curL, shift);
598
+ const PI = SHA3_PI[t];
599
+ curH = s[PI];
600
+ curL = s[PI + 1];
601
+ s[PI] = Th;
602
+ s[PI + 1] = Tl;
603
+ }
604
+ // Chi (χ)
605
+ for (let y = 0; y < 50; y += 10) {
606
+ for (let x = 0; x < 10; x++)
607
+ B[x] = s[y + x];
608
+ for (let x = 0; x < 10; x++)
609
+ s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
610
+ }
611
+ // Iota (ι)
612
+ s[0] ^= SHA3_IOTA_H[round];
613
+ s[1] ^= SHA3_IOTA_L[round];
614
+ }
615
+ clean(B);
616
+ }
617
+ /** Keccak sponge function. */
618
+ class Keccak extends Hash {
619
+ // NOTE: we accept arguments in bytes instead of bits here.
620
+ constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
621
+ super();
622
+ this.pos = 0;
623
+ this.posOut = 0;
624
+ this.finished = false;
625
+ this.destroyed = false;
626
+ this.enableXOF = false;
627
+ this.blockLen = blockLen;
628
+ this.suffix = suffix;
629
+ this.outputLen = outputLen;
630
+ this.enableXOF = enableXOF;
631
+ this.rounds = rounds;
632
+ // Can be passed from user as dkLen
633
+ anumber(outputLen);
634
+ // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
635
+ // 0 < blockLen < 200
636
+ if (!(0 < blockLen && blockLen < 200))
637
+ throw new Error('only keccak-f1600 function is supported');
638
+ this.state = new Uint8Array(200);
639
+ this.state32 = u32(this.state);
640
+ }
641
+ clone() {
642
+ return this._cloneInto();
643
+ }
644
+ keccak() {
645
+ swap32IfBE(this.state32);
646
+ keccakP(this.state32, this.rounds);
647
+ swap32IfBE(this.state32);
648
+ this.posOut = 0;
649
+ this.pos = 0;
650
+ }
651
+ update(data) {
652
+ aexists(this);
653
+ data = toBytes(data);
654
+ abytes(data);
655
+ const { blockLen, state } = this;
656
+ const len = data.length;
657
+ for (let pos = 0; pos < len;) {
658
+ const take = Math.min(blockLen - this.pos, len - pos);
659
+ for (let i = 0; i < take; i++)
660
+ state[this.pos++] ^= data[pos++];
661
+ if (this.pos === blockLen)
662
+ this.keccak();
663
+ }
664
+ return this;
665
+ }
666
+ finish() {
667
+ if (this.finished)
668
+ return;
669
+ this.finished = true;
670
+ const { state, suffix, pos, blockLen } = this;
671
+ // Do the padding
672
+ state[pos] ^= suffix;
673
+ if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
674
+ this.keccak();
675
+ state[blockLen - 1] ^= 0x80;
676
+ this.keccak();
677
+ }
678
+ writeInto(out) {
679
+ aexists(this, false);
680
+ abytes(out);
681
+ this.finish();
682
+ const bufferOut = this.state;
683
+ const { blockLen } = this;
684
+ for (let pos = 0, len = out.length; pos < len;) {
685
+ if (this.posOut >= blockLen)
686
+ this.keccak();
687
+ const take = Math.min(blockLen - this.posOut, len - pos);
688
+ out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
689
+ this.posOut += take;
690
+ pos += take;
691
+ }
692
+ return out;
693
+ }
694
+ xofInto(out) {
695
+ // Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
696
+ if (!this.enableXOF)
697
+ throw new Error('XOF is not possible for this instance');
698
+ return this.writeInto(out);
699
+ }
700
+ xof(bytes) {
701
+ anumber(bytes);
702
+ return this.xofInto(new Uint8Array(bytes));
703
+ }
704
+ digestInto(out) {
705
+ aoutput(out, this);
706
+ if (this.finished)
707
+ throw new Error('digest() was already called');
708
+ this.writeInto(out);
709
+ this.destroy();
710
+ return out;
711
+ }
712
+ digest() {
713
+ return this.digestInto(new Uint8Array(this.outputLen));
714
+ }
715
+ destroy() {
716
+ this.destroyed = true;
717
+ clean(this.state);
718
+ }
719
+ _cloneInto(to) {
720
+ const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
721
+ to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
722
+ to.state32.set(this.state32);
723
+ to.pos = this.pos;
724
+ to.posOut = this.posOut;
725
+ to.finished = this.finished;
726
+ to.rounds = rounds;
727
+ // Suffix can change in cSHAKE
728
+ to.suffix = suffix;
729
+ to.outputLen = outputLen;
730
+ to.enableXOF = enableXOF;
731
+ to.destroyed = this.destroyed;
732
+ return to;
733
+ }
734
+ }
735
+ const gen = (suffix, blockLen, outputLen) => createHasher(() => new Keccak(blockLen, suffix, outputLen));
736
+ /** keccak-256 hash function. Different from SHA3-256. */
737
+ const keccak_256 = /* @__PURE__ */ (() => gen(0x01, 136, 256 / 8))();
738
+
739
+ function keccak256(value, to_) {
740
+ const bytes = keccak_256(isHex(value, { strict: false }) ? toBytes$1(value) : value);
741
+ return bytes;
742
+ }
743
+
744
+ /**
745
+ * Map with a LRU (Least recently used) policy.
746
+ *
747
+ * @link https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
748
+ */
749
+ class LruMap extends Map {
750
+ constructor(size) {
751
+ super();
752
+ Object.defineProperty(this, "maxSize", {
753
+ enumerable: true,
754
+ configurable: true,
755
+ writable: true,
756
+ value: void 0
757
+ });
758
+ this.maxSize = size;
759
+ }
760
+ get(key) {
761
+ const value = super.get(key);
762
+ if (super.has(key) && value !== undefined) {
763
+ this.delete(key);
764
+ super.set(key, value);
765
+ }
766
+ return value;
767
+ }
768
+ set(key, value) {
769
+ super.set(key, value);
770
+ if (this.maxSize && this.size > this.maxSize) {
771
+ const firstKey = this.keys().next().value;
772
+ if (firstKey)
773
+ this.delete(firstKey);
774
+ }
775
+ return this;
776
+ }
777
+ }
778
+
779
+ const checksumAddressCache = /*#__PURE__*/ new LruMap(8192);
780
+ function checksumAddress(address_,
781
+ /**
782
+ * Warning: EIP-1191 checksum addresses are generally not backwards compatible with the
783
+ * wider Ethereum ecosystem, meaning it will break when validated against an application/tool
784
+ * that relies on EIP-55 checksum encoding (checksum without chainId).
785
+ *
786
+ * It is highly recommended to not use this feature unless you
787
+ * know what you are doing.
788
+ *
789
+ * See more: https://github.com/ethereum/EIPs/issues/1121
790
+ */
791
+ chainId) {
792
+ if (checksumAddressCache.has(`${address_}.${chainId}`))
793
+ return checksumAddressCache.get(`${address_}.${chainId}`);
794
+ const hexAddress = address_.substring(2).toLowerCase();
795
+ const hash = keccak256(stringToBytes(hexAddress));
796
+ const address = (hexAddress).split('');
797
+ for (let i = 0; i < 40; i += 2) {
798
+ if (hash[i >> 1] >> 4 >= 8 && address[i]) {
799
+ address[i] = address[i].toUpperCase();
800
+ }
801
+ if ((hash[i >> 1] & 0x0f) >= 8 && address[i + 1]) {
802
+ address[i + 1] = address[i + 1].toUpperCase();
803
+ }
804
+ }
805
+ const result = `0x${address.join('')}`;
806
+ checksumAddressCache.set(`${address_}.${chainId}`, result);
807
+ return result;
808
+ }
809
+
810
+ const addressRegex = /^0x[a-fA-F0-9]{40}$/;
811
+ /** @internal */
812
+ const isAddressCache = /*#__PURE__*/ new LruMap(8192);
813
+ function isAddress(address, options) {
814
+ const { strict = true } = {};
815
+ const cacheKey = `${address}.${strict}`;
816
+ if (isAddressCache.has(cacheKey))
817
+ return isAddressCache.get(cacheKey);
818
+ const result = (() => {
819
+ if (!addressRegex.test(address))
820
+ return false;
821
+ if (address.toLowerCase() === address)
822
+ return true;
823
+ if (strict)
824
+ return checksumAddress(address) === address;
825
+ return true;
826
+ })();
827
+ isAddressCache.set(cacheKey, result);
828
+ return result;
829
+ }
830
+
831
+ export { isAddress as i };