@vorim/sdk 3.8.0 → 3.9.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 (77) hide show
  1. package/dist/index.cjs +208 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +133 -3
  4. package/dist/index.d.ts +133 -3
  5. package/dist/index.js +208 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/integrations/anthropic.d.cts +1 -1
  8. package/dist/integrations/anthropic.d.ts +1 -1
  9. package/dist/integrations/ap2.cjs +218 -0
  10. package/dist/integrations/ap2.cjs.map +1 -0
  11. package/dist/integrations/ap2.d.cts +95 -0
  12. package/dist/integrations/ap2.d.ts +95 -0
  13. package/dist/integrations/ap2.js +192 -0
  14. package/dist/integrations/ap2.js.map +1 -0
  15. package/dist/integrations/bank-rails.cjs +173 -0
  16. package/dist/integrations/bank-rails.cjs.map +1 -0
  17. package/dist/integrations/bank-rails.d.cts +87 -0
  18. package/dist/integrations/bank-rails.d.ts +87 -0
  19. package/dist/integrations/bank-rails.js +147 -0
  20. package/dist/integrations/bank-rails.js.map +1 -0
  21. package/dist/integrations/circle.cjs +159 -0
  22. package/dist/integrations/circle.cjs.map +1 -0
  23. package/dist/integrations/circle.d.cts +75 -0
  24. package/dist/integrations/circle.d.ts +75 -0
  25. package/dist/integrations/circle.js +133 -0
  26. package/dist/integrations/circle.js.map +1 -0
  27. package/dist/integrations/crewai.d.cts +1 -1
  28. package/dist/integrations/crewai.d.ts +1 -1
  29. package/dist/integrations/hermes.cjs +280 -0
  30. package/dist/integrations/hermes.cjs.map +1 -0
  31. package/dist/integrations/hermes.d.cts +60 -0
  32. package/dist/integrations/hermes.d.ts +60 -0
  33. package/dist/integrations/hermes.js +242 -0
  34. package/dist/integrations/hermes.js.map +1 -0
  35. package/dist/integrations/langchain.d.cts +1 -1
  36. package/dist/integrations/langchain.d.ts +1 -1
  37. package/dist/integrations/langgraph.d.cts +1 -1
  38. package/dist/integrations/langgraph.d.ts +1 -1
  39. package/dist/integrations/llamaindex.d.cts +1 -1
  40. package/dist/integrations/llamaindex.d.ts +1 -1
  41. package/dist/integrations/nekuda.cjs +165 -0
  42. package/dist/integrations/nekuda.cjs.map +1 -0
  43. package/dist/integrations/nekuda.d.cts +79 -0
  44. package/dist/integrations/nekuda.d.ts +79 -0
  45. package/dist/integrations/nekuda.js +139 -0
  46. package/dist/integrations/nekuda.js.map +1 -0
  47. package/dist/integrations/openai.d.cts +1 -1
  48. package/dist/integrations/openai.d.ts +1 -1
  49. package/dist/integrations/paypal-agent.cjs +215 -0
  50. package/dist/integrations/paypal-agent.cjs.map +1 -0
  51. package/dist/integrations/paypal-agent.d.cts +87 -0
  52. package/dist/integrations/paypal-agent.d.ts +87 -0
  53. package/dist/integrations/paypal-agent.js +189 -0
  54. package/dist/integrations/paypal-agent.js.map +1 -0
  55. package/dist/integrations/siem.d.cts +1 -1
  56. package/dist/integrations/siem.d.ts +1 -1
  57. package/dist/integrations/skyfire.cjs +166 -0
  58. package/dist/integrations/skyfire.cjs.map +1 -0
  59. package/dist/integrations/skyfire.d.cts +80 -0
  60. package/dist/integrations/skyfire.d.ts +80 -0
  61. package/dist/integrations/skyfire.js +140 -0
  62. package/dist/integrations/skyfire.js.map +1 -0
  63. package/dist/integrations/stripe-acp.d.cts +1 -1
  64. package/dist/integrations/stripe-acp.d.ts +1 -1
  65. package/dist/integrations/ucp.d.cts +1 -1
  66. package/dist/integrations/ucp.d.ts +1 -1
  67. package/dist/integrations/vercel-ai.d.cts +1 -1
  68. package/dist/integrations/vercel-ai.d.ts +1 -1
  69. package/dist/integrations/x402.cjs +211 -0
  70. package/dist/integrations/x402.cjs.map +1 -0
  71. package/dist/integrations/x402.d.cts +87 -0
  72. package/dist/integrations/x402.d.ts +87 -0
  73. package/dist/integrations/x402.js +185 -0
  74. package/dist/integrations/x402.js.map +1 -0
  75. package/dist/{types-Cb6V-boJ.d.cts → types-zzK9YOoa.d.cts} +86 -1
  76. package/dist/{types-Cb6V-boJ.d.ts → types-zzK9YOoa.d.ts} +86 -1
  77. package/package.json +83 -2
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/integrations/ap2.ts
21
+ var ap2_exports = {};
22
+ __export(ap2_exports, {
23
+ VorimAP2: () => VorimAP2,
24
+ createVorimAP2: () => createVorimAP2
25
+ });
26
+ module.exports = __toCommonJS(ap2_exports);
27
+ var VorimAP2 = class _VorimAP2 {
28
+ sdk;
29
+ minTrustScore;
30
+ requiredScope;
31
+ /** One-time guard so the no-auth confused-deputy warning logs at most once. */
32
+ static _warnedNoAuth = false;
33
+ constructor(sdk, config) {
34
+ this.sdk = sdk;
35
+ this.minTrustScore = config?.minTrustScore ?? 50;
36
+ this.requiredScope = config?.requiredScope ?? "agent:transact";
37
+ }
38
+ /**
39
+ * Verify an agent is authorized to present/sign an AP2 mandate. Same gate as
40
+ * the other payment integrations: transact scope → active status → trust
41
+ * threshold. Every outcome (allow or deny) is recorded on the audit trail,
42
+ * carrying the AP2 checkout hash so the evidence trails reconcile.
43
+ */
44
+ async authorizeMandate(req) {
45
+ const threshold = req.minTrustScore ?? this.minTrustScore;
46
+ const action = `ap2.${req.mandateType ?? "payment"}_mandate.authorize`;
47
+ try {
48
+ const perm = await this.sdk.check(req.agentId, this.requiredScope);
49
+ if (!perm.allowed) {
50
+ await this.logEvent(req.agentId, action, "denied", req, { reason: `Missing ${this.requiredScope}` });
51
+ return {
52
+ authorized: false,
53
+ agentId: req.agentId,
54
+ trustScore: 0,
55
+ status: "unknown",
56
+ reason: `Agent does not have ${this.requiredScope} permission`,
57
+ scopeVerified: false
58
+ };
59
+ }
60
+ } catch (e) {
61
+ await this.logEvent(req.agentId, action, "denied", req, { reason: e.message });
62
+ return {
63
+ authorized: false,
64
+ agentId: req.agentId,
65
+ trustScore: 0,
66
+ status: "unknown",
67
+ reason: `Permission check failed: ${e.message}`,
68
+ scopeVerified: false
69
+ };
70
+ }
71
+ let score = 0;
72
+ let status = "unknown";
73
+ try {
74
+ const trust = await this.sdk.verify(req.agentId);
75
+ score = trust.trust_score ?? 0;
76
+ status = trust.status ?? "unknown";
77
+ } catch {
78
+ }
79
+ if (status !== "active") {
80
+ await this.logEvent(req.agentId, action, "denied", req, { reason: `Status: ${status}` });
81
+ return {
82
+ authorized: false,
83
+ agentId: req.agentId,
84
+ trustScore: score,
85
+ status,
86
+ reason: `Agent is ${status}, not active`,
87
+ scopeVerified: true
88
+ };
89
+ }
90
+ if (score < threshold) {
91
+ await this.logEvent(req.agentId, action, "denied", req, { reason: `Score ${score} < ${threshold}` });
92
+ return {
93
+ authorized: false,
94
+ agentId: req.agentId,
95
+ trustScore: score,
96
+ status,
97
+ reason: `Trust score ${score} is below minimum ${threshold}`,
98
+ scopeVerified: true
99
+ };
100
+ }
101
+ await this.logEvent(req.agentId, action, "success", req, { trust_score: score });
102
+ return {
103
+ authorized: true,
104
+ agentId: req.agentId,
105
+ trustScore: score,
106
+ status,
107
+ scopeVerified: true
108
+ };
109
+ }
110
+ /** Log that an AP2 mandate was presented/signed. */
111
+ async logMandatePresented(agentId, mandateId, req) {
112
+ await this.logEvent(agentId, `ap2.${req?.mandateType ?? "payment"}_mandate.presented`, "success", req, { mandate_id: mandateId });
113
+ }
114
+ /** Log that an AP2 payment receipt was issued and verified (payment settled). */
115
+ async logReceiptVerified(agentId, receiptId, req) {
116
+ await this.logEvent(agentId, "ap2.receipt.verified", "success", req, { receipt_id: receiptId });
117
+ }
118
+ /** Log that an AP2 mandate/payment was rejected or failed downstream. */
119
+ async logMandateRejected(agentId, mandateId, reason, req) {
120
+ await this.logEvent(agentId, `ap2.${req?.mandateType ?? "payment"}_mandate.rejected`, "error", req, { mandate_id: mandateId, reject_reason: reason });
121
+ }
122
+ /**
123
+ * Express/Connect middleware for a merchant/processor endpoint that gates an
124
+ * AP2 mandate presentation on agent authorization.
125
+ *
126
+ * ⚠️ CONFUSED-DEPUTY WARNING. This evaluates the *named* agent's standing
127
+ * only; it does NOT prove the HTTP caller controls that agent, and it does
128
+ * NOT itself verify the AP2 mandate's cryptographic signature (do that with
129
+ * the AP2 SDK). Authenticate the caller-to-agent binding upstream, or pass
130
+ * `authenticate`. When omitted, a one-time warning is logged.
131
+ */
132
+ middleware(options) {
133
+ const header = options?.agentIdHeader ?? "x-vorim-agent-id";
134
+ const threshold = options?.minTrustScore ?? this.minTrustScore;
135
+ const authenticate = options?.authenticate;
136
+ if (!authenticate && !_VorimAP2._warnedNoAuth) {
137
+ _VorimAP2._warnedNoAuth = true;
138
+ console.warn(
139
+ "[vorim] VorimAP2.middleware() called without an `authenticate` hook: it authorizes the agent NAMED in the header but does not verify the caller controls that agent (confused-deputy risk), and it does not verify the AP2 mandate signature. Authenticate the caller-to-agent binding upstream or pass `authenticate`."
140
+ );
141
+ }
142
+ return async (req, res, next) => {
143
+ const agentId = req.headers?.[header] || req.headers?.[header.toLowerCase()] || "";
144
+ if (!agentId) {
145
+ res.status(401).json({
146
+ error: { code: "MISSING_AGENT_ID", message: `Agent ID required in ${header} header` }
147
+ });
148
+ return;
149
+ }
150
+ if (authenticate) {
151
+ let bound = false;
152
+ try {
153
+ bound = await authenticate(agentId, req);
154
+ } catch {
155
+ bound = false;
156
+ }
157
+ if (!bound) {
158
+ res.status(401).json({
159
+ error: { code: "AGENT_BINDING_UNVERIFIED", message: "Caller is not authenticated as the named agent" }
160
+ });
161
+ return;
162
+ }
163
+ }
164
+ const auth = await this.authorizeMandate({
165
+ agentId,
166
+ mandateType: req.body?.mandateType || "payment",
167
+ merchant: req.body?.merchant || "",
168
+ amount: req.body?.amount || 0,
169
+ currency: req.body?.currency || "usd",
170
+ checkoutHash: req.body?.checkoutHash || "",
171
+ minTrustScore: threshold
172
+ });
173
+ if (!auth.authorized) {
174
+ res.status(403).json({
175
+ error: {
176
+ code: "UNAUTHORIZED_AGENT",
177
+ message: auth.reason || "Agent not authorized to present an AP2 mandate",
178
+ trust_score: auth.trustScore,
179
+ status: auth.status
180
+ }
181
+ });
182
+ return;
183
+ }
184
+ req.vorimAuthorization = auth;
185
+ next();
186
+ };
187
+ }
188
+ async logEvent(agentId, action, result, req, metadata) {
189
+ try {
190
+ await this.sdk.emit({
191
+ agent_id: agentId,
192
+ event_type: "api_request",
193
+ action,
194
+ result,
195
+ resource: req?.merchant || "",
196
+ metadata: {
197
+ mandate_type: req?.mandateType || "",
198
+ merchant: req?.merchant || "",
199
+ amount: req?.amount || 0,
200
+ currency: req?.currency || "",
201
+ checkout_hash: req?.checkoutHash || "",
202
+ mode: req?.mode || "",
203
+ ...metadata
204
+ }
205
+ });
206
+ } catch {
207
+ }
208
+ }
209
+ };
210
+ function createVorimAP2(sdk, config) {
211
+ return new VorimAP2(sdk, config);
212
+ }
213
+ // Annotate the CommonJS export names for ESM import in node:
214
+ 0 && (module.exports = {
215
+ VorimAP2,
216
+ createVorimAP2
217
+ });
218
+ //# sourceMappingURL=ap2.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/integrations/ap2.ts"],"sourcesContent":["// ============================================================================\n// Vorim AI — AP2 (Agent Payments Protocol) Integration\n//\n// AP2 is an open agent-payments standard (announced by Google in Sept 2025,\n// donated to the FIDO Alliance in April 2026). It defines cryptographically\n// signed \"mandates\" that authorize an agent to buy and to pay:\n// • Checkout Mandate — proof the agent is authorized to buy the assembled cart\n// • Payment Mandate — proof the agent is authorized to pay for that checkout\n// Mandates are SD-JWT Verifiable Credentials (ES256), chained to a merchant-\n// signed Checkout JWT. AP2 is the authorization layer; it does not move money.\n//\n// This integration adds Vorim's identity, permission, and audit layer AROUND\n// an AP2 mandate flow: before an agent presents or signs a mandate, it verifies\n// the agent holds `transact`, is active, and clears a trust threshold, and it\n// records the mandate + receipt lifecycle on a signed, tamper-evident audit\n// trail — with the AP2 checkout/mandate hashes captured so the two evidence\n// trails line up. It does NOT sign AP2 mandates or hold keys; that stays with\n// the AP2 SDK and the user's trusted surface.\n//\n// Usage:\n// import createVorim from '@vorim/sdk';\n// import { createVorimAP2 } from '@vorim/sdk/integrations/ap2';\n//\n// const vorim = createVorim({ apiKey: 'agid_sk_...' });\n// const ap2 = createVorimAP2(vorim, { minTrustScore: 60 });\n//\n// // Before the agent presents/signs a mandate for a cart:\n// const auth = await ap2.authorizeMandate({\n// agentId: 'agid_abc123',\n// mandateType: 'payment',\n// merchant: 'acme-store',\n// amount: 9999,\n// currency: 'usd',\n// checkoutHash: 'sha256:...', // hash of the merchant-signed Checkout JWT\n// });\n// if (auth.authorized) {\n// // ... present/sign the AP2 mandate via the AP2 SDK, then:\n// await ap2.logMandatePresented(auth.agentId, mandateId, { checkoutHash });\n// }\n// ============================================================================\n\nimport type { VorimSDK } from '../index.js';\n\nexport type AP2MandateType = 'checkout' | 'payment';\n\nexport interface AP2Config {\n apiKey: string;\n baseUrl?: string;\n minTrustScore?: number;\n requiredScope?: string;\n}\n\nexport interface AP2MandateRequest {\n /** The Vorim agent id presenting/signing the mandate. */\n agentId: string;\n /** Which AP2 mandate is being authorized. */\n mandateType?: AP2MandateType;\n /** The merchant the cart/checkout belongs to. */\n merchant?: string;\n /** Amount in the smallest currency unit. */\n amount?: number;\n /** Currency / instrument code (e.g. 'usd', 'USDC'). */\n currency?: string;\n /**\n * Hash of the merchant-signed AP2 Checkout JWT that the mandate binds to.\n * Recorded so the Vorim audit trail and the AP2 mandate chain reconcile.\n */\n checkoutHash?: string;\n /** AP2 mode: user present (Direct) or not present (Autonomous). Informational. */\n mode?: 'direct' | 'autonomous';\n /** Per-call override of the trust threshold. */\n minTrustScore?: number;\n}\n\nexport interface AP2Authorization {\n authorized: boolean;\n agentId: string;\n trustScore: number;\n status: string;\n reason?: string;\n scopeVerified: boolean;\n}\n\nexport class VorimAP2 {\n private sdk: VorimSDK;\n private minTrustScore: number;\n private requiredScope: string;\n /** One-time guard so the no-auth confused-deputy warning logs at most once. */\n private static _warnedNoAuth = false;\n\n constructor(sdk: VorimSDK, config?: Partial<AP2Config>) {\n this.sdk = sdk;\n this.minTrustScore = config?.minTrustScore ?? 50;\n this.requiredScope = config?.requiredScope ?? 'agent:transact';\n }\n\n /**\n * Verify an agent is authorized to present/sign an AP2 mandate. Same gate as\n * the other payment integrations: transact scope → active status → trust\n * threshold. Every outcome (allow or deny) is recorded on the audit trail,\n * carrying the AP2 checkout hash so the evidence trails reconcile.\n */\n async authorizeMandate(req: AP2MandateRequest): Promise<AP2Authorization> {\n const threshold = req.minTrustScore ?? this.minTrustScore;\n const action = `ap2.${req.mandateType ?? 'payment'}_mandate.authorize`;\n\n // 1. Permission: the agent must hold the transact scope.\n try {\n const perm = await this.sdk.check(req.agentId, this.requiredScope as any);\n if (!(perm as any).allowed) {\n await this.logEvent(req.agentId, action, 'denied', req, { reason: `Missing ${this.requiredScope}` });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: 0,\n status: 'unknown',\n reason: `Agent does not have ${this.requiredScope} permission`,\n scopeVerified: false,\n };\n }\n } catch (e: any) {\n await this.logEvent(req.agentId, action, 'denied', req, { reason: e.message });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: 0,\n status: 'unknown',\n reason: `Permission check failed: ${e.message}`,\n scopeVerified: false,\n };\n }\n\n // 2. Trust: verify the agent's public standing.\n let score = 0;\n let status = 'unknown';\n try {\n const trust = await this.sdk.verify(req.agentId);\n score = (trust as any).trust_score ?? 0;\n status = (trust as any).status ?? 'unknown';\n } catch {\n // Trust verification failed — treated as not-active below.\n }\n\n if (status !== 'active') {\n await this.logEvent(req.agentId, action, 'denied', req, { reason: `Status: ${status}` });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: score,\n status,\n reason: `Agent is ${status}, not active`,\n scopeVerified: true,\n };\n }\n\n if (score < threshold) {\n await this.logEvent(req.agentId, action, 'denied', req, { reason: `Score ${score} < ${threshold}` });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: score,\n status,\n reason: `Trust score ${score} is below minimum ${threshold}`,\n scopeVerified: true,\n };\n }\n\n await this.logEvent(req.agentId, action, 'success', req, { trust_score: score });\n\n return {\n authorized: true,\n agentId: req.agentId,\n trustScore: score,\n status,\n scopeVerified: true,\n };\n }\n\n /** Log that an AP2 mandate was presented/signed. */\n async logMandatePresented(agentId: string, mandateId: string, req?: Partial<AP2MandateRequest>): Promise<void> {\n await this.logEvent(agentId, `ap2.${req?.mandateType ?? 'payment'}_mandate.presented`, 'success', req, { mandate_id: mandateId });\n }\n\n /** Log that an AP2 payment receipt was issued and verified (payment settled). */\n async logReceiptVerified(agentId: string, receiptId: string, req?: Partial<AP2MandateRequest>): Promise<void> {\n await this.logEvent(agentId, 'ap2.receipt.verified', 'success', req, { receipt_id: receiptId });\n }\n\n /** Log that an AP2 mandate/payment was rejected or failed downstream. */\n async logMandateRejected(agentId: string, mandateId: string, reason?: string, req?: Partial<AP2MandateRequest>): Promise<void> {\n await this.logEvent(agentId, `ap2.${req?.mandateType ?? 'payment'}_mandate.rejected`, 'error', req, { mandate_id: mandateId, reject_reason: reason });\n }\n\n /**\n * Express/Connect middleware for a merchant/processor endpoint that gates an\n * AP2 mandate presentation on agent authorization.\n *\n * ⚠️ CONFUSED-DEPUTY WARNING. This evaluates the *named* agent's standing\n * only; it does NOT prove the HTTP caller controls that agent, and it does\n * NOT itself verify the AP2 mandate's cryptographic signature (do that with\n * the AP2 SDK). Authenticate the caller-to-agent binding upstream, or pass\n * `authenticate`. When omitted, a one-time warning is logged.\n */\n middleware(options?: {\n agentIdHeader?: string;\n minTrustScore?: number;\n authenticate?: (agentId: string, req: any) => boolean | Promise<boolean>;\n }) {\n const header = options?.agentIdHeader ?? 'x-vorim-agent-id';\n const threshold = options?.minTrustScore ?? this.minTrustScore;\n const authenticate = options?.authenticate;\n\n if (!authenticate && !VorimAP2._warnedNoAuth) {\n VorimAP2._warnedNoAuth = true;\n // eslint-disable-next-line no-console\n console.warn(\n '[vorim] VorimAP2.middleware() called without an `authenticate` hook: ' +\n 'it authorizes the agent NAMED in the header but does not verify the ' +\n 'caller controls that agent (confused-deputy risk), and it does not ' +\n 'verify the AP2 mandate signature. Authenticate the caller-to-agent ' +\n 'binding upstream or pass `authenticate`.'\n );\n }\n\n return async (req: any, res: any, next: any) => {\n const agentId = req.headers?.[header] || req.headers?.[header.toLowerCase()] || '';\n\n if (!agentId) {\n res.status(401).json({\n error: { code: 'MISSING_AGENT_ID', message: `Agent ID required in ${header} header` },\n });\n return;\n }\n\n if (authenticate) {\n let bound = false;\n try {\n bound = await authenticate(agentId, req);\n } catch {\n bound = false;\n }\n if (!bound) {\n res.status(401).json({\n error: { code: 'AGENT_BINDING_UNVERIFIED', message: 'Caller is not authenticated as the named agent' },\n });\n return;\n }\n }\n\n const auth = await this.authorizeMandate({\n agentId,\n mandateType: (req.body?.mandateType as AP2MandateType) || 'payment',\n merchant: req.body?.merchant || '',\n amount: req.body?.amount || 0,\n currency: req.body?.currency || 'usd',\n checkoutHash: req.body?.checkoutHash || '',\n minTrustScore: threshold,\n });\n\n if (!auth.authorized) {\n res.status(403).json({\n error: {\n code: 'UNAUTHORIZED_AGENT',\n message: auth.reason || 'Agent not authorized to present an AP2 mandate',\n trust_score: auth.trustScore,\n status: auth.status,\n },\n });\n return;\n }\n\n req.vorimAuthorization = auth;\n next();\n };\n }\n\n private async logEvent(\n agentId: string,\n action: string,\n result: 'success' | 'denied' | 'error',\n req?: Partial<AP2MandateRequest>,\n metadata?: Record<string, any>,\n ): Promise<void> {\n try {\n await this.sdk.emit({\n agent_id: agentId,\n event_type: 'api_request',\n action,\n result,\n resource: req?.merchant || '',\n metadata: {\n mandate_type: req?.mandateType || '',\n merchant: req?.merchant || '',\n amount: req?.amount || 0,\n currency: req?.currency || '',\n checkout_hash: req?.checkoutHash || '',\n mode: req?.mode || '',\n ...metadata,\n },\n });\n } catch {\n // Never block the payment flow on an audit hiccup.\n }\n }\n}\n\n/**\n * Create a VorimAP2 instance.\n *\n * @example\n * ```typescript\n * import createVorim from '@vorim/sdk';\n * import { createVorimAP2 } from '@vorim/sdk/integrations/ap2';\n *\n * const vorim = createVorim({ apiKey: 'agid_sk_...' });\n * const ap2 = createVorimAP2(vorim, { minTrustScore: 70 });\n *\n * const auth = await ap2.authorizeMandate({ agentId: 'agid_abc123', mandateType: 'payment', merchant: 'shop', amount: 4999 });\n *\n * app.post('/ap2/mandate', ap2.middleware(), presentMandateHandler);\n * ```\n */\nexport function createVorimAP2(sdk: VorimSDK, config?: Partial<AP2Config>): VorimAP2 {\n return new VorimAP2(sdk, config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAmFO,IAAM,WAAN,MAAM,UAAS;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAER,OAAe,gBAAgB;AAAA,EAE/B,YAAY,KAAe,QAA6B;AACtD,SAAK,MAAM;AACX,SAAK,gBAAgB,QAAQ,iBAAiB;AAC9C,SAAK,gBAAgB,QAAQ,iBAAiB;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,iBAAiB,KAAmD;AACxE,UAAM,YAAY,IAAI,iBAAiB,KAAK;AAC5C,UAAM,SAAS,OAAO,IAAI,eAAe,SAAS;AAGlD,QAAI;AACF,YA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AAc,KAAK,eAAe;AAAA,UAClC,UAAU,KAAK,YAAY;AAAA,UAC3B,QAAQ,KAAK,UAAU;AAAA,UACvB,UAAU,KAAK,YAAY;AAAA,UAC3B,eAAe,KAAK,gBAAgB;AAAA,UACpC,MAAM,KAAK,QAAQ;AAAA,UACnB,GAAG;AAAA,QACL;AAAA,MACF,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AAkBO,SAAS,eAAe,KAAe,QAAuC;AACnF,SAAO,IAAI,SAAS,KAAK,MAAM;AACjC;","names":[]}
@@ -0,0 +1,95 @@
1
+ import { VorimSDK } from '../index.cjs';
2
+ import '../types-zzK9YOoa.cjs';
3
+
4
+ type AP2MandateType = 'checkout' | 'payment';
5
+ interface AP2Config {
6
+ apiKey: string;
7
+ baseUrl?: string;
8
+ minTrustScore?: number;
9
+ requiredScope?: string;
10
+ }
11
+ interface AP2MandateRequest {
12
+ /** The Vorim agent id presenting/signing the mandate. */
13
+ agentId: string;
14
+ /** Which AP2 mandate is being authorized. */
15
+ mandateType?: AP2MandateType;
16
+ /** The merchant the cart/checkout belongs to. */
17
+ merchant?: string;
18
+ /** Amount in the smallest currency unit. */
19
+ amount?: number;
20
+ /** Currency / instrument code (e.g. 'usd', 'USDC'). */
21
+ currency?: string;
22
+ /**
23
+ * Hash of the merchant-signed AP2 Checkout JWT that the mandate binds to.
24
+ * Recorded so the Vorim audit trail and the AP2 mandate chain reconcile.
25
+ */
26
+ checkoutHash?: string;
27
+ /** AP2 mode: user present (Direct) or not present (Autonomous). Informational. */
28
+ mode?: 'direct' | 'autonomous';
29
+ /** Per-call override of the trust threshold. */
30
+ minTrustScore?: number;
31
+ }
32
+ interface AP2Authorization {
33
+ authorized: boolean;
34
+ agentId: string;
35
+ trustScore: number;
36
+ status: string;
37
+ reason?: string;
38
+ scopeVerified: boolean;
39
+ }
40
+ declare class VorimAP2 {
41
+ private sdk;
42
+ private minTrustScore;
43
+ private requiredScope;
44
+ /** One-time guard so the no-auth confused-deputy warning logs at most once. */
45
+ private static _warnedNoAuth;
46
+ constructor(sdk: VorimSDK, config?: Partial<AP2Config>);
47
+ /**
48
+ * Verify an agent is authorized to present/sign an AP2 mandate. Same gate as
49
+ * the other payment integrations: transact scope → active status → trust
50
+ * threshold. Every outcome (allow or deny) is recorded on the audit trail,
51
+ * carrying the AP2 checkout hash so the evidence trails reconcile.
52
+ */
53
+ authorizeMandate(req: AP2MandateRequest): Promise<AP2Authorization>;
54
+ /** Log that an AP2 mandate was presented/signed. */
55
+ logMandatePresented(agentId: string, mandateId: string, req?: Partial<AP2MandateRequest>): Promise<void>;
56
+ /** Log that an AP2 payment receipt was issued and verified (payment settled). */
57
+ logReceiptVerified(agentId: string, receiptId: string, req?: Partial<AP2MandateRequest>): Promise<void>;
58
+ /** Log that an AP2 mandate/payment was rejected or failed downstream. */
59
+ logMandateRejected(agentId: string, mandateId: string, reason?: string, req?: Partial<AP2MandateRequest>): Promise<void>;
60
+ /**
61
+ * Express/Connect middleware for a merchant/processor endpoint that gates an
62
+ * AP2 mandate presentation on agent authorization.
63
+ *
64
+ * ⚠️ CONFUSED-DEPUTY WARNING. This evaluates the *named* agent's standing
65
+ * only; it does NOT prove the HTTP caller controls that agent, and it does
66
+ * NOT itself verify the AP2 mandate's cryptographic signature (do that with
67
+ * the AP2 SDK). Authenticate the caller-to-agent binding upstream, or pass
68
+ * `authenticate`. When omitted, a one-time warning is logged.
69
+ */
70
+ middleware(options?: {
71
+ agentIdHeader?: string;
72
+ minTrustScore?: number;
73
+ authenticate?: (agentId: string, req: any) => boolean | Promise<boolean>;
74
+ }): (req: any, res: any, next: any) => Promise<void>;
75
+ private logEvent;
76
+ }
77
+ /**
78
+ * Create a VorimAP2 instance.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import createVorim from '@vorim/sdk';
83
+ * import { createVorimAP2 } from '@vorim/sdk/integrations/ap2';
84
+ *
85
+ * const vorim = createVorim({ apiKey: 'agid_sk_...' });
86
+ * const ap2 = createVorimAP2(vorim, { minTrustScore: 70 });
87
+ *
88
+ * const auth = await ap2.authorizeMandate({ agentId: 'agid_abc123', mandateType: 'payment', merchant: 'shop', amount: 4999 });
89
+ *
90
+ * app.post('/ap2/mandate', ap2.middleware(), presentMandateHandler);
91
+ * ```
92
+ */
93
+ declare function createVorimAP2(sdk: VorimSDK, config?: Partial<AP2Config>): VorimAP2;
94
+
95
+ export { type AP2Authorization, type AP2Config, type AP2MandateRequest, type AP2MandateType, VorimAP2, createVorimAP2 };
@@ -0,0 +1,95 @@
1
+ import { VorimSDK } from '../index.js';
2
+ import '../types-zzK9YOoa.js';
3
+
4
+ type AP2MandateType = 'checkout' | 'payment';
5
+ interface AP2Config {
6
+ apiKey: string;
7
+ baseUrl?: string;
8
+ minTrustScore?: number;
9
+ requiredScope?: string;
10
+ }
11
+ interface AP2MandateRequest {
12
+ /** The Vorim agent id presenting/signing the mandate. */
13
+ agentId: string;
14
+ /** Which AP2 mandate is being authorized. */
15
+ mandateType?: AP2MandateType;
16
+ /** The merchant the cart/checkout belongs to. */
17
+ merchant?: string;
18
+ /** Amount in the smallest currency unit. */
19
+ amount?: number;
20
+ /** Currency / instrument code (e.g. 'usd', 'USDC'). */
21
+ currency?: string;
22
+ /**
23
+ * Hash of the merchant-signed AP2 Checkout JWT that the mandate binds to.
24
+ * Recorded so the Vorim audit trail and the AP2 mandate chain reconcile.
25
+ */
26
+ checkoutHash?: string;
27
+ /** AP2 mode: user present (Direct) or not present (Autonomous). Informational. */
28
+ mode?: 'direct' | 'autonomous';
29
+ /** Per-call override of the trust threshold. */
30
+ minTrustScore?: number;
31
+ }
32
+ interface AP2Authorization {
33
+ authorized: boolean;
34
+ agentId: string;
35
+ trustScore: number;
36
+ status: string;
37
+ reason?: string;
38
+ scopeVerified: boolean;
39
+ }
40
+ declare class VorimAP2 {
41
+ private sdk;
42
+ private minTrustScore;
43
+ private requiredScope;
44
+ /** One-time guard so the no-auth confused-deputy warning logs at most once. */
45
+ private static _warnedNoAuth;
46
+ constructor(sdk: VorimSDK, config?: Partial<AP2Config>);
47
+ /**
48
+ * Verify an agent is authorized to present/sign an AP2 mandate. Same gate as
49
+ * the other payment integrations: transact scope → active status → trust
50
+ * threshold. Every outcome (allow or deny) is recorded on the audit trail,
51
+ * carrying the AP2 checkout hash so the evidence trails reconcile.
52
+ */
53
+ authorizeMandate(req: AP2MandateRequest): Promise<AP2Authorization>;
54
+ /** Log that an AP2 mandate was presented/signed. */
55
+ logMandatePresented(agentId: string, mandateId: string, req?: Partial<AP2MandateRequest>): Promise<void>;
56
+ /** Log that an AP2 payment receipt was issued and verified (payment settled). */
57
+ logReceiptVerified(agentId: string, receiptId: string, req?: Partial<AP2MandateRequest>): Promise<void>;
58
+ /** Log that an AP2 mandate/payment was rejected or failed downstream. */
59
+ logMandateRejected(agentId: string, mandateId: string, reason?: string, req?: Partial<AP2MandateRequest>): Promise<void>;
60
+ /**
61
+ * Express/Connect middleware for a merchant/processor endpoint that gates an
62
+ * AP2 mandate presentation on agent authorization.
63
+ *
64
+ * ⚠️ CONFUSED-DEPUTY WARNING. This evaluates the *named* agent's standing
65
+ * only; it does NOT prove the HTTP caller controls that agent, and it does
66
+ * NOT itself verify the AP2 mandate's cryptographic signature (do that with
67
+ * the AP2 SDK). Authenticate the caller-to-agent binding upstream, or pass
68
+ * `authenticate`. When omitted, a one-time warning is logged.
69
+ */
70
+ middleware(options?: {
71
+ agentIdHeader?: string;
72
+ minTrustScore?: number;
73
+ authenticate?: (agentId: string, req: any) => boolean | Promise<boolean>;
74
+ }): (req: any, res: any, next: any) => Promise<void>;
75
+ private logEvent;
76
+ }
77
+ /**
78
+ * Create a VorimAP2 instance.
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * import createVorim from '@vorim/sdk';
83
+ * import { createVorimAP2 } from '@vorim/sdk/integrations/ap2';
84
+ *
85
+ * const vorim = createVorim({ apiKey: 'agid_sk_...' });
86
+ * const ap2 = createVorimAP2(vorim, { minTrustScore: 70 });
87
+ *
88
+ * const auth = await ap2.authorizeMandate({ agentId: 'agid_abc123', mandateType: 'payment', merchant: 'shop', amount: 4999 });
89
+ *
90
+ * app.post('/ap2/mandate', ap2.middleware(), presentMandateHandler);
91
+ * ```
92
+ */
93
+ declare function createVorimAP2(sdk: VorimSDK, config?: Partial<AP2Config>): VorimAP2;
94
+
95
+ export { type AP2Authorization, type AP2Config, type AP2MandateRequest, type AP2MandateType, VorimAP2, createVorimAP2 };
@@ -0,0 +1,192 @@
1
+ // src/integrations/ap2.ts
2
+ var VorimAP2 = class _VorimAP2 {
3
+ sdk;
4
+ minTrustScore;
5
+ requiredScope;
6
+ /** One-time guard so the no-auth confused-deputy warning logs at most once. */
7
+ static _warnedNoAuth = false;
8
+ constructor(sdk, config) {
9
+ this.sdk = sdk;
10
+ this.minTrustScore = config?.minTrustScore ?? 50;
11
+ this.requiredScope = config?.requiredScope ?? "agent:transact";
12
+ }
13
+ /**
14
+ * Verify an agent is authorized to present/sign an AP2 mandate. Same gate as
15
+ * the other payment integrations: transact scope → active status → trust
16
+ * threshold. Every outcome (allow or deny) is recorded on the audit trail,
17
+ * carrying the AP2 checkout hash so the evidence trails reconcile.
18
+ */
19
+ async authorizeMandate(req) {
20
+ const threshold = req.minTrustScore ?? this.minTrustScore;
21
+ const action = `ap2.${req.mandateType ?? "payment"}_mandate.authorize`;
22
+ try {
23
+ const perm = await this.sdk.check(req.agentId, this.requiredScope);
24
+ if (!perm.allowed) {
25
+ await this.logEvent(req.agentId, action, "denied", req, { reason: `Missing ${this.requiredScope}` });
26
+ return {
27
+ authorized: false,
28
+ agentId: req.agentId,
29
+ trustScore: 0,
30
+ status: "unknown",
31
+ reason: `Agent does not have ${this.requiredScope} permission`,
32
+ scopeVerified: false
33
+ };
34
+ }
35
+ } catch (e) {
36
+ await this.logEvent(req.agentId, action, "denied", req, { reason: e.message });
37
+ return {
38
+ authorized: false,
39
+ agentId: req.agentId,
40
+ trustScore: 0,
41
+ status: "unknown",
42
+ reason: `Permission check failed: ${e.message}`,
43
+ scopeVerified: false
44
+ };
45
+ }
46
+ let score = 0;
47
+ let status = "unknown";
48
+ try {
49
+ const trust = await this.sdk.verify(req.agentId);
50
+ score = trust.trust_score ?? 0;
51
+ status = trust.status ?? "unknown";
52
+ } catch {
53
+ }
54
+ if (status !== "active") {
55
+ await this.logEvent(req.agentId, action, "denied", req, { reason: `Status: ${status}` });
56
+ return {
57
+ authorized: false,
58
+ agentId: req.agentId,
59
+ trustScore: score,
60
+ status,
61
+ reason: `Agent is ${status}, not active`,
62
+ scopeVerified: true
63
+ };
64
+ }
65
+ if (score < threshold) {
66
+ await this.logEvent(req.agentId, action, "denied", req, { reason: `Score ${score} < ${threshold}` });
67
+ return {
68
+ authorized: false,
69
+ agentId: req.agentId,
70
+ trustScore: score,
71
+ status,
72
+ reason: `Trust score ${score} is below minimum ${threshold}`,
73
+ scopeVerified: true
74
+ };
75
+ }
76
+ await this.logEvent(req.agentId, action, "success", req, { trust_score: score });
77
+ return {
78
+ authorized: true,
79
+ agentId: req.agentId,
80
+ trustScore: score,
81
+ status,
82
+ scopeVerified: true
83
+ };
84
+ }
85
+ /** Log that an AP2 mandate was presented/signed. */
86
+ async logMandatePresented(agentId, mandateId, req) {
87
+ await this.logEvent(agentId, `ap2.${req?.mandateType ?? "payment"}_mandate.presented`, "success", req, { mandate_id: mandateId });
88
+ }
89
+ /** Log that an AP2 payment receipt was issued and verified (payment settled). */
90
+ async logReceiptVerified(agentId, receiptId, req) {
91
+ await this.logEvent(agentId, "ap2.receipt.verified", "success", req, { receipt_id: receiptId });
92
+ }
93
+ /** Log that an AP2 mandate/payment was rejected or failed downstream. */
94
+ async logMandateRejected(agentId, mandateId, reason, req) {
95
+ await this.logEvent(agentId, `ap2.${req?.mandateType ?? "payment"}_mandate.rejected`, "error", req, { mandate_id: mandateId, reject_reason: reason });
96
+ }
97
+ /**
98
+ * Express/Connect middleware for a merchant/processor endpoint that gates an
99
+ * AP2 mandate presentation on agent authorization.
100
+ *
101
+ * ⚠️ CONFUSED-DEPUTY WARNING. This evaluates the *named* agent's standing
102
+ * only; it does NOT prove the HTTP caller controls that agent, and it does
103
+ * NOT itself verify the AP2 mandate's cryptographic signature (do that with
104
+ * the AP2 SDK). Authenticate the caller-to-agent binding upstream, or pass
105
+ * `authenticate`. When omitted, a one-time warning is logged.
106
+ */
107
+ middleware(options) {
108
+ const header = options?.agentIdHeader ?? "x-vorim-agent-id";
109
+ const threshold = options?.minTrustScore ?? this.minTrustScore;
110
+ const authenticate = options?.authenticate;
111
+ if (!authenticate && !_VorimAP2._warnedNoAuth) {
112
+ _VorimAP2._warnedNoAuth = true;
113
+ console.warn(
114
+ "[vorim] VorimAP2.middleware() called without an `authenticate` hook: it authorizes the agent NAMED in the header but does not verify the caller controls that agent (confused-deputy risk), and it does not verify the AP2 mandate signature. Authenticate the caller-to-agent binding upstream or pass `authenticate`."
115
+ );
116
+ }
117
+ return async (req, res, next) => {
118
+ const agentId = req.headers?.[header] || req.headers?.[header.toLowerCase()] || "";
119
+ if (!agentId) {
120
+ res.status(401).json({
121
+ error: { code: "MISSING_AGENT_ID", message: `Agent ID required in ${header} header` }
122
+ });
123
+ return;
124
+ }
125
+ if (authenticate) {
126
+ let bound = false;
127
+ try {
128
+ bound = await authenticate(agentId, req);
129
+ } catch {
130
+ bound = false;
131
+ }
132
+ if (!bound) {
133
+ res.status(401).json({
134
+ error: { code: "AGENT_BINDING_UNVERIFIED", message: "Caller is not authenticated as the named agent" }
135
+ });
136
+ return;
137
+ }
138
+ }
139
+ const auth = await this.authorizeMandate({
140
+ agentId,
141
+ mandateType: req.body?.mandateType || "payment",
142
+ merchant: req.body?.merchant || "",
143
+ amount: req.body?.amount || 0,
144
+ currency: req.body?.currency || "usd",
145
+ checkoutHash: req.body?.checkoutHash || "",
146
+ minTrustScore: threshold
147
+ });
148
+ if (!auth.authorized) {
149
+ res.status(403).json({
150
+ error: {
151
+ code: "UNAUTHORIZED_AGENT",
152
+ message: auth.reason || "Agent not authorized to present an AP2 mandate",
153
+ trust_score: auth.trustScore,
154
+ status: auth.status
155
+ }
156
+ });
157
+ return;
158
+ }
159
+ req.vorimAuthorization = auth;
160
+ next();
161
+ };
162
+ }
163
+ async logEvent(agentId, action, result, req, metadata) {
164
+ try {
165
+ await this.sdk.emit({
166
+ agent_id: agentId,
167
+ event_type: "api_request",
168
+ action,
169
+ result,
170
+ resource: req?.merchant || "",
171
+ metadata: {
172
+ mandate_type: req?.mandateType || "",
173
+ merchant: req?.merchant || "",
174
+ amount: req?.amount || 0,
175
+ currency: req?.currency || "",
176
+ checkout_hash: req?.checkoutHash || "",
177
+ mode: req?.mode || "",
178
+ ...metadata
179
+ }
180
+ });
181
+ } catch {
182
+ }
183
+ }
184
+ };
185
+ function createVorimAP2(sdk, config) {
186
+ return new VorimAP2(sdk, config);
187
+ }
188
+ export {
189
+ VorimAP2,
190
+ createVorimAP2
191
+ };
192
+ //# sourceMappingURL=ap2.js.map