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
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint-plugin-functional": "^10.0.0",
"eslint-plugin-import-x": "^4.17.1",
"eslint-plugin-promise": "^7.3.0",
"eslint-plugin-unicorn": "^72.0.0",
"eslint-plugin-unicorn": "^73.0.0",
"prettier": "3.9.6",
"prettier-plugin-solidity": "^2.4.0",
"typescript-eslint": "^8.66.0"
Expand Down
2 changes: 1 addition & 1 deletion src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('buildPaymentAuthHash', () => {
// =============================================================================
// x402 payment auth
//
// verifyPayment talks to an RPC node; tests inject a fake client via
// isPaymentValid talks to an RPC node; tests inject a fake client via
// setRpcClientFactory so the on-chain checks are exercised deterministically.
// =============================================================================
const TRANSFER_TOPIC = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
Expand Down
12 changes: 6 additions & 6 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function isPaymentRequired(result: AuthResult): result is AuthPaymentRequired {
// =============================================================================
// Shared RPC client pool
//
// `verifyPayment` only needs three read methods. We model that narrow surface
// `isPaymentValid` only needs three read methods. We model that narrow surface
// explicitly (a) so it's clear what an RPC outage / mismatch can affect and
// (b) so tests can inject a fake client via `setRpcClientFactory`.
// =============================================================================
Expand Down Expand Up @@ -162,7 +162,7 @@ const PROOF_RETENTION_MS = (MAX_TX_AGE_SECONDS + 60) * 1000;
const MAX_PROOF_LIFETIME_MS = 10 * 60 * 1000;

// Replay protection. FIFO eviction is refused for entries that are still
// within the recency window enforced by verifyPayment — otherwise a flooder
// within the recency window enforced by isPaymentValid — otherwise a flooder
// could push legitimate entries out and replay them.
const usedPaymentProofs = createBoundedMap<string, PaymentProofEntry>({
maxEntries: 500_000,
Expand All @@ -174,7 +174,7 @@ const usedPaymentProofs = createBoundedMap<string, PaymentProofEntry>({
// =============================================================================
// x402 DoS guard — verification rate limit
//
// `verifyPayment` hits the chain RPC (3 reads × up to 3 retries) per attempt,
// `isPaymentValid` hits the chain RPC (3 reads × up to 3 retries) per attempt,
// so unauthenticated callers spamming bogus `X-Payment-Proof` headers would
// otherwise drain the operator's RPC quota. A separate, much stricter per-IP
// token bucket on x402 verification attempts (the global `server.rateLimit`
Expand Down Expand Up @@ -223,7 +223,7 @@ function parsePaymentProof(raw: string): PaymentProofHeader | string {
};
}

async function verifyPayment(
async function isPaymentValid(
rpc: string,
txHash: Hex,
token: string,
Expand Down Expand Up @@ -313,7 +313,7 @@ async function checkX402(
}

// A submitted proof — whether bogus or not — will hit the chain RPC in
// verifyPayment. Apply a stricter per-IP bucket here so an attacker can't use
// isPaymentValid. Apply a stricter per-IP bucket here so an attacker can't use
// a single permissive `server.rateLimit` to flood the verification path and
// drain the operator's RPC quota. The bucket is independent of the global
// limit; missing client IPs (programmatic callers) share an 'unknown' bucket.
Expand Down Expand Up @@ -364,7 +364,7 @@ async function checkX402(
return { authenticated: false, error: 'Replay cache full — retry shortly' };
}

const isValid = await verifyPayment(config.rpc, parsed.txHash, config.token, config.amount, config.recipient, payer);
const isValid = await isPaymentValid(config.rpc, parsed.txHash, config.token, config.amount, config.recipient, payer);
if (!isValid) {
usedPaymentProofs.delete(normalizedHash);
return { authenticated: false, error: 'Payment verification failed' };
Expand Down