-
Notifications
You must be signed in to change notification settings - Fork 137
docs(rfc): preserve payment grants through 402 challenges #215
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
aadopii
wants to merge
2
commits into
agentcommercekit:ack-id-core-rfc
Choose a base branch
from
aadopii:docs/v2-single-use-payment-grants
base: ack-id-core-rfc
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.
Open
Changes from all commits
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| name: Check RFC | ||
|
|
||
| on: | ||
| push: | ||
| paths: | ||
| - "docs/ack-id/rfc/**" | ||
| - "docs/ack-pay/rfc/**" | ||
| - "docs/package.json" | ||
| - "docs/vitest.config.ts" | ||
| - ".github/workflows/check-rfc.yaml" | ||
| pull_request: | ||
| branches: | ||
| - ack-id-core-rfc | ||
| paths: | ||
| - "docs/ack-id/rfc/**" | ||
| - "docs/ack-pay/rfc/**" | ||
| - "docs/package.json" | ||
| - "docs/vitest.config.ts" | ||
| - ".github/workflows/check-rfc.yaml" | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| check: | ||
| runs-on: ubuntu-latest | ||
| env: | ||
| ANTHROPIC_API_KEY: secret | ||
| ISSUER_PRIVATE_KEY: "0xa45f5c566918ef954e8c200a96b14092cabcd69cb8a1a132804a2b8cbb8489a1" | ||
| VERIFIER_PRIVATE_KEY: "0xeeca8f89b2f5196126f7d9199e739153bd43a13f9cdd1099e7191a33143a2059" | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: ./.github/actions/setup | ||
| - run: pnpm run build | ||
| - run: pnpm run check | ||
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,17 @@ | ||
| # Single-use payment grant lifecycle examples | ||
|
|
||
| Run from the repository root after installing its pinned dependencies: | ||
|
|
||
| ```sh | ||
| pnpm --filter @docs/agentcommercekit test | ||
| ``` | ||
|
|
||
| These executable examples specify the ordering in ACK-Pay core Section 4.1. | ||
| Request/grant and payment validation are explicit inputs; the examples do | ||
| not implement cryptography or verify x402 payloads. They cover the unpaid | ||
| challenge, paid retry, concurrent requests, and uncertain execution outcome. | ||
|
|
||
| The in-memory claim is atomic within one process only. A production adapter | ||
| needs durable atomic claims shared across the RP's acceptance domain and | ||
| rail-specific recovery. The examples do not test distributed storage, | ||
| settlement, record-retention deadlines, or exactly-once execution. |
145 changes: 145 additions & 0 deletions
145
docs/ack-pay/rfc/vectors/payment-grant-lifecycle.test.ts
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,145 @@ | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| // Ordering examples, not an ACK verifier or a settlement implementation. | ||
| // Inputs stand for the results of fresh request/grant and payment validation. | ||
| type Grant = { iss: string; jti: string } | ||
| type Attempt = { | ||
| grant: Grant | ||
| authorized: boolean | ||
| payment: "absent" | "invalid" | "valid" | ||
| execute?: () => Promise<void> | ||
| } | ||
|
|
||
| function createMerchant() { | ||
| const records = new Map<string, "started" | "completed" | "unknown">() | ||
| let started = 0 | ||
|
|
||
| async function handle(attempt: Attempt) { | ||
| if (!attempt.authorized) { | ||
| return "unauthorized" | ||
| } | ||
| if (attempt.payment === "absent") { | ||
| return "challenge" | ||
| } | ||
| if (attempt.payment === "invalid") { | ||
| return "invalid-payment" | ||
| } | ||
|
|
||
| // The identity comes from the validated grant, not the payment payload. | ||
| const key = JSON.stringify([attempt.grant.iss, attempt.grant.jti]) | ||
| // Synchronous claim-before-await is atomic only within this model. | ||
| // Real adapters need a durable atomic store shared by all RP replicas. | ||
| if (records.has(key)) { | ||
| return "already-claimed" | ||
| } | ||
| records.set(key, "started") | ||
| started++ | ||
| try { | ||
| await attempt.execute?.() | ||
| records.set(key, "completed") | ||
| return "completed" | ||
| } catch { | ||
| records.set(key, "unknown") | ||
| return "unknown" | ||
| } | ||
| } | ||
|
|
||
| return { handle, started: () => started } | ||
| } | ||
|
|
||
| const grant = { iss: "did:web:owner.example", jti: "purchase-1" } | ||
| const unpaid: Attempt = { grant, authorized: true, payment: "absent" } | ||
| const paid: Attempt = { grant, authorized: true, payment: "valid" } | ||
|
|
||
| describe("single-use payment grant lifecycle", () => { | ||
| it("preserves a checked grant through the challenge and paid retry", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle(unpaid)).toBe("challenge") | ||
| expect(merchant.started()).toBe(0) | ||
| expect(await merchant.handle(paid)).toBe("completed") | ||
| expect(merchant.started()).toBe(1) | ||
| }) | ||
|
|
||
| it("preserves the grant through repeated challenge-only requests", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle(unpaid)).toBe("challenge") | ||
| expect(await merchant.handle(unpaid)).toBe("challenge") | ||
| expect(await merchant.handle(paid)).toBe("completed") | ||
| expect(merchant.started()).toBe(1) | ||
| }) | ||
|
|
||
| it("rejects an unauthorized attempt before redeeming the grant", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle({ ...paid, authorized: false })).toBe( | ||
| "unauthorized", | ||
| ) | ||
| expect(merchant.started()).toBe(0) | ||
| expect(await merchant.handle(paid)).toBe("completed") | ||
| }) | ||
|
|
||
| it("rejects invalid payment evidence before redeeming the grant", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle({ ...paid, payment: "invalid" })).toBe( | ||
| "invalid-payment", | ||
| ) | ||
| expect(merchant.started()).toBe(0) | ||
| expect(await merchant.handle(paid)).toBe("completed") | ||
| }) | ||
|
|
||
| it("uses the paid retry's current authorization result", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle(unpaid)).toBe("challenge") | ||
| // For example, the grant expired or was revoked after the challenge. | ||
| expect(await merchant.handle({ ...paid, authorized: false })).toBe( | ||
| "unauthorized", | ||
| ) | ||
| expect(merchant.started()).toBe(0) | ||
| }) | ||
|
|
||
| it("allows only one concurrent paid request to begin execution", async () => { | ||
| const merchant = createMerchant() | ||
| let release: (() => void) | undefined | ||
| const pending = new Promise<void>((resolve) => { | ||
| release = resolve | ||
| }) | ||
| const first = merchant.handle({ ...paid, execute: () => pending }) | ||
| try { | ||
| expect(await merchant.handle(paid)).toBe("already-claimed") | ||
| expect(merchant.started()).toBe(1) | ||
| } finally { | ||
| release?.() | ||
| } | ||
| expect(await first).toBe("completed") | ||
| }) | ||
|
|
||
| it("keeps the claim when a submitted operation has an uncertain outcome", async () => { | ||
| const merchant = createMerchant() | ||
| expect( | ||
| await merchant.handle({ | ||
| ...paid, | ||
| execute: () => Promise.reject(new Error("settlement timed out")), | ||
| }), | ||
| ).toBe("unknown") | ||
| expect(await merchant.handle(paid)).toBe("already-claimed") | ||
| expect(merchant.started()).toBe(1) | ||
| }) | ||
|
|
||
| it("rejects another paid operation after completion", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle(paid)).toBe("completed") | ||
| expect(await merchant.handle(paid)).toBe("already-claimed") | ||
| expect(merchant.started()).toBe(1) | ||
| }) | ||
|
|
||
| it("keeps grants with the same jti from different issuers independent", async () => { | ||
| const merchant = createMerchant() | ||
| expect(await merchant.handle(paid)).toBe("completed") | ||
| expect( | ||
| await merchant.handle({ | ||
| ...paid, | ||
| grant: { ...grant, iss: "did:web:another-owner.example" }, | ||
| }), | ||
| ).toBe("completed") | ||
| expect(merchant.started()).toBe(2) | ||
| }) | ||
| }) |
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,8 @@ | ||
| import { defineConfig } from "vitest/config" | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| include: ["ack-pay/rfc/vectors/**/*.test.ts"], | ||
| watch: false, | ||
| }, | ||
| }) |
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.
Private keys are being shown