@ocap/util 1.30.25 → 1.31.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.
- package/esm/amm.d.mts +158 -0
- package/esm/amm.mjs +269 -0
- package/esm/arcade.d.mts +217 -0
- package/esm/arcade.mjs +484 -0
- package/esm/bonding-reserve-aup.d.mts +125 -0
- package/esm/bonding-reserve-aup.mjs +230 -0
- package/esm/composable-container.d.mts +141 -0
- package/esm/composable-container.mjs +264 -0
- package/esm/conditional-market.d.mts +151 -0
- package/esm/conditional-market.mjs +376 -0
- package/esm/curve.d.mts +43 -5
- package/esm/curve.mjs +71 -9
- package/esm/foreign-receipt.d.mts +126 -0
- package/esm/foreign-receipt.mjs +388 -0
- package/esm/index.d.mts +8 -1
- package/esm/index.mjs +8 -1
- package/esm/node_modules/@noble/hashes/_md.mjs +168 -0
- package/esm/node_modules/@noble/hashes/sha2.mjs +185 -0
- package/esm/node_modules/@noble/hashes/utils.mjs +175 -0
- package/esm/url.mjs +17 -8
- package/lib/amm.cjs +279 -0
- package/lib/amm.d.cts +158 -0
- package/lib/arcade.cjs +491 -0
- package/lib/arcade.d.cts +217 -0
- package/lib/bonding-reserve-aup.cjs +232 -0
- package/lib/bonding-reserve-aup.d.cts +125 -0
- package/lib/composable-container.cjs +270 -0
- package/lib/composable-container.d.cts +141 -0
- package/lib/conditional-market.cjs +377 -0
- package/lib/conditional-market.d.cts +151 -0
- package/lib/curve.cjs +73 -9
- package/lib/curve.d.cts +43 -5
- package/lib/foreign-receipt.cjs +396 -0
- package/lib/foreign-receipt.d.cts +126 -0
- package/lib/index.cjs +57 -1
- package/lib/index.d.cts +8 -1
- package/lib/node_modules/@noble/hashes/_md.cjs +171 -0
- package/lib/node_modules/@noble/hashes/sha2.cjs +185 -0
- package/lib/node_modules/@noble/hashes/utils.cjs +183 -0
- package/lib/url.cjs +17 -8
- package/package.json +2 -2
package/esm/index.mjs
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { BN } from "./bn.mjs";
|
|
2
2
|
import { scopeMatch, scopeMatchAny } from "./scope-match.mjs";
|
|
3
|
+
import { RevenueSharePool, assertMachineBankrollSolvency, deriveProvablyFairRoll, generateHashChain, resolveHierarchicalSplitDag, resolveWeightedLootTable, settleArcadeMachinePlay, verifyHashChainStep } from "./arcade.mjs";
|
|
4
|
+
import { BondingReserveVault, buildAupMultiViewManifest, quoteBondingCurveMint } from "./bonding-reserve-aup.mjs";
|
|
5
|
+
import { assertAssetRedeemableOrTransferable, assertNoContainerCycle, createComposableAssetNode, mountContainerSlotItem, synthesizeContainerRecipe, unmountContainerSlotItem } from "./composable-container.mjs";
|
|
6
|
+
import { ConditionalMarketPool } from "./conditional-market.mjs";
|
|
7
|
+
import { ForeignReceiptRegistry, SponsoredQuotaTracker, computeForeignQuoteDigest, computeForeignReceiptId, formatForeignReceiptKey, isAssetUnlockedByReceipt, sha256Hex, splitForeignVaultPayment } from "./foreign-receipt.mjs";
|
|
8
|
+
import { Direction, calcConstantCost, calcConstantPrice, calcCost, calcFee, calcLinearCost, calcLinearPrice, calcPrice, calcQuadraticCost, calcQuadraticPrice, calcQuotaMintFromPayment, consumePrepaidQuota } from "./curve.mjs";
|
|
9
|
+
import { MAX_TOTAL_FEE_BPS, MINIMUM_LIQUIDITY, calcAddLiquidity, calcGraduationSettlement, calcGraduationSupplySplit, calcInitialLiquidity, calcLimitOrderFill, calcRemoveLiquidity, calcSwapExactIn, calcSwapExactOut, isqrt } from "./amm.mjs";
|
|
3
10
|
import BaseBN from "bn.js";
|
|
4
11
|
import base64 from "base64-url";
|
|
5
12
|
import base58 from "bs58";
|
|
@@ -478,4 +485,4 @@ function formatTxType(type) {
|
|
|
478
485
|
}
|
|
479
486
|
|
|
480
487
|
//#endregion
|
|
481
|
-
export { BN, UUID, bytesToHex, extractMethod, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, hexToBytes, hexToNumber, hexToUtf8, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, leftPad, numberToBN, numberToHex, numberToString, rightPad, scopeMatch, scopeMatchAny, stripHexPrefix, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, utf8ToHex };
|
|
488
|
+
export { BN, BondingReserveVault, ConditionalMarketPool, Direction, ForeignReceiptRegistry, MAX_TOTAL_FEE_BPS, MINIMUM_LIQUIDITY, RevenueSharePool, SponsoredQuotaTracker, UUID, assertAssetRedeemableOrTransferable, assertMachineBankrollSolvency, assertNoContainerCycle, buildAupMultiViewManifest, bytesToHex, calcAddLiquidity, calcConstantCost, calcConstantPrice, calcCost, calcFee, calcGraduationSettlement, calcGraduationSupplySplit, calcInitialLiquidity, calcLimitOrderFill, calcLinearCost, calcLinearPrice, calcPrice, calcQuadraticCost, calcQuadraticPrice, calcQuotaMintFromPayment, calcRemoveLiquidity, calcSwapExactIn, calcSwapExactOut, computeForeignQuoteDigest, computeForeignReceiptId, consumePrepaidQuota, createComposableAssetNode, deriveProvablyFairRoll, extractMethod, formatForeignReceiptKey, formatTxType, fromBase58, fromBase64, fromTokenToUnit, fromUnitToToken, generateHashChain, hexToBytes, hexToNumber, hexToUtf8, isAssetUnlockedByReceipt, isBN, isBase58btc, isBigNumber, isHex, isHexPrefixed, isHexStrict, isSameDid, isUUID, isUint8Array, isqrt, leftPad, mountContainerSlotItem, numberToBN, numberToHex, numberToString, quoteBondingCurveMint, resolveHierarchicalSplitDag, resolveWeightedLootTable, rightPad, scopeMatch, scopeMatchAny, settleArcadeMachinePlay, sha256Hex, splitForeignVaultPayment, stripHexPrefix, synthesizeContainerRecipe, toAddress, toBN, toBase58, toBase64, toBuffer, toDid, toHex, toUint8Array, unmountContainerSlotItem, utf8ToHex, verifyHashChainStep };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { abytes, aexists, aoutput, clean, createView } from "./utils.mjs";
|
|
2
|
+
|
|
3
|
+
//#region ../../node_modules/@noble/hashes/_md.js
|
|
4
|
+
/**
|
|
5
|
+
* Internal Merkle-Damgard hash utils.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
|
|
10
|
+
* Returns bits from `b` when `a` is set, otherwise from `c`.
|
|
11
|
+
* The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
|
|
12
|
+
* set the same bit.
|
|
13
|
+
* @param a - selector word
|
|
14
|
+
* @param b - word chosen when selector bit is set
|
|
15
|
+
* @param c - word chosen when selector bit is clear
|
|
16
|
+
* @returns Mixed 32-bit word.
|
|
17
|
+
* @example
|
|
18
|
+
* Combine three words with the shared 32-bit choice primitive.
|
|
19
|
+
* ```ts
|
|
20
|
+
* Chi(0xffffffff, 0x12345678, 0x87654321);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function Chi(a, b, c) {
|
|
24
|
+
return a & b ^ ~a & c;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
|
|
28
|
+
* Returns bits shared by at least two inputs.
|
|
29
|
+
* @param a - first input word
|
|
30
|
+
* @param b - second input word
|
|
31
|
+
* @param c - third input word
|
|
32
|
+
* @returns Mixed 32-bit word.
|
|
33
|
+
* @example
|
|
34
|
+
* Combine three words with the shared 32-bit majority primitive.
|
|
35
|
+
* ```ts
|
|
36
|
+
* Maj(0xffffffff, 0x12345678, 0x87654321);
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function Maj(a, b, c) {
|
|
40
|
+
return a & b ^ a & c ^ b & c;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Merkle-Damgard hash construction base class.
|
|
44
|
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
|
45
|
+
* Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
|
|
46
|
+
* strings with partial-byte tails.
|
|
47
|
+
* @param blockLen - internal block size in bytes
|
|
48
|
+
* @param outputLen - digest size in bytes
|
|
49
|
+
* @param padOffset - trailing length field size in bytes
|
|
50
|
+
* @param isLE - whether length and state words are encoded in little-endian
|
|
51
|
+
* @example
|
|
52
|
+
* Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
|
|
53
|
+
* ```ts
|
|
54
|
+
* import { _SHA1 } from '@noble/hashes/legacy.js';
|
|
55
|
+
* const hash = new _SHA1();
|
|
56
|
+
* hash.update(new Uint8Array([97, 98, 99]));
|
|
57
|
+
* hash.digest();
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
var HashMD = class {
|
|
61
|
+
blockLen;
|
|
62
|
+
outputLen;
|
|
63
|
+
canXOF = false;
|
|
64
|
+
padOffset;
|
|
65
|
+
isLE;
|
|
66
|
+
buffer;
|
|
67
|
+
view;
|
|
68
|
+
finished = false;
|
|
69
|
+
length = 0;
|
|
70
|
+
pos = 0;
|
|
71
|
+
destroyed = false;
|
|
72
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
73
|
+
this.blockLen = blockLen;
|
|
74
|
+
this.outputLen = outputLen;
|
|
75
|
+
this.padOffset = padOffset;
|
|
76
|
+
this.isLE = isLE;
|
|
77
|
+
this.buffer = new Uint8Array(blockLen);
|
|
78
|
+
this.view = createView(this.buffer);
|
|
79
|
+
}
|
|
80
|
+
update(data) {
|
|
81
|
+
aexists(this);
|
|
82
|
+
abytes(data);
|
|
83
|
+
const { view, buffer, blockLen } = this;
|
|
84
|
+
const len = data.length;
|
|
85
|
+
for (let pos = 0; pos < len;) {
|
|
86
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
87
|
+
if (take === blockLen) {
|
|
88
|
+
const dataView = createView(data);
|
|
89
|
+
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
93
|
+
this.pos += take;
|
|
94
|
+
pos += take;
|
|
95
|
+
if (this.pos === blockLen) {
|
|
96
|
+
this.process(view, 0);
|
|
97
|
+
this.pos = 0;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this.length += data.length;
|
|
101
|
+
this.roundClean();
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
digestInto(out) {
|
|
105
|
+
aexists(this);
|
|
106
|
+
aoutput(out, this);
|
|
107
|
+
this.finished = true;
|
|
108
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
109
|
+
let { pos } = this;
|
|
110
|
+
buffer[pos++] = 128;
|
|
111
|
+
clean(this.buffer.subarray(pos));
|
|
112
|
+
if (this.padOffset > blockLen - pos) {
|
|
113
|
+
this.process(view, 0);
|
|
114
|
+
pos = 0;
|
|
115
|
+
}
|
|
116
|
+
for (let i = pos; i < blockLen; i++) buffer[i] = 0;
|
|
117
|
+
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
118
|
+
this.process(view, 0);
|
|
119
|
+
const oview = createView(out);
|
|
120
|
+
const len = this.outputLen;
|
|
121
|
+
if (len % 4) throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
122
|
+
const outLen = len / 4;
|
|
123
|
+
const state = this.get();
|
|
124
|
+
if (outLen > state.length) throw new Error("_sha2: outputLen bigger than state");
|
|
125
|
+
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
126
|
+
}
|
|
127
|
+
digest() {
|
|
128
|
+
const { buffer, outputLen } = this;
|
|
129
|
+
this.digestInto(buffer);
|
|
130
|
+
const res = buffer.slice(0, outputLen);
|
|
131
|
+
this.destroy();
|
|
132
|
+
return res;
|
|
133
|
+
}
|
|
134
|
+
_cloneInto(to) {
|
|
135
|
+
to ||= new this.constructor();
|
|
136
|
+
to.set(...this.get());
|
|
137
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
138
|
+
to.destroyed = destroyed;
|
|
139
|
+
to.finished = finished;
|
|
140
|
+
to.length = length;
|
|
141
|
+
to.pos = pos;
|
|
142
|
+
if (length % blockLen) to.buffer.set(buffer);
|
|
143
|
+
return to;
|
|
144
|
+
}
|
|
145
|
+
clone() {
|
|
146
|
+
return this._cloneInto();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
|
151
|
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
|
152
|
+
*/
|
|
153
|
+
/** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
|
|
154
|
+
* square roots of the first eight prime numbers. Exported as a shared table; callers must treat
|
|
155
|
+
* it as read-only because constructors copy words from it by index. */
|
|
156
|
+
const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
157
|
+
1779033703,
|
|
158
|
+
3144134277,
|
|
159
|
+
1013904242,
|
|
160
|
+
2773480762,
|
|
161
|
+
1359893119,
|
|
162
|
+
2600822924,
|
|
163
|
+
528734635,
|
|
164
|
+
1541459225
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
//#endregion
|
|
168
|
+
export { Chi, HashMD, Maj, SHA256_IV };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { clean, createHasher, oidNist, rotr } from "./utils.mjs";
|
|
2
|
+
import { Chi, HashMD, Maj, SHA256_IV } from "./_md.mjs";
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/@noble/hashes/sha2.js
|
|
5
|
+
/**
|
|
6
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
7
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
8
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
9
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* SHA-224 / SHA-256 round constants from RFC 6234 §5.1: the first 32 bits
|
|
14
|
+
* of the cube roots of the first 64 primes (2..311).
|
|
15
|
+
*/
|
|
16
|
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
17
|
+
1116352408,
|
|
18
|
+
1899447441,
|
|
19
|
+
3049323471,
|
|
20
|
+
3921009573,
|
|
21
|
+
961987163,
|
|
22
|
+
1508970993,
|
|
23
|
+
2453635748,
|
|
24
|
+
2870763221,
|
|
25
|
+
3624381080,
|
|
26
|
+
310598401,
|
|
27
|
+
607225278,
|
|
28
|
+
1426881987,
|
|
29
|
+
1925078388,
|
|
30
|
+
2162078206,
|
|
31
|
+
2614888103,
|
|
32
|
+
3248222580,
|
|
33
|
+
3835390401,
|
|
34
|
+
4022224774,
|
|
35
|
+
264347078,
|
|
36
|
+
604807628,
|
|
37
|
+
770255983,
|
|
38
|
+
1249150122,
|
|
39
|
+
1555081692,
|
|
40
|
+
1996064986,
|
|
41
|
+
2554220882,
|
|
42
|
+
2821834349,
|
|
43
|
+
2952996808,
|
|
44
|
+
3210313671,
|
|
45
|
+
3336571891,
|
|
46
|
+
3584528711,
|
|
47
|
+
113926993,
|
|
48
|
+
338241895,
|
|
49
|
+
666307205,
|
|
50
|
+
773529912,
|
|
51
|
+
1294757372,
|
|
52
|
+
1396182291,
|
|
53
|
+
1695183700,
|
|
54
|
+
1986661051,
|
|
55
|
+
2177026350,
|
|
56
|
+
2456956037,
|
|
57
|
+
2730485921,
|
|
58
|
+
2820302411,
|
|
59
|
+
3259730800,
|
|
60
|
+
3345764771,
|
|
61
|
+
3516065817,
|
|
62
|
+
3600352804,
|
|
63
|
+
4094571909,
|
|
64
|
+
275423344,
|
|
65
|
+
430227734,
|
|
66
|
+
506948616,
|
|
67
|
+
659060556,
|
|
68
|
+
883997877,
|
|
69
|
+
958139571,
|
|
70
|
+
1322822218,
|
|
71
|
+
1537002063,
|
|
72
|
+
1747873779,
|
|
73
|
+
1955562222,
|
|
74
|
+
2024104815,
|
|
75
|
+
2227730452,
|
|
76
|
+
2361852424,
|
|
77
|
+
2428436474,
|
|
78
|
+
2756734187,
|
|
79
|
+
3204031479,
|
|
80
|
+
3329325298
|
|
81
|
+
]);
|
|
82
|
+
/** Reusable SHA-224 / SHA-256 message schedule buffer `W_t` from RFC 6234 §6.2 step 1. */
|
|
83
|
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
84
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
85
|
+
var SHA2_32B = class extends HashMD {
|
|
86
|
+
constructor(outputLen) {
|
|
87
|
+
super(64, outputLen, 8, false);
|
|
88
|
+
}
|
|
89
|
+
get() {
|
|
90
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
91
|
+
return [
|
|
92
|
+
A,
|
|
93
|
+
B,
|
|
94
|
+
C,
|
|
95
|
+
D,
|
|
96
|
+
E,
|
|
97
|
+
F,
|
|
98
|
+
G,
|
|
99
|
+
H
|
|
100
|
+
];
|
|
101
|
+
}
|
|
102
|
+
set(A, B, C, D, E, F, G, H) {
|
|
103
|
+
this.A = A | 0;
|
|
104
|
+
this.B = B | 0;
|
|
105
|
+
this.C = C | 0;
|
|
106
|
+
this.D = D | 0;
|
|
107
|
+
this.E = E | 0;
|
|
108
|
+
this.F = F | 0;
|
|
109
|
+
this.G = G | 0;
|
|
110
|
+
this.H = H | 0;
|
|
111
|
+
}
|
|
112
|
+
process(view, offset) {
|
|
113
|
+
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
|
114
|
+
for (let i = 16; i < 64; i++) {
|
|
115
|
+
const W15 = SHA256_W[i - 15];
|
|
116
|
+
const W2 = SHA256_W[i - 2];
|
|
117
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
118
|
+
SHA256_W[i] = (rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10) + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
119
|
+
}
|
|
120
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
121
|
+
for (let i = 0; i < 64; i++) {
|
|
122
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
123
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
124
|
+
const T2 = (rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22)) + Maj(A, B, C) | 0;
|
|
125
|
+
H = G;
|
|
126
|
+
G = F;
|
|
127
|
+
F = E;
|
|
128
|
+
E = D + T1 | 0;
|
|
129
|
+
D = C;
|
|
130
|
+
C = B;
|
|
131
|
+
B = A;
|
|
132
|
+
A = T1 + T2 | 0;
|
|
133
|
+
}
|
|
134
|
+
A = A + this.A | 0;
|
|
135
|
+
B = B + this.B | 0;
|
|
136
|
+
C = C + this.C | 0;
|
|
137
|
+
D = D + this.D | 0;
|
|
138
|
+
E = E + this.E | 0;
|
|
139
|
+
F = F + this.F | 0;
|
|
140
|
+
G = G + this.G | 0;
|
|
141
|
+
H = H + this.H | 0;
|
|
142
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
143
|
+
}
|
|
144
|
+
roundClean() {
|
|
145
|
+
clean(SHA256_W);
|
|
146
|
+
}
|
|
147
|
+
destroy() {
|
|
148
|
+
this.destroyed = true;
|
|
149
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
150
|
+
clean(this.buffer);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
154
|
+
var _SHA256 = class extends SHA2_32B {
|
|
155
|
+
A = SHA256_IV[0] | 0;
|
|
156
|
+
B = SHA256_IV[1] | 0;
|
|
157
|
+
C = SHA256_IV[2] | 0;
|
|
158
|
+
D = SHA256_IV[3] | 0;
|
|
159
|
+
E = SHA256_IV[4] | 0;
|
|
160
|
+
F = SHA256_IV[5] | 0;
|
|
161
|
+
G = SHA256_IV[6] | 0;
|
|
162
|
+
H = SHA256_IV[7] | 0;
|
|
163
|
+
constructor() {
|
|
164
|
+
super(32);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
|
|
169
|
+
*
|
|
170
|
+
* - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
|
|
171
|
+
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
172
|
+
* - Each sha256 hash is executing 2^18 bit operations.
|
|
173
|
+
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
174
|
+
* @param msg - message bytes to hash
|
|
175
|
+
* @returns Digest bytes.
|
|
176
|
+
* @example
|
|
177
|
+
* Hash a message with SHA2-256.
|
|
178
|
+
* ```ts
|
|
179
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
const sha256 = /* @__PURE__ */ createHasher(() => new _SHA256(), /* @__PURE__ */ oidNist(1));
|
|
183
|
+
|
|
184
|
+
//#endregion
|
|
185
|
+
export { sha256 };
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
//#region ../../node_modules/@noble/hashes/utils.js
|
|
2
|
+
/**
|
|
3
|
+
* Checks if something is Uint8Array. Be careful: nodejs Buffer will return true.
|
|
4
|
+
* @param a - value to test
|
|
5
|
+
* @returns `true` when the value is a Uint8Array-compatible view.
|
|
6
|
+
* @example
|
|
7
|
+
* Check whether a value is a Uint8Array-compatible view.
|
|
8
|
+
* ```ts
|
|
9
|
+
* isBytes(new Uint8Array([1, 2, 3]));
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
function isBytes(a) {
|
|
13
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array" && "BYTES_PER_ELEMENT" in a && a.BYTES_PER_ELEMENT === 1;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Asserts something is Uint8Array.
|
|
17
|
+
* @param value - value to validate
|
|
18
|
+
* @param length - optional exact length constraint
|
|
19
|
+
* @param title - label included in thrown errors
|
|
20
|
+
* @returns The validated byte array.
|
|
21
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
22
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
23
|
+
* @example
|
|
24
|
+
* Validate that a value is a byte array.
|
|
25
|
+
* ```ts
|
|
26
|
+
* abytes(new Uint8Array([1, 2, 3]));
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
function abytes(value, length, title = "") {
|
|
30
|
+
const bytes = isBytes(value);
|
|
31
|
+
const len = value?.length;
|
|
32
|
+
const needsLen = length !== void 0;
|
|
33
|
+
if (!bytes || needsLen && len !== length) {
|
|
34
|
+
const prefix = title && `"${title}" `;
|
|
35
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
36
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
37
|
+
const message = prefix + "expected Uint8Array" + ofLen + ", got " + got;
|
|
38
|
+
if (!bytes) throw new TypeError(message);
|
|
39
|
+
throw new RangeError(message);
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Asserts a hash instance has not been destroyed or finished.
|
|
45
|
+
* @param instance - hash instance to validate
|
|
46
|
+
* @param checkFinished - whether to reject finalized instances
|
|
47
|
+
* @throws If the hash instance has already been destroyed or finalized. {@link Error}
|
|
48
|
+
* @example
|
|
49
|
+
* Validate that a hash instance is still usable.
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { aexists } from '@noble/hashes/utils.js';
|
|
52
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
53
|
+
* const hash = sha256.create();
|
|
54
|
+
* aexists(hash);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
function aexists(instance, checkFinished = true) {
|
|
58
|
+
if (instance.destroyed) throw new Error("Hash instance has been destroyed");
|
|
59
|
+
if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Asserts output is a sufficiently-sized byte array.
|
|
63
|
+
* @param out - destination buffer
|
|
64
|
+
* @param instance - hash instance providing output length
|
|
65
|
+
* Oversized buffers are allowed; downstream code only promises to fill the first `outputLen` bytes.
|
|
66
|
+
* @throws On wrong argument types. {@link TypeError}
|
|
67
|
+
* @throws On wrong argument ranges or values. {@link RangeError}
|
|
68
|
+
* @example
|
|
69
|
+
* Validate a caller-provided digest buffer.
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { aoutput } from '@noble/hashes/utils.js';
|
|
72
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
73
|
+
* const hash = sha256.create();
|
|
74
|
+
* aoutput(new Uint8Array(hash.outputLen), hash);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
function aoutput(out, instance) {
|
|
78
|
+
abytes(out, void 0, "digestInto() output");
|
|
79
|
+
const min = instance.outputLen;
|
|
80
|
+
if (out.length < min) throw new RangeError("\"digestInto() output\" expected to be of length >=" + min);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Zeroizes typed arrays in place. Warning: JS provides no guarantees.
|
|
84
|
+
* @param arrays - arrays to overwrite with zeros
|
|
85
|
+
* @example
|
|
86
|
+
* Zeroize sensitive buffers in place.
|
|
87
|
+
* ```ts
|
|
88
|
+
* clean(new Uint8Array([1, 2, 3]));
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
function clean(...arrays) {
|
|
92
|
+
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a DataView for byte-level manipulation.
|
|
96
|
+
* @param arr - source typed array
|
|
97
|
+
* @returns DataView over the same buffer region.
|
|
98
|
+
* @example
|
|
99
|
+
* Create a DataView over an existing buffer.
|
|
100
|
+
* ```ts
|
|
101
|
+
* createView(new Uint8Array(4));
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
function createView(arr) {
|
|
105
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Rotate-right operation for uint32 values.
|
|
109
|
+
* @param word - source word
|
|
110
|
+
* @param shift - shift amount in bits
|
|
111
|
+
* @returns Rotated word.
|
|
112
|
+
* @example
|
|
113
|
+
* Rotate a 32-bit word to the right.
|
|
114
|
+
* ```ts
|
|
115
|
+
* rotr(0x12345678, 8);
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
function rotr(word, shift) {
|
|
119
|
+
return word << 32 - shift | word >>> shift;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Creates a callable hash function from a stateful class constructor.
|
|
123
|
+
* @param hashCons - hash constructor or factory
|
|
124
|
+
* @param info - optional metadata such as DER OID
|
|
125
|
+
* @returns Frozen callable hash wrapper with `.create()`.
|
|
126
|
+
* Wrapper construction eagerly calls `hashCons(undefined)` once to read
|
|
127
|
+
* `outputLen` / `blockLen`, so constructor side effects happen at module
|
|
128
|
+
* init time.
|
|
129
|
+
* @example
|
|
130
|
+
* Wrap a stateful hash constructor into a callable helper.
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { createHasher } from '@noble/hashes/utils.js';
|
|
133
|
+
* import { sha256 } from '@noble/hashes/sha2.js';
|
|
134
|
+
* const wrapped = createHasher(sha256.create, { oid: sha256.oid });
|
|
135
|
+
* wrapped(new Uint8Array([1]));
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
function createHasher(hashCons, info = {}) {
|
|
139
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
140
|
+
const tmp = hashCons(void 0);
|
|
141
|
+
hashC.outputLen = tmp.outputLen;
|
|
142
|
+
hashC.blockLen = tmp.blockLen;
|
|
143
|
+
hashC.canXOF = tmp.canXOF;
|
|
144
|
+
hashC.create = (opts) => hashCons(opts);
|
|
145
|
+
Object.assign(hashC, info);
|
|
146
|
+
return Object.freeze(hashC);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Creates OID metadata for NIST hashes with prefix `06 09 60 86 48 01 65 03 04 02`.
|
|
150
|
+
* @param suffix - final OID byte for the selected hash.
|
|
151
|
+
* The helper accepts any byte even though only the documented NIST hash
|
|
152
|
+
* suffixes are meaningful downstream.
|
|
153
|
+
* @returns Object containing the DER-encoded OID.
|
|
154
|
+
* @example
|
|
155
|
+
* Build OID metadata for a NIST hash.
|
|
156
|
+
* ```ts
|
|
157
|
+
* oidNist(0x01);
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
const oidNist = (suffix) => ({ oid: Uint8Array.from([
|
|
161
|
+
6,
|
|
162
|
+
9,
|
|
163
|
+
96,
|
|
164
|
+
134,
|
|
165
|
+
72,
|
|
166
|
+
1,
|
|
167
|
+
101,
|
|
168
|
+
3,
|
|
169
|
+
4,
|
|
170
|
+
2,
|
|
171
|
+
suffix
|
|
172
|
+
]) });
|
|
173
|
+
|
|
174
|
+
//#endregion
|
|
175
|
+
export { abytes, aexists, aoutput, clean, createHasher, createView, oidNist, rotr };
|
package/esm/url.mjs
CHANGED
|
@@ -4,20 +4,29 @@ import ipaddr from "ipaddr.js";
|
|
|
4
4
|
|
|
5
5
|
//#region src/url.ts
|
|
6
6
|
const MAX_URL_LENGTH = 256;
|
|
7
|
+
function stripTrailingDots(host) {
|
|
8
|
+
return host.replace(/\.+$/, "");
|
|
9
|
+
}
|
|
10
|
+
function stripIpv6Brackets(host) {
|
|
11
|
+
const trimmed = host.trim();
|
|
12
|
+
if (trimmed.startsWith("[") && trimmed.endsWith("]")) return trimmed.slice(1, -1);
|
|
13
|
+
return trimmed;
|
|
14
|
+
}
|
|
15
|
+
function normalizeHostname(host) {
|
|
16
|
+
return stripTrailingDots(stripIpv6Brackets(stripTrailingDots(host)));
|
|
17
|
+
}
|
|
7
18
|
function parseIp(host) {
|
|
8
|
-
|
|
19
|
+
const normalized = normalizeHostname(host);
|
|
20
|
+
if (!normalized || !ipaddr.isValid(normalized)) return null;
|
|
9
21
|
try {
|
|
10
|
-
return ipaddr.parse(
|
|
22
|
+
return ipaddr.parse(normalized);
|
|
11
23
|
} catch {
|
|
12
24
|
return null;
|
|
13
25
|
}
|
|
14
26
|
}
|
|
15
|
-
function stripTrailingDots(host) {
|
|
16
|
-
return host.replace(/\.+$/, "");
|
|
17
|
-
}
|
|
18
27
|
function isBlockedHostname(host, blockedHosts, blockedSuffixes) {
|
|
19
|
-
const normalized =
|
|
20
|
-
if (blockedHosts.some((entry) =>
|
|
28
|
+
const normalized = normalizeHostname(host).toLowerCase();
|
|
29
|
+
if (blockedHosts.some((entry) => normalizeHostname(entry).toLowerCase() === normalized)) return true;
|
|
21
30
|
if (blockedSuffixes.some((suffix) => {
|
|
22
31
|
const trimmed = stripTrailingDots(suffix).toLowerCase();
|
|
23
32
|
const clean = trimmed.replace(/^\.+/, "");
|
|
@@ -56,7 +65,7 @@ async function verifyUrl(url, options = {}) {
|
|
|
56
65
|
if (url.length > maxLength) throw new Error(`URL exceeds maximum length of ${maxLength} characters: ${url.length}`);
|
|
57
66
|
const { hostname, protocol } = new URL(url);
|
|
58
67
|
if (!protocols.includes(protocol)) throw new Error(`Protocol '${protocol}' is not allowed. Allowed protocols: ${protocols.join(", ")}`);
|
|
59
|
-
const normalizedHost =
|
|
68
|
+
const normalizedHost = normalizeHostname(hostname);
|
|
60
69
|
if (isBlockedHostname(normalizedHost, blockedHosts, blockedSuffixes)) throw new Error(`Hostname '${normalizedHost}' is blocked`);
|
|
61
70
|
const isIP = Boolean(parseIp(normalizedHost));
|
|
62
71
|
if (isIP) {
|