Repository navigation
feat(payment-anchor): verify an x402 anchor on chain, bound to its contract, once (shadow, default off) #1572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DealAppSeo
wants to merge
3
commits into
main
Choose a base branch
from
claude/payment-anchor-verified-10203a
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,444
−17
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * payment-anchor-db.ts — the Supabase side of `payment-anchor-verify.ts`. | ||
| * | ||
| * TERMS come from three tables, all written by the engine, none by the caller: | ||
| * service_contracts provider, buyer, agreed_price_usdc_raw (the price, in USDC's smallest unit) | ||
| * repid_agents the provider's `wallet_address` = the payee | ||
| * x402_settlements the settlement recorded for the contract (`idempotency_key` = contract id): | ||
| * its `tx_hash` and the facilitator-verified `payer_address` | ||
| * | ||
| * Why `wallet_address` and not `erc8004_address`: MEASURED 2026-11-16 on the five most recent real | ||
| * settlements, every payee was the provider's `wallet_address`, while `erc8004_address` held values | ||
| * such as `pending-mint:…`, a registry contract's address, and free text. A payee read from that | ||
| * column would refuse real payments or accept payments to the wrong address. | ||
| * | ||
| * Why the payer is NOT the buyer's wallet: the same five settlements were all paid by the custodian | ||
| * wallet, not by the buyer agent's own `wallet_address`. The payer is what the facilitator verified | ||
| * from the signed authorization, which `x402_settlements.payer_address` records. | ||
| * | ||
| * REPLAY TABLE: `payment_anchor_claims`, created by the DRAFT migration | ||
| * `supabase/migrations/20261116130000_payment_anchor_claims.sql`. Until Sean applies it, an anchor | ||
| * the chain refuses is still REFUSED, and one the chain accepts reports NOT_CHECKED | ||
| * `replay_store_unreachable` with `chain_verified: true`: it says exactly what is missing instead of | ||
| * passing. | ||
| */ | ||
| import { db } from '../db'; | ||
| import type { AnchorBindingRow, AnchorReplayStore, AnchorTerms } from './payment-anchor-verify'; | ||
|
|
||
| const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; | ||
| const HEX32 = /^0x[0-9a-fA-F]{64}$/; | ||
|
|
||
| /** Load a contract's payment terms. null = no such contract. Throws when the read fails. */ | ||
| export async function dbAnchorTerms(contractId: string): Promise<AnchorTerms | null> { | ||
| if (!UUID.test(contractId)) return null; // service_contracts.id is a uuid; anything else is not a contract | ||
|
|
||
| const { data: c, error: cErr } = await db | ||
| .from('service_contracts') | ||
| .select('id, provider_agent_id, agreed_price_usdc_raw') | ||
| .eq('id', contractId) | ||
| .maybeSingle(); | ||
| if (cErr) throw new Error('service_contracts read failed'); | ||
| if (!c) return null; | ||
|
|
||
| const { data: p, error: pErr } = await db | ||
| .from('repid_agents') | ||
| .select('wallet_address') | ||
| .eq('id', (c as any).provider_agent_id) | ||
| .maybeSingle(); | ||
| if (pErr) throw new Error('repid_agents read failed'); | ||
|
|
||
| // More than one settlement row for a contract is itself an anomaly: maybeSingle errors, which | ||
| // surfaces as NOT_CHECKED rather than picking one. | ||
| const { data: s, error: sErr } = await db | ||
| .from('x402_settlements') | ||
| .select('tx_hash, payer_address, is_simulated') | ||
| .eq('idempotency_key', contractId) | ||
| .maybeSingle(); | ||
| if (sErr) throw new Error('x402_settlements read failed'); | ||
|
|
||
| const recordedTx = | ||
| s && (s as any).is_simulated === false && HEX32.test(String((s as any).tx_hash ?? '')) ? String((s as any).tx_hash) : null; | ||
|
|
||
| let units: bigint; | ||
| try { | ||
| units = BigInt((c as any).agreed_price_usdc_raw ?? 0); | ||
| } catch { | ||
| units = 0n; | ||
| } | ||
|
|
||
| return { | ||
| contractId, | ||
| payeeAddress: (p as any)?.wallet_address ?? null, | ||
| claimedUnits: units, | ||
| payerAddress: recordedTx ? ((s as any).payer_address ?? null) : null, | ||
| recordedTxHash: recordedTx, | ||
| }; | ||
| } | ||
|
|
||
| /** The replay table. Any read or write error throws, which the verifier reports as NOT_CHECKED. */ | ||
| export function dbAnchorReplayStore(): AnchorReplayStore { | ||
| return { | ||
| async find(chainId, txHash) { | ||
| const { data, error } = await db | ||
| .from('payment_anchor_claims') | ||
| .select('contract_id') | ||
| .eq('chain_id', chainId) | ||
| .eq('tx_hash', txHash.toLowerCase()) | ||
| .maybeSingle(); | ||
| if (error) throw new Error('payment_anchor_claims read failed'); | ||
| return data ? { contract_id: String((data as any).contract_id) } : null; | ||
| }, | ||
| async bind(row: AnchorBindingRow) { | ||
| const { error } = await db.from('payment_anchor_claims').insert(row); | ||
| if (!error) return 'ok'; | ||
| if (String(error.code) === '23505') return 'duplicate'; | ||
| throw new Error('payment_anchor_claims write failed'); | ||
| }, | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 Payment-anchor verification drops the payer and contract-tx binding for a contract whose recorded settlement is simulated
Severity: LOW
TL;DR: For a contract whose settlement row is
is_simulated,dbAnchorTermsreturns no recorded tx and no payer, soverifyPaymentAnchorskips the tx binding and credits any sender, answering VERIFIED for a transfer that never paid for this contract.Impact: Once the draft replay table is applied and
enforceships, an agent can anchor a contract it never paid for using another buyer's real transfer to the same provider, consuming that tx so its rightful contract can no longer verify. In shadow mode no score moves.Root cause:
is_simulatedflag rather than from whether the contract has a real payment, so the one row that records "no money moved" is treated as carrying no binding information instead of as a reason to refuse.payment-anchor-db.ts:59-60,73nullsrecordedTxHashand gatespayerAddresson it, sopayment-anchor-verify.ts:271never compares the anchor against the contract's own tx and:315-317falls back to summing every sender's inflow in the receipt.Prompt to fix with AI
React 👍 / 👎 to tune Strix for this repo. A repo collaborator (or the PR author) can resolve this thread to dismiss the finding.