Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sentence under it is the summary shown on the

### Fixed

- The legacy `X-PAYMENT` header is now plain ASCII. A 402 challenge whose `extensions` echoed text such as `…` used to abort every paid call before the request left the machine, with `Cannot convert argument to a ByteString because the character at index ... has a value of 8230`. Non-ASCII characters are escaped, so the payload parses to the same JSON that `PAYMENT-SIGNATURE` carries.
- `vapi router usage` no longer reads as a free daily allowance when the owner account has no Compute. It shows the Compute left today, names the agent's figure as a cap, and says to stake or buy Router balance when neither exists. A chat refused in that state says the same instead of "used up". `docs/router.md` explains each usage field.
- `vapi login`, `vapi logout`, `vapi whoami`, `vapi router`, `vapi stake`, `vapi task`, `vapi code`, `vapi agent`, `vapi docs` and cloud backup honor `allowPrivateNetwork` in the config. Before, they kept the address check on, so on a machine whose DNS answers with proxy addresses such as `198.18.0.0/15` they failed with `api.vapinetwork.ai is not a public address.` even with the flag set. With the flag unset nothing changes.
- Router model, usage and chat errors say why the request never reached vAPI, the way `vapi login` does: `The vAPI Router usage request failed: api.vapinetwork.ai is not a public address.` Before, they printed `The vAPI Router usage request failed.` with no reason.
Expand Down
34 changes: 34 additions & 0 deletions packages/core/src/x402.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,40 @@ describe("browser-neutral x402 client", () => {
});
});

it("keeps the compatibility header ASCII-safe when extensions carry non-ASCII text", async () => {
const quote = parse402Challenge(
{
...exactChallenge({}),
extensions: {
bazaar: {
info: { input: {}, output: { example: { note: "ECB reference rates are…" } } },
},
},
},
networks,
);

const payment = await buildCompatibleX402Payment({
account: { address: BUYER, signTypedData: async () => SIGNATURE },
quote,
nonce: NONCE,
nowSeconds: 1_700_000_000,
});
const compatible = payment.headers["X-PAYMENT"];

// `Headers.set` throws on any code point above 0xff, so a raw JSON header
// broke every payment to a server whose bazaar text used typographic punctuation.
expect(() => new Headers({ "X-PAYMENT": compatible })).not.toThrow();
expect(compatible).toContain("\\u2026");
expect(JSON.parse(compatible)).toMatchObject({
extensions: {
bazaar: {
info: { input: {}, output: { example: { note: "ECB reference rates are…" } } },
},
},
});
});

it("rejects a malformed supplied payment id only when payment-identifier is advertised", async () => {
await expect(
buildPaymentWithExtensions(
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/x402.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ export type X402CompatiblePaymentHeaders = X402PaymentHeaders & {
"X-PAYMENT": string;
};

/**
* Serializes a legacy `X-PAYMENT` payload as header-safe ASCII JSON.
*
* The canonical `PAYMENT-SIGNATURE` header is base64 and carries UTF-8 as-is,
* but the older header is a raw JSON string. Server-declared extensions can
* echo text such as `…`, which `Headers.set` rejects with
* `Cannot convert argument to a ByteString`. Escaping every non-ASCII code
* point keeps the JSON identical after parsing.
*/
export function encodeLegacyPaymentHeader(value: unknown): string {
return JSON.stringify(value).replace(
/[\u007f-\uffff]/g,
(character) => `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`,
);
}

export type X402SettlementOutcome = "succeeded" | "rejected" | "unknown";

export class X402Error extends Error {
Expand Down Expand Up @@ -709,7 +725,7 @@ export async function buildCompatibleX402Payment(args: {
payload: payment.payload,
headers: {
...payment.headers,
"X-PAYMENT": JSON.stringify({
"X-PAYMENT": encodeLegacyPaymentHeader({
x402Version: 2,
scheme: args.quote.accepted.scheme,
network: args.quote.accepted.network,
Expand Down
3 changes: 2 additions & 1 deletion packages/mcp/src/x402.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
assertMaxPrice,
buildEip3009TypedData,
buildX402Payment as buildSharedX402Payment,
encodeLegacyPaymentHeader,
classifySettlement,
parse402Challenge as parseShared402Challenge,
parse402Response as parseShared402Response,
Expand Down Expand Up @@ -115,7 +116,7 @@ export async function buildX402Payment(args: {
payload: payment.payload,
headers: {
...payment.headers,
"X-PAYMENT": JSON.stringify({
"X-PAYMENT": encodeLegacyPaymentHeader({
x402Version: 2,
scheme: args.quote.accepted.scheme,
network: args.quote.accepted.network,
Expand Down
Loading