@agent-score/commerce 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{_response-DmziuJz6.d.mts → _response-DS-LR590.d.mts} +13 -8
- package/dist/{_response-rbK0zM7y.d.ts → _response-RpEB7-vl.d.ts} +13 -8
- package/dist/challenge/index.js.map +1 -1
- package/dist/challenge/index.mjs.map +1 -1
- package/dist/core.js +86 -67
- package/dist/core.js.map +1 -1
- package/dist/core.mjs +86 -67
- package/dist/core.mjs.map +1 -1
- package/dist/discovery/index.js +3 -3
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +3 -3
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/identity/express.d.mts +1 -1
- package/dist/identity/express.d.ts +1 -1
- package/dist/identity/express.js +120 -71
- package/dist/identity/express.js.map +1 -1
- package/dist/identity/express.mjs +120 -71
- package/dist/identity/express.mjs.map +1 -1
- package/dist/identity/fastify.d.mts +1 -1
- package/dist/identity/fastify.d.ts +1 -1
- package/dist/identity/fastify.js +120 -71
- package/dist/identity/fastify.js.map +1 -1
- package/dist/identity/fastify.mjs +120 -71
- package/dist/identity/fastify.mjs.map +1 -1
- package/dist/identity/hono.d.mts +1 -1
- package/dist/identity/hono.d.ts +1 -1
- package/dist/identity/hono.js +120 -71
- package/dist/identity/hono.js.map +1 -1
- package/dist/identity/hono.mjs +120 -71
- package/dist/identity/hono.mjs.map +1 -1
- package/dist/identity/nextjs.d.mts +1 -1
- package/dist/identity/nextjs.d.ts +1 -1
- package/dist/identity/nextjs.js +120 -71
- package/dist/identity/nextjs.js.map +1 -1
- package/dist/identity/nextjs.mjs +120 -71
- package/dist/identity/nextjs.mjs.map +1 -1
- package/dist/identity/policy.d.mts +1 -2
- package/dist/identity/policy.d.ts +1 -2
- package/dist/identity/policy.js.map +1 -1
- package/dist/identity/policy.mjs.map +1 -1
- package/dist/identity/web.d.mts +1 -1
- package/dist/identity/web.d.ts +1 -1
- package/dist/identity/web.js +120 -71
- package/dist/identity/web.js.map +1 -1
- package/dist/identity/web.mjs +120 -71
- package/dist/identity/web.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +122 -81
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -81
- package/dist/index.mjs.map +1 -1
- package/dist/stripe-multichain/index.d.mts +3 -3
- package/dist/stripe-multichain/index.d.ts +3 -3
- package/dist/stripe-multichain/index.js.map +1 -1
- package/dist/stripe-multichain/index.mjs.map +1 -1
- package/package.json +5 -5
|
@@ -21,18 +21,23 @@ import { VerifyWalletSignerResult, DenialReason } from './core.mjs';
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* Compliance denial reasons that can be resolved by re-completing KYC.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* Compliance denial reasons that can be resolved by re-completing KYC. The API emits these
|
|
25
|
+
* when KYC is missing/pending/failed; the user can re-verify and retry.
|
|
26
|
+
*
|
|
27
|
+
* `jurisdiction_restricted` is NOT in this set — the API only emits it AFTER KYC is verified,
|
|
28
|
+
* meaning the user's KYC'd country is in the merchant's blocked list (or absent from the
|
|
29
|
+
* allowed list). Re-doing KYC won't change the country, so it's permanent. Same shape as
|
|
30
|
+
* `sanctions_flagged` and `age_insufficient` — surface contact_support, don't waste a
|
|
31
|
+
* /v1/sessions mint.
|
|
27
32
|
*/
|
|
28
33
|
declare const FIXABLE_DENIAL_REASONS: ReadonlySet<string>;
|
|
29
34
|
/**
|
|
30
|
-
* Returns true when a `wallet_not_trusted` denial's reasons are all
|
|
31
|
-
* re-
|
|
35
|
+
* Returns true when a `wallet_not_trusted` denial's reasons are all fixable via KYC
|
|
36
|
+
* re-verification. False when any reason is permanent (sanctions, age, jurisdiction_restricted).
|
|
32
37
|
*
|
|
33
|
-
* Empty reasons
|
|
34
|
-
*
|
|
35
|
-
*
|
|
38
|
+
* Empty reasons returns false — without a known reason we can't promise a fix, so default to
|
|
39
|
+
* the bare denial path (vendors can override via custom onDenied if they want different
|
|
40
|
+
* behavior on empty reasons).
|
|
36
41
|
*/
|
|
37
42
|
declare function isFixableDenial(reasons: readonly string[] | undefined): boolean;
|
|
38
43
|
/**
|
|
@@ -21,18 +21,23 @@ import { VerifyWalletSignerResult, DenialReason } from './core.js';
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* Compliance denial reasons that can be resolved by re-completing KYC.
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* Compliance denial reasons that can be resolved by re-completing KYC. The API emits these
|
|
25
|
+
* when KYC is missing/pending/failed; the user can re-verify and retry.
|
|
26
|
+
*
|
|
27
|
+
* `jurisdiction_restricted` is NOT in this set — the API only emits it AFTER KYC is verified,
|
|
28
|
+
* meaning the user's KYC'd country is in the merchant's blocked list (or absent from the
|
|
29
|
+
* allowed list). Re-doing KYC won't change the country, so it's permanent. Same shape as
|
|
30
|
+
* `sanctions_flagged` and `age_insufficient` — surface contact_support, don't waste a
|
|
31
|
+
* /v1/sessions mint.
|
|
27
32
|
*/
|
|
28
33
|
declare const FIXABLE_DENIAL_REASONS: ReadonlySet<string>;
|
|
29
34
|
/**
|
|
30
|
-
* Returns true when a `wallet_not_trusted` denial's reasons are all
|
|
31
|
-
* re-
|
|
35
|
+
* Returns true when a `wallet_not_trusted` denial's reasons are all fixable via KYC
|
|
36
|
+
* re-verification. False when any reason is permanent (sanctions, age, jurisdiction_restricted).
|
|
32
37
|
*
|
|
33
|
-
* Empty reasons
|
|
34
|
-
*
|
|
35
|
-
*
|
|
38
|
+
* Empty reasons returns false — without a known reason we can't promise a fix, so default to
|
|
39
|
+
* the bare denial path (vendors can override via custom onDenied if they want different
|
|
40
|
+
* behavior on empty reasons).
|
|
36
41
|
*/
|
|
37
42
|
declare function isFixableDenial(reasons: readonly string[] | undefined): boolean;
|
|
38
43
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/challenge/index.ts","../../src/challenge/accepted_methods.ts","../../src/challenge/identity.ts","../../src/challenge/how_to_pay.ts","../../src/challenge/agent_instructions.ts","../../src/core.ts","../../src/challenge/agent_memory.ts","../../src/challenge/body.ts","../../src/challenge/pricing.ts","../../src/payment/wwwauthenticate.ts","../../src/challenge/respond_402.ts","../../src/challenge/validation_error.ts"],"sourcesContent":["export * from './accepted_methods';\nexport * from './identity';\nexport * from './how_to_pay';\nexport * from './agent_instructions';\nexport * from './agent_memory';\nexport * from './body';\nexport * from './pricing';\nexport * from './order_receipt';\nexport * from './respond_402';\nexport * from './validation_error';\n","export interface TempoMethodEntry {\n method: 'tempo/charge';\n network: string;\n chain_id: number;\n token: string;\n symbol: string;\n decimals: number;\n pay_to: string;\n}\n\nexport interface X402MethodEntry {\n method: 'x402/exact';\n network: string;\n chain_id?: number;\n token: string;\n symbol: string;\n decimals: number;\n pay_to: string;\n}\n\nexport interface StripeMethodEntry {\n method: 'stripe/charge';\n rails: ('card' | 'link' | 'shared_payment_token')[];\n profile_id: string | null;\n}\n\nexport type AcceptedMethodEntry = TempoMethodEntry | X402MethodEntry | StripeMethodEntry;\n\nexport interface BuildAcceptedMethodsInput {\n tempo?: {\n recipient: string;\n network?: string;\n chainId?: number;\n token?: string;\n symbol?: string;\n decimals?: number;\n };\n x402_base?: {\n recipient: string;\n network?: string;\n chainId?: number;\n token?: string;\n symbol?: string;\n decimals?: number;\n };\n x402_solana?: {\n recipient: string;\n network?: string;\n token?: string;\n symbol?: string;\n decimals?: number;\n };\n stripe?: {\n profileId?: string | null;\n rails?: ('card' | 'link' | 'shared_payment_token')[];\n };\n}\n\n/**\n * Build the `accepted_methods[]` array for an enriched 402 body. Each rail entry is\n * conditionally included based on whether the vendor passed it. Per-rail shapes follow\n * the conventions established in martin-estate's reference 402.\n */\nexport function buildAcceptedMethods(input: BuildAcceptedMethodsInput): AcceptedMethodEntry[] {\n const out: AcceptedMethodEntry[] = [];\n\n if (input.tempo) {\n out.push({\n method: 'tempo/charge',\n network: input.tempo.network ?? 'tempo-mainnet',\n chain_id: input.tempo.chainId ?? 4217,\n token: input.tempo.token ?? '0x20C000000000000000000000b9537d11c60E8b50',\n symbol: input.tempo.symbol ?? 'USDC.e',\n decimals: input.tempo.decimals ?? 6,\n pay_to: input.tempo.recipient,\n });\n }\n\n if (input.x402_base) {\n out.push({\n method: 'x402/exact',\n network: input.x402_base.network ?? 'eip155:8453',\n chain_id: input.x402_base.chainId ?? 8453,\n token: input.x402_base.token ?? '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',\n symbol: input.x402_base.symbol ?? 'USDC',\n decimals: input.x402_base.decimals ?? 6,\n pay_to: input.x402_base.recipient,\n });\n }\n\n if (input.x402_solana) {\n out.push({\n method: 'x402/exact',\n network: input.x402_solana.network ?? 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',\n token: input.x402_solana.token ?? 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',\n symbol: input.x402_solana.symbol ?? 'USDC',\n decimals: input.x402_solana.decimals ?? 6,\n pay_to: input.x402_solana.recipient,\n });\n }\n\n if (input.stripe) {\n out.push({\n method: 'stripe/charge',\n rails: input.stripe.rails ?? ['card', 'link', 'shared_payment_token'],\n profile_id: input.stripe.profileId ?? null,\n });\n }\n\n return out;\n}\n","export type IdentityMode = 'wallet' | 'operator_token';\n\nexport interface SignerMatchResultLike {\n kind: 'pass' | 'wallet_signer_mismatch' | 'wallet_auth_requires_wallet_signing' | string;\n expectedSigner?: string;\n actualSigner?: string;\n linkedWallets?: string[];\n}\n\nexport interface IdentityMetadataInput {\n /** Current request's identity mode. */\n mode: IdentityMode;\n /** Claimed wallet address (when mode === 'wallet'). */\n wallet?: string;\n /** Result of a prior verifyWalletSignerMatch call. */\n signerMatchResult?: SignerMatchResultLike;\n /** Same-operator linked wallets (from assess response). */\n linkedWallets?: string[];\n /** Optional explicit constraint description (overrides the auto-generated one). */\n signerConstraint?: string;\n}\n\nexport interface IdentityMetadataBlock {\n identity_mode: IdentityMode;\n required_signer?: string;\n linked_wallets?: string[];\n signer_constraint?: string;\n}\n\n/**\n * Build the identity-metadata block for an enriched 402 body. Echoes the agent's\n * identity context (wallet vs. operator-token mode) so the agent can self-correct\n * before signing — specifically, on wallet-auth rails the agent MUST sign with one\n * of the wallets in linked_wallets (all resolve to the same operator).\n */\nexport function buildIdentityMetadata(input: IdentityMetadataInput): IdentityMetadataBlock {\n const block: IdentityMetadataBlock = { identity_mode: input.mode };\n\n if (input.mode !== 'wallet') return block;\n\n if (input.wallet) {\n block.required_signer = input.signerMatchResult?.expectedSigner ?? input.wallet;\n }\n if (input.linkedWallets && input.linkedWallets.length > 0) {\n block.linked_wallets = input.linkedWallets;\n }\n block.signer_constraint =\n input.signerConstraint ??\n 'Payment must be signed with the claimed wallet OR any same-operator linked wallet listed in linked_wallets.';\n\n return block;\n}\n","export interface HowToPayRailEntry {\n setup?: string[];\n prerequisite?: string;\n command: string;\n alternative_command?: string;\n what_it_does: string;\n}\n\nexport interface HowToPayStripeEntry {\n prerequisite: string;\n instructions: string;\n setup_link_cli?: string[];\n command_link_cli?: string[];\n what_it_does_link_cli?: string;\n note?: string;\n}\n\nexport interface HowToPayBlock {\n tempo?: HowToPayRailEntry;\n x402_base?: HowToPayRailEntry;\n x402_solana?: HowToPayRailEntry;\n stripe?: HowToPayStripeEntry;\n}\n\nexport interface BuildHowToPayInput {\n /** The merchant's full URL (e.g., 'https://agents.merchant.example/api/buy'). */\n url: string;\n /** JSON string of the body the agent should retry with — typically the original request body. */\n retryBodyJson: string;\n /** Total amount in USD (string or number). Used to compute max-spend defaults and stripe context. */\n totalUsd: string | number;\n /** Per-rail config — each is optional. Pass only the rails you support. */\n rails: {\n tempo?: { recipient: string; networkName?: string; chainId?: number; recommend?: 'tempo' | 'agentscore-pay' | 'both' };\n x402_base?: { recipient: string; network?: string };\n x402_solana?: { recipient: string; network?: string };\n stripe?: { profileId?: string | null; productName?: string };\n };\n /** Placeholder text for the operator token in commands. Defaults to '<your_opc_token>'. */\n opTokenPlaceholder?: string;\n /** Override max-spend value used in commands. Default: ceil(totalUsd) + 1. */\n maxSpend?: string | number;\n}\n\nconst TEMPO_SETUP = [\n 'curl -fsSL https://tempo.xyz/install | bash',\n 'tempo wallet login',\n 'tempo wallet whoami',\n 'tempo wallet fund # if balance is zero',\n];\n\nconst PAY_SETUP_BASE = [\n 'npm install -g @agent-score/pay # or: brew install agentscore/tap/agentscore-pay',\n 'agentscore-pay wallet create --chain base',\n 'agentscore-pay balance --chain base # fund the printed address with USDC on Base',\n];\n\nconst PAY_SETUP_SOLANA = [\n 'npm install -g @agent-score/pay # or: brew install agentscore/tap/agentscore-pay',\n 'agentscore-pay wallet create --chain solana',\n 'agentscore-pay balance --chain solana # fund the printed address with USDC on Solana',\n];\n\n/**\n * Build the agent_instructions.how_to_pay block. Generates per-rail setup/command/what_it_does\n * boilerplate so agents see concrete commands per rail in the 402 body. Vendors pass the rails\n * they support; the helper produces the right command for each.\n *\n * Tool recommendations (tempo CLI vs agentscore-pay vs link-cli) are configurable per rail.\n */\nexport function buildHowToPay(input: BuildHowToPayInput): HowToPayBlock {\n const totalNum = typeof input.totalUsd === 'string' ? Number(input.totalUsd) : input.totalUsd;\n const maxSpend = String(input.maxSpend ?? (Math.ceil(totalNum) + 1).toFixed(2));\n const opToken = input.opTokenPlaceholder ?? '<your_opc_token>';\n const block: HowToPayBlock = {};\n\n if (input.rails.tempo) {\n const networkName = input.rails.tempo.networkName ?? 'tempo-mainnet';\n const chainId = input.rails.tempo.chainId ?? 4217;\n const recommend = input.rails.tempo.recommend ?? 'both';\n const tempoCommand = `tempo request -X POST -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' --json '${input.retryBodyJson}' --max-spend ${maxSpend} ${input.url}`;\n const payCommand = `agentscore-pay pay POST ${input.url} --chain tempo -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' -d '${input.retryBodyJson}' --max-spend ${maxSpend}`;\n block.tempo = {\n setup: TEMPO_SETUP,\n prerequisite: `Run \\`tempo wallet whoami\\` and confirm USDC.e balance on ${networkName} (chain ${chainId}) is at least $${maxSpend}. If the tempo CLI is not installed, run the setup commands above first.`,\n command: recommend === 'agentscore-pay' ? payCommand : tempoCommand,\n ...(recommend === 'both'\n ? { alternative_command: payCommand }\n : recommend === 'agentscore-pay'\n ? { alternative_command: tempoCommand }\n : {}),\n what_it_does: `Hits this endpoint, receives this same 402, signs the MPP challenge on ${networkName}, and submits the credential back via Authorization: Payment. Either client (tempo request or agentscore-pay pay --chain tempo) works — both run the full MPP handshake.`,\n };\n }\n\n if (input.rails.x402_base) {\n const network = input.rails.x402_base.network ?? 'eip155:8453';\n block.x402_base = {\n setup: PAY_SETUP_BASE,\n prerequisite: `Run \\`agentscore-pay balance --chain base\\` and confirm USDC balance on Base (${network}) is at least $${maxSpend}. If the CLI is not installed, run the setup commands above first.`,\n command: `agentscore-pay pay POST ${input.url} --chain base -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' -d '${input.retryBodyJson}' --max-spend ${maxSpend}`,\n what_it_does:\n 'Hits this endpoint, receives this same 402, signs an EIP-3009 USDC TransferWithAuthorization on Base, submits via X-Payment header. Server verifies + settles via the Coinbase facilitator + returns 200 with the completed order.',\n };\n }\n\n if (input.rails.x402_solana) {\n const network = input.rails.x402_solana.network ?? 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n block.x402_solana = {\n setup: PAY_SETUP_SOLANA,\n prerequisite: `Run \\`agentscore-pay balance --chain solana\\` and confirm USDC balance on Solana (${network}) is at least $${maxSpend}. If the CLI is not installed, run the setup commands above first.`,\n command: `agentscore-pay pay POST ${input.url} --chain solana -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' -d '${input.retryBodyJson}' --max-spend ${maxSpend}`,\n what_it_does:\n 'Hits this endpoint, receives this same 402, signs an SPL Token TransferChecked transaction on Solana, submits via X-Payment header. Server verifies + settles via the Coinbase facilitator + returns 200 with the completed order.',\n };\n }\n\n if (input.rails.stripe) {\n const stripeCfg = input.rails.stripe;\n const amountCents = Math.round(totalNum * 100);\n const linkCliBlocked = amountCents > 50000;\n const productName = stripeCfg.productName ?? 'this purchase';\n const sptContext = `Purchasing \"${productName}\" via the agent commerce API. The user authorized this purchase through their AI agent for $${totalNum}; charge to be settled via shared payment token over the Machine Payments Protocol.`;\n const stripe: HowToPayStripeEntry = {\n prerequisite:\n 'Either your own Stripe account with Shared Payment Token acceptance, OR a Stripe Link wallet (any user with link.com).',\n instructions:\n 'Mint a SharedPaymentToken scoped to the profile_id advertised in accepted_methods, then submit via Authorization: Payment MPP header with method=stripe/charge.',\n };\n if (stripeCfg.profileId && !linkCliBlocked) {\n stripe.setup_link_cli = [\n 'npm install -g @stripe/link-cli # or use npx -y @stripe/link-cli for one-shot',\n 'link-cli auth login # one-time, opens your Link wallet',\n 'link-cli payment-methods list --output-json # copy a csmrpd_... id',\n ];\n stripe.command_link_cli = [\n `SPEND_ID=$(link-cli spend-request create --payment-method-id <csmrpd_id_from_payment_methods_list> --credential-type shared_payment_token --network-id ${stripeCfg.profileId} --amount ${amountCents} --context \"${sptContext}\" --request-approval --output-json | jq -r .id)`,\n `link-cli mpp pay ${input.url} --spend-request-id $SPEND_ID --method POST --data '${input.retryBodyJson}' --header 'X-Operator-Token: ${opToken}' --output-json`,\n ];\n stripe.what_it_does_link_cli =\n 'For users who have a Stripe Link wallet: step 1 mints a one-time-use SharedPaymentToken scoped to this purchase and pushes a notification to the user for approval (blocks until approved); step 2 submits the SPT via the MPP handshake along with your AgentScore operator credential.';\n } else if (linkCliBlocked) {\n stripe.note = `link-cli SPT path not available for this purchase — Stripe link-cli caps spend requests at $500.00 ($50000 cents); your total is $${totalNum}. Use your own Stripe account with the SharedPaymentToken API instead.`;\n }\n block.stripe = stripe;\n }\n\n return block;\n}\n","import type { HowToPayBlock } from './how_to_pay';\n\n/** Map of rail key (e.g. 'x402_base', 'tempo_mpp', 'stripe') → list of client identifiers\n * that have been smoke-verified by the merchant against the protocol shape they emit.\n * Strings are display labels, not install commands — agents already get install commands\n * via `how_to_pay.<rail>.setup`. Use these as a \"what's known to work\" hint. */\nexport type CompatibleClients = Record<string, string[]>;\n\nexport interface BuildAgentInstructionsInput {\n /** Per-rail commands. Build with `buildHowToPay`. */\n howToPay: HowToPayBlock;\n /** Tool recommendations as human-readable strings. Defaults to a sensible set covering tempo + agentscore-pay. */\n recommendedTools?: string[];\n /** Wallet-stack compatibility note for the agent. Default: rail-neutral, no specific wallet stack required. */\n walletCompatibility?: string;\n /** How long the merchant will wait for payment after the 402. Default 300 (5 minutes). */\n timeoutSeconds?: number;\n /** Warnings about common footguns. Defaults include tempo wallet transfer + raw on-chain x402 deposits. */\n warnings?: string[];\n /** Recommended rail (e.g., 'tempo', 'x402_base'). Surfaced for agents to default to. */\n recommended?: string;\n /** Per-rail list of client names the merchant has verified work end-to-end. Vendors set\n * this from their own smoke matrix — defaults to none (avoids vouching for clients the\n * merchant has not tested). When omitted, the field is not emitted. */\n compatibleClients?: CompatibleClients;\n /** Arbitrary additional fields the vendor wants merged into the agent_instructions object. */\n extra?: Record<string, unknown>;\n}\n\nexport interface AgentInstructions {\n how_to_pay: HowToPayBlock;\n recommended_tools: string[];\n wallet_compatibility: string;\n timeout_seconds: number;\n warnings: string[];\n recommended?: string;\n compatible_clients?: CompatibleClients;\n [key: string]: unknown;\n}\n\nconst TEMPO_WARNING =\n 'Do NOT use `tempo wallet transfer` to pay to the address above. That moves USDC on-chain but does not notify this server, leaving your order in pending_identity state. Use `tempo request` instead — it performs the full MPP handshake (signs, submits Authorization: Payment, waits for server confirmation).';\n\nconst X402_WARNING =\n 'Do NOT send USDC manually to the x402 deposit addresses (e.g. via a bare wallet `transfer`). Use `agentscore-pay pay` so the X-Payment credential is signed and submitted; otherwise the order stays in pending_identity even though the deposit lands.';\n\nconst TEMPO_TOOL = '`tempo request` for Tempo USDC (installs via `tempo add request`)';\nconst AGENTSCORE_PAY_TOOL =\n '`agentscore-pay` (npm: `@agent-score/pay`) — single CLI for x402 on Base + Solana, also speaks tempo MPP via `--chain tempo`';\n\nconst DEFAULT_WALLET_COMPATIBILITY =\n 'No specific wallet stack required. The 402 challenge is rail-neutral: any client that can produce a valid MPP credential (Authorization: Payment) or x402 X-Payment header is accepted. The CLI commands above are the easiest path; sign-it-yourself is fine too.';\n\nfunction defaultRecommendedTools(howToPay: HowToPayBlock): string[] {\n const tools: string[] = [];\n if (howToPay.tempo) tools.push(TEMPO_TOOL);\n if (howToPay.tempo || howToPay.x402_base || howToPay.x402_solana) tools.push(AGENTSCORE_PAY_TOOL);\n return tools;\n}\n\nfunction defaultWarnings(howToPay: HowToPayBlock): string[] {\n const w: string[] = [];\n if (howToPay.tempo) w.push(TEMPO_WARNING);\n if (howToPay.x402_base || howToPay.x402_solana) w.push(X402_WARNING);\n return w;\n}\n\n/**\n * Default `compatible_clients` derived from the rails declared in `howToPay`. Lists\n * clients the AgentScore team has smoke-verified end-to-end against an `@agent-score/commerce`\n * merchant; entries appear only for rails the vendor actually offers. Vendors override\n * this in `buildAgentInstructions({compatibleClients: {...}})` to add their own tested\n * clients or remove entries that don't fit their endpoint.\n *\n * Verified state as of the SDK release. The same data is also published as a docs page\n * for humans (rationale, per-rail commands, why some clients don't fully work, last\n * verified date) — this default keeps the merchant-side surface in sync.\n */\nfunction defaultCompatibleClients(howToPay: HowToPayBlock): CompatibleClients | undefined {\n const out: CompatibleClients = {};\n if (howToPay.tempo) out.tempo_mpp = ['agentscore-pay', 'tempo request', 'x402-proxy'];\n if (howToPay.x402_base) out.x402_base = ['agentscore-pay', 'x402-proxy', 'purl (omit --network flag)'];\n if (howToPay.x402_solana) out.x402_solana = ['agentscore-pay'];\n if (howToPay.stripe) out.stripe = ['link-cli'];\n return Object.keys(out).length === 0 ? undefined : out;\n}\n\n/**\n * Build the agent_instructions object for the 402 body. Combines how_to_pay with\n * recommended tools, warnings, wallet-compatibility note, and timeout.\n *\n * Defaults adapt to the rails declared in `howToPay`: only tempo-relevant warnings/tools\n * appear if `howToPay.tempo` is set, only x402-relevant ones if `x402_base`/`x402_solana`\n * are set. Stripe-only merchants get neither rail-specific warning. Vendors override\n * `warnings`/`recommendedTools` for full control.\n */\nexport function buildAgentInstructions(input: BuildAgentInstructionsInput): AgentInstructions {\n const compatibleClients = input.compatibleClients ?? defaultCompatibleClients(input.howToPay);\n return {\n how_to_pay: input.howToPay,\n recommended_tools: input.recommendedTools ?? defaultRecommendedTools(input.howToPay),\n wallet_compatibility: input.walletCompatibility ?? DEFAULT_WALLET_COMPATIBILITY,\n timeout_seconds: input.timeoutSeconds ?? 300,\n warnings: input.warnings ?? defaultWarnings(input.howToPay),\n ...(input.recommended ? { recommended: input.recommended } : {}),\n ...(compatibleClients ? { compatible_clients: compatibleClients } : {}),\n ...(input.extra ?? {}),\n };\n}\n","import { normalizeAddress } from './address';\nimport { TTLCache } from './cache';\n\n// Character-based trim avoids a CodeQL polynomial-redos false positive on\n// `/\\/+$/` patterns that report library-input strings.\nfunction stripTrailingSlashes(s: string): string {\n let end = s.length;\n while (end > 0 && s.charCodeAt(end - 1) === 47 /* '/' */) end--;\n return end === s.length ? s : s.slice(0, end);\n}\n\ndeclare const __VERSION__: string;\n\n// ---------------------------------------------------------------------------\n// Public types (framework-agnostic)\n// ---------------------------------------------------------------------------\n\nexport interface AgentIdentity {\n address?: string;\n operatorToken?: string;\n}\n\n/**\n * Session metadata returned from `POST /v1/sessions`. Surfaced to the `onBeforeSession`\n * hook so merchants can correlate an AgentScore session with their own resume token\n * (e.g. a pending-order id).\n */\nexport interface SessionMetadata {\n session_id: string;\n verify_url: string;\n poll_secret: string;\n poll_url: string;\n expires_at?: string;\n}\n\n/**\n * Configuration for auto-creating a verification session when no identity is present.\n *\n * The static `context` / `productName` options are sent on every session request. For\n * per-request context (e.g. the specific product the agent was trying to buy), pass\n * a `getSessionOptions` callback that returns dynamic values; its return is merged\n * over the static defaults.\n *\n * `onBeforeSession` is a side-effect hook that runs after the session is minted but\n * before the 403 is built. Use it to pre-create a reservation/draft/pending-order\n * row in your DB so agents can resume via a merchant-specific id. Return value is\n * merged into `DenialReason.extra`, so it surfaces in both the default 403 body and\n * in a custom `onDenied` handler.\n */\nexport interface CreateSessionOnMissing<TCtx = unknown> {\n apiKey: string;\n baseUrl?: string;\n context?: string;\n productName?: string;\n /** Per-request override of `context` / `productName`. Invoked with the framework context. */\n getSessionOptions?: (ctx: TCtx) => Promise<{ context?: string; productName?: string }>\n | { context?: string; productName?: string };\n /** Side-effect hook that runs after the session is minted. Return value is merged\n * into `DenialReason.extra` so custom `onDenied` handlers can include merchant-specific\n * fields (e.g. `order_id`) in the 403 response. Hook errors are logged and swallowed —\n * a failing side effect should not block the 403 from reaching the agent. */\n onBeforeSession?: (ctx: TCtx, session: SessionMetadata) => Promise<Record<string, unknown>>\n | Record<string, unknown>;\n}\n\nexport interface AgentScoreCoreOptions {\n /** AgentScore API key. Required. */\n apiKey: string;\n /** Require KYC verification. */\n requireKyc?: boolean;\n /** Require operator to be clear of sanctions. */\n requireSanctionsClear?: boolean;\n /** Minimum operator age bracket (18 or 21). */\n minAge?: number;\n /** List of blocked jurisdictions (blocklist). */\n blockedJurisdictions?: string[];\n /** List of allowed jurisdictions (allowlist — only these pass). */\n allowedJurisdictions?: string[];\n /** If true, allow the request through when the API is unreachable. Defaults to false. */\n failOpen?: boolean;\n /** How long to cache results, in seconds. Defaults to 300. */\n cacheSeconds?: number;\n /** AgentScore API base URL. Defaults to \"https://api.agentscore.sh\". */\n baseUrl?: string;\n /** Optional chain to filter scoring to. */\n chain?: string;\n /** Prepended to the default User-Agent as `\"{userAgent} (@agent-score/commerce@{version})\"`. Use to attribute API calls to your app. */\n userAgent?: string;\n /** When set and no identity is found, create a verification session instead of denying immediately. */\n createSessionOnMissing?: CreateSessionOnMissing;\n}\n\nexport type DenialCode =\n | 'wallet_not_trusted'\n | 'missing_identity'\n | 'api_error'\n | 'payment_required'\n | 'identity_verification_required'\n // Wallet-signer binding: claimed X-Wallet-Address must resolve to the same operator as\n // the payment signer; wallet-auth is rejected on rails with no wallet signer.\n | 'wallet_signer_mismatch'\n | 'wallet_auth_requires_wallet_signing'\n // Granular credential-state denials passed through from /v1/assess.\n // - `token_expired`: revoked + TTL-expired (the API unifies these). The 401 body carries\n // an auto-minted session, forwarded into DenialReason for one-shot recovery.\n // - `invalid_credential`: the token doesn't exist at all (typo, never minted, fabricated).\n // No auto-session is minted by the API for this case — agents should switch to a\n // different stored token or drop the header to bootstrap a fresh session.\n | 'token_expired'\n | 'invalid_credential';\n\n/**\n * Cross-merchant pattern hint emitted on bootstrap denials and session-create responses.\n * LLM-hosted agents should persist this to long-term memory so future AgentScore encounters skip\n * the cold-start session flow. Contents are the pattern, not secrets.\n */\nexport interface AgentMemoryHint {\n save_for_future_agentscore_gates: true;\n pattern_summary: string;\n quickstart: string;\n identity_check_endpoint: string;\n list_wallets_endpoint?: string;\n identity_paths: { wallet: string; operator_token: string };\n bootstrap: string;\n do_not_persist_in_memory: string[];\n persist_in_credential_store: string[];\n}\n\nexport interface DenialReason {\n code: DenialCode;\n /** Human-readable explanation. When omitted, `denialReasonToBody` substitutes a per-code default. */\n message?: string;\n decision?: string;\n reasons?: string[];\n verify_url?: string;\n session_id?: string;\n poll_secret?: string;\n poll_url?: string;\n agent_instructions?: string;\n /** Cross-merchant memory hint. Emitted on bootstrap denials only by default. */\n agent_memory?: AgentMemoryHint;\n /** Full assess response when the denial came from `/v1/assess`. Lets consumers access fields\n * not promoted to first-class DenialReason properties (e.g., `policy_result`). Undefined for\n * denials that did not originate from an assess call (missing_identity, api_error,\n * payment_required, identity_verification_required). */\n data?: AgentScoreData;\n /** Extra fields returned from the `createSessionOnMissing.onBeforeSession` hook. Merged\n * into the default 403 body; custom `onDenied` handlers can spread these into their own\n * response shape (e.g. to include a merchant-minted `order_id`). */\n extra?: Record<string, unknown>;\n // ---------------------------------------------------------------------------\n // Wallet-signer-match fields — populated for wallet_signer_mismatch only.\n // ---------------------------------------------------------------------------\n /** Operator id resolved from `X-Wallet-Address`. */\n claimed_operator?: string;\n /** Operator id the actual payment signer resolves to. `null` when the signer wallet isn't\n * linked to any operator (treat as a different identity). */\n actual_signer_operator?: string | null;\n /** The wallet the agent claimed via header. Echoed back for self-correction. */\n expected_signer?: string;\n /** The wallet that actually signed the payment. */\n actual_signer?: string;\n /** Wallets the claimed operator could sign with (if enumerable). Present when non-empty. */\n linked_wallets?: string[];\n}\n\nexport interface AgentScoreData {\n decision: string | null;\n decision_reasons: string[];\n identity_method?: string;\n operator_verification?: {\n level: string;\n operator_type: string | null;\n verified_at: string | null;\n };\n /** Account-level KYC facts that apply to every operator under the same account.\n * Populated when the API returns account_verification (post-KYC operator). */\n account_verification?: {\n kyc_level?: string;\n sanctions_clear?: boolean;\n age_bracket?: string;\n jurisdiction?: string;\n verified_at?: string | null;\n };\n resolved_operator?: string | null;\n /** Wallets linked to the same operator as the resolved identity. Capped at 100 entries\n * by the API. Useful for advertising in 402 challenges so wallet-auth agents know which\n * alt-signers will satisfy `wallet_signer_mismatch`. */\n linked_wallets?: string[];\n verify_url?: string;\n policy_result?: {\n all_passed: boolean;\n checks: Array<{\n rule: string;\n passed: boolean;\n required?: unknown;\n actual?: unknown;\n }>;\n } | null;\n}\n\n/**\n * Outcome from `AgentScoreCore.evaluate()`. Adapters map this to framework-specific responses.\n *\n * - `{ kind: 'allow', data }` — the request passed the policy. `data` is present on a normal\n * allow; `undefined` when fail-open short-circuited (identity missing, API unreachable,\n * timeout, or 402 paid-tier required).\n * - `{ kind: 'deny', reason }` — the request was denied. Adapters should render a 403 with the\n * reason, or invoke the caller's custom denial handler.\n */\nexport type EvaluateOutcome =\n | { kind: 'allow'; data?: AgentScoreData }\n | { kind: 'deny'; reason: DenialReason };\n\nexport interface CaptureWalletOptions {\n /** Operator credential (`opc_...`) that the agent authenticated with. */\n operatorToken: string;\n /** Signer wallet recovered from the payment payload. */\n walletAddress: string;\n /** Key-derivation family — `\"evm\"` for any EVM chain, `\"solana\"` for Solana. */\n network: 'evm' | 'solana';\n /** Optional stable key for the logical payment (e.g., payment intent id, tx hash). When the\n * same key is seen again for the same (credential, wallet, network), the server no-ops —\n * prevents agent retries from inflating transaction_count. */\n idempotencyKey?: string;\n}\n\nexport interface VerifyWalletSignerMatchOptions {\n /** The wallet claimed via `X-Wallet-Address`. */\n claimedWallet: string;\n /** The signer wallet recovered from the payment credential. `null` means the rail carries\n * no wallet signer (SPT, card) — the helper returns `wallet_auth_requires_wallet_signing`. */\n signer: string | null;\n /** Network of the signer. EVM covers every EVM chain; `solana` lives in its own namespace. */\n network?: 'evm' | 'solana';\n}\n\nexport type VerifyWalletSignerResult =\n | { kind: 'pass'; claimedOperator: string | null; signerOperator: string | null }\n | {\n kind: 'wallet_signer_mismatch';\n claimedOperator: string | null;\n actualSignerOperator: string | null;\n expectedSigner: string;\n actualSigner: string;\n linkedWallets: string[];\n /** JSON-encoded action copy (action + steps + user_message). Spread into the 403 body\n * verbatim so agents get a concrete recovery path inside the denial response itself. */\n agentInstructions: string;\n }\n | {\n kind: 'wallet_auth_requires_wallet_signing';\n claimedWallet: string;\n agentInstructions: string;\n }\n // Transient — the resolve call to /v1/assess failed or timed out. Caller should\n // retry or surface as 503. Distinct from wallet_signer_mismatch (which is an actual\n // security reject) so legitimate users don't get rejected on network flakes.\n | { kind: 'api_error'; claimedWallet: string };\n\nexport interface AgentScoreCore {\n /**\n * Evaluate the request's identity against the configured policy.\n * @param identity - extracted identity (wallet address and/or operator token)\n * @param ctx - optional framework-specific context (Hono c, Express req, etc.) passed\n * through to `createSessionOnMissing` hooks. Opaque to core.\n */\n evaluate(identity: AgentIdentity | undefined, ctx?: unknown): Promise<EvaluateOutcome>;\n /** Report a wallet seen paying under an operator credential. Fire-and-forget; silently\n * swallows non-fatal errors because capture is informational, not on the critical path. */\n captureWallet(options: CaptureWalletOptions): Promise<void>;\n /**\n * Verify the payment signer resolves to the same operator as the claimed `X-Wallet-Address`.\n *\n * Returns `pass` when the signer is linked to the same operator as the claimed wallet\n * (byte-equal wallets pass trivially; other wallets linked to the same operator also pass —\n * multi-wallet agents work without ergonomic pain). Returns `wallet_signer_mismatch` when\n * the signer resolves to a different (or no) operator. Returns `wallet_auth_requires_wallet_signing`\n * when the signer is `null` (SPT, card — rails that carry no wallet signature).\n *\n * Call this AFTER the gate evaluates (so the claimed wallet's operator is cached) and\n * AFTER the payment credential is parsed (so the signer is known). Merchants should call\n * it before settling payment.\n */\n verifyWalletSignerMatch(options: VerifyWalletSignerMatchOptions): Promise<VerifyWalletSignerResult>;\n}\n\n// ---------------------------------------------------------------------------\n// Internal types\n// ---------------------------------------------------------------------------\n\ninterface AssessResult {\n allow: boolean;\n decision?: string;\n reasons?: string[];\n raw?: unknown;\n}\n\n/**\n * Build the cross-merchant memory hint emitted on bootstrap denials. Base URLs are\n * derived from the gate's AgentScore API base so self-hosted / staging deployments get\n * correct pointers. Contents describe the AgentScore identity substrate in transferable\n * terms; merchant-specific context lives in other `agent_instructions` fields.\n */\n// Canonical production AgentScore API — used as the authoritative source for endpoint pointers\n// emitted to agent memory regardless of how a given merchant configured their gate's baseUrl.\n// Hardcoded so a malicious merchant can't set `baseUrl: \"evil.com\"` and phish agents into\n// sending their credentials to attacker-controlled endpoints.\nconst CANONICAL_AGENTSCORE_API = 'https://api.agentscore.sh';\n\n// JSON-encoded action copy emitted on wallet-signer-match denials. Spread into 403 bodies\n// by merchants so agents get a concrete recovery path inside the denial response itself —\n// no discovery-doc round trip required.\nconst WALLET_SIGNER_MISMATCH_INSTRUCTIONS = JSON.stringify({\n action: 'resign_or_switch_to_operator_token',\n steps: [\n 'Preferred: re-submit the payment signed by expected_signer (or any entry in linked_wallets — same-operator wallets are fungible) and retry with the same X-Wallet-Address.',\n 'Alternative: drop X-Wallet-Address and retry with X-Operator-Token. Use a stored opc_... if you have one; otherwise retry this request with NO identity header — the merchant will mint a verification session in the 403 body (verify_url + poll_secret). Share verify_url with the user, poll, receive a fresh opc_...',\n ],\n user_message:\n 'The payment signer resolves to a different operator than X-Wallet-Address. Re-sign from expected_signer or any linked_wallets entry, or switch to X-Operator-Token.',\n});\n\nconst WALLET_AUTH_REQUIRES_WALLET_SIGNING_INSTRUCTIONS = JSON.stringify({\n action: 'switch_to_operator_token',\n steps: [\n 'This payment rail (Stripe SPT, card) carries no wallet signature — X-Wallet-Address cannot be verified against the payment.',\n 'Drop X-Wallet-Address and retry with X-Operator-Token. If you do not have a stored opc_..., retry with no identity header to receive a verification session.',\n ],\n user_message:\n 'Wallet-address identity is only supported on wallet-signing rails (Tempo MPP, x402). On Stripe or card, use X-Operator-Token instead.',\n});\n\n// `invalid_credential` is permanent — the token doesn't exist (typo, never minted, fabricated).\n// Distinct from `token_expired` (which carries an auto-session for one-shot recovery): the\n// API doesn't auto-mint a session here because the client may simply have a different valid\n// token to try. Agents should switch tokens or drop the header to bootstrap a session.\nconst INVALID_CREDENTIAL_INSTRUCTIONS = JSON.stringify({\n action: 'switch_token_or_restart_session',\n steps: [\n 'The X-Operator-Token you sent does not match any credential. This is a permanent state — retrying with the same token will keep failing.',\n 'If you have other stored opc_... credentials, retry with one of them.',\n 'Otherwise drop X-Operator-Token and retry with no identity header — the merchant will mint a fresh verification session in the 403 body (verify_url + poll_secret) so the user can re-verify and you can poll for a new operator_token.',\n ],\n user_message:\n 'The operator_token is not recognized. Use a different stored token, or restart the verification session flow to mint a new one.',\n});\n\nexport function buildAgentMemoryHint(): AgentMemoryHint {\n // Memory pointers always point at the canonical production API — merchants must not be\n // able to redirect the cross-merchant memory surface, since that would be a phishing\n // vector for the verify URL.\n const api = CANONICAL_AGENTSCORE_API;\n return {\n save_for_future_agentscore_gates: true,\n pattern_summary:\n 'AgentScore is a cross-merchant identity substrate for agent commerce. Any endpoint that ' +\n 'returns 403 missing_identity / identity_verification_required or 402 challenges referencing AgentScore uses ' +\n 'the same identity system. One KYC works across all AgentScore merchants — no re-verification ' +\n 'per site. Save this pattern (and the endpoints below) to persistent memory so future ' +\n 'AgentScore encounters skip the cold-start session flow.',\n quickstart: 'https://docs.agentscore.sh/guides/agent-identity',\n identity_check_endpoint: `${api}/v1/credentials`,\n identity_paths: {\n wallet:\n 'If paying from a crypto wallet you have used before on any AgentScore merchant, send ' +\n '`X-Wallet-Address: 0x...`. Shortest path; no token lifecycle to manage.',\n operator_token:\n 'For any rail (Stripe SPT, card, or a wallet you have not linked yet), send ' +\n '`X-Operator-Token: opc_...`. Reusable across merchants until the token expires.',\n },\n bootstrap:\n 'If you have neither a linked wallet nor a valid operator_token, follow the session/verify ' +\n 'flow in the per-request `agent_instructions` block. This happens at most once per agent ' +\n 'identity — after first verification, the operator_token and any subsequently used wallet ' +\n 'are reusable everywhere.',\n do_not_persist_in_memory: ['operator_token', 'poll_secret'],\n persist_in_credential_store: ['operator_token'],\n };\n}\n\n// ---------------------------------------------------------------------------\n// Core factory\n// ---------------------------------------------------------------------------\n\nexport function createAgentScoreCore(options: AgentScoreCoreOptions): AgentScoreCore {\n if (!options.apiKey) {\n throw new Error('AgentScore API key is required. Get one at https://agentscore.sh/sign-up');\n }\n\n const {\n apiKey,\n requireKyc,\n requireSanctionsClear,\n minAge,\n blockedJurisdictions,\n allowedJurisdictions,\n failOpen = false,\n cacheSeconds = 300,\n baseUrl: rawBaseUrl = 'https://api.agentscore.sh',\n chain: gateChain,\n userAgent,\n createSessionOnMissing,\n } = options;\n\n const baseUrl = stripTrailingSlashes(rawBaseUrl);\n const agentMemoryHint = buildAgentMemoryHint();\n\n const defaultUa = `@agent-score/commerce@${__VERSION__}`;\n const userAgentHeader = userAgent ? `${userAgent} (${defaultUa})` : defaultUa;\n\n const API_TIMEOUT_MS = 10_000;\n\n const cache = new TTLCache<AssessResult>(cacheSeconds * 1000);\n\n async function evaluate(identity: AgentIdentity | undefined, ctx?: unknown): Promise<EvaluateOutcome> {\n // Treat \"returned identity object with no usable fields\" the same as \"no identity at all\" —\n // otherwise a misbehaving custom extractIdentity would send an empty body to /v1/assess.\n if (!identity || (!identity.address && !identity.operatorToken)) {\n if (failOpen) return { kind: 'allow' };\n\n if (createSessionOnMissing) {\n try {\n // Start with static context/productName; let getSessionOptions override per-request.\n const sessionBody: { context?: string; product_name?: string } = {};\n if (createSessionOnMissing.context != null) sessionBody.context = createSessionOnMissing.context;\n if (createSessionOnMissing.productName != null) sessionBody.product_name = createSessionOnMissing.productName;\n\n if (createSessionOnMissing.getSessionOptions && ctx !== undefined) {\n try {\n const dynamic = await createSessionOnMissing.getSessionOptions(ctx);\n if (dynamic?.context != null) sessionBody.context = dynamic.context;\n if (dynamic?.productName != null) sessionBody.product_name = dynamic.productName;\n } catch (err) {\n console.warn('[gate] createSessionOnMissing.getSessionOptions hook failed:', err instanceof Error ? err.message : err);\n }\n }\n\n const sessionBaseUrl = stripTrailingSlashes(createSessionOnMissing.baseUrl ?? 'https://api.agentscore.sh');\n const sessionRes = await fetch(`${sessionBaseUrl}/v1/sessions`, {\n method: 'POST',\n headers: {\n 'X-API-Key': createSessionOnMissing.apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify(sessionBody),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n\n if (sessionRes.ok) {\n const data = (await sessionRes.json()) as Record<string, unknown>;\n\n // Validate required fields before trusting the response. A misbehaving\n // (or mocked-wrong) API could 200 without session_id/poll_secret/verify_url,\n // which would propagate `undefined` into the 403 body and leave the agent\n // stuck — treat that as a session-create failure and fall back to the bare\n // missing_identity denial with the probe strategy copy.\n if (\n typeof data.session_id !== 'string' ||\n typeof data.poll_secret !== 'string' ||\n typeof data.verify_url !== 'string'\n ) {\n console.warn('[gate] /v1/sessions returned 200 without required fields — falling back to bare missing_identity');\n // fall through to the bare denial below\n } else {\n\n // Run onBeforeSession side-effect hook. Errors are swallowed — a failing DB\n // write (e.g. can't insert pending order) should not block the 403.\n let extra: Record<string, unknown> | undefined;\n if (createSessionOnMissing.onBeforeSession && ctx !== undefined) {\n try {\n const sessionMeta = {\n session_id: data.session_id as string,\n verify_url: data.verify_url as string,\n poll_secret: data.poll_secret as string,\n poll_url: data.poll_url as string,\n expires_at: data.expires_at as string | undefined,\n };\n const result = await createSessionOnMissing.onBeforeSession(ctx, sessionMeta);\n if (result && typeof result === 'object') extra = result;\n } catch (err) {\n console.warn('[gate] createSessionOnMissing.onBeforeSession hook failed:', err instanceof Error ? err.message : err);\n }\n }\n\n // The API emits `next_steps` (structured object) on /v1/sessions success.\n // Stringify it into the gate's `agent_instructions` contract so merchants\n // get the same JSON-encoded {action, steps, user_message} envelope as every\n // other gate-emitted denial.\n const apiNextSteps = data.next_steps as Record<string, unknown> | undefined;\n return {\n kind: 'deny',\n reason: {\n code: 'identity_verification_required',\n verify_url: data.verify_url as string,\n session_id: data.session_id as string,\n poll_secret: data.poll_secret as string,\n poll_url: data.poll_url as string | undefined,\n agent_instructions: apiNextSteps ? JSON.stringify(apiNextSteps) : undefined,\n agent_memory: agentMemoryHint,\n ...(extra && { extra }),\n },\n };\n }\n }\n } catch (err) {\n // Session-mint failed (network, /v1/sessions returned non-2xx, body parse error,\n // onBeforeSession threw inside the inner try). Falling through to bare\n // missing_identity is correct — agents still get a 403 with a probe-strategy\n // hint. But the silent catch used to mask /v1/sessions schema drift and\n // unreachable-API issues for hours, so log loudly.\n console.warn('[gate] createSessionOnMissing path failed — falling back to bare missing_identity:', err instanceof Error ? err.message : err);\n }\n }\n\n // Bare missing-identity denial (no session was auto-created). Describe the probe\n // strategy so agents without memory can recover: try wallet first on signing rails,\n // fall back to stored opc_..., fall back to session flow (noting that only\n // auto-session merchants return verify_url + poll data on the retry).\n const missingIdentityInstructions = JSON.stringify({\n action: 'probe_identity_then_session',\n steps: [\n 'If you have a wallet AND this merchant accepts a wallet-signing rail (Tempo MPP or x402 — see openapi.json x-payment-info): send X-Wallet-Address and retry. If the wallet is already linked to an AgentScore operator (via prior SIWE claim or prior captureWallet), this passes in one round trip. If the wallet is unlinked or the account has no KYC, the 403 will include a verify_url — share it with the user to claim the wallet + complete KYC, then retry.',\n 'If step 1 is denied or you already have a stored operator_token (valid + not expired): send X-Operator-Token: opc_... and retry.',\n 'If neither applies: retry with NO identity header. Merchants that auto-create verification sessions (most AgentScore merchants do) return verify_url + session_id + poll_secret in the 403 body — share verify_url with the user, then poll poll_url every 5s with the X-Poll-Secret header until status=verified (the poll returns a one-time operator_token). If the retry returns the same bare 403, this merchant does not support self-service session bootstrapping — direct the user to https://agentscore.sh/sign-up to create an AgentScore identity and mint an operator_token from their dashboard (https://agentscore.sh/dashboard/verify). The user hands the opc_... to you, and you retry with X-Operator-Token.',\n ],\n user_message:\n 'Try X-Wallet-Address first if you have a wallet and the merchant accepts Tempo/x402; fall back to a stored X-Operator-Token, then to the session/verify flow described in agent_memory.bootstrap.',\n });\n return {\n kind: 'deny',\n reason: {\n code: 'missing_identity',\n agent_instructions: missingIdentityInstructions,\n agent_memory: agentMemoryHint,\n },\n };\n }\n\n // operator_token is opaque + ASCII-only — lowercasing is safe. Wallet addresses go\n // through normalizeAddress because Solana base58 is case-sensitive and lowercasing\n // would corrupt the cache key (a Solana cache miss every time, plus collision risk\n // with mixed-case variants of the same operator).\n const cacheKey = identity.operatorToken?.toLowerCase() ?? (identity.address ? normalizeAddress(identity.address) : '');\n\n const cached = cache.get(cacheKey);\n if (cached) {\n if (cached.allow) {\n return { kind: 'allow', data: cached.raw as AgentScoreData };\n }\n return {\n kind: 'deny',\n reason: {\n code: 'wallet_not_trusted',\n decision: cached.decision,\n reasons: cached.reasons,\n verify_url: (cached.raw as Record<string, unknown> | undefined)?.verify_url as string | undefined,\n data: cached.raw as AgentScoreData | undefined,\n },\n };\n }\n\n try {\n const body: Record<string, unknown> = {};\n if (identity.address) body.address = identity.address;\n if (identity.operatorToken) body.operator_token = identity.operatorToken;\n if (gateChain) body.chain = gateChain;\n\n const policy: Record<string, unknown> = {};\n if (requireKyc != null) policy.require_kyc = requireKyc;\n if (requireSanctionsClear != null) policy.require_sanctions_clear = requireSanctionsClear;\n if (minAge != null) policy.min_age = minAge;\n if (blockedJurisdictions != null) policy.blocked_jurisdictions = blockedJurisdictions;\n if (allowedJurisdictions != null) policy.allowed_jurisdictions = allowedJurisdictions;\n if (Object.keys(policy).length > 0) body.policy = policy;\n\n const response = await fetch(`${baseUrl}/v1/assess`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify(body),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n\n if (response.status === 402) {\n if (failOpen) return { kind: 'allow' };\n return { kind: 'deny', reason: { code: 'payment_required' } };\n }\n\n // Pass through the API's token_expired 401 (covers both expired and revoked\n // credentials — the API deliberately doesn't distinguish). The 401 body carries\n // an auto-minted session (verify_url + session_id + poll_secret + next_steps +\n // agent_memory) so agents can recover without holding an API key. Forward all of\n // that into the DenialReason so the gate's 403 body includes the session fields.\n if (response.status === 401) {\n try {\n const errData = (await response.clone().json()) as {\n error?: { code?: string };\n session_id?: unknown;\n poll_secret?: unknown;\n verify_url?: unknown;\n poll_url?: unknown;\n next_steps?: unknown;\n agent_memory?: unknown;\n };\n const code = errData?.error?.code;\n if (code === 'token_expired') {\n return {\n kind: 'deny',\n reason: {\n code,\n data: errData as unknown as AgentScoreData,\n ...(typeof errData.verify_url === 'string' ? { verify_url: errData.verify_url } : {}),\n ...(typeof errData.session_id === 'string' ? { session_id: errData.session_id } : {}),\n ...(typeof errData.poll_secret === 'string' ? { poll_secret: errData.poll_secret } : {}),\n ...(typeof errData.poll_url === 'string' ? { poll_url: errData.poll_url } : {}),\n ...(errData.next_steps ? { agent_instructions: JSON.stringify(errData.next_steps) } : {}),\n ...(errData.agent_memory ? { agent_memory: errData.agent_memory as AgentMemoryHint } : {}),\n },\n };\n }\n // The API returns this when the operator_token doesn't exist at all (typo,\n // never minted, fabricated). Distinct from token_expired — no auto-session\n // is issued because the agent may have other valid tokens to try first.\n // Without this branch the gate would fall through to api_error → 503 retry,\n // which loops forever on a permanent state.\n if (code === 'invalid_credential') {\n return {\n kind: 'deny',\n reason: {\n code: 'invalid_credential',\n agent_instructions: INVALID_CREDENTIAL_INSTRUCTIONS,\n agent_memory: agentMemoryHint,\n },\n };\n }\n // Unknown 401 code — log so we notice if the API adds a new credential-state\n // code without us mapping it. Falls through to api_error below.\n if (code) {\n console.warn(`[gate] /v1/assess returned 401 ${code} — no specific handler, surfacing as api_error.`);\n }\n } catch (err) {\n // Body wasn't the expected JSON shape. Don't crash the request, but DO log —\n // a silent swallow here used to mask /v1/sessions schema drift for hours.\n console.warn('[gate] /v1/assess 401 body parse failed:', err instanceof Error ? err.message : err);\n }\n }\n\n // 4xx with a structured error body that ISN'T 401/402: log it so operators see\n // misclassifications instead of opaque 503 retries. Most common cause: a merchant\n // that didn't validate input shape before invoking the gate (invalid_address,\n // invalid_identity). We still fall through to api_error so behavior is unchanged\n // for callers — just visible now.\n if (response.status >= 400 && response.status < 500 && response.status !== 402) {\n try {\n const errData = (await response.clone().json()) as { error?: { code?: string; message?: string } };\n const code = errData?.error?.code;\n if (code && code !== 'token_expired' && code !== 'invalid_credential') {\n console.warn(\n `[gate] /v1/assess returned ${response.status} ${code} — surfacing as api_error. ` +\n 'Validate input shape before invoking the gate to avoid this.',\n );\n }\n } catch {\n // Body wasn't JSON or didn't have the expected shape — silent fall-through.\n }\n }\n\n if (!response.ok) {\n throw new Error(`AgentScore API returned ${response.status}`);\n }\n\n const data = (await response.json()) as Record<string, unknown>;\n const decision = data.decision as string | null | undefined;\n const decisionReasons = (data.decision_reasons as string[]) ?? [];\n const allow = decision === 'allow' || decision == null;\n\n cache.set(cacheKey, { allow, decision: decision ?? undefined, reasons: decisionReasons, raw: data });\n\n if (allow) {\n return { kind: 'allow', data: data as unknown as AgentScoreData };\n }\n\n return {\n kind: 'deny',\n reason: {\n code: 'wallet_not_trusted',\n decision: decision ?? undefined,\n reasons: decisionReasons,\n verify_url: data.verify_url as string | undefined,\n data: data as unknown as AgentScoreData,\n },\n };\n } catch (err) {\n // Network failure, AbortSignal timeout, JSON parse error on a 2xx, or the\n // explicit `throw new Error(...)` for an unhandled non-ok status. Log so ops\n // can distinguish \"API down\" from \"merchant config wrong\" — without this,\n // every transient blip looked identical to a misconfigured base URL.\n console.warn('[gate] /v1/assess call failed — surfacing as api_error:', err instanceof Error ? err.message : err);\n if (failOpen) return { kind: 'allow' };\n return { kind: 'deny', reason: { code: 'api_error' } };\n }\n }\n\n async function captureWallet(options: CaptureWalletOptions): Promise<void> {\n try {\n const body: Record<string, unknown> = {\n operator_token: options.operatorToken,\n wallet_address: options.walletAddress,\n network: options.network,\n };\n if (options.idempotencyKey) body.idempotency_key = options.idempotencyKey;\n await fetch(`${baseUrl}/v1/credentials/wallets`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify(body),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n } catch (err) {\n // Fire-and-forget: don't throw. Log so a persistent capture outage is visible\n // to merchant ops — otherwise wallet↔operator linkage silently stops.\n console.warn('[agentscore-commerce] captureWallet failed:', err instanceof Error ? err.message : err);\n }\n }\n\n /**\n * Resolve a wallet to its operator id via /v1/assess.\n *\n * Returns:\n * - `{ ok: true, operator: <id> }` — wallet is linked to that operator\n * - `{ ok: true, operator: null }` — wallet is valid but not linked to any operator\n * - `{ ok: false }` — the API call failed (network, timeout, non-2xx). Distinguishable so\n * callers can emit `api_error` instead of falsely asserting \"no operator linked\".\n *\n * Checks the main evaluate() cache before making a fresh call — if the gate already\n * resolved this wallet during identity evaluation, we have the resolved_operator already.\n */\n async function resolveWalletToOperator(\n walletAddress: string,\n ): Promise<{ ok: true; operator: string | null; linkedWallets: string[] } | { ok: false }> {\n // Network-aware: lowercases EVM, preserves Solana base58 case. The DB stores both\n // formats verbatim in operator_credential_wallets.wallet_address; lowercasing a\n // Solana address would never match.\n const wallet = normalizeAddress(walletAddress);\n\n // Cache lookup — first the plain cache (populated by evaluate() for identity-headered wallets).\n // Saves a second /v1/assess call when the gate already looked up this wallet.\n const extractFromCached = (raw: Record<string, unknown>): { operator: string | null; linkedWallets: string[] } => {\n const op = raw.resolved_operator;\n const links = raw.linked_wallets;\n return {\n operator: typeof op === 'string' ? op : null,\n linkedWallets: Array.isArray(links) ? (links as unknown[]).filter((w): w is string => typeof w === 'string') : [],\n };\n };\n\n const plainCached = cache.get(wallet);\n if (plainCached?.raw) {\n return { ok: true, ...extractFromCached(plainCached.raw as Record<string, unknown>) };\n }\n const resolveCached = cache.get(`resolve:${wallet}`);\n if (resolveCached?.raw) {\n return { ok: true, ...extractFromCached(resolveCached.raw as Record<string, unknown>) };\n }\n\n try {\n const response = await fetch(`${baseUrl}/v1/assess`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify({ address: walletAddress }),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n if (!response.ok) return { ok: false };\n const data = (await response.json()) as Record<string, unknown>;\n cache.set(`resolve:${wallet}`, { allow: true, raw: data });\n return { ok: true, ...extractFromCached(data) };\n } catch (err) {\n // Network/timeout/parse on the wallet→operator resolve path. Caller maps\n // `{ok:false}` to `wallet_signer_mismatch.kind === 'api_error'`, which already\n // surfaces as 503 — but log here too so multi-wallet match failures aren't\n // silently indistinguishable from \"operator simply has no linked wallet\".\n console.warn('[gate] resolveWalletToOperator failed — returning { ok:false }:', err instanceof Error ? err.message : err);\n return { ok: false };\n }\n }\n\n function reportSignerEvent(kind: 'pass' | 'wallet_signer_mismatch' | 'wallet_auth_requires_wallet_signing' | 'api_error'): void {\n // Fire-and-forget: surfaces mismatch-catch rate + api_error SLO on the dashboard.\n // Never blocks, awaits, or throws — telemetry failure must not affect the gate's decision.\n try {\n const pending = fetch(`${baseUrl}/v1/telemetry/signer-match`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify({ kind }),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n if (pending && typeof pending.catch === 'function') {\n pending.catch((err) => {\n console.warn('[agentscore-commerce] signer-match telemetry failed:', err instanceof Error ? err.message : err);\n });\n }\n } catch {\n // Thrown synchronously (e.g., fetch unavailable in test harness) — swallow silently.\n }\n }\n\n async function verifyWalletSignerMatch(\n options: VerifyWalletSignerMatchOptions,\n ): Promise<VerifyWalletSignerResult> {\n const { claimedWallet, signer } = options;\n\n if (!signer) {\n reportSignerEvent('wallet_auth_requires_wallet_signing');\n return {\n kind: 'wallet_auth_requires_wallet_signing',\n claimedWallet,\n agentInstructions: WALLET_AUTH_REQUIRES_WALLET_SIGNING_INSTRUCTIONS,\n };\n }\n\n // Network-aware normalization: lowercase EVM, preserve Solana base58. The byte-equal\n // short-circuit and downstream cache-key derivation MUST match how the DB stores\n // wallets; lowercasing Solana would corrupt both.\n const claimedNorm = normalizeAddress(claimedWallet);\n const signerNorm = normalizeAddress(signer);\n\n // Byte-equal short-circuit — no API lookup; same wallet ≡ same operator by definition.\n if (claimedNorm === signerNorm) {\n reportSignerEvent('pass');\n return { kind: 'pass', claimedOperator: null, signerOperator: null };\n }\n\n const [claimedResolve, signerResolve] = await Promise.all([\n resolveWalletToOperator(claimedNorm),\n resolveWalletToOperator(signerNorm),\n ]);\n\n // Transient API failure on either resolve → emit api_error. Caller should retry or\n // surface 503 rather than falsely reject a legitimate user on a network flake.\n if (!claimedResolve.ok || !signerResolve.ok) {\n reportSignerEvent('api_error');\n return { kind: 'api_error', claimedWallet: claimedNorm };\n }\n\n const claimedOperator = claimedResolve.operator;\n const signerOperator = signerResolve.operator;\n\n if (claimedOperator && signerOperator && claimedOperator === signerOperator) {\n reportSignerEvent('pass');\n return { kind: 'pass', claimedOperator, signerOperator };\n }\n\n reportSignerEvent('wallet_signer_mismatch');\n return {\n kind: 'wallet_signer_mismatch',\n claimedOperator,\n actualSignerOperator: signerOperator,\n expectedSigner: claimedNorm,\n actualSigner: signerNorm,\n // Populated from /v1/assess.linked_wallets on the claimed wallet — the full set of\n // wallets the agent CAN sign with to satisfy the claim (same-operator rule).\n linkedWallets: claimedResolve.linkedWallets,\n agentInstructions: WALLET_SIGNER_MISMATCH_INSTRUCTIONS,\n };\n }\n\n return { evaluate, captureWallet, verifyWalletSignerMatch };\n}\n","/**\n * Helpers for emitting the cross-merchant `agent_memory` hint on merchant 402 responses.\n *\n * The gate (`@agent-score/commerce/identity/*`) emits `agent_memory` on identity-related\n * responses (sessions, credentials, missing_identity bootstraps). Merchants can ALSO\n * include the hint in their own 402 challenge bodies on first-encounter requests so\n * agents persist the cross-merchant pattern even when entering the ecosystem through a\n * merchant-side endpoint rather than a direct AgentScore API call.\n *\n * Usage pattern:\n * - Merchant tracks per-operator (or per-IP / per-fingerprint) \"have I seen this agent\n * before?\" in their own DB\n * - On first encounter (no prior request from this operator/wallet/IP), include the hint\n * so the agent saves the pattern\n * - On subsequent encounters, skip — the agent already has it (or never will)\n *\n * The hint contents come from `buildAgentMemoryHint` (re-exported here for convenience).\n * Keep it stateless: AgentScore's pattern doesn't depend on the merchant's identity, so\n * every merchant emits the same shape.\n */\n\nimport { buildAgentMemoryHint, type AgentMemoryHint } from '../core';\n\nexport { buildAgentMemoryHint };\nexport type { AgentMemoryHint };\n\nexport interface FirstEncounterAgentMemoryInput {\n /**\n * Whether this is a first encounter for this operator/wallet/IP at this merchant.\n * Merchant-determined (DB lookup, cache flag, etc.). Pass `false` to suppress emission\n * cleanly without wrapping the call in an `if`.\n */\n firstEncounter: boolean;\n}\n\n/**\n * Returns the `agent_memory` hint when this is a first encounter, otherwise `undefined`.\n * Use directly with the `agentMemory` field of `build402Body`:\n *\n * ```ts\n * const body = build402Body({\n * acceptedMethods,\n * agentInstructions,\n * pricing,\n * agentMemory: firstEncounterAgentMemory({ firstEncounter: !this.hasSeenOperator(opToken) }),\n * });\n * ```\n *\n * Returning `undefined` means `build402Body` cleanly skips the field instead of emitting\n * `agent_memory: null` (which would imply \"I tried but failed\" rather than \"didn't apply\").\n */\nexport function firstEncounterAgentMemory(\n input: FirstEncounterAgentMemoryInput,\n): AgentMemoryHint | undefined {\n if (!input.firstEncounter) return undefined;\n return buildAgentMemoryHint();\n}\n","import type { AcceptedMethodEntry } from './accepted_methods';\nimport type { AgentInstructions } from './agent_instructions';\nimport type { IdentityMetadataBlock } from './identity';\nimport type { PricingBlock as _PricingBlock } from './pricing';\n\nexport type { PricingBlock } from './pricing';\n\nexport interface Build402BodyInput {\n /** From buildAcceptedMethods — list of MPP method entries. */\n acceptedMethods: AcceptedMethodEntry[];\n /** From buildAgentInstructions — wraps how_to_pay + warnings + recommended_tools. */\n agentInstructions?: AgentInstructions;\n /** From buildIdentityMetadata — wallet-mode echoer. Spread into the body when present. */\n identityMetadata?: IdentityMetadataBlock;\n /** Cross-merchant agent_memory hint (from gate). */\n agentMemory?: unknown;\n /** Pricing breakdown. */\n pricing?: _PricingBlock;\n /** Total amount in USD as a string (e.g., '250.00'). */\n amountUsd?: string;\n /** Currency code. Default 'USD'. */\n currency?: string;\n /** Order id for retry correlation. */\n orderId?: string | null;\n /** Product info — surfaced on the 402 so agents can confirm what they're buying. */\n product?: { id: string; name: string };\n /** The body the agent should retry with after payment (e.g., the original request body). */\n retryBody?: unknown;\n /** Recommended rail — agent's default if multiple are listed. */\n recommended?: string;\n /** x402-compliance fields (paired with the PAYMENT-REQUIRED header from `payment/wwwauthenticate`). */\n x402?: {\n accepts: unknown[];\n version?: 1 | 2;\n };\n /** Vendor-specific extra fields merged at the top level. */\n extra?: Record<string, unknown>;\n}\n\n/**\n * Assemble the full enriched 402 response body. Combines accepted_methods, agent_instructions,\n * identity metadata, pricing, x402 compliance fields, and any vendor-specific extras into a\n * single object suitable for `JSON.stringify`. Vendors pass only what they have; the builder\n * conditionally includes each section.\n *\n * Pair this with a Response that sets:\n * - 'content-type: application/json'\n * - 'www-authenticate: <wwwAuthenticateHeader([...])>' from `payment/wwwauthenticate`\n * - 'PAYMENT-REQUIRED: <paymentRequiredHeader({...})>' for x402 clients\n */\nexport function build402Body(input: Build402BodyInput): Record<string, unknown> {\n const body: Record<string, unknown> = {\n payment_required: true,\n accepted_methods: input.acceptedMethods,\n };\n\n if (input.x402) {\n body.x402Version = input.x402.version ?? 2;\n body.accepts = input.x402.accepts;\n }\n\n if (input.amountUsd !== undefined) body.amount_usd = input.amountUsd;\n if (input.currency) body.currency = input.currency;\n if (input.pricing) body.pricing = input.pricing;\n if (input.orderId !== undefined) body.order_id = input.orderId;\n if (input.product) body.product = input.product;\n if (input.recommended) body.recommended = input.recommended;\n if (input.retryBody !== undefined) body.retry_body = input.retryBody;\n\n if (input.identityMetadata) {\n Object.assign(body, input.identityMetadata);\n }\n\n if (input.agentInstructions) body.agent_instructions = input.agentInstructions;\n if (input.agentMemory !== undefined) body.agent_memory = input.agentMemory;\n\n if (input.extra) Object.assign(body, input.extra);\n\n return body;\n}\n","/**\n * Pricing block builder + canonical type.\n *\n * Composes the cents-denominated price components into the dollar-string shape that\n * 402 challenge bodies advertise. Lifts the inline pattern from martin-estate's\n * `purchase.ts` so every merchant — current and future commerce-platform plugins\n * (Commerce7, WooCommerce, Shopify) — surfaces the same shape to agents.\n *\n * Shipping is included by default because most physical-goods merchants carry it; pass\n * `shippingCents: 0` (or omit) for digital goods / services. Tax is optional for\n * merchants outside taxable jurisdictions.\n */\n\nexport interface PricingBlock {\n /** Pre-tax, pre-shipping subtotal as a dollar-string (e.g. `\"250.00\"`). */\n subtotal: string;\n /** Tax amount as a dollar-string. Always present even if `\"0.00\"`. */\n tax: string;\n /** Shipping cost as a dollar-string. Always present even if `\"0.00\"`. */\n shipping?: string;\n /** Final total = subtotal + tax + shipping, dollar-string. */\n total: string;\n /** Tax rate as a decimal fraction (e.g. `0.0775` for 7.75%). Optional — omit for tax-free merchants. */\n tax_rate?: number;\n /** ISO-3166-2 state code or jurisdiction name used for tax calc. Optional. */\n tax_state?: string;\n /** ISO-4217 currency code. Default `\"USD\"`. */\n currency?: string;\n}\n\nexport interface BuildPricingBlockInput {\n /** Pre-tax, pre-shipping subtotal in the smallest currency unit (cents, satoshi, etc.). */\n subtotalCents: number;\n /** Tax amount in the smallest currency unit. Default `0`. */\n taxCents?: number;\n /** Shipping cost in the smallest currency unit. Default `0`. */\n shippingCents?: number;\n /** Override the computed total. By default `subtotalCents + taxCents + shippingCents`. */\n totalCents?: number;\n /** Tax rate as a decimal fraction (e.g. `0.0775`). */\n taxRate?: number;\n /** Tax jurisdiction (state code, country, etc.). */\n taxState?: string;\n /** ISO-4217 currency. Default `\"USD\"`. */\n currency?: string;\n}\n\n/**\n * Compose a `PricingBlock` from cents-denominated inputs. Handles the cents → dollar-string\n * conversion (always 2 decimals) and computes the total when not explicitly provided.\n *\n * Example:\n * ```ts\n * const pricing = buildPricingBlock({\n * subtotalCents: 25000,\n * taxCents: 1875,\n * shippingCents: 999,\n * taxRate: 0.075,\n * taxState: 'CA',\n * });\n * // → { subtotal: '250.00', tax: '18.75', shipping: '9.99', total: '278.74', tax_rate: 0.075, tax_state: 'CA' }\n * ```\n *\n * Pass `shippingCents: 0` for digital goods if you want the field present (it's then `\"0.00\"`);\n * omit entirely if you don't want shipping in the response shape at all.\n */\nexport function buildPricingBlock(input: BuildPricingBlockInput): PricingBlock {\n const tax = input.taxCents ?? 0;\n const shipping = input.shippingCents ?? 0;\n const total = input.totalCents ?? input.subtotalCents + tax + shipping;\n\n const block: PricingBlock = {\n subtotal: formatCents(input.subtotalCents),\n tax: formatCents(tax),\n total: formatCents(total),\n };\n\n if (input.shippingCents !== undefined) block.shipping = formatCents(shipping);\n if (input.taxRate !== undefined) block.tax_rate = input.taxRate;\n if (input.taxState !== undefined) block.tax_state = input.taxState;\n if (input.currency !== undefined) block.currency = input.currency;\n\n return block;\n}\n\nfunction formatCents(cents: number): string {\n return (cents / 100).toFixed(2);\n}\n","/**\n * Joins multiple Payment directives into a single WWW-Authenticate header value.\n * Per RFC 7235, multiple challenges are comma-separated.\n */\nexport function wwwAuthenticateHeader(directives: string[]): string {\n return directives.join(', ');\n}\n\nexport interface PaymentRequiredHeaderInput {\n x402Version: 1 | 2;\n accepts: unknown[];\n resource?: { url: string; mimeType?: string };\n}\n\n/**\n * Add the v1↔v2 amount-field alias to each accepts entry. Idempotent. Used by both\n * `paymentRequiredHeader` (header emit) and `build402Body` (body emit) so every\n * x402 entry on the wire carries BOTH `amount` (v2 spec) AND `maxAmountRequired`\n * (v1 spec) — strict v1-only parsers (e.g. Coinbase awal at `payments-mcp.coinbase.com`,\n * which is hardcoded to read `maxAmountRequired`) work alongside strict v2 parsers,\n * which ignore the alias.\n */\nexport function aliasAmountFields(accepts: unknown[]): unknown[] {\n return accepts.map((entry) => {\n if (entry === null || typeof entry !== 'object') return entry;\n const e = entry as Record<string, unknown>;\n const hasAmount = e.amount !== undefined;\n const hasMaxAmount = e.maxAmountRequired !== undefined;\n if (hasAmount && !hasMaxAmount) return { ...e, maxAmountRequired: e.amount };\n if (hasMaxAmount && !hasAmount) return { ...e, amount: e.maxAmountRequired };\n return e;\n });\n}\n\n/**\n * Encode the standard x402 PAYMENT-REQUIRED header (base64-encoded JSON of the\n * PaymentRequired object). Clients that recognize the header (`@x402/fetch`,\n * `@x402/core` HTTPClient, `agentscore-pay`) prefer it over body fields.\n *\n * Note: do NOT add a v1↔v2 amount-field alias here. `@x402/core`'s\n * `findMatchingRequirements` uses `deepEqual` against the agent's signed\n * `accepted` payload — any field present on one side and missing on the other\n * (e.g. `maxAmountRequired` on the wire body but not in `buildPaymentRequirements`'s\n * output) makes the match silently fail at settle time. Keep `accepts` shape\n * identical to whatever `buildPaymentRequirements` produces server-side.\n */\nexport function paymentRequiredHeader(input: PaymentRequiredHeaderInput): string {\n return Buffer.from(JSON.stringify(input)).toString('base64');\n}\n","/**\n * `respond402` — single-call 402 emit for merchants who use both `mppx` (for tempo + stripe\n * MPP rails) AND x402 (for Base + Solana).\n *\n * The seam is fiddly enough to get wrong by hand:\n * - mppx's `compose()(req)` returns a 402 Response with WWW-Authenticate directives\n * whose ids mppx's server-side validator REMEMBERS — they round-trip in client\n * credentials. Overwriting that header (e.g. with `buildPaymentHeaders` output)\n * breaks the round-trip.\n * - x402 needs the binary-friendly `PAYMENT-REQUIRED` header (base64-encoded JSON\n * of `{x402Version, accepts, resource}`) — mppx doesn't emit it.\n * - Merchants want a richer JSON body (pricing, identity metadata, agent_instructions,\n * agent_memory, retry_body, accepted_methods cross-reference) than the bare mppx body.\n *\n * `respond402` composes all three in one call:\n * - PRESERVES mppx's WWW-Authenticate verbatim\n * - ADDS PAYMENT-REQUIRED when x402 entries are present\n * - REPLACES the body with the rich body via `build402Body`\n *\n * Usage:\n * ```ts\n * const result = await m.compose(['tempo/charge', {...}], ['stripe/charge', {...}])(c.req.raw);\n * if (result.status === 402) {\n * return commerce.respond402({\n * mppxChallenge: result.challenge,\n * x402: { version: 2, accepts: x402Accepts, resource: { url: c.req.url, mimeType: 'application/json' } },\n * body: { acceptedMethods, agentInstructions, identityMetadata, pricing, agentMemory, retryBody, ... },\n * });\n * }\n * ```\n */\n\nimport { paymentRequiredHeader, type PaymentRequiredHeaderInput } from '../payment/wwwauthenticate';\nimport { build402Body, type Build402BodyInput } from './body';\n\nexport interface Respond402Input {\n /** The 402 Response returned by `mppx.compose()(req)`. Its WWW-Authenticate header\n * is preserved verbatim — mppx's server-side validator matches credentials to the\n * directive ids it generated, so overwriting breaks the round-trip. */\n mppxChallenge: Response;\n /** Inputs to `build402Body` — the rich JSON body sent to the agent. */\n body: Build402BodyInput;\n /** When set, layers on the x402 PAYMENT-REQUIRED header (base64-encoded JSON).\n * Omit for merchants that don't accept x402 (Base/Solana) — mppx-only setups. */\n x402?: PaymentRequiredHeaderInput;\n}\n\nexport function respond402(input: Respond402Input): Response {\n const body = build402Body(input.body);\n const headers = new Headers(input.mppxChallenge.headers);\n headers.set('content-type', 'application/json');\n if (input.x402) {\n headers.set('PAYMENT-REQUIRED', paymentRequiredHeader(input.x402));\n }\n return new Response(JSON.stringify(body), { headers, status: 402 });\n}\n","/**\n * Build a structured 4xx validation-error body that pairs cleanly with the\n * existing 402 / 403 builders. Every commerce merchant returning helpful\n * `bad_request` / `not_found` / `out_of_stock` / etc. errors converges on the\n * same shape: `{ error: {code, message}, ...optional_hints, next_steps? }`.\n *\n * This builder doesn't choose the HTTP status — vendors wrap the returned\n * body in their framework's response (`c.json(body, 400)` in Hono,\n * `Response.json(body, {status: 400})` for the Web Fetch path, etc.). Status\n * stays the merchant's call because the same shape works for 400/404/409/422.\n */\nexport interface BuildValidationErrorInput {\n /** Machine-readable error code (e.g. 'bad_request', 'not_found', 'out_of_stock'). */\n code: string;\n /** Human-readable message — surfaced directly to the user via the agent. */\n message: string;\n /** Optional schema description of required body fields, keyed by field name. Surfaced\n * so agents can self-correct without fetching docs. */\n requiredFields?: Record<string, string>;\n /** Optional concrete example body. Pairs with `requiredFields` for max self-serve. */\n exampleBody?: unknown;\n /** Optional next-step hint block (`{action, user_message?, ...vendor_extras}`). */\n nextSteps?: Record<string, unknown>;\n /** Vendor-specific top-level fields merged into the body (e.g. `available`,\n * `blocked_states`, `max_length`). */\n extra?: Record<string, unknown>;\n}\n\nexport interface ValidationErrorBody {\n error: { code: string; message: string };\n required_fields?: Record<string, string>;\n example_body?: unknown;\n next_steps?: Record<string, unknown>;\n [key: string]: unknown;\n}\n\n/**\n * Compose a 4xx body that vendors return via their framework's response helper.\n * Combine with the merchant's chosen HTTP status (400 for body shape errors,\n * 404 for missing entities, 409 for stock conflicts, 403 for policy denials, etc.).\n *\n * Example:\n * ```ts\n * return c.json(buildValidationError({\n * code: 'bad_request',\n * message: 'product_id, email, and shipping are required',\n * requiredFields: { product_id: 'uuid', email: 'string', shipping: 'object' },\n * nextSteps: { action: 'retry_with_complete_body' },\n * }), 400);\n * ```\n */\nexport function buildValidationError(input: BuildValidationErrorInput): ValidationErrorBody {\n const body: ValidationErrorBody = {\n error: { code: input.code, message: input.message },\n };\n if (input.requiredFields) body.required_fields = input.requiredFields;\n if (input.exampleBody !== undefined) body.example_body = input.exampleBody;\n if (input.nextSteps) body.next_steps = input.nextSteps;\n if (input.extra) Object.assign(body, input.extra);\n return body;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC+DO,SAAS,qBAAqB,OAAyD;AAC5F,QAAM,MAA6B,CAAC;AAEpC,MAAI,MAAM,OAAO;AACf,QAAI,KAAK;AAAA,MACP,QAAQ;AAAA,MACR,SAAS,MAAM,MAAM,WAAW;AAAA,MAChC,UAAU,MAAM,MAAM,WAAW;AAAA,MACjC,OAAO,MAAM,MAAM,SAAS;AAAA,MAC5B,QAAQ,MAAM,MAAM,UAAU;AAAA,MAC9B,UAAU,MAAM,MAAM,YAAY;AAAA,MAClC,QAAQ,MAAM,MAAM;AAAA,IACtB,CAAC;AAAA,EACH;AAEA,MAAI,MAAM,WAAW;AACnB,QAAI,KAAK;AAAA,MACP,QAAQ;AAAA,MACR,SAAS,MAAM,UAAU,WAAW;AAAA,MACpC,UAAU,MAAM,UAAU,WAAW;AAAA,MACrC,OAAO,MAAM,UAAU,SAAS;AAAA,MAChC,QAAQ,MAAM,UAAU,UAAU;AAAA,MAClC,UAAU,MAAM,UAAU,YAAY;AAAA,MACtC,QAAQ,MAAM,UAAU;AAAA,IAC1B,CAAC;AAAA,EACH;AAEA,MAAI,MAAM,aAAa;AACrB,QAAI,KAAK;AAAA,MACP,QAAQ;AAAA,MACR,SAAS,MAAM,YAAY,WAAW;AAAA,MACtC,OAAO,MAAM,YAAY,SAAS;AAAA,MAClC,QAAQ,MAAM,YAAY,UAAU;AAAA,MACpC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MAAM,YAAY,OAAW,OAAM,WAAW,MAAM;AACxD,MAAI,MAAM,aAAa,OAAW,OAAM,YAAY,MAAM;AAC1D,MAAI,MAAM,aAAa,OAAW,OAAM,WAAW,MAAM;AAEzD,SAAO;AACT;AAEA,SAAS,YAAY,OAAuB;AAC1C,UAAQ,QAAQ,KAAK,QAAQ,CAAC;AAChC;;;ACzCO,SAAS,sBAAsB,OAA2C;AAC/E,SAAO,OAAO,KAAK,KAAK,UAAU,KAAK,CAAC,EAAE,SAAS,QAAQ;AAC7D;;;ACDO,SAAS,WAAW,OAAkC;AAC3D,QAAM,OAAO,aAAa,MAAM,IAAI;AACpC,QAAM,UAAU,IAAI,QAAQ,MAAM,cAAc,OAAO;AACvD,UAAQ,IAAI,gBAAgB,kBAAkB;AAC9C,MAAI,MAAM,MAAM;AACd,YAAQ,IAAI,oBAAoB,sBAAsB,MAAM,IAAI,CAAC;AAAA,EACnE;AACA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG,EAAE,SAAS,QAAQ,IAAI,CAAC;AACpE;;;ACJO,SAAS,qBAAqB,OAAuD;AAC1F,QAAM,OAA4B;AAAA,IAChC,OAAO,EAAE,MAAM,MAAM,MAAM,SAAS,MAAM,QAAQ;AAAA,EACpD;AACA,MAAI,MAAM,eAAgB,MAAK,kBAAkB,MAAM;AACvD,MAAI,MAAM,gBAAgB,OAAW,MAAK,eAAe,MAAM;AAC/D,MAAI,MAAM,UAAW,MAAK,aAAa,MAAM;AAC7C,MAAI,MAAM,MAAO,QAAO,OAAO,MAAM,MAAM,KAAK;AAChD,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/challenge/index.ts","../../src/challenge/accepted_methods.ts","../../src/challenge/identity.ts","../../src/challenge/how_to_pay.ts","../../src/challenge/agent_instructions.ts","../../src/core.ts","../../src/challenge/agent_memory.ts","../../src/challenge/body.ts","../../src/challenge/pricing.ts","../../src/payment/wwwauthenticate.ts","../../src/challenge/respond_402.ts","../../src/challenge/validation_error.ts"],"sourcesContent":["export * from './accepted_methods';\nexport * from './identity';\nexport * from './how_to_pay';\nexport * from './agent_instructions';\nexport * from './agent_memory';\nexport * from './body';\nexport * from './pricing';\nexport * from './order_receipt';\nexport * from './respond_402';\nexport * from './validation_error';\n","export interface TempoMethodEntry {\n method: 'tempo/charge';\n network: string;\n chain_id: number;\n token: string;\n symbol: string;\n decimals: number;\n pay_to: string;\n}\n\nexport interface X402MethodEntry {\n method: 'x402/exact';\n network: string;\n chain_id?: number;\n token: string;\n symbol: string;\n decimals: number;\n pay_to: string;\n}\n\nexport interface StripeMethodEntry {\n method: 'stripe/charge';\n rails: ('card' | 'link' | 'shared_payment_token')[];\n profile_id: string | null;\n}\n\nexport type AcceptedMethodEntry = TempoMethodEntry | X402MethodEntry | StripeMethodEntry;\n\nexport interface BuildAcceptedMethodsInput {\n tempo?: {\n recipient: string;\n network?: string;\n chainId?: number;\n token?: string;\n symbol?: string;\n decimals?: number;\n };\n x402_base?: {\n recipient: string;\n network?: string;\n chainId?: number;\n token?: string;\n symbol?: string;\n decimals?: number;\n };\n x402_solana?: {\n recipient: string;\n network?: string;\n token?: string;\n symbol?: string;\n decimals?: number;\n };\n stripe?: {\n profileId?: string | null;\n rails?: ('card' | 'link' | 'shared_payment_token')[];\n };\n}\n\n/**\n * Build the `accepted_methods[]` array for an enriched 402 body. Each rail entry is\n * conditionally included based on whether the vendor passed it. Per-rail shapes follow\n * the conventions established in martin-estate's reference 402.\n */\nexport function buildAcceptedMethods(input: BuildAcceptedMethodsInput): AcceptedMethodEntry[] {\n const out: AcceptedMethodEntry[] = [];\n\n if (input.tempo) {\n out.push({\n method: 'tempo/charge',\n network: input.tempo.network ?? 'tempo-mainnet',\n chain_id: input.tempo.chainId ?? 4217,\n token: input.tempo.token ?? '0x20C000000000000000000000b9537d11c60E8b50',\n symbol: input.tempo.symbol ?? 'USDC.e',\n decimals: input.tempo.decimals ?? 6,\n pay_to: input.tempo.recipient,\n });\n }\n\n if (input.x402_base) {\n out.push({\n method: 'x402/exact',\n network: input.x402_base.network ?? 'eip155:8453',\n chain_id: input.x402_base.chainId ?? 8453,\n token: input.x402_base.token ?? '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',\n symbol: input.x402_base.symbol ?? 'USDC',\n decimals: input.x402_base.decimals ?? 6,\n pay_to: input.x402_base.recipient,\n });\n }\n\n if (input.x402_solana) {\n out.push({\n method: 'x402/exact',\n network: input.x402_solana.network ?? 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',\n token: input.x402_solana.token ?? 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',\n symbol: input.x402_solana.symbol ?? 'USDC',\n decimals: input.x402_solana.decimals ?? 6,\n pay_to: input.x402_solana.recipient,\n });\n }\n\n if (input.stripe) {\n out.push({\n method: 'stripe/charge',\n rails: input.stripe.rails ?? ['card', 'link', 'shared_payment_token'],\n profile_id: input.stripe.profileId ?? null,\n });\n }\n\n return out;\n}\n","export type IdentityMode = 'wallet' | 'operator_token';\n\nexport interface SignerMatchResultLike {\n kind: 'pass' | 'wallet_signer_mismatch' | 'wallet_auth_requires_wallet_signing' | string;\n expectedSigner?: string;\n actualSigner?: string;\n linkedWallets?: string[];\n}\n\nexport interface IdentityMetadataInput {\n /** Current request's identity mode. */\n mode: IdentityMode;\n /** Claimed wallet address (when mode === 'wallet'). */\n wallet?: string;\n /** Result of a prior verifyWalletSignerMatch call. */\n signerMatchResult?: SignerMatchResultLike;\n /** Same-operator linked wallets (from assess response). */\n linkedWallets?: string[];\n /** Optional explicit constraint description (overrides the auto-generated one). */\n signerConstraint?: string;\n}\n\nexport interface IdentityMetadataBlock {\n identity_mode: IdentityMode;\n required_signer?: string;\n linked_wallets?: string[];\n signer_constraint?: string;\n}\n\n/**\n * Build the identity-metadata block for an enriched 402 body. Echoes the agent's\n * identity context (wallet vs. operator-token mode) so the agent can self-correct\n * before signing — specifically, on wallet-auth rails the agent MUST sign with one\n * of the wallets in linked_wallets (all resolve to the same operator).\n */\nexport function buildIdentityMetadata(input: IdentityMetadataInput): IdentityMetadataBlock {\n const block: IdentityMetadataBlock = { identity_mode: input.mode };\n\n if (input.mode !== 'wallet') return block;\n\n if (input.wallet) {\n block.required_signer = input.signerMatchResult?.expectedSigner ?? input.wallet;\n }\n if (input.linkedWallets && input.linkedWallets.length > 0) {\n block.linked_wallets = input.linkedWallets;\n }\n block.signer_constraint =\n input.signerConstraint ??\n 'Payment must be signed with the claimed wallet OR any same-operator linked wallet listed in linked_wallets.';\n\n return block;\n}\n","export interface HowToPayRailEntry {\n setup?: string[];\n prerequisite?: string;\n command: string;\n alternative_command?: string;\n what_it_does: string;\n}\n\nexport interface HowToPayStripeEntry {\n prerequisite: string;\n instructions: string;\n setup_link_cli?: string[];\n command_link_cli?: string[];\n what_it_does_link_cli?: string;\n note?: string;\n}\n\nexport interface HowToPayBlock {\n tempo?: HowToPayRailEntry;\n x402_base?: HowToPayRailEntry;\n x402_solana?: HowToPayRailEntry;\n stripe?: HowToPayStripeEntry;\n}\n\nexport interface BuildHowToPayInput {\n /** The merchant's full URL (e.g., 'https://agents.merchant.example/api/buy'). */\n url: string;\n /** JSON string of the body the agent should retry with — typically the original request body. */\n retryBodyJson: string;\n /** Total amount in USD (string or number). Used to compute max-spend defaults and stripe context. */\n totalUsd: string | number;\n /** Per-rail config — each is optional. Pass only the rails you support. */\n rails: {\n tempo?: { recipient: string; networkName?: string; chainId?: number; recommend?: 'tempo' | 'agentscore-pay' | 'both' };\n x402_base?: { recipient: string; network?: string };\n x402_solana?: { recipient: string; network?: string };\n stripe?: { profileId?: string | null; productName?: string };\n };\n /** Placeholder text for the operator token in commands. Defaults to '<your_opc_token>'. */\n opTokenPlaceholder?: string;\n /** Override max-spend value used in commands. Default: ceil(totalUsd) + 1. */\n maxSpend?: string | number;\n}\n\nconst TEMPO_SETUP = [\n 'curl -fsSL https://tempo.xyz/install | bash',\n 'tempo wallet login',\n 'tempo wallet whoami',\n 'tempo wallet fund # if balance is zero',\n];\n\nconst PAY_SETUP_BASE = [\n 'npm install -g @agent-score/pay # or: brew install agentscore/tap/agentscore-pay',\n 'agentscore-pay wallet create --chain base',\n 'agentscore-pay balance --chain base # fund the printed address with USDC on Base',\n];\n\nconst PAY_SETUP_SOLANA = [\n 'npm install -g @agent-score/pay # or: brew install agentscore/tap/agentscore-pay',\n 'agentscore-pay wallet create --chain solana',\n 'agentscore-pay balance --chain solana # fund the printed address with USDC on Solana',\n];\n\n/**\n * Build the agent_instructions.how_to_pay block. Generates per-rail setup/command/what_it_does\n * boilerplate so agents see concrete commands per rail in the 402 body. Vendors pass the rails\n * they support; the helper produces the right command for each.\n *\n * Tool recommendations (tempo CLI vs agentscore-pay vs link-cli) are configurable per rail.\n */\nexport function buildHowToPay(input: BuildHowToPayInput): HowToPayBlock {\n const totalNum = typeof input.totalUsd === 'string' ? Number(input.totalUsd) : input.totalUsd;\n const maxSpend = String(input.maxSpend ?? (Math.ceil(totalNum) + 1).toFixed(2));\n const opToken = input.opTokenPlaceholder ?? '<your_opc_token>';\n const block: HowToPayBlock = {};\n\n if (input.rails.tempo) {\n const networkName = input.rails.tempo.networkName ?? 'tempo-mainnet';\n const chainId = input.rails.tempo.chainId ?? 4217;\n const recommend = input.rails.tempo.recommend ?? 'both';\n const tempoCommand = `tempo request -X POST -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' --json '${input.retryBodyJson}' --max-spend ${maxSpend} ${input.url}`;\n const payCommand = `agentscore-pay pay POST ${input.url} --chain tempo -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' -d '${input.retryBodyJson}' --max-spend ${maxSpend}`;\n block.tempo = {\n setup: TEMPO_SETUP,\n prerequisite: `Run \\`tempo wallet whoami\\` and confirm USDC.e balance on ${networkName} (chain ${chainId}) is at least $${maxSpend}. If the tempo CLI is not installed, run the setup commands above first.`,\n command: recommend === 'agentscore-pay' ? payCommand : tempoCommand,\n ...(recommend === 'both'\n ? { alternative_command: payCommand }\n : recommend === 'agentscore-pay'\n ? { alternative_command: tempoCommand }\n : {}),\n what_it_does: `Hits this endpoint, receives this same 402, signs the MPP challenge on ${networkName}, and submits the credential back via Authorization: Payment. Either client (tempo request or agentscore-pay pay --chain tempo) works — both run the full MPP handshake.`,\n };\n }\n\n if (input.rails.x402_base) {\n const network = input.rails.x402_base.network ?? 'eip155:8453';\n block.x402_base = {\n setup: PAY_SETUP_BASE,\n prerequisite: `Run \\`agentscore-pay balance --chain base\\` and confirm USDC balance on Base (${network}) is at least $${maxSpend}. If the CLI is not installed, run the setup commands above first.`,\n command: `agentscore-pay pay POST ${input.url} --chain base -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' -d '${input.retryBodyJson}' --max-spend ${maxSpend}`,\n what_it_does:\n 'Hits this endpoint, receives this same 402, signs an EIP-3009 USDC TransferWithAuthorization on Base, submits via X-Payment header. Server verifies + settles via the Coinbase facilitator + returns 200 with the completed order.',\n };\n }\n\n if (input.rails.x402_solana) {\n const network = input.rails.x402_solana.network ?? 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp';\n block.x402_solana = {\n setup: PAY_SETUP_SOLANA,\n prerequisite: `Run \\`agentscore-pay balance --chain solana\\` and confirm USDC balance on Solana (${network}) is at least $${maxSpend}. If the CLI is not installed, run the setup commands above first.`,\n command: `agentscore-pay pay POST ${input.url} --chain solana -H 'X-Operator-Token: ${opToken}' -H 'Content-Type: application/json' -d '${input.retryBodyJson}' --max-spend ${maxSpend}`,\n what_it_does:\n 'Hits this endpoint, receives this same 402, signs an SPL Token TransferChecked transaction on Solana, submits via X-Payment header. Server verifies + settles via the Coinbase facilitator + returns 200 with the completed order.',\n };\n }\n\n if (input.rails.stripe) {\n const stripeCfg = input.rails.stripe;\n const amountCents = Math.round(totalNum * 100);\n const linkCliBlocked = amountCents > 50000;\n const productName = stripeCfg.productName ?? 'this purchase';\n const sptContext = `Purchasing \"${productName}\" via the agent commerce API. The user authorized this purchase through their AI agent for $${totalNum}; charge to be settled via shared payment token over the Machine Payments Protocol.`;\n const stripe: HowToPayStripeEntry = {\n prerequisite:\n 'Either your own Stripe account with Shared Payment Token acceptance, OR a Stripe Link wallet (any user with link.com).',\n instructions:\n 'Mint a SharedPaymentToken scoped to the profile_id advertised in accepted_methods, then submit via Authorization: Payment MPP header with method=stripe/charge.',\n };\n if (stripeCfg.profileId && !linkCliBlocked) {\n stripe.setup_link_cli = [\n 'npm install -g @stripe/link-cli # or use npx -y @stripe/link-cli for one-shot',\n 'link-cli auth login # one-time, opens your Link wallet',\n 'link-cli payment-methods list --output-json # copy a csmrpd_... id',\n ];\n stripe.command_link_cli = [\n `SPEND_ID=$(link-cli spend-request create --payment-method-id <csmrpd_id_from_payment_methods_list> --credential-type shared_payment_token --network-id ${stripeCfg.profileId} --amount ${amountCents} --context \"${sptContext}\" --request-approval --output-json | jq -r .id)`,\n `link-cli mpp pay ${input.url} --spend-request-id $SPEND_ID --method POST --data '${input.retryBodyJson}' --header 'X-Operator-Token: ${opToken}' --output-json`,\n ];\n stripe.what_it_does_link_cli =\n 'For users who have a Stripe Link wallet: step 1 mints a one-time-use SharedPaymentToken scoped to this purchase and pushes a notification to the user for approval (blocks until approved); step 2 submits the SPT via the MPP handshake along with your AgentScore operator credential.';\n } else if (linkCliBlocked) {\n stripe.note = `link-cli SPT path not available for this purchase — Stripe link-cli caps spend requests at $500.00 ($50000 cents); your total is $${totalNum}. Use your own Stripe account with the SharedPaymentToken API instead.`;\n }\n block.stripe = stripe;\n }\n\n return block;\n}\n","import type { HowToPayBlock } from './how_to_pay';\n\n/** Map of rail key (e.g. 'x402_base', 'tempo_mpp', 'stripe') → list of client identifiers\n * that have been smoke-verified by the merchant against the protocol shape they emit.\n * Strings are display labels, not install commands — agents already get install commands\n * via `how_to_pay.<rail>.setup`. Use these as a \"what's known to work\" hint. */\nexport type CompatibleClients = Record<string, string[]>;\n\nexport interface BuildAgentInstructionsInput {\n /** Per-rail commands. Build with `buildHowToPay`. */\n howToPay: HowToPayBlock;\n /** Tool recommendations as human-readable strings. Defaults to a sensible set covering tempo + agentscore-pay. */\n recommendedTools?: string[];\n /** Wallet-stack compatibility note for the agent. Default: rail-neutral, no specific wallet stack required. */\n walletCompatibility?: string;\n /** How long the merchant will wait for payment after the 402. Default 300 (5 minutes). */\n timeoutSeconds?: number;\n /** Warnings about common footguns. Defaults include tempo wallet transfer + raw on-chain x402 deposits. */\n warnings?: string[];\n /** Recommended rail (e.g., 'tempo', 'x402_base'). Surfaced for agents to default to. */\n recommended?: string;\n /** Per-rail list of client names the merchant has verified work end-to-end. Vendors set\n * this from their own smoke matrix — defaults to none (avoids vouching for clients the\n * merchant has not tested). When omitted, the field is not emitted. */\n compatibleClients?: CompatibleClients;\n /** Arbitrary additional fields the vendor wants merged into the agent_instructions object. */\n extra?: Record<string, unknown>;\n}\n\nexport interface AgentInstructions {\n how_to_pay: HowToPayBlock;\n recommended_tools: string[];\n wallet_compatibility: string;\n timeout_seconds: number;\n warnings: string[];\n recommended?: string;\n compatible_clients?: CompatibleClients;\n [key: string]: unknown;\n}\n\nconst TEMPO_WARNING =\n 'Do NOT use `tempo wallet transfer` to pay to the address above. That moves USDC on-chain but does not notify this server, leaving your order in pending_identity state. Use `tempo request` instead — it performs the full MPP handshake (signs, submits Authorization: Payment, waits for server confirmation).';\n\nconst X402_WARNING =\n 'Do NOT send USDC manually to the x402 deposit addresses (e.g. via a bare wallet `transfer`). Use `agentscore-pay pay` so the X-Payment credential is signed and submitted; otherwise the order stays in pending_identity even though the deposit lands.';\n\nconst TEMPO_TOOL = '`tempo request` for Tempo USDC (installs via `tempo add request`)';\nconst AGENTSCORE_PAY_TOOL =\n '`agentscore-pay` (npm: `@agent-score/pay`) — single CLI for x402 on Base + Solana, also speaks tempo MPP via `--chain tempo`';\n\nconst DEFAULT_WALLET_COMPATIBILITY =\n 'No specific wallet stack required. The 402 challenge is rail-neutral: any client that can produce a valid MPP credential (Authorization: Payment) or x402 X-Payment header is accepted. The CLI commands above are the easiest path; sign-it-yourself is fine too.';\n\nfunction defaultRecommendedTools(howToPay: HowToPayBlock): string[] {\n const tools: string[] = [];\n if (howToPay.tempo) tools.push(TEMPO_TOOL);\n if (howToPay.tempo || howToPay.x402_base || howToPay.x402_solana) tools.push(AGENTSCORE_PAY_TOOL);\n return tools;\n}\n\nfunction defaultWarnings(howToPay: HowToPayBlock): string[] {\n const w: string[] = [];\n if (howToPay.tempo) w.push(TEMPO_WARNING);\n if (howToPay.x402_base || howToPay.x402_solana) w.push(X402_WARNING);\n return w;\n}\n\n/**\n * Default `compatible_clients` derived from the rails declared in `howToPay`. Lists\n * clients the AgentScore team has smoke-verified end-to-end against an `@agent-score/commerce`\n * merchant; entries appear only for rails the vendor actually offers. Vendors override\n * this in `buildAgentInstructions({compatibleClients: {...}})` to add their own tested\n * clients or remove entries that don't fit their endpoint.\n *\n * Verified state as of the SDK release. The same data is also published as a docs page\n * for humans (rationale, per-rail commands, why some clients don't fully work, last\n * verified date) — this default keeps the merchant-side surface in sync.\n */\nfunction defaultCompatibleClients(howToPay: HowToPayBlock): CompatibleClients | undefined {\n const out: CompatibleClients = {};\n if (howToPay.tempo) out.tempo_mpp = ['agentscore-pay', 'tempo request', 'x402-proxy'];\n if (howToPay.x402_base) out.x402_base = ['agentscore-pay', 'x402-proxy', 'purl (omit --network flag)'];\n if (howToPay.x402_solana) out.x402_solana = ['agentscore-pay'];\n if (howToPay.stripe) out.stripe = ['link-cli'];\n return Object.keys(out).length === 0 ? undefined : out;\n}\n\n/**\n * Build the agent_instructions object for the 402 body. Combines how_to_pay with\n * recommended tools, warnings, wallet-compatibility note, and timeout.\n *\n * Defaults adapt to the rails declared in `howToPay`: only tempo-relevant warnings/tools\n * appear if `howToPay.tempo` is set, only x402-relevant ones if `x402_base`/`x402_solana`\n * are set. Stripe-only merchants get neither rail-specific warning. Vendors override\n * `warnings`/`recommendedTools` for full control.\n */\nexport function buildAgentInstructions(input: BuildAgentInstructionsInput): AgentInstructions {\n const compatibleClients = input.compatibleClients ?? defaultCompatibleClients(input.howToPay);\n return {\n how_to_pay: input.howToPay,\n recommended_tools: input.recommendedTools ?? defaultRecommendedTools(input.howToPay),\n wallet_compatibility: input.walletCompatibility ?? DEFAULT_WALLET_COMPATIBILITY,\n timeout_seconds: input.timeoutSeconds ?? 300,\n warnings: input.warnings ?? defaultWarnings(input.howToPay),\n ...(input.recommended ? { recommended: input.recommended } : {}),\n ...(compatibleClients ? { compatible_clients: compatibleClients } : {}),\n ...(input.extra ?? {}),\n };\n}\n","import { isFixableDenial } from './_denial';\nimport { normalizeAddress } from './address';\nimport { TTLCache } from './cache';\n\n// Character-based trim avoids a CodeQL polynomial-redos false positive on\n// `/\\/+$/` patterns that report library-input strings.\nfunction stripTrailingSlashes(s: string): string {\n let end = s.length;\n while (end > 0 && s.charCodeAt(end - 1) === 47 /* '/' */) end--;\n return end === s.length ? s : s.slice(0, end);\n}\n\ndeclare const __VERSION__: string;\n\n// ---------------------------------------------------------------------------\n// Public types (framework-agnostic)\n// ---------------------------------------------------------------------------\n\nexport interface AgentIdentity {\n address?: string;\n operatorToken?: string;\n}\n\n/**\n * Session metadata returned from `POST /v1/sessions`. Surfaced to the `onBeforeSession`\n * hook so merchants can correlate an AgentScore session with their own resume token\n * (e.g. a pending-order id).\n */\nexport interface SessionMetadata {\n session_id: string;\n verify_url: string;\n poll_secret: string;\n poll_url: string;\n expires_at?: string;\n}\n\n/**\n * Configuration for auto-creating a verification session when no identity is present.\n *\n * The static `context` / `productName` options are sent on every session request. For\n * per-request context (e.g. the specific product the agent was trying to buy), pass\n * a `getSessionOptions` callback that returns dynamic values; its return is merged\n * over the static defaults.\n *\n * `onBeforeSession` is a side-effect hook that runs after the session is minted but\n * before the 403 is built. Use it to pre-create a reservation/draft/pending-order\n * row in your DB so agents can resume via a merchant-specific id. Return value is\n * merged into `DenialReason.extra`, so it surfaces in both the default 403 body and\n * in a custom `onDenied` handler.\n */\nexport interface CreateSessionOnMissing<TCtx = unknown> {\n apiKey: string;\n baseUrl?: string;\n context?: string;\n productName?: string;\n /** Per-request override of `context` / `productName`. Invoked with the framework context. */\n getSessionOptions?: (ctx: TCtx) => Promise<{ context?: string; productName?: string }>\n | { context?: string; productName?: string };\n /** Side-effect hook that runs after the session is minted. Return value is merged\n * into `DenialReason.extra` so custom `onDenied` handlers can include merchant-specific\n * fields (e.g. `order_id`) in the 403 response. Hook errors are logged and swallowed —\n * a failing side effect should not block the 403 from reaching the agent. */\n onBeforeSession?: (ctx: TCtx, session: SessionMetadata) => Promise<Record<string, unknown>>\n | Record<string, unknown>;\n}\n\nexport interface AgentScoreCoreOptions {\n /** AgentScore API key. Required. */\n apiKey: string;\n /** Require KYC verification. */\n requireKyc?: boolean;\n /** Require operator to be clear of sanctions. */\n requireSanctionsClear?: boolean;\n /** Minimum operator age bracket (18 or 21). */\n minAge?: number;\n /** List of blocked jurisdictions (blocklist). */\n blockedJurisdictions?: string[];\n /** List of allowed jurisdictions (allowlist — only these pass). */\n allowedJurisdictions?: string[];\n /** If true, allow the request through when the API is unreachable. Defaults to false. */\n failOpen?: boolean;\n /** How long to cache results, in seconds. Defaults to 300. */\n cacheSeconds?: number;\n /** AgentScore API base URL. Defaults to \"https://api.agentscore.sh\". */\n baseUrl?: string;\n /** Optional chain to filter scoring to. */\n chain?: string;\n /** Prepended to the default User-Agent as `\"{userAgent} (@agent-score/commerce@{version})\"`. Use to attribute API calls to your app. */\n userAgent?: string;\n /** When set and no identity is found, create a verification session instead of denying immediately. */\n createSessionOnMissing?: CreateSessionOnMissing;\n}\n\nexport type DenialCode =\n | 'wallet_not_trusted'\n | 'missing_identity'\n | 'api_error'\n | 'payment_required'\n | 'identity_verification_required'\n // Wallet-signer binding: claimed X-Wallet-Address must resolve to the same operator as\n // the payment signer; wallet-auth is rejected on rails with no wallet signer.\n | 'wallet_signer_mismatch'\n | 'wallet_auth_requires_wallet_signing'\n // Granular credential-state denials passed through from /v1/assess.\n // - `token_expired`: revoked + TTL-expired (the API unifies these). The 401 body carries\n // an auto-minted session, forwarded into DenialReason for one-shot recovery.\n // - `invalid_credential`: the token doesn't exist at all (typo, never minted, fabricated).\n // No auto-session is minted by the API for this case — agents should switch to a\n // different stored token or drop the header to bootstrap a fresh session.\n | 'token_expired'\n | 'invalid_credential';\n\n/**\n * Cross-merchant pattern hint emitted on bootstrap denials and session-create responses.\n * LLM-hosted agents should persist this to long-term memory so future AgentScore encounters skip\n * the cold-start session flow. Contents are the pattern, not secrets.\n */\nexport interface AgentMemoryHint {\n save_for_future_agentscore_gates: true;\n pattern_summary: string;\n quickstart: string;\n identity_check_endpoint: string;\n list_wallets_endpoint?: string;\n identity_paths: { wallet: string; operator_token: string };\n bootstrap: string;\n do_not_persist_in_memory: string[];\n persist_in_credential_store: string[];\n}\n\nexport interface DenialReason {\n code: DenialCode;\n /** Human-readable explanation. When omitted, `denialReasonToBody` substitutes a per-code default. */\n message?: string;\n decision?: string;\n reasons?: string[];\n verify_url?: string;\n session_id?: string;\n poll_secret?: string;\n poll_url?: string;\n agent_instructions?: string;\n /** Cross-merchant memory hint. Emitted on bootstrap denials only by default. */\n agent_memory?: AgentMemoryHint;\n /** Full assess response when the denial came from `/v1/assess`. Lets consumers access fields\n * not promoted to first-class DenialReason properties (e.g., `policy_result`). Undefined for\n * denials that did not originate from an assess call (missing_identity, api_error,\n * payment_required, identity_verification_required). */\n data?: AgentScoreData;\n /** Extra fields returned from the `createSessionOnMissing.onBeforeSession` hook. Merged\n * into the default 403 body; custom `onDenied` handlers can spread these into their own\n * response shape (e.g. to include a merchant-minted `order_id`). */\n extra?: Record<string, unknown>;\n // ---------------------------------------------------------------------------\n // Wallet-signer-match fields — populated for wallet_signer_mismatch only.\n // ---------------------------------------------------------------------------\n /** Operator id resolved from `X-Wallet-Address`. */\n claimed_operator?: string;\n /** Operator id the actual payment signer resolves to. `null` when the signer wallet isn't\n * linked to any operator (treat as a different identity). */\n actual_signer_operator?: string | null;\n /** The wallet the agent claimed via header. Echoed back for self-correction. */\n expected_signer?: string;\n /** The wallet that actually signed the payment. */\n actual_signer?: string;\n /** Wallets the claimed operator could sign with (if enumerable). Present when non-empty. */\n linked_wallets?: string[];\n}\n\nexport interface AgentScoreData {\n decision: string | null;\n decision_reasons: string[];\n identity_method?: string;\n operator_verification?: {\n level: string;\n operator_type: string | null;\n verified_at: string | null;\n };\n /** Account-level KYC facts that apply to every operator under the same account.\n * Populated when the API returns account_verification (post-KYC operator). */\n account_verification?: {\n kyc_level?: string;\n sanctions_clear?: boolean;\n age_bracket?: string;\n jurisdiction?: string;\n verified_at?: string | null;\n };\n resolved_operator?: string | null;\n /** Wallets linked to the same operator as the resolved identity. Capped at 100 entries\n * by the API. Useful for advertising in 402 challenges so wallet-auth agents know which\n * alt-signers will satisfy `wallet_signer_mismatch`. */\n linked_wallets?: string[];\n verify_url?: string;\n policy_result?: {\n all_passed: boolean;\n checks: Array<{\n rule: string;\n passed: boolean;\n required?: unknown;\n actual?: unknown;\n }>;\n } | null;\n}\n\n/**\n * Outcome from `AgentScoreCore.evaluate()`. Adapters map this to framework-specific responses.\n *\n * - `{ kind: 'allow', data }` — the request passed the policy. `data` is present on a normal\n * allow; `undefined` when fail-open short-circuited (identity missing, API unreachable,\n * timeout, or 402 paid-tier required).\n * - `{ kind: 'deny', reason }` — the request was denied. Adapters should render a 403 with the\n * reason, or invoke the caller's custom denial handler.\n */\nexport type EvaluateOutcome =\n | { kind: 'allow'; data?: AgentScoreData }\n | { kind: 'deny'; reason: DenialReason };\n\nexport interface CaptureWalletOptions {\n /** Operator credential (`opc_...`) that the agent authenticated with. */\n operatorToken: string;\n /** Signer wallet recovered from the payment payload. */\n walletAddress: string;\n /** Key-derivation family — `\"evm\"` for any EVM chain, `\"solana\"` for Solana. */\n network: 'evm' | 'solana';\n /** Optional stable key for the logical payment (e.g., payment intent id, tx hash). When the\n * same key is seen again for the same (credential, wallet, network), the server no-ops —\n * prevents agent retries from inflating transaction_count. */\n idempotencyKey?: string;\n}\n\nexport interface VerifyWalletSignerMatchOptions {\n /** The wallet claimed via `X-Wallet-Address`. */\n claimedWallet: string;\n /** The signer wallet recovered from the payment credential. `null` means the rail carries\n * no wallet signer (SPT, card) — the helper returns `wallet_auth_requires_wallet_signing`. */\n signer: string | null;\n /** Network of the signer. EVM covers every EVM chain; `solana` lives in its own namespace. */\n network?: 'evm' | 'solana';\n}\n\nexport type VerifyWalletSignerResult =\n | { kind: 'pass'; claimedOperator: string | null; signerOperator: string | null }\n | {\n kind: 'wallet_signer_mismatch';\n claimedOperator: string | null;\n actualSignerOperator: string | null;\n expectedSigner: string;\n actualSigner: string;\n linkedWallets: string[];\n /** JSON-encoded action copy (action + steps + user_message). Spread into the 403 body\n * verbatim so agents get a concrete recovery path inside the denial response itself. */\n agentInstructions: string;\n }\n | {\n kind: 'wallet_auth_requires_wallet_signing';\n claimedWallet: string;\n agentInstructions: string;\n }\n // Transient — the resolve call to /v1/assess failed or timed out. Caller should\n // retry or surface as 503. Distinct from wallet_signer_mismatch (which is an actual\n // security reject) so legitimate users don't get rejected on network flakes.\n | { kind: 'api_error'; claimedWallet: string };\n\nexport interface AgentScoreCore {\n /**\n * Evaluate the request's identity against the configured policy.\n * @param identity - extracted identity (wallet address and/or operator token)\n * @param ctx - optional framework-specific context (Hono c, Express req, etc.) passed\n * through to `createSessionOnMissing` hooks. Opaque to core.\n */\n evaluate(identity: AgentIdentity | undefined, ctx?: unknown): Promise<EvaluateOutcome>;\n /** Report a wallet seen paying under an operator credential. Fire-and-forget; silently\n * swallows non-fatal errors because capture is informational, not on the critical path. */\n captureWallet(options: CaptureWalletOptions): Promise<void>;\n /**\n * Verify the payment signer resolves to the same operator as the claimed `X-Wallet-Address`.\n *\n * Returns `pass` when the signer is linked to the same operator as the claimed wallet\n * (byte-equal wallets pass trivially; other wallets linked to the same operator also pass —\n * multi-wallet agents work without ergonomic pain). Returns `wallet_signer_mismatch` when\n * the signer resolves to a different (or no) operator. Returns `wallet_auth_requires_wallet_signing`\n * when the signer is `null` (SPT, card — rails that carry no wallet signature).\n *\n * Call this AFTER the gate evaluates (so the claimed wallet's operator is cached) and\n * AFTER the payment credential is parsed (so the signer is known). Merchants should call\n * it before settling payment.\n */\n verifyWalletSignerMatch(options: VerifyWalletSignerMatchOptions): Promise<VerifyWalletSignerResult>;\n}\n\n// ---------------------------------------------------------------------------\n// Internal types\n// ---------------------------------------------------------------------------\n\ninterface AssessResult {\n allow: boolean;\n decision?: string;\n reasons?: string[];\n raw?: unknown;\n}\n\n/**\n * Build the cross-merchant memory hint emitted on bootstrap denials. Base URLs are\n * derived from the gate's AgentScore API base so self-hosted / staging deployments get\n * correct pointers. Contents describe the AgentScore identity substrate in transferable\n * terms; merchant-specific context lives in other `agent_instructions` fields.\n */\n// Canonical production AgentScore API — used as the authoritative source for endpoint pointers\n// emitted to agent memory regardless of how a given merchant configured their gate's baseUrl.\n// Hardcoded so a malicious merchant can't set `baseUrl: \"evil.com\"` and phish agents into\n// sending their credentials to attacker-controlled endpoints.\nconst CANONICAL_AGENTSCORE_API = 'https://api.agentscore.sh';\n\n// JSON-encoded action copy emitted on wallet-signer-match denials. Spread into 403 bodies\n// by merchants so agents get a concrete recovery path inside the denial response itself —\n// no discovery-doc round trip required.\nconst WALLET_SIGNER_MISMATCH_INSTRUCTIONS = JSON.stringify({\n action: 'resign_or_switch_to_operator_token',\n steps: [\n 'Preferred: re-submit the payment signed by expected_signer (or any entry in linked_wallets — same-operator wallets are fungible) and retry with the same X-Wallet-Address.',\n 'Alternative: drop X-Wallet-Address and retry with X-Operator-Token. Use a stored opc_... if you have one; otherwise retry this request with NO identity header — the merchant will mint a verification session in the 403 body (verify_url + poll_secret). Share verify_url with the user, poll, receive a fresh opc_...',\n ],\n user_message:\n 'The payment signer resolves to a different operator than X-Wallet-Address. Re-sign from expected_signer or any linked_wallets entry, or switch to X-Operator-Token.',\n});\n\nconst WALLET_AUTH_REQUIRES_WALLET_SIGNING_INSTRUCTIONS = JSON.stringify({\n action: 'switch_to_operator_token',\n steps: [\n 'This payment rail (Stripe SPT, card) carries no wallet signature — X-Wallet-Address cannot be verified against the payment.',\n 'Drop X-Wallet-Address and retry with X-Operator-Token. If you do not have a stored opc_..., retry with no identity header to receive a verification session.',\n ],\n user_message:\n 'Wallet-address identity is only supported on wallet-signing rails (Tempo MPP, x402). On Stripe or card, use X-Operator-Token instead.',\n});\n\n// `invalid_credential` is permanent — the token doesn't exist (typo, never minted, fabricated).\n// Distinct from `token_expired` (which carries an auto-session for one-shot recovery): the\n// API doesn't auto-mint a session here because the client may simply have a different valid\n// token to try. Agents should switch tokens or drop the header to bootstrap a session.\nconst INVALID_CREDENTIAL_INSTRUCTIONS = JSON.stringify({\n action: 'switch_token_or_restart_session',\n steps: [\n 'The X-Operator-Token you sent does not match any credential. This is a permanent state — retrying with the same token will keep failing.',\n 'If you have other stored opc_... credentials, retry with one of them.',\n 'Otherwise drop X-Operator-Token and retry with no identity header — the merchant will mint a fresh verification session in the 403 body (verify_url + poll_secret) so the user can re-verify and you can poll for a new operator_token.',\n ],\n user_message:\n 'The operator_token is not recognized. Use a different stored token, or restart the verification session flow to mint a new one.',\n});\n\nexport function buildAgentMemoryHint(): AgentMemoryHint {\n // Memory pointers always point at the canonical production API — merchants must not be\n // able to redirect the cross-merchant memory surface, since that would be a phishing\n // vector for the verify URL.\n const api = CANONICAL_AGENTSCORE_API;\n return {\n save_for_future_agentscore_gates: true,\n pattern_summary:\n 'AgentScore is a cross-merchant identity substrate for agent commerce. Any endpoint that ' +\n 'returns 403 missing_identity / identity_verification_required or 402 challenges referencing AgentScore uses ' +\n 'the same identity system. One KYC works across all AgentScore merchants — no re-verification ' +\n 'per site. Save this pattern (and the endpoints below) to persistent memory so future ' +\n 'AgentScore encounters skip the cold-start session flow.',\n quickstart: 'https://docs.agentscore.sh/guides/agent-identity',\n identity_check_endpoint: `${api}/v1/credentials`,\n identity_paths: {\n wallet:\n 'If paying from a crypto wallet you have used before on any AgentScore merchant, send ' +\n '`X-Wallet-Address: 0x...`. Shortest path; no token lifecycle to manage.',\n operator_token:\n 'For any rail (Stripe SPT, card, or a wallet you have not linked yet), send ' +\n '`X-Operator-Token: opc_...`. Reusable across merchants until the token expires.',\n },\n bootstrap:\n 'If you have neither a linked wallet nor a valid operator_token, follow the session/verify ' +\n 'flow in the per-request `agent_instructions` block. This happens at most once per agent ' +\n 'identity — after first verification, the operator_token and any subsequently used wallet ' +\n 'are reusable everywhere.',\n do_not_persist_in_memory: ['operator_token', 'poll_secret'],\n persist_in_credential_store: ['operator_token'],\n };\n}\n\n// ---------------------------------------------------------------------------\n// Core factory\n// ---------------------------------------------------------------------------\n\nexport function createAgentScoreCore(options: AgentScoreCoreOptions): AgentScoreCore {\n if (!options.apiKey) {\n throw new Error('AgentScore API key is required. Get one at https://agentscore.sh/sign-up');\n }\n\n const {\n apiKey,\n requireKyc,\n requireSanctionsClear,\n minAge,\n blockedJurisdictions,\n allowedJurisdictions,\n failOpen = false,\n cacheSeconds = 300,\n baseUrl: rawBaseUrl = 'https://api.agentscore.sh',\n chain: gateChain,\n userAgent,\n createSessionOnMissing,\n } = options;\n\n const baseUrl = stripTrailingSlashes(rawBaseUrl);\n const agentMemoryHint = buildAgentMemoryHint();\n\n const defaultUa = `@agent-score/commerce@${__VERSION__}`;\n const userAgentHeader = userAgent ? `${userAgent} (${defaultUa})` : defaultUa;\n\n const API_TIMEOUT_MS = 10_000;\n\n const cache = new TTLCache<AssessResult>(cacheSeconds * 1000);\n\n // Mint a verification session via /v1/sessions and return the resulting\n // identity_verification_required DenialReason — or undefined if the mint failed (network\n // error, non-2xx, missing fields). Used for both the missing-identity path and the\n // fixable-wallet bootstrap path: in both cases the UX is identical (agent polls the\n // returned poll_url until it gets a fresh opc_... and retries).\n async function tryMintSessionDenial(ctx: unknown): Promise<DenialReason | undefined> {\n if (!createSessionOnMissing) return undefined;\n try {\n const sessionBody: { context?: string; product_name?: string } = {};\n if (createSessionOnMissing.context != null) sessionBody.context = createSessionOnMissing.context;\n if (createSessionOnMissing.productName != null) sessionBody.product_name = createSessionOnMissing.productName;\n\n if (createSessionOnMissing.getSessionOptions && ctx !== undefined) {\n try {\n const dynamic = await createSessionOnMissing.getSessionOptions(ctx);\n if (dynamic?.context != null) sessionBody.context = dynamic.context;\n if (dynamic?.productName != null) sessionBody.product_name = dynamic.productName;\n } catch (err) {\n console.warn('[gate] createSessionOnMissing.getSessionOptions hook failed:', err instanceof Error ? err.message : err);\n }\n }\n\n const sessionBaseUrl = stripTrailingSlashes(createSessionOnMissing.baseUrl ?? 'https://api.agentscore.sh');\n const sessionRes = await fetch(`${sessionBaseUrl}/v1/sessions`, {\n method: 'POST',\n headers: {\n 'X-API-Key': createSessionOnMissing.apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify(sessionBody),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n\n if (!sessionRes.ok) return undefined;\n const data = (await sessionRes.json()) as Record<string, unknown>;\n\n // Validate required fields before trusting the response. A misbehaving (or mocked-wrong)\n // API could 200 without session_id/poll_secret/verify_url, which would propagate\n // `undefined` into the 403 body and leave the agent stuck — treat as session-create\n // failure and fall back to the caller's bare denial.\n if (\n typeof data.session_id !== 'string' ||\n typeof data.poll_secret !== 'string' ||\n typeof data.verify_url !== 'string'\n ) {\n console.warn('[gate] /v1/sessions returned 200 without required fields — falling back to bare denial');\n return undefined;\n }\n\n // Run onBeforeSession side-effect hook. Errors are swallowed — a failing DB write\n // (e.g. can't insert pending order) should not block the 403.\n let extra: Record<string, unknown> | undefined;\n if (createSessionOnMissing.onBeforeSession && ctx !== undefined) {\n try {\n const sessionMeta = {\n session_id: data.session_id as string,\n verify_url: data.verify_url as string,\n poll_secret: data.poll_secret as string,\n poll_url: data.poll_url as string,\n expires_at: data.expires_at as string | undefined,\n };\n const result = await createSessionOnMissing.onBeforeSession(ctx, sessionMeta);\n if (result && typeof result === 'object') extra = result;\n } catch (err) {\n console.warn('[gate] createSessionOnMissing.onBeforeSession hook failed:', err instanceof Error ? err.message : err);\n }\n }\n\n // The API emits `next_steps` (structured object) on /v1/sessions success. Stringify it\n // into the gate's `agent_instructions` contract so merchants get the same JSON-encoded\n // {action, steps, user_message} envelope as every other gate-emitted denial.\n const apiNextSteps = data.next_steps as Record<string, unknown> | undefined;\n return {\n code: 'identity_verification_required',\n verify_url: data.verify_url as string,\n session_id: data.session_id as string,\n poll_secret: data.poll_secret as string,\n poll_url: data.poll_url as string | undefined,\n agent_instructions: apiNextSteps ? JSON.stringify(apiNextSteps) : undefined,\n agent_memory: agentMemoryHint,\n ...(extra && { extra }),\n };\n } catch (err) {\n // Session-mint failed (network, /v1/sessions returned non-2xx, body parse error,\n // onBeforeSession threw inside the inner try). Caller falls back to a bare denial —\n // agents still get a 403 with a probe-strategy hint. Log loudly so a persistent\n // /v1/sessions outage isn't masked.\n console.warn('[gate] createSessionOnMissing path failed — falling back to bare denial:', err instanceof Error ? err.message : err);\n return undefined;\n }\n }\n\n async function evaluate(identity: AgentIdentity | undefined, ctx?: unknown): Promise<EvaluateOutcome> {\n // Treat \"returned identity object with no usable fields\" the same as \"no identity at all\" —\n // otherwise a misbehaving custom extractIdentity would send an empty body to /v1/assess.\n if (!identity || (!identity.address && !identity.operatorToken)) {\n if (failOpen) return { kind: 'allow' };\n\n const sessionReason = await tryMintSessionDenial(ctx);\n if (sessionReason) return { kind: 'deny', reason: sessionReason };\n\n // Bare missing-identity denial (no session was auto-created). Describe the probe\n // strategy so agents without memory can recover: try wallet first on signing rails,\n // fall back to stored opc_..., fall back to session flow (noting that only\n // auto-session merchants return verify_url + poll data on the retry).\n const missingIdentityInstructions = JSON.stringify({\n action: 'probe_identity_then_session',\n steps: [\n 'If you have a wallet AND this merchant accepts a wallet-signing rail (Tempo MPP or x402 — see openapi.json x-payment-info): send X-Wallet-Address and retry. If the wallet is already linked to an AgentScore operator (via prior SIWE claim or prior captureWallet), this passes in one round trip. If the wallet is unlinked or the account has no KYC, the 403 will include a verify_url — share it with the user to claim the wallet + complete KYC, then retry.',\n 'If step 1 is denied or you already have a stored operator_token (valid + not expired): send X-Operator-Token: opc_... and retry.',\n 'If neither applies: retry with NO identity header. Merchants that auto-create verification sessions (most AgentScore merchants do) return verify_url + session_id + poll_secret in the 403 body — share verify_url with the user, then poll poll_url every 5s with the X-Poll-Secret header until status=verified (the poll returns a one-time operator_token). If the retry returns the same bare 403, this merchant does not support self-service session bootstrapping — direct the user to https://agentscore.sh/sign-up to create an AgentScore identity and mint an operator_token from their dashboard (https://agentscore.sh/dashboard/verify). The user hands the opc_... to you, and you retry with X-Operator-Token.',\n ],\n user_message:\n 'Try X-Wallet-Address first if you have a wallet and the merchant accepts Tempo/x402; fall back to a stored X-Operator-Token, then to the session/verify flow described in agent_memory.bootstrap.',\n });\n return {\n kind: 'deny',\n reason: {\n code: 'missing_identity',\n agent_instructions: missingIdentityInstructions,\n agent_memory: agentMemoryHint,\n },\n };\n }\n\n // operator_token is opaque + ASCII-only — lowercasing is safe. Wallet addresses go\n // through normalizeAddress because Solana base58 is case-sensitive and lowercasing\n // would corrupt the cache key (a Solana cache miss every time, plus collision risk\n // with mixed-case variants of the same operator).\n const cacheKey = identity.operatorToken?.toLowerCase() ?? (identity.address ? normalizeAddress(identity.address) : '');\n\n const cached = cache.get(cacheKey);\n if (cached) {\n if (cached.allow) {\n return { kind: 'allow', data: cached.raw as AgentScoreData };\n }\n // Fixable compliance denials (kyc_required, kyc_pending, kyc_failed) get the\n // same UX as missing_identity: mint a fresh verification session, agent polls\n // until status=verified, gets a fresh opc_..., retries. Unfixable reasons\n // (sanctions_flagged, age_insufficient, jurisdiction_restricted) keep the bare\n // wallet_not_trusted denial. `jurisdiction_restricted` is unfixable: the API\n // only emits it after KYC is verified (the user's KYC'd country is in the\n // blocked list — re-doing KYC won't change the country).\n if (isFixableDenial(cached.reasons)) {\n const sessionReason = await tryMintSessionDenial(ctx);\n if (sessionReason) return { kind: 'deny', reason: sessionReason };\n }\n return {\n kind: 'deny',\n reason: {\n code: 'wallet_not_trusted',\n decision: cached.decision,\n reasons: cached.reasons,\n verify_url: (cached.raw as Record<string, unknown> | undefined)?.verify_url as string | undefined,\n data: cached.raw as AgentScoreData | undefined,\n },\n };\n }\n\n try {\n const body: Record<string, unknown> = {};\n if (identity.address) body.address = identity.address;\n if (identity.operatorToken) body.operator_token = identity.operatorToken;\n if (gateChain) body.chain = gateChain;\n\n const policy: Record<string, unknown> = {};\n if (requireKyc != null) policy.require_kyc = requireKyc;\n if (requireSanctionsClear != null) policy.require_sanctions_clear = requireSanctionsClear;\n if (minAge != null) policy.min_age = minAge;\n if (blockedJurisdictions != null) policy.blocked_jurisdictions = blockedJurisdictions;\n if (allowedJurisdictions != null) policy.allowed_jurisdictions = allowedJurisdictions;\n if (Object.keys(policy).length > 0) body.policy = policy;\n\n const response = await fetch(`${baseUrl}/v1/assess`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify(body),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n\n if (response.status === 402) {\n if (failOpen) return { kind: 'allow' };\n return { kind: 'deny', reason: { code: 'payment_required' } };\n }\n\n // Pass through the API's token_expired 401 (covers both expired and revoked\n // credentials — the API deliberately doesn't distinguish). The 401 body carries\n // an auto-minted session (verify_url + session_id + poll_secret + next_steps +\n // agent_memory) so agents can recover without holding an API key. Forward all of\n // that into the DenialReason so the gate's 403 body includes the session fields.\n if (response.status === 401) {\n try {\n const errData = (await response.clone().json()) as {\n error?: { code?: string };\n session_id?: unknown;\n poll_secret?: unknown;\n verify_url?: unknown;\n poll_url?: unknown;\n next_steps?: unknown;\n agent_memory?: unknown;\n };\n const code = errData?.error?.code;\n if (code === 'token_expired') {\n return {\n kind: 'deny',\n reason: {\n code,\n data: errData as unknown as AgentScoreData,\n ...(typeof errData.verify_url === 'string' ? { verify_url: errData.verify_url } : {}),\n ...(typeof errData.session_id === 'string' ? { session_id: errData.session_id } : {}),\n ...(typeof errData.poll_secret === 'string' ? { poll_secret: errData.poll_secret } : {}),\n ...(typeof errData.poll_url === 'string' ? { poll_url: errData.poll_url } : {}),\n ...(errData.next_steps ? { agent_instructions: JSON.stringify(errData.next_steps) } : {}),\n ...(errData.agent_memory ? { agent_memory: errData.agent_memory as AgentMemoryHint } : {}),\n },\n };\n }\n // The API returns this when the operator_token doesn't exist at all (typo,\n // never minted, fabricated). Distinct from token_expired — no auto-session\n // is issued because the agent may have other valid tokens to try first.\n // Without this branch the gate would fall through to api_error → 503 retry,\n // which loops forever on a permanent state.\n if (code === 'invalid_credential') {\n return {\n kind: 'deny',\n reason: {\n code: 'invalid_credential',\n agent_instructions: INVALID_CREDENTIAL_INSTRUCTIONS,\n agent_memory: agentMemoryHint,\n },\n };\n }\n // Unknown 401 code — log so we notice if the API adds a new credential-state\n // code without us mapping it. Falls through to api_error below.\n if (code) {\n console.warn(`[gate] /v1/assess returned 401 ${code} — no specific handler, surfacing as api_error.`);\n }\n } catch (err) {\n // Body wasn't the expected JSON shape. Don't crash the request, but DO log —\n // a silent swallow here used to mask /v1/sessions schema drift for hours.\n console.warn('[gate] /v1/assess 401 body parse failed:', err instanceof Error ? err.message : err);\n }\n }\n\n // 4xx with a structured error body that ISN'T 401/402: log it so operators see\n // misclassifications instead of opaque 503 retries. Most common cause: a merchant\n // that didn't validate input shape before invoking the gate (invalid_address,\n // invalid_identity). We still fall through to api_error so behavior is unchanged\n // for callers — just visible now.\n if (response.status >= 400 && response.status < 500 && response.status !== 402) {\n try {\n const errData = (await response.clone().json()) as { error?: { code?: string; message?: string } };\n const code = errData?.error?.code;\n if (code && code !== 'token_expired' && code !== 'invalid_credential') {\n console.warn(\n `[gate] /v1/assess returned ${response.status} ${code} — surfacing as api_error. ` +\n 'Validate input shape before invoking the gate to avoid this.',\n );\n }\n } catch {\n // Body wasn't JSON or didn't have the expected shape — silent fall-through.\n }\n }\n\n if (!response.ok) {\n throw new Error(`AgentScore API returned ${response.status}`);\n }\n\n const data = (await response.json()) as Record<string, unknown>;\n const decision = data.decision as string | null | undefined;\n const decisionReasons = (data.decision_reasons as string[]) ?? [];\n const allow = decision === 'allow' || decision == null;\n\n cache.set(cacheKey, { allow, decision: decision ?? undefined, reasons: decisionReasons, raw: data });\n\n if (allow) {\n return { kind: 'allow', data: data as unknown as AgentScoreData };\n }\n\n // Fixable compliance denials (kyc_required, kyc_pending, kyc_failed) get the\n // same UX as missing_identity: mint a fresh verification session, agent polls\n // until status=verified, gets a fresh opc_..., retries. Unfixable reasons\n // (sanctions_flagged, age_insufficient, jurisdiction_restricted) keep the bare\n // wallet_not_trusted denial. `jurisdiction_restricted` is unfixable: the API\n // only emits it after KYC is verified (the user's KYC'd country is in the\n // blocked list — re-doing KYC won't change the country).\n if (isFixableDenial(decisionReasons)) {\n const sessionReason = await tryMintSessionDenial(ctx);\n if (sessionReason) return { kind: 'deny', reason: sessionReason };\n }\n\n return {\n kind: 'deny',\n reason: {\n code: 'wallet_not_trusted',\n decision: decision ?? undefined,\n reasons: decisionReasons,\n verify_url: data.verify_url as string | undefined,\n data: data as unknown as AgentScoreData,\n },\n };\n } catch (err) {\n // Network failure, AbortSignal timeout, JSON parse error on a 2xx, or the\n // explicit `throw new Error(...)` for an unhandled non-ok status. Log so ops\n // can distinguish \"API down\" from \"merchant config wrong\" — without this,\n // every transient blip looked identical to a misconfigured base URL.\n console.warn('[gate] /v1/assess call failed — surfacing as api_error:', err instanceof Error ? err.message : err);\n if (failOpen) return { kind: 'allow' };\n return { kind: 'deny', reason: { code: 'api_error' } };\n }\n }\n\n async function captureWallet(options: CaptureWalletOptions): Promise<void> {\n try {\n const body: Record<string, unknown> = {\n operator_token: options.operatorToken,\n wallet_address: options.walletAddress,\n network: options.network,\n };\n if (options.idempotencyKey) body.idempotency_key = options.idempotencyKey;\n await fetch(`${baseUrl}/v1/credentials/wallets`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify(body),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n } catch (err) {\n // Fire-and-forget: don't throw. Log so a persistent capture outage is visible\n // to merchant ops — otherwise wallet↔operator linkage silently stops.\n console.warn('[agentscore-commerce] captureWallet failed:', err instanceof Error ? err.message : err);\n }\n }\n\n /**\n * Resolve a wallet to its operator id via /v1/assess.\n *\n * Returns:\n * - `{ ok: true, operator: <id> }` — wallet is linked to that operator\n * - `{ ok: true, operator: null }` — wallet is valid but not linked to any operator\n * - `{ ok: false }` — the API call failed (network, timeout, non-2xx). Distinguishable so\n * callers can emit `api_error` instead of falsely asserting \"no operator linked\".\n *\n * Checks the main evaluate() cache before making a fresh call — if the gate already\n * resolved this wallet during identity evaluation, we have the resolved_operator already.\n */\n async function resolveWalletToOperator(\n walletAddress: string,\n ): Promise<{ ok: true; operator: string | null; linkedWallets: string[] } | { ok: false }> {\n // Network-aware: lowercases EVM, preserves Solana base58 case. The DB stores both\n // formats verbatim in operator_credential_wallets.wallet_address; lowercasing a\n // Solana address would never match.\n const wallet = normalizeAddress(walletAddress);\n\n // Cache lookup — first the plain cache (populated by evaluate() for identity-headered wallets).\n // Saves a second /v1/assess call when the gate already looked up this wallet.\n const extractFromCached = (raw: Record<string, unknown>): { operator: string | null; linkedWallets: string[] } => {\n const op = raw.resolved_operator;\n const links = raw.linked_wallets;\n return {\n operator: typeof op === 'string' ? op : null,\n linkedWallets: Array.isArray(links) ? (links as unknown[]).filter((w): w is string => typeof w === 'string') : [],\n };\n };\n\n const plainCached = cache.get(wallet);\n if (plainCached?.raw) {\n return { ok: true, ...extractFromCached(plainCached.raw as Record<string, unknown>) };\n }\n const resolveCached = cache.get(`resolve:${wallet}`);\n if (resolveCached?.raw) {\n return { ok: true, ...extractFromCached(resolveCached.raw as Record<string, unknown>) };\n }\n\n try {\n const response = await fetch(`${baseUrl}/v1/assess`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify({ address: walletAddress }),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n if (!response.ok) return { ok: false };\n const data = (await response.json()) as Record<string, unknown>;\n cache.set(`resolve:${wallet}`, { allow: true, raw: data });\n return { ok: true, ...extractFromCached(data) };\n } catch (err) {\n // Network/timeout/parse on the wallet→operator resolve path. Caller maps\n // `{ok:false}` to `wallet_signer_mismatch.kind === 'api_error'`, which already\n // surfaces as 503 — but log here too so multi-wallet match failures aren't\n // silently indistinguishable from \"operator simply has no linked wallet\".\n console.warn('[gate] resolveWalletToOperator failed — returning { ok:false }:', err instanceof Error ? err.message : err);\n return { ok: false };\n }\n }\n\n function reportSignerEvent(kind: 'pass' | 'wallet_signer_mismatch' | 'wallet_auth_requires_wallet_signing' | 'api_error'): void {\n // Fire-and-forget: surfaces mismatch-catch rate + api_error SLO on the dashboard.\n // Never blocks, awaits, or throws — telemetry failure must not affect the gate's decision.\n try {\n const pending = fetch(`${baseUrl}/v1/telemetry/signer-match`, {\n method: 'POST',\n headers: {\n 'X-API-Key': apiKey,\n 'Content-Type': 'application/json',\n Accept: 'application/json',\n 'User-Agent': userAgentHeader,\n },\n body: JSON.stringify({ kind }),\n signal: AbortSignal.timeout(API_TIMEOUT_MS),\n });\n if (pending && typeof pending.catch === 'function') {\n pending.catch((err) => {\n console.warn('[agentscore-commerce] signer-match telemetry failed:', err instanceof Error ? err.message : err);\n });\n }\n } catch {\n // Thrown synchronously (e.g., fetch unavailable in test harness) — swallow silently.\n }\n }\n\n async function verifyWalletSignerMatch(\n options: VerifyWalletSignerMatchOptions,\n ): Promise<VerifyWalletSignerResult> {\n const { claimedWallet, signer } = options;\n\n if (!signer) {\n reportSignerEvent('wallet_auth_requires_wallet_signing');\n return {\n kind: 'wallet_auth_requires_wallet_signing',\n claimedWallet,\n agentInstructions: WALLET_AUTH_REQUIRES_WALLET_SIGNING_INSTRUCTIONS,\n };\n }\n\n // Network-aware normalization: lowercase EVM, preserve Solana base58. The byte-equal\n // short-circuit and downstream cache-key derivation MUST match how the DB stores\n // wallets; lowercasing Solana would corrupt both.\n const claimedNorm = normalizeAddress(claimedWallet);\n const signerNorm = normalizeAddress(signer);\n\n // Byte-equal short-circuit — no API lookup; same wallet ≡ same operator by definition.\n if (claimedNorm === signerNorm) {\n reportSignerEvent('pass');\n return { kind: 'pass', claimedOperator: null, signerOperator: null };\n }\n\n const [claimedResolve, signerResolve] = await Promise.all([\n resolveWalletToOperator(claimedNorm),\n resolveWalletToOperator(signerNorm),\n ]);\n\n // Transient API failure on either resolve → emit api_error. Caller should retry or\n // surface 503 rather than falsely reject a legitimate user on a network flake.\n if (!claimedResolve.ok || !signerResolve.ok) {\n reportSignerEvent('api_error');\n return { kind: 'api_error', claimedWallet: claimedNorm };\n }\n\n const claimedOperator = claimedResolve.operator;\n const signerOperator = signerResolve.operator;\n\n if (claimedOperator && signerOperator && claimedOperator === signerOperator) {\n reportSignerEvent('pass');\n return { kind: 'pass', claimedOperator, signerOperator };\n }\n\n reportSignerEvent('wallet_signer_mismatch');\n return {\n kind: 'wallet_signer_mismatch',\n claimedOperator,\n actualSignerOperator: signerOperator,\n expectedSigner: claimedNorm,\n actualSigner: signerNorm,\n // Populated from /v1/assess.linked_wallets on the claimed wallet — the full set of\n // wallets the agent CAN sign with to satisfy the claim (same-operator rule).\n linkedWallets: claimedResolve.linkedWallets,\n agentInstructions: WALLET_SIGNER_MISMATCH_INSTRUCTIONS,\n };\n }\n\n return { evaluate, captureWallet, verifyWalletSignerMatch };\n}\n","/**\n * Helpers for emitting the cross-merchant `agent_memory` hint on merchant 402 responses.\n *\n * The gate (`@agent-score/commerce/identity/*`) emits `agent_memory` on identity-related\n * responses (sessions, credentials, missing_identity bootstraps). Merchants can ALSO\n * include the hint in their own 402 challenge bodies on first-encounter requests so\n * agents persist the cross-merchant pattern even when entering the ecosystem through a\n * merchant-side endpoint rather than a direct AgentScore API call.\n *\n * Usage pattern:\n * - Merchant tracks per-operator (or per-IP / per-fingerprint) \"have I seen this agent\n * before?\" in their own DB\n * - On first encounter (no prior request from this operator/wallet/IP), include the hint\n * so the agent saves the pattern\n * - On subsequent encounters, skip — the agent already has it (or never will)\n *\n * The hint contents come from `buildAgentMemoryHint` (re-exported here for convenience).\n * Keep it stateless: AgentScore's pattern doesn't depend on the merchant's identity, so\n * every merchant emits the same shape.\n */\n\nimport { buildAgentMemoryHint, type AgentMemoryHint } from '../core';\n\nexport { buildAgentMemoryHint };\nexport type { AgentMemoryHint };\n\nexport interface FirstEncounterAgentMemoryInput {\n /**\n * Whether this is a first encounter for this operator/wallet/IP at this merchant.\n * Merchant-determined (DB lookup, cache flag, etc.). Pass `false` to suppress emission\n * cleanly without wrapping the call in an `if`.\n */\n firstEncounter: boolean;\n}\n\n/**\n * Returns the `agent_memory` hint when this is a first encounter, otherwise `undefined`.\n * Use directly with the `agentMemory` field of `build402Body`:\n *\n * ```ts\n * const body = build402Body({\n * acceptedMethods,\n * agentInstructions,\n * pricing,\n * agentMemory: firstEncounterAgentMemory({ firstEncounter: !this.hasSeenOperator(opToken) }),\n * });\n * ```\n *\n * Returning `undefined` means `build402Body` cleanly skips the field instead of emitting\n * `agent_memory: null` (which would imply \"I tried but failed\" rather than \"didn't apply\").\n */\nexport function firstEncounterAgentMemory(\n input: FirstEncounterAgentMemoryInput,\n): AgentMemoryHint | undefined {\n if (!input.firstEncounter) return undefined;\n return buildAgentMemoryHint();\n}\n","import type { AcceptedMethodEntry } from './accepted_methods';\nimport type { AgentInstructions } from './agent_instructions';\nimport type { IdentityMetadataBlock } from './identity';\nimport type { PricingBlock as _PricingBlock } from './pricing';\n\nexport type { PricingBlock } from './pricing';\n\nexport interface Build402BodyInput {\n /** From buildAcceptedMethods — list of MPP method entries. */\n acceptedMethods: AcceptedMethodEntry[];\n /** From buildAgentInstructions — wraps how_to_pay + warnings + recommended_tools. */\n agentInstructions?: AgentInstructions;\n /** From buildIdentityMetadata — wallet-mode echoer. Spread into the body when present. */\n identityMetadata?: IdentityMetadataBlock;\n /** Cross-merchant agent_memory hint (from gate). */\n agentMemory?: unknown;\n /** Pricing breakdown. */\n pricing?: _PricingBlock;\n /** Total amount in USD as a string (e.g., '250.00'). */\n amountUsd?: string;\n /** Currency code. Default 'USD'. */\n currency?: string;\n /** Order id for retry correlation. */\n orderId?: string | null;\n /** Product info — surfaced on the 402 so agents can confirm what they're buying. */\n product?: { id: string; name: string };\n /** The body the agent should retry with after payment (e.g., the original request body). */\n retryBody?: unknown;\n /** Recommended rail — agent's default if multiple are listed. */\n recommended?: string;\n /** x402-compliance fields (paired with the PAYMENT-REQUIRED header from `payment/wwwauthenticate`). */\n x402?: {\n accepts: unknown[];\n version?: 1 | 2;\n };\n /** Vendor-specific extra fields merged at the top level. */\n extra?: Record<string, unknown>;\n}\n\n/**\n * Assemble the full enriched 402 response body. Combines accepted_methods, agent_instructions,\n * identity metadata, pricing, x402 compliance fields, and any vendor-specific extras into a\n * single object suitable for `JSON.stringify`. Vendors pass only what they have; the builder\n * conditionally includes each section.\n *\n * Pair this with a Response that sets:\n * - 'content-type: application/json'\n * - 'www-authenticate: <wwwAuthenticateHeader([...])>' from `payment/wwwauthenticate`\n * - 'PAYMENT-REQUIRED: <paymentRequiredHeader({...})>' for x402 clients\n */\nexport function build402Body(input: Build402BodyInput): Record<string, unknown> {\n const body: Record<string, unknown> = {\n payment_required: true,\n accepted_methods: input.acceptedMethods,\n };\n\n if (input.x402) {\n body.x402Version = input.x402.version ?? 2;\n body.accepts = input.x402.accepts;\n }\n\n if (input.amountUsd !== undefined) body.amount_usd = input.amountUsd;\n if (input.currency) body.currency = input.currency;\n if (input.pricing) body.pricing = input.pricing;\n if (input.orderId !== undefined) body.order_id = input.orderId;\n if (input.product) body.product = input.product;\n if (input.recommended) body.recommended = input.recommended;\n if (input.retryBody !== undefined) body.retry_body = input.retryBody;\n\n if (input.identityMetadata) {\n Object.assign(body, input.identityMetadata);\n }\n\n if (input.agentInstructions) body.agent_instructions = input.agentInstructions;\n if (input.agentMemory !== undefined) body.agent_memory = input.agentMemory;\n\n if (input.extra) Object.assign(body, input.extra);\n\n return body;\n}\n","/**\n * Pricing block builder + canonical type.\n *\n * Composes the cents-denominated price components into the dollar-string shape that\n * 402 challenge bodies advertise. Lifts the inline pattern from martin-estate's\n * `purchase.ts` so every merchant — current and future commerce-platform plugins\n * (Commerce7, WooCommerce, Shopify) — surfaces the same shape to agents.\n *\n * Shipping is included by default because most physical-goods merchants carry it; pass\n * `shippingCents: 0` (or omit) for digital goods / services. Tax is optional for\n * merchants outside taxable jurisdictions.\n */\n\nexport interface PricingBlock {\n /** Pre-tax, pre-shipping subtotal as a dollar-string (e.g. `\"250.00\"`). */\n subtotal: string;\n /** Tax amount as a dollar-string. Always present even if `\"0.00\"`. */\n tax: string;\n /** Shipping cost as a dollar-string. Always present even if `\"0.00\"`. */\n shipping?: string;\n /** Final total = subtotal + tax + shipping, dollar-string. */\n total: string;\n /** Tax rate as a decimal fraction (e.g. `0.0775` for 7.75%). Optional — omit for tax-free merchants. */\n tax_rate?: number;\n /** ISO-3166-2 state code or jurisdiction name used for tax calc. Optional. */\n tax_state?: string;\n /** ISO-4217 currency code. Default `\"USD\"`. */\n currency?: string;\n}\n\nexport interface BuildPricingBlockInput {\n /** Pre-tax, pre-shipping subtotal in the smallest currency unit (cents, satoshi, etc.). */\n subtotalCents: number;\n /** Tax amount in the smallest currency unit. Default `0`. */\n taxCents?: number;\n /** Shipping cost in the smallest currency unit. Default `0`. */\n shippingCents?: number;\n /** Override the computed total. By default `subtotalCents + taxCents + shippingCents`. */\n totalCents?: number;\n /** Tax rate as a decimal fraction (e.g. `0.0775`). */\n taxRate?: number;\n /** Tax jurisdiction (state code, country, etc.). */\n taxState?: string;\n /** ISO-4217 currency. Default `\"USD\"`. */\n currency?: string;\n}\n\n/**\n * Compose a `PricingBlock` from cents-denominated inputs. Handles the cents → dollar-string\n * conversion (always 2 decimals) and computes the total when not explicitly provided.\n *\n * Example:\n * ```ts\n * const pricing = buildPricingBlock({\n * subtotalCents: 25000,\n * taxCents: 1875,\n * shippingCents: 999,\n * taxRate: 0.075,\n * taxState: 'CA',\n * });\n * // → { subtotal: '250.00', tax: '18.75', shipping: '9.99', total: '278.74', tax_rate: 0.075, tax_state: 'CA' }\n * ```\n *\n * Pass `shippingCents: 0` for digital goods if you want the field present (it's then `\"0.00\"`);\n * omit entirely if you don't want shipping in the response shape at all.\n */\nexport function buildPricingBlock(input: BuildPricingBlockInput): PricingBlock {\n const tax = input.taxCents ?? 0;\n const shipping = input.shippingCents ?? 0;\n const total = input.totalCents ?? input.subtotalCents + tax + shipping;\n\n const block: PricingBlock = {\n subtotal: formatCents(input.subtotalCents),\n tax: formatCents(tax),\n total: formatCents(total),\n };\n\n if (input.shippingCents !== undefined) block.shipping = formatCents(shipping);\n if (input.taxRate !== undefined) block.tax_rate = input.taxRate;\n if (input.taxState !== undefined) block.tax_state = input.taxState;\n if (input.currency !== undefined) block.currency = input.currency;\n\n return block;\n}\n\nfunction formatCents(cents: number): string {\n return (cents / 100).toFixed(2);\n}\n","/**\n * Joins multiple Payment directives into a single WWW-Authenticate header value.\n * Per RFC 7235, multiple challenges are comma-separated.\n */\nexport function wwwAuthenticateHeader(directives: string[]): string {\n return directives.join(', ');\n}\n\nexport interface PaymentRequiredHeaderInput {\n x402Version: 1 | 2;\n accepts: unknown[];\n resource?: { url: string; mimeType?: string };\n}\n\n/**\n * Add the v1↔v2 amount-field alias to each accepts entry. Idempotent. Used by both\n * `paymentRequiredHeader` (header emit) and `build402Body` (body emit) so every\n * x402 entry on the wire carries BOTH `amount` (v2 spec) AND `maxAmountRequired`\n * (v1 spec) — strict v1-only parsers (e.g. Coinbase awal at `payments-mcp.coinbase.com`,\n * which is hardcoded to read `maxAmountRequired`) work alongside strict v2 parsers,\n * which ignore the alias.\n */\nexport function aliasAmountFields(accepts: unknown[]): unknown[] {\n return accepts.map((entry) => {\n if (entry === null || typeof entry !== 'object') return entry;\n const e = entry as Record<string, unknown>;\n const hasAmount = e.amount !== undefined;\n const hasMaxAmount = e.maxAmountRequired !== undefined;\n if (hasAmount && !hasMaxAmount) return { ...e, maxAmountRequired: e.amount };\n if (hasMaxAmount && !hasAmount) return { ...e, amount: e.maxAmountRequired };\n return e;\n });\n}\n\n/**\n * Encode the standard x402 PAYMENT-REQUIRED header (base64-encoded JSON of the\n * PaymentRequired object). Clients that recognize the header (`@x402/fetch`,\n * `@x402/core` HTTPClient, `agentscore-pay`) prefer it over body fields.\n *\n * Note: do NOT add a v1↔v2 amount-field alias here. `@x402/core`'s\n * `findMatchingRequirements` uses `deepEqual` against the agent's signed\n * `accepted` payload — any field present on one side and missing on the other\n * (e.g. `maxAmountRequired` on the wire body but not in `buildPaymentRequirements`'s\n * output) makes the match silently fail at settle time. Keep `accepts` shape\n * identical to whatever `buildPaymentRequirements` produces server-side.\n */\nexport function paymentRequiredHeader(input: PaymentRequiredHeaderInput): string {\n return Buffer.from(JSON.stringify(input)).toString('base64');\n}\n","/**\n * `respond402` — single-call 402 emit for merchants who use both `mppx` (for tempo + stripe\n * MPP rails) AND x402 (for Base + Solana).\n *\n * The seam is fiddly enough to get wrong by hand:\n * - mppx's `compose()(req)` returns a 402 Response with WWW-Authenticate directives\n * whose ids mppx's server-side validator REMEMBERS — they round-trip in client\n * credentials. Overwriting that header (e.g. with `buildPaymentHeaders` output)\n * breaks the round-trip.\n * - x402 needs the binary-friendly `PAYMENT-REQUIRED` header (base64-encoded JSON\n * of `{x402Version, accepts, resource}`) — mppx doesn't emit it.\n * - Merchants want a richer JSON body (pricing, identity metadata, agent_instructions,\n * agent_memory, retry_body, accepted_methods cross-reference) than the bare mppx body.\n *\n * `respond402` composes all three in one call:\n * - PRESERVES mppx's WWW-Authenticate verbatim\n * - ADDS PAYMENT-REQUIRED when x402 entries are present\n * - REPLACES the body with the rich body via `build402Body`\n *\n * Usage:\n * ```ts\n * const result = await m.compose(['tempo/charge', {...}], ['stripe/charge', {...}])(c.req.raw);\n * if (result.status === 402) {\n * return commerce.respond402({\n * mppxChallenge: result.challenge,\n * x402: { version: 2, accepts: x402Accepts, resource: { url: c.req.url, mimeType: 'application/json' } },\n * body: { acceptedMethods, agentInstructions, identityMetadata, pricing, agentMemory, retryBody, ... },\n * });\n * }\n * ```\n */\n\nimport { paymentRequiredHeader, type PaymentRequiredHeaderInput } from '../payment/wwwauthenticate';\nimport { build402Body, type Build402BodyInput } from './body';\n\nexport interface Respond402Input {\n /** The 402 Response returned by `mppx.compose()(req)`. Its WWW-Authenticate header\n * is preserved verbatim — mppx's server-side validator matches credentials to the\n * directive ids it generated, so overwriting breaks the round-trip. */\n mppxChallenge: Response;\n /** Inputs to `build402Body` — the rich JSON body sent to the agent. */\n body: Build402BodyInput;\n /** When set, layers on the x402 PAYMENT-REQUIRED header (base64-encoded JSON).\n * Omit for merchants that don't accept x402 (Base/Solana) — mppx-only setups. */\n x402?: PaymentRequiredHeaderInput;\n}\n\nexport function respond402(input: Respond402Input): Response {\n const body = build402Body(input.body);\n const headers = new Headers(input.mppxChallenge.headers);\n headers.set('content-type', 'application/json');\n if (input.x402) {\n headers.set('PAYMENT-REQUIRED', paymentRequiredHeader(input.x402));\n }\n return new Response(JSON.stringify(body), { headers, status: 402 });\n}\n","/**\n * Build a structured 4xx validation-error body that pairs cleanly with the\n * existing 402 / 403 builders. Every commerce merchant returning helpful\n * `bad_request` / `not_found` / `out_of_stock` / etc. errors converges on the\n * same shape: `{ error: {code, message}, ...optional_hints, next_steps? }`.\n *\n * This builder doesn't choose the HTTP status — vendors wrap the returned\n * body in their framework's response (`c.json(body, 400)` in Hono,\n * `Response.json(body, {status: 400})` for the Web Fetch path, etc.). Status\n * stays the merchant's call because the same shape works for 400/404/409/422.\n */\nexport interface BuildValidationErrorInput {\n /** Machine-readable error code (e.g. 'bad_request', 'not_found', 'out_of_stock'). */\n code: string;\n /** Human-readable message — surfaced directly to the user via the agent. */\n message: string;\n /** Optional schema description of required body fields, keyed by field name. Surfaced\n * so agents can self-correct without fetching docs. */\n requiredFields?: Record<string, string>;\n /** Optional concrete example body. Pairs with `requiredFields` for max self-serve. */\n exampleBody?: unknown;\n /** Optional next-step hint block (`{action, user_message?, ...vendor_extras}`). */\n nextSteps?: Record<string, unknown>;\n /** Vendor-specific top-level fields merged into the body (e.g. `available`,\n * `blocked_states`, `max_length`). */\n extra?: Record<string, unknown>;\n}\n\nexport interface ValidationErrorBody {\n error: { code: string; message: string };\n required_fields?: Record<string, string>;\n example_body?: unknown;\n next_steps?: Record<string, unknown>;\n [key: string]: unknown;\n}\n\n/**\n * Compose a 4xx body that vendors return via their framework's response helper.\n * Combine with the merchant's chosen HTTP status (400 for body shape errors,\n * 404 for missing entities, 409 for stock conflicts, 403 for policy denials, etc.).\n *\n * Example:\n * ```ts\n * return c.json(buildValidationError({\n * code: 'bad_request',\n * message: 'product_id, email, and shipping are required',\n * requiredFields: { product_id: 'uuid', email: 'string', shipping: 'object' },\n * nextSteps: { action: 'retry_with_complete_body' },\n * }), 400);\n * ```\n */\nexport function buildValidationError(input: BuildValidationErrorInput): ValidationErrorBody {\n const body: ValidationErrorBody = {\n error: { code: input.code, message: input.message },\n };\n if (input.requiredFields) body.required_fields = input.requiredFields;\n if (input.exampleBody !== undefined) body.example_body = input.exampleBody;\n if (input.nextSteps) body.next_steps = input.nextSteps;\n if (input.extra) Object.assign(body, input.extra);\n return body;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC+DO,SAAS,qBAAqB,OAAyD;AAC5F,QAAM,MAA6B,CAAC;AAEpC,MAAI,MAAM,OAAO;AACf,QAAI,KAAK;AAAA,MACP,QAAQ;AAAA,MACR,SAAS,MAAM,MAAM,WAAW;AAAA,MAChC,UAAU,MAAM,MA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