agenti icon indicating copy to clipboard operation
agenti copied to clipboard

pay() re-signs a fresh EIP-3009 authorization on every call — no idempotency key for caller-level retries

Open cv-scvd opened this issue 2 months ago • 1 comments

Where: packages/sdk/src/pay.ts, signEVMPayment() (called from the exported pay()), plus packages/sdk/src/resilience.ts.

Failure mode: signEVMPayment() calls createNonce() to generate a fresh random 32-byte nonce on every invocation, and pay() does exactly one fetch() with the resulting PAYMENT-SIGNATURE/X-Payment header — there's no request-level idempotency key, no cache of an in-flight/just-settled authorization, and no way to recognize "this is the same logical purchase I already tried."

That's fine as long as pay() itself never gets called twice for the same intent. But resilience.ts ships fetchWithRetry/createCircuitBreaker for exactly the scenario where a caller wraps unreliable network calls in retry logic — and if an agent (or the app embedding agenti) wraps pay() in that same retry pattern (or just calls pay() again after a client-side timeout whose response actually made it to the facilitator), each attempt signs and submits an independent EIP-3009 authorization with its own nonce. Every replay guard downstream (nonce-based, as EIP-3009 requires) correctly admits it, because it genuinely is a different authorization — so a caller-level retry after an ambiguous first attempt can settle twice. I checked open/closed issues (#124 network-mismatch validation, #114 async-completion timing, #111 verify-before-settle ordering, #120/#121 body-forwarding bugs) — none of them cover this specific double-submission path, so it doesn't look like duplicate ground.

How we solved the identical problem: our own x402 seller stack had this same gap until 2026-08-01. The fix is an opt-in idempotency key the caller attaches (either an Idempotency-Key HTTP header or _meta['x402/idempotency-key'] in the payment payload) that the resource server uses to dedupe a retried request against a request it already settled, instead of trusting the payment's own nonce to represent request identity. Source: src/lib/idempotency.ts in our repo (scvd.store). When no key is present we disclose that via idempotentHint:false in the 402 response rather than silently accepting the risk, so a buyer's agent/client can tell up front whether retry-safety is available.

For a wallet/SDK layer like agenti specifically, the natural shape is: pay() accepts an optional idempotency key (or generates+persists one keyed off url + options.body for the duration of a logical call), reuses the same signed payload/nonce on retry instead of re-signing, and only re-signs once the caller explicitly starts a new logical purchase. Happy to point at the exact request/response shape we use if useful.

— cv-scvd

cv-scvd avatar Sep 06 '26 14:09 cv-scvd

Thank you, @cv-scvd, for clearly separating payment nonces from logical-request identity. PR #133 is merged: pay() supports caller idempotency keys, reuses the signed authorization for retries, scopes keys to their requirements, and includes focused tests.

nirholas avatar Sep 14 '26 18:09 nirholas