@agentokratia/x402-escrow 2.0.0 → 2.0.1

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.
@@ -77,6 +77,7 @@ declare function parsePaymentResponseHeader(header: string): PaymentResponseData
77
77
  * - LocalStorage: For browser persistence across page loads
78
78
  */
79
79
 
80
+ type SessionStatus = 'active' | 'inactive' | 'expired';
80
81
  interface StoredSession {
81
82
  sessionId: string;
82
83
  sessionToken: string;
@@ -86,6 +87,7 @@ interface StoredSession {
86
87
  balance: string;
87
88
  authorizationExpiry: number;
88
89
  createdAt: number;
90
+ status: SessionStatus;
89
91
  }
90
92
  interface SessionStorage {
91
93
  get(network: string, receiver: Address): StoredSession | null;
@@ -146,7 +148,11 @@ declare class SessionManager {
146
148
  /**
147
149
  * Store a session from escrow settlement response.
148
150
  */
149
- store(session: Omit<StoredSession, 'createdAt'>): void;
151
+ store(session: Omit<StoredSession, 'createdAt' | 'status'>): void;
152
+ /**
153
+ * Update session status (e.g., mark as inactive when reclaimed).
154
+ */
155
+ setStatus(sessionId: string, status: SessionStatus): void;
150
156
  /**
151
157
  * Get session for a specific receiver.
152
158
  */
@@ -167,6 +173,14 @@ declare class SessionManager {
167
173
  * Get all stored sessions.
168
174
  */
169
175
  getAll(): StoredSession[];
176
+ /**
177
+ * Get all active sessions for a specific receiver.
178
+ */
179
+ getAllForReceiver(receiver: Address): StoredSession[];
180
+ /**
181
+ * Get session by ID.
182
+ */
183
+ getById(sessionId: string): StoredSession | null;
170
184
  /**
171
185
  * Remove a specific session.
172
186
  */
@@ -202,7 +216,22 @@ interface PaymentPayload {
202
216
  }
203
217
  interface SchemeNetworkClient {
204
218
  readonly scheme: string;
205
- createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
219
+ createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements, options?: PayloadOptions): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
220
+ }
221
+ /**
222
+ * Options for createPaymentPayload
223
+ */
224
+ interface PayloadOptions {
225
+ /**
226
+ * Force creation of a new session even if a valid existing session exists.
227
+ * Useful when the user explicitly wants a fresh session with new deposit.
228
+ */
229
+ forceNew?: boolean;
230
+ /**
231
+ * Use a specific session by ID instead of auto-selecting the best one.
232
+ * If the session doesn't exist or is expired, falls back to creating new session.
233
+ */
234
+ sessionId?: string;
206
235
  }
207
236
  interface EscrowSchemeOptions {
208
237
  /** Session duration in seconds (default: 1 hour) */
@@ -240,13 +269,22 @@ declare class EscrowScheme implements SchemeNetworkClient {
240
269
  private readonly sessionDuration;
241
270
  private readonly refundWindow;
242
271
  private readonly customDepositAmount?;
272
+ /** Force new session on next createPaymentPayload call (resets after use) */
273
+ forceNewSession: boolean;
274
+ /** Use specific session ID on next createPaymentPayload call (resets after use) */
275
+ selectedSessionId?: string;
243
276
  constructor(walletClient: WalletClient, options?: EscrowSchemeOptions);
244
277
  get address(): Address;
245
278
  /**
246
279
  * Creates payment payload for escrow scheme.
247
280
  * Auto-detects whether to create new session or use existing one.
281
+ *
282
+ * @param x402Version - Protocol version
283
+ * @param paymentRequirements - Payment requirements from 402 response
284
+ * @param options - Optional payload options
285
+ * @param options.forceNew - Skip session lookup, always create new session
248
286
  */
249
- createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
287
+ createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements, options?: PayloadOptions): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
250
288
  /**
251
289
  * Session USAGE payload - uses existing session (no signature).
252
290
  */
@@ -291,6 +329,35 @@ declare class EscrowScheme implements SchemeNetworkClient {
291
329
  */
292
330
 
293
331
  type FetchLike = typeof globalThis.fetch;
332
+ /**
333
+ * Extended RequestInit with session selection option.
334
+ */
335
+ interface EscrowRequestInit extends RequestInit {
336
+ /**
337
+ * Session selection mode:
338
+ * - 'auto' (default): Auto-select best available session for the receiver
339
+ * - 'new': Force create new session (ignores existing sessions)
340
+ * - string (session ID): Use a specific session by ID
341
+ *
342
+ * @example
343
+ * ```typescript
344
+ * // Auto-select best session (default)
345
+ * await escrowFetch(url);
346
+ * await escrowFetch(url, { session: 'auto' });
347
+ *
348
+ * // Force new session creation
349
+ * await escrowFetch(url, { session: 'new' });
350
+ *
351
+ * // Use specific session
352
+ * await escrowFetch(url, { session: 'abc-123-def' });
353
+ * ```
354
+ */
355
+ session?: 'auto' | 'new' | string;
356
+ }
357
+ /**
358
+ * Escrow-aware fetch function with session options.
359
+ */
360
+ type EscrowFetch = (input: RequestInfo | URL, init?: EscrowRequestInit) => Promise<Response>;
294
361
  /** Options for createEscrowFetch */
295
362
  interface CreateEscrowFetchOptions extends EscrowSchemeOptions {
296
363
  /** Custom fetch implementation (default: globalThis.fetch) */
@@ -299,7 +366,7 @@ interface CreateEscrowFetchOptions extends EscrowSchemeOptions {
299
366
  /** Result from createEscrowFetch */
300
367
  interface EscrowFetchResult {
301
368
  /** Fetch function with automatic payment + session handling */
302
- fetch: FetchLike;
369
+ fetch: EscrowFetch;
303
370
  /** Access to underlying scheme for session management */
304
371
  scheme: EscrowScheme;
305
372
  /** Access to x402Client for adding hooks (onBeforePaymentCreation, etc.) */
@@ -309,26 +376,34 @@ interface EscrowFetchResult {
309
376
  * Creates a fetch function with automatic escrow payment and session handling.
310
377
  * This is the simplest way to integrate x402 escrow payments.
311
378
  *
312
- * @example Simple usage
379
+ * @example Basic usage (auto-selects best session)
313
380
  * ```typescript
314
- * const { fetch: escrowFetch, scheme, x402 } = createEscrowFetch(walletClient);
381
+ * const { fetch: escrowFetch, scheme } = createEscrowFetch(walletClient);
315
382
  * const response = await escrowFetch('https://api.example.com/premium');
383
+ * ```
316
384
  *
317
- * // Access sessions
318
- * scheme.sessions.getAll();
319
- * scheme.sessions.hasValid(receiverAddress, '10000');
385
+ * @example Session selection options
386
+ * ```typescript
387
+ * // Auto-select best session (default)
388
+ * await escrowFetch(url);
389
+ * await escrowFetch(url, { session: 'auto' });
390
+ *
391
+ * // Force new session creation
392
+ * await escrowFetch(url, { session: 'new' });
393
+ *
394
+ * // Use specific session by ID
395
+ * await escrowFetch(url, { session: 'session-abc-123' });
320
396
  * ```
321
397
  *
322
- * @example With custom fetch and hooks
398
+ * @example Access session manager
323
399
  * ```typescript
324
- * const { fetch: escrowFetch, x402 } = createEscrowFetch(walletClient, {
325
- * fetch: customFetch, // Use ky, undici, node-fetch, etc.
326
- * });
400
+ * const { fetch: escrowFetch, scheme } = createEscrowFetch(walletClient);
327
401
  *
328
- * // Add hooks for user control
329
- * x402.onBeforePaymentCreation(async (ctx) => {
330
- * console.log('About to pay:', ctx.paymentRequirements);
331
- * });
402
+ * // List all sessions for a receiver
403
+ * const sessions = scheme.sessions.getAllForReceiver(receiverAddress);
404
+ *
405
+ * // Check if valid session exists
406
+ * scheme.sessions.hasValid(receiverAddress, '10000');
332
407
  * ```
333
408
  */
334
409
  declare function createEscrowFetch(walletClient: WalletClient, options?: CreateEscrowFetchOptions): EscrowFetchResult;
@@ -369,4 +444,4 @@ interface AxiosResponseLike {
369
444
  */
370
445
  declare function withAxiosSessionExtraction(escrowScheme: EscrowScheme): <T extends AxiosResponseLike>(response: T) => T;
371
446
 
372
- export { BrowserLocalStorage as B, type CreateEscrowFetchOptions as C, type EscrowFetchResult as E, InMemoryStorage as I, type PaymentResponseData as P, type StoredSession as S, X402_HEADERS as X, EscrowScheme as a, type EscrowSchemeOptions as b, createEscrowFetch as c, withAxiosSessionExtraction as d, type SettleResponse as e, SessionManager as f, type SessionManagerOptions as g, type SessionStorage as h, createStorage as i, X402_VERSION as j, parsePaymentResponseHeader as p, withSessionExtraction as w };
447
+ export { BrowserLocalStorage as B, type CreateEscrowFetchOptions as C, type EscrowFetchResult as E, InMemoryStorage as I, type PaymentResponseData as P, type StoredSession as S, X402_HEADERS as X, EscrowScheme as a, type EscrowSchemeOptions as b, createEscrowFetch as c, withAxiosSessionExtraction as d, type SettleResponse as e, type EscrowFetch as f, type EscrowRequestInit as g, type PayloadOptions as h, SessionManager as i, type SessionManagerOptions as j, type SessionStorage as k, createStorage as l, X402_VERSION as m, parsePaymentResponseHeader as p, withSessionExtraction as w };
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@agentokratia/x402-escrow",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Escrow payment scheme for x402 protocol - session-based payments for high-frequency APIs",
5
5
  "license": "MIT",
6
6
  "author": "Agentokratia",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "https://github.com/agentokratia/x402-escrow",
9
+ "url": "https://github.com/Agentokratia/x402-escrow",
10
10
  "directory": "packages/escrow"
11
11
  },
12
- "homepage": "https://github.com/agentokratia/x402-escrow/tree/main/packages/escrow",
13
- "bugs": "https://github.com/agentokratia/x402-escrow/issues",
12
+ "homepage": "https://github.com/Agentokratia/x402-escrow/tree/main/packages/escrow",
13
+ "bugs": "https://github.com/Agentokratia/x402-escrow/issues",
14
14
  "keywords": [
15
15
  "x402",
16
16
  "escrow",
@@ -48,12 +48,15 @@
48
48
  "files": [
49
49
  "dist"
50
50
  ],
51
- "dependencies": {
52
- "viem": "^2.21.9"
51
+ "scripts": {
52
+ "build": "tsup",
53
+ "dev": "tsup --watch",
54
+ "typecheck": "tsc --noEmit"
53
55
  },
54
56
  "peerDependencies": {
55
57
  "@x402/core": "^2.0.0",
56
- "@x402/fetch": "^2.0.0"
58
+ "@x402/fetch": "^2.0.0",
59
+ "viem": "^2.21.9"
57
60
  },
58
61
  "peerDependenciesMeta": {
59
62
  "@x402/fetch": {
@@ -64,11 +67,7 @@
64
67
  "@x402/core": "^2.0.0",
65
68
  "@x402/fetch": "^2.0.0",
66
69
  "tsup": "^8.0.0",
67
- "typescript": "^5.0.0"
68
- },
69
- "scripts": {
70
- "build": "tsup",
71
- "dev": "tsup --watch",
72
- "typecheck": "tsc --noEmit"
70
+ "typescript": "^5.0.0",
71
+ "viem": "^2.21.9"
73
72
  }
74
- }
73
+ }