@leofcoin/chain 1.10.13 → 1.10.15
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/exports/beacon-envelope.js +2 -2
- package/exports/beacon-epoch.js +1 -1
- package/exports/beacon-lifecycle.js +2 -2
- package/exports/beacon-round.js +1 -1
- package/exports/beacon.js +1 -1
- package/exports/browser/beacon-envelope.js +79 -3
- package/exports/browser/beacon-epoch.js +2 -1
- package/exports/browser/beacon-lifecycle.js +3 -2
- package/exports/browser/beacon-round.js +2 -1
- package/exports/browser/beacon-wire.js +3 -3
- package/exports/browser/beacon.js +3 -2
- package/exports/browser/{browser-DvU1xNFS-sdlKNCJc.js → browser-DvU1xNFS-DmmNUdE-.js} +4 -4
- package/exports/browser/{proposal.proto-BcUgd885.js → browser-crypto-B9N9KlL4.js} +35 -6
- package/exports/browser/chain.js +16 -295
- package/exports/browser/{client-B-jyclOB-CsNYfj4Z.js → client-DDC8C510-3uaB70KL.js} +36 -12
- package/exports/browser/connection-monitor-GPztgSSJ.js +454 -0
- package/exports/browser/hkdf-CnZvwpZ9.js +222 -0
- package/exports/browser/{index-Bxr5Iztg-C6xBk0rK.js → index-Bxr5Iztg-WbFIosyf.js} +4 -4
- package/exports/browser/{index-CFBGfpG1.js → index-Ccn5sExe.js} +1 -2
- package/exports/browser/{messages-FMRAS8QX-CvlZBzy5.js → messages-DH0tgNM9-C2CtyLsf.js} +4 -4
- package/exports/browser/{node-browser-xlqSBOaN.js → node-browser-BYi7D_oa.js} +6 -6
- package/exports/browser/node-browser.js +4 -4
- package/exports/browser/sha2-piimb6xL.js +614 -0
- package/exports/browser/{weierstrass-C-jX_jly.js → weierstrass-Q0QND1ZT.js} +1 -1
- package/exports/browser/workers/machine-worker.js +412 -135
- package/exports/chain.js +13 -291
- package/exports/connection-monitor.js +324 -0
- package/exports/node.js +7 -0
- package/exports/workers/machine-worker.js +412 -135
- package/package.json +8 -4
- package/exports/browser/browser-CWeoyGUw-BabtHowB.js +0 -57
- package/exports/browser/constants-Cv0p224A.js +0 -130
- package/exports/browser/hkdf-DhdhLAAv.js +0 -147
|
@@ -6434,201 +6434,446 @@ const instrumentContractSource = (source) => {
|
|
|
6434
6434
|
return output;
|
|
6435
6435
|
};
|
|
6436
6436
|
|
|
6437
|
-
|
|
6438
|
-
|
|
6439
|
-
|
|
6440
|
-
|
|
6441
|
-
|
|
6437
|
+
// Split a JS number into u32 halves without a BigInt allocation. Exact only for integers
|
|
6438
|
+
// `0 <= n < 2**53`; callers use it on byte / bit counters, which JS length math caps far below
|
|
6439
|
+
// that (an ArrayBuffer cannot exceed 2**53 - 1 bytes).
|
|
6440
|
+
const fromNumH = (n) => (n / 2 ** 32) | 0;
|
|
6441
|
+
const fromNumL = (n) => n >>> 0;
|
|
6442
|
+
// Drop-in replacement for `view.setBigUint64(byteOffset, BigInt(n), isLE)` without the per-call
|
|
6443
|
+
// BigInt allocation. Same `n < 2**53` precondition as `fromNumH`/`fromNumL`.
|
|
6444
|
+
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
6445
|
+
const h = fromNumH(n);
|
|
6446
|
+
const l = fromNumL(n);
|
|
6447
|
+
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
6448
|
+
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
6442
6449
|
}
|
|
6443
|
-
|
|
6450
|
+
|
|
6451
|
+
/**
|
|
6452
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
6453
|
+
* @param a - value to test
|
|
6454
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
6455
|
+
* @example
|
|
6456
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
6457
|
+
* ```ts
|
|
6458
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
6459
|
+
* ```
|
|
6460
|
+
*/
|
|
6461
|
+
function isBytes(a) {
|
|
6462
|
+
// Plain `instanceof Uint8Array` is too strict for some Buffer / proxy / cross-realm cases.
|
|
6463
|
+
// The fallback still requires a real ArrayBuffer view, so plain
|
|
6464
|
+
// JSON-deserialized `{ constructor: ... }` spoofing is rejected, and
|
|
6465
|
+
// `BYTES_PER_ELEMENT === 1` keeps the fallback on byte-oriented views.
|
|
6466
|
+
return (a instanceof Uint8Array ||
|
|
6467
|
+
(ArrayBuffer.isView(a) &&
|
|
6468
|
+
a.constructor.name === 'Uint8Array' &&
|
|
6469
|
+
'BYTES_PER_ELEMENT' in a &&
|
|
6470
|
+
a.BYTES_PER_ELEMENT === 1));
|
|
6471
|
+
}
|
|
6472
|
+
// Shared error-message prefix builder. Only called on throw paths, so assert
|
|
6473
|
+
// success paths never pay for the string concatenation.
|
|
6474
|
+
const atitle = (title) => (title ? `"${title}" ` : '');
|
|
6475
|
+
/**
|
|
6476
|
+
* Asserts something is Uint8Array.
|
|
6477
|
+
* @param value - value to validate
|
|
6478
|
+
* @param length - optional exact length constraint
|
|
6479
|
+
* @param title - label included in thrown errors
|
|
6480
|
+
* @returns The validated byte array.
|
|
6481
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
6482
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
6483
|
+
* @example
|
|
6484
|
+
* Validate that a value is a byte array.
|
|
6485
|
+
* ```ts
|
|
6486
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
6487
|
+
* ```
|
|
6488
|
+
*/
|
|
6489
|
+
function abytes(value, length, title = '') {
|
|
6490
|
+
// Success path first: this runs at the start of every update() / digestInto(), and the
|
|
6491
|
+
// common `abytes(data)` form must not pay for length handling it does not use.
|
|
6492
|
+
if (isBytes(value) && (length === undefined))
|
|
6493
|
+
return value;
|
|
6494
|
+
const bytes = isBytes(value);
|
|
6495
|
+
const ofLen = '';
|
|
6496
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
6497
|
+
const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
6498
|
+
if (!bytes)
|
|
6499
|
+
throw new TypeError(message);
|
|
6500
|
+
throw new RangeError(message);
|
|
6501
|
+
}
|
|
6502
|
+
const aobject = (value, label) => {
|
|
6503
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
6504
|
+
throw new TypeError((label === 'object' ? '' : `"${label}" `) + 'expected object, got type=' + typeof value);
|
|
6505
|
+
};
|
|
6506
|
+
const aopts = (value, label) => {
|
|
6507
|
+
aobject(value, label);
|
|
6508
|
+
const proto = Object.getPrototypeOf(value);
|
|
6509
|
+
if (proto !== Object.prototype && proto !== null)
|
|
6510
|
+
throw new TypeError(`"${label}" expected plain object`);
|
|
6511
|
+
// Object.assign() treats an own "__proto__" source key as a write to the target's legacy
|
|
6512
|
+
// prototype setter. Reject it before merging so inherited option values cannot be injected.
|
|
6513
|
+
if (Object.hasOwn(value, '__proto__'))
|
|
6514
|
+
throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
6515
|
+
};
|
|
6516
|
+
/**
|
|
6517
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
6518
|
+
* @param instance - hash instance to validate
|
|
6519
|
+
* @param checkFinished - whether to reject finalized instances
|
|
6520
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
6521
|
+
* @example
|
|
6522
|
+
* Validate that a hash instance is still usable.
|
|
6523
|
+
* ```ts
|
|
6524
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
6525
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
6526
|
+
* const hash = sha256.create();
|
|
6527
|
+
* aexists(hash);
|
|
6528
|
+
* ```
|
|
6529
|
+
*/
|
|
6530
|
+
function aexists(instance, checkFinished = true) {
|
|
6531
|
+
// Runs on every update()/digestInto(); the flags are library-owned booleans, so only their
|
|
6532
|
+
// truthiness is checked - re-validating their type per call was pure hot-path overhead.
|
|
6444
6533
|
if (instance.destroyed)
|
|
6445
|
-
throw new Error('
|
|
6534
|
+
throw new Error('hash was destroyed');
|
|
6446
6535
|
if (checkFinished && instance.finished)
|
|
6447
|
-
throw new Error('
|
|
6536
|
+
throw new Error('digest() was already called');
|
|
6448
6537
|
}
|
|
6449
|
-
|
|
6450
|
-
|
|
6538
|
+
/**
|
|
6539
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
6540
|
+
* @param out - destination buffer
|
|
6541
|
+
* @param instance - hash instance providing output length
|
|
6542
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
6543
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
6544
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
6545
|
+
* @example
|
|
6546
|
+
* Validate a caller-provided digest buffer.
|
|
6547
|
+
* ```ts
|
|
6548
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
6549
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
6550
|
+
* const hash = sha256.create();
|
|
6551
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
6552
|
+
* ```
|
|
6553
|
+
*/
|
|
6554
|
+
function aoutput(out, instance) {
|
|
6555
|
+
abytes(out, undefined, 'output');
|
|
6556
|
+
// `outputLen` is a library-owned readonly number; the negated comparison keeps failing fast
|
|
6557
|
+
// when it is missing/NaN (comparisons with undefined/NaN are false) without an anumber() call.
|
|
6451
6558
|
const min = instance.outputLen;
|
|
6452
|
-
if (out.length
|
|
6453
|
-
throw new
|
|
6559
|
+
if (!(out.length >= min)) {
|
|
6560
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
6454
6561
|
}
|
|
6455
6562
|
}
|
|
6456
|
-
|
|
6457
|
-
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
6458
|
-
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
6459
|
-
// node.js versions earlier than v19 don't declare it in global scope.
|
|
6460
|
-
// For node.js, package.json#exports field mapping rewrites import
|
|
6461
|
-
// from `crypto` to `cryptoNode`, which imports native module.
|
|
6462
|
-
// Makes the utils un-importable in browsers without a bundler.
|
|
6463
|
-
// Once node.js 18 is deprecated, we can just drop the import.
|
|
6464
|
-
const u8a = (a) => a instanceof Uint8Array;
|
|
6465
|
-
// Cast array to view
|
|
6466
|
-
const createView = (arr) => new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
6467
|
-
// The rotate right (circular right shift) operation for uint32
|
|
6468
|
-
const rotr = (word, shift) => (word << (32 - shift)) | (word >>> shift);
|
|
6469
|
-
// big-endian hardware is rare. Just in case someone still decides to run hashes:
|
|
6470
|
-
// early-throw an error because we don't support BE yet.
|
|
6471
|
-
const isLE = new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44;
|
|
6472
|
-
if (!isLE)
|
|
6473
|
-
throw new Error('Non little-endian hardware is not supported');
|
|
6474
6563
|
/**
|
|
6475
|
-
*
|
|
6564
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
6565
|
+
* @param arrays - arrays to overwrite with zeros
|
|
6566
|
+
* @example
|
|
6567
|
+
* Zeroize sensitive buffers in place.
|
|
6568
|
+
* ```ts
|
|
6569
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
6570
|
+
* ```
|
|
6476
6571
|
*/
|
|
6477
|
-
function
|
|
6478
|
-
|
|
6479
|
-
|
|
6480
|
-
|
|
6572
|
+
function clean(...arrays) {
|
|
6573
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
6574
|
+
arrays[i].fill(0);
|
|
6575
|
+
}
|
|
6481
6576
|
}
|
|
6482
6577
|
/**
|
|
6483
|
-
*
|
|
6484
|
-
*
|
|
6485
|
-
*
|
|
6578
|
+
* Creates a DataView for byte-level manipulation.
|
|
6579
|
+
* @param arr - source typed array
|
|
6580
|
+
* @returns DataView over the same buffer region.
|
|
6581
|
+
* @example
|
|
6582
|
+
* Create a DataView over an existing buffer.
|
|
6583
|
+
* ```ts
|
|
6584
|
+
* createView(new Uint8Array(4));
|
|
6585
|
+
* ```
|
|
6486
6586
|
*/
|
|
6487
|
-
function
|
|
6488
|
-
|
|
6489
|
-
data = utf8ToBytes(data);
|
|
6490
|
-
if (!u8a(data))
|
|
6491
|
-
throw new Error(`expected Uint8Array, got ${typeof data}`);
|
|
6492
|
-
return data;
|
|
6587
|
+
function createView(arr) {
|
|
6588
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
6493
6589
|
}
|
|
6494
|
-
|
|
6495
|
-
|
|
6496
|
-
|
|
6497
|
-
|
|
6498
|
-
|
|
6499
|
-
|
|
6590
|
+
/**
|
|
6591
|
+
* Rotate-right operation for uint32 values.
|
|
6592
|
+
* @param word - source word
|
|
6593
|
+
* @param shift - shift amount in bits
|
|
6594
|
+
* @returns Rotated word.
|
|
6595
|
+
* @example
|
|
6596
|
+
* Rotate a 32-bit word to the right.
|
|
6597
|
+
* ```ts
|
|
6598
|
+
* rotr(0x12345678, 8);
|
|
6599
|
+
* ```
|
|
6600
|
+
*/
|
|
6601
|
+
function rotr(word, shift) {
|
|
6602
|
+
return (word << (32 - shift)) | (word >>> shift);
|
|
6500
6603
|
}
|
|
6501
|
-
|
|
6502
|
-
|
|
6503
|
-
|
|
6604
|
+
/**
|
|
6605
|
+
* Merges default options and passed options.
|
|
6606
|
+
* @param defaults - base option object
|
|
6607
|
+
* @param opts - user overrides
|
|
6608
|
+
* @param title - label included in thrown override errors
|
|
6609
|
+
* @returns Fresh merged option object with a null prototype.
|
|
6610
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
6611
|
+
* @example
|
|
6612
|
+
* Merge user overrides onto default options.
|
|
6613
|
+
* ```ts
|
|
6614
|
+
* checkOpts({ dkLen: 32 }, { asyncTick: 10 });
|
|
6615
|
+
* ```
|
|
6616
|
+
*/
|
|
6617
|
+
function checkOpts(defaults, opts, title = 'opts') {
|
|
6618
|
+
aopts(defaults, 'defaults');
|
|
6619
|
+
if (opts !== undefined)
|
|
6620
|
+
aopts(opts, title);
|
|
6621
|
+
// Callers read optional fields directly, so omitted values must not fall through to ambient
|
|
6622
|
+
// Object.prototype pollution (for example a forged `dkLen` changing SHAKE's default output).
|
|
6623
|
+
const merged = Object.assign(Object.create(null), defaults, opts);
|
|
6624
|
+
return merged;
|
|
6625
|
+
}
|
|
6626
|
+
/**
|
|
6627
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
6628
|
+
* @param hashCons - hash constructor or factory
|
|
6629
|
+
* @param info - optional metadata such as DER OID
|
|
6630
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
6631
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
6632
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
6633
|
+
* init time.
|
|
6634
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
6635
|
+
* @example
|
|
6636
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
6637
|
+
* ```ts
|
|
6638
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
6639
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
6640
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
6641
|
+
* wrapped(new Uint8Array([1]));
|
|
6642
|
+
* ```
|
|
6643
|
+
*/
|
|
6644
|
+
function createHasher(hashCons, info = {}) {
|
|
6645
|
+
if (typeof hashCons !== 'function')
|
|
6646
|
+
throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
6647
|
+
info = checkOpts({}, info, 'info');
|
|
6648
|
+
const hashC = (msg, opts) => hashCons(opts)
|
|
6649
|
+
.update(msg)
|
|
6650
|
+
.digest();
|
|
6651
|
+
const tmp = hashCons(undefined);
|
|
6504
6652
|
hashC.outputLen = tmp.outputLen;
|
|
6505
6653
|
hashC.blockLen = tmp.blockLen;
|
|
6506
|
-
hashC.
|
|
6507
|
-
|
|
6654
|
+
hashC.canXOF = tmp.canXOF;
|
|
6655
|
+
hashC.create = (opts) => hashCons(opts);
|
|
6656
|
+
Object.assign(hashC, info);
|
|
6657
|
+
return Object.freeze(hashC);
|
|
6508
6658
|
}
|
|
6659
|
+
/**
|
|
6660
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
6661
|
+
* @param suffix - final OID byte for the selected hash.
|
|
6662
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
6663
|
+
* suffixes are meaningful downstream.
|
|
6664
|
+
* @returns Object containing the DER-encoded OID.
|
|
6665
|
+
* @example
|
|
6666
|
+
* Build OID metadata for a NIST hash.
|
|
6667
|
+
* ```ts
|
|
6668
|
+
* oidNist(0x01);
|
|
6669
|
+
* ```
|
|
6670
|
+
*/
|
|
6671
|
+
const oidNist = (suffix) => ({
|
|
6672
|
+
// Current NIST hashAlgs suffixes used here fit in one DER subidentifier octet.
|
|
6673
|
+
// Larger suffix values would need base-128 OID encoding and a different length byte.
|
|
6674
|
+
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix]),
|
|
6675
|
+
});
|
|
6509
6676
|
|
|
6510
|
-
|
|
6511
|
-
|
|
6512
|
-
|
|
6513
|
-
|
|
6514
|
-
|
|
6515
|
-
|
|
6516
|
-
|
|
6517
|
-
|
|
6518
|
-
|
|
6519
|
-
|
|
6520
|
-
|
|
6521
|
-
|
|
6677
|
+
/**
|
|
6678
|
+
* Internal Merkle-Damgard hash utils.
|
|
6679
|
+
* @module
|
|
6680
|
+
*/
|
|
6681
|
+
/**
|
|
6682
|
+
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
6683
|
+
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
6684
|
+
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
6685
|
+
* set the same bit.
|
|
6686
|
+
* @param a - selector word
|
|
6687
|
+
* @param b - word chosen when selector bit is set
|
|
6688
|
+
* @param c - word chosen when selector bit is clear
|
|
6689
|
+
* @returns Mixed 32-bit word.
|
|
6690
|
+
* @example
|
|
6691
|
+
* Combine three words with the shared 32-bit choice primitive.
|
|
6692
|
+
* ```ts
|
|
6693
|
+
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
6694
|
+
* ```
|
|
6695
|
+
*/
|
|
6696
|
+
function Chi(a, b, c) {
|
|
6697
|
+
return (a & b) ^ (~a & c);
|
|
6522
6698
|
}
|
|
6523
|
-
|
|
6524
|
-
|
|
6699
|
+
/**
|
|
6700
|
+
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
6701
|
+
* Returns bits shared by at least two inputs.
|
|
6702
|
+
* @param a - first input word
|
|
6703
|
+
* @param b - second input word
|
|
6704
|
+
* @param c - third input word
|
|
6705
|
+
* @returns Mixed 32-bit word.
|
|
6706
|
+
* @example
|
|
6707
|
+
* Combine three words with the shared 32-bit majority primitive.
|
|
6708
|
+
* ```ts
|
|
6709
|
+
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
6710
|
+
* ```
|
|
6711
|
+
*/
|
|
6712
|
+
function Maj(a, b, c) {
|
|
6713
|
+
return (a & b) ^ (a & c) ^ (b & c);
|
|
6714
|
+
}
|
|
6715
|
+
/**
|
|
6716
|
+
* Merkle-Damgard hash construction base class.
|
|
6717
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
6718
|
+
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
6719
|
+
* strings with partial-byte tails.
|
|
6720
|
+
* @param blockLen - internal block size in bytes
|
|
6721
|
+
* @param outputLen - digest size in bytes
|
|
6722
|
+
* @param padOffset - trailing length field size in bytes
|
|
6723
|
+
* @param isLE - whether length and state words are encoded in little-endian
|
|
6724
|
+
* @example
|
|
6725
|
+
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
6726
|
+
* ```ts
|
|
6727
|
+
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
6728
|
+
* const hash = new _SHA1();
|
|
6729
|
+
* hash.update(new Uint8Array([97, 98, 99]));
|
|
6730
|
+
* hash.digest();
|
|
6731
|
+
* ```
|
|
6732
|
+
*/
|
|
6733
|
+
class HashMD {
|
|
6734
|
+
blockLen;
|
|
6735
|
+
outputLen;
|
|
6736
|
+
canXOF = false;
|
|
6737
|
+
padOffset;
|
|
6738
|
+
isLE;
|
|
6739
|
+
// For partial updates less than block size
|
|
6740
|
+
buffer;
|
|
6741
|
+
view;
|
|
6742
|
+
finished = false;
|
|
6743
|
+
length = 0;
|
|
6744
|
+
pos = 0;
|
|
6745
|
+
destroyed = false;
|
|
6525
6746
|
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
6526
|
-
super();
|
|
6527
6747
|
this.blockLen = blockLen;
|
|
6528
6748
|
this.outputLen = outputLen;
|
|
6529
6749
|
this.padOffset = padOffset;
|
|
6530
6750
|
this.isLE = isLE;
|
|
6531
|
-
this.finished = false;
|
|
6532
|
-
this.length = 0;
|
|
6533
|
-
this.pos = 0;
|
|
6534
|
-
this.destroyed = false;
|
|
6535
6751
|
this.buffer = new Uint8Array(blockLen);
|
|
6536
6752
|
this.view = createView(this.buffer);
|
|
6537
6753
|
}
|
|
6538
6754
|
update(data) {
|
|
6539
|
-
|
|
6755
|
+
aexists(this);
|
|
6756
|
+
abytes(data);
|
|
6540
6757
|
const { view, buffer, blockLen } = this;
|
|
6541
|
-
data = toBytes(data);
|
|
6542
6758
|
const len = data.length;
|
|
6759
|
+
let processed = false;
|
|
6543
6760
|
for (let pos = 0; pos < len;) {
|
|
6544
6761
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
6545
|
-
// Fast path
|
|
6762
|
+
// Fast path only when there is no buffered partial block: `take === blockLen` implies
|
|
6763
|
+
// `this.pos === 0`, so we can process full blocks directly from the input view.
|
|
6546
6764
|
if (take === blockLen) {
|
|
6547
6765
|
const dataView = createView(data);
|
|
6548
6766
|
for (; blockLen <= len - pos; pos += blockLen)
|
|
6549
6767
|
this.process(dataView, pos);
|
|
6768
|
+
processed = true;
|
|
6550
6769
|
continue;
|
|
6551
6770
|
}
|
|
6552
|
-
|
|
6771
|
+
// When the whole input is buffered in one go (common for short messages), passing `data`
|
|
6772
|
+
// directly avoids allocating a subarray view.
|
|
6773
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
6553
6774
|
this.pos += take;
|
|
6554
6775
|
pos += take;
|
|
6555
6776
|
if (this.pos === blockLen) {
|
|
6556
6777
|
this.process(view, 0);
|
|
6557
6778
|
this.pos = 0;
|
|
6779
|
+
processed = true;
|
|
6558
6780
|
}
|
|
6559
6781
|
}
|
|
6560
6782
|
this.length += data.length;
|
|
6561
|
-
|
|
6783
|
+
// Shared schedule buffers only pick up input-derived words inside process(); if everything
|
|
6784
|
+
// was buffered without processing, there is nothing to zero.
|
|
6785
|
+
if (processed)
|
|
6786
|
+
this.roundClean();
|
|
6562
6787
|
return this;
|
|
6563
6788
|
}
|
|
6564
6789
|
digestInto(out) {
|
|
6565
|
-
|
|
6566
|
-
|
|
6790
|
+
aexists(this);
|
|
6791
|
+
aoutput(out, this);
|
|
6567
6792
|
this.finished = true;
|
|
6568
6793
|
// Padding
|
|
6569
6794
|
// We can avoid allocation of buffer for padding completely if it
|
|
6570
6795
|
// was previously not allocated here. But it won't change performance.
|
|
6571
6796
|
const { buffer, view, blockLen, isLE } = this;
|
|
6572
6797
|
let { pos } = this;
|
|
6573
|
-
// append the bit '1' to the message
|
|
6798
|
+
// append the bit '1' to the message, then zero-pad the rest of the block
|
|
6574
6799
|
buffer[pos++] = 0b10000000;
|
|
6575
|
-
|
|
6576
|
-
// we have less than padOffset left in buffer, so we cannot put length in
|
|
6800
|
+
buffer.fill(0, pos);
|
|
6801
|
+
// we have less than padOffset left in buffer, so we cannot put length in
|
|
6802
|
+
// current block, need process it and pad again
|
|
6577
6803
|
if (this.padOffset > blockLen - pos) {
|
|
6578
6804
|
this.process(view, 0);
|
|
6579
|
-
|
|
6805
|
+
buffer.fill(0);
|
|
6580
6806
|
}
|
|
6581
|
-
//
|
|
6582
|
-
|
|
6583
|
-
|
|
6584
|
-
//
|
|
6585
|
-
|
|
6586
|
-
// So we just write lowest 64 bits of that value.
|
|
6587
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
6807
|
+
// `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
|
|
6808
|
+
// the padding fill above, and JS will overflow before user input can make that half non-zero.
|
|
6809
|
+
// So we only need to write the low 64 bits here (`length * 8` only scales the exponent of an
|
|
6810
|
+
// integer below 2**53, so the split inside the helper stays exact).
|
|
6811
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
6588
6812
|
this.process(view, 0);
|
|
6589
|
-
|
|
6813
|
+
// The final block above is processed outside update(), so the shared message-schedule
|
|
6814
|
+
// buffers (e.g. SHA256_W) would otherwise retain input-derived words after digest().
|
|
6815
|
+
this.roundClean();
|
|
6816
|
+
// digest() passes our own `buffer` as `out`; reuse its cached view instead of allocating one.
|
|
6817
|
+
const oview = out === buffer ? view : createView(out);
|
|
6590
6818
|
const len = this.outputLen;
|
|
6591
|
-
// NOTE: we do division by 4 later, which
|
|
6592
|
-
if (len % 4)
|
|
6593
|
-
throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
6819
|
+
// NOTE: we do division by 4 later, which must be fused in single op with modulo by JIT
|
|
6594
6820
|
const outLen = len / 4;
|
|
6595
6821
|
const state = this.get();
|
|
6596
|
-
|
|
6597
|
-
|
|
6822
|
+
// Subclass-misconfiguration invariant: outputLen must be 32-bit aligned and fit the state.
|
|
6823
|
+
if (len % 4 || outLen > state.length)
|
|
6824
|
+
throw new Error('invalid outputLen');
|
|
6598
6825
|
for (let i = 0; i < outLen; i++)
|
|
6599
6826
|
oview.setUint32(4 * i, state[i], isLE);
|
|
6600
6827
|
}
|
|
6601
6828
|
digest() {
|
|
6602
6829
|
const { buffer, outputLen } = this;
|
|
6603
6830
|
this.digestInto(buffer);
|
|
6831
|
+
// Copy before destroy(): subclasses wipe `buffer` during cleanup, but `digest()` must return
|
|
6832
|
+
// fresh bytes to the caller.
|
|
6604
6833
|
const res = buffer.slice(0, outputLen);
|
|
6605
6834
|
this.destroy();
|
|
6606
6835
|
return res;
|
|
6607
6836
|
}
|
|
6608
|
-
|
|
6609
|
-
|
|
6610
|
-
to.
|
|
6611
|
-
|
|
6837
|
+
_cloneIntoMeta(to) {
|
|
6838
|
+
const { buffer, length, finished, destroyed, pos } = this;
|
|
6839
|
+
to.destroyed = destroyed;
|
|
6840
|
+
to.finished = finished;
|
|
6612
6841
|
to.length = length;
|
|
6613
6842
|
to.pos = pos;
|
|
6614
|
-
|
|
6615
|
-
to.
|
|
6616
|
-
if (
|
|
6617
|
-
to.buffer.set(buffer);
|
|
6843
|
+
// Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
|
|
6844
|
+
// later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
|
|
6845
|
+
if (pos)
|
|
6846
|
+
to.buffer.set(buffer); // Avoid a hot modulo guard.
|
|
6618
6847
|
return to;
|
|
6619
6848
|
}
|
|
6849
|
+
clone() {
|
|
6850
|
+
return this._cloneInto();
|
|
6851
|
+
}
|
|
6620
6852
|
}
|
|
6853
|
+
/**
|
|
6854
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
6855
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
6856
|
+
*/
|
|
6857
|
+
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
6858
|
+
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
6859
|
+
* it as read-only because constructors copy words from it by index. */
|
|
6860
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
6861
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
|
6862
|
+
]);
|
|
6621
6863
|
|
|
6622
|
-
|
|
6623
|
-
|
|
6624
|
-
|
|
6625
|
-
|
|
6626
|
-
|
|
6627
|
-
|
|
6628
|
-
|
|
6629
|
-
|
|
6864
|
+
/**
|
|
6865
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
6866
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
6867
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
6868
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
6869
|
+
* @module
|
|
6870
|
+
*/
|
|
6871
|
+
/**
|
|
6872
|
+
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
6873
|
+
* of the cube roots of the first 64 primes (2..311).
|
|
6874
|
+
*/
|
|
6630
6875
|
// prettier-ignore
|
|
6631
|
-
const SHA256_K = /* @__PURE__ */
|
|
6876
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
6632
6877
|
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
6633
6878
|
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
6634
6879
|
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
@@ -6638,19 +6883,24 @@ const SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
|
6638
6883
|
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
6639
6884
|
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
6640
6885
|
]);
|
|
6641
|
-
|
|
6642
|
-
// prettier-ignore
|
|
6643
|
-
const IV = /* @__PURE__ */ new Uint32Array([
|
|
6644
|
-
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
6645
|
-
]);
|
|
6646
|
-
// Temporary buffer, not used to store anything between runs
|
|
6647
|
-
// Named this way because it matches specification.
|
|
6886
|
+
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
6648
6887
|
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
6649
|
-
|
|
6650
|
-
|
|
6651
|
-
|
|
6652
|
-
|
|
6653
|
-
|
|
6888
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
6889
|
+
class SHA2_32B extends HashMD {
|
|
6890
|
+
// We cannot use array here since array allows indexing by variable
|
|
6891
|
+
// which means optimizer/compiler cannot use registers.
|
|
6892
|
+
// Numeric initializers matter: starting the fields as `undefined` changes
|
|
6893
|
+
// V8's field representation and makes sha256 3x slower (measured).
|
|
6894
|
+
A = 0;
|
|
6895
|
+
B = 0;
|
|
6896
|
+
C = 0;
|
|
6897
|
+
D = 0;
|
|
6898
|
+
E = 0;
|
|
6899
|
+
F = 0;
|
|
6900
|
+
G = 0;
|
|
6901
|
+
H = 0;
|
|
6902
|
+
constructor(outputLen, IV) {
|
|
6903
|
+
super(64, outputLen, 8, false);
|
|
6654
6904
|
this.A = IV[0] | 0;
|
|
6655
6905
|
this.B = IV[1] | 0;
|
|
6656
6906
|
this.C = IV[2] | 0;
|
|
@@ -6675,6 +6925,10 @@ class SHA256 extends SHA2 {
|
|
|
6675
6925
|
this.G = G | 0;
|
|
6676
6926
|
this.H = H | 0;
|
|
6677
6927
|
}
|
|
6928
|
+
_cloneInto(to) {
|
|
6929
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
6930
|
+
return this._cloneIntoMeta(to);
|
|
6931
|
+
}
|
|
6678
6932
|
process(view, offset) {
|
|
6679
6933
|
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
6680
6934
|
for (let i = 0; i < 16; i++, offset += 4)
|
|
@@ -6714,18 +6968,40 @@ class SHA256 extends SHA2 {
|
|
|
6714
6968
|
this.set(A, B, C, D, E, F, G, H);
|
|
6715
6969
|
}
|
|
6716
6970
|
roundClean() {
|
|
6717
|
-
SHA256_W
|
|
6971
|
+
clean(SHA256_W);
|
|
6718
6972
|
}
|
|
6719
6973
|
destroy() {
|
|
6974
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
6975
|
+
// update()/digest() callable on reused instances.
|
|
6976
|
+
this.destroyed = true;
|
|
6720
6977
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
6721
|
-
this.buffer
|
|
6978
|
+
clean(this.buffer);
|
|
6979
|
+
}
|
|
6980
|
+
}
|
|
6981
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
6982
|
+
class _SHA256 extends SHA2_32B {
|
|
6983
|
+
constructor() {
|
|
6984
|
+
super(32, SHA256_IV);
|
|
6722
6985
|
}
|
|
6723
6986
|
}
|
|
6724
6987
|
/**
|
|
6725
|
-
* SHA2-256 hash function
|
|
6726
|
-
*
|
|
6988
|
+
* SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
|
|
6989
|
+
*
|
|
6990
|
+
* - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
|
|
6991
|
+
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
6992
|
+
* - Each sha256 hash is executing 2^18 bit operations.
|
|
6993
|
+
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
6994
|
+
* @param msg - message bytes to hash
|
|
6995
|
+
* @param opts - Reserved hash options.
|
|
6996
|
+
* @returns Digest bytes.
|
|
6997
|
+
* @example
|
|
6998
|
+
* Hash a message with SHA2-256.
|
|
6999
|
+
* ```ts
|
|
7000
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
7001
|
+
* ```
|
|
6727
7002
|
*/
|
|
6728
|
-
const sha256 = /* @__PURE__ */
|
|
7003
|
+
const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(),
|
|
7004
|
+
/* @__PURE__ */ oidNist(0x01));
|
|
6729
7005
|
|
|
6730
7006
|
const encoder = new TextEncoder();
|
|
6731
7007
|
const INTEGER_ARRAYS = new Set([
|
|
@@ -8161,9 +8437,10 @@ const get = async ({ contract, method, params }) => {
|
|
|
8161
8437
|
if (topLevelRead)
|
|
8162
8438
|
beginExecution(`read:${contract}:${method}:${JSON.stringify(params ?? [], jsonStringifyBigInt$1)}`);
|
|
8163
8439
|
try {
|
|
8164
|
-
|
|
8165
|
-
|
|
8166
|
-
|
|
8440
|
+
const member = contracts[contract][method];
|
|
8441
|
+
if (typeof member === 'function')
|
|
8442
|
+
return await member.apply(contracts[contract], params ?? []);
|
|
8443
|
+
return member;
|
|
8167
8444
|
}
|
|
8168
8445
|
finally {
|
|
8169
8446
|
if (topLevelRead) {
|