@agent-score/commerce 1.0.0

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.
Files changed (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +306 -0
  3. package/dist/_response-DmziuJz6.d.mts +137 -0
  4. package/dist/_response-rbK0zM7y.d.ts +137 -0
  5. package/dist/api/index.d.mts +1 -0
  6. package/dist/api/index.d.ts +1 -0
  7. package/dist/api/index.js +37 -0
  8. package/dist/api/index.js.map +1 -0
  9. package/dist/api/index.mjs +14 -0
  10. package/dist/api/index.mjs.map +1 -0
  11. package/dist/challenge/index.d.mts +523 -0
  12. package/dist/challenge/index.d.ts +523 -0
  13. package/dist/challenge/index.js +354 -0
  14. package/dist/challenge/index.js.map +1 -0
  15. package/dist/challenge/index.mjs +318 -0
  16. package/dist/challenge/index.mjs.map +1 -0
  17. package/dist/core.d.mts +252 -0
  18. package/dist/core.d.ts +252 -0
  19. package/dist/core.js +500 -0
  20. package/dist/core.js.map +1 -0
  21. package/dist/core.mjs +472 -0
  22. package/dist/core.mjs.map +1 -0
  23. package/dist/discovery/index.d.mts +382 -0
  24. package/dist/discovery/index.d.ts +382 -0
  25. package/dist/discovery/index.js +675 -0
  26. package/dist/discovery/index.js.map +1 -0
  27. package/dist/discovery/index.mjs +630 -0
  28. package/dist/discovery/index.mjs.map +1 -0
  29. package/dist/identity/express.d.mts +44 -0
  30. package/dist/identity/express.d.ts +44 -0
  31. package/dist/identity/express.js +777 -0
  32. package/dist/identity/express.js.map +1 -0
  33. package/dist/identity/express.mjs +738 -0
  34. package/dist/identity/express.mjs.map +1 -0
  35. package/dist/identity/fastify.d.mts +63 -0
  36. package/dist/identity/fastify.d.ts +63 -0
  37. package/dist/identity/fastify.js +780 -0
  38. package/dist/identity/fastify.js.map +1 -0
  39. package/dist/identity/fastify.mjs +741 -0
  40. package/dist/identity/fastify.mjs.map +1 -0
  41. package/dist/identity/hono.d.mts +83 -0
  42. package/dist/identity/hono.d.ts +83 -0
  43. package/dist/identity/hono.js +779 -0
  44. package/dist/identity/hono.js.map +1 -0
  45. package/dist/identity/hono.mjs +740 -0
  46. package/dist/identity/hono.mjs.map +1 -0
  47. package/dist/identity/nextjs.d.mts +62 -0
  48. package/dist/identity/nextjs.d.ts +62 -0
  49. package/dist/identity/nextjs.js +784 -0
  50. package/dist/identity/nextjs.js.map +1 -0
  51. package/dist/identity/nextjs.mjs +747 -0
  52. package/dist/identity/nextjs.mjs.map +1 -0
  53. package/dist/identity/policy.d.mts +115 -0
  54. package/dist/identity/policy.d.ts +115 -0
  55. package/dist/identity/policy.js +81 -0
  56. package/dist/identity/policy.js.map +1 -0
  57. package/dist/identity/policy.mjs +53 -0
  58. package/dist/identity/policy.mjs.map +1 -0
  59. package/dist/identity/web.d.mts +82 -0
  60. package/dist/identity/web.d.ts +82 -0
  61. package/dist/identity/web.js +775 -0
  62. package/dist/identity/web.js.map +1 -0
  63. package/dist/identity/web.mjs +738 -0
  64. package/dist/identity/web.mjs.map +1 -0
  65. package/dist/index.d.mts +252 -0
  66. package/dist/index.d.ts +252 -0
  67. package/dist/index.js +432 -0
  68. package/dist/index.js.map +1 -0
  69. package/dist/index.mjs +388 -0
  70. package/dist/index.mjs.map +1 -0
  71. package/dist/payment/index.d.mts +716 -0
  72. package/dist/payment/index.d.ts +716 -0
  73. package/dist/payment/index.js +691 -0
  74. package/dist/payment/index.js.map +1 -0
  75. package/dist/payment/index.mjs +639 -0
  76. package/dist/payment/index.mjs.map +1 -0
  77. package/dist/signer-Cvdwn6Cs.d.mts +48 -0
  78. package/dist/signer-Cvdwn6Cs.d.ts +48 -0
  79. package/dist/stripe-multichain/index.d.mts +221 -0
  80. package/dist/stripe-multichain/index.d.ts +221 -0
  81. package/dist/stripe-multichain/index.js +243 -0
  82. package/dist/stripe-multichain/index.js.map +1 -0
  83. package/dist/stripe-multichain/index.mjs +199 -0
  84. package/dist/stripe-multichain/index.mjs.map +1 -0
  85. package/dist/wwwauthenticate-CU1eNvMQ.d.mts +37 -0
  86. package/dist/wwwauthenticate-CU1eNvMQ.d.ts +37 -0
  87. package/package.json +172 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/identity/policy.ts"],"sourcesContent":["/**\n * Per-product / per-tier compliance policy helpers.\n *\n * A *policy* is a small bag of fields describing what identity the merchant wants\n * verified for a given resource:\n *\n * - `enforcement`: `\"hard\"` (today's wine path — 403 on miss) or `\"soft\"` (gate\n * denial is swallowed; the order completes with a degraded `identity_status`).\n * `null` / absent = no gate at all.\n * - `requireKyc` / `requireSanctionsClear` / `minAge`: passed through to the\n * per-framework `agentscoreGate(...)` factory.\n * - `allowedJurisdictions`: buyer-verified country list (`[\"US\", \"CA\", ...]`).\n * - `allowedShippingCountries` / `allowedShippingStates`: optional shipping\n * allowlists. State list is only enforced for US shipments.\n *\n * This module ships three primitives:\n *\n * 1. {@link PolicyBlock} — the typed shape.\n * 2. {@link policyToGateOptions} — translate a block into the options object the\n * per-framework `agentscoreGate(...)` accepts. Returns `null` when the policy\n * has no enforcement (treat as \"no gate; anonymous OK\").\n * 3. {@link runGateWithEnforcement} — wrap a per-framework middleware in the\n * hard/soft enforcement runner. The middleware is given an `onDenied` shim\n * that captures the denial body and status; the runner returns a structured\n * {@link GateResult} so the vendor decides how to surface it.\n *\n * All three are additive — vendors using `agentscoreGate(...)` directly are\n * unaffected. The pattern was extracted from `agentscore/store`; see its\n * `store/routes/purchase.py` (Python sibling) for the full per-request flow.\n */\n\nimport type { AgentScoreCoreOptions, DenialReason } from '../core.js';\n\n/** Hard = 403 propagates; soft = swallowed + identity_status=\"unverified\". */\nexport type EnforcementMode = 'hard' | 'soft';\n\n/** Per-order trust level captured at settle time. */\nexport type IdentityStatus = 'verified' | 'unverified' | 'anonymous' | 'denied';\n\n/** Compliance fields a merchant attaches per product / per tier. All optional. */\nexport interface PolicyBlock {\n enforcement?: EnforcementMode;\n requireKyc?: boolean;\n requireSanctionsClear?: boolean;\n minAge?: number;\n allowedJurisdictions?: readonly string[];\n allowedShippingCountries?: readonly string[];\n allowedShippingStates?: readonly string[];\n}\n\n/**\n * Outcome of running a gate under an enforcement mode.\n *\n * - `verified`: gate accepted; identity is fully verified for the policy.\n * - `unverified`: soft mode swallowed a gate denial; the agent had *some*\n * identity but didn't meet the policy. Stamp this on the order so\n * ops/analytics can tell apart soft passes from hard passes.\n * - `anonymous`: no gate ran (policy was null / no enforcement).\n * - `denied`: hard mode rejected; the caller must propagate the 403. The\n * `denialBody` and `denialStatus` carry the original gate response so the\n * caller can return it as-is.\n */\nexport interface GateResult {\n status: IdentityStatus;\n denialStatus?: number;\n denialBody?: Record<string, unknown>;\n denialReason?: DenialReason;\n}\n\n/**\n * Translate a {@link PolicyBlock} into the options the per-framework\n * `agentscoreGate(...)` expects. Returns `null` when the block has no\n * `enforcement` set — the caller should treat that as \"no gate; anonymous OK\".\n *\n * Use a fresh gate per request rather than constructing once at module scope\n * when the policy varies per resource (e.g. per product). Each adapter's gate\n * is cheap to instantiate.\n */\nexport function policyToGateOptions(\n policy: PolicyBlock | null | undefined,\n base: { apiKey: string; baseUrl?: string },\n): AgentScoreCoreOptions | null {\n if (!policy || !policy.enforcement) return null;\n return {\n apiKey: base.apiKey,\n ...(base.baseUrl !== undefined && { baseUrl: base.baseUrl }),\n ...(policy.requireKyc !== undefined && { requireKyc: policy.requireKyc }),\n ...(policy.requireSanctionsClear !== undefined && {\n requireSanctionsClear: policy.requireSanctionsClear,\n }),\n ...(policy.minAge !== undefined && { minAge: policy.minAge }),\n ...(policy.allowedJurisdictions !== undefined && {\n allowedJurisdictions: [...policy.allowedJurisdictions],\n }),\n };\n}\n\n/**\n * Run a per-framework gate middleware respecting the enforcement mode.\n *\n * The vendor passes:\n * - `gate`: their framework's middleware (Hono `MiddlewareHandler`, Express\n * `(req, res, next) => void`, etc.) — anything that resolves on accept and\n * throws or returns a `Response` on deny.\n * - `runGate`: a thin adapter that calls the middleware with the framework\n * context and returns either `{ ok: true }` (gate accepted) or\n * `{ ok: false, status, body, reason? }` (gate denied with details).\n *\n * `runGateWithEnforcement` wraps that in the hard/soft split:\n *\n * - `gate=null` or `enforcement=null`: no gate fires; status=\"anonymous\".\n * - `enforcement=\"hard\"` + denied: status=\"denied\"; caller propagates denialStatus + denialBody.\n * - `enforcement=\"soft\"` + denied: swallow; status=\"unverified\".\n * - accepted: status=\"verified\".\n */\nexport async function runGateWithEnforcement(\n enforcement: EnforcementMode | undefined,\n runGate: (() => Promise<{ ok: true } | { ok: false; status: number; body: Record<string, unknown>; reason?: DenialReason }>) | null,\n): Promise<GateResult> {\n if (!runGate || !enforcement) return { status: 'anonymous' };\n\n const outcome = await runGate();\n if (outcome.ok) return { status: 'verified' };\n\n if (enforcement === 'hard') {\n return {\n status: 'denied',\n denialStatus: outcome.status,\n denialBody: outcome.body,\n ...(outcome.reason !== undefined && { denialReason: outcome.reason }),\n };\n }\n return {\n status: 'unverified',\n denialStatus: outcome.status,\n denialBody: outcome.body,\n ...(outcome.reason !== undefined && { denialReason: outcome.reason }),\n };\n}\n\n/** NULL policy / NULL allowlist → ship anywhere. Otherwise country must be in the list. */\nexport function shippingCountryAllowed(country: string, policy: PolicyBlock | null | undefined): boolean {\n if (!policy?.allowedShippingCountries || policy.allowedShippingCountries.length === 0) return true;\n const allowed = new Set(policy.allowedShippingCountries.map((c) => c.toUpperCase()));\n return allowed.has(country.toUpperCase());\n}\n\n/**\n * US-state allowlist (e.g. wine).\n *\n * Only enforced for US shipments — non-US shipments are governed by\n * {@link shippingCountryAllowed} independently.\n */\nexport function shippingStateAllowed(\n state: string,\n country: string,\n policy: PolicyBlock | null | undefined,\n): boolean {\n if (!policy?.allowedShippingStates || policy.allowedShippingStates.length === 0) return true;\n if (country.toUpperCase() !== 'US') return true;\n const allowed = new Set(policy.allowedShippingStates.map((s) => s.toUpperCase()));\n return allowed.has(state.toUpperCase());\n}\n"],"mappings":";AA8EO,SAAS,oBACd,QACA,MAC8B;AAC9B,MAAI,CAAC,UAAU,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A,QACS;AACT,MAAI,CAAC,QAAQ,yBAAyB,OAAO,sBAAsB,WAAW,EAAG,QAAO;AACxF,MAAI,QAAQ,YAAY,MAAM,KAAM,QAAO;AAC3C,QAAM,UAAU,IAAI,IAAI,OAAO,sBAAsB,IAAI,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAChF,SAAO,QAAQ,IAAI,MAAM,YAAY,CAAC;AACxC;","names":[]}
@@ -0,0 +1,82 @@
1
+ export { F as FIXABLE_DENIAL_REASONS, b as buildContactSupportNextSteps, a as buildSignerMismatchBody, d as denialReasonStatus, c as denialReasonToBody, i as isFixableDenial, v as verificationAgentInstructions } from '../_response-DmziuJz6.mjs';
2
+ export { e as extractPaymentSignerAddress, r as readX402PaymentHeader } from '../signer-Cvdwn6Cs.mjs';
3
+ import { AgentScoreCoreOptions, AgentIdentity, DenialReason, CreateSessionOnMissing, AgentScoreData, VerifyWalletSignerResult } from '../core.mjs';
4
+
5
+ interface AgentScoreGateOptions extends Omit<AgentScoreCoreOptions, 'createSessionOnMissing'> {
6
+ /** Custom function to extract agent identity from a Request. */
7
+ extractIdentity?: (req: Request) => AgentIdentity | undefined;
8
+ /** Custom handler invoked when a request is denied. Must return a Response. */
9
+ onDenied?: (req: Request, reason: DenialReason) => Response | Promise<Response>;
10
+ /** Auto-create a verification session on missing identity. Hooks receive the `Request`. */
11
+ createSessionOnMissing?: CreateSessionOnMissing<Request>;
12
+ }
13
+ /**
14
+ * Result of a gate check. `allowed: true` means the request passed; forward it to your
15
+ * handler. `allowed: false` means it was denied; return `response` directly to the client.
16
+ *
17
+ * When the request was authenticated via `operator_token`, `captureWallet` is bound to the
18
+ * identity and can be called after payment to report the signer wallet back to AgentScore.
19
+ * When the request was wallet-authenticated (nothing to associate), `captureWallet` is
20
+ * undefined. Always fire-and-forget.
21
+ */
22
+ type GuardResult = {
23
+ allowed: true;
24
+ data?: AgentScoreData;
25
+ captureWallet?: (opts: {
26
+ walletAddress: string;
27
+ network: 'evm' | 'solana';
28
+ idempotencyKey?: string;
29
+ }) => Promise<void>;
30
+ /** Verify the payment signer matches the claimed X-Wallet-Address. Bound only when
31
+ * the request was wallet-authenticated. Pass `opts.signer` explicitly or omit to
32
+ * auto-extract from the original `Request`. */
33
+ verifyWalletSignerMatch?: (opts?: {
34
+ signer?: string | null;
35
+ network?: 'evm' | 'solana';
36
+ }) => Promise<VerifyWalletSignerResult>;
37
+ } | {
38
+ allowed: false;
39
+ response: Response;
40
+ };
41
+ /**
42
+ * Create a Web Fetch-compatible gate. Works with any runtime that speaks the standard
43
+ * Request/Response API: Cloudflare Workers, Deno Deploy, Bun, Next.js App Router, etc.
44
+ *
45
+ * ```ts
46
+ * const guard = createAgentScoreGate({ apiKey: 'as_live_...', requireKyc: true });
47
+ *
48
+ * export default {
49
+ * async fetch(req: Request) {
50
+ * const result = await guard(req);
51
+ * if (!result.allowed) return result.response;
52
+ * return handle(req, result.data);
53
+ * },
54
+ * };
55
+ * ```
56
+ */
57
+ declare function createAgentScoreGate(options: AgentScoreGateOptions): (req: Request) => Promise<GuardResult>;
58
+ /**
59
+ * Wrap a Web Fetch request handler with the gate. Denied requests are returned directly;
60
+ * allowed requests are passed to `handler` along with the assess data.
61
+ *
62
+ * ```ts
63
+ * export const POST = withAgentScoreGate(
64
+ * { apiKey: 'as_live_...', requireKyc: true },
65
+ * async (req, { data }) => Response.json({ ok: true }),
66
+ * );
67
+ * ```
68
+ */
69
+ declare function withAgentScoreGate<TCtx = unknown>(options: AgentScoreGateOptions, handler: (req: Request, gate: {
70
+ data?: AgentScoreData;
71
+ captureWallet?: (opts: {
72
+ walletAddress: string;
73
+ network: 'evm' | 'solana';
74
+ idempotencyKey?: string;
75
+ }) => Promise<void>;
76
+ verifyWalletSignerMatch?: (opts?: {
77
+ signer?: string | null;
78
+ network?: 'evm' | 'solana';
79
+ }) => Promise<VerifyWalletSignerResult>;
80
+ }, ctx?: TCtx) => Response | Promise<Response>): (req: Request, ctx?: TCtx) => Promise<Response>;
81
+
82
+ export { type AgentScoreGateOptions, type GuardResult, createAgentScoreGate, withAgentScoreGate };
@@ -0,0 +1,82 @@
1
+ export { F as FIXABLE_DENIAL_REASONS, b as buildContactSupportNextSteps, a as buildSignerMismatchBody, d as denialReasonStatus, c as denialReasonToBody, i as isFixableDenial, v as verificationAgentInstructions } from '../_response-rbK0zM7y.js';
2
+ export { e as extractPaymentSignerAddress, r as readX402PaymentHeader } from '../signer-Cvdwn6Cs.js';
3
+ import { AgentScoreCoreOptions, AgentIdentity, DenialReason, CreateSessionOnMissing, AgentScoreData, VerifyWalletSignerResult } from '../core.js';
4
+
5
+ interface AgentScoreGateOptions extends Omit<AgentScoreCoreOptions, 'createSessionOnMissing'> {
6
+ /** Custom function to extract agent identity from a Request. */
7
+ extractIdentity?: (req: Request) => AgentIdentity | undefined;
8
+ /** Custom handler invoked when a request is denied. Must return a Response. */
9
+ onDenied?: (req: Request, reason: DenialReason) => Response | Promise<Response>;
10
+ /** Auto-create a verification session on missing identity. Hooks receive the `Request`. */
11
+ createSessionOnMissing?: CreateSessionOnMissing<Request>;
12
+ }
13
+ /**
14
+ * Result of a gate check. `allowed: true` means the request passed; forward it to your
15
+ * handler. `allowed: false` means it was denied; return `response` directly to the client.
16
+ *
17
+ * When the request was authenticated via `operator_token`, `captureWallet` is bound to the
18
+ * identity and can be called after payment to report the signer wallet back to AgentScore.
19
+ * When the request was wallet-authenticated (nothing to associate), `captureWallet` is
20
+ * undefined. Always fire-and-forget.
21
+ */
22
+ type GuardResult = {
23
+ allowed: true;
24
+ data?: AgentScoreData;
25
+ captureWallet?: (opts: {
26
+ walletAddress: string;
27
+ network: 'evm' | 'solana';
28
+ idempotencyKey?: string;
29
+ }) => Promise<void>;
30
+ /** Verify the payment signer matches the claimed X-Wallet-Address. Bound only when
31
+ * the request was wallet-authenticated. Pass `opts.signer` explicitly or omit to
32
+ * auto-extract from the original `Request`. */
33
+ verifyWalletSignerMatch?: (opts?: {
34
+ signer?: string | null;
35
+ network?: 'evm' | 'solana';
36
+ }) => Promise<VerifyWalletSignerResult>;
37
+ } | {
38
+ allowed: false;
39
+ response: Response;
40
+ };
41
+ /**
42
+ * Create a Web Fetch-compatible gate. Works with any runtime that speaks the standard
43
+ * Request/Response API: Cloudflare Workers, Deno Deploy, Bun, Next.js App Router, etc.
44
+ *
45
+ * ```ts
46
+ * const guard = createAgentScoreGate({ apiKey: 'as_live_...', requireKyc: true });
47
+ *
48
+ * export default {
49
+ * async fetch(req: Request) {
50
+ * const result = await guard(req);
51
+ * if (!result.allowed) return result.response;
52
+ * return handle(req, result.data);
53
+ * },
54
+ * };
55
+ * ```
56
+ */
57
+ declare function createAgentScoreGate(options: AgentScoreGateOptions): (req: Request) => Promise<GuardResult>;
58
+ /**
59
+ * Wrap a Web Fetch request handler with the gate. Denied requests are returned directly;
60
+ * allowed requests are passed to `handler` along with the assess data.
61
+ *
62
+ * ```ts
63
+ * export const POST = withAgentScoreGate(
64
+ * { apiKey: 'as_live_...', requireKyc: true },
65
+ * async (req, { data }) => Response.json({ ok: true }),
66
+ * );
67
+ * ```
68
+ */
69
+ declare function withAgentScoreGate<TCtx = unknown>(options: AgentScoreGateOptions, handler: (req: Request, gate: {
70
+ data?: AgentScoreData;
71
+ captureWallet?: (opts: {
72
+ walletAddress: string;
73
+ network: 'evm' | 'solana';
74
+ idempotencyKey?: string;
75
+ }) => Promise<void>;
76
+ verifyWalletSignerMatch?: (opts?: {
77
+ signer?: string | null;
78
+ network?: 'evm' | 'solana';
79
+ }) => Promise<VerifyWalletSignerResult>;
80
+ }, ctx?: TCtx) => Response | Promise<Response>): (req: Request, ctx?: TCtx) => Promise<Response>;
81
+
82
+ export { type AgentScoreGateOptions, type GuardResult, createAgentScoreGate, withAgentScoreGate };