@devlab.group/agent-commerce 2.5.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -21
- package/dist/ap2.d.ts +19 -6
- package/dist/ap2.js +56 -18
- package/dist/ap2.js.map +1 -1
- package/dist/{chunk-ES3V6ECS.js → chunk-77HLISG4.js} +3 -2
- package/dist/chunk-77HLISG4.js.map +1 -0
- package/dist/{chunk-5ZYXBCDX.js → chunk-D7FPSGUQ.js} +25 -4
- package/dist/chunk-D7FPSGUQ.js.map +1 -0
- package/dist/{chunk-VUCMBGUR.js → chunk-DJRWYDKJ.js} +3 -3
- package/dist/{chunk-VUCMBGUR.js.map → chunk-DJRWYDKJ.js.map} +1 -1
- package/dist/{chunk-4K7FIBJM.js → chunk-EMKITGVV.js} +4 -4
- package/dist/chunk-EMKITGVV.js.map +1 -0
- package/dist/chunk-IWXVQYG5.js +6 -0
- package/dist/chunk-IWXVQYG5.js.map +1 -0
- package/dist/chunk-O3ACF45K.js +8 -0
- package/dist/chunk-O3ACF45K.js.map +1 -0
- package/dist/chunk-WDD2ZUR3.js +20 -0
- package/dist/chunk-WDD2ZUR3.js.map +1 -0
- package/dist/{chunk-3OTCFFFT.js → chunk-YHNTTMED.js} +7 -7
- package/dist/chunk-YHNTTMED.js.map +1 -0
- package/dist/{chunk-FKQK5XZA.js → chunk-ZSIUZPNB.js} +183 -80
- package/dist/chunk-ZSIUZPNB.js.map +1 -0
- package/dist/cli/{ap2-YAPLERSC.js → ap2-5FFGSW6F.js} +61 -25
- package/dist/cli/ap2-5FFGSW6F.js.map +1 -0
- package/dist/cli/{chunk-5K4XEAZV.js → chunk-3IEXJ7I3.js} +25 -4
- package/dist/cli/chunk-3IEXJ7I3.js.map +1 -0
- package/dist/cli/{chunk-NIU5EMSB.js → chunk-3NVJAQRO.js} +5 -5
- package/dist/cli/{chunk-NIU5EMSB.js.map → chunk-3NVJAQRO.js.map} +1 -1
- package/dist/cli/{chunk-IDY2735M.js → chunk-55AI6CLW.js} +6 -4
- package/dist/cli/chunk-55AI6CLW.js.map +1 -0
- package/dist/cli/{chunk-QA2OPHBF.js → chunk-5K2JDM4G.js} +185 -83
- package/dist/cli/chunk-5K2JDM4G.js.map +1 -0
- package/dist/cli/{chunk-QLUCNPRT.js → chunk-5TID36YG.js} +860 -170
- package/dist/cli/chunk-5TID36YG.js.map +1 -0
- package/dist/cli/{chunk-5R7DM5LM.js → chunk-66NTYSXI.js} +27 -27
- package/dist/cli/chunk-66NTYSXI.js.map +1 -0
- package/dist/cli/{chunk-VJ4EO4LE.js → chunk-6EBDOH37.js} +3 -3
- package/dist/cli/{chunk-VJ4EO4LE.js.map → chunk-6EBDOH37.js.map} +1 -1
- package/dist/cli/{chunk-4H2QLHQI.js → chunk-AGQ4U7IN.js} +5 -5
- package/dist/cli/chunk-AGQ4U7IN.js.map +1 -0
- package/dist/cli/chunk-APC5BTQM.js +6 -0
- package/dist/cli/chunk-APC5BTQM.js.map +1 -0
- package/dist/cli/{chunk-SYQLJXIU.js → chunk-C5YISPT6.js} +3 -3
- package/dist/cli/chunk-C5YISPT6.js.map +1 -0
- package/dist/cli/chunk-DKWO6LLX.js +34 -0
- package/dist/cli/chunk-DKWO6LLX.js.map +1 -0
- package/dist/cli/{chunk-SPAYKVUP.js → chunk-FL4HPCD4.js} +3 -2
- package/dist/cli/chunk-FL4HPCD4.js.map +1 -0
- package/dist/cli/{chunk-3EQBCFZY.js → chunk-H7EBJ4I3.js} +3 -3
- package/dist/cli/{chunk-3EQBCFZY.js.map → chunk-H7EBJ4I3.js.map} +1 -1
- package/dist/cli/{chunk-Z34UMZBB.js → chunk-OETKXDGA.js} +546 -175
- package/dist/cli/chunk-OETKXDGA.js.map +1 -0
- package/dist/cli/{chunk-JFVRG2HH.js → chunk-PLHPKXAT.js} +8 -8
- package/dist/cli/chunk-PLHPKXAT.js.map +1 -0
- package/dist/cli/{chunk-2UPPX3FJ.js → chunk-VSSSU4AU.js} +76 -14
- package/dist/cli/chunk-VSSSU4AU.js.map +1 -0
- package/dist/cli/chunk-ZCW56PZ3.js +8 -0
- package/dist/cli/{chunk-M7NXVRU5.js.map → chunk-ZCW56PZ3.js.map} +1 -1
- package/dist/cli/{chunk-2J2IQQ4X.js → chunk-ZFVQWOGG.js} +95 -42
- package/dist/cli/chunk-ZFVQWOGG.js.map +1 -0
- package/dist/cli/config-WT5HHDRR.js +13 -0
- package/dist/cli/{config-BWFMN2ZB.js.map → config-WT5HHDRR.js.map} +1 -1
- package/dist/cli/index.js +560 -252
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/{mcp-KZEMXLHR.js → mcp-HHGNOUKE.js} +7 -7
- package/dist/cli/{mcp-KZEMXLHR.js.map → mcp-HHGNOUKE.js.map} +1 -1
- package/dist/cli/payment-providers-EWXZFBIE.js +6 -0
- package/dist/cli/{payment-providers-SZL36H3Y.js.map → payment-providers-EWXZFBIE.js.map} +1 -1
- package/dist/cli/{provider-6TKB5ADJ.js → provider-XUPLYNQ7.js} +36 -18
- package/dist/cli/provider-XUPLYNQ7.js.map +1 -0
- package/dist/cli/receipts-3DSV4DIK.js +9 -0
- package/dist/cli/{receipts-ZGLJWMQG.js.map → receipts-3DSV4DIK.js.map} +1 -1
- package/dist/cli/reconcile-GS25MQ42.js +4 -0
- package/dist/cli/{reconcile-IC22MJD5.js.map → reconcile-GS25MQ42.js.map} +1 -1
- package/dist/cli/{run-ZTYPQI4D.js → run-7KLKW3HE.js} +310 -91
- package/dist/cli/run-7KLKW3HE.js.map +1 -0
- package/dist/cli/{x402-WLYBLODI.js → x402-HP4E3QET.js} +11 -10
- package/dist/cli/x402-HP4E3QET.js.map +1 -0
- package/dist/{guardrails-CX_jYXvZ.d.ts → guardrails-CeyLS1ou.d.ts} +2 -2
- package/dist/index.d.ts +119 -206
- package/dist/index.js +1703 -427
- package/dist/index.js.map +1 -1
- package/dist/logger-CllGwQ8i.d.ts +19 -0
- package/dist/mcp.d.ts +3 -1
- package/dist/mcp.js +4 -3
- package/dist/mcp.js.map +1 -1
- package/dist/mpp.d.ts +2 -1
- package/dist/mpp.js +32 -13
- package/dist/mpp.js.map +1 -1
- package/dist/{protocol-adapter-DEXpdp8m.d.ts → protocol-adapter-DdFL5LBU.d.ts} +15 -132
- package/dist/{runtime-Dz2fSO-P.d.ts → runtime-BmaSp2Ce.d.ts} +36 -23
- package/dist/store-BVvMzpuV.d.ts +389 -0
- package/dist/testing.d.ts +51 -0
- package/dist/testing.js +2050 -0
- package/dist/testing.js.map +1 -0
- package/dist/transaction-BXXJltZ-.d.ts +129 -0
- package/dist/{types-j7MnUXYN.d.ts → types-DWqjz81-.d.ts} +5 -2
- package/dist/x402.d.ts +9 -3
- package/dist/x402.js +7 -5
- package/dist/x402.js.map +1 -1
- package/package.json +17 -29
- package/dist/chunk-3OTCFFFT.js.map +0 -1
- package/dist/chunk-4K7FIBJM.js.map +0 -1
- package/dist/chunk-5ZYXBCDX.js.map +0 -1
- package/dist/chunk-ES3V6ECS.js.map +0 -1
- package/dist/chunk-FKQK5XZA.js.map +0 -1
- package/dist/cli/ap2-YAPLERSC.js.map +0 -1
- package/dist/cli/chunk-2J2IQQ4X.js.map +0 -1
- package/dist/cli/chunk-2UPPX3FJ.js.map +0 -1
- package/dist/cli/chunk-4H2QLHQI.js.map +0 -1
- package/dist/cli/chunk-5K4XEAZV.js.map +0 -1
- package/dist/cli/chunk-5R7DM5LM.js.map +0 -1
- package/dist/cli/chunk-FHCFR2GS.js +0 -17
- package/dist/cli/chunk-FHCFR2GS.js.map +0 -1
- package/dist/cli/chunk-IDY2735M.js.map +0 -1
- package/dist/cli/chunk-JFVRG2HH.js.map +0 -1
- package/dist/cli/chunk-M7NXVRU5.js +0 -8
- package/dist/cli/chunk-QA2OPHBF.js.map +0 -1
- package/dist/cli/chunk-QLUCNPRT.js.map +0 -1
- package/dist/cli/chunk-SPAYKVUP.js.map +0 -1
- package/dist/cli/chunk-SYQLJXIU.js.map +0 -1
- package/dist/cli/chunk-Z34UMZBB.js.map +0 -1
- package/dist/cli/config-BWFMN2ZB.js +0 -12
- package/dist/cli/payment-providers-SZL36H3Y.js +0 -6
- package/dist/cli/provider-6TKB5ADJ.js.map +0 -1
- package/dist/cli/receipts-ZGLJWMQG.js +0 -8
- package/dist/cli/reconcile-IC22MJD5.js +0 -4
- package/dist/cli/run-ZTYPQI4D.js.map +0 -1
- package/dist/cli/x402-WLYBLODI.js.map +0 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { isWellKnownDevKey, findX402Network, normalizeAddress, assertPayToIsNotDevAddress, resolveX402Deployment, assertDevKeyIsLocalOnly, describeRpc, canonicalJson, X402_DEFAULT_PAYMENT_FLOW, REUSE_REJECTED_AUTHORIZATION_METADATA_KEY, SETTLE_AFTER_BACKEND_METADATA_KEY, sameAddress, LOCAL_CHAIN_ID, isTransactionReference, x402OfferName, isEvmAddress, describeDeploymentMode } from './chunk-
|
|
2
|
-
import {
|
|
1
|
+
import { isWellKnownDevKey, findX402Network, normalizeAddress, assertPayToIsNotDevAddress, resolveX402Deployment, assertDevKeyIsLocalOnly, describeRpc, canonicalJson, X402_DEFAULT_PAYMENT_FLOW, REUSE_REJECTED_AUTHORIZATION_METADATA_KEY, SETTLE_AFTER_BACKEND_METADATA_KEY, sameAddress, LOCAL_CHAIN_ID, isTransactionReference, x402OfferName, CDP_FACILITATOR_URL, isEvmAddress, describeDeploymentMode } from './chunk-EMKITGVV.js';
|
|
2
|
+
import { PACKAGE_VERSION } from './chunk-O3ACF45K.js';
|
|
3
|
+
import { CommerceError, systemClock, NOOP_LOGGER, redactedErrorText, RECONCILIATION_METADATA_KEY, isCommerceError } from './chunk-YHNTTMED.js';
|
|
3
4
|
import { LOCATOR_VERSION, eip3009Locator, computeReplayKey, permit2Locator, computePermit2ReplayKey, isoFromSeconds } from './chunk-GA3O2A7R.js';
|
|
4
5
|
import { isRecord } from './chunk-2CJNA7MS.js';
|
|
5
6
|
import { x402ExactPermit2ProxyAddress, toFacilitatorEvmSigner, PERMIT2_ADDRESS, eip3009ABI, x402ExactPermit2ProxyABI } from '@x402/evm';
|
|
@@ -7,9 +8,9 @@ import { parseAbi, isAddress, keccak256, stringToBytes, isHash, getAddress, form
|
|
|
7
8
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
8
9
|
import { HTTPFacilitatorClient } from '@x402/core/http';
|
|
9
10
|
import { SettleError, VerifyError } from '@x402/core/types';
|
|
11
|
+
import { randomBytes, createPrivateKey, sign } from 'crypto';
|
|
10
12
|
import { x402Facilitator } from '@x402/core/facilitator';
|
|
11
13
|
import { registerExactEvmScheme } from '@x402/evm/exact/facilitator';
|
|
12
|
-
import { randomBytes } from 'crypto';
|
|
13
14
|
import { PaymentPayloadV2Schema } from '@x402/core/schemas';
|
|
14
15
|
|
|
15
16
|
// src/payments/x402/provider/constants.ts
|
|
@@ -425,32 +426,71 @@ function createLocalFacilitatorClient(rpcUrl, signerPrivateKey, chainId) {
|
|
|
425
426
|
transport: http(rpcUrl)
|
|
426
427
|
}).extend(publicActions);
|
|
427
428
|
}
|
|
428
|
-
var
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
(mod) => mod.createCdpAuthHeaders(apiKeyId, apiKeySecret),
|
|
433
|
-
(cause) => {
|
|
434
|
-
throw new CommerceError(
|
|
435
|
-
"CONFIG_INVALID",
|
|
436
|
-
'x402 provider: facilitator.auth.type is "cdp", which needs the optional peer "@coinbase/x402". Install it (npm install @coinbase/x402), or use a facilitator that accepts a static token with auth.type "bearer".',
|
|
437
|
-
{ cause }
|
|
438
|
-
);
|
|
439
|
-
}
|
|
440
|
-
);
|
|
441
|
-
loading.catch(() => {
|
|
442
|
-
});
|
|
429
|
+
var TOKEN_LIFETIME_SECONDS = 120;
|
|
430
|
+
function createCdpAuthHeaders(apiKeyId, apiKeySecret, now = Date.now) {
|
|
431
|
+
const signingKey = readKey(apiKeySecret);
|
|
432
|
+
const { host, pathname } = new URL(CDP_FACILITATOR_URL);
|
|
443
433
|
return async () => {
|
|
444
|
-
|
|
445
|
-
if (!create) {
|
|
434
|
+
if (!signingKey) {
|
|
446
435
|
throw new CommerceError(
|
|
447
|
-
"
|
|
448
|
-
"x402 provider:
|
|
436
|
+
"CONFIG_INVALID",
|
|
437
|
+
"x402 provider: facilitator.auth.apiKeySecret is not a CDP API key secret. Use the PEM EC private key or the base64 Ed25519 key that CDP issued."
|
|
449
438
|
);
|
|
450
439
|
}
|
|
451
|
-
|
|
440
|
+
const issuedAt = Math.floor(now() / 1e3);
|
|
441
|
+
const header = (method, route) => ({
|
|
442
|
+
Authorization: `Bearer ${signJwt(signingKey, apiKeyId, `${method} ${host}${pathname}/${route}`, issuedAt)}`
|
|
443
|
+
});
|
|
444
|
+
return {
|
|
445
|
+
verify: header("POST", "verify"),
|
|
446
|
+
settle: header("POST", "settle"),
|
|
447
|
+
supported: header("GET", "supported")
|
|
448
|
+
};
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
function readKey(secret) {
|
|
452
|
+
try {
|
|
453
|
+
if (secret.includes("-----BEGIN")) {
|
|
454
|
+
const key2 = createPrivateKey(secret);
|
|
455
|
+
const p256 = key2.asymmetricKeyType === "ec" && key2.asymmetricKeyDetails?.namedCurve === "prime256v1";
|
|
456
|
+
return p256 ? { alg: "ES256", key: key2 } : void 0;
|
|
457
|
+
}
|
|
458
|
+
const raw = Buffer.from(secret, "base64");
|
|
459
|
+
if (raw.length !== 64) return void 0;
|
|
460
|
+
const key = createPrivateKey({
|
|
461
|
+
format: "jwk",
|
|
462
|
+
key: {
|
|
463
|
+
kty: "OKP",
|
|
464
|
+
crv: "Ed25519",
|
|
465
|
+
d: raw.subarray(0, 32).toString("base64url"),
|
|
466
|
+
x: raw.subarray(32).toString("base64url")
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
return { alg: "EdDSA", key };
|
|
470
|
+
} catch {
|
|
471
|
+
return void 0;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
function signJwt(signingKey, apiKeyId, uri, issuedAt) {
|
|
475
|
+
const { alg, key } = signingKey;
|
|
476
|
+
const header = { alg, kid: apiKeyId, typ: "JWT", nonce: randomBytes(16).toString("hex") };
|
|
477
|
+
const claims = {
|
|
478
|
+
sub: apiKeyId,
|
|
479
|
+
iss: "cdp",
|
|
480
|
+
uris: [uri],
|
|
481
|
+
iat: issuedAt,
|
|
482
|
+
nbf: issuedAt,
|
|
483
|
+
exp: issuedAt + TOKEN_LIFETIME_SECONDS
|
|
452
484
|
};
|
|
485
|
+
const input = `${base64url(header)}.${base64url(claims)}`;
|
|
486
|
+
const signature = alg === "ES256" ? sign("sha256", Buffer.from(input), { key, dsaEncoding: "ieee-p1363" }) : sign(null, Buffer.from(input), key);
|
|
487
|
+
return `${input}.${signature.toString("base64url")}`;
|
|
453
488
|
}
|
|
489
|
+
var base64url = (value) => Buffer.from(JSON.stringify(value)).toString("base64url");
|
|
490
|
+
|
|
491
|
+
// src/payments/x402/facilitator.ts
|
|
492
|
+
var SETTLEMENT_CONFIRMATION_TIMEOUT_MS = 6e4;
|
|
493
|
+
var SUPPORTED_TIMEOUT_MS = 1e4;
|
|
454
494
|
function verdictFromVerifyError(err) {
|
|
455
495
|
if (err instanceof VerifyError && err.statusCode === 400 && typeof err.invalidReason === "string") {
|
|
456
496
|
return {
|
|
@@ -482,7 +522,7 @@ function createRemoteFacilitatorBinding(options) {
|
|
|
482
522
|
return { verify: headers, settle: headers, supported: headers };
|
|
483
523
|
};
|
|
484
524
|
} else if (auth.type === "cdp") {
|
|
485
|
-
createAuthHeaders =
|
|
525
|
+
createAuthHeaders = createCdpAuthHeaders(auth.apiKeyId, auth.apiKeySecret);
|
|
486
526
|
}
|
|
487
527
|
const client = new HTTPFacilitatorClient({
|
|
488
528
|
url: options.url,
|
|
@@ -633,10 +673,10 @@ function resolveOffers(inputs, options) {
|
|
|
633
673
|
let remoteBinding;
|
|
634
674
|
let mode;
|
|
635
675
|
const built = /* @__PURE__ */ new Map();
|
|
636
|
-
const resolveList = (list,
|
|
676
|
+
const resolveList = (list, field2) => {
|
|
637
677
|
const seen = /* @__PURE__ */ new Set();
|
|
638
678
|
return list.map((input, index) => {
|
|
639
|
-
const at = (name) =>
|
|
679
|
+
const at = (name) => field2(index, name);
|
|
640
680
|
const known = findX402Network(input.network, options.customNetworks);
|
|
641
681
|
if (known !== void 0 && known.family !== "evm") {
|
|
642
682
|
const shape = known.family === "svm" ? "a Solana address (base58, 32 bytes)" : "a NEAR account ID";
|
|
@@ -792,6 +832,104 @@ function offerCurrency(offer, quoted) {
|
|
|
792
832
|
const canonical = offer.profile.canonicalAsset;
|
|
793
833
|
return canonical !== void 0 && sameAddress(offer.profile, offer.asset, canonical.address) ? canonical.symbol : quoted;
|
|
794
834
|
}
|
|
835
|
+
function decodePaymentSubmission(payloadBase64) {
|
|
836
|
+
const json = decodeJson(payloadBase64);
|
|
837
|
+
if (json === void 0) return void 0;
|
|
838
|
+
const result = PaymentPayloadV2Schema.safeParse(json);
|
|
839
|
+
return result.success ? result.data : void 0;
|
|
840
|
+
}
|
|
841
|
+
function declaredX402Version(payloadBase64) {
|
|
842
|
+
const json = decodeJson(payloadBase64);
|
|
843
|
+
return typeof json === "object" && json !== null ? json["x402Version"] : void 0;
|
|
844
|
+
}
|
|
845
|
+
function forFacilitator(payload) {
|
|
846
|
+
const { resource: _resource, extensions: _extensions, ...rest } = payload;
|
|
847
|
+
return rest;
|
|
848
|
+
}
|
|
849
|
+
function decodeJson(payloadBase64) {
|
|
850
|
+
try {
|
|
851
|
+
return JSON.parse(Buffer.from(payloadBase64, "base64").toString("utf8"));
|
|
852
|
+
} catch {
|
|
853
|
+
return void 0;
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
function isExactEvmPayload(payload) {
|
|
857
|
+
const inner = payload.payload;
|
|
858
|
+
return payload.accepted.scheme === "exact" && isRecord(inner) && isRecord(inner["authorization"]) && !("permit2Authorization" in inner) && typeof inner["signature"] === "string";
|
|
859
|
+
}
|
|
860
|
+
var UINT256 = /^(0|[1-9]\d{0,77})$/;
|
|
861
|
+
var UINT256_MAX = 2n ** 256n - 1n;
|
|
862
|
+
var BYTES32 = /^0x[0-9a-fA-F]{64}$/;
|
|
863
|
+
var isUint256 = (value) => typeof value === "string" && UINT256.test(value) && BigInt(value) <= UINT256_MAX;
|
|
864
|
+
function hasEip3009Fields(authorization) {
|
|
865
|
+
return isEvmAddress(authorization["from"]) && isEvmAddress(authorization["to"]) && isUint256(authorization["value"]) && isUint256(authorization["validAfter"]) && isUint256(authorization["validBefore"]) && typeof authorization["nonce"] === "string" && BYTES32.test(authorization["nonce"]);
|
|
866
|
+
}
|
|
867
|
+
function isExactPermit2Payload(payload) {
|
|
868
|
+
const inner = payload.payload;
|
|
869
|
+
if (payload.accepted.scheme !== "exact" || !isRecord(inner)) return false;
|
|
870
|
+
const auth = inner["permit2Authorization"];
|
|
871
|
+
if (!isRecord(auth) || "authorization" in inner || typeof inner["signature"] !== "string") {
|
|
872
|
+
return false;
|
|
873
|
+
}
|
|
874
|
+
const { permitted, witness } = auth;
|
|
875
|
+
return isRecord(permitted) && isRecord(witness) && isEvmAddress(auth["from"]) && isEvmAddress(auth["spender"]) && isEvmAddress(permitted["token"]) && isEvmAddress(witness["to"]) && isUint256(permitted["amount"]) && isUint256(auth["nonce"]) && isUint256(auth["deadline"]) && isUint256(witness["validAfter"]);
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// src/payments/x402/provider/payment-id.ts
|
|
879
|
+
var PAYMENT_IDENTIFIER = "payment-identifier";
|
|
880
|
+
var ID_PATTERN = /^[A-Za-z0-9_-]{16,128}$/;
|
|
881
|
+
var field = (value, key) => isRecord(value) ? value[key] : void 0;
|
|
882
|
+
function paymentIdentifierExtensions(resource) {
|
|
883
|
+
const redelivery = resource.idempotency?.redelivery;
|
|
884
|
+
if (redelivery === void 0) return void 0;
|
|
885
|
+
return {
|
|
886
|
+
[PAYMENT_IDENTIFIER]: {
|
|
887
|
+
info: { required: redelivery.required },
|
|
888
|
+
schema: {
|
|
889
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
890
|
+
type: "object",
|
|
891
|
+
properties: {
|
|
892
|
+
required: { type: "boolean" },
|
|
893
|
+
id: { type: "string", minLength: 16, maxLength: 128, pattern: "^[a-zA-Z0-9_-]+$" }
|
|
894
|
+
},
|
|
895
|
+
required: ["required"]
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
function paymentRetryKey(context) {
|
|
901
|
+
const { resource, submission } = context;
|
|
902
|
+
const redelivery = resource.idempotency?.redelivery;
|
|
903
|
+
if (redelivery === void 0) return void 0;
|
|
904
|
+
const payload = decodePaymentSubmission(submission.payload);
|
|
905
|
+
if (payload === void 0) return void 0;
|
|
906
|
+
const extension = payload.extensions?.[PAYMENT_IDENTIFIER];
|
|
907
|
+
const info = field(extension, "info");
|
|
908
|
+
if (extension !== void 0 && !isRecord(info)) throw invalid(resource);
|
|
909
|
+
const id = field(info, "id");
|
|
910
|
+
if (id === void 0) {
|
|
911
|
+
if (!redelivery.required) return void 0;
|
|
912
|
+
throw new CommerceError("INPUT_INVALID", "This resource requires a payment identifier", {
|
|
913
|
+
resourceId: resource.id,
|
|
914
|
+
details: { reason: "payment_identifier_required" }
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
if (typeof id !== "string" || !ID_PATTERN.test(id)) throw invalid(resource);
|
|
918
|
+
return id;
|
|
919
|
+
}
|
|
920
|
+
function paymentIdentifierEchoed(envelope, payload) {
|
|
921
|
+
const declared = field(field(field(envelope, "extensions"), PAYMENT_IDENTIFIER), "info");
|
|
922
|
+
const echoed = payload.extensions?.[PAYMENT_IDENTIFIER];
|
|
923
|
+
if (!isRecord(declared) || echoed === void 0) return true;
|
|
924
|
+
return field(field(echoed, "info"), "required") === declared["required"];
|
|
925
|
+
}
|
|
926
|
+
function invalid(resource) {
|
|
927
|
+
return new CommerceError(
|
|
928
|
+
"INPUT_INVALID",
|
|
929
|
+
'A payment identifier must be 16 to 128 characters of A-Z, a-z, 0-9, "_" and "-"',
|
|
930
|
+
{ resourceId: resource.id, details: { reason: "payment_identifier_invalid" } }
|
|
931
|
+
);
|
|
932
|
+
}
|
|
795
933
|
var QUOTE_EXTENSION = "erc-8409";
|
|
796
934
|
var QUOTE_REQUEST_SCHEME = "agent-commerce/request/v1";
|
|
797
935
|
var QUOTE_DOMAIN = { name: "Signed Service Payment Quotes", version: "1" };
|
|
@@ -824,9 +962,9 @@ var QUOTE_INFO_SCHEMA = {
|
|
|
824
962
|
// Every field is a string in the ERC's JSON encoding
|
|
825
963
|
quote: {
|
|
826
964
|
type: "object",
|
|
827
|
-
required: QUOTE_TYPES.ServicePaymentQuote.map((
|
|
965
|
+
required: QUOTE_TYPES.ServicePaymentQuote.map((field2) => field2.name),
|
|
828
966
|
properties: Object.fromEntries(
|
|
829
|
-
QUOTE_TYPES.ServicePaymentQuote.map((
|
|
967
|
+
QUOTE_TYPES.ServicePaymentQuote.map((field2) => [field2.name, { type: "string" }])
|
|
830
968
|
)
|
|
831
969
|
},
|
|
832
970
|
signature: { type: "string" }
|
|
@@ -1510,48 +1648,6 @@ function parseCanonicalAmount(amount, decimals) {
|
|
|
1510
1648
|
const combined = `${integerPart}${paddedFraction}`;
|
|
1511
1649
|
return BigInt(combined);
|
|
1512
1650
|
}
|
|
1513
|
-
function decodePaymentSubmission(payloadBase64) {
|
|
1514
|
-
const json = decodeJson(payloadBase64);
|
|
1515
|
-
if (json === void 0) return void 0;
|
|
1516
|
-
const result = PaymentPayloadV2Schema.safeParse(json);
|
|
1517
|
-
return result.success ? result.data : void 0;
|
|
1518
|
-
}
|
|
1519
|
-
function declaredX402Version(payloadBase64) {
|
|
1520
|
-
const json = decodeJson(payloadBase64);
|
|
1521
|
-
return typeof json === "object" && json !== null ? json["x402Version"] : void 0;
|
|
1522
|
-
}
|
|
1523
|
-
function forFacilitator(payload) {
|
|
1524
|
-
const { resource: _resource, extensions: _extensions, ...rest } = payload;
|
|
1525
|
-
return rest;
|
|
1526
|
-
}
|
|
1527
|
-
function decodeJson(payloadBase64) {
|
|
1528
|
-
try {
|
|
1529
|
-
return JSON.parse(Buffer.from(payloadBase64, "base64").toString("utf8"));
|
|
1530
|
-
} catch {
|
|
1531
|
-
return void 0;
|
|
1532
|
-
}
|
|
1533
|
-
}
|
|
1534
|
-
function isExactEvmPayload(payload) {
|
|
1535
|
-
const inner = payload.payload;
|
|
1536
|
-
return payload.accepted.scheme === "exact" && isRecord(inner) && isRecord(inner["authorization"]) && !("permit2Authorization" in inner) && typeof inner["signature"] === "string";
|
|
1537
|
-
}
|
|
1538
|
-
var UINT256 = /^(0|[1-9]\d{0,77})$/;
|
|
1539
|
-
var UINT256_MAX = 2n ** 256n - 1n;
|
|
1540
|
-
var BYTES32 = /^0x[0-9a-fA-F]{64}$/;
|
|
1541
|
-
var isUint256 = (value) => typeof value === "string" && UINT256.test(value) && BigInt(value) <= UINT256_MAX;
|
|
1542
|
-
function hasEip3009Fields(authorization) {
|
|
1543
|
-
return isEvmAddress(authorization["from"]) && isEvmAddress(authorization["to"]) && isUint256(authorization["value"]) && isUint256(authorization["validAfter"]) && isUint256(authorization["validBefore"]) && typeof authorization["nonce"] === "string" && BYTES32.test(authorization["nonce"]);
|
|
1544
|
-
}
|
|
1545
|
-
function isExactPermit2Payload(payload) {
|
|
1546
|
-
const inner = payload.payload;
|
|
1547
|
-
if (payload.accepted.scheme !== "exact" || !isRecord(inner)) return false;
|
|
1548
|
-
const auth = inner["permit2Authorization"];
|
|
1549
|
-
if (!isRecord(auth) || "authorization" in inner || typeof inner["signature"] !== "string") {
|
|
1550
|
-
return false;
|
|
1551
|
-
}
|
|
1552
|
-
const { permitted, witness } = auth;
|
|
1553
|
-
return isRecord(permitted) && isRecord(witness) && isEvmAddress(auth["from"]) && isEvmAddress(auth["spender"]) && isEvmAddress(permitted["token"]) && isEvmAddress(witness["to"]) && isUint256(permitted["amount"]) && isUint256(auth["nonce"]) && isUint256(auth["deadline"]) && isUint256(witness["validAfter"]);
|
|
1554
|
-
}
|
|
1555
1651
|
|
|
1556
1652
|
// src/payments/x402/provider/requirement.ts
|
|
1557
1653
|
var SELECTED_OFFER_KEY = "x402.offer";
|
|
@@ -1596,7 +1692,7 @@ function createRequirementBuilders(ctx, svmFeePayer) {
|
|
|
1596
1692
|
}))
|
|
1597
1693
|
);
|
|
1598
1694
|
const single = offers.length === 1 ? offers[0] : void 0;
|
|
1599
|
-
const
|
|
1695
|
+
const quoteExtensions = await ctx.quotes?.extensionsFor(
|
|
1600
1696
|
context.resource.id,
|
|
1601
1697
|
context.input,
|
|
1602
1698
|
offers.flatMap((offer, index) => {
|
|
@@ -1604,6 +1700,8 @@ function createRequirementBuilders(ctx, svmFeePayer) {
|
|
|
1604
1700
|
return offer.family === "evm" && accepted ? [{ offer, amount: accepted.amount }] : [];
|
|
1605
1701
|
})
|
|
1606
1702
|
);
|
|
1703
|
+
const identifier = paymentIdentifierExtensions(context.resource);
|
|
1704
|
+
const extensions = identifier === void 0 ? quoteExtensions : { ...quoteExtensions, ...identifier };
|
|
1607
1705
|
const resourceId = encodeURIComponent(context.resource.id);
|
|
1608
1706
|
const envelope = {
|
|
1609
1707
|
x402Version: X402_VERSION,
|
|
@@ -1954,31 +2052,35 @@ function createVerify(ctx, nearRelayers) {
|
|
|
1954
2052
|
locator = read.locator;
|
|
1955
2053
|
derivedPayer = read.from;
|
|
1956
2054
|
}
|
|
2055
|
+
const refused = (reason) => ({ ...rejected(reason), replayKey });
|
|
1957
2056
|
if (payload.accepted.network !== offer.network) {
|
|
1958
|
-
return
|
|
2057
|
+
return refused("invalid_network");
|
|
1959
2058
|
}
|
|
1960
2059
|
const offeredUrl = requirement.challenge.envelope?.resource?.url;
|
|
1961
2060
|
if (!acceptedMatchesOffer(requirements, payload.accepted, offer.profile) || payload.resource !== void 0 && payload.resource.url !== offeredUrl) {
|
|
1962
|
-
return
|
|
2061
|
+
return refused("invalid_payment_requirements");
|
|
2062
|
+
}
|
|
2063
|
+
if (!paymentIdentifierEchoed(requirement.challenge.envelope, payload)) {
|
|
2064
|
+
return refused("extension_echo_mismatch");
|
|
1963
2065
|
}
|
|
1964
2066
|
let required;
|
|
1965
2067
|
try {
|
|
1966
2068
|
required = BigInt(requirements.amount);
|
|
1967
2069
|
} catch {
|
|
1968
|
-
return
|
|
2070
|
+
return refused("invalid_payload");
|
|
1969
2071
|
}
|
|
1970
2072
|
if (transfer) {
|
|
1971
2073
|
if (transfer.to !== getAddress(offer.payTo)) {
|
|
1972
|
-
return
|
|
2074
|
+
return refused(transfer.recipientMismatch);
|
|
1973
2075
|
}
|
|
1974
2076
|
let authorizedValue;
|
|
1975
2077
|
try {
|
|
1976
2078
|
authorizedValue = BigInt(transfer.value);
|
|
1977
2079
|
} catch {
|
|
1978
|
-
return
|
|
2080
|
+
return refused("invalid_payload");
|
|
1979
2081
|
}
|
|
1980
2082
|
if (authorizedValue !== required) {
|
|
1981
|
-
return
|
|
2083
|
+
return refused(transfer.amountMismatch);
|
|
1982
2084
|
}
|
|
1983
2085
|
}
|
|
1984
2086
|
const relayers = offer.family === "near" ? nearRelayers(offer) : void 0;
|
|
@@ -2015,7 +2117,7 @@ function createVerify(ctx, nearRelayers) {
|
|
|
2015
2117
|
"x402 verify(): facilitator rejection reason"
|
|
2016
2118
|
);
|
|
2017
2119
|
}
|
|
2018
|
-
return
|
|
2120
|
+
return refused(sanitizeReason(sdkResult.invalidReason, "invalid_payment"));
|
|
2019
2121
|
}
|
|
2020
2122
|
const payer = derivedPayer ?? (sdkResult.payer !== void 0 && normalizeAddress(offer.profile, sdkResult.payer) ? sdkResult.payer : void 0);
|
|
2021
2123
|
if (offer.family === "svm" && payer !== void 0) locator = { ...locator, payer };
|
|
@@ -2158,6 +2260,7 @@ function createX402PaymentProvider(options) {
|
|
|
2158
2260
|
createRequirement,
|
|
2159
2261
|
// A single offer needs no selection
|
|
2160
2262
|
...severalOffers ? { selectRequirement } : {},
|
|
2263
|
+
retryKey: paymentRetryKey,
|
|
2161
2264
|
verify,
|
|
2162
2265
|
settle,
|
|
2163
2266
|
reconcile: createReconcile(ctx),
|
|
@@ -2183,5 +2286,5 @@ function loadOptionalModule(load, missing) {
|
|
|
2183
2286
|
}
|
|
2184
2287
|
|
|
2185
2288
|
export { DEFAULT_IDS, chainIdFromCaip2, createX402PaymentProvider, parseCanonicalAmount };
|
|
2186
|
-
//# sourceMappingURL=chunk-
|
|
2187
|
-
//# sourceMappingURL=chunk-
|
|
2289
|
+
//# sourceMappingURL=chunk-ZSIUZPNB.js.map
|
|
2290
|
+
//# sourceMappingURL=chunk-ZSIUZPNB.js.map
|