Skip to content
Open
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
42 changes: 42 additions & 0 deletions src/__tests__/validate-payment-requirements.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest"
import {
type PaymentRequirements,
validatePaymentRequirements,
} from "../lib/client/x402-payment.js"

const base: PaymentRequirements = {
scheme: "exact",
network: "base",
maxAmountRequired: "1000000",
payTo: "0x1111111111111111111111111111111111111111",
// USDC on Base; validatePaymentRequirements rejects any other asset when
// allowedAssets is not provided.
asset: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
}

describe("validatePaymentRequirements", () => {
it("passes for a well-formed requirement", () => {
expect(() => validatePaymentRequirements(base, {})).not.toThrow()
})

it("rejects a burn/zero payTo address", () => {
expect(() =>
validatePaymentRequirements(
{ ...base, payTo: "0x0000000000000000000000000000000000000000" },
{},
),
).toThrow(/burn\/zero address/)
})

it("rejects when the server amount exceeds maxAmount", () => {
expect(() =>
validatePaymentRequirements(base, { maxAmount: "999999" }),
).toThrow(/maxAmount/)
})

it("passes when the server amount is within maxAmount", () => {
expect(() =>
validatePaymentRequirements(base, { maxAmount: "1000000" }),
).not.toThrow()
})
})
24 changes: 22 additions & 2 deletions src/cli/commands/pay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Command } from "commander"
import pc from "picocolors"
import type { PaymentRequirements } from "../../lib/client/x402-payment.js"
import { signX402Payment } from "../../lib/client/x402-payment.js"
import {
signX402Payment,
validatePaymentRequirements,
} from "../../lib/client/x402-payment.js"
import {
createWalletForProvider,
createWalletFromEnv,
Expand All @@ -14,6 +17,7 @@ import { readInput } from "./read-input.js"
interface PayOptions {
body?: string
walletProvider?: string
maxAmount?: string
}

export const payCommand = new Command("pay")
Expand All @@ -26,6 +30,10 @@ export const payCommand = new Command("pay")
"--wallet-provider <provider>",
`Wallet provider: ${WALLET_PROVIDERS.join(", ")}`,
)
.option(
"--max-amount <atomic>",
"Maximum payment amount in atomic units the CLI will sign; the server's 402 requirements are rejected if they exceed it",
)
.action(async (url: string, options: PayOptions) => {
let adapter: WalletAdapter
try {
Expand Down Expand Up @@ -65,13 +73,14 @@ export const payCommand = new Command("pay")
process.exit(1)
}

await runPaymentOnly(url, inputBody, adapter)
await runPaymentOnly(url, inputBody, adapter, options.maxAmount)
})

async function runPaymentOnly(
url: string,
inputBody: string,
adapter: WalletAdapter,
maxAmount?: string,
): Promise<void> {
console.log(pc.cyan("Probing endpoint for payment requirements..."))

Expand Down Expand Up @@ -125,6 +134,17 @@ async function runPaymentOnly(
console.log(` Pay To: ${requirements.payTo}`)
console.log(` Asset: ${requirements.asset}`)

try {
validatePaymentRequirements(requirements, { maxAmount })
} catch (err) {
console.error(
pc.red(
`Refusing to sign: ${err instanceof Error ? err.message : String(err)}`,
),
)
process.exit(1)
}

console.log(pc.cyan("\nSigning EIP-3009 transferWithAuthorization..."))

const xPayment = await signX402Payment({
Expand Down
2 changes: 1 addition & 1 deletion src/lib/client/x402-payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export async function paidFetch(
return paidRes
}

function validatePaymentRequirements(
export function validatePaymentRequirements(
reqs: PaymentRequirements,
opts: {
maxAmount?: string
Expand Down