@dvmkit/dvm-cli 0.5.0 → 0.7.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/dist/{_esm-MN4BUYV6.js → _esm-KJAA4KAJ.js} +7 -7
- package/dist/{actions-LR65XILR.js → actions-J7VGYDBF.js} +4 -4
- package/dist/{ccip-JXYJSXTW.js → ccip-CJFG4PLA.js} +1 -1
- package/dist/{chains-LJ7XK6H3.js → chains-J6VIUNUB.js} +4 -4
- package/dist/{chunk-QICJRKB7.js → chunk-32AOWN2U.js} +11032 -12469
- package/dist/chunk-3E4LW5R5.js +77 -0
- package/dist/chunk-5RVUW52C.js +122 -0
- package/dist/{chunk-MJ4X4BEW.js → chunk-6LP4EGKA.js} +61 -61
- package/dist/chunk-6PVFVHS5.js +5460 -0
- package/dist/{chunk-BECPI5H5.js → chunk-B4WHUMZM.js} +36 -36
- package/dist/{chunk-CJTE5TS3.js → chunk-ESOJUXVY.js} +5 -5
- package/dist/{chunk-F55NNH2F.js → chunk-FOWQ7R7S.js} +16 -16
- package/dist/{chunk-ID5G54HJ.js → chunk-FPWP4S5S.js} +5 -3
- package/dist/{chunk-EE2MREOM.js → chunk-IQV4KG63.js} +5 -3
- package/dist/{chunk-R55XNXAR.js → chunk-IUTVV6N5.js} +3 -3
- package/dist/{chunk-Z4CZUMUT.js → chunk-JBU7RKQ3.js} +15 -15
- package/dist/{chunk-SHZMNEXE.js → chunk-JTMBSEZO.js} +2770 -926
- package/dist/{chunk-ERJFRCJO.js → chunk-KDNFUDOG.js} +5 -5
- package/dist/chunk-MWWZX7OQ.js +262 -0
- package/dist/chunk-PATIATFK.js +765 -0
- package/dist/{chunk-WTM2EICO.js → chunk-PUOXBKY2.js} +3 -3
- package/dist/{chunk-GZWKCIYN.js → chunk-QXMKABL7.js} +3 -3
- package/dist/chunk-R2DSAZBO.js +87 -0
- package/dist/chunk-SNSJAOR3.js +43 -0
- package/dist/{chunk-FL2ICZW5.js → chunk-SYFKO6BA.js} +3 -3
- package/dist/{chunk-LH3QPVN6.js → chunk-TAZWRWM4.js} +8060 -8058
- package/dist/chunk-VIRAAIDU.js +409 -0
- package/dist/{chunk-TZWZTS37.js → chunk-WN5R367B.js} +57 -83
- package/dist/{chunk-EZHT2RLJ.js → chunk-X7SEGALJ.js} +3 -3
- package/dist/{chunk-XHOFQ3OS.js → chunk-Y3SQD37C.js} +9 -9
- package/dist/{client-RYDA62DU.js → client-6IHIUB6J.js} +10 -10
- package/dist/client-VHGN7ZFZ.js +36 -0
- package/dist/{dist-GKCEIY2A.js → dist-23E7OOCA.js} +9 -9
- package/dist/dvm.js +2844 -623
- package/dist/{internal-INUTGTXQ.js → internal-7RZYJLLG.js} +325 -508
- package/dist/lifecycle-Z7YUOTR5.js +36 -0
- package/dist/{payout-reporter-3Y2U55ZX-CMKACKA3.js → payout-reporter-WT7XYW42-U2QPDIYC.js} +3 -2
- package/dist/{pocket-refill-K5T4LZF6.js → pocket-refill-CCWF3G4N.js} +33 -30
- package/dist/{revenue-reporter-MAERPYGK-CCPZXGFA.js → revenue-reporter-7UR3W2SD-C5OMRGK3.js} +3 -2
- package/dist/{tempo-AXTE4ZEA.js → tempo-ORFVGUKP.js} +10 -10
- package/dist/tempo-lifecycle-OT75TYVA.js +38 -0
- package/dist/tempo-wallet-SGYOEWVY.js +43 -0
- package/dist/wallet-BRXTQ4BB.js +45 -0
- package/dist/{x402-R6HPBY5D-VMSW7Z32.js → x402-ZU4GML7Z-RYEACIIT.js} +4 -19
- package/package.json +4 -2
- package/skills/dvm/credit.md +4 -0
- package/skills/dvm/wallets.md +4 -0
- package/dist/chunk-3YE43JJR.js +0 -215
- package/dist/chunk-4KINCBHL.js +0 -5153
- package/dist/chunk-FSYEZERJ.js +0 -1667
- package/dist/chunk-VOO5IAK7.js +0 -2318
- package/dist/tempo-lifecycle-ZBBLFBMH-HWI4PLYP.js +0 -32
- package/dist/tempo-wallet-B5XXV3YH-SRQBMYFV.js +0 -41
|
@@ -2,38 +2,58 @@
|
|
|
2
2
|
import { createRequire as __dvmCreateRequire } from "node:module";
|
|
3
3
|
const require = __dvmCreateRequire(import.meta.url);
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
formatStaleness,
|
|
8
|
-
formatUsd,
|
|
9
|
-
fundingReachableAt,
|
|
10
|
-
getLastKnownBtcRate,
|
|
11
|
-
listCredits,
|
|
12
|
-
maxAdvertisedMicro,
|
|
13
|
-
msatsToUsdc,
|
|
14
|
-
usdcToMsats,
|
|
15
|
-
withinAdvertisedTolerance
|
|
16
|
-
} from "./chunk-VOO5IAK7.js";
|
|
17
|
-
import {
|
|
18
|
-
DvmError,
|
|
19
|
-
X402_CHANNELS_FILE,
|
|
20
|
-
authorizeProtectedRemoval,
|
|
21
|
-
ensureConfigDir,
|
|
22
|
-
isValidEvmPrivateKey,
|
|
23
|
-
loadConfig,
|
|
24
|
-
redactUrl,
|
|
25
|
-
redactUrlsInText,
|
|
26
|
-
require_proper_lockfile,
|
|
27
|
-
resolveRpcHttpTransportOptions,
|
|
28
|
-
resolveRpcOverride,
|
|
29
|
-
updateConfig
|
|
30
|
-
} from "./chunk-4KINCBHL.js";
|
|
5
|
+
diagnosticConsole
|
|
6
|
+
} from "./chunk-WN5R367B.js";
|
|
31
7
|
import {
|
|
32
|
-
__export
|
|
33
|
-
__toESM
|
|
8
|
+
__export
|
|
34
9
|
} from "./chunk-IYHTITNZ.js";
|
|
35
10
|
|
|
36
|
-
// node_modules/@dvmkit/sdk/dist/chunk-
|
|
11
|
+
// node_modules/@dvmkit/sdk/dist/chunk-BPYMEMQX.js
|
|
12
|
+
var DvmError = class extends Error {
|
|
13
|
+
constructor(code, message2, hint, data, display, proseSources) {
|
|
14
|
+
super(message2);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.hint = hint;
|
|
17
|
+
this.data = data;
|
|
18
|
+
this.display = display;
|
|
19
|
+
this.proseSources = proseSources;
|
|
20
|
+
this.name = "DvmError";
|
|
21
|
+
}
|
|
22
|
+
code;
|
|
23
|
+
hint;
|
|
24
|
+
data;
|
|
25
|
+
display;
|
|
26
|
+
proseSources;
|
|
27
|
+
};
|
|
28
|
+
function providerProseSources(...sources) {
|
|
29
|
+
const provenance = {};
|
|
30
|
+
for (const source of sources) {
|
|
31
|
+
if (!source) continue;
|
|
32
|
+
for (const field of ["message", "hint", "display"]) {
|
|
33
|
+
if (typeof source[field] === "string") provenance[field] = "provider";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return Object.keys(provenance).length > 0 ? provenance : void 0;
|
|
37
|
+
}
|
|
38
|
+
function isAbortError(err) {
|
|
39
|
+
return err instanceof Error && (err.name === "AbortError" || err.name === "TimeoutError");
|
|
40
|
+
}
|
|
41
|
+
function transportErrorCode(err) {
|
|
42
|
+
let current = err;
|
|
43
|
+
const seen = /* @__PURE__ */ new Set();
|
|
44
|
+
for (let depth = 0; depth < 5 && isErrorLike(current) && !seen.has(current); depth++) {
|
|
45
|
+
seen.add(current);
|
|
46
|
+
const code = current.code;
|
|
47
|
+
if (typeof code === "string" && /^(?:E[A-Z0-9_]{1,63}|UND_ERR_[A-Z0-9_]{1,63})$/.test(code)) {
|
|
48
|
+
return code;
|
|
49
|
+
}
|
|
50
|
+
current = current.cause;
|
|
51
|
+
}
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
function isErrorLike(value) {
|
|
55
|
+
return (typeof value === "object" || typeof value === "function") && value !== null;
|
|
56
|
+
}
|
|
37
57
|
var X402_NETWORKS = [
|
|
38
58
|
{
|
|
39
59
|
caip2: "eip155:8453",
|
|
@@ -129,7 +149,7 @@ function x402SupportedNetworksHint() {
|
|
|
129
149
|
var BY_CAIP2 = new Map(X402_NETWORKS.map((n) => [n.caip2, n]));
|
|
130
150
|
var BY_SLUG = new Map(X402_NETWORKS.map((n) => [n.slug, n]));
|
|
131
151
|
|
|
132
|
-
// node_modules/@dvmkit/sdk/dist/chunk-
|
|
152
|
+
// node_modules/@dvmkit/sdk/dist/chunk-62FI3NJF.js
|
|
133
153
|
var X402_DEFAULT_FACILITATOR = "https://x402.org/facilitator";
|
|
134
154
|
var X402_VERSION = 2;
|
|
135
155
|
var X402_V1_VERSION = 1;
|
|
@@ -327,625 +347,2690 @@ function isRecord(value) {
|
|
|
327
347
|
function isOptionalRecord(value) {
|
|
328
348
|
return value === void 0 || value === null || isRecord(value);
|
|
329
349
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
var import_proper_lockfile = __toESM(require_proper_lockfile(), 1);
|
|
333
|
-
import { writeSync } from "fs";
|
|
334
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
335
|
-
import { existsSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
336
|
-
import { randomBytes } from "crypto";
|
|
337
|
-
function resolveX402Asset(network = X402_DEFAULT_NETWORK) {
|
|
338
|
-
const spec = x402NetworkByCaip2(network);
|
|
339
|
-
if (!spec) return null;
|
|
340
|
-
return {
|
|
341
|
-
address: spec.usdc,
|
|
342
|
-
symbol: "USDC",
|
|
343
|
-
// "native" is the load-bearing word: every contract in the table is
|
|
344
|
-
// Circle's own USDC, and the dollars that read as zero here are the
|
|
345
|
-
// bridged ones.
|
|
346
|
-
label: "native USDC",
|
|
347
|
-
unit: "USDC",
|
|
348
|
-
networkLabel: spec.label,
|
|
349
|
-
confusable: spec.confusable
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
function x402WrongAssetHint(asset) {
|
|
353
|
-
const wrongToken = asset.confusable ? `Another dollar on ${asset.networkLabel} \u2014 ${asset.confusable}, the legacy bridged dollar, or any other stablecoin \u2014 is a different contract and will never show up here, however much of it your wallet says you hold.` : `Another dollar on ${asset.networkLabel} is a different contract and will never show up here, however much of it your wallet says you hold.`;
|
|
354
|
-
return `This rail reads only ${asset.label} (${asset.address}) on ${asset.networkLabel}. ${wrongToken} USDC on a different chain is invisible here too \u2014 this wallet reads ${asset.networkLabel} alone. If you funded the wrong one, swap it to ${asset.label} on ${asset.networkLabel}, or bridge the funds across to ${asset.networkLabel}; either way they land where this rail can spend them.`;
|
|
350
|
+
function msatsToUsd(msats, rate) {
|
|
351
|
+
return msats / 1e11 * rate;
|
|
355
352
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
_traceId = id;
|
|
353
|
+
function usdToMsats(usd, rate) {
|
|
354
|
+
return Math.round(usd / rate * 1e11);
|
|
359
355
|
}
|
|
360
|
-
function
|
|
361
|
-
|
|
356
|
+
function msatsToUsdCents(msats, btcPriceUsd) {
|
|
357
|
+
const usd = msatsToUsd(msats, btcPriceUsd);
|
|
358
|
+
return Math.round(usd * 100);
|
|
362
359
|
}
|
|
363
|
-
function
|
|
364
|
-
|
|
365
|
-
|
|
360
|
+
function msatsToUsdc(msats, btcUsdRate) {
|
|
361
|
+
if (!Number.isSafeInteger(msats) || msats < 0) {
|
|
362
|
+
throw new RangeError("msats must be a non-negative safe integer");
|
|
363
|
+
}
|
|
364
|
+
if (!Number.isFinite(btcUsdRate) || btcUsdRate <= 0) {
|
|
365
|
+
throw new RangeError("BTC/USD rate must be finite and positive");
|
|
366
|
+
}
|
|
367
|
+
const [mantissa, exponent = "0"] = btcUsdRate.toString().split("e");
|
|
368
|
+
const decimalPlaces = mantissa.split(".")[1]?.length ?? 0;
|
|
369
|
+
const shift = Number(exponent) - decimalPlaces;
|
|
370
|
+
let numerator = BigInt(msats) * BigInt(mantissa.replace(".", ""));
|
|
371
|
+
let denominator = 100000n;
|
|
372
|
+
if (shift > 0) numerator *= 10n ** BigInt(shift);
|
|
373
|
+
else if (shift < 0) denominator *= 10n ** BigInt(-shift);
|
|
374
|
+
const micro = (numerator + denominator - 1n) / denominator;
|
|
375
|
+
if (micro > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
376
|
+
throw new RangeError("USDC micro-unit amount exceeds the safe integer range");
|
|
377
|
+
}
|
|
378
|
+
return Number(micro);
|
|
366
379
|
}
|
|
367
|
-
function
|
|
368
|
-
|
|
380
|
+
function usdcToMsats(usdc, btcUsdRate) {
|
|
381
|
+
return usdToMsats(usdc, btcUsdRate);
|
|
369
382
|
}
|
|
370
|
-
function
|
|
371
|
-
|
|
383
|
+
function msatsToMicro(msats, btcRate) {
|
|
384
|
+
return Math.round(msatsToUsd(msats, btcRate) * 1e6);
|
|
372
385
|
}
|
|
373
|
-
function
|
|
374
|
-
|
|
386
|
+
function microToSats(micro, btcRate) {
|
|
387
|
+
return Math.ceil(usdToMsats(micro / 1e6, btcRate) / 1e3);
|
|
375
388
|
}
|
|
376
|
-
function
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
while (offset < buf.length) {
|
|
380
|
-
try {
|
|
381
|
-
offset += writeSync(fd, buf, offset, buf.length - offset);
|
|
382
|
-
} catch (err) {
|
|
383
|
-
const code = err.code;
|
|
384
|
-
if (code === "EAGAIN" || code === "EINTR") continue;
|
|
385
|
-
throw err;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
389
|
+
function resolveRpcOverride(envValue, bundled) {
|
|
390
|
+
const override = envValue?.trim() ?? "";
|
|
391
|
+
return override.length > 0 ? { url: override, source: "override" } : { url: bundled, source: "bundled" };
|
|
388
392
|
}
|
|
389
|
-
|
|
393
|
+
function resolveRpcHttpTransportOptions(source, envValue) {
|
|
394
|
+
if (source !== "override" || !envValue?.trim()) return void 0;
|
|
395
|
+
let parsed;
|
|
390
396
|
try {
|
|
391
|
-
|
|
397
|
+
parsed = JSON.parse(envValue);
|
|
392
398
|
} catch {
|
|
393
|
-
|
|
394
|
-
if (cached) {
|
|
395
|
-
warnStaleRate(cached.cachedAt);
|
|
396
|
-
return { rate: cached.rate, stale: true, cachedAt: cached.cachedAt };
|
|
397
|
-
}
|
|
398
|
-
throw rateUnavailable(purpose);
|
|
399
|
+
throw new Error("RPC headers must be a JSON object of non-empty string values.");
|
|
399
400
|
}
|
|
401
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
402
|
+
throw new Error("RPC headers must be a JSON object of non-empty string values.");
|
|
403
|
+
const headers = {};
|
|
404
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
405
|
+
if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name) || typeof value !== "string" || value.length === 0 || /[\r\n]/.test(value))
|
|
406
|
+
throw new Error("RPC headers must be a JSON object of non-empty string values.");
|
|
407
|
+
headers[name] = value;
|
|
408
|
+
}
|
|
409
|
+
if (Object.keys(headers).length === 0)
|
|
410
|
+
throw new Error("RPC headers must be a JSON object of non-empty string values.");
|
|
411
|
+
return { fetchOptions: { headers } };
|
|
400
412
|
}
|
|
401
|
-
|
|
413
|
+
|
|
414
|
+
// node_modules/@dvmkit/sdk/dist/chunk-KXWROQGK.js
|
|
415
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
416
|
+
var LONG_HEX_RE = /^[0-9a-f]{16,}$/i;
|
|
417
|
+
var NUMERIC_RE = /^\d{4,}$/;
|
|
418
|
+
var BASE58_RE = /^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]{32,}$/;
|
|
419
|
+
var OPAQUE_TOKEN_MIN_LENGTH = 32;
|
|
420
|
+
var OPAQUE_TOKEN_MIN_UNIQUE_CHARS = 8;
|
|
421
|
+
var URL_IN_TEXT_RE = /https?:\/\/[^\s"'`<>]+/gi;
|
|
422
|
+
function redactPath(pathname) {
|
|
423
|
+
const segments = pathname.split("/");
|
|
424
|
+
const redacted = segments.map((seg) => {
|
|
425
|
+
if (!seg) return seg;
|
|
426
|
+
if (UUID_RE.test(seg)) return ":uuid";
|
|
427
|
+
if (LONG_HEX_RE.test(seg)) return ":hex";
|
|
428
|
+
if (NUMERIC_RE.test(seg)) return ":n";
|
|
429
|
+
if (BASE58_RE.test(seg)) return ":b58";
|
|
430
|
+
if (isOpaqueToken(seg)) return ":token";
|
|
431
|
+
return seg;
|
|
432
|
+
});
|
|
433
|
+
return redacted.join("/");
|
|
434
|
+
}
|
|
435
|
+
function redactUrl(url) {
|
|
436
|
+
let parsed;
|
|
402
437
|
try {
|
|
403
|
-
|
|
438
|
+
parsed = new URL(url);
|
|
404
439
|
} catch {
|
|
405
|
-
return
|
|
440
|
+
return url;
|
|
406
441
|
}
|
|
442
|
+
return `${parsed.origin}${redactPath(parsed.pathname)}`;
|
|
407
443
|
}
|
|
408
|
-
function
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
display: `Priced with a BTC/USD rate cached ${age} \u2014 the rate provider is unreachable, so dollar figures may have drifted.`
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
var warnedStale = false;
|
|
418
|
-
function warnStaleRate(cachedAt) {
|
|
419
|
-
if (warnedStale) return;
|
|
420
|
-
warnedStale = true;
|
|
421
|
-
info(
|
|
422
|
-
`Warning: the BTC/USD rate provider is unreachable \u2014 using the last rate this machine saw (cached ${formatStaleness(cachedAt)}). Dollar figures may have drifted.`
|
|
423
|
-
);
|
|
424
|
-
}
|
|
425
|
-
function rateUnavailable(purpose) {
|
|
426
|
-
const outcome = purpose === "payment" ? "nothing was paid" : "no conversion was performed";
|
|
427
|
-
const noun = purpose === "payment" ? "payment" : "conversion";
|
|
428
|
-
const capped = outcome.charAt(0).toUpperCase() + outcome.slice(1);
|
|
429
|
-
return new DvmError(
|
|
430
|
-
"rate_unavailable",
|
|
431
|
-
`Could not price this ${noun}: the BTC/USD rate provider is unreachable and no cached rate is available.`,
|
|
432
|
-
`${capped}. The rate provider (CoinGecko) is rate-limiting or down, and this machine has never cached a rate. Retry in a minute; if it keeps failing, check network access to api.coingecko.com.${purpose === "payment" ? " Budgets are denominated in dollars, so a payment cannot be authorised without a rate." : ""}`,
|
|
433
|
-
{
|
|
434
|
-
display: `BTC/USD rate unavailable \u2014 ${outcome}.`,
|
|
435
|
-
anchor: "rate_unavailable",
|
|
436
|
-
purpose,
|
|
437
|
-
provider: "coingecko",
|
|
438
|
-
retryable: true
|
|
439
|
-
}
|
|
440
|
-
);
|
|
444
|
+
function redactUrlsInText(text) {
|
|
445
|
+
return text.replace(URL_IN_TEXT_RE, (match) => {
|
|
446
|
+
const trailing = /[.,;:!?)\]}]+$/.exec(match)?.[0] ?? "";
|
|
447
|
+
const url = trailing.length > 0 ? match.slice(0, -trailing.length) : match;
|
|
448
|
+
return `${redactUrl(url)}${trailing}`;
|
|
449
|
+
});
|
|
441
450
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
file.exact = [
|
|
451
|
-
...file.exact.filter((entry) => !sameFundingCredit(entry, pending, credits)),
|
|
452
|
-
pending
|
|
453
|
-
];
|
|
454
|
-
saveFile(file);
|
|
451
|
+
function isOpaqueToken(segment) {
|
|
452
|
+
if (segment.length < OPAQUE_TOKEN_MIN_LENGTH) return false;
|
|
453
|
+
const distinct = /* @__PURE__ */ new Set();
|
|
454
|
+
for (let index = 0; index < segment.length; index++) {
|
|
455
|
+
distinct.add(segment.charCodeAt(index));
|
|
456
|
+
if (distinct.size >= OPAQUE_TOKEN_MIN_UNIQUE_CHARS) return true;
|
|
457
|
+
}
|
|
458
|
+
return false;
|
|
455
459
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
(entry) => sameFundingCredit(entry, args, credits) && !x402ExactFundingDead(entry, args.now)
|
|
460
|
-
);
|
|
460
|
+
var callerLoggers = buildNoopLoggers();
|
|
461
|
+
function initCallerLoggers(nextLoggers) {
|
|
462
|
+
callerLoggers = nextLoggers;
|
|
461
463
|
}
|
|
462
|
-
function
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
464
|
+
function buildNoopLoggers() {
|
|
465
|
+
const noopLogger = {
|
|
466
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function -- standalone caller has no exporter
|
|
467
|
+
info() {
|
|
468
|
+
},
|
|
469
|
+
async span(_name, _attrs, fn) {
|
|
470
|
+
return fn();
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
return {
|
|
474
|
+
cashu: noopLogger,
|
|
475
|
+
lightning: noopLogger,
|
|
476
|
+
x402: noopLogger
|
|
477
|
+
};
|
|
468
478
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
479
|
+
|
|
480
|
+
// node_modules/@noble/hashes/utils.js
|
|
481
|
+
function isBytes(a) {
|
|
482
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
483
|
+
}
|
|
484
|
+
function anumber(n, title = "") {
|
|
485
|
+
if (!Number.isSafeInteger(n) || n < 0) {
|
|
486
|
+
const prefix = title && `"${title}" `;
|
|
487
|
+
throw new Error(`${prefix}expected integer >= 0, got ${n}`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function abytes(value, length, title = "") {
|
|
491
|
+
const bytes = isBytes(value);
|
|
492
|
+
const len = value?.length;
|
|
493
|
+
const needsLen = length !== void 0;
|
|
494
|
+
if (!bytes || needsLen && len !== length) {
|
|
495
|
+
const prefix = title && `"${title}" `;
|
|
496
|
+
const ofLen = needsLen ? ` of length ${length}` : "";
|
|
497
|
+
const got = bytes ? `length=${len}` : `type=${typeof value}`;
|
|
498
|
+
throw new Error(prefix + "expected Uint8Array" + ofLen + ", got " + got);
|
|
499
|
+
}
|
|
500
|
+
return value;
|
|
476
501
|
}
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
502
|
+
function ahash(h) {
|
|
503
|
+
if (typeof h !== "function" || typeof h.create !== "function")
|
|
504
|
+
throw new Error("Hash must wrapped by utils.createHasher");
|
|
505
|
+
anumber(h.outputLen);
|
|
506
|
+
anumber(h.blockLen);
|
|
507
|
+
}
|
|
508
|
+
function aexists(instance, checkFinished = true) {
|
|
509
|
+
if (instance.destroyed)
|
|
510
|
+
throw new Error("Hash instance has been destroyed");
|
|
511
|
+
if (checkFinished && instance.finished)
|
|
512
|
+
throw new Error("Hash#digest() has already been called");
|
|
513
|
+
}
|
|
514
|
+
function aoutput(out, instance) {
|
|
515
|
+
abytes(out, void 0, "digestInto() output");
|
|
516
|
+
const min = instance.outputLen;
|
|
517
|
+
if (out.length < min) {
|
|
518
|
+
throw new Error('"digestInto() output" expected to be of length >=' + min);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function clean(...arrays) {
|
|
522
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
523
|
+
arrays[i].fill(0);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function createView(arr) {
|
|
527
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
528
|
+
}
|
|
529
|
+
function rotr(word, shift) {
|
|
530
|
+
return word << 32 - shift | word >>> shift;
|
|
531
|
+
}
|
|
532
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
533
|
+
// @ts-ignore
|
|
534
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
535
|
+
))();
|
|
536
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
|
537
|
+
function bytesToHex(bytes) {
|
|
538
|
+
abytes(bytes);
|
|
539
|
+
if (hasHexBuiltin)
|
|
540
|
+
return bytes.toHex();
|
|
541
|
+
let hex = "";
|
|
542
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
543
|
+
hex += hexes[bytes[i]];
|
|
544
|
+
}
|
|
545
|
+
return hex;
|
|
546
|
+
}
|
|
547
|
+
var asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
|
548
|
+
function asciiToBase16(ch) {
|
|
549
|
+
if (ch >= asciis._0 && ch <= asciis._9)
|
|
550
|
+
return ch - asciis._0;
|
|
551
|
+
if (ch >= asciis.A && ch <= asciis.F)
|
|
552
|
+
return ch - (asciis.A - 10);
|
|
553
|
+
if (ch >= asciis.a && ch <= asciis.f)
|
|
554
|
+
return ch - (asciis.a - 10);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
function hexToBytes(hex) {
|
|
558
|
+
if (typeof hex !== "string")
|
|
559
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
560
|
+
if (hasHexBuiltin)
|
|
561
|
+
return Uint8Array.fromHex(hex);
|
|
562
|
+
const hl = hex.length;
|
|
563
|
+
const al = hl / 2;
|
|
564
|
+
if (hl % 2)
|
|
565
|
+
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
|
566
|
+
const array = new Uint8Array(al);
|
|
567
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
568
|
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
569
|
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
570
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
571
|
+
const char = hex[hi] + hex[hi + 1];
|
|
572
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
573
|
+
}
|
|
574
|
+
array[ai] = n1 * 16 + n2;
|
|
575
|
+
}
|
|
576
|
+
return array;
|
|
577
|
+
}
|
|
578
|
+
function utf8ToBytes(str) {
|
|
579
|
+
if (typeof str !== "string")
|
|
580
|
+
throw new Error("string expected");
|
|
581
|
+
return new Uint8Array(new TextEncoder().encode(str));
|
|
582
|
+
}
|
|
583
|
+
function kdfInputToBytes(data, errorTitle = "") {
|
|
584
|
+
if (typeof data === "string")
|
|
585
|
+
return utf8ToBytes(data);
|
|
586
|
+
return abytes(data, void 0, errorTitle);
|
|
587
|
+
}
|
|
588
|
+
function concatBytes(...arrays) {
|
|
589
|
+
let sum = 0;
|
|
590
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
591
|
+
const a = arrays[i];
|
|
592
|
+
abytes(a);
|
|
593
|
+
sum += a.length;
|
|
594
|
+
}
|
|
595
|
+
const res = new Uint8Array(sum);
|
|
596
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
597
|
+
const a = arrays[i];
|
|
598
|
+
res.set(a, pad);
|
|
599
|
+
pad += a.length;
|
|
600
|
+
}
|
|
601
|
+
return res;
|
|
602
|
+
}
|
|
603
|
+
function checkOpts(defaults, opts) {
|
|
604
|
+
if (opts !== void 0 && {}.toString.call(opts) !== "[object Object]")
|
|
605
|
+
throw new Error("options must be object or undefined");
|
|
606
|
+
const merged = Object.assign(defaults, opts);
|
|
607
|
+
return merged;
|
|
480
608
|
}
|
|
481
|
-
function
|
|
482
|
-
|
|
609
|
+
function createHasher(hashCons, info = {}) {
|
|
610
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
611
|
+
const tmp = hashCons(void 0);
|
|
612
|
+
hashC.outputLen = tmp.outputLen;
|
|
613
|
+
hashC.blockLen = tmp.blockLen;
|
|
614
|
+
hashC.create = (opts) => hashCons(opts);
|
|
615
|
+
Object.assign(hashC, info);
|
|
616
|
+
return Object.freeze(hashC);
|
|
617
|
+
}
|
|
618
|
+
function randomBytes(bytesLength = 32) {
|
|
619
|
+
const cr = typeof globalThis === "object" ? globalThis.crypto : null;
|
|
620
|
+
if (typeof cr?.getRandomValues !== "function")
|
|
621
|
+
throw new Error("crypto.getRandomValues must be defined");
|
|
622
|
+
return cr.getRandomValues(new Uint8Array(bytesLength));
|
|
623
|
+
}
|
|
624
|
+
var oidNist = (suffix) => ({
|
|
625
|
+
oid: Uint8Array.from([6, 9, 96, 134, 72, 1, 101, 3, 4, 2, suffix])
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
// node_modules/@noble/hashes/_md.js
|
|
629
|
+
function Chi(a, b, c) {
|
|
630
|
+
return a & b ^ ~a & c;
|
|
631
|
+
}
|
|
632
|
+
function Maj(a, b, c) {
|
|
633
|
+
return a & b ^ a & c ^ b & c;
|
|
634
|
+
}
|
|
635
|
+
var HashMD = class {
|
|
636
|
+
blockLen;
|
|
637
|
+
outputLen;
|
|
638
|
+
padOffset;
|
|
639
|
+
isLE;
|
|
640
|
+
// For partial updates less than block size
|
|
641
|
+
buffer;
|
|
642
|
+
view;
|
|
643
|
+
finished = false;
|
|
644
|
+
length = 0;
|
|
645
|
+
pos = 0;
|
|
646
|
+
destroyed = false;
|
|
647
|
+
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
648
|
+
this.blockLen = blockLen;
|
|
649
|
+
this.outputLen = outputLen;
|
|
650
|
+
this.padOffset = padOffset;
|
|
651
|
+
this.isLE = isLE;
|
|
652
|
+
this.buffer = new Uint8Array(blockLen);
|
|
653
|
+
this.view = createView(this.buffer);
|
|
654
|
+
}
|
|
655
|
+
update(data) {
|
|
656
|
+
aexists(this);
|
|
657
|
+
abytes(data);
|
|
658
|
+
const { view, buffer, blockLen } = this;
|
|
659
|
+
const len = data.length;
|
|
660
|
+
for (let pos = 0; pos < len; ) {
|
|
661
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
662
|
+
if (take === blockLen) {
|
|
663
|
+
const dataView = createView(data);
|
|
664
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
665
|
+
this.process(dataView, pos);
|
|
666
|
+
continue;
|
|
667
|
+
}
|
|
668
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
669
|
+
this.pos += take;
|
|
670
|
+
pos += take;
|
|
671
|
+
if (this.pos === blockLen) {
|
|
672
|
+
this.process(view, 0);
|
|
673
|
+
this.pos = 0;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
this.length += data.length;
|
|
677
|
+
this.roundClean();
|
|
678
|
+
return this;
|
|
679
|
+
}
|
|
680
|
+
digestInto(out) {
|
|
681
|
+
aexists(this);
|
|
682
|
+
aoutput(out, this);
|
|
683
|
+
this.finished = true;
|
|
684
|
+
const { buffer, view, blockLen, isLE } = this;
|
|
685
|
+
let { pos } = this;
|
|
686
|
+
buffer[pos++] = 128;
|
|
687
|
+
clean(this.buffer.subarray(pos));
|
|
688
|
+
if (this.padOffset > blockLen - pos) {
|
|
689
|
+
this.process(view, 0);
|
|
690
|
+
pos = 0;
|
|
691
|
+
}
|
|
692
|
+
for (let i = pos; i < blockLen; i++)
|
|
693
|
+
buffer[i] = 0;
|
|
694
|
+
view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
|
|
695
|
+
this.process(view, 0);
|
|
696
|
+
const oview = createView(out);
|
|
697
|
+
const len = this.outputLen;
|
|
698
|
+
if (len % 4)
|
|
699
|
+
throw new Error("_sha2: outputLen must be aligned to 32bit");
|
|
700
|
+
const outLen = len / 4;
|
|
701
|
+
const state = this.get();
|
|
702
|
+
if (outLen > state.length)
|
|
703
|
+
throw new Error("_sha2: outputLen bigger than state");
|
|
704
|
+
for (let i = 0; i < outLen; i++)
|
|
705
|
+
oview.setUint32(4 * i, state[i], isLE);
|
|
706
|
+
}
|
|
707
|
+
digest() {
|
|
708
|
+
const { buffer, outputLen } = this;
|
|
709
|
+
this.digestInto(buffer);
|
|
710
|
+
const res = buffer.slice(0, outputLen);
|
|
711
|
+
this.destroy();
|
|
712
|
+
return res;
|
|
713
|
+
}
|
|
714
|
+
_cloneInto(to) {
|
|
715
|
+
to ||= new this.constructor();
|
|
716
|
+
to.set(...this.get());
|
|
717
|
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
|
718
|
+
to.destroyed = destroyed;
|
|
719
|
+
to.finished = finished;
|
|
720
|
+
to.length = length;
|
|
721
|
+
to.pos = pos;
|
|
722
|
+
if (length % blockLen)
|
|
723
|
+
to.buffer.set(buffer);
|
|
724
|
+
return to;
|
|
725
|
+
}
|
|
726
|
+
clone() {
|
|
727
|
+
return this._cloneInto();
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
var SHA256_IV = /* @__PURE__ */ Uint32Array.from([
|
|
731
|
+
1779033703,
|
|
732
|
+
3144134277,
|
|
733
|
+
1013904242,
|
|
734
|
+
2773480762,
|
|
735
|
+
1359893119,
|
|
736
|
+
2600822924,
|
|
737
|
+
528734635,
|
|
738
|
+
1541459225
|
|
739
|
+
]);
|
|
740
|
+
var SHA512_IV = /* @__PURE__ */ Uint32Array.from([
|
|
741
|
+
1779033703,
|
|
742
|
+
4089235720,
|
|
743
|
+
3144134277,
|
|
744
|
+
2227873595,
|
|
745
|
+
1013904242,
|
|
746
|
+
4271175723,
|
|
747
|
+
2773480762,
|
|
748
|
+
1595750129,
|
|
749
|
+
1359893119,
|
|
750
|
+
2917565137,
|
|
751
|
+
2600822924,
|
|
752
|
+
725511199,
|
|
753
|
+
528734635,
|
|
754
|
+
4215389547,
|
|
755
|
+
1541459225,
|
|
756
|
+
327033209
|
|
757
|
+
]);
|
|
758
|
+
|
|
759
|
+
// node_modules/@noble/hashes/_u64.js
|
|
760
|
+
var U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
|
761
|
+
var _32n = /* @__PURE__ */ BigInt(32);
|
|
762
|
+
function fromBig(n, le = false) {
|
|
763
|
+
if (le)
|
|
764
|
+
return { h: Number(n & U32_MASK64), l: Number(n >> _32n & U32_MASK64) };
|
|
765
|
+
return { h: Number(n >> _32n & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
|
766
|
+
}
|
|
767
|
+
function split(lst, le = false) {
|
|
768
|
+
const len = lst.length;
|
|
769
|
+
let Ah = new Uint32Array(len);
|
|
770
|
+
let Al = new Uint32Array(len);
|
|
771
|
+
for (let i = 0; i < len; i++) {
|
|
772
|
+
const { h, l } = fromBig(lst[i], le);
|
|
773
|
+
[Ah[i], Al[i]] = [h, l];
|
|
774
|
+
}
|
|
775
|
+
return [Ah, Al];
|
|
776
|
+
}
|
|
777
|
+
var shrSH = (h, _l, s) => h >>> s;
|
|
778
|
+
var shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
779
|
+
var rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
780
|
+
var rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
781
|
+
var rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
782
|
+
var rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
783
|
+
function add(Ah, Al, Bh, Bl) {
|
|
784
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
785
|
+
return { h: Ah + Bh + (l / 2 ** 32 | 0) | 0, l: l | 0 };
|
|
786
|
+
}
|
|
787
|
+
var add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
788
|
+
var add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
789
|
+
var add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
790
|
+
var add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
791
|
+
var add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
792
|
+
var add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
793
|
+
|
|
794
|
+
// node_modules/@noble/hashes/sha2.js
|
|
795
|
+
var SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
|
796
|
+
1116352408,
|
|
797
|
+
1899447441,
|
|
798
|
+
3049323471,
|
|
799
|
+
3921009573,
|
|
800
|
+
961987163,
|
|
801
|
+
1508970993,
|
|
802
|
+
2453635748,
|
|
803
|
+
2870763221,
|
|
804
|
+
3624381080,
|
|
805
|
+
310598401,
|
|
806
|
+
607225278,
|
|
807
|
+
1426881987,
|
|
808
|
+
1925078388,
|
|
809
|
+
2162078206,
|
|
810
|
+
2614888103,
|
|
811
|
+
3248222580,
|
|
812
|
+
3835390401,
|
|
813
|
+
4022224774,
|
|
814
|
+
264347078,
|
|
815
|
+
604807628,
|
|
816
|
+
770255983,
|
|
817
|
+
1249150122,
|
|
818
|
+
1555081692,
|
|
819
|
+
1996064986,
|
|
820
|
+
2554220882,
|
|
821
|
+
2821834349,
|
|
822
|
+
2952996808,
|
|
823
|
+
3210313671,
|
|
824
|
+
3336571891,
|
|
825
|
+
3584528711,
|
|
826
|
+
113926993,
|
|
827
|
+
338241895,
|
|
828
|
+
666307205,
|
|
829
|
+
773529912,
|
|
830
|
+
1294757372,
|
|
831
|
+
1396182291,
|
|
832
|
+
1695183700,
|
|
833
|
+
1986661051,
|
|
834
|
+
2177026350,
|
|
835
|
+
2456956037,
|
|
836
|
+
2730485921,
|
|
837
|
+
2820302411,
|
|
838
|
+
3259730800,
|
|
839
|
+
3345764771,
|
|
840
|
+
3516065817,
|
|
841
|
+
3600352804,
|
|
842
|
+
4094571909,
|
|
843
|
+
275423344,
|
|
844
|
+
430227734,
|
|
845
|
+
506948616,
|
|
846
|
+
659060556,
|
|
847
|
+
883997877,
|
|
848
|
+
958139571,
|
|
849
|
+
1322822218,
|
|
850
|
+
1537002063,
|
|
851
|
+
1747873779,
|
|
852
|
+
1955562222,
|
|
853
|
+
2024104815,
|
|
854
|
+
2227730452,
|
|
855
|
+
2361852424,
|
|
856
|
+
2428436474,
|
|
857
|
+
2756734187,
|
|
858
|
+
3204031479,
|
|
859
|
+
3329325298
|
|
860
|
+
]);
|
|
861
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
862
|
+
var SHA2_32B = class extends HashMD {
|
|
863
|
+
constructor(outputLen) {
|
|
864
|
+
super(64, outputLen, 8, false);
|
|
865
|
+
}
|
|
866
|
+
get() {
|
|
867
|
+
const { A, B, C, D, E, F, G, H } = this;
|
|
868
|
+
return [A, B, C, D, E, F, G, H];
|
|
869
|
+
}
|
|
870
|
+
// prettier-ignore
|
|
871
|
+
set(A, B, C, D, E, F, G, H) {
|
|
872
|
+
this.A = A | 0;
|
|
873
|
+
this.B = B | 0;
|
|
874
|
+
this.C = C | 0;
|
|
875
|
+
this.D = D | 0;
|
|
876
|
+
this.E = E | 0;
|
|
877
|
+
this.F = F | 0;
|
|
878
|
+
this.G = G | 0;
|
|
879
|
+
this.H = H | 0;
|
|
880
|
+
}
|
|
881
|
+
process(view, offset) {
|
|
882
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
883
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
884
|
+
for (let i = 16; i < 64; i++) {
|
|
885
|
+
const W15 = SHA256_W[i - 15];
|
|
886
|
+
const W2 = SHA256_W[i - 2];
|
|
887
|
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
|
|
888
|
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
|
|
889
|
+
SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
|
|
890
|
+
}
|
|
891
|
+
let { A, B, C, D, E, F, G, H } = this;
|
|
892
|
+
for (let i = 0; i < 64; i++) {
|
|
893
|
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
|
894
|
+
const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
|
|
895
|
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
|
896
|
+
const T2 = sigma0 + Maj(A, B, C) | 0;
|
|
897
|
+
H = G;
|
|
898
|
+
G = F;
|
|
899
|
+
F = E;
|
|
900
|
+
E = D + T1 | 0;
|
|
901
|
+
D = C;
|
|
902
|
+
C = B;
|
|
903
|
+
B = A;
|
|
904
|
+
A = T1 + T2 | 0;
|
|
905
|
+
}
|
|
906
|
+
A = A + this.A | 0;
|
|
907
|
+
B = B + this.B | 0;
|
|
908
|
+
C = C + this.C | 0;
|
|
909
|
+
D = D + this.D | 0;
|
|
910
|
+
E = E + this.E | 0;
|
|
911
|
+
F = F + this.F | 0;
|
|
912
|
+
G = G + this.G | 0;
|
|
913
|
+
H = H + this.H | 0;
|
|
914
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
915
|
+
}
|
|
916
|
+
roundClean() {
|
|
917
|
+
clean(SHA256_W);
|
|
918
|
+
}
|
|
919
|
+
destroy() {
|
|
920
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
921
|
+
clean(this.buffer);
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
var _SHA256 = class extends SHA2_32B {
|
|
925
|
+
// We cannot use array here since array allows indexing by variable
|
|
926
|
+
// which means optimizer/compiler cannot use registers.
|
|
927
|
+
A = SHA256_IV[0] | 0;
|
|
928
|
+
B = SHA256_IV[1] | 0;
|
|
929
|
+
C = SHA256_IV[2] | 0;
|
|
930
|
+
D = SHA256_IV[3] | 0;
|
|
931
|
+
E = SHA256_IV[4] | 0;
|
|
932
|
+
F = SHA256_IV[5] | 0;
|
|
933
|
+
G = SHA256_IV[6] | 0;
|
|
934
|
+
H = SHA256_IV[7] | 0;
|
|
935
|
+
constructor() {
|
|
936
|
+
super(32);
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
var K512 = /* @__PURE__ */ (() => split([
|
|
940
|
+
"0x428a2f98d728ae22",
|
|
941
|
+
"0x7137449123ef65cd",
|
|
942
|
+
"0xb5c0fbcfec4d3b2f",
|
|
943
|
+
"0xe9b5dba58189dbbc",
|
|
944
|
+
"0x3956c25bf348b538",
|
|
945
|
+
"0x59f111f1b605d019",
|
|
946
|
+
"0x923f82a4af194f9b",
|
|
947
|
+
"0xab1c5ed5da6d8118",
|
|
948
|
+
"0xd807aa98a3030242",
|
|
949
|
+
"0x12835b0145706fbe",
|
|
950
|
+
"0x243185be4ee4b28c",
|
|
951
|
+
"0x550c7dc3d5ffb4e2",
|
|
952
|
+
"0x72be5d74f27b896f",
|
|
953
|
+
"0x80deb1fe3b1696b1",
|
|
954
|
+
"0x9bdc06a725c71235",
|
|
955
|
+
"0xc19bf174cf692694",
|
|
956
|
+
"0xe49b69c19ef14ad2",
|
|
957
|
+
"0xefbe4786384f25e3",
|
|
958
|
+
"0x0fc19dc68b8cd5b5",
|
|
959
|
+
"0x240ca1cc77ac9c65",
|
|
960
|
+
"0x2de92c6f592b0275",
|
|
961
|
+
"0x4a7484aa6ea6e483",
|
|
962
|
+
"0x5cb0a9dcbd41fbd4",
|
|
963
|
+
"0x76f988da831153b5",
|
|
964
|
+
"0x983e5152ee66dfab",
|
|
965
|
+
"0xa831c66d2db43210",
|
|
966
|
+
"0xb00327c898fb213f",
|
|
967
|
+
"0xbf597fc7beef0ee4",
|
|
968
|
+
"0xc6e00bf33da88fc2",
|
|
969
|
+
"0xd5a79147930aa725",
|
|
970
|
+
"0x06ca6351e003826f",
|
|
971
|
+
"0x142929670a0e6e70",
|
|
972
|
+
"0x27b70a8546d22ffc",
|
|
973
|
+
"0x2e1b21385c26c926",
|
|
974
|
+
"0x4d2c6dfc5ac42aed",
|
|
975
|
+
"0x53380d139d95b3df",
|
|
976
|
+
"0x650a73548baf63de",
|
|
977
|
+
"0x766a0abb3c77b2a8",
|
|
978
|
+
"0x81c2c92e47edaee6",
|
|
979
|
+
"0x92722c851482353b",
|
|
980
|
+
"0xa2bfe8a14cf10364",
|
|
981
|
+
"0xa81a664bbc423001",
|
|
982
|
+
"0xc24b8b70d0f89791",
|
|
983
|
+
"0xc76c51a30654be30",
|
|
984
|
+
"0xd192e819d6ef5218",
|
|
985
|
+
"0xd69906245565a910",
|
|
986
|
+
"0xf40e35855771202a",
|
|
987
|
+
"0x106aa07032bbd1b8",
|
|
988
|
+
"0x19a4c116b8d2d0c8",
|
|
989
|
+
"0x1e376c085141ab53",
|
|
990
|
+
"0x2748774cdf8eeb99",
|
|
991
|
+
"0x34b0bcb5e19b48a8",
|
|
992
|
+
"0x391c0cb3c5c95a63",
|
|
993
|
+
"0x4ed8aa4ae3418acb",
|
|
994
|
+
"0x5b9cca4f7763e373",
|
|
995
|
+
"0x682e6ff3d6b2b8a3",
|
|
996
|
+
"0x748f82ee5defb2fc",
|
|
997
|
+
"0x78a5636f43172f60",
|
|
998
|
+
"0x84c87814a1f0ab72",
|
|
999
|
+
"0x8cc702081a6439ec",
|
|
1000
|
+
"0x90befffa23631e28",
|
|
1001
|
+
"0xa4506cebde82bde9",
|
|
1002
|
+
"0xbef9a3f7b2c67915",
|
|
1003
|
+
"0xc67178f2e372532b",
|
|
1004
|
+
"0xca273eceea26619c",
|
|
1005
|
+
"0xd186b8c721c0c207",
|
|
1006
|
+
"0xeada7dd6cde0eb1e",
|
|
1007
|
+
"0xf57d4f7fee6ed178",
|
|
1008
|
+
"0x06f067aa72176fba",
|
|
1009
|
+
"0x0a637dc5a2c898a6",
|
|
1010
|
+
"0x113f9804bef90dae",
|
|
1011
|
+
"0x1b710b35131c471b",
|
|
1012
|
+
"0x28db77f523047d84",
|
|
1013
|
+
"0x32caab7b40c72493",
|
|
1014
|
+
"0x3c9ebe0a15c9bebc",
|
|
1015
|
+
"0x431d67c49c100d4c",
|
|
1016
|
+
"0x4cc5d4becb3e42b6",
|
|
1017
|
+
"0x597f299cfc657e2a",
|
|
1018
|
+
"0x5fcb6fab3ad6faec",
|
|
1019
|
+
"0x6c44198c4a475817"
|
|
1020
|
+
].map((n) => BigInt(n))))();
|
|
1021
|
+
var SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
|
1022
|
+
var SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
|
1023
|
+
var SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
|
1024
|
+
var SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
|
1025
|
+
var SHA2_64B = class extends HashMD {
|
|
1026
|
+
constructor(outputLen) {
|
|
1027
|
+
super(128, outputLen, 16, false);
|
|
1028
|
+
}
|
|
1029
|
+
// prettier-ignore
|
|
1030
|
+
get() {
|
|
1031
|
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1032
|
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
|
1033
|
+
}
|
|
1034
|
+
// prettier-ignore
|
|
1035
|
+
set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
|
|
1036
|
+
this.Ah = Ah | 0;
|
|
1037
|
+
this.Al = Al | 0;
|
|
1038
|
+
this.Bh = Bh | 0;
|
|
1039
|
+
this.Bl = Bl | 0;
|
|
1040
|
+
this.Ch = Ch | 0;
|
|
1041
|
+
this.Cl = Cl | 0;
|
|
1042
|
+
this.Dh = Dh | 0;
|
|
1043
|
+
this.Dl = Dl | 0;
|
|
1044
|
+
this.Eh = Eh | 0;
|
|
1045
|
+
this.El = El | 0;
|
|
1046
|
+
this.Fh = Fh | 0;
|
|
1047
|
+
this.Fl = Fl | 0;
|
|
1048
|
+
this.Gh = Gh | 0;
|
|
1049
|
+
this.Gl = Gl | 0;
|
|
1050
|
+
this.Hh = Hh | 0;
|
|
1051
|
+
this.Hl = Hl | 0;
|
|
1052
|
+
}
|
|
1053
|
+
process(view, offset) {
|
|
1054
|
+
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1055
|
+
SHA512_W_H[i] = view.getUint32(offset);
|
|
1056
|
+
SHA512_W_L[i] = view.getUint32(offset += 4);
|
|
1057
|
+
}
|
|
1058
|
+
for (let i = 16; i < 80; i++) {
|
|
1059
|
+
const W15h = SHA512_W_H[i - 15] | 0;
|
|
1060
|
+
const W15l = SHA512_W_L[i - 15] | 0;
|
|
1061
|
+
const s0h = rotrSH(W15h, W15l, 1) ^ rotrSH(W15h, W15l, 8) ^ shrSH(W15h, W15l, 7);
|
|
1062
|
+
const s0l = rotrSL(W15h, W15l, 1) ^ rotrSL(W15h, W15l, 8) ^ shrSL(W15h, W15l, 7);
|
|
1063
|
+
const W2h = SHA512_W_H[i - 2] | 0;
|
|
1064
|
+
const W2l = SHA512_W_L[i - 2] | 0;
|
|
1065
|
+
const s1h = rotrSH(W2h, W2l, 19) ^ rotrBH(W2h, W2l, 61) ^ shrSH(W2h, W2l, 6);
|
|
1066
|
+
const s1l = rotrSL(W2h, W2l, 19) ^ rotrBL(W2h, W2l, 61) ^ shrSL(W2h, W2l, 6);
|
|
1067
|
+
const SUMl = add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
|
1068
|
+
const SUMh = add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
|
1069
|
+
SHA512_W_H[i] = SUMh | 0;
|
|
1070
|
+
SHA512_W_L[i] = SUMl | 0;
|
|
1071
|
+
}
|
|
1072
|
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
|
1073
|
+
for (let i = 0; i < 80; i++) {
|
|
1074
|
+
const sigma1h = rotrSH(Eh, El, 14) ^ rotrSH(Eh, El, 18) ^ rotrBH(Eh, El, 41);
|
|
1075
|
+
const sigma1l = rotrSL(Eh, El, 14) ^ rotrSL(Eh, El, 18) ^ rotrBL(Eh, El, 41);
|
|
1076
|
+
const CHIh = Eh & Fh ^ ~Eh & Gh;
|
|
1077
|
+
const CHIl = El & Fl ^ ~El & Gl;
|
|
1078
|
+
const T1ll = add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
|
1079
|
+
const T1h = add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
|
1080
|
+
const T1l = T1ll | 0;
|
|
1081
|
+
const sigma0h = rotrSH(Ah, Al, 28) ^ rotrBH(Ah, Al, 34) ^ rotrBH(Ah, Al, 39);
|
|
1082
|
+
const sigma0l = rotrSL(Ah, Al, 28) ^ rotrBL(Ah, Al, 34) ^ rotrBL(Ah, Al, 39);
|
|
1083
|
+
const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
|
|
1084
|
+
const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
|
|
1085
|
+
Hh = Gh | 0;
|
|
1086
|
+
Hl = Gl | 0;
|
|
1087
|
+
Gh = Fh | 0;
|
|
1088
|
+
Gl = Fl | 0;
|
|
1089
|
+
Fh = Eh | 0;
|
|
1090
|
+
Fl = El | 0;
|
|
1091
|
+
({ h: Eh, l: El } = add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
|
1092
|
+
Dh = Ch | 0;
|
|
1093
|
+
Dl = Cl | 0;
|
|
1094
|
+
Ch = Bh | 0;
|
|
1095
|
+
Cl = Bl | 0;
|
|
1096
|
+
Bh = Ah | 0;
|
|
1097
|
+
Bl = Al | 0;
|
|
1098
|
+
const All = add3L(T1l, sigma0l, MAJl);
|
|
1099
|
+
Ah = add3H(All, T1h, sigma0h, MAJh);
|
|
1100
|
+
Al = All | 0;
|
|
1101
|
+
}
|
|
1102
|
+
({ h: Ah, l: Al } = add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
|
1103
|
+
({ h: Bh, l: Bl } = add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
|
1104
|
+
({ h: Ch, l: Cl } = add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
|
1105
|
+
({ h: Dh, l: Dl } = add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
|
1106
|
+
({ h: Eh, l: El } = add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
|
1107
|
+
({ h: Fh, l: Fl } = add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
|
1108
|
+
({ h: Gh, l: Gl } = add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
|
1109
|
+
({ h: Hh, l: Hl } = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
1110
|
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1111
|
+
}
|
|
1112
|
+
roundClean() {
|
|
1113
|
+
clean(SHA512_W_H, SHA512_W_L);
|
|
1114
|
+
}
|
|
1115
|
+
destroy() {
|
|
1116
|
+
clean(this.buffer);
|
|
1117
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
var _SHA512 = class extends SHA2_64B {
|
|
1121
|
+
Ah = SHA512_IV[0] | 0;
|
|
1122
|
+
Al = SHA512_IV[1] | 0;
|
|
1123
|
+
Bh = SHA512_IV[2] | 0;
|
|
1124
|
+
Bl = SHA512_IV[3] | 0;
|
|
1125
|
+
Ch = SHA512_IV[4] | 0;
|
|
1126
|
+
Cl = SHA512_IV[5] | 0;
|
|
1127
|
+
Dh = SHA512_IV[6] | 0;
|
|
1128
|
+
Dl = SHA512_IV[7] | 0;
|
|
1129
|
+
Eh = SHA512_IV[8] | 0;
|
|
1130
|
+
El = SHA512_IV[9] | 0;
|
|
1131
|
+
Fh = SHA512_IV[10] | 0;
|
|
1132
|
+
Fl = SHA512_IV[11] | 0;
|
|
1133
|
+
Gh = SHA512_IV[12] | 0;
|
|
1134
|
+
Gl = SHA512_IV[13] | 0;
|
|
1135
|
+
Hh = SHA512_IV[14] | 0;
|
|
1136
|
+
Hl = SHA512_IV[15] | 0;
|
|
1137
|
+
constructor() {
|
|
1138
|
+
super(64);
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
var sha256 = /* @__PURE__ */ createHasher(
|
|
1142
|
+
() => new _SHA256(),
|
|
1143
|
+
/* @__PURE__ */ oidNist(1)
|
|
1144
|
+
);
|
|
1145
|
+
var sha512 = /* @__PURE__ */ createHasher(
|
|
1146
|
+
() => new _SHA512(),
|
|
1147
|
+
/* @__PURE__ */ oidNist(3)
|
|
1148
|
+
);
|
|
1149
|
+
|
|
1150
|
+
// node_modules/@noble/curves/utils.js
|
|
1151
|
+
var _0n = /* @__PURE__ */ BigInt(0);
|
|
1152
|
+
var _1n = /* @__PURE__ */ BigInt(1);
|
|
1153
|
+
function abool(value, title = "") {
|
|
1154
|
+
if (typeof value !== "boolean") {
|
|
1155
|
+
const prefix = title && `"${title}" `;
|
|
1156
|
+
throw new Error(prefix + "expected boolean, got type=" + typeof value);
|
|
1157
|
+
}
|
|
1158
|
+
return value;
|
|
483
1159
|
}
|
|
484
|
-
function
|
|
485
|
-
|
|
1160
|
+
function abignumber(n) {
|
|
1161
|
+
if (typeof n === "bigint") {
|
|
1162
|
+
if (!isPosBig(n))
|
|
1163
|
+
throw new Error("positive bigint expected, got " + n);
|
|
1164
|
+
} else
|
|
1165
|
+
anumber(n);
|
|
1166
|
+
return n;
|
|
1167
|
+
}
|
|
1168
|
+
function numberToHexUnpadded(num2) {
|
|
1169
|
+
const hex = abignumber(num2).toString(16);
|
|
1170
|
+
return hex.length & 1 ? "0" + hex : hex;
|
|
1171
|
+
}
|
|
1172
|
+
function hexToNumber(hex) {
|
|
1173
|
+
if (typeof hex !== "string")
|
|
1174
|
+
throw new Error("hex string expected, got " + typeof hex);
|
|
1175
|
+
return hex === "" ? _0n : BigInt("0x" + hex);
|
|
1176
|
+
}
|
|
1177
|
+
function bytesToNumberBE(bytes) {
|
|
1178
|
+
return hexToNumber(bytesToHex(bytes));
|
|
1179
|
+
}
|
|
1180
|
+
function bytesToNumberLE(bytes) {
|
|
1181
|
+
return hexToNumber(bytesToHex(copyBytes(abytes(bytes)).reverse()));
|
|
1182
|
+
}
|
|
1183
|
+
function numberToBytesBE(n, len) {
|
|
1184
|
+
anumber(len);
|
|
1185
|
+
n = abignumber(n);
|
|
1186
|
+
const res = hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
|
1187
|
+
if (res.length !== len)
|
|
1188
|
+
throw new Error("number too large");
|
|
1189
|
+
return res;
|
|
1190
|
+
}
|
|
1191
|
+
function numberToBytesLE(n, len) {
|
|
1192
|
+
return numberToBytesBE(n, len).reverse();
|
|
1193
|
+
}
|
|
1194
|
+
function copyBytes(bytes) {
|
|
1195
|
+
return Uint8Array.from(bytes);
|
|
1196
|
+
}
|
|
1197
|
+
function asciiToBytes(ascii) {
|
|
1198
|
+
return Uint8Array.from(ascii, (c, i) => {
|
|
1199
|
+
const charCode = c.charCodeAt(0);
|
|
1200
|
+
if (c.length !== 1 || charCode > 127) {
|
|
1201
|
+
throw new Error(`string contains non-ASCII character "${ascii[i]}" with code ${charCode} at position ${i}`);
|
|
1202
|
+
}
|
|
1203
|
+
return charCode;
|
|
1204
|
+
});
|
|
486
1205
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
return
|
|
1206
|
+
var isPosBig = (n) => typeof n === "bigint" && _0n <= n;
|
|
1207
|
+
function inRange(n, min, max) {
|
|
1208
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
1209
|
+
}
|
|
1210
|
+
function aInRange(title, n, min, max) {
|
|
1211
|
+
if (!inRange(n, min, max))
|
|
1212
|
+
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
|
1213
|
+
}
|
|
1214
|
+
function bitLen(n) {
|
|
1215
|
+
let len;
|
|
1216
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
1217
|
+
;
|
|
1218
|
+
return len;
|
|
1219
|
+
}
|
|
1220
|
+
var bitMask = (n) => (_1n << BigInt(n)) - _1n;
|
|
1221
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
1222
|
+
anumber(hashLen, "hashLen");
|
|
1223
|
+
anumber(qByteLen, "qByteLen");
|
|
1224
|
+
if (typeof hmacFn !== "function")
|
|
1225
|
+
throw new Error("hmacFn must be a function");
|
|
1226
|
+
const u8n = (len) => new Uint8Array(len);
|
|
1227
|
+
const NULL = Uint8Array.of();
|
|
1228
|
+
const byte0 = Uint8Array.of(0);
|
|
1229
|
+
const byte1 = Uint8Array.of(1);
|
|
1230
|
+
const _maxDrbgIters = 1e3;
|
|
1231
|
+
let v = u8n(hashLen);
|
|
1232
|
+
let k = u8n(hashLen);
|
|
1233
|
+
let i = 0;
|
|
1234
|
+
const reset = () => {
|
|
1235
|
+
v.fill(1);
|
|
1236
|
+
k.fill(0);
|
|
1237
|
+
i = 0;
|
|
1238
|
+
};
|
|
1239
|
+
const h = (...msgs) => hmacFn(k, concatBytes(v, ...msgs));
|
|
1240
|
+
const reseed = (seed = NULL) => {
|
|
1241
|
+
k = h(byte0, seed);
|
|
1242
|
+
v = h();
|
|
1243
|
+
if (seed.length === 0)
|
|
1244
|
+
return;
|
|
1245
|
+
k = h(byte1, seed);
|
|
1246
|
+
v = h();
|
|
1247
|
+
};
|
|
1248
|
+
const gen = () => {
|
|
1249
|
+
if (i++ >= _maxDrbgIters)
|
|
1250
|
+
throw new Error("drbg: tried max amount of iterations");
|
|
1251
|
+
let len = 0;
|
|
1252
|
+
const out = [];
|
|
1253
|
+
while (len < qByteLen) {
|
|
1254
|
+
v = h();
|
|
1255
|
+
const sl = v.slice();
|
|
1256
|
+
out.push(sl);
|
|
1257
|
+
len += v.length;
|
|
1258
|
+
}
|
|
1259
|
+
return concatBytes(...out);
|
|
1260
|
+
};
|
|
1261
|
+
const genUntil = (seed, pred) => {
|
|
1262
|
+
reset();
|
|
1263
|
+
reseed(seed);
|
|
1264
|
+
let res = void 0;
|
|
1265
|
+
while (!(res = pred(gen())))
|
|
1266
|
+
reseed();
|
|
1267
|
+
reset();
|
|
1268
|
+
return res;
|
|
1269
|
+
};
|
|
1270
|
+
return genUntil;
|
|
1271
|
+
}
|
|
1272
|
+
function validateObject(object, fields = {}, optFields = {}) {
|
|
1273
|
+
if (!object || typeof object !== "object")
|
|
1274
|
+
throw new Error("expected valid options object");
|
|
1275
|
+
function checkField(fieldName, expectedType, isOpt) {
|
|
1276
|
+
const val = object[fieldName];
|
|
1277
|
+
if (isOpt && val === void 0)
|
|
1278
|
+
return;
|
|
1279
|
+
const current = typeof val;
|
|
1280
|
+
if (current !== expectedType || val === null)
|
|
1281
|
+
throw new Error(`param "${fieldName}" is invalid: expected ${expectedType}, got ${current}`);
|
|
1282
|
+
}
|
|
1283
|
+
const iter = (f, isOpt) => Object.entries(f).forEach(([k, v]) => checkField(k, v, isOpt));
|
|
1284
|
+
iter(fields, false);
|
|
1285
|
+
iter(optFields, true);
|
|
1286
|
+
}
|
|
1287
|
+
function memoized(fn) {
|
|
1288
|
+
const map = /* @__PURE__ */ new WeakMap();
|
|
1289
|
+
return (arg, ...args) => {
|
|
1290
|
+
const val = map.get(arg);
|
|
1291
|
+
if (val !== void 0)
|
|
1292
|
+
return val;
|
|
1293
|
+
const computed = fn(arg, ...args);
|
|
1294
|
+
map.set(arg, computed);
|
|
1295
|
+
return computed;
|
|
1296
|
+
};
|
|
490
1297
|
}
|
|
491
|
-
|
|
492
|
-
|
|
1298
|
+
|
|
1299
|
+
// node_modules/@noble/curves/abstract/modular.js
|
|
1300
|
+
var _0n2 = /* @__PURE__ */ BigInt(0);
|
|
1301
|
+
var _1n2 = /* @__PURE__ */ BigInt(1);
|
|
1302
|
+
var _2n = /* @__PURE__ */ BigInt(2);
|
|
1303
|
+
var _3n = /* @__PURE__ */ BigInt(3);
|
|
1304
|
+
var _4n = /* @__PURE__ */ BigInt(4);
|
|
1305
|
+
var _5n = /* @__PURE__ */ BigInt(5);
|
|
1306
|
+
var _7n = /* @__PURE__ */ BigInt(7);
|
|
1307
|
+
var _8n = /* @__PURE__ */ BigInt(8);
|
|
1308
|
+
var _9n = /* @__PURE__ */ BigInt(9);
|
|
1309
|
+
var _16n = /* @__PURE__ */ BigInt(16);
|
|
1310
|
+
function mod(a, b) {
|
|
1311
|
+
const result = a % b;
|
|
1312
|
+
return result >= _0n2 ? result : b + result;
|
|
1313
|
+
}
|
|
1314
|
+
function pow2(x, power, modulo) {
|
|
1315
|
+
let res = x;
|
|
1316
|
+
while (power-- > _0n2) {
|
|
1317
|
+
res *= res;
|
|
1318
|
+
res %= modulo;
|
|
1319
|
+
}
|
|
1320
|
+
return res;
|
|
1321
|
+
}
|
|
1322
|
+
function invert(number, modulo) {
|
|
1323
|
+
if (number === _0n2)
|
|
1324
|
+
throw new Error("invert: expected non-zero number");
|
|
1325
|
+
if (modulo <= _0n2)
|
|
1326
|
+
throw new Error("invert: expected positive modulus, got " + modulo);
|
|
1327
|
+
let a = mod(number, modulo);
|
|
1328
|
+
let b = modulo;
|
|
1329
|
+
let x = _0n2, y = _1n2, u = _1n2, v = _0n2;
|
|
1330
|
+
while (a !== _0n2) {
|
|
1331
|
+
const q = b / a;
|
|
1332
|
+
const r = b % a;
|
|
1333
|
+
const m = x - u * q;
|
|
1334
|
+
const n = y - v * q;
|
|
1335
|
+
b = a, a = r, x = u, y = v, u = m, v = n;
|
|
1336
|
+
}
|
|
1337
|
+
const gcd = b;
|
|
1338
|
+
if (gcd !== _1n2)
|
|
1339
|
+
throw new Error("invert: does not exist");
|
|
1340
|
+
return mod(x, modulo);
|
|
1341
|
+
}
|
|
1342
|
+
function assertIsSquare(Fp, root, n) {
|
|
1343
|
+
if (!Fp.eql(Fp.sqr(root), n))
|
|
1344
|
+
throw new Error("Cannot find square root");
|
|
1345
|
+
}
|
|
1346
|
+
function sqrt3mod4(Fp, n) {
|
|
1347
|
+
const p1div4 = (Fp.ORDER + _1n2) / _4n;
|
|
1348
|
+
const root = Fp.pow(n, p1div4);
|
|
1349
|
+
assertIsSquare(Fp, root, n);
|
|
1350
|
+
return root;
|
|
1351
|
+
}
|
|
1352
|
+
function sqrt5mod8(Fp, n) {
|
|
1353
|
+
const p5div8 = (Fp.ORDER - _5n) / _8n;
|
|
1354
|
+
const n2 = Fp.mul(n, _2n);
|
|
1355
|
+
const v = Fp.pow(n2, p5div8);
|
|
1356
|
+
const nv = Fp.mul(n, v);
|
|
1357
|
+
const i = Fp.mul(Fp.mul(nv, _2n), v);
|
|
1358
|
+
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
|
1359
|
+
assertIsSquare(Fp, root, n);
|
|
1360
|
+
return root;
|
|
1361
|
+
}
|
|
1362
|
+
function sqrt9mod16(P) {
|
|
1363
|
+
const Fp_ = Field(P);
|
|
1364
|
+
const tn = tonelliShanks(P);
|
|
1365
|
+
const c1 = tn(Fp_, Fp_.neg(Fp_.ONE));
|
|
1366
|
+
const c2 = tn(Fp_, c1);
|
|
1367
|
+
const c3 = tn(Fp_, Fp_.neg(c1));
|
|
1368
|
+
const c4 = (P + _7n) / _16n;
|
|
1369
|
+
return (Fp, n) => {
|
|
1370
|
+
let tv1 = Fp.pow(n, c4);
|
|
1371
|
+
let tv2 = Fp.mul(tv1, c1);
|
|
1372
|
+
const tv3 = Fp.mul(tv1, c2);
|
|
1373
|
+
const tv4 = Fp.mul(tv1, c3);
|
|
1374
|
+
const e1 = Fp.eql(Fp.sqr(tv2), n);
|
|
1375
|
+
const e2 = Fp.eql(Fp.sqr(tv3), n);
|
|
1376
|
+
tv1 = Fp.cmov(tv1, tv2, e1);
|
|
1377
|
+
tv2 = Fp.cmov(tv4, tv3, e2);
|
|
1378
|
+
const e3 = Fp.eql(Fp.sqr(tv2), n);
|
|
1379
|
+
const root = Fp.cmov(tv1, tv2, e3);
|
|
1380
|
+
assertIsSquare(Fp, root, n);
|
|
1381
|
+
return root;
|
|
1382
|
+
};
|
|
493
1383
|
}
|
|
494
|
-
function
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
1384
|
+
function tonelliShanks(P) {
|
|
1385
|
+
if (P < _3n)
|
|
1386
|
+
throw new Error("sqrt is not defined for small field");
|
|
1387
|
+
let Q = P - _1n2;
|
|
1388
|
+
let S = 0;
|
|
1389
|
+
while (Q % _2n === _0n2) {
|
|
1390
|
+
Q /= _2n;
|
|
1391
|
+
S++;
|
|
1392
|
+
}
|
|
1393
|
+
let Z = _2n;
|
|
1394
|
+
const _Fp = Field(P);
|
|
1395
|
+
while (FpLegendre(_Fp, Z) === 1) {
|
|
1396
|
+
if (Z++ > 1e3)
|
|
1397
|
+
throw new Error("Cannot find square root: probably non-prime P");
|
|
1398
|
+
}
|
|
1399
|
+
if (S === 1)
|
|
1400
|
+
return sqrt3mod4;
|
|
1401
|
+
let cc = _Fp.pow(Z, Q);
|
|
1402
|
+
const Q1div2 = (Q + _1n2) / _2n;
|
|
1403
|
+
return function tonelliSlow(Fp, n) {
|
|
1404
|
+
if (Fp.is0(n))
|
|
1405
|
+
return n;
|
|
1406
|
+
if (FpLegendre(Fp, n) !== 1)
|
|
1407
|
+
throw new Error("Cannot find square root");
|
|
1408
|
+
let M = S;
|
|
1409
|
+
let c = Fp.mul(Fp.ONE, cc);
|
|
1410
|
+
let t = Fp.pow(n, Q);
|
|
1411
|
+
let R = Fp.pow(n, Q1div2);
|
|
1412
|
+
while (!Fp.eql(t, Fp.ONE)) {
|
|
1413
|
+
if (Fp.is0(t))
|
|
1414
|
+
return Fp.ZERO;
|
|
1415
|
+
let i = 1;
|
|
1416
|
+
let t_tmp = Fp.sqr(t);
|
|
1417
|
+
while (!Fp.eql(t_tmp, Fp.ONE)) {
|
|
1418
|
+
i++;
|
|
1419
|
+
t_tmp = Fp.sqr(t_tmp);
|
|
1420
|
+
if (i === M)
|
|
1421
|
+
throw new Error("Cannot find square root");
|
|
1422
|
+
}
|
|
1423
|
+
const exponent = _1n2 << BigInt(M - i - 1);
|
|
1424
|
+
const b = Fp.pow(c, exponent);
|
|
1425
|
+
M = i;
|
|
1426
|
+
c = Fp.sqr(b);
|
|
1427
|
+
t = Fp.mul(t, c);
|
|
1428
|
+
R = Fp.mul(R, b);
|
|
1429
|
+
}
|
|
1430
|
+
return R;
|
|
523
1431
|
};
|
|
524
1432
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
if (
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
1433
|
+
function FpSqrt(P) {
|
|
1434
|
+
if (P % _4n === _3n)
|
|
1435
|
+
return sqrt3mod4;
|
|
1436
|
+
if (P % _8n === _5n)
|
|
1437
|
+
return sqrt5mod8;
|
|
1438
|
+
if (P % _16n === _9n)
|
|
1439
|
+
return sqrt9mod16(P);
|
|
1440
|
+
return tonelliShanks(P);
|
|
1441
|
+
}
|
|
1442
|
+
var FIELD_FIELDS = [
|
|
1443
|
+
"create",
|
|
1444
|
+
"isValid",
|
|
1445
|
+
"is0",
|
|
1446
|
+
"neg",
|
|
1447
|
+
"inv",
|
|
1448
|
+
"sqrt",
|
|
1449
|
+
"sqr",
|
|
1450
|
+
"eql",
|
|
1451
|
+
"add",
|
|
1452
|
+
"sub",
|
|
1453
|
+
"mul",
|
|
1454
|
+
"pow",
|
|
1455
|
+
"div",
|
|
1456
|
+
"addN",
|
|
1457
|
+
"subN",
|
|
1458
|
+
"mulN",
|
|
1459
|
+
"sqrN"
|
|
1460
|
+
];
|
|
1461
|
+
function validateField(field) {
|
|
1462
|
+
const initial = {
|
|
1463
|
+
ORDER: "bigint",
|
|
1464
|
+
BYTES: "number",
|
|
1465
|
+
BITS: "number"
|
|
1466
|
+
};
|
|
1467
|
+
const opts = FIELD_FIELDS.reduce((map, val) => {
|
|
1468
|
+
map[val] = "function";
|
|
1469
|
+
return map;
|
|
1470
|
+
}, initial);
|
|
1471
|
+
validateObject(field, opts);
|
|
1472
|
+
return field;
|
|
1473
|
+
}
|
|
1474
|
+
function FpPow(Fp, num2, power) {
|
|
1475
|
+
if (power < _0n2)
|
|
1476
|
+
throw new Error("invalid exponent, negatives unsupported");
|
|
1477
|
+
if (power === _0n2)
|
|
1478
|
+
return Fp.ONE;
|
|
1479
|
+
if (power === _1n2)
|
|
1480
|
+
return num2;
|
|
1481
|
+
let p = Fp.ONE;
|
|
1482
|
+
let d = num2;
|
|
1483
|
+
while (power > _0n2) {
|
|
1484
|
+
if (power & _1n2)
|
|
1485
|
+
p = Fp.mul(p, d);
|
|
1486
|
+
d = Fp.sqr(d);
|
|
1487
|
+
power >>= _1n2;
|
|
1488
|
+
}
|
|
1489
|
+
return p;
|
|
1490
|
+
}
|
|
1491
|
+
function FpInvertBatch(Fp, nums, passZero = false) {
|
|
1492
|
+
const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
|
|
1493
|
+
const multipliedAcc = nums.reduce((acc, num2, i) => {
|
|
1494
|
+
if (Fp.is0(num2))
|
|
1495
|
+
return acc;
|
|
1496
|
+
inverted[i] = acc;
|
|
1497
|
+
return Fp.mul(acc, num2);
|
|
1498
|
+
}, Fp.ONE);
|
|
1499
|
+
const invertedAcc = Fp.inv(multipliedAcc);
|
|
1500
|
+
nums.reduceRight((acc, num2, i) => {
|
|
1501
|
+
if (Fp.is0(num2))
|
|
1502
|
+
return acc;
|
|
1503
|
+
inverted[i] = Fp.mul(acc, inverted[i]);
|
|
1504
|
+
return Fp.mul(acc, num2);
|
|
1505
|
+
}, invertedAcc);
|
|
1506
|
+
return inverted;
|
|
1507
|
+
}
|
|
1508
|
+
function FpLegendre(Fp, n) {
|
|
1509
|
+
const p1mod2 = (Fp.ORDER - _1n2) / _2n;
|
|
1510
|
+
const powered = Fp.pow(n, p1mod2);
|
|
1511
|
+
const yes = Fp.eql(powered, Fp.ONE);
|
|
1512
|
+
const zero = Fp.eql(powered, Fp.ZERO);
|
|
1513
|
+
const no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
|
1514
|
+
if (!yes && !zero && !no)
|
|
1515
|
+
throw new Error("invalid Legendre symbol result");
|
|
1516
|
+
return yes ? 1 : zero ? 0 : -1;
|
|
1517
|
+
}
|
|
1518
|
+
function nLength(n, nBitLength) {
|
|
1519
|
+
if (nBitLength !== void 0)
|
|
1520
|
+
anumber(nBitLength);
|
|
1521
|
+
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
|
1522
|
+
const nByteLength = Math.ceil(_nBitLength / 8);
|
|
1523
|
+
return { nBitLength: _nBitLength, nByteLength };
|
|
1524
|
+
}
|
|
1525
|
+
var _Field = class {
|
|
1526
|
+
ORDER;
|
|
1527
|
+
BITS;
|
|
1528
|
+
BYTES;
|
|
1529
|
+
isLE;
|
|
1530
|
+
ZERO = _0n2;
|
|
1531
|
+
ONE = _1n2;
|
|
1532
|
+
_lengths;
|
|
1533
|
+
_sqrt;
|
|
1534
|
+
// cached sqrt
|
|
1535
|
+
_mod;
|
|
1536
|
+
constructor(ORDER, opts = {}) {
|
|
1537
|
+
if (ORDER <= _0n2)
|
|
1538
|
+
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
|
1539
|
+
let _nbitLength = void 0;
|
|
1540
|
+
this.isLE = false;
|
|
1541
|
+
if (opts != null && typeof opts === "object") {
|
|
1542
|
+
if (typeof opts.BITS === "number")
|
|
1543
|
+
_nbitLength = opts.BITS;
|
|
1544
|
+
if (typeof opts.sqrt === "function")
|
|
1545
|
+
this.sqrt = opts.sqrt;
|
|
1546
|
+
if (typeof opts.isLE === "boolean")
|
|
1547
|
+
this.isLE = opts.isLE;
|
|
1548
|
+
if (opts.allowedLengths)
|
|
1549
|
+
this._lengths = opts.allowedLengths?.slice();
|
|
1550
|
+
if (typeof opts.modFromBytes === "boolean")
|
|
1551
|
+
this._mod = opts.modFromBytes;
|
|
1552
|
+
}
|
|
1553
|
+
const { nBitLength, nByteLength } = nLength(ORDER, _nbitLength);
|
|
1554
|
+
if (nByteLength > 2048)
|
|
1555
|
+
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
|
1556
|
+
this.ORDER = ORDER;
|
|
1557
|
+
this.BITS = nBitLength;
|
|
1558
|
+
this.BYTES = nByteLength;
|
|
1559
|
+
this._sqrt = void 0;
|
|
1560
|
+
Object.preventExtensions(this);
|
|
1561
|
+
}
|
|
1562
|
+
create(num2) {
|
|
1563
|
+
return mod(num2, this.ORDER);
|
|
1564
|
+
}
|
|
1565
|
+
isValid(num2) {
|
|
1566
|
+
if (typeof num2 !== "bigint")
|
|
1567
|
+
throw new Error("invalid field element: expected bigint, got " + typeof num2);
|
|
1568
|
+
return _0n2 <= num2 && num2 < this.ORDER;
|
|
1569
|
+
}
|
|
1570
|
+
is0(num2) {
|
|
1571
|
+
return num2 === _0n2;
|
|
1572
|
+
}
|
|
1573
|
+
// is valid and invertible
|
|
1574
|
+
isValidNot0(num2) {
|
|
1575
|
+
return !this.is0(num2) && this.isValid(num2);
|
|
1576
|
+
}
|
|
1577
|
+
isOdd(num2) {
|
|
1578
|
+
return (num2 & _1n2) === _1n2;
|
|
1579
|
+
}
|
|
1580
|
+
neg(num2) {
|
|
1581
|
+
return mod(-num2, this.ORDER);
|
|
1582
|
+
}
|
|
1583
|
+
eql(lhs, rhs) {
|
|
1584
|
+
return lhs === rhs;
|
|
1585
|
+
}
|
|
1586
|
+
sqr(num2) {
|
|
1587
|
+
return mod(num2 * num2, this.ORDER);
|
|
1588
|
+
}
|
|
1589
|
+
add(lhs, rhs) {
|
|
1590
|
+
return mod(lhs + rhs, this.ORDER);
|
|
1591
|
+
}
|
|
1592
|
+
sub(lhs, rhs) {
|
|
1593
|
+
return mod(lhs - rhs, this.ORDER);
|
|
1594
|
+
}
|
|
1595
|
+
mul(lhs, rhs) {
|
|
1596
|
+
return mod(lhs * rhs, this.ORDER);
|
|
1597
|
+
}
|
|
1598
|
+
pow(num2, power) {
|
|
1599
|
+
return FpPow(this, num2, power);
|
|
1600
|
+
}
|
|
1601
|
+
div(lhs, rhs) {
|
|
1602
|
+
return mod(lhs * invert(rhs, this.ORDER), this.ORDER);
|
|
1603
|
+
}
|
|
1604
|
+
// Same as above, but doesn't normalize
|
|
1605
|
+
sqrN(num2) {
|
|
1606
|
+
return num2 * num2;
|
|
1607
|
+
}
|
|
1608
|
+
addN(lhs, rhs) {
|
|
1609
|
+
return lhs + rhs;
|
|
1610
|
+
}
|
|
1611
|
+
subN(lhs, rhs) {
|
|
1612
|
+
return lhs - rhs;
|
|
1613
|
+
}
|
|
1614
|
+
mulN(lhs, rhs) {
|
|
1615
|
+
return lhs * rhs;
|
|
1616
|
+
}
|
|
1617
|
+
inv(num2) {
|
|
1618
|
+
return invert(num2, this.ORDER);
|
|
1619
|
+
}
|
|
1620
|
+
sqrt(num2) {
|
|
1621
|
+
if (!this._sqrt)
|
|
1622
|
+
this._sqrt = FpSqrt(this.ORDER);
|
|
1623
|
+
return this._sqrt(this, num2);
|
|
1624
|
+
}
|
|
1625
|
+
toBytes(num2) {
|
|
1626
|
+
return this.isLE ? numberToBytesLE(num2, this.BYTES) : numberToBytesBE(num2, this.BYTES);
|
|
1627
|
+
}
|
|
1628
|
+
fromBytes(bytes, skipValidation = false) {
|
|
1629
|
+
abytes(bytes);
|
|
1630
|
+
const { _lengths: allowedLengths, BYTES, isLE, ORDER, _mod: modFromBytes } = this;
|
|
1631
|
+
if (allowedLengths) {
|
|
1632
|
+
if (!allowedLengths.includes(bytes.length) || bytes.length > BYTES) {
|
|
1633
|
+
throw new Error("Field.fromBytes: expected " + allowedLengths + " bytes, got " + bytes.length);
|
|
1634
|
+
}
|
|
1635
|
+
const padded = new Uint8Array(BYTES);
|
|
1636
|
+
padded.set(bytes, isLE ? 0 : padded.length - bytes.length);
|
|
1637
|
+
bytes = padded;
|
|
1638
|
+
}
|
|
1639
|
+
if (bytes.length !== BYTES)
|
|
1640
|
+
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
|
1641
|
+
let scalar = isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
|
1642
|
+
if (modFromBytes)
|
|
1643
|
+
scalar = mod(scalar, ORDER);
|
|
1644
|
+
if (!skipValidation) {
|
|
1645
|
+
if (!this.isValid(scalar))
|
|
1646
|
+
throw new Error("invalid field element: outside of range 0..ORDER");
|
|
1647
|
+
}
|
|
1648
|
+
return scalar;
|
|
540
1649
|
}
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
get(key) {
|
|
545
|
-
return fromSynchronousStore(() => loadFile().channels[canonicalChannelId(key)]?.context);
|
|
1650
|
+
// TODO: we don't need it here, move out to separate fn
|
|
1651
|
+
invertBatch(lst) {
|
|
1652
|
+
return FpInvertBatch(this, lst);
|
|
546
1653
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
);
|
|
1654
|
+
// We can't move this out because Fp6, Fp12 implement it
|
|
1655
|
+
// and it's unclear what to return in there.
|
|
1656
|
+
cmov(a, b, condition) {
|
|
1657
|
+
return condition ? b : a;
|
|
552
1658
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
1659
|
+
};
|
|
1660
|
+
function Field(ORDER, opts = {}) {
|
|
1661
|
+
return new _Field(ORDER, opts);
|
|
1662
|
+
}
|
|
1663
|
+
function getFieldBytesLength(fieldOrder) {
|
|
1664
|
+
if (typeof fieldOrder !== "bigint")
|
|
1665
|
+
throw new Error("field order must be bigint");
|
|
1666
|
+
const bitLength = fieldOrder.toString(2).length;
|
|
1667
|
+
return Math.ceil(bitLength / 8);
|
|
1668
|
+
}
|
|
1669
|
+
function getMinHashLength(fieldOrder) {
|
|
1670
|
+
const length = getFieldBytesLength(fieldOrder);
|
|
1671
|
+
return length + Math.ceil(length / 2);
|
|
1672
|
+
}
|
|
1673
|
+
function mapHashToField(key, fieldOrder, isLE = false) {
|
|
1674
|
+
abytes(key);
|
|
1675
|
+
const len = key.length;
|
|
1676
|
+
const fieldLen = getFieldBytesLength(fieldOrder);
|
|
1677
|
+
const minLen = getMinHashLength(fieldOrder);
|
|
1678
|
+
if (len < 16 || len < minLen || len > 1024)
|
|
1679
|
+
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
|
1680
|
+
const num2 = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
|
1681
|
+
const reduced = mod(num2, fieldOrder - _1n2) + _1n2;
|
|
1682
|
+
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
// node_modules/@noble/curves/abstract/curve.js
|
|
1686
|
+
var _0n3 = /* @__PURE__ */ BigInt(0);
|
|
1687
|
+
var _1n3 = /* @__PURE__ */ BigInt(1);
|
|
1688
|
+
function negateCt(condition, item) {
|
|
1689
|
+
const neg = item.negate();
|
|
1690
|
+
return condition ? neg : item;
|
|
1691
|
+
}
|
|
1692
|
+
function normalizeZ(c, points) {
|
|
1693
|
+
const invertedZs = FpInvertBatch(c.Fp, points.map((p) => p.Z));
|
|
1694
|
+
return points.map((p, i) => c.fromAffine(p.toAffine(invertedZs[i])));
|
|
1695
|
+
}
|
|
1696
|
+
function validateW(W, bits) {
|
|
1697
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
1698
|
+
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
|
1699
|
+
}
|
|
1700
|
+
function calcWOpts(W, scalarBits) {
|
|
1701
|
+
validateW(W, scalarBits);
|
|
1702
|
+
const windows = Math.ceil(scalarBits / W) + 1;
|
|
1703
|
+
const windowSize = 2 ** (W - 1);
|
|
1704
|
+
const maxNumber = 2 ** W;
|
|
1705
|
+
const mask = bitMask(W);
|
|
1706
|
+
const shiftBy = BigInt(W);
|
|
1707
|
+
return { windows, windowSize, mask, maxNumber, shiftBy };
|
|
1708
|
+
}
|
|
1709
|
+
function calcOffsets(n, window, wOpts) {
|
|
1710
|
+
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
|
1711
|
+
let wbits = Number(n & mask);
|
|
1712
|
+
let nextN = n >> shiftBy;
|
|
1713
|
+
if (wbits > windowSize) {
|
|
1714
|
+
wbits -= maxNumber;
|
|
1715
|
+
nextN += _1n3;
|
|
1716
|
+
}
|
|
1717
|
+
const offsetStart = window * windowSize;
|
|
1718
|
+
const offset = offsetStart + Math.abs(wbits) - 1;
|
|
1719
|
+
const isZero = wbits === 0;
|
|
1720
|
+
const isNeg = wbits < 0;
|
|
1721
|
+
const isNegF = window % 2 !== 0;
|
|
1722
|
+
const offsetF = offsetStart;
|
|
1723
|
+
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
|
1724
|
+
}
|
|
1725
|
+
var pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
|
1726
|
+
var pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
|
1727
|
+
function getW(P) {
|
|
1728
|
+
return pointWindowSizes.get(P) || 1;
|
|
1729
|
+
}
|
|
1730
|
+
function assert0(n) {
|
|
1731
|
+
if (n !== _0n3)
|
|
1732
|
+
throw new Error("invalid wNAF");
|
|
1733
|
+
}
|
|
1734
|
+
var wNAF = class {
|
|
1735
|
+
BASE;
|
|
1736
|
+
ZERO;
|
|
1737
|
+
Fn;
|
|
1738
|
+
bits;
|
|
1739
|
+
// Parametrized with a given Point class (not individual point)
|
|
1740
|
+
constructor(Point, bits) {
|
|
1741
|
+
this.BASE = Point.BASE;
|
|
1742
|
+
this.ZERO = Point.ZERO;
|
|
1743
|
+
this.Fn = Point.Fn;
|
|
1744
|
+
this.bits = bits;
|
|
1745
|
+
}
|
|
1746
|
+
// non-const time multiplication ladder
|
|
1747
|
+
_unsafeLadder(elm, n, p = this.ZERO) {
|
|
1748
|
+
let d = elm;
|
|
1749
|
+
while (n > _0n3) {
|
|
1750
|
+
if (n & _1n3)
|
|
1751
|
+
p = p.add(d);
|
|
1752
|
+
d = d.double();
|
|
1753
|
+
n >>= _1n3;
|
|
1754
|
+
}
|
|
1755
|
+
return p;
|
|
564
1756
|
}
|
|
565
1757
|
/**
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
*
|
|
1758
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
1759
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
1760
|
+
* Number of precomputed points depends on the curve size:
|
|
1761
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
1762
|
+
* - 𝑊 is the window size
|
|
1763
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
1764
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
1765
|
+
* @param point Point instance
|
|
1766
|
+
* @param W window size
|
|
1767
|
+
* @returns precomputed point tables flattened to a single array
|
|
569
1768
|
*/
|
|
570
|
-
|
|
571
|
-
const
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
1769
|
+
precomputeWindow(point, W) {
|
|
1770
|
+
const { windows, windowSize } = calcWOpts(W, this.bits);
|
|
1771
|
+
const points = [];
|
|
1772
|
+
let p = point;
|
|
1773
|
+
let base = p;
|
|
1774
|
+
for (let window = 0; window < windows; window++) {
|
|
1775
|
+
base = p;
|
|
1776
|
+
points.push(base);
|
|
1777
|
+
for (let i = 1; i < windowSize; i++) {
|
|
1778
|
+
base = base.add(p);
|
|
1779
|
+
points.push(base);
|
|
1780
|
+
}
|
|
1781
|
+
p = base.double();
|
|
1782
|
+
}
|
|
1783
|
+
return points;
|
|
576
1784
|
}
|
|
577
1785
|
/**
|
|
578
|
-
*
|
|
579
|
-
*
|
|
580
|
-
*
|
|
581
|
-
*
|
|
582
|
-
* matters later: where the figure came from. Nothing else about the write
|
|
583
|
-
* differs.
|
|
1786
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
1787
|
+
* More compact implementation:
|
|
1788
|
+
* https://github.com/paulmillr/noble-secp256k1/blob/47cb1669b6e506ad66b35fe7d76132ae97465da2/index.ts#L502-L541
|
|
1789
|
+
* @returns real and fake (for const-time) points
|
|
584
1790
|
*/
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
1791
|
+
wNAF(W, precomputes, n) {
|
|
1792
|
+
if (!this.Fn.isValid(n))
|
|
1793
|
+
throw new Error("invalid scalar");
|
|
1794
|
+
let p = this.ZERO;
|
|
1795
|
+
let f = this.BASE;
|
|
1796
|
+
const wo = calcWOpts(W, this.bits);
|
|
1797
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1798
|
+
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
|
1799
|
+
n = nextN;
|
|
1800
|
+
if (isZero) {
|
|
1801
|
+
f = f.add(negateCt(isNegF, precomputes[offsetF]));
|
|
1802
|
+
} else {
|
|
1803
|
+
p = p.add(negateCt(isNeg, precomputes[offset]));
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
assert0(n);
|
|
1807
|
+
return { p, f };
|
|
589
1808
|
}
|
|
590
1809
|
/**
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
* The replay acknowledgement's idempotent second pass is what needs this: it
|
|
595
|
-
* restates a cumulative base without adding to the balance, so it observes
|
|
596
|
-
* nothing and must not certify the local sum it left alone. The caller owes
|
|
597
|
-
* the precondition in the name — a write that does move `balance` belongs on
|
|
598
|
-
* one of the other two.
|
|
1810
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
1811
|
+
* @param acc accumulator point to add result of multiplication
|
|
1812
|
+
* @returns point
|
|
599
1813
|
*/
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
1814
|
+
wNAFUnsafe(W, precomputes, n, acc = this.ZERO) {
|
|
1815
|
+
const wo = calcWOpts(W, this.bits);
|
|
1816
|
+
for (let window = 0; window < wo.windows; window++) {
|
|
1817
|
+
if (n === _0n3)
|
|
1818
|
+
break;
|
|
1819
|
+
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
|
1820
|
+
n = nextN;
|
|
1821
|
+
if (isZero) {
|
|
1822
|
+
continue;
|
|
1823
|
+
} else {
|
|
1824
|
+
const item = precomputes[offset];
|
|
1825
|
+
acc = acc.add(isNeg ? item.negate() : item);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
assert0(n);
|
|
1829
|
+
return acc;
|
|
604
1830
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
1831
|
+
getPrecomputes(W, point, transform) {
|
|
1832
|
+
let comp = pointPrecomputes.get(point);
|
|
1833
|
+
if (!comp) {
|
|
1834
|
+
comp = this.precomputeWindow(point, W);
|
|
1835
|
+
if (W !== 1) {
|
|
1836
|
+
if (typeof transform === "function")
|
|
1837
|
+
comp = transform(comp);
|
|
1838
|
+
pointPrecomputes.set(point, comp);
|
|
1839
|
+
}
|
|
1840
|
+
}
|
|
1841
|
+
return comp;
|
|
616
1842
|
}
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
* Only `balance` and `totalClaimed` move. The highest DVM-acknowledged
|
|
621
|
-
* cumulative base stays fail-closed, and the chain observation clears the
|
|
622
|
-
* unreconciled stamp.
|
|
623
|
-
*/
|
|
624
|
-
reconcileChainState(key, state) {
|
|
625
|
-
return fromSynchronousStore(() => {
|
|
626
|
-
const file = loadFile();
|
|
627
|
-
const id = canonicalChannelId(key);
|
|
628
|
-
const record = file.channels[id] ?? {};
|
|
629
|
-
file.channels[id] = channelRecordWithContext(
|
|
630
|
-
record,
|
|
631
|
-
{ ...record.context ?? {}, ...state },
|
|
632
|
-
"observed",
|
|
633
|
-
"hold"
|
|
634
|
-
);
|
|
635
|
-
saveFile(file);
|
|
636
|
-
});
|
|
1843
|
+
cached(point, scalar, transform) {
|
|
1844
|
+
const W = getW(point);
|
|
1845
|
+
return this.wNAF(W, this.getPrecomputes(W, point, transform), scalar);
|
|
637
1846
|
}
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
1847
|
+
unsafe(point, scalar, transform, prev) {
|
|
1848
|
+
const W = getW(point);
|
|
1849
|
+
if (W === 1)
|
|
1850
|
+
return this._unsafeLadder(point, scalar, prev);
|
|
1851
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, point, transform), scalar, prev);
|
|
1852
|
+
}
|
|
1853
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
1854
|
+
// using windowed method. This specifies window size and
|
|
1855
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
1856
|
+
createCache(P, W) {
|
|
1857
|
+
validateW(W, this.bits);
|
|
1858
|
+
pointWindowSizes.set(P, W);
|
|
1859
|
+
pointPrecomputes.delete(P);
|
|
1860
|
+
}
|
|
1861
|
+
hasCache(elm) {
|
|
1862
|
+
return getW(elm) !== 1;
|
|
651
1863
|
}
|
|
652
1864
|
};
|
|
653
|
-
function
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
1865
|
+
function mulEndoUnsafe(Point, point, k1, k2) {
|
|
1866
|
+
let acc = point;
|
|
1867
|
+
let p1 = Point.ZERO;
|
|
1868
|
+
let p2 = Point.ZERO;
|
|
1869
|
+
while (k1 > _0n3 || k2 > _0n3) {
|
|
1870
|
+
if (k1 & _1n3)
|
|
1871
|
+
p1 = p1.add(acc);
|
|
1872
|
+
if (k2 & _1n3)
|
|
1873
|
+
p2 = p2.add(acc);
|
|
1874
|
+
acc = acc.double();
|
|
1875
|
+
k1 >>= _1n3;
|
|
1876
|
+
k2 >>= _1n3;
|
|
1877
|
+
}
|
|
1878
|
+
return { p1, p2 };
|
|
1879
|
+
}
|
|
1880
|
+
function createField(order, field, isLE) {
|
|
1881
|
+
if (field) {
|
|
1882
|
+
if (field.ORDER !== order)
|
|
1883
|
+
throw new Error("Field.ORDER must match order: Fp == p, Fn == n");
|
|
1884
|
+
validateField(field);
|
|
1885
|
+
return field;
|
|
1886
|
+
} else {
|
|
1887
|
+
return Field(order, { isLE });
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
function createCurveFields(type, CURVE, curveOpts = {}, FpFnLE) {
|
|
1891
|
+
if (FpFnLE === void 0)
|
|
1892
|
+
FpFnLE = type === "edwards";
|
|
1893
|
+
if (!CURVE || typeof CURVE !== "object")
|
|
1894
|
+
throw new Error(`expected valid ${type} CURVE object`);
|
|
1895
|
+
for (const p of ["p", "n", "h"]) {
|
|
1896
|
+
const val = CURVE[p];
|
|
1897
|
+
if (!(typeof val === "bigint" && val > _0n3))
|
|
1898
|
+
throw new Error(`CURVE.${p} must be positive bigint`);
|
|
1899
|
+
}
|
|
1900
|
+
const Fp = createField(CURVE.p, curveOpts.Fp, FpFnLE);
|
|
1901
|
+
const Fn = createField(CURVE.n, curveOpts.Fn, FpFnLE);
|
|
1902
|
+
const _b = type === "weierstrass" ? "b" : "d";
|
|
1903
|
+
const params = ["Gx", "Gy", "a", _b];
|
|
1904
|
+
for (const p of params) {
|
|
1905
|
+
if (!Fp.isValid(CURVE[p]))
|
|
1906
|
+
throw new Error(`CURVE.${p} must be valid field element of CURVE.Fp`);
|
|
1907
|
+
}
|
|
1908
|
+
CURVE = Object.freeze(Object.assign({}, CURVE));
|
|
1909
|
+
return { CURVE, Fp, Fn };
|
|
1910
|
+
}
|
|
1911
|
+
function createKeygen(randomSecretKey, getPublicKey) {
|
|
1912
|
+
return function keygen(seed) {
|
|
1913
|
+
const secretKey = randomSecretKey(seed);
|
|
1914
|
+
return { secretKey, publicKey: getPublicKey(secretKey) };
|
|
661
1915
|
};
|
|
662
|
-
saveFile(file);
|
|
663
1916
|
}
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
1917
|
+
|
|
1918
|
+
// node_modules/@noble/hashes/hmac.js
|
|
1919
|
+
var _HMAC = class {
|
|
1920
|
+
oHash;
|
|
1921
|
+
iHash;
|
|
1922
|
+
blockLen;
|
|
1923
|
+
outputLen;
|
|
1924
|
+
finished = false;
|
|
1925
|
+
destroyed = false;
|
|
1926
|
+
constructor(hash, key) {
|
|
1927
|
+
ahash(hash);
|
|
1928
|
+
abytes(key, void 0, "key");
|
|
1929
|
+
this.iHash = hash.create();
|
|
1930
|
+
if (typeof this.iHash.update !== "function")
|
|
1931
|
+
throw new Error("Expected instance of class which extends utils.Hash");
|
|
1932
|
+
this.blockLen = this.iHash.blockLen;
|
|
1933
|
+
this.outputLen = this.iHash.outputLen;
|
|
1934
|
+
const blockLen = this.blockLen;
|
|
1935
|
+
const pad = new Uint8Array(blockLen);
|
|
1936
|
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
|
1937
|
+
for (let i = 0; i < pad.length; i++)
|
|
1938
|
+
pad[i] ^= 54;
|
|
1939
|
+
this.iHash.update(pad);
|
|
1940
|
+
this.oHash = hash.create();
|
|
1941
|
+
for (let i = 0; i < pad.length; i++)
|
|
1942
|
+
pad[i] ^= 54 ^ 92;
|
|
1943
|
+
this.oHash.update(pad);
|
|
1944
|
+
clean(pad);
|
|
1945
|
+
}
|
|
1946
|
+
update(buf) {
|
|
1947
|
+
aexists(this);
|
|
1948
|
+
this.iHash.update(buf);
|
|
1949
|
+
return this;
|
|
1950
|
+
}
|
|
1951
|
+
digestInto(out) {
|
|
1952
|
+
aexists(this);
|
|
1953
|
+
abytes(out, this.outputLen, "output");
|
|
1954
|
+
this.finished = true;
|
|
1955
|
+
this.iHash.digestInto(out);
|
|
1956
|
+
this.oHash.update(out);
|
|
1957
|
+
this.oHash.digestInto(out);
|
|
1958
|
+
this.destroy();
|
|
1959
|
+
}
|
|
1960
|
+
digest() {
|
|
1961
|
+
const out = new Uint8Array(this.oHash.outputLen);
|
|
1962
|
+
this.digestInto(out);
|
|
1963
|
+
return out;
|
|
1964
|
+
}
|
|
1965
|
+
_cloneInto(to) {
|
|
1966
|
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
|
1967
|
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
|
1968
|
+
to = to;
|
|
1969
|
+
to.finished = finished;
|
|
1970
|
+
to.destroyed = destroyed;
|
|
1971
|
+
to.blockLen = blockLen;
|
|
1972
|
+
to.outputLen = outputLen;
|
|
1973
|
+
to.oHash = oHash._cloneInto(to.oHash);
|
|
1974
|
+
to.iHash = iHash._cloneInto(to.iHash);
|
|
1975
|
+
return to;
|
|
1976
|
+
}
|
|
1977
|
+
clone() {
|
|
1978
|
+
return this._cloneInto();
|
|
1979
|
+
}
|
|
1980
|
+
destroy() {
|
|
1981
|
+
this.destroyed = true;
|
|
1982
|
+
this.oHash.destroy();
|
|
1983
|
+
this.iHash.destroy();
|
|
1984
|
+
}
|
|
1985
|
+
};
|
|
1986
|
+
var hmac = (hash, key, message2) => new _HMAC(hash, key).update(message2).digest();
|
|
1987
|
+
hmac.create = (hash, key) => new _HMAC(hash, key);
|
|
1988
|
+
|
|
1989
|
+
// node_modules/@noble/curves/abstract/weierstrass.js
|
|
1990
|
+
var divNearest = (num2, den) => (num2 + (num2 >= 0 ? den : -den) / _2n2) / den;
|
|
1991
|
+
function _splitEndoScalar(k, basis, n) {
|
|
1992
|
+
const [[a1, b1], [a2, b2]] = basis;
|
|
1993
|
+
const c1 = divNearest(b2 * k, n);
|
|
1994
|
+
const c2 = divNearest(-b1 * k, n);
|
|
1995
|
+
let k1 = k - c1 * a1 - c2 * a2;
|
|
1996
|
+
let k2 = -c1 * b1 - c2 * b2;
|
|
1997
|
+
const k1neg = k1 < _0n4;
|
|
1998
|
+
const k2neg = k2 < _0n4;
|
|
1999
|
+
if (k1neg)
|
|
2000
|
+
k1 = -k1;
|
|
2001
|
+
if (k2neg)
|
|
2002
|
+
k2 = -k2;
|
|
2003
|
+
const MAX_NUM = bitMask(Math.ceil(bitLen(n) / 2)) + _1n4;
|
|
2004
|
+
if (k1 < _0n4 || k1 >= MAX_NUM || k2 < _0n4 || k2 >= MAX_NUM) {
|
|
2005
|
+
throw new Error("splitScalar (endomorphism): failed, k=" + k);
|
|
2006
|
+
}
|
|
2007
|
+
return { k1neg, k1, k2neg, k2 };
|
|
2008
|
+
}
|
|
2009
|
+
function validateSigFormat(format) {
|
|
2010
|
+
if (!["compact", "recovered", "der"].includes(format))
|
|
2011
|
+
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
2012
|
+
return format;
|
|
2013
|
+
}
|
|
2014
|
+
function validateSigOpts(opts, def) {
|
|
2015
|
+
const optsn = {};
|
|
2016
|
+
for (let optName of Object.keys(def)) {
|
|
2017
|
+
optsn[optName] = opts[optName] === void 0 ? def[optName] : opts[optName];
|
|
2018
|
+
}
|
|
2019
|
+
abool(optsn.lowS, "lowS");
|
|
2020
|
+
abool(optsn.prehash, "prehash");
|
|
2021
|
+
if (optsn.format !== void 0)
|
|
2022
|
+
validateSigFormat(optsn.format);
|
|
2023
|
+
return optsn;
|
|
2024
|
+
}
|
|
2025
|
+
var DERErr = class extends Error {
|
|
2026
|
+
constructor(m = "") {
|
|
2027
|
+
super(m);
|
|
672
2028
|
}
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
2029
|
+
};
|
|
2030
|
+
var DER = {
|
|
2031
|
+
// asn.1 DER encoding utils
|
|
2032
|
+
Err: DERErr,
|
|
2033
|
+
// Basic building block is TLV (Tag-Length-Value)
|
|
2034
|
+
_tlv: {
|
|
2035
|
+
encode: (tag2, data) => {
|
|
2036
|
+
const { Err: E } = DER;
|
|
2037
|
+
if (tag2 < 0 || tag2 > 256)
|
|
2038
|
+
throw new E("tlv.encode: wrong tag");
|
|
2039
|
+
if (data.length & 1)
|
|
2040
|
+
throw new E("tlv.encode: unpadded data");
|
|
2041
|
+
const dataLen = data.length / 2;
|
|
2042
|
+
const len = numberToHexUnpadded(dataLen);
|
|
2043
|
+
if (len.length / 2 & 128)
|
|
2044
|
+
throw new E("tlv.encode: long form length too big");
|
|
2045
|
+
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
|
2046
|
+
const t = numberToHexUnpadded(tag2);
|
|
2047
|
+
return t + lenLen + len + data;
|
|
2048
|
+
},
|
|
2049
|
+
// v - value, l - left bytes (unparsed)
|
|
2050
|
+
decode(tag2, data) {
|
|
2051
|
+
const { Err: E } = DER;
|
|
2052
|
+
let pos = 0;
|
|
2053
|
+
if (tag2 < 0 || tag2 > 256)
|
|
2054
|
+
throw new E("tlv.encode: wrong tag");
|
|
2055
|
+
if (data.length < 2 || data[pos++] !== tag2)
|
|
2056
|
+
throw new E("tlv.decode: wrong tlv");
|
|
2057
|
+
const first = data[pos++];
|
|
2058
|
+
const isLong = !!(first & 128);
|
|
2059
|
+
let length = 0;
|
|
2060
|
+
if (!isLong)
|
|
2061
|
+
length = first;
|
|
2062
|
+
else {
|
|
2063
|
+
const lenLen = first & 127;
|
|
2064
|
+
if (!lenLen)
|
|
2065
|
+
throw new E("tlv.decode(long): indefinite length not supported");
|
|
2066
|
+
if (lenLen > 4)
|
|
2067
|
+
throw new E("tlv.decode(long): byte length is too big");
|
|
2068
|
+
const lengthBytes = data.subarray(pos, pos + lenLen);
|
|
2069
|
+
if (lengthBytes.length !== lenLen)
|
|
2070
|
+
throw new E("tlv.decode: length bytes not complete");
|
|
2071
|
+
if (lengthBytes[0] === 0)
|
|
2072
|
+
throw new E("tlv.decode(long): zero leftmost byte");
|
|
2073
|
+
for (const b of lengthBytes)
|
|
2074
|
+
length = length << 8 | b;
|
|
2075
|
+
pos += lenLen;
|
|
2076
|
+
if (length < 128)
|
|
2077
|
+
throw new E("tlv.decode(long): not minimal encoding");
|
|
2078
|
+
}
|
|
2079
|
+
const v = data.subarray(pos, pos + length);
|
|
2080
|
+
if (v.length !== length)
|
|
2081
|
+
throw new E("tlv.decode: wrong value length");
|
|
2082
|
+
return { v, l: data.subarray(pos + length) };
|
|
681
2083
|
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
})
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
)
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
const refusedAt = Date.now();
|
|
746
|
-
file.pending = file.pending.map((entry) => entry === target ? { ...entry, refusedAt } : entry);
|
|
747
|
-
saveFile(file);
|
|
748
|
-
}
|
|
749
|
-
function clearPendingX402Funding(pending) {
|
|
750
|
-
const { file } = loadFundingFile();
|
|
751
|
-
const next = file.pending.filter((entry) => !sameJournalEntry(entry, pending));
|
|
752
|
-
if (next.length === file.pending.length) return;
|
|
753
|
-
file.pending = next;
|
|
754
|
-
saveFile(file);
|
|
755
|
-
}
|
|
756
|
-
function writeContext(key, context, provenance, base) {
|
|
757
|
-
const file = loadFile();
|
|
758
|
-
const id = canonicalChannelId(key);
|
|
759
|
-
const record = file.channels[id] ?? {};
|
|
760
|
-
file.channels[id] = channelRecordWithContext(record, context, provenance, base);
|
|
761
|
-
saveFile(file);
|
|
762
|
-
}
|
|
763
|
-
function channelRecordWithContext(record, context, provenance, base) {
|
|
764
|
-
const { unreconciledBalanceAt: previousStamp, ...kept } = record;
|
|
765
|
-
const stamp = provenance === "unreconciled" ? Date.now() : provenance === "unchanged" ? previousStamp : void 0;
|
|
766
|
-
const stored = readAcknowledgedCumulativeAmount(record);
|
|
767
|
-
const acknowledgedCumulativeAmount = base === "advance" ? highestCumulativeAmount(stored, context.chargedCumulativeAmount) : stored;
|
|
768
|
-
return {
|
|
769
|
-
...kept,
|
|
770
|
-
context,
|
|
771
|
-
...acknowledgedCumulativeAmount !== void 0 ? { acknowledgedCumulativeAmount } : {},
|
|
772
|
-
...stamp !== void 0 ? { unreconciledBalanceAt: stamp } : {}
|
|
773
|
-
};
|
|
774
|
-
}
|
|
775
|
-
function allChannelMetadata() {
|
|
776
|
-
return Object.values(loadFile().channels).filter((record) => record !== void 0).map((record) => record.metadata).filter((entry) => entry !== void 0);
|
|
777
|
-
}
|
|
778
|
-
function pendingKey(entry) {
|
|
779
|
-
return `${normalizeEndpoint(entry.endpoint)}\0${entry.callerPubkey}\0${entry.creditId}`;
|
|
780
|
-
}
|
|
781
|
-
function sameFundingCredit(entry, target, credits) {
|
|
782
|
-
return entry.callerPubkey === target.callerPubkey && entry.creditId === target.creditId && fundingReachableAt(entry, normalizeEndpoint(target.endpoint), credits);
|
|
783
|
-
}
|
|
784
|
-
function loadFundingFile() {
|
|
785
|
-
const snapshot = collapseFundingFile();
|
|
786
|
-
if (!snapshot.changed) return snapshot;
|
|
787
|
-
const persisted = tryWithX402ChannelLockSync(() => {
|
|
788
|
-
const current = collapseFundingFile();
|
|
789
|
-
if (current.changed) saveFile(current.file);
|
|
790
|
-
return current;
|
|
2084
|
+
},
|
|
2085
|
+
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
2086
|
+
// since we always use positive integers here. It must always be empty:
|
|
2087
|
+
// - add zero byte if exists
|
|
2088
|
+
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
2089
|
+
_int: {
|
|
2090
|
+
encode(num2) {
|
|
2091
|
+
const { Err: E } = DER;
|
|
2092
|
+
if (num2 < _0n4)
|
|
2093
|
+
throw new E("integer: negative integers are not allowed");
|
|
2094
|
+
let hex = numberToHexUnpadded(num2);
|
|
2095
|
+
if (Number.parseInt(hex[0], 16) & 8)
|
|
2096
|
+
hex = "00" + hex;
|
|
2097
|
+
if (hex.length & 1)
|
|
2098
|
+
throw new E("unexpected DER parsing assertion: unpadded hex");
|
|
2099
|
+
return hex;
|
|
2100
|
+
},
|
|
2101
|
+
decode(data) {
|
|
2102
|
+
const { Err: E } = DER;
|
|
2103
|
+
if (data[0] & 128)
|
|
2104
|
+
throw new E("invalid signature integer: negative");
|
|
2105
|
+
if (data[0] === 0 && !(data[1] & 128))
|
|
2106
|
+
throw new E("invalid signature integer: unnecessary leading zero");
|
|
2107
|
+
return bytesToNumberBE(data);
|
|
2108
|
+
}
|
|
2109
|
+
},
|
|
2110
|
+
toSig(bytes) {
|
|
2111
|
+
const { Err: E, _int: int, _tlv: tlv } = DER;
|
|
2112
|
+
const data = abytes(bytes, void 0, "signature");
|
|
2113
|
+
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
|
2114
|
+
if (seqLeftBytes.length)
|
|
2115
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
2116
|
+
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
|
2117
|
+
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
|
2118
|
+
if (sLeftBytes.length)
|
|
2119
|
+
throw new E("invalid signature: left bytes after parsing");
|
|
2120
|
+
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
|
2121
|
+
},
|
|
2122
|
+
hexFromSig(sig) {
|
|
2123
|
+
const { _tlv: tlv, _int: int } = DER;
|
|
2124
|
+
const rs = tlv.encode(2, int.encode(sig.r));
|
|
2125
|
+
const ss = tlv.encode(2, int.encode(sig.s));
|
|
2126
|
+
const seq = rs + ss;
|
|
2127
|
+
return tlv.encode(48, seq);
|
|
2128
|
+
}
|
|
2129
|
+
};
|
|
2130
|
+
var _0n4 = BigInt(0);
|
|
2131
|
+
var _1n4 = BigInt(1);
|
|
2132
|
+
var _2n2 = BigInt(2);
|
|
2133
|
+
var _3n2 = BigInt(3);
|
|
2134
|
+
var _4n2 = BigInt(4);
|
|
2135
|
+
function weierstrass(params, extraOpts = {}) {
|
|
2136
|
+
const validated = createCurveFields("weierstrass", params, extraOpts);
|
|
2137
|
+
const { Fp, Fn } = validated;
|
|
2138
|
+
let CURVE = validated.CURVE;
|
|
2139
|
+
const { h: cofactor, n: CURVE_ORDER } = CURVE;
|
|
2140
|
+
validateObject(extraOpts, {}, {
|
|
2141
|
+
allowInfinityPoint: "boolean",
|
|
2142
|
+
clearCofactor: "function",
|
|
2143
|
+
isTorsionFree: "function",
|
|
2144
|
+
fromBytes: "function",
|
|
2145
|
+
toBytes: "function",
|
|
2146
|
+
endo: "object"
|
|
791
2147
|
});
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
const
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
}
|
|
803
|
-
function
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
if (
|
|
808
|
-
|
|
809
|
-
|
|
2148
|
+
const { endo } = extraOpts;
|
|
2149
|
+
if (endo) {
|
|
2150
|
+
if (!Fp.is0(CURVE.a) || typeof endo.beta !== "bigint" || !Array.isArray(endo.basises)) {
|
|
2151
|
+
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
const lengths = getWLengths(Fp, Fn);
|
|
2155
|
+
function assertCompressionIsSupported() {
|
|
2156
|
+
if (!Fp.isOdd)
|
|
2157
|
+
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
2158
|
+
}
|
|
2159
|
+
function pointToBytes2(_c, point, isCompressed) {
|
|
2160
|
+
const { x, y } = point.toAffine();
|
|
2161
|
+
const bx = Fp.toBytes(x);
|
|
2162
|
+
abool(isCompressed, "isCompressed");
|
|
2163
|
+
if (isCompressed) {
|
|
2164
|
+
assertCompressionIsSupported();
|
|
2165
|
+
const hasEvenY = !Fp.isOdd(y);
|
|
2166
|
+
return concatBytes(pprefix(hasEvenY), bx);
|
|
2167
|
+
} else {
|
|
2168
|
+
return concatBytes(Uint8Array.of(4), bx, Fp.toBytes(y));
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
function pointFromBytes(bytes) {
|
|
2172
|
+
abytes(bytes, void 0, "Point");
|
|
2173
|
+
const { publicKey: comp, publicKeyUncompressed: uncomp } = lengths;
|
|
2174
|
+
const length = bytes.length;
|
|
2175
|
+
const head = bytes[0];
|
|
2176
|
+
const tail = bytes.subarray(1);
|
|
2177
|
+
if (length === comp && (head === 2 || head === 3)) {
|
|
2178
|
+
const x = Fp.fromBytes(tail);
|
|
2179
|
+
if (!Fp.isValid(x))
|
|
2180
|
+
throw new Error("bad point: is not on curve, wrong x");
|
|
2181
|
+
const y2 = weierstrassEquation(x);
|
|
2182
|
+
let y;
|
|
2183
|
+
try {
|
|
2184
|
+
y = Fp.sqrt(y2);
|
|
2185
|
+
} catch (sqrtError) {
|
|
2186
|
+
const err = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
|
2187
|
+
throw new Error("bad point: is not on curve, sqrt error" + err);
|
|
2188
|
+
}
|
|
2189
|
+
assertCompressionIsSupported();
|
|
2190
|
+
const evenY = Fp.isOdd(y);
|
|
2191
|
+
const evenH = (head & 1) === 1;
|
|
2192
|
+
if (evenH !== evenY)
|
|
2193
|
+
y = Fp.neg(y);
|
|
2194
|
+
return { x, y };
|
|
2195
|
+
} else if (length === uncomp && head === 4) {
|
|
2196
|
+
const L = Fp.BYTES;
|
|
2197
|
+
const x = Fp.fromBytes(tail.subarray(0, L));
|
|
2198
|
+
const y = Fp.fromBytes(tail.subarray(L, L * 2));
|
|
2199
|
+
if (!isValidXY(x, y))
|
|
2200
|
+
throw new Error("bad point: is not on curve");
|
|
2201
|
+
return { x, y };
|
|
2202
|
+
} else {
|
|
2203
|
+
throw new Error(`bad point: got length ${length}, expected compressed=${comp} or uncompressed=${uncomp}`);
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
const encodePoint = extraOpts.toBytes || pointToBytes2;
|
|
2207
|
+
const decodePoint = extraOpts.fromBytes || pointFromBytes;
|
|
2208
|
+
function weierstrassEquation(x) {
|
|
2209
|
+
const x2 = Fp.sqr(x);
|
|
2210
|
+
const x3 = Fp.mul(x2, x);
|
|
2211
|
+
return Fp.add(Fp.add(x3, Fp.mul(x, CURVE.a)), CURVE.b);
|
|
2212
|
+
}
|
|
2213
|
+
function isValidXY(x, y) {
|
|
2214
|
+
const left = Fp.sqr(y);
|
|
2215
|
+
const right = weierstrassEquation(x);
|
|
2216
|
+
return Fp.eql(left, right);
|
|
2217
|
+
}
|
|
2218
|
+
if (!isValidXY(CURVE.Gx, CURVE.Gy))
|
|
2219
|
+
throw new Error("bad curve params: generator point");
|
|
2220
|
+
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n2), _4n2);
|
|
2221
|
+
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
|
2222
|
+
if (Fp.is0(Fp.add(_4a3, _27b2)))
|
|
2223
|
+
throw new Error("bad curve params: a or b");
|
|
2224
|
+
function acoord(title, n, banZero = false) {
|
|
2225
|
+
if (!Fp.isValid(n) || banZero && Fp.is0(n))
|
|
2226
|
+
throw new Error(`bad point coordinate ${title}`);
|
|
2227
|
+
return n;
|
|
2228
|
+
}
|
|
2229
|
+
function aprjpoint(other) {
|
|
2230
|
+
if (!(other instanceof Point))
|
|
2231
|
+
throw new Error("Weierstrass Point expected");
|
|
2232
|
+
}
|
|
2233
|
+
function splitEndoScalarN(k) {
|
|
2234
|
+
if (!endo || !endo.basises)
|
|
2235
|
+
throw new Error("no endo");
|
|
2236
|
+
return _splitEndoScalar(k, endo.basises, Fn.ORDER);
|
|
2237
|
+
}
|
|
2238
|
+
const toAffineMemo = memoized((p, iz) => {
|
|
2239
|
+
const { X, Y, Z } = p;
|
|
2240
|
+
if (Fp.eql(Z, Fp.ONE))
|
|
2241
|
+
return { x: X, y: Y };
|
|
2242
|
+
const is0 = p.is0();
|
|
2243
|
+
if (iz == null)
|
|
2244
|
+
iz = is0 ? Fp.ONE : Fp.inv(Z);
|
|
2245
|
+
const x = Fp.mul(X, iz);
|
|
2246
|
+
const y = Fp.mul(Y, iz);
|
|
2247
|
+
const zz = Fp.mul(Z, iz);
|
|
2248
|
+
if (is0)
|
|
2249
|
+
return { x: Fp.ZERO, y: Fp.ZERO };
|
|
2250
|
+
if (!Fp.eql(zz, Fp.ONE))
|
|
2251
|
+
throw new Error("invZ was invalid");
|
|
2252
|
+
return { x, y };
|
|
2253
|
+
});
|
|
2254
|
+
const assertValidMemo = memoized((p) => {
|
|
2255
|
+
if (p.is0()) {
|
|
2256
|
+
if (extraOpts.allowInfinityPoint && !Fp.is0(p.Y))
|
|
2257
|
+
return;
|
|
2258
|
+
throw new Error("bad point: ZERO");
|
|
2259
|
+
}
|
|
2260
|
+
const { x, y } = p.toAffine();
|
|
2261
|
+
if (!Fp.isValid(x) || !Fp.isValid(y))
|
|
2262
|
+
throw new Error("bad point: x or y not field elements");
|
|
2263
|
+
if (!isValidXY(x, y))
|
|
2264
|
+
throw new Error("bad point: equation left != right");
|
|
2265
|
+
if (!p.isTorsionFree())
|
|
2266
|
+
throw new Error("bad point: not in prime-order subgroup");
|
|
2267
|
+
return true;
|
|
2268
|
+
});
|
|
2269
|
+
function finishEndo(endoBeta, k1p, k2p, k1neg, k2neg) {
|
|
2270
|
+
k2p = new Point(Fp.mul(k2p.X, endoBeta), k2p.Y, k2p.Z);
|
|
2271
|
+
k1p = negateCt(k1neg, k1p);
|
|
2272
|
+
k2p = negateCt(k2neg, k2p);
|
|
2273
|
+
return k1p.add(k2p);
|
|
2274
|
+
}
|
|
2275
|
+
class Point {
|
|
2276
|
+
// base / generator point
|
|
2277
|
+
static BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
|
2278
|
+
// zero / infinity / identity point
|
|
2279
|
+
static ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
|
2280
|
+
// 0, 1, 0
|
|
2281
|
+
// math field
|
|
2282
|
+
static Fp = Fp;
|
|
2283
|
+
// scalar field
|
|
2284
|
+
static Fn = Fn;
|
|
2285
|
+
X;
|
|
2286
|
+
Y;
|
|
2287
|
+
Z;
|
|
2288
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2289
|
+
constructor(X, Y, Z) {
|
|
2290
|
+
this.X = acoord("x", X);
|
|
2291
|
+
this.Y = acoord("y", Y, true);
|
|
2292
|
+
this.Z = acoord("z", Z);
|
|
2293
|
+
Object.freeze(this);
|
|
2294
|
+
}
|
|
2295
|
+
static CURVE() {
|
|
2296
|
+
return CURVE;
|
|
2297
|
+
}
|
|
2298
|
+
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
2299
|
+
static fromAffine(p) {
|
|
2300
|
+
const { x, y } = p || {};
|
|
2301
|
+
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
|
2302
|
+
throw new Error("invalid affine point");
|
|
2303
|
+
if (p instanceof Point)
|
|
2304
|
+
throw new Error("projective point not allowed");
|
|
2305
|
+
if (Fp.is0(x) && Fp.is0(y))
|
|
2306
|
+
return Point.ZERO;
|
|
2307
|
+
return new Point(x, y, Fp.ONE);
|
|
2308
|
+
}
|
|
2309
|
+
static fromBytes(bytes) {
|
|
2310
|
+
const P = Point.fromAffine(decodePoint(abytes(bytes, void 0, "point")));
|
|
2311
|
+
P.assertValidity();
|
|
2312
|
+
return P;
|
|
2313
|
+
}
|
|
2314
|
+
static fromHex(hex) {
|
|
2315
|
+
return Point.fromBytes(hexToBytes(hex));
|
|
2316
|
+
}
|
|
2317
|
+
get x() {
|
|
2318
|
+
return this.toAffine().x;
|
|
2319
|
+
}
|
|
2320
|
+
get y() {
|
|
2321
|
+
return this.toAffine().y;
|
|
2322
|
+
}
|
|
2323
|
+
/**
|
|
2324
|
+
*
|
|
2325
|
+
* @param windowSize
|
|
2326
|
+
* @param isLazy true will defer table computation until the first multiplication
|
|
2327
|
+
* @returns
|
|
2328
|
+
*/
|
|
2329
|
+
precompute(windowSize = 8, isLazy = true) {
|
|
2330
|
+
wnaf.createCache(this, windowSize);
|
|
2331
|
+
if (!isLazy)
|
|
2332
|
+
this.multiply(_3n2);
|
|
2333
|
+
return this;
|
|
810
2334
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
)
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
2335
|
+
// TODO: return `this`
|
|
2336
|
+
/** A point on curve is valid if it conforms to equation. */
|
|
2337
|
+
assertValidity() {
|
|
2338
|
+
assertValidMemo(this);
|
|
2339
|
+
}
|
|
2340
|
+
hasEvenY() {
|
|
2341
|
+
const { y } = this.toAffine();
|
|
2342
|
+
if (!Fp.isOdd)
|
|
2343
|
+
throw new Error("Field doesn't support isOdd");
|
|
2344
|
+
return !Fp.isOdd(y);
|
|
2345
|
+
}
|
|
2346
|
+
/** Compare one point to another. */
|
|
2347
|
+
equals(other) {
|
|
2348
|
+
aprjpoint(other);
|
|
2349
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2350
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
2351
|
+
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
|
2352
|
+
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
|
2353
|
+
return U1 && U2;
|
|
2354
|
+
}
|
|
2355
|
+
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
2356
|
+
negate() {
|
|
2357
|
+
return new Point(this.X, Fp.neg(this.Y), this.Z);
|
|
2358
|
+
}
|
|
2359
|
+
// Renes-Costello-Batina exception-free doubling formula.
|
|
2360
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2361
|
+
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
2362
|
+
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
2363
|
+
double() {
|
|
2364
|
+
const { a, b } = CURVE;
|
|
2365
|
+
const b3 = Fp.mul(b, _3n2);
|
|
2366
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2367
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2368
|
+
let t0 = Fp.mul(X1, X1);
|
|
2369
|
+
let t1 = Fp.mul(Y1, Y1);
|
|
2370
|
+
let t2 = Fp.mul(Z1, Z1);
|
|
2371
|
+
let t3 = Fp.mul(X1, Y1);
|
|
2372
|
+
t3 = Fp.add(t3, t3);
|
|
2373
|
+
Z3 = Fp.mul(X1, Z1);
|
|
2374
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2375
|
+
X3 = Fp.mul(a, Z3);
|
|
2376
|
+
Y3 = Fp.mul(b3, t2);
|
|
2377
|
+
Y3 = Fp.add(X3, Y3);
|
|
2378
|
+
X3 = Fp.sub(t1, Y3);
|
|
2379
|
+
Y3 = Fp.add(t1, Y3);
|
|
2380
|
+
Y3 = Fp.mul(X3, Y3);
|
|
2381
|
+
X3 = Fp.mul(t3, X3);
|
|
2382
|
+
Z3 = Fp.mul(b3, Z3);
|
|
2383
|
+
t2 = Fp.mul(a, t2);
|
|
2384
|
+
t3 = Fp.sub(t0, t2);
|
|
2385
|
+
t3 = Fp.mul(a, t3);
|
|
2386
|
+
t3 = Fp.add(t3, Z3);
|
|
2387
|
+
Z3 = Fp.add(t0, t0);
|
|
2388
|
+
t0 = Fp.add(Z3, t0);
|
|
2389
|
+
t0 = Fp.add(t0, t2);
|
|
2390
|
+
t0 = Fp.mul(t0, t3);
|
|
2391
|
+
Y3 = Fp.add(Y3, t0);
|
|
2392
|
+
t2 = Fp.mul(Y1, Z1);
|
|
2393
|
+
t2 = Fp.add(t2, t2);
|
|
2394
|
+
t0 = Fp.mul(t2, t3);
|
|
2395
|
+
X3 = Fp.sub(X3, t0);
|
|
2396
|
+
Z3 = Fp.mul(t2, t1);
|
|
2397
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2398
|
+
Z3 = Fp.add(Z3, Z3);
|
|
2399
|
+
return new Point(X3, Y3, Z3);
|
|
2400
|
+
}
|
|
2401
|
+
// Renes-Costello-Batina exception-free addition formula.
|
|
2402
|
+
// There is 30% faster Jacobian formula, but it is not complete.
|
|
2403
|
+
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
2404
|
+
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
2405
|
+
add(other) {
|
|
2406
|
+
aprjpoint(other);
|
|
2407
|
+
const { X: X1, Y: Y1, Z: Z1 } = this;
|
|
2408
|
+
const { X: X2, Y: Y2, Z: Z2 } = other;
|
|
2409
|
+
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
|
2410
|
+
const a = CURVE.a;
|
|
2411
|
+
const b3 = Fp.mul(CURVE.b, _3n2);
|
|
2412
|
+
let t0 = Fp.mul(X1, X2);
|
|
2413
|
+
let t1 = Fp.mul(Y1, Y2);
|
|
2414
|
+
let t2 = Fp.mul(Z1, Z2);
|
|
2415
|
+
let t3 = Fp.add(X1, Y1);
|
|
2416
|
+
let t4 = Fp.add(X2, Y2);
|
|
2417
|
+
t3 = Fp.mul(t3, t4);
|
|
2418
|
+
t4 = Fp.add(t0, t1);
|
|
2419
|
+
t3 = Fp.sub(t3, t4);
|
|
2420
|
+
t4 = Fp.add(X1, Z1);
|
|
2421
|
+
let t5 = Fp.add(X2, Z2);
|
|
2422
|
+
t4 = Fp.mul(t4, t5);
|
|
2423
|
+
t5 = Fp.add(t0, t2);
|
|
2424
|
+
t4 = Fp.sub(t4, t5);
|
|
2425
|
+
t5 = Fp.add(Y1, Z1);
|
|
2426
|
+
X3 = Fp.add(Y2, Z2);
|
|
2427
|
+
t5 = Fp.mul(t5, X3);
|
|
2428
|
+
X3 = Fp.add(t1, t2);
|
|
2429
|
+
t5 = Fp.sub(t5, X3);
|
|
2430
|
+
Z3 = Fp.mul(a, t4);
|
|
2431
|
+
X3 = Fp.mul(b3, t2);
|
|
2432
|
+
Z3 = Fp.add(X3, Z3);
|
|
2433
|
+
X3 = Fp.sub(t1, Z3);
|
|
2434
|
+
Z3 = Fp.add(t1, Z3);
|
|
2435
|
+
Y3 = Fp.mul(X3, Z3);
|
|
2436
|
+
t1 = Fp.add(t0, t0);
|
|
2437
|
+
t1 = Fp.add(t1, t0);
|
|
2438
|
+
t2 = Fp.mul(a, t2);
|
|
2439
|
+
t4 = Fp.mul(b3, t4);
|
|
2440
|
+
t1 = Fp.add(t1, t2);
|
|
2441
|
+
t2 = Fp.sub(t0, t2);
|
|
2442
|
+
t2 = Fp.mul(a, t2);
|
|
2443
|
+
t4 = Fp.add(t4, t2);
|
|
2444
|
+
t0 = Fp.mul(t1, t4);
|
|
2445
|
+
Y3 = Fp.add(Y3, t0);
|
|
2446
|
+
t0 = Fp.mul(t5, t4);
|
|
2447
|
+
X3 = Fp.mul(t3, X3);
|
|
2448
|
+
X3 = Fp.sub(X3, t0);
|
|
2449
|
+
t0 = Fp.mul(t3, t1);
|
|
2450
|
+
Z3 = Fp.mul(t5, Z3);
|
|
2451
|
+
Z3 = Fp.add(Z3, t0);
|
|
2452
|
+
return new Point(X3, Y3, Z3);
|
|
2453
|
+
}
|
|
2454
|
+
subtract(other) {
|
|
2455
|
+
return this.add(other.negate());
|
|
2456
|
+
}
|
|
2457
|
+
is0() {
|
|
2458
|
+
return this.equals(Point.ZERO);
|
|
2459
|
+
}
|
|
2460
|
+
/**
|
|
2461
|
+
* Constant time multiplication.
|
|
2462
|
+
* Uses wNAF method. Windowed method may be 10% faster,
|
|
2463
|
+
* but takes 2x longer to generate and consumes 2x memory.
|
|
2464
|
+
* Uses precomputes when available.
|
|
2465
|
+
* Uses endomorphism for Koblitz curves.
|
|
2466
|
+
* @param scalar by which the point would be multiplied
|
|
2467
|
+
* @returns New point
|
|
2468
|
+
*/
|
|
2469
|
+
multiply(scalar) {
|
|
2470
|
+
const { endo: endo2 } = extraOpts;
|
|
2471
|
+
if (!Fn.isValidNot0(scalar))
|
|
2472
|
+
throw new Error("invalid scalar: out of range");
|
|
2473
|
+
let point, fake;
|
|
2474
|
+
const mul = (n) => wnaf.cached(this, n, (p) => normalizeZ(Point, p));
|
|
2475
|
+
if (endo2) {
|
|
2476
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(scalar);
|
|
2477
|
+
const { p: k1p, f: k1f } = mul(k1);
|
|
2478
|
+
const { p: k2p, f: k2f } = mul(k2);
|
|
2479
|
+
fake = k1f.add(k2f);
|
|
2480
|
+
point = finishEndo(endo2.beta, k1p, k2p, k1neg, k2neg);
|
|
2481
|
+
} else {
|
|
2482
|
+
const { p, f } = mul(scalar);
|
|
2483
|
+
point = p;
|
|
2484
|
+
fake = f;
|
|
2485
|
+
}
|
|
2486
|
+
return normalizeZ(Point, [point, fake])[0];
|
|
2487
|
+
}
|
|
2488
|
+
/**
|
|
2489
|
+
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
2490
|
+
* It's faster, but should only be used when you don't care about
|
|
2491
|
+
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
2492
|
+
*/
|
|
2493
|
+
multiplyUnsafe(sc) {
|
|
2494
|
+
const { endo: endo2 } = extraOpts;
|
|
2495
|
+
const p = this;
|
|
2496
|
+
if (!Fn.isValid(sc))
|
|
2497
|
+
throw new Error("invalid scalar: out of range");
|
|
2498
|
+
if (sc === _0n4 || p.is0())
|
|
2499
|
+
return Point.ZERO;
|
|
2500
|
+
if (sc === _1n4)
|
|
2501
|
+
return p;
|
|
2502
|
+
if (wnaf.hasCache(this))
|
|
2503
|
+
return this.multiply(sc);
|
|
2504
|
+
if (endo2) {
|
|
2505
|
+
const { k1neg, k1, k2neg, k2 } = splitEndoScalarN(sc);
|
|
2506
|
+
const { p1, p2 } = mulEndoUnsafe(Point, p, k1, k2);
|
|
2507
|
+
return finishEndo(endo2.beta, p1, p2, k1neg, k2neg);
|
|
2508
|
+
} else {
|
|
2509
|
+
return wnaf.unsafe(p, sc);
|
|
2510
|
+
}
|
|
2511
|
+
}
|
|
2512
|
+
/**
|
|
2513
|
+
* Converts Projective point to affine (x, y) coordinates.
|
|
2514
|
+
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
2515
|
+
*/
|
|
2516
|
+
toAffine(invertedZ) {
|
|
2517
|
+
return toAffineMemo(this, invertedZ);
|
|
2518
|
+
}
|
|
2519
|
+
/**
|
|
2520
|
+
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
2521
|
+
* Always torsion-free for cofactor=1 curves.
|
|
2522
|
+
*/
|
|
2523
|
+
isTorsionFree() {
|
|
2524
|
+
const { isTorsionFree } = extraOpts;
|
|
2525
|
+
if (cofactor === _1n4)
|
|
2526
|
+
return true;
|
|
2527
|
+
if (isTorsionFree)
|
|
2528
|
+
return isTorsionFree(Point, this);
|
|
2529
|
+
return wnaf.unsafe(this, CURVE_ORDER).is0();
|
|
817
2530
|
}
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
}
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
continue;
|
|
2531
|
+
clearCofactor() {
|
|
2532
|
+
const { clearCofactor } = extraOpts;
|
|
2533
|
+
if (cofactor === _1n4)
|
|
2534
|
+
return this;
|
|
2535
|
+
if (clearCofactor)
|
|
2536
|
+
return clearCofactor(Point, this);
|
|
2537
|
+
return this.multiplyUnsafe(cofactor);
|
|
2538
|
+
}
|
|
2539
|
+
isSmallOrder() {
|
|
2540
|
+
return this.multiplyUnsafe(cofactor).is0();
|
|
2541
|
+
}
|
|
2542
|
+
toBytes(isCompressed = true) {
|
|
2543
|
+
abool(isCompressed, "isCompressed");
|
|
2544
|
+
this.assertValidity();
|
|
2545
|
+
return encodePoint(Point, this, isCompressed);
|
|
2546
|
+
}
|
|
2547
|
+
toHex(isCompressed = true) {
|
|
2548
|
+
return bytesToHex(this.toBytes(isCompressed));
|
|
2549
|
+
}
|
|
2550
|
+
toString() {
|
|
2551
|
+
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
840
2552
|
}
|
|
841
|
-
const seenAt = Number.isFinite(kept[index].requestedAt) ? kept[index].requestedAt : 0;
|
|
842
|
-
const entryAt = Number.isFinite(entry.requestedAt) ? entry.requestedAt : 0;
|
|
843
|
-
if (entryAt > seenAt) kept[index] = entry;
|
|
844
|
-
changed = true;
|
|
845
|
-
}
|
|
846
|
-
return { entries: kept, changed };
|
|
847
|
-
}
|
|
848
|
-
function sameJournalEntry(entry, target) {
|
|
849
|
-
return pendingKey(entry) === pendingKey(target) && entry.fundId === target.fundId && x402PendingRetired(entry) === x402PendingRetired(target);
|
|
850
|
-
}
|
|
851
|
-
function fromSynchronousStore(operation) {
|
|
852
|
-
try {
|
|
853
|
-
return Promise.resolve(operation());
|
|
854
|
-
} catch (error) {
|
|
855
|
-
return Promise.reject(error instanceof Error ? error : new Error(String(error)));
|
|
856
2553
|
}
|
|
2554
|
+
const bits = Fn.BITS;
|
|
2555
|
+
const wnaf = new wNAF(Point, extraOpts.endo ? Math.ceil(bits / 2) : bits);
|
|
2556
|
+
Point.BASE.precompute(8);
|
|
2557
|
+
return Point;
|
|
857
2558
|
}
|
|
858
|
-
function
|
|
859
|
-
|
|
860
|
-
if (next === void 0) return previous;
|
|
861
|
-
return BigInt(next) > BigInt(previous) ? next : previous;
|
|
862
|
-
}
|
|
863
|
-
function readAcknowledgedCumulativeAmount(record) {
|
|
864
|
-
return record?.acknowledgedCumulativeAmount ?? record?.context?.chargedCumulativeAmount;
|
|
2559
|
+
function pprefix(hasEvenY) {
|
|
2560
|
+
return Uint8Array.of(hasEvenY ? 2 : 3);
|
|
865
2561
|
}
|
|
866
|
-
function
|
|
867
|
-
return
|
|
2562
|
+
function getWLengths(Fp, Fn) {
|
|
2563
|
+
return {
|
|
2564
|
+
secretKey: Fn.BYTES,
|
|
2565
|
+
publicKey: 1 + Fp.BYTES,
|
|
2566
|
+
publicKeyUncompressed: 1 + 2 * Fp.BYTES,
|
|
2567
|
+
publicKeyHasPrefix: true,
|
|
2568
|
+
signature: 2 * Fn.BYTES
|
|
2569
|
+
};
|
|
868
2570
|
}
|
|
869
|
-
function
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
2571
|
+
function ecdh(Point, ecdhOpts = {}) {
|
|
2572
|
+
const { Fn } = Point;
|
|
2573
|
+
const randomBytes_ = ecdhOpts.randomBytes || randomBytes;
|
|
2574
|
+
const lengths = Object.assign(getWLengths(Point.Fp, Fn), { seed: getMinHashLength(Fn.ORDER) });
|
|
2575
|
+
function isValidSecretKey(secretKey) {
|
|
2576
|
+
try {
|
|
2577
|
+
const num2 = Fn.fromBytes(secretKey);
|
|
2578
|
+
return Fn.isValidNot0(num2);
|
|
2579
|
+
} catch (error) {
|
|
2580
|
+
return false;
|
|
2581
|
+
}
|
|
874
2582
|
}
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
2583
|
+
function isValidPublicKey(publicKey, isCompressed) {
|
|
2584
|
+
const { publicKey: comp, publicKeyUncompressed } = lengths;
|
|
2585
|
+
try {
|
|
2586
|
+
const l = publicKey.length;
|
|
2587
|
+
if (isCompressed === true && l !== comp)
|
|
2588
|
+
return false;
|
|
2589
|
+
if (isCompressed === false && l !== publicKeyUncompressed)
|
|
2590
|
+
return false;
|
|
2591
|
+
return !!Point.fromBytes(publicKey);
|
|
2592
|
+
} catch (error) {
|
|
2593
|
+
return false;
|
|
2594
|
+
}
|
|
885
2595
|
}
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
2596
|
+
function randomSecretKey(seed = randomBytes_(lengths.seed)) {
|
|
2597
|
+
return mapHashToField(abytes(seed, lengths.seed, "seed"), Fn.ORDER);
|
|
2598
|
+
}
|
|
2599
|
+
function getPublicKey(secretKey, isCompressed = true) {
|
|
2600
|
+
return Point.BASE.multiply(Fn.fromBytes(secretKey)).toBytes(isCompressed);
|
|
2601
|
+
}
|
|
2602
|
+
function isProbPub(item) {
|
|
2603
|
+
const { secretKey, publicKey, publicKeyUncompressed } = lengths;
|
|
2604
|
+
if (!isBytes(item))
|
|
2605
|
+
return void 0;
|
|
2606
|
+
if ("_lengths" in Fn && Fn._lengths || secretKey === publicKey)
|
|
2607
|
+
return void 0;
|
|
2608
|
+
const l = abytes(item, void 0, "key").length;
|
|
2609
|
+
return l === publicKey || l === publicKeyUncompressed;
|
|
2610
|
+
}
|
|
2611
|
+
function getSharedSecret(secretKeyA, publicKeyB, isCompressed = true) {
|
|
2612
|
+
if (isProbPub(secretKeyA) === true)
|
|
2613
|
+
throw new Error("first arg must be private key");
|
|
2614
|
+
if (isProbPub(publicKeyB) === false)
|
|
2615
|
+
throw new Error("second arg must be public key");
|
|
2616
|
+
const s = Fn.fromBytes(secretKeyA);
|
|
2617
|
+
const b = Point.fromBytes(publicKeyB);
|
|
2618
|
+
return b.multiply(s).toBytes(isCompressed);
|
|
2619
|
+
}
|
|
2620
|
+
const utils = {
|
|
2621
|
+
isValidSecretKey,
|
|
2622
|
+
isValidPublicKey,
|
|
2623
|
+
randomSecretKey
|
|
2624
|
+
};
|
|
2625
|
+
const keygen = createKeygen(randomSecretKey, getPublicKey);
|
|
2626
|
+
return Object.freeze({ getPublicKey, getSharedSecret, keygen, Point, utils, lengths });
|
|
2627
|
+
}
|
|
2628
|
+
function ecdsa(Point, hash, ecdsaOpts = {}) {
|
|
2629
|
+
ahash(hash);
|
|
2630
|
+
validateObject(ecdsaOpts, {}, {
|
|
2631
|
+
hmac: "function",
|
|
2632
|
+
lowS: "boolean",
|
|
2633
|
+
randomBytes: "function",
|
|
2634
|
+
bits2int: "function",
|
|
2635
|
+
bits2int_modN: "function"
|
|
2636
|
+
});
|
|
2637
|
+
ecdsaOpts = Object.assign({}, ecdsaOpts);
|
|
2638
|
+
const randomBytes3 = ecdsaOpts.randomBytes || randomBytes;
|
|
2639
|
+
const hmac2 = ecdsaOpts.hmac || ((key, msg) => hmac(hash, key, msg));
|
|
2640
|
+
const { Fp, Fn } = Point;
|
|
2641
|
+
const { ORDER: CURVE_ORDER, BITS: fnBits } = Fn;
|
|
2642
|
+
const { keygen, getPublicKey, getSharedSecret, utils, lengths } = ecdh(Point, ecdsaOpts);
|
|
2643
|
+
const defaultSigOpts = {
|
|
2644
|
+
prehash: true,
|
|
2645
|
+
lowS: typeof ecdsaOpts.lowS === "boolean" ? ecdsaOpts.lowS : true,
|
|
2646
|
+
format: "compact",
|
|
2647
|
+
extraEntropy: false
|
|
2648
|
+
};
|
|
2649
|
+
const hasLargeCofactor = CURVE_ORDER * _2n2 < Fp.ORDER;
|
|
2650
|
+
function isBiggerThanHalfOrder(number) {
|
|
2651
|
+
const HALF = CURVE_ORDER >> _1n4;
|
|
2652
|
+
return number > HALF;
|
|
2653
|
+
}
|
|
2654
|
+
function validateRS(title, num2) {
|
|
2655
|
+
if (!Fn.isValidNot0(num2))
|
|
2656
|
+
throw new Error(`invalid signature ${title}: out of range 1..Point.Fn.ORDER`);
|
|
2657
|
+
return num2;
|
|
2658
|
+
}
|
|
2659
|
+
function assertSmallCofactor() {
|
|
2660
|
+
if (hasLargeCofactor)
|
|
2661
|
+
throw new Error('"recovered" sig type is not supported for cofactor >2 curves');
|
|
2662
|
+
}
|
|
2663
|
+
function validateSigLength(bytes, format) {
|
|
2664
|
+
validateSigFormat(format);
|
|
2665
|
+
const size = lengths.signature;
|
|
2666
|
+
const sizer = format === "compact" ? size : format === "recovered" ? size + 1 : void 0;
|
|
2667
|
+
return abytes(bytes, sizer);
|
|
2668
|
+
}
|
|
2669
|
+
class Signature {
|
|
2670
|
+
r;
|
|
2671
|
+
s;
|
|
2672
|
+
recovery;
|
|
2673
|
+
constructor(r, s, recovery) {
|
|
2674
|
+
this.r = validateRS("r", r);
|
|
2675
|
+
this.s = validateRS("s", s);
|
|
2676
|
+
if (recovery != null) {
|
|
2677
|
+
assertSmallCofactor();
|
|
2678
|
+
if (![0, 1, 2, 3].includes(recovery))
|
|
2679
|
+
throw new Error("invalid recovery id");
|
|
2680
|
+
this.recovery = recovery;
|
|
2681
|
+
}
|
|
2682
|
+
Object.freeze(this);
|
|
2683
|
+
}
|
|
2684
|
+
static fromBytes(bytes, format = defaultSigOpts.format) {
|
|
2685
|
+
validateSigLength(bytes, format);
|
|
2686
|
+
let recid;
|
|
2687
|
+
if (format === "der") {
|
|
2688
|
+
const { r: r2, s: s2 } = DER.toSig(abytes(bytes));
|
|
2689
|
+
return new Signature(r2, s2);
|
|
2690
|
+
}
|
|
2691
|
+
if (format === "recovered") {
|
|
2692
|
+
recid = bytes[0];
|
|
2693
|
+
format = "compact";
|
|
2694
|
+
bytes = bytes.subarray(1);
|
|
2695
|
+
}
|
|
2696
|
+
const L = lengths.signature / 2;
|
|
2697
|
+
const r = bytes.subarray(0, L);
|
|
2698
|
+
const s = bytes.subarray(L, L * 2);
|
|
2699
|
+
return new Signature(Fn.fromBytes(r), Fn.fromBytes(s), recid);
|
|
2700
|
+
}
|
|
2701
|
+
static fromHex(hex, format) {
|
|
2702
|
+
return this.fromBytes(hexToBytes(hex), format);
|
|
2703
|
+
}
|
|
2704
|
+
assertRecovery() {
|
|
2705
|
+
const { recovery } = this;
|
|
2706
|
+
if (recovery == null)
|
|
2707
|
+
throw new Error("invalid recovery id: must be present");
|
|
2708
|
+
return recovery;
|
|
2709
|
+
}
|
|
2710
|
+
addRecoveryBit(recovery) {
|
|
2711
|
+
return new Signature(this.r, this.s, recovery);
|
|
2712
|
+
}
|
|
2713
|
+
recoverPublicKey(messageHash) {
|
|
2714
|
+
const { r, s } = this;
|
|
2715
|
+
const recovery = this.assertRecovery();
|
|
2716
|
+
const radj = recovery === 2 || recovery === 3 ? r + CURVE_ORDER : r;
|
|
2717
|
+
if (!Fp.isValid(radj))
|
|
2718
|
+
throw new Error("invalid recovery id: sig.r+curve.n != R.x");
|
|
2719
|
+
const x = Fp.toBytes(radj);
|
|
2720
|
+
const R = Point.fromBytes(concatBytes(pprefix((recovery & 1) === 0), x));
|
|
2721
|
+
const ir = Fn.inv(radj);
|
|
2722
|
+
const h = bits2int_modN(abytes(messageHash, void 0, "msgHash"));
|
|
2723
|
+
const u1 = Fn.create(-h * ir);
|
|
2724
|
+
const u2 = Fn.create(s * ir);
|
|
2725
|
+
const Q = Point.BASE.multiplyUnsafe(u1).add(R.multiplyUnsafe(u2));
|
|
2726
|
+
if (Q.is0())
|
|
2727
|
+
throw new Error("invalid recovery: point at infinify");
|
|
2728
|
+
Q.assertValidity();
|
|
2729
|
+
return Q;
|
|
2730
|
+
}
|
|
2731
|
+
// Signatures should be low-s, to prevent malleability.
|
|
2732
|
+
hasHighS() {
|
|
2733
|
+
return isBiggerThanHalfOrder(this.s);
|
|
2734
|
+
}
|
|
2735
|
+
toBytes(format = defaultSigOpts.format) {
|
|
2736
|
+
validateSigFormat(format);
|
|
2737
|
+
if (format === "der")
|
|
2738
|
+
return hexToBytes(DER.hexFromSig(this));
|
|
2739
|
+
const { r, s } = this;
|
|
2740
|
+
const rb = Fn.toBytes(r);
|
|
2741
|
+
const sb = Fn.toBytes(s);
|
|
2742
|
+
if (format === "recovered") {
|
|
2743
|
+
assertSmallCofactor();
|
|
2744
|
+
return concatBytes(Uint8Array.of(this.assertRecovery()), rb, sb);
|
|
2745
|
+
}
|
|
2746
|
+
return concatBytes(rb, sb);
|
|
2747
|
+
}
|
|
2748
|
+
toHex(format) {
|
|
2749
|
+
return bytesToHex(this.toBytes(format));
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
const bits2int = ecdsaOpts.bits2int || function bits2int_def(bytes) {
|
|
2753
|
+
if (bytes.length > 8192)
|
|
2754
|
+
throw new Error("input is too large");
|
|
2755
|
+
const num2 = bytesToNumberBE(bytes);
|
|
2756
|
+
const delta = bytes.length * 8 - fnBits;
|
|
2757
|
+
return delta > 0 ? num2 >> BigInt(delta) : num2;
|
|
2758
|
+
};
|
|
2759
|
+
const bits2int_modN = ecdsaOpts.bits2int_modN || function bits2int_modN_def(bytes) {
|
|
2760
|
+
return Fn.create(bits2int(bytes));
|
|
2761
|
+
};
|
|
2762
|
+
const ORDER_MASK = bitMask(fnBits);
|
|
2763
|
+
function int2octets(num2) {
|
|
2764
|
+
aInRange("num < 2^" + fnBits, num2, _0n4, ORDER_MASK);
|
|
2765
|
+
return Fn.toBytes(num2);
|
|
2766
|
+
}
|
|
2767
|
+
function validateMsgAndHash(message2, prehash) {
|
|
2768
|
+
abytes(message2, void 0, "message");
|
|
2769
|
+
return prehash ? abytes(hash(message2), void 0, "prehashed message") : message2;
|
|
2770
|
+
}
|
|
2771
|
+
function prepSig(message2, secretKey, opts) {
|
|
2772
|
+
const { lowS, prehash, extraEntropy } = validateSigOpts(opts, defaultSigOpts);
|
|
2773
|
+
message2 = validateMsgAndHash(message2, prehash);
|
|
2774
|
+
const h1int = bits2int_modN(message2);
|
|
2775
|
+
const d = Fn.fromBytes(secretKey);
|
|
2776
|
+
if (!Fn.isValidNot0(d))
|
|
2777
|
+
throw new Error("invalid private key");
|
|
2778
|
+
const seedArgs = [int2octets(d), int2octets(h1int)];
|
|
2779
|
+
if (extraEntropy != null && extraEntropy !== false) {
|
|
2780
|
+
const e = extraEntropy === true ? randomBytes3(lengths.secretKey) : extraEntropy;
|
|
2781
|
+
seedArgs.push(abytes(e, void 0, "extraEntropy"));
|
|
2782
|
+
}
|
|
2783
|
+
const seed = concatBytes(...seedArgs);
|
|
2784
|
+
const m = h1int;
|
|
2785
|
+
function k2sig(kBytes) {
|
|
2786
|
+
const k = bits2int(kBytes);
|
|
2787
|
+
if (!Fn.isValidNot0(k))
|
|
2788
|
+
return;
|
|
2789
|
+
const ik = Fn.inv(k);
|
|
2790
|
+
const q = Point.BASE.multiply(k).toAffine();
|
|
2791
|
+
const r = Fn.create(q.x);
|
|
2792
|
+
if (r === _0n4)
|
|
2793
|
+
return;
|
|
2794
|
+
const s = Fn.create(ik * Fn.create(m + r * d));
|
|
2795
|
+
if (s === _0n4)
|
|
2796
|
+
return;
|
|
2797
|
+
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n4);
|
|
2798
|
+
let normS = s;
|
|
2799
|
+
if (lowS && isBiggerThanHalfOrder(s)) {
|
|
2800
|
+
normS = Fn.neg(s);
|
|
2801
|
+
recovery ^= 1;
|
|
2802
|
+
}
|
|
2803
|
+
return new Signature(r, normS, hasLargeCofactor ? void 0 : recovery);
|
|
2804
|
+
}
|
|
2805
|
+
return { seed, k2sig };
|
|
2806
|
+
}
|
|
2807
|
+
function sign2(message2, secretKey, opts = {}) {
|
|
2808
|
+
const { seed, k2sig } = prepSig(message2, secretKey, opts);
|
|
2809
|
+
const drbg = createHmacDrbg(hash.outputLen, Fn.BYTES, hmac2);
|
|
2810
|
+
const sig = drbg(seed, k2sig);
|
|
2811
|
+
return sig.toBytes(opts.format);
|
|
2812
|
+
}
|
|
2813
|
+
function verify(signature, message2, publicKey, opts = {}) {
|
|
2814
|
+
const { lowS, prehash, format } = validateSigOpts(opts, defaultSigOpts);
|
|
2815
|
+
publicKey = abytes(publicKey, void 0, "publicKey");
|
|
2816
|
+
message2 = validateMsgAndHash(message2, prehash);
|
|
2817
|
+
if (!isBytes(signature)) {
|
|
2818
|
+
const end = signature instanceof Signature ? ", use sig.toBytes()" : "";
|
|
2819
|
+
throw new Error("verify expects Uint8Array signature" + end);
|
|
2820
|
+
}
|
|
2821
|
+
validateSigLength(signature, format);
|
|
2822
|
+
try {
|
|
2823
|
+
const sig = Signature.fromBytes(signature, format);
|
|
2824
|
+
const P = Point.fromBytes(publicKey);
|
|
2825
|
+
if (lowS && sig.hasHighS())
|
|
2826
|
+
return false;
|
|
2827
|
+
const { r, s } = sig;
|
|
2828
|
+
const h = bits2int_modN(message2);
|
|
2829
|
+
const is = Fn.inv(s);
|
|
2830
|
+
const u1 = Fn.create(h * is);
|
|
2831
|
+
const u2 = Fn.create(r * is);
|
|
2832
|
+
const R = Point.BASE.multiplyUnsafe(u1).add(P.multiplyUnsafe(u2));
|
|
2833
|
+
if (R.is0())
|
|
2834
|
+
return false;
|
|
2835
|
+
const v = Fn.create(R.x);
|
|
2836
|
+
return v === r;
|
|
2837
|
+
} catch (e) {
|
|
2838
|
+
return false;
|
|
2839
|
+
}
|
|
892
2840
|
}
|
|
2841
|
+
function recoverPublicKey(signature, message2, opts = {}) {
|
|
2842
|
+
const { prehash } = validateSigOpts(opts, defaultSigOpts);
|
|
2843
|
+
message2 = validateMsgAndHash(message2, prehash);
|
|
2844
|
+
return Signature.fromBytes(signature, "recovered").recoverPublicKey(message2).toBytes();
|
|
2845
|
+
}
|
|
2846
|
+
return Object.freeze({
|
|
2847
|
+
keygen,
|
|
2848
|
+
getPublicKey,
|
|
2849
|
+
getSharedSecret,
|
|
2850
|
+
utils,
|
|
2851
|
+
lengths,
|
|
2852
|
+
Point,
|
|
2853
|
+
sign: sign2,
|
|
2854
|
+
verify,
|
|
2855
|
+
recoverPublicKey,
|
|
2856
|
+
Signature,
|
|
2857
|
+
hash
|
|
2858
|
+
});
|
|
893
2859
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
2860
|
+
|
|
2861
|
+
// node_modules/@noble/curves/secp256k1.js
|
|
2862
|
+
var secp256k1_CURVE = {
|
|
2863
|
+
p: BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),
|
|
2864
|
+
n: BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),
|
|
2865
|
+
h: BigInt(1),
|
|
2866
|
+
a: BigInt(0),
|
|
2867
|
+
b: BigInt(7),
|
|
2868
|
+
Gx: BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),
|
|
2869
|
+
Gy: BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")
|
|
2870
|
+
};
|
|
2871
|
+
var secp256k1_ENDO = {
|
|
2872
|
+
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
|
2873
|
+
basises: [
|
|
2874
|
+
[BigInt("0x3086d221a7d46bcde86c90e49284eb15"), -BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],
|
|
2875
|
+
[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"), BigInt("0x3086d221a7d46bcde86c90e49284eb15")]
|
|
2876
|
+
]
|
|
2877
|
+
};
|
|
2878
|
+
var _0n5 = /* @__PURE__ */ BigInt(0);
|
|
2879
|
+
var _2n3 = /* @__PURE__ */ BigInt(2);
|
|
2880
|
+
function sqrtMod(y) {
|
|
2881
|
+
const P = secp256k1_CURVE.p;
|
|
2882
|
+
const _3n3 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
2883
|
+
const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
|
2884
|
+
const b2 = y * y * y % P;
|
|
2885
|
+
const b3 = b2 * b2 * y % P;
|
|
2886
|
+
const b6 = pow2(b3, _3n3, P) * b3 % P;
|
|
2887
|
+
const b9 = pow2(b6, _3n3, P) * b3 % P;
|
|
2888
|
+
const b11 = pow2(b9, _2n3, P) * b2 % P;
|
|
2889
|
+
const b22 = pow2(b11, _11n, P) * b11 % P;
|
|
2890
|
+
const b44 = pow2(b22, _22n, P) * b22 % P;
|
|
2891
|
+
const b88 = pow2(b44, _44n, P) * b44 % P;
|
|
2892
|
+
const b176 = pow2(b88, _88n, P) * b88 % P;
|
|
2893
|
+
const b220 = pow2(b176, _44n, P) * b44 % P;
|
|
2894
|
+
const b223 = pow2(b220, _3n3, P) * b3 % P;
|
|
2895
|
+
const t1 = pow2(b223, _23n, P) * b22 % P;
|
|
2896
|
+
const t2 = pow2(t1, _6n, P) * b2 % P;
|
|
2897
|
+
const root = pow2(t2, _2n3, P);
|
|
2898
|
+
if (!Fpk1.eql(Fpk1.sqr(root), y))
|
|
2899
|
+
throw new Error("Cannot find square root");
|
|
2900
|
+
return root;
|
|
2901
|
+
}
|
|
2902
|
+
var Fpk1 = Field(secp256k1_CURVE.p, { sqrt: sqrtMod });
|
|
2903
|
+
var Pointk1 = /* @__PURE__ */ weierstrass(secp256k1_CURVE, {
|
|
2904
|
+
Fp: Fpk1,
|
|
2905
|
+
endo: secp256k1_ENDO
|
|
2906
|
+
});
|
|
2907
|
+
var secp256k1 = /* @__PURE__ */ ecdsa(Pointk1, sha256);
|
|
2908
|
+
var TAGGED_HASH_PREFIXES = {};
|
|
2909
|
+
function taggedHash(tag2, ...messages) {
|
|
2910
|
+
let tagP = TAGGED_HASH_PREFIXES[tag2];
|
|
2911
|
+
if (tagP === void 0) {
|
|
2912
|
+
const tagH = sha256(asciiToBytes(tag2));
|
|
2913
|
+
tagP = concatBytes(tagH, tagH);
|
|
2914
|
+
TAGGED_HASH_PREFIXES[tag2] = tagP;
|
|
2915
|
+
}
|
|
2916
|
+
return sha256(concatBytes(tagP, ...messages));
|
|
2917
|
+
}
|
|
2918
|
+
var pointToBytes = (point) => point.toBytes(true).slice(1);
|
|
2919
|
+
var hasEven = (y) => y % _2n3 === _0n5;
|
|
2920
|
+
function schnorrGetExtPubKey(priv) {
|
|
2921
|
+
const { Fn, BASE } = Pointk1;
|
|
2922
|
+
const d_ = Fn.fromBytes(priv);
|
|
2923
|
+
const p = BASE.multiply(d_);
|
|
2924
|
+
const scalar = hasEven(p.y) ? d_ : Fn.neg(d_);
|
|
2925
|
+
return { scalar, bytes: pointToBytes(p) };
|
|
2926
|
+
}
|
|
2927
|
+
function lift_x(x) {
|
|
2928
|
+
const Fp = Fpk1;
|
|
2929
|
+
if (!Fp.isValidNot0(x))
|
|
2930
|
+
throw new Error("invalid x: Fail if x \u2265 p");
|
|
2931
|
+
const xx = Fp.create(x * x);
|
|
2932
|
+
const c = Fp.create(xx * x + BigInt(7));
|
|
2933
|
+
let y = Fp.sqrt(c);
|
|
2934
|
+
if (!hasEven(y))
|
|
2935
|
+
y = Fp.neg(y);
|
|
2936
|
+
const p = Pointk1.fromAffine({ x, y });
|
|
2937
|
+
p.assertValidity();
|
|
2938
|
+
return p;
|
|
2939
|
+
}
|
|
2940
|
+
var num = bytesToNumberBE;
|
|
2941
|
+
function challenge(...args) {
|
|
2942
|
+
return Pointk1.Fn.create(num(taggedHash("BIP0340/challenge", ...args)));
|
|
2943
|
+
}
|
|
2944
|
+
function schnorrGetPublicKey(secretKey) {
|
|
2945
|
+
return schnorrGetExtPubKey(secretKey).bytes;
|
|
2946
|
+
}
|
|
2947
|
+
function schnorrSign(message2, secretKey, auxRand = randomBytes(32)) {
|
|
2948
|
+
const { Fn } = Pointk1;
|
|
2949
|
+
const m = abytes(message2, void 0, "message");
|
|
2950
|
+
const { bytes: px, scalar: d } = schnorrGetExtPubKey(secretKey);
|
|
2951
|
+
const a = abytes(auxRand, 32, "auxRand");
|
|
2952
|
+
const t = Fn.toBytes(d ^ num(taggedHash("BIP0340/aux", a)));
|
|
2953
|
+
const rand = taggedHash("BIP0340/nonce", t, px, m);
|
|
2954
|
+
const { bytes: rx, scalar: k } = schnorrGetExtPubKey(rand);
|
|
2955
|
+
const e = challenge(rx, px, m);
|
|
2956
|
+
const sig = new Uint8Array(64);
|
|
2957
|
+
sig.set(rx, 0);
|
|
2958
|
+
sig.set(Fn.toBytes(Fn.create(k + e * d)), 32);
|
|
2959
|
+
if (!schnorrVerify(sig, m, px))
|
|
2960
|
+
throw new Error("sign: Invalid signature produced");
|
|
2961
|
+
return sig;
|
|
2962
|
+
}
|
|
2963
|
+
function schnorrVerify(signature, message2, publicKey) {
|
|
2964
|
+
const { Fp, Fn, BASE } = Pointk1;
|
|
2965
|
+
const sig = abytes(signature, 64, "signature");
|
|
2966
|
+
const m = abytes(message2, void 0, "message");
|
|
2967
|
+
const pub = abytes(publicKey, 32, "publicKey");
|
|
897
2968
|
try {
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
2969
|
+
const P = lift_x(num(pub));
|
|
2970
|
+
const r = num(sig.subarray(0, 32));
|
|
2971
|
+
if (!Fp.isValidNot0(r))
|
|
2972
|
+
return false;
|
|
2973
|
+
const s = num(sig.subarray(32, 64));
|
|
2974
|
+
if (!Fn.isValidNot0(s))
|
|
2975
|
+
return false;
|
|
2976
|
+
const e = challenge(Fn.toBytes(r), pointToBytes(P), m);
|
|
2977
|
+
const R = BASE.multiplyUnsafe(s).add(P.multiplyUnsafe(Fn.neg(e)));
|
|
2978
|
+
const { x, y } = R.toAffine();
|
|
2979
|
+
if (R.is0() || !hasEven(y) || x !== r)
|
|
2980
|
+
return false;
|
|
2981
|
+
return true;
|
|
2982
|
+
} catch (error) {
|
|
2983
|
+
return false;
|
|
904
2984
|
}
|
|
905
2985
|
}
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
2986
|
+
var schnorr = /* @__PURE__ */ (() => {
|
|
2987
|
+
const size = 32;
|
|
2988
|
+
const seedLength = 48;
|
|
2989
|
+
const randomSecretKey = (seed = randomBytes(seedLength)) => {
|
|
2990
|
+
return mapHashToField(seed, secp256k1_CURVE.n);
|
|
2991
|
+
};
|
|
2992
|
+
return {
|
|
2993
|
+
keygen: createKeygen(randomSecretKey, schnorrGetPublicKey),
|
|
2994
|
+
getPublicKey: schnorrGetPublicKey,
|
|
2995
|
+
sign: schnorrSign,
|
|
2996
|
+
verify: schnorrVerify,
|
|
2997
|
+
Point: Pointk1,
|
|
2998
|
+
utils: {
|
|
2999
|
+
randomSecretKey,
|
|
3000
|
+
taggedHash,
|
|
3001
|
+
lift_x,
|
|
3002
|
+
pointToBytes
|
|
3003
|
+
},
|
|
3004
|
+
lengths: {
|
|
3005
|
+
secretKey: size,
|
|
3006
|
+
publicKey: size,
|
|
3007
|
+
publicKeyHasPrefix: false,
|
|
3008
|
+
signature: size * 2,
|
|
3009
|
+
seed: seedLength
|
|
912
3010
|
}
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
exact: Array.isArray(raw2.exact) ? raw2.exact : []
|
|
921
|
-
};
|
|
922
|
-
} catch (cause) {
|
|
923
|
-
throw new DvmError(
|
|
924
|
-
"x402_channel_store_invalid",
|
|
925
|
-
"The saved x402 channel journal cannot be read safely.",
|
|
926
|
-
`Do not delete ${X402_CHANNELS_FILE}; restore or repair it before signing another channel voucher.`,
|
|
927
|
-
{ cause: cause instanceof Error ? cause.message : String(cause) }
|
|
928
|
-
);
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
function saveFile(file) {
|
|
932
|
-
ensureConfigDir();
|
|
933
|
-
const tmp = `${X402_CHANNELS_FILE}.${String(process.pid)}.tmp`;
|
|
934
|
-
writeFileSync(tmp, `${JSON.stringify(file, null, 2)}
|
|
935
|
-
`, { mode: 384 });
|
|
936
|
-
renameSync(tmp, X402_CHANNELS_FILE);
|
|
937
|
-
}
|
|
938
|
-
function loadX402Wallet() {
|
|
939
|
-
const config = loadConfig();
|
|
940
|
-
const x402 = config?.x402;
|
|
941
|
-
const privateKey = x402?.privateKey;
|
|
942
|
-
if (!privateKey || !isValidEvmPrivateKey(privateKey)) return null;
|
|
3011
|
+
};
|
|
3012
|
+
})();
|
|
3013
|
+
|
|
3014
|
+
// node_modules/@dvmkit/sdk/dist/chunk-OFSOLHRX.js
|
|
3015
|
+
function resolveX402Asset(network = X402_DEFAULT_NETWORK) {
|
|
3016
|
+
const spec = x402NetworkByCaip2(network);
|
|
3017
|
+
if (!spec) return null;
|
|
943
3018
|
return {
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
3019
|
+
address: spec.usdc,
|
|
3020
|
+
symbol: "USDC",
|
|
3021
|
+
// "native" is the load-bearing word: every contract in the table is
|
|
3022
|
+
// Circle's own USDC, and the dollars that read as zero here are the
|
|
3023
|
+
// bridged ones.
|
|
3024
|
+
label: "native USDC",
|
|
3025
|
+
unit: "USDC",
|
|
3026
|
+
networkLabel: spec.label,
|
|
3027
|
+
confusable: spec.confusable
|
|
947
3028
|
};
|
|
948
3029
|
}
|
|
3030
|
+
var EVM_PRIVATE_KEY_RE = /^0x[0-9a-fA-F]{64}$/;
|
|
3031
|
+
function isValidEvmPrivateKey(value) {
|
|
3032
|
+
return EVM_PRIVATE_KEY_RE.test(value) && secp256k1.utils.isValidSecretKey(hexToBytes(value.slice(2)));
|
|
3033
|
+
}
|
|
949
3034
|
async function ensureAddress(wallet) {
|
|
950
3035
|
if (!isValidEvmPrivateKey(wallet.privateKey)) throw invalidX402PrivateKey();
|
|
951
3036
|
if (wallet.address) return wallet;
|
|
@@ -953,63 +3038,10 @@ async function ensureAddress(wallet) {
|
|
|
953
3038
|
const account = privateKeyToAccount(wallet.privateKey);
|
|
954
3039
|
return { ...wallet, address: account.address };
|
|
955
3040
|
}
|
|
956
|
-
async function connectedX402Payer() {
|
|
957
|
-
const wallet = loadX402Wallet();
|
|
958
|
-
if (!wallet) return null;
|
|
959
|
-
return (await ensureAddress(wallet)).address;
|
|
960
|
-
}
|
|
961
3041
|
async function generateX402PrivateKey() {
|
|
962
3042
|
const { generatePrivateKey } = await import("./accounts-2WZE4N5F.js");
|
|
963
3043
|
return generatePrivateKey();
|
|
964
3044
|
}
|
|
965
|
-
async function repinX402WalletNetwork(network) {
|
|
966
|
-
assertSupportedX402Network(network);
|
|
967
|
-
return withX402ChannelLock(async () => {
|
|
968
|
-
const { previous } = await updateConfig({
|
|
969
|
-
reason: "x402_wallet_repin",
|
|
970
|
-
patch: (current) => {
|
|
971
|
-
if (!current) throw new DvmError("no_config", "No config found.", "Run: dvm init");
|
|
972
|
-
const privateKey = current.x402?.privateKey;
|
|
973
|
-
if (privateKey === void 0) throw x402WalletMissingError();
|
|
974
|
-
if (!isValidEvmPrivateKey(privateKey)) throw invalidX402PrivateKey();
|
|
975
|
-
return { set: { x402: { privateKey, network } } };
|
|
976
|
-
}
|
|
977
|
-
});
|
|
978
|
-
const stored = previous?.x402;
|
|
979
|
-
if (stored === void 0) throw x402WalletMissingError();
|
|
980
|
-
if (!isValidEvmPrivateKey(stored.privateKey)) throw invalidX402PrivateKey();
|
|
981
|
-
const { privateKeyToAccount } = await import("./accounts-2WZE4N5F.js");
|
|
982
|
-
return {
|
|
983
|
-
address: privateKeyToAccount(stored.privateKey).address,
|
|
984
|
-
previousNetwork: stored.network ?? X402_DEFAULT_NETWORK,
|
|
985
|
-
network
|
|
986
|
-
};
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
async function saveX402Wallet(privateKey, network) {
|
|
990
|
-
if (network !== void 0) assertSupportedX402Network(network);
|
|
991
|
-
if (!isValidEvmPrivateKey(privateKey)) throw invalidX402PrivateKey();
|
|
992
|
-
const { privateKeyToAccount } = await import("./accounts-2WZE4N5F.js");
|
|
993
|
-
const account = privateKeyToAccount(privateKey);
|
|
994
|
-
let resolved;
|
|
995
|
-
await updateConfig({
|
|
996
|
-
reason: "x402_wallet_connect",
|
|
997
|
-
patch: (current) => {
|
|
998
|
-
resolved = network ?? current?.x402?.network;
|
|
999
|
-
return {
|
|
1000
|
-
set: { x402: resolved === void 0 ? { privateKey } : { privateKey, network: resolved } }
|
|
1001
|
-
};
|
|
1002
|
-
}
|
|
1003
|
-
});
|
|
1004
|
-
return { address: account.address, network: resolved ?? X402_DEFAULT_NETWORK };
|
|
1005
|
-
}
|
|
1006
|
-
async function removeX402Wallet() {
|
|
1007
|
-
await updateConfig({
|
|
1008
|
-
reason: "x402_wallet_disconnect",
|
|
1009
|
-
authorize: authorizeProtectedRemoval("x402"),
|
|
1010
|
-
patch: () => ({ unset: ["x402"] })
|
|
1011
|
-
});
|
|
1012
|
-
}
|
|
1013
3045
|
function invalidX402PrivateKey() {
|
|
1014
3046
|
return new DvmError(
|
|
1015
3047
|
"invalid_key",
|
|
@@ -1017,13 +3049,6 @@ function invalidX402PrivateKey() {
|
|
|
1017
3049
|
"Reconnect with `dvm wallet x402-connect --key-file <path>`, set DVM_X402_KEY, or pipe a valid 0x-prefixed 32-byte secp256k1 private key through stdin."
|
|
1018
3050
|
);
|
|
1019
3051
|
}
|
|
1020
|
-
function x402WalletMissingError() {
|
|
1021
|
-
return new DvmError(
|
|
1022
|
-
"x402_wallet_missing",
|
|
1023
|
-
"No x402 wallet is connected.",
|
|
1024
|
-
"Run 'dvm wallet x402-connect' to create one, or add --key-file <path> to import a key you already hold."
|
|
1025
|
-
);
|
|
1026
|
-
}
|
|
1027
3052
|
async function getX402Balance(wallet) {
|
|
1028
3053
|
const w = await ensureAddress(wallet);
|
|
1029
3054
|
const spec = x402NetworkByCaip2(w.network);
|
|
@@ -1034,8 +3059,8 @@ async function getX402Balance(wallet) {
|
|
|
1034
3059
|
`${x402SupportedNetworksHint()} Re-pin the connected wallet without handling its key: 'dvm wallet x402-network ${X402_DEFAULT_NETWORK}'.`
|
|
1035
3060
|
);
|
|
1036
3061
|
}
|
|
1037
|
-
const chains = await import("./chains-
|
|
1038
|
-
const { createPublicClient, http, erc20Abi } = await import("./_esm-
|
|
3062
|
+
const chains = await import("./chains-J6VIUNUB.js");
|
|
3063
|
+
const { createPublicClient, http, erc20Abi } = await import("./_esm-KJAA4KAJ.js");
|
|
1039
3064
|
const chain = chains[spec.viemChain];
|
|
1040
3065
|
const { url: rpcUrl, source: rpcSource } = resolveRpcOverride(
|
|
1041
3066
|
process.env.DVM_X402_RPC_URL,
|
|
@@ -1077,235 +3102,9 @@ async function getX402Balance(wallet) {
|
|
|
1077
3102
|
);
|
|
1078
3103
|
}
|
|
1079
3104
|
}
|
|
1080
|
-
async function tryX402Payment(request, wallet) {
|
|
1081
|
-
const selection = selectPaymentRequirement(request);
|
|
1082
|
-
let networkSlug;
|
|
1083
|
-
let networkCaip2;
|
|
1084
|
-
let chainId;
|
|
1085
|
-
let usdc;
|
|
1086
|
-
try {
|
|
1087
|
-
networkCaip2 = x402NetworkToCaip2(selection.network);
|
|
1088
|
-
const mapped = x402NetworkByCaip2(networkCaip2);
|
|
1089
|
-
networkSlug = mapped?.slug ?? selection.network;
|
|
1090
|
-
chainId = chainIdFromCaip2(networkCaip2);
|
|
1091
|
-
usdc = usdcContractFor(networkSlug);
|
|
1092
|
-
} catch (err) {
|
|
1093
|
-
throw new DvmError(
|
|
1094
|
-
"x402_build_failed",
|
|
1095
|
-
`Could not resolve x402 network from '${selection.network}': ${err instanceof Error ? err.message : String(err)}`,
|
|
1096
|
-
"Check that the DVM's advertised x402.network is a CAIP-2 chain id (e.g. 'eip155:8453' for Base)."
|
|
1097
|
-
);
|
|
1098
|
-
}
|
|
1099
|
-
if (!usdc) return null;
|
|
1100
|
-
const rate = await priceableRate(request.btcRate);
|
|
1101
|
-
if (rate === null) return null;
|
|
1102
|
-
const localUsdcMicro = BigInt(msatsToUsdc(request.amountMsats, rate));
|
|
1103
|
-
const requiredUsdcMicro = resolveRequiredUsdcMicro(selection.amount, localUsdcMicro);
|
|
1104
|
-
const signedAmountMsats = usdcToMsats(Number(requiredUsdcMicro) / 1e6, rate);
|
|
1105
|
-
try {
|
|
1106
|
-
const w = await ensureAddress(wallet);
|
|
1107
|
-
const validAfter = 0n;
|
|
1108
|
-
const validBefore = BigInt(
|
|
1109
|
-
Math.floor(Date.now() / 1e3) + X402_EXACT_AUTHORIZATION_TTL_SECONDS
|
|
1110
|
-
);
|
|
1111
|
-
const nonce = request.nonce ?? `0x${randomBytes(32).toString("hex")}`;
|
|
1112
|
-
const domainName = selectedDomainField(selection.accepted?.extra?.name, "name") ?? usdcDomainNameFor(networkSlug) ?? "USD Coin";
|
|
1113
|
-
const domainVersion = selectedDomainField(selection.accepted?.extra?.version, "version") ?? usdcDomainVersionFor(networkSlug) ?? "2";
|
|
1114
|
-
const { signTypedData } = await import("./accounts-2WZE4N5F.js");
|
|
1115
|
-
const signature = await signTypedData({
|
|
1116
|
-
privateKey: w.privateKey,
|
|
1117
|
-
domain: {
|
|
1118
|
-
name: domainName,
|
|
1119
|
-
version: domainVersion,
|
|
1120
|
-
chainId,
|
|
1121
|
-
verifyingContract: usdc
|
|
1122
|
-
},
|
|
1123
|
-
types: {
|
|
1124
|
-
TransferWithAuthorization: [
|
|
1125
|
-
{ name: "from", type: "address" },
|
|
1126
|
-
{ name: "to", type: "address" },
|
|
1127
|
-
{ name: "value", type: "uint256" },
|
|
1128
|
-
{ name: "validAfter", type: "uint256" },
|
|
1129
|
-
{ name: "validBefore", type: "uint256" },
|
|
1130
|
-
{ name: "nonce", type: "bytes32" }
|
|
1131
|
-
]
|
|
1132
|
-
},
|
|
1133
|
-
primaryType: "TransferWithAuthorization",
|
|
1134
|
-
message: {
|
|
1135
|
-
from: w.address,
|
|
1136
|
-
to: selection.payTo,
|
|
1137
|
-
value: requiredUsdcMicro,
|
|
1138
|
-
validAfter,
|
|
1139
|
-
validBefore,
|
|
1140
|
-
nonce
|
|
1141
|
-
}
|
|
1142
|
-
});
|
|
1143
|
-
const exactPayload = {
|
|
1144
|
-
signature,
|
|
1145
|
-
payload: {
|
|
1146
|
-
authorization: {
|
|
1147
|
-
from: w.address,
|
|
1148
|
-
to: selection.payTo,
|
|
1149
|
-
value: requiredUsdcMicro.toString(),
|
|
1150
|
-
validAfter: validAfter.toString(),
|
|
1151
|
-
validBefore: validBefore.toString(),
|
|
1152
|
-
nonce
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
};
|
|
1156
|
-
const payload = selection.version === X402_V1_VERSION ? {
|
|
1157
|
-
x402Version: X402_V1_VERSION,
|
|
1158
|
-
scheme: "exact",
|
|
1159
|
-
network: networkSlug,
|
|
1160
|
-
payload: { signature: exactPayload.signature, ...exactPayload.payload }
|
|
1161
|
-
} : {
|
|
1162
|
-
x402Version: X402_VERSION,
|
|
1163
|
-
...selection.resource ? { resource: selection.resource } : {},
|
|
1164
|
-
accepted: selection.accepted ?? buildPaymentRequirements({
|
|
1165
|
-
x402Config: {
|
|
1166
|
-
payTo: selection.payTo,
|
|
1167
|
-
network: networkCaip2,
|
|
1168
|
-
...selection.asset ? { asset: selection.asset } : {}
|
|
1169
|
-
},
|
|
1170
|
-
requiredUsdcMicro,
|
|
1171
|
-
resource: selection.resource?.url ?? "",
|
|
1172
|
-
version: X402_VERSION
|
|
1173
|
-
}),
|
|
1174
|
-
payload: { signature: exactPayload.signature, ...exactPayload.payload },
|
|
1175
|
-
extensions: selection.extensions ?? {}
|
|
1176
|
-
};
|
|
1177
|
-
return {
|
|
1178
|
-
paymentHeader: encodePayment(payload),
|
|
1179
|
-
x402Version: selection.version,
|
|
1180
|
-
amountUsdcMicro: requiredUsdcMicro.toString(),
|
|
1181
|
-
amountMsats: signedAmountMsats,
|
|
1182
|
-
btcRate: rate
|
|
1183
|
-
};
|
|
1184
|
-
} catch (err) {
|
|
1185
|
-
throw new DvmError(
|
|
1186
|
-
"x402_build_failed",
|
|
1187
|
-
`Failed to build x402 payment: ${err instanceof Error ? err.message : String(err)}`,
|
|
1188
|
-
"Check the configured x402 private key and RPC reachability for the target chain."
|
|
1189
|
-
);
|
|
1190
|
-
}
|
|
1191
|
-
}
|
|
1192
|
-
function isSignableExactRequirement(requirement) {
|
|
1193
|
-
const transferMethod = requirement.extra?.assetTransferMethod;
|
|
1194
|
-
return requirement.scheme === "exact" && (transferMethod === void 0 || transferMethod === "eip3009");
|
|
1195
|
-
}
|
|
1196
|
-
function selectPaymentRequirement(request) {
|
|
1197
|
-
const challenge = request.paymentRequired;
|
|
1198
|
-
if (challenge?.x402Version === X402_VERSION) {
|
|
1199
|
-
const accepted = challenge.accepts.find(isSignableExactRequirement);
|
|
1200
|
-
if (!accepted) {
|
|
1201
|
-
throw invalidChallenge("The DVM's v2 x402 challenge has no exact EIP-3009 requirement.");
|
|
1202
|
-
}
|
|
1203
|
-
return {
|
|
1204
|
-
version: X402_VERSION,
|
|
1205
|
-
network: accepted.network,
|
|
1206
|
-
amount: accepted.amount,
|
|
1207
|
-
payTo: accepted.payTo,
|
|
1208
|
-
asset: accepted.asset,
|
|
1209
|
-
accepted,
|
|
1210
|
-
resource: challenge.resource,
|
|
1211
|
-
extensions: challenge.extensions
|
|
1212
|
-
};
|
|
1213
|
-
}
|
|
1214
|
-
if (challenge?.x402Version === X402_V1_VERSION) {
|
|
1215
|
-
const accepted = challenge.accepts.find((candidate) => candidate.scheme === "exact");
|
|
1216
|
-
if (!accepted) throw invalidChallenge("The DVM's v1 x402 challenge has no exact requirement.");
|
|
1217
|
-
return {
|
|
1218
|
-
version: X402_V1_VERSION,
|
|
1219
|
-
network: accepted.network,
|
|
1220
|
-
amount: accepted.maxAmountRequired,
|
|
1221
|
-
payTo: accepted.payTo,
|
|
1222
|
-
asset: accepted.asset
|
|
1223
|
-
};
|
|
1224
|
-
}
|
|
1225
|
-
return {
|
|
1226
|
-
version: request.x402Version ?? X402_V1_VERSION,
|
|
1227
|
-
network: request.network,
|
|
1228
|
-
amount: request.requiredUsdcMicro,
|
|
1229
|
-
payTo: request.payTo,
|
|
1230
|
-
asset: request.asset,
|
|
1231
|
-
resource: request.resource
|
|
1232
|
-
};
|
|
1233
|
-
}
|
|
1234
|
-
function invalidChallenge(message2) {
|
|
1235
|
-
return new DvmError(
|
|
1236
|
-
"x402_build_failed",
|
|
1237
|
-
message2,
|
|
1238
|
-
"Ask the DVM operator to advertise an exact x402 payment requirement."
|
|
1239
|
-
);
|
|
1240
|
-
}
|
|
1241
|
-
function selectedDomainField(value, field) {
|
|
1242
|
-
if (value === void 0 || value === null) return void 0;
|
|
1243
|
-
if (typeof value === "string" && value.length > 0) return value;
|
|
1244
|
-
throw invalidChallenge(`The DVM advertised an invalid x402 ${field}.`);
|
|
1245
|
-
}
|
|
1246
|
-
function resolveRequiredUsdcMicro(advertised, localUsdcMicro) {
|
|
1247
|
-
if (advertised === void 0) return localUsdcMicro;
|
|
1248
|
-
if (!/^[1-9]\d*$/.test(advertised)) {
|
|
1249
|
-
throw new DvmError(
|
|
1250
|
-
"x402_amount_invalid",
|
|
1251
|
-
`The DVM advertised an invalid x402 maxAmountRequired: '${advertised}'.`,
|
|
1252
|
-
"Do not sign this payment. Ask the DVM operator to return a positive integer number of USDC micro-units."
|
|
1253
|
-
);
|
|
1254
|
-
}
|
|
1255
|
-
const required = BigInt(advertised);
|
|
1256
|
-
if (withinAdvertisedTolerance(required, localUsdcMicro)) return required;
|
|
1257
|
-
const advertisedUsd = Number(required) / 1e6;
|
|
1258
|
-
const localUsd = Number(localUsdcMicro) / 1e6;
|
|
1259
|
-
throw new DvmError(
|
|
1260
|
-
"x402_amount_exceeds_limit",
|
|
1261
|
-
`The DVM's x402 charge ($${formatUsd(advertisedUsd)} USDC) is more than 10% above the caller's local price ($${formatUsd(localUsd)} USDC).`,
|
|
1262
|
-
"The payment was not signed. Refresh the quote or ask the DVM operator to check its BTC/USD rate.",
|
|
1263
|
-
{
|
|
1264
|
-
advertised_usdc_microunits: required.toString(),
|
|
1265
|
-
local_usdc_microunits: localUsdcMicro.toString(),
|
|
1266
|
-
max_usdc_microunits: maxAdvertisedMicro(localUsdcMicro).toString(),
|
|
1267
|
-
anchor: costAnchor(advertisedUsd),
|
|
1268
|
-
display: `Payment refused: $${formatUsd(advertisedUsd)} USDC exceeds the 10% safety margin over the caller's $${formatUsd(localUsd)} USDC estimate.`
|
|
1269
|
-
}
|
|
1270
|
-
);
|
|
1271
|
-
}
|
|
1272
|
-
async function priceableRate(btcRate) {
|
|
1273
|
-
if (btcRate && btcRate > 0) return btcRate;
|
|
1274
|
-
try {
|
|
1275
|
-
return (await resolveBtcUsdRate("payment")).rate;
|
|
1276
|
-
} catch (err) {
|
|
1277
|
-
if (err instanceof DvmError && err.code === "rate_unavailable") {
|
|
1278
|
-
info(
|
|
1279
|
-
"Skipping the x402 rail: it prices in USDC and the BTC/USD rate is unavailable (no cached rate on this machine). Trying the remaining rails."
|
|
1280
|
-
);
|
|
1281
|
-
return null;
|
|
1282
|
-
}
|
|
1283
|
-
throw err;
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
3105
|
|
|
1287
|
-
// node_modules/@dvmkit/sdk/dist/chunk-
|
|
1288
|
-
|
|
1289
|
-
function initCallerLoggers(nextLoggers) {
|
|
1290
|
-
callerLoggers = nextLoggers;
|
|
1291
|
-
}
|
|
1292
|
-
function buildNoopLoggers() {
|
|
1293
|
-
const noopLogger = {
|
|
1294
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function -- standalone caller has no exporter
|
|
1295
|
-
info() {
|
|
1296
|
-
},
|
|
1297
|
-
async span(_name, _attrs, fn) {
|
|
1298
|
-
return fn();
|
|
1299
|
-
}
|
|
1300
|
-
};
|
|
1301
|
-
return {
|
|
1302
|
-
cashu: noopLogger,
|
|
1303
|
-
lightning: noopLogger,
|
|
1304
|
-
x402: noopLogger
|
|
1305
|
-
};
|
|
1306
|
-
}
|
|
1307
|
-
|
|
1308
|
-
// node_modules/@dvmkit/sdk/dist/chunk-PLG4XER6.js
|
|
3106
|
+
// node_modules/@dvmkit/sdk/dist/chunk-26W6GPCD.js
|
|
3107
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
1309
3108
|
import { createPrivateKey, randomBytes as randomBytes2 } from "crypto";
|
|
1310
3109
|
|
|
1311
3110
|
// node_modules/@x402/core/node_modules/zod/v3/external.js
|
|
@@ -1574,8 +3373,8 @@ var ZodIssueCode = util.arrayToEnum([
|
|
|
1574
3373
|
"not_finite"
|
|
1575
3374
|
]);
|
|
1576
3375
|
var quotelessJson = (obj) => {
|
|
1577
|
-
const
|
|
1578
|
-
return
|
|
3376
|
+
const json = JSON.stringify(obj, null, 2);
|
|
3377
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
1579
3378
|
};
|
|
1580
3379
|
var ZodError = class _ZodError extends Error {
|
|
1581
3380
|
get errors() {
|
|
@@ -8796,7 +10595,26 @@ var SignJWT = class {
|
|
|
8796
10595
|
}
|
|
8797
10596
|
};
|
|
8798
10597
|
|
|
8799
|
-
// node_modules/@dvmkit/sdk/dist/chunk-
|
|
10598
|
+
// node_modules/@dvmkit/sdk/dist/chunk-26W6GPCD.js
|
|
10599
|
+
var scopes = new AsyncLocalStorage();
|
|
10600
|
+
function withPaymentDiagnostics(fn) {
|
|
10601
|
+
return scopes.run({}, fn);
|
|
10602
|
+
}
|
|
10603
|
+
async function withPaymentStage(stage, fn) {
|
|
10604
|
+
try {
|
|
10605
|
+
return await fn();
|
|
10606
|
+
} catch (error) {
|
|
10607
|
+
const scope = scopes.getStore();
|
|
10608
|
+
if (scope && (!scope.failure || !Object.is(scope.failure.error, error))) {
|
|
10609
|
+
scope.failure = { error, stage };
|
|
10610
|
+
}
|
|
10611
|
+
throw error;
|
|
10612
|
+
}
|
|
10613
|
+
}
|
|
10614
|
+
function paymentFailureStage(error) {
|
|
10615
|
+
const failure = scopes.getStore()?.failure;
|
|
10616
|
+
return failure && Object.is(failure.error, error) ? failure.stage : "unknown";
|
|
10617
|
+
}
|
|
8800
10618
|
var AUTH_HEADER_TTL_MS = 6e4;
|
|
8801
10619
|
var AUTH_TOKEN_TTL_SECONDS = 120;
|
|
8802
10620
|
var FACILITATOR_PATHS = {
|
|
@@ -8868,7 +10686,7 @@ async function verifyWithFacilitator(payload, requirements, config = {}, createA
|
|
|
8868
10686
|
return callerLoggers.x402.span(
|
|
8869
10687
|
"x402.verify_with_facilitator",
|
|
8870
10688
|
{ facilitator_url: url },
|
|
8871
|
-
async () => {
|
|
10689
|
+
() => withPaymentStage("verifier", async () => {
|
|
8872
10690
|
const authHeaders = (await createAuthHeaders?.())?.verify ?? {};
|
|
8873
10691
|
const res = await fetch(url, {
|
|
8874
10692
|
method: "POST",
|
|
@@ -8882,14 +10700,17 @@ async function verifyWithFacilitator(payload, requirements, config = {}, createA
|
|
|
8882
10700
|
if (!res.ok) {
|
|
8883
10701
|
const rejection = await readFacilitatorRejection(res);
|
|
8884
10702
|
if (rejection?.isValid === false && typeof rejection.invalidReason === "string" && rejection.invalidReason.length > 0) {
|
|
10703
|
+
logRejected("verifier");
|
|
8885
10704
|
return rejection;
|
|
8886
10705
|
}
|
|
10706
|
+
logRejected("verifier");
|
|
8887
10707
|
return { isValid: false, invalidReason: `facilitator_status_${res.status}` };
|
|
8888
10708
|
}
|
|
8889
10709
|
const data = await res.json();
|
|
10710
|
+
if (!data.isValid) logRejected("verifier");
|
|
8890
10711
|
callerLoggers.x402.info("x402.verify_with_facilitator.result", { is_valid: data.isValid });
|
|
8891
10712
|
return data;
|
|
8892
|
-
}
|
|
10713
|
+
})
|
|
8893
10714
|
);
|
|
8894
10715
|
}
|
|
8895
10716
|
async function settleWithFacilitator(payload, requirements, config = {}, createAuthHeaders = createX402FacilitatorAuthHeaders(config)) {
|
|
@@ -8898,7 +10719,7 @@ async function settleWithFacilitator(payload, requirements, config = {}, createA
|
|
|
8898
10719
|
return callerLoggers.x402.span(
|
|
8899
10720
|
"x402.settle_with_facilitator",
|
|
8900
10721
|
{ facilitator_url: url },
|
|
8901
|
-
async () => {
|
|
10722
|
+
() => withPaymentStage("broadcast", async () => {
|
|
8902
10723
|
const authHeaders = (await createAuthHeaders?.())?.settle ?? {};
|
|
8903
10724
|
const res = await fetch(url, {
|
|
8904
10725
|
method: "POST",
|
|
@@ -8912,17 +10733,20 @@ async function settleWithFacilitator(payload, requirements, config = {}, createA
|
|
|
8912
10733
|
if (!res.ok) {
|
|
8913
10734
|
const rejection = await readFacilitatorRejection(res);
|
|
8914
10735
|
if (rejection?.success === false && typeof rejection.errorReason === "string" && rejection.errorReason.length > 0) {
|
|
10736
|
+
logRejected("broadcast");
|
|
8915
10737
|
return rejection;
|
|
8916
10738
|
}
|
|
10739
|
+
logRejected("broadcast");
|
|
8917
10740
|
return { success: false, errorReason: `facilitator_status_${res.status}` };
|
|
8918
10741
|
}
|
|
8919
10742
|
const data = await res.json();
|
|
10743
|
+
if (!data.success) logRejected("broadcast");
|
|
8920
10744
|
callerLoggers.x402.info("x402.settle_with_facilitator.result", {
|
|
8921
10745
|
success: data.success,
|
|
8922
10746
|
tx_hash: data.transaction
|
|
8923
10747
|
});
|
|
8924
10748
|
return data;
|
|
8925
|
-
}
|
|
10749
|
+
})
|
|
8926
10750
|
);
|
|
8927
10751
|
}
|
|
8928
10752
|
async function readFacilitatorRejection(res) {
|
|
@@ -8937,8 +10761,16 @@ async function readFacilitatorRejection(res) {
|
|
|
8937
10761
|
function isRecord2(value) {
|
|
8938
10762
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
8939
10763
|
}
|
|
10764
|
+
function logRejected(stage) {
|
|
10765
|
+
diagnosticConsole.warn({
|
|
10766
|
+
event: "x402_credential_rejected",
|
|
10767
|
+
rail: "x402",
|
|
10768
|
+
failure_category: stage === "verifier" ? "verification_failed" : "settlement_failed",
|
|
10769
|
+
failure_stage: stage
|
|
10770
|
+
});
|
|
10771
|
+
}
|
|
8940
10772
|
|
|
8941
|
-
// node_modules/@dvmkit/sdk/dist/chunk-
|
|
10773
|
+
// node_modules/@dvmkit/sdk/dist/chunk-H5FVVEEN.js
|
|
8942
10774
|
async function verifyX402Payment(paymentHeader, config, requiredUsdcMicro, resource, btcUsdRate, exactVersions = { v1: true, v2: true }) {
|
|
8943
10775
|
return callerLoggers.x402.span(
|
|
8944
10776
|
"x402.verify_payment",
|
|
@@ -8952,9 +10784,11 @@ async function verifyX402Payment(paymentHeader, config, requiredUsdcMicro, resou
|
|
|
8952
10784
|
try {
|
|
8953
10785
|
payload = decodePayment(paymentHeader);
|
|
8954
10786
|
} catch {
|
|
10787
|
+
logRejected2("malformed_credential", "verifier");
|
|
8955
10788
|
return { verified: false, amountMsats: 0, invalidReason: "decode_failed" };
|
|
8956
10789
|
}
|
|
8957
10790
|
if (payload.x402Version === 1 && !exactVersions.v1 || payload.x402Version === 2 && !exactVersions.v2) {
|
|
10791
|
+
logRejected2("unsupported_version", "verifier");
|
|
8958
10792
|
return { verified: false, amountMsats: 0, invalidReason: "unsupported_version" };
|
|
8959
10793
|
}
|
|
8960
10794
|
const requirementOpts = { x402Config: config, requiredUsdcMicro, resource };
|
|
@@ -9003,8 +10837,20 @@ async function verifyX402Payment(paymentHeader, config, requiredUsdcMicro, resou
|
|
|
9003
10837
|
}
|
|
9004
10838
|
);
|
|
9005
10839
|
}
|
|
10840
|
+
function logRejected2(category, stage) {
|
|
10841
|
+
diagnosticConsole.warn({
|
|
10842
|
+
event: "x402_credential_rejected",
|
|
10843
|
+
rail: "x402",
|
|
10844
|
+
failure_category: category,
|
|
10845
|
+
failure_stage: stage
|
|
10846
|
+
});
|
|
10847
|
+
}
|
|
9006
10848
|
|
|
9007
10849
|
export {
|
|
10850
|
+
DvmError,
|
|
10851
|
+
providerProseSources,
|
|
10852
|
+
isAbortError,
|
|
10853
|
+
transportErrorCode,
|
|
9008
10854
|
X402_NETWORKS,
|
|
9009
10855
|
x402NetworkByCaip2,
|
|
9010
10856
|
x402NetworkBySlug,
|
|
@@ -9034,57 +10880,40 @@ export {
|
|
|
9034
10880
|
decodePaymentRequiredHeader,
|
|
9035
10881
|
exactEvmAuthorization,
|
|
9036
10882
|
encodeSettleResponseHeader,
|
|
10883
|
+
msatsToUsd,
|
|
10884
|
+
usdToMsats,
|
|
10885
|
+
msatsToUsdCents,
|
|
10886
|
+
msatsToUsdc,
|
|
10887
|
+
usdcToMsats,
|
|
10888
|
+
msatsToMicro,
|
|
10889
|
+
microToSats,
|
|
10890
|
+
resolveRpcOverride,
|
|
10891
|
+
resolveRpcHttpTransportOptions,
|
|
10892
|
+
redactUrl,
|
|
10893
|
+
redactUrlsInText,
|
|
10894
|
+
callerLoggers,
|
|
10895
|
+
initCallerLoggers,
|
|
10896
|
+
anumber,
|
|
10897
|
+
abytes,
|
|
10898
|
+
ahash,
|
|
10899
|
+
clean,
|
|
10900
|
+
createView,
|
|
10901
|
+
bytesToHex,
|
|
10902
|
+
hexToBytes,
|
|
10903
|
+
kdfInputToBytes,
|
|
10904
|
+
concatBytes,
|
|
10905
|
+
checkOpts,
|
|
10906
|
+
randomBytes,
|
|
10907
|
+
sha256,
|
|
10908
|
+
sha512,
|
|
10909
|
+
hmac,
|
|
10910
|
+
secp256k1,
|
|
10911
|
+
schnorr,
|
|
9037
10912
|
resolveX402Asset,
|
|
9038
|
-
|
|
9039
|
-
setOutputTraceId,
|
|
9040
|
-
getOutputTraceId,
|
|
9041
|
-
json,
|
|
9042
|
-
info,
|
|
9043
|
-
raw,
|
|
9044
|
-
progress,
|
|
9045
|
-
resolveBtcUsdRate,
|
|
9046
|
-
tryResolveBtcUsdRate,
|
|
9047
|
-
staleRateNotice,
|
|
9048
|
-
X402_EXACT_AUTHORIZATION_TTL_SECONDS,
|
|
9049
|
-
X402_REFUSAL_TRANSACTION_DISCOVERY_WINDOW_MS,
|
|
9050
|
-
x402ExactFundingDead,
|
|
9051
|
-
recordPendingX402ExactFunding,
|
|
9052
|
-
loadResumableX402ExactFunding,
|
|
9053
|
-
listResumableX402ExactFundings,
|
|
9054
|
-
clearPendingX402ExactFunding,
|
|
9055
|
-
X402_PENDING_STALE_MS,
|
|
9056
|
-
x402PendingStale,
|
|
9057
|
-
x402PendingRetired,
|
|
9058
|
-
x402PendingBlockExpiresAt,
|
|
9059
|
-
x402PendingDepositExpiresAt,
|
|
9060
|
-
x402PendingUnsettled,
|
|
9061
|
-
x402PendingFundingJson,
|
|
9062
|
-
withX402ChannelLock,
|
|
9063
|
-
DvmX402ChannelStorage,
|
|
9064
|
-
recordX402ChannelMetadata,
|
|
9065
|
-
acknowledgeX402ChannelOwner,
|
|
9066
|
-
listX402Channels,
|
|
9067
|
-
findX402Channel,
|
|
9068
|
-
x402ChannelBalanceUnreconciledAt,
|
|
9069
|
-
recordPendingX402Funding,
|
|
9070
|
-
loadResumableX402Funding,
|
|
9071
|
-
listPendingX402Fundings,
|
|
9072
|
-
listResumableX402Fundings,
|
|
9073
|
-
listPendingX402FundingsForChannel,
|
|
9074
|
-
retirePendingX402Funding,
|
|
9075
|
-
clearPendingX402Funding,
|
|
9076
|
-
loadX402Wallet,
|
|
10913
|
+
isValidEvmPrivateKey,
|
|
9077
10914
|
ensureAddress,
|
|
9078
|
-
connectedX402Payer,
|
|
9079
10915
|
generateX402PrivateKey,
|
|
9080
|
-
repinX402WalletNetwork,
|
|
9081
|
-
saveX402Wallet,
|
|
9082
|
-
removeX402Wallet,
|
|
9083
10916
|
getX402Balance,
|
|
9084
|
-
tryX402Payment,
|
|
9085
|
-
isSignableExactRequirement,
|
|
9086
|
-
callerLoggers,
|
|
9087
|
-
initCallerLoggers,
|
|
9088
10917
|
x402Version,
|
|
9089
10918
|
numberToDecimalString,
|
|
9090
10919
|
parseMoneyString,
|
|
@@ -9103,9 +10932,24 @@ export {
|
|
|
9103
10932
|
encodePaymentResponseHeader,
|
|
9104
10933
|
decodePaymentResponseHeader,
|
|
9105
10934
|
x402ResourceServer,
|
|
10935
|
+
withPaymentDiagnostics,
|
|
10936
|
+
withPaymentStage,
|
|
10937
|
+
paymentFailureStage,
|
|
9106
10938
|
createX402FacilitatorAuthHeaders,
|
|
9107
10939
|
createX402FacilitatorClient,
|
|
9108
10940
|
verifyWithFacilitator,
|
|
9109
10941
|
settleWithFacilitator,
|
|
9110
10942
|
verifyX402Payment
|
|
9111
10943
|
};
|
|
10944
|
+
/*! Bundled license information:
|
|
10945
|
+
|
|
10946
|
+
@noble/hashes/utils.js:
|
|
10947
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
10948
|
+
|
|
10949
|
+
@noble/curves/utils.js:
|
|
10950
|
+
@noble/curves/abstract/modular.js:
|
|
10951
|
+
@noble/curves/abstract/curve.js:
|
|
10952
|
+
@noble/curves/abstract/weierstrass.js:
|
|
10953
|
+
@noble/curves/secp256k1.js:
|
|
10954
|
+
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
10955
|
+
*/
|