-
Notifications
You must be signed in to change notification settings - Fork 109
demo(payments): enforce policy before signing #97
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
EfeDurmaz16
wants to merge
6
commits into
agentcommercekit:main
Choose a base branch
from
EfeDurmaz16:demo/ack-pay-policy-before-signing
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
47e59db
demo(payments): enforce policy before signing
EfeDurmaz16 02014d3
fix(demo): use configured payment recipient allowlist
EfeDurmaz16 790ad42
fix(demo): use BigInt and per-currency limits in payment policy
EfeDurmaz16 734f40a
refactor(demo): make demoPaymentPolicy the single policy source
EfeDurmaz16 4cfc2e7
docs(demo): clarify per-transaction cap is not a real spend control
EfeDurmaz16 783fe1a
fix(demo): reject prototype-key currencies in payment policy
EfeDurmaz16 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| import { evaluatePaymentPolicy } from "./payment-policy" | ||
|
|
||
| const basePaymentOption = { | ||
| id: "base-usdc", | ||
| amount: 100, | ||
| decimals: 6, | ||
| currency: "USDC", | ||
| recipient: "did:example:merchant", | ||
| } | ||
|
|
||
| describe("evaluatePaymentPolicy", () => { | ||
| it("approves below-threshold payments to allowed recipients", () => { | ||
| const decision = evaluatePaymentPolicy(basePaymentOption, { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "approved", | ||
| }) | ||
| }) | ||
|
|
||
| it("approves string subunit amounts within the limit", () => { | ||
| const decision = evaluatePaymentPolicy( | ||
| { ...basePaymentOption, amount: "50000" }, | ||
| { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 5_000_000n }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "approved", | ||
| }) | ||
| }) | ||
|
|
||
| it("does not approve self-asserted recipients without an allowlist", () => { | ||
| const decision = evaluatePaymentPolicy(basePaymentOption, { | ||
| allowedRecipients: [], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "approval_required", | ||
| reason: "Recipient is not on the autonomous payment allowlist", | ||
| }) | ||
| }) | ||
|
|
||
| it("requires approval before execution for unknown recipients", () => { | ||
| const decision = evaluatePaymentPolicy(basePaymentOption, { | ||
| allowedRecipients: ["did:example:trusted-merchant"], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "approval_required", | ||
| reason: "Recipient is not on the autonomous payment allowlist", | ||
| }) | ||
| }) | ||
|
|
||
| it("denies payments above the autonomous spend limit", () => { | ||
| const decision = evaluatePaymentPolicy( | ||
| { | ||
| ...basePaymentOption, | ||
| amount: 10_000, | ||
| }, | ||
| { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "denied", | ||
| reason: "Payment amount exceeds the autonomous spend limit", | ||
| }) | ||
| }) | ||
|
|
||
| it("applies the per-currency limit in the currency's own subunits", () => { | ||
| const policy = { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| // 5.00 USD (2dp) and 5.000000 USDC (6dp) — same value, different subunits | ||
| maxAutonomousAmount: { USD: 500n, USDC: 5_000_000n }, | ||
| } | ||
|
|
||
| // 4.00 USD is below the USD limit | ||
| expect( | ||
| evaluatePaymentPolicy( | ||
| { ...basePaymentOption, amount: 400, decimals: 2, currency: "USD" }, | ||
| policy, | ||
| ), | ||
| ).toEqual({ status: "approved" }) | ||
|
|
||
| // The same 400 subunits in USDC (0.0004) is also below the USDC limit, | ||
| // confirming each currency is bounded by its own threshold | ||
| expect( | ||
| evaluatePaymentPolicy({ ...basePaymentOption, amount: 400 }, policy), | ||
| ).toEqual({ status: "approved" }) | ||
| }) | ||
|
|
||
| it("denies currencies with no configured limit", () => { | ||
| const decision = evaluatePaymentPolicy( | ||
| { ...basePaymentOption, currency: "SOL", decimals: 9 }, | ||
| { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "denied", | ||
| reason: "No autonomous spend limit configured for currency SOL", | ||
| }) | ||
| }) | ||
|
|
||
| it("denies non-positive amounts", () => { | ||
| const decision = evaluatePaymentPolicy( | ||
| { ...basePaymentOption, amount: 0 }, | ||
| { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "denied", | ||
| reason: "Payment amount must be greater than zero", | ||
| }) | ||
| }) | ||
|
|
||
| it("denies currencies matching inherited prototype keys", () => { | ||
| const decision = evaluatePaymentPolicy( | ||
| { ...basePaymentOption, currency: "constructor" }, | ||
| { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "denied", | ||
| reason: "No autonomous spend limit configured for currency constructor", | ||
| }) | ||
| }) | ||
|
|
||
| it("denies fractional or malformed amounts the schema permits as strings", () => { | ||
| const decision = evaluatePaymentPolicy( | ||
| { ...basePaymentOption, amount: "1.5" }, | ||
| { | ||
| allowedRecipients: [basePaymentOption.recipient], | ||
| maxAutonomousAmount: { USDC: 1_000n }, | ||
| }, | ||
| ) | ||
|
|
||
| expect(decision).toEqual({ | ||
| status: "denied", | ||
| reason: "Payment amount must be a positive integer in subunits", | ||
| }) | ||
| }) | ||
| }) | ||
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,94 @@ | ||
| import type { PaymentOption } from "agentcommercekit" | ||
|
|
||
| export type PaymentPolicyDecision = | ||
| | { | ||
| status: "approved" | ||
| } | ||
| | { | ||
| status: "approval_required" | "denied" | ||
| reason: string | ||
| } | ||
|
|
||
| export interface PaymentPolicy { | ||
| allowedRecipients: readonly string[] | ||
| /** | ||
| * Per-transaction autonomous spend limits, expressed in each currency's | ||
| * smallest subunit (matching `PaymentOption.amount`) and keyed by currency | ||
| * code. Keeping the limit per-currency avoids comparing a single flat | ||
| * threshold across currencies with different decimals (e.g. USD at 2dp vs | ||
| * USDC at 6dp). A currency with no configured limit is denied. | ||
| * | ||
| * NOTE: this is a per-transaction cap only, not a cumulative or rate budget. | ||
| * See the demo README — a real spend control needs windowed/cumulative | ||
| * limits, since a per-transaction cap is trivially split-gameable. | ||
| */ | ||
| maxAutonomousAmount: Readonly<Record<string, bigint>> | ||
| } | ||
|
|
||
| export const demoPaymentPolicy: PaymentPolicy = { | ||
| allowedRecipients: [], | ||
| maxAutonomousAmount: { | ||
| // 5.00 USD (2 decimals) and 5.000000 USDC (6 decimals) | ||
| USD: 500n, | ||
| USDC: 5_000_000n, | ||
| }, | ||
| } | ||
|
|
||
| export function evaluatePaymentPolicy( | ||
| paymentOption: PaymentOption, | ||
| policy: PaymentPolicy = demoPaymentPolicy, | ||
| ): PaymentPolicyDecision { | ||
| let amount: bigint | ||
| try { | ||
| // Follows the repo-wide BigInt money convention (see receipt-service.ts, | ||
| // index.ts). `BigInt()` throws on fractional/malformed amounts the | ||
| // ACK-Pay schema's string branch otherwise permits. | ||
| amount = BigInt(paymentOption.amount) | ||
| } catch { | ||
| return { | ||
| status: "denied", | ||
| reason: "Payment amount must be a positive integer in subunits", | ||
| } | ||
| } | ||
|
|
||
| if (amount <= 0n) { | ||
| return { | ||
| status: "denied", | ||
| reason: "Payment amount must be greater than zero", | ||
| } | ||
| } | ||
|
|
||
| // `currency` is an unconstrained wire string, so guard against inherited | ||
| // prototype keys (e.g. "constructor", "toString") that would otherwise | ||
| // resolve to a non-bigint value and slip past the comparison below. | ||
| const limit = Object.prototype.hasOwnProperty.call( | ||
| policy.maxAutonomousAmount, | ||
| paymentOption.currency, | ||
| ) | ||
| ? policy.maxAutonomousAmount[paymentOption.currency] | ||
| : undefined | ||
| if (typeof limit !== "bigint") { | ||
| return { | ||
| status: "denied", | ||
| reason: `No autonomous spend limit configured for currency ${paymentOption.currency}`, | ||
| } | ||
| } | ||
|
|
||
| if (amount > limit) { | ||
| return { | ||
| status: "denied", | ||
| reason: "Payment amount exceeds the autonomous spend limit", | ||
| } | ||
| } | ||
|
|
||
| if (!policy.allowedRecipients.includes(paymentOption.recipient)) { | ||
| return { | ||
| status: "approval_required", | ||
| reason: "Recipient is not on the autonomous payment allowlist", | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| status: "approved", | ||
| } | ||
| } |
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
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.
Rename test cases to the required assertive pattern.
Current
it(...)titles don’t follow the enforcedcreates/throws/requires/returnsnaming convention for*.test.tsfiles. Please rename these test names accordingly (for example,it("returns approved decision..."),it("requires approval..."),it("returns denied decision...")).As per coding guidelines:
Use assertive test names with patterns: "it("creates...")" , "it("throws...")" , "it("requires...")" , "it("returns...")".🤖 Prompt for AI Agents