Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

chore: update x402-fetch package — client, index, tests

nirholasda83ded400c9
diff --git a/packages/x402-fetch/package.json b/packages/x402-fetch/package.json
index 8c9c66e0a..1a0b3c607 100644
--- a/packages/x402-fetch/package.json
+++ b/packages/x402-fetch/package.json
@@ -1,6 +1,6 @@
{
"name": "@three-ws/x402-fetch",
- "version": "1.0.0",
+ "version": "1.0.1",
"description": "Drop-in fetch wrapper that automatically pays x402 payment challenges with USDC on Base",
"type": "module",
"main": "dist/index.cjs",
diff --git a/packages/x402-fetch/src/client.js b/packages/x402-fetch/src/client.js
index 290a38bbf..b9a5f11a4 100644
--- a/packages/x402-fetch/src/client.js
+++ b/packages/x402-fetch/src/client.js
@@ -55,12 +55,16 @@ export function buildEip3009TypedData({ accept, payerAddress, chainId, nowSecond
{ name: 'nonce', type: 'bytes32' },
],
};
+ // The x402 v2 `exact` PaymentRequirements schema (and the CDP facilitator that
+ // validates it) require the authorization's numeric fields as decimal STRINGS.
+ // The signed EIP-712 message hashes them as uint256 either way, so the
+ // signature stays valid; the strings are what the facilitator parses.
const authorization = {
from: payerAddress,
to: accept.payTo,
value: String(accept.amount),
- validAfter: 0,
- validBefore,
+ validAfter: '0',
+ validBefore: String(validBefore),
nonce: nonce || randomNonce(),
};
return {
@@ -69,15 +73,19 @@ export function buildEip3009TypedData({ accept, payerAddress, chainId, nowSecond
};
}
-/** Assemble the v2 PaymentPayload for an EVM EIP-3009 signature. */
-export function buildPaymentPayload({ accept, signature, authorization, resourceUrl }) {
+// Assemble the x402 v2 `exact`-scheme PaymentPayload. The shape mirrors
+// @x402/evm's ExactEvmScheme exactly — `{ x402Version, scheme, network,
+// payload: { authorization, signature } }` with NO extra keys, since the CDP
+// facilitator's schema union rejects anything that doesn't match a branch.
+export function buildPaymentPayload({ accept, signature, authorization }) {
return {
x402Version: 2,
- scheme: 'exact',
+ scheme: accept.scheme || 'exact',
network: accept.network,
- resource: resourceUrl ? { url: resourceUrl, mimeType: 'application/json' } : undefined,
+ // The CDP v2 schema requires the matched requirement echoed back as
+ // `accepted` so the facilitator binds the payment to the exact offer.
accepted: accept,
- payload: { signature, authorization },
+ payload: { authorization, signature },
};
}
@@ -86,7 +94,7 @@ export function buildPaymentPayload({ accept, signature, authorization, resource
* @param {{ accept: any, adapter: { getAddress: () => Promise<string>, signTypedData: (td:any)=>Promise<string> }, resourceUrl?: string, nowSeconds?: number, nonce?: string }} args
* @returns {Promise<string>}
*/
-export async function createPaymentHeader({ accept, adapter, resourceUrl, nowSeconds, nonce }) {
+export async function createPaymentHeader({ accept, adapter, nowSeconds, nonce }) {
if (!isEvmNetwork(accept.network)) {
throw new Error(
`x402: network "${accept.network}" is not locally signable by @three-ws/x402-fetch (EVM EIP-3009 / USDC on Base only)`,
@@ -105,7 +113,7 @@ export async function createPaymentHeader({ accept, adapter, resourceUrl, nowSec
nonce,
});
const signature = await adapter.signTypedData(typedData);
- const payload = buildPaymentPayload({ accept, signature, authorization, resourceUrl });
+ const payload = buildPaymentPayload({ accept, signature, authorization });
return b64encode(payload);
}
diff --git a/packages/x402-fetch/src/index.js b/packages/x402-fetch/src/index.js
index ef2bf58fc..dd068782c 100644
--- a/packages/x402-fetch/src/index.js
+++ b/packages/x402-fetch/src/index.js
@@ -104,7 +104,7 @@ export function withX402(arg1, arg2) {
if (onPayment) onPayment({ amount: usd, to: payTo, requestUrl });
const xPayment = await withTimeout(
- createPaymentHeader({ accept, adapter, resourceUrl: challenge.resource?.url || requestUrl }),
+ createPaymentHeader({ accept, adapter }),
timeout,
'payment authorization',
);
diff --git a/packages/x402-fetch/tests/client.test.mjs b/packages/x402-fetch/tests/client.test.mjs
index 8c00c5e8c..2225fbf1b 100644
--- a/packages/x402-fetch/tests/client.test.mjs
+++ b/packages/x402-fetch/tests/client.test.mjs
@@ -78,9 +78,22 @@ test('2. the retried request carries a correct X-PAYMENT header', async () => {
assert.equal(payload.x402Version, 2);
assert.equal(payload.scheme, 'exact');
assert.equal(payload.network, 'eip155:8453');
- assert.equal(payload.payload.authorization.to, PAY_TO);
- assert.equal(payload.payload.authorization.value, '50000');
- assert.equal(payload.payload.authorization.from, WALLET.address);
+ // v2 exact-scheme shape: top-level echoes `accepted`; payload is exactly
+ // { authorization, signature } — no stray keys the CDP schema would reject.
+ assert.deepEqual(
+ Object.keys(payload).sort(),
+ ['accepted', 'network', 'payload', 'scheme', 'x402Version'],
+ );
+ assert.deepEqual(payload.accepted, baseAccept(50_000));
+ assert.deepEqual(Object.keys(payload.payload).sort(), ['authorization', 'signature']);
+ const auth = payload.payload.authorization;
+ assert.equal(auth.to, PAY_TO);
+ assert.equal(auth.value, '50000');
+ assert.equal(auth.from, WALLET.address);
+ // Numeric fields MUST be decimal strings (CDP facilitator schema requires it).
+ assert.equal(auth.validAfter, '0');
+ assert.equal(typeof auth.validBefore, 'string');
+ assert.match(auth.nonce, /^0x[0-9a-f]{64}$/);
// 65-byte recoverable signature (0x + 130 hex).
assert.match(payload.payload.signature, /^0x[0-9a-f]{130}$/);
});