x402-proxy 0.10.7 → 0.10.9
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/CHANGELOG.md +23 -1
- package/README.md +7 -1
- package/dist/Credential-COZQnr1-.js +2055 -0
- package/dist/Mcp-CrCEqLqO.js +10 -0
- package/dist/Sse-ChldYgU7.js +9742 -0
- package/dist/Sse-kCB38G56.js +16482 -0
- package/dist/accounts-DsuvWwph.js +232 -0
- package/dist/accounts-DzvAlQRn.js +5 -0
- package/dist/accounts-IG-Cmrwy.js +229 -0
- package/dist/api-CUzmQvTQ.js +2802 -0
- package/dist/auth-DTzQmnZ_.js +1196 -0
- package/dist/bin/cli.js +585 -242
- package/dist/ccip-Bx-zoUCJ.js +240 -0
- package/dist/ccip-C2k1DD1T.js +153 -0
- package/dist/ccip-C6CQOJYv.js +152 -0
- package/dist/ccip-RZzsZ5Mv.js +156 -0
- package/dist/chain-CafcHffR.js +1997 -0
- package/dist/chain-DwfP5RGZ.js +1968 -0
- package/dist/chunk-DBEY4PJZ.js +16 -0
- package/dist/chunk-DjEMn6fM.js +36 -0
- package/dist/client-Blw2V7LF.js +657 -0
- package/dist/client-C37gWJOZ.js +102 -0
- package/dist/client-CEc4NYAA.js +6388 -0
- package/dist/client-CVDTUY0l.js +5152 -0
- package/dist/config-BUQsit4s.js +3 -0
- package/dist/config-DR1Fs_wL.js +6600 -0
- package/dist/{config-D9wIR3xc.js → config-rvKA3SYT.js} +10 -5
- package/dist/decodeFunctionData-DuFcwhC_.js +4510 -0
- package/dist/decodeFunctionData-JPOUdvil.js +4394 -0
- package/dist/derive-DNUl8LU9.js +9109 -0
- package/dist/dist-C2YO6HSQ.js +6581 -0
- package/dist/dist-DM5_F3r5.js +4 -0
- package/dist/dist-DxJCYyL5.js +1388 -0
- package/dist/hashTypedData-BHmP9dBd.js +859 -0
- package/dist/hashTypedData-CtEdfx4y.js +846 -0
- package/dist/helpers-CuUSw-tH.js +7125 -0
- package/dist/hmac-59IlS_by.js +648 -0
- package/dist/http-BAtucMbS.js +2060 -0
- package/dist/index.d.ts +1903 -9
- package/dist/index.js +18006 -50
- package/dist/index.node-CxkL0OFh.js +3592 -0
- package/dist/index.node-DvmeuZBj.js +3 -0
- package/dist/isAddressEqual-BLrd1Hg1.js +9 -0
- package/dist/isAddressEqual-DsAqfQOD.js +10 -0
- package/dist/localBatchGatewayRequest-C-RPJyDO.js +6260 -0
- package/dist/localBatchGatewayRequest-DOdQ9bR7.js +93 -0
- package/dist/localBatchGatewayRequest-DQkbZaSy.js +6261 -0
- package/dist/parseUnits-CApwcKSD.js +49 -0
- package/dist/parseUnits-cMO2udMe.js +48 -0
- package/dist/schemas-BxMFYNbH.js +1270 -0
- package/dist/secp256k1-BZpiyffY.js +2525 -0
- package/dist/secp256k1-BjenrLl5.js +1877 -0
- package/dist/secp256k1-CLPUX17u.js +3 -0
- package/dist/sendRawTransactionSync-DvSkhZtW.js +3612 -0
- package/dist/server-CSq0IuUq.js +565 -0
- package/dist/setup-BY4J49Lv.js +1110 -0
- package/dist/setup-wMOAgrsN.js +3 -0
- package/dist/sha256-FAs0qeni.js +17 -0
- package/dist/sha3-CYkWM8Xa.js +195 -0
- package/dist/sha3-DbMJRJ3C.js +194 -0
- package/dist/sse-B4LLqBQm.js +408 -0
- package/dist/status-Bu23RjW6.js +3 -0
- package/dist/{status-DihAcUSC.js → status-X21VnGUO.js} +16 -15
- package/dist/stdio-BADqxZdZ.js +85 -0
- package/dist/streamableHttp-BHkJypcI.js +358 -0
- package/dist/tempo-3nttrxgQ.js +17 -0
- package/dist/tempo-DER0P-ul.js +18 -0
- package/dist/types-BEKUz-Mf.js +1240 -0
- package/dist/types-DatK5vR5.js +3 -0
- package/dist/utils-BYjkXZDF.js +444 -0
- package/dist/utils-SeGHMW9O.js +445 -0
- package/dist/wallet-DKVlrR1S.js +3 -0
- package/dist/wallet-DSyht15_.js +17759 -0
- package/package.json +18 -71
- package/dist/config-B_upkJeK.js +0 -66
- package/dist/config-Be35NM5s.js +0 -3
- package/dist/config-J1m-CWXT.js +0 -27
- package/dist/derive-CL6e8K0Z.js +0 -81
- package/dist/openclaw/plugin.d.ts +0 -15
- package/dist/openclaw/plugin.js +0 -2067
- package/dist/openclaw.plugin.json +0 -93
- package/dist/setup-CNyMLnM-.js +0 -197
- package/dist/setup-DTIxPe58.js +0 -3
- package/dist/status-DZlJ4pS7.js +0 -3
- package/dist/wallet-B0S-rma9.js +0 -544
- package/dist/wallet-DBrVZJqe.js +0 -3
- package/openclaw.plugin.json +0 -93
- package/skills/SKILL.md +0 -183
- package/skills/references/library.md +0 -85
- package/skills/references/openclaw-plugin.md +0 -145
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as nc from "node:crypto";
|
|
3
|
+
//#region node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/_u64.js
|
|
4
|
+
/**
|
|
5
|
+
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
|
6
|
+
* @todo re-check https://issues.chromium.org/issues/42212588
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
10
|
+
const _32n = /* @__PURE__ */ BigInt(32);
|
|
11
|
+
function fromBig(n, le = false) {
|
|
12
|
+
if (le) return {
|
|
13
|
+
h: Number(n & U32_MASK64),
|
|
14
|
+
l: Number(n >> _32n & U32_MASK64)
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
18
|
+
l: Number(n & U32_MASK64) | 0
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function split(lst, le = false) {
|
|
22
|
+
const len = lst.length;
|
|
23
|
+
let Ah = new Uint32Array(len);
|
|
24
|
+
let Al = new Uint32Array(len);
|
|
25
|
+
for (let i = 0; i < len; i++) {
|
|
26
|
+
const { h, l } = fromBig(lst[i], le);
|
|
27
|
+
[Ah[i], Al[i]] = [h, l];
|
|
28
|
+
}
|
|
29
|
+
return [Ah, Al];
|
|
30
|
+
}
|
|
31
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
32
|
+
const shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
33
|
+
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
34
|
+
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
35
|
+
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
36
|
+
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
37
|
+
const rotlSH = (h, l, s) => h << s | l >>> 32 - s;
|
|
38
|
+
const rotlSL = (h, l, s) => l << s | h >>> 32 - s;
|
|
39
|
+
const rotlBH = (h, l, s) => l << s - 32 | h >>> 64 - s;
|
|
40
|
+
const rotlBL = (h, l, s) => h << s - 32 | l >>> 64 - s;
|
|
41
|
+
function add(Ah, Al, Bh, Bl) {
|
|
42
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
43
|
+
return {
|
|
44
|
+
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
45
|
+
l: l | 0
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
49
|
+
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
50
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
51
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
52
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
53
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/cryptoNode.js
|
|
56
|
+
/**
|
|
57
|
+
* Internal webcrypto alias.
|
|
58
|
+
* We prefer WebCrypto aka globalThis.crypto, which exists in node.js 16+.
|
|
59
|
+
* Falls back to Node.js built-in crypto for Node.js <=v14.
|
|
60
|
+
* See utils.ts for details.
|
|
61
|
+
* @module
|
|
62
|
+
*/
|
|
63
|
+
const crypto = nc && typeof nc === "object" && "webcrypto" in nc ? nc.webcrypto : nc && typeof nc === "object" && "randomBytes" in nc ? nc : void 0;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region node_modules/.pnpm/@noble+hashes@1.8.0/node_modules/@noble/hashes/esm/utils.js
|
|
66
|
+
/**
|
|
67
|
+
* Utilities for hex, bytes, CSPRNG.
|
|
68
|
+
* @module
|
|
69
|
+
*/
|
|
70
|
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
71
|
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
72
|
+
function isBytes$1(a) {
|
|
73
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
74
|
+
}
|
|
75
|
+
/** Asserts something is positive integer. */
|
|
76
|
+
function anumber(n) {
|
|
77
|
+
if (!Number.isSafeInteger(n) || n < 0) throw new Error("positive integer expected, got " + n);
|
|
78
|
+
}
|
|
79
|
+
/** Asserts something is Uint8Array. */
|
|
80
|
+
function abytes$1(b, ...lengths) {
|
|
81
|
+
if (!isBytes$1(b)) throw new Error("Uint8Array expected");
|
|
82
|
+
if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
|
|
83
|
+
}
|
|
84
|
+
/** Asserts something is hash */
|
|
85
|
+
function ahash(h) {
|
|
86
|
+
if (typeof h !== "function" || typeof h.create !== "function") throw new Error("Hash should be wrapped by utils.createHasher");
|
|
87
|
+
anumber(h.outputLen);
|
|
88
|
+
anumber(h.blockLen);
|
|
89
|
+
}
|
|
90
|
+
/** Asserts a hash instance has not been destroyed / finished */
|
|
91
|
+
function aexists(instance, checkFinished = true) {
|
|
92
|
+
if (instance.destroyed) throw new Error("Hash instance has been destroyed");
|
|
93
|
+
if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
|
|
94
|
+
}
|
|
95
|
+
/** Asserts output is properly-sized byte array */
|
|
96
|
+
function aoutput(out, instance) {
|
|
97
|
+
abytes$1(out);
|
|
98
|
+
const min = instance.outputLen;
|
|
99
|
+
if (out.length < min) throw new Error("digestInto() expects output buffer of length at least " + min);
|
|
100
|
+
}
|
|
101
|
+
/** Cast u8 / u16 / u32 to u32. */
|
|
102
|
+
function u32(arr) {
|
|
103
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
104
|
+
}
|
|
105
|
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
|
106
|
+
function clean(...arrays) {
|
|
107
|
+
for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
|
|
108
|
+
}
|
|
109
|
+
/** Create DataView of an array for easy byte-level manipulation. */
|
|
110
|
+
function createView(arr) {
|
|
111
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
112
|
+
}
|
|
113
|
+
/** The rotate right (circular right shift) operation for uint32 */
|
|
114
|
+
function rotr(word, shift) {
|
|
115
|
+
return word << 32 - shift | word >>> shift;
|
|
116
|
+
}
|
|
117
|
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
|
118
|
+
const isLE = new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
|
119
|
+
/** The byte swap operation for uint32 */
|
|
120
|
+
function byteSwap(word) {
|
|
121
|
+
return word << 24 & 4278190080 | word << 8 & 16711680 | word >>> 8 & 65280 | word >>> 24 & 255;
|
|
122
|
+
}
|
|
123
|
+
/** In place byte swap for Uint32Array */
|
|
124
|
+
function byteSwap32(arr) {
|
|
125
|
+
for (let i = 0; i < arr.length; i++) arr[i] = byteSwap(arr[i]);
|
|
126
|
+
return arr;
|
|
127
|
+
}
|
|
128
|
+
const swap32IfBE = isLE ? (u) => u : byteSwap32;
|
|
129
|
+
typeof Uint8Array.from([]).toHex === "function" && Uint8Array.fromHex;
|
|
130
|
+
/**
|
|
131
|
+
* Converts string to bytes using UTF8 encoding.
|
|
132
|
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
|
133
|
+
*/
|
|
134
|
+
function utf8ToBytes$1(str) {
|
|
135
|
+
if (typeof str !== "string") throw new Error("string expected");
|
|
136
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
|
140
|
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
|
141
|
+
* Keep in mind for future mutable operations.
|
|
142
|
+
*/
|
|
143
|
+
function toBytes(data) {
|
|
144
|
+
if (typeof data === "string") data = utf8ToBytes$1(data);
|
|
145
|
+
abytes$1(data);
|
|
146
|
+
return data;
|
|
147
|
+
}
|
|
148
|
+
/** Copies several Uint8Arrays into one. */
|
|
149
|
+
function concatBytes$1(...arrays) {
|
|
150
|
+
let sum = 0;
|
|
151
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
152
|
+
const a = arrays[i];
|
|
153
|
+
abytes$1(a);
|
|
154
|
+
sum += a.length;
|
|
155
|
+
}
|
|
156
|
+
const res = new Uint8Array(sum);
|
|
157
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
158
|
+
const a = arrays[i];
|
|
159
|
+
res.set(a, pad);
|
|
160
|
+
pad += a.length;
|
|
161
|
+
}
|
|
162
|
+
return res;
|
|
163
|
+
}
|
|
164
|
+
/** For runtime check if class implements interface */
|
|
165
|
+
var Hash = class {};
|
|
166
|
+
/** Wraps hash function, creating an interface on top of it */
|
|
167
|
+
function createHasher(hashCons) {
|
|
168
|
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
|
169
|
+
const tmp = hashCons();
|
|
170
|
+
hashC.outputLen = tmp.outputLen;
|
|
171
|
+
hashC.blockLen = tmp.blockLen;
|
|
172
|
+
hashC.create = () => hashCons();
|
|
173
|
+
return hashC;
|
|
174
|
+
}
|
|
175
|
+
function createXOFer(hashCons) {
|
|
176
|
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
|
177
|
+
const tmp = hashCons({});
|
|
178
|
+
hashC.outputLen = tmp.outputLen;
|
|
179
|
+
hashC.blockLen = tmp.blockLen;
|
|
180
|
+
hashC.create = (opts) => hashCons(opts);
|
|
181
|
+
return hashC;
|
|
182
|
+
}
|
|
183
|
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
|
184
|
+
function randomBytes(bytesLength = 32) {
|
|
185
|
+
if (crypto && typeof crypto.getRandomValues === "function") return crypto.getRandomValues(new Uint8Array(bytesLength));
|
|
186
|
+
if (crypto && typeof crypto.randomBytes === "function") return Uint8Array.from(crypto.randomBytes(bytesLength));
|
|
187
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
188
|
+
}
|
|
189
|
+
//#endregion
|
|
190
|
+
//#region node_modules/.pnpm/@noble+curves@1.9.1/node_modules/@noble/curves/esm/abstract/utils.js
|
|
191
|
+
/**
|
|
192
|
+
* Hex, bytes and number utilities.
|
|
193
|
+
* @module
|
|
194
|
+
*/
|
|
195
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
196
|
+
const _0n = /* @__PURE__ */ BigInt(0);
|
|
197
|
+
const _1n = /* @__PURE__ */ BigInt(1);
|
|
198
|
+
function isBytes(a) {
|
|
199
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
200
|
+
}
|
|
201
|
+
function abytes(item) {
|
|
202
|
+
if (!isBytes(item)) throw new Error("Uint8Array expected");
|
|
203
|
+
}
|
|
204
|
+
function abool(title, value) {
|
|
205
|
+
if (typeof value !== "boolean") throw new Error(title + " boolean expected, got " + value);
|
|
206
|
+
}
|
|
207
|
+
function numberToHexUnpadded(num) {
|
|
208
|
+
const hex = num.toString(16);
|
|
209
|
+
return hex.length & 1 ? "0" + hex : hex;
|
|
210
|
+
}
|
|
211
|
+
function hexToNumber(hex) {
|
|
212
|
+
if (typeof hex !== "string") throw new Error("hex string expected, got " + typeof hex);
|
|
213
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
214
|
+
}
|
|
215
|
+
const hasHexBuiltin = typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function";
|
|
216
|
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
217
|
+
/**
|
|
218
|
+
* Convert byte array to hex string. Uses built-in function, when available.
|
|
219
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
220
|
+
*/
|
|
221
|
+
function bytesToHex(bytes) {
|
|
222
|
+
abytes(bytes);
|
|
223
|
+
if (hasHexBuiltin) return bytes.toHex();
|
|
224
|
+
let hex = "";
|
|
225
|
+
for (let i = 0; i < bytes.length; i++) hex += hexes[bytes[i]];
|
|
226
|
+
return hex;
|
|
227
|
+
}
|
|
228
|
+
const asciis = {
|
|
229
|
+
_0: 48,
|
|
230
|
+
_9: 57,
|
|
231
|
+
A: 65,
|
|
232
|
+
F: 70,
|
|
233
|
+
a: 97,
|
|
234
|
+
f: 102
|
|
235
|
+
};
|
|
236
|
+
function asciiToBase16(ch) {
|
|
237
|
+
if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0;
|
|
238
|
+
if (ch >= asciis.A && ch <= asciis.F) return ch - (asciis.A - 10);
|
|
239
|
+
if (ch >= asciis.a && ch <= asciis.f) return ch - (asciis.a - 10);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Convert hex string to byte array. Uses built-in function, when available.
|
|
243
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
244
|
+
*/
|
|
245
|
+
function hexToBytes(hex) {
|
|
246
|
+
if (typeof hex !== "string") throw new Error("hex string expected, got " + typeof hex);
|
|
247
|
+
if (hasHexBuiltin) return Uint8Array.fromHex(hex);
|
|
248
|
+
const hl = hex.length;
|
|
249
|
+
const al = hl / 2;
|
|
250
|
+
if (hl % 2) throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
251
|
+
const array = new Uint8Array(al);
|
|
252
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
253
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
254
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
255
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
256
|
+
const char = hex[hi] + hex[hi + 1];
|
|
257
|
+
throw new Error("hex string expected, got non-hex character \"" + char + "\" at index " + hi);
|
|
258
|
+
}
|
|
259
|
+
array[ai] = n1 * 16 + n2;
|
|
260
|
+
}
|
|
261
|
+
return array;
|
|
262
|
+
}
|
|
263
|
+
function bytesToNumberBE(bytes) {
|
|
264
|
+
return hexToNumber(bytesToHex(bytes));
|
|
265
|
+
}
|
|
266
|
+
function bytesToNumberLE(bytes) {
|
|
267
|
+
abytes(bytes);
|
|
268
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
269
|
+
}
|
|
270
|
+
function numberToBytesBE(n, len) {
|
|
271
|
+
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
272
|
+
}
|
|
273
|
+
function numberToBytesLE(n, len) {
|
|
274
|
+
return numberToBytesBE(n, len).reverse();
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Takes hex string or Uint8Array, converts to Uint8Array.
|
|
278
|
+
* Validates output length.
|
|
279
|
+
* Will throw error for other types.
|
|
280
|
+
* @param title descriptive title for an error e.g. 'private key'
|
|
281
|
+
* @param hex hex string or Uint8Array
|
|
282
|
+
* @param expectedLength optional, will compare to result array's length
|
|
283
|
+
* @returns
|
|
284
|
+
*/
|
|
285
|
+
function ensureBytes(title, hex, expectedLength) {
|
|
286
|
+
let res;
|
|
287
|
+
if (typeof hex === "string") try {
|
|
288
|
+
res = hexToBytes(hex);
|
|
289
|
+
} catch (e) {
|
|
290
|
+
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
|
291
|
+
}
|
|
292
|
+
else if (isBytes(hex)) res = Uint8Array.from(hex);
|
|
293
|
+
else throw new Error(title + " must be hex string or Uint8Array");
|
|
294
|
+
const len = res.length;
|
|
295
|
+
if (typeof expectedLength === "number" && len !== expectedLength) throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
|
296
|
+
return res;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Copies several Uint8Arrays into one.
|
|
300
|
+
*/
|
|
301
|
+
function concatBytes(...arrays) {
|
|
302
|
+
let sum = 0;
|
|
303
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
304
|
+
const a = arrays[i];
|
|
305
|
+
abytes(a);
|
|
306
|
+
sum += a.length;
|
|
307
|
+
}
|
|
308
|
+
const res = new Uint8Array(sum);
|
|
309
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
310
|
+
const a = arrays[i];
|
|
311
|
+
res.set(a, pad);
|
|
312
|
+
pad += a.length;
|
|
313
|
+
}
|
|
314
|
+
return res;
|
|
315
|
+
}
|
|
316
|
+
function equalBytes(a, b) {
|
|
317
|
+
if (a.length !== b.length) return false;
|
|
318
|
+
let diff = 0;
|
|
319
|
+
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
320
|
+
return diff === 0;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
324
|
+
*/
|
|
325
|
+
function utf8ToBytes(str) {
|
|
326
|
+
if (typeof str !== "string") throw new Error("string expected");
|
|
327
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
328
|
+
}
|
|
329
|
+
const isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
330
|
+
function inRange(n, min, max) {
|
|
331
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Asserts min <= n < max. NOTE: It's < max and not <= max.
|
|
335
|
+
* @example
|
|
336
|
+
* aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
|
|
337
|
+
*/
|
|
338
|
+
function aInRange(title, n, min, max) {
|
|
339
|
+
if (!inRange(n, min, max)) throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Calculates amount of bits in a bigint.
|
|
343
|
+
* Same as `n.toString(2).length`
|
|
344
|
+
* TODO: merge with nLength in modular
|
|
345
|
+
*/
|
|
346
|
+
function bitLen(n) {
|
|
347
|
+
let len;
|
|
348
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1);
|
|
349
|
+
return len;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Calculate mask for N bits. Not using ** operator with bigints because of old engines.
|
|
353
|
+
* Same as BigInt(`0b${Array(i).fill('1').join('')}`)
|
|
354
|
+
*/
|
|
355
|
+
const bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
356
|
+
const u8n = (len) => new Uint8Array(len);
|
|
357
|
+
const u8fr = (arr) => Uint8Array.from(arr);
|
|
358
|
+
/**
|
|
359
|
+
* Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
360
|
+
* @returns function that will call DRBG until 2nd arg returns something meaningful
|
|
361
|
+
* @example
|
|
362
|
+
* const drbg = createHmacDRBG<Key>(32, 32, hmac);
|
|
363
|
+
* drbg(seed, bytesToKey); // bytesToKey must return Key or undefined
|
|
364
|
+
*/
|
|
365
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
366
|
+
if (typeof hashLen !== "number" || hashLen < 2) throw new Error("hashLen must be a number");
|
|
367
|
+
if (typeof qByteLen !== "number" || qByteLen < 2) throw new Error("qByteLen must be a number");
|
|
368
|
+
if (typeof hmacFn !== "function") throw new Error("hmacFn must be a function");
|
|
369
|
+
let v = u8n(hashLen);
|
|
370
|
+
let k = u8n(hashLen);
|
|
371
|
+
let i = 0;
|
|
372
|
+
const reset = () => {
|
|
373
|
+
v.fill(1);
|
|
374
|
+
k.fill(0);
|
|
375
|
+
i = 0;
|
|
376
|
+
};
|
|
377
|
+
const h = (...b) => hmacFn(k, v, ...b);
|
|
378
|
+
const reseed = (seed = u8n(0)) => {
|
|
379
|
+
k = h(u8fr([0]), seed);
|
|
380
|
+
v = h();
|
|
381
|
+
if (seed.length === 0) return;
|
|
382
|
+
k = h(u8fr([1]), seed);
|
|
383
|
+
v = h();
|
|
384
|
+
};
|
|
385
|
+
const gen = () => {
|
|
386
|
+
if (i++ >= 1e3) throw new Error("drbg: tried 1000 values");
|
|
387
|
+
let len = 0;
|
|
388
|
+
const out = [];
|
|
389
|
+
while (len < qByteLen) {
|
|
390
|
+
v = h();
|
|
391
|
+
const sl = v.slice();
|
|
392
|
+
out.push(sl);
|
|
393
|
+
len += v.length;
|
|
394
|
+
}
|
|
395
|
+
return concatBytes(...out);
|
|
396
|
+
};
|
|
397
|
+
const genUntil = (seed, pred) => {
|
|
398
|
+
reset();
|
|
399
|
+
reseed(seed);
|
|
400
|
+
let res = void 0;
|
|
401
|
+
while (!(res = pred(gen()))) reseed();
|
|
402
|
+
reset();
|
|
403
|
+
return res;
|
|
404
|
+
};
|
|
405
|
+
return genUntil;
|
|
406
|
+
}
|
|
407
|
+
const validatorFns = {
|
|
408
|
+
bigint: (val) => typeof val === "bigint",
|
|
409
|
+
function: (val) => typeof val === "function",
|
|
410
|
+
boolean: (val) => typeof val === "boolean",
|
|
411
|
+
string: (val) => typeof val === "string",
|
|
412
|
+
stringOrUint8Array: (val) => typeof val === "string" || isBytes(val),
|
|
413
|
+
isSafeInteger: (val) => Number.isSafeInteger(val),
|
|
414
|
+
array: (val) => Array.isArray(val),
|
|
415
|
+
field: (val, object) => object.Fp.isValid(val),
|
|
416
|
+
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
|
417
|
+
};
|
|
418
|
+
function validateObject(object, validators, optValidators = {}) {
|
|
419
|
+
const checkField = (fieldName, type, isOptional) => {
|
|
420
|
+
const checkVal = validatorFns[type];
|
|
421
|
+
if (typeof checkVal !== "function") throw new Error("invalid validator function");
|
|
422
|
+
const val = object[fieldName];
|
|
423
|
+
if (isOptional && val === void 0) return;
|
|
424
|
+
if (!checkVal(val, object)) throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val);
|
|
425
|
+
};
|
|
426
|
+
for (const [fieldName, type] of Object.entries(validators)) checkField(fieldName, type, false);
|
|
427
|
+
for (const [fieldName, type] of Object.entries(optValidators)) checkField(fieldName, type, true);
|
|
428
|
+
return object;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Memoizes (caches) computation result.
|
|
432
|
+
* Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
|
|
433
|
+
*/
|
|
434
|
+
function memoized(fn) {
|
|
435
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
436
|
+
return (arg, ...args) => {
|
|
437
|
+
const val = map.get(arg);
|
|
438
|
+
if (val !== void 0) return val;
|
|
439
|
+
const computed = fn(arg, ...args);
|
|
440
|
+
map.set(arg, computed);
|
|
441
|
+
return computed;
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
//#endregion
|
|
445
|
+
export { shrSH as $, createHasher as A, add3L as B, abytes$1 as C, aoutput as D, anumber as E, swap32IfBE as F, rotlBH as G, add4L as H, toBytes as I, rotlSL as J, rotlBL as K, u32 as L, createXOFer as M, randomBytes as N, clean as O, rotr as P, rotrSL as Q, add as R, Hash as S, ahash as T, add5H as U, add4H as V, add5L as W, rotrBL as X, rotrBH as Y, rotrSH as Z, numberToBytesBE as _, bitMask as a, utf8ToBytes as b, bytesToNumberLE as c, ensureBytes as d, shrSL as et, equalBytes as f, memoized as g, isBytes as h, bitLen as i, createView as j, concatBytes$1 as k, concatBytes as l, inRange as m, abool as n, bytesToHex as o, hexToBytes as p, rotlSH as q, abytes as r, bytesToNumberBE as s, aInRange as t, split as tt, createHmacDrbg as u, numberToBytesLE as v, aexists as w, validateObject as x, numberToHexUnpadded as y, add3H as z };
|