@routedock/routedock 0.1.1 → 0.2.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 (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +281 -19
  3. package/dist/MppSessionHandler-B4TyD7Xg.d.cts +121 -0
  4. package/dist/MppSessionHandler-CLB5SzgN.d.ts +121 -0
  5. package/dist/ProviderRegistry-BJ9oJLVz.d.cts +142 -0
  6. package/dist/ProviderRegistry-C50nyhaF.d.ts +142 -0
  7. package/dist/RouteDockClient-B1roCa8h.d.cts +455 -0
  8. package/dist/RouteDockClient-BbkqjhMG.d.ts +455 -0
  9. package/dist/SessionReconciler-Dscw43F3.d.ts +125 -0
  10. package/dist/SessionReconciler-bi0z2qtN.d.cts +125 -0
  11. package/dist/SessionStore-BpK8vv0L.d.cts +111 -0
  12. package/dist/SessionStore-DvkZv8-E.d.ts +111 -0
  13. package/dist/client.cjs +2419 -202
  14. package/dist/client.d.cts +50 -81
  15. package/dist/client.d.ts +50 -81
  16. package/dist/client.js +2373 -203
  17. package/dist/index.cjs +3080 -298
  18. package/dist/index.d.cts +12 -3
  19. package/dist/index.d.ts +12 -3
  20. package/dist/index.js +3026 -298
  21. package/dist/provider/fastify.cjs +882 -0
  22. package/dist/provider/fastify.d.cts +35 -0
  23. package/dist/provider/fastify.d.ts +35 -0
  24. package/dist/provider/fastify.js +880 -0
  25. package/dist/provider/hono.cjs +948 -0
  26. package/dist/provider/hono.d.cts +99 -0
  27. package/dist/provider/hono.d.ts +99 -0
  28. package/dist/provider/hono.js +938 -0
  29. package/dist/provider.cjs +827 -97
  30. package/dist/provider.d.cts +32 -25
  31. package/dist/provider.d.ts +32 -25
  32. package/dist/provider.js +810 -99
  33. package/dist/react.cjs +2641 -0
  34. package/dist/react.d.cts +107 -0
  35. package/dist/react.d.ts +107 -0
  36. package/dist/react.js +2634 -0
  37. package/dist/routedockMiddleware-BlDsHuPl.d.ts +59 -0
  38. package/dist/routedockMiddleware-D97A5ptQ.d.cts +59 -0
  39. package/dist/schema.cjs +364 -0
  40. package/dist/schema.d.cts +3 -0
  41. package/dist/schema.d.ts +3 -0
  42. package/dist/schema.js +359 -0
  43. package/dist/sign-CUf3rhTx.d.cts +53 -0
  44. package/dist/sign-Dpj2Mp7k.d.ts +53 -0
  45. package/dist/testing.cjs +57 -0
  46. package/dist/testing.d.cts +95 -0
  47. package/dist/testing.d.ts +95 -0
  48. package/dist/testing.js +54 -0
  49. package/dist/types-DmhmfLzP.d.cts +410 -0
  50. package/dist/types-DmhmfLzP.d.ts +410 -0
  51. package/package.json +143 -23
  52. package/PUBLISH_CHECKLIST.md +0 -70
  53. package/dist/SessionStore-CCVO0PHM.d.cts +0 -152
  54. package/dist/SessionStore-CCVO0PHM.d.ts +0 -152
  55. package/dist/routedockMiddleware-B7WTB3LT.d.cts +0 -53
  56. package/dist/routedockMiddleware-DRgdrycG.d.ts +0 -53
  57. package/src/client/ModeRouter.ts +0 -104
  58. package/src/client/MppChargeClient.ts +0 -44
  59. package/src/client/MppSessionClient.ts +0 -144
  60. package/src/client/RouteDockClient.ts +0 -124
  61. package/src/client/index.ts +0 -8
  62. package/src/client/x402Client.ts +0 -84
  63. package/src/index.ts +0 -9
  64. package/src/provider/MppChargeHandler.ts +0 -90
  65. package/src/provider/MppSessionHandler.ts +0 -157
  66. package/src/provider/index.ts +0 -7
  67. package/src/provider/routedockMiddleware.ts +0 -154
  68. package/src/provider/x402Handler.ts +0 -164
  69. package/src/schemas/routedock.schema.json +0 -159
  70. package/src/store/SessionStore.ts +0 -90
  71. package/src/tests/smoke.test.ts +0 -228
  72. package/src/types.ts +0 -165
  73. package/tsconfig.json +0 -14
  74. package/tsup.config.ts +0 -15
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 RouteDock
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -6,8 +6,6 @@ Unified payment execution layer for autonomous agents on Stellar. One SDK for x4
6
6
 
7
7
  ```bash
8
8
  npm install @routedock/sdk
9
- # or
10
- pnpm add @routedock/sdk
11
9
  ```
12
10
 
13
11
  ## Agent Usage
@@ -19,17 +17,17 @@ import { Keypair } from '@stellar/stellar-sdk'
19
17
  const client = new RouteDockClient({
20
18
  wallet: Keypair.fromSecret(process.env.AGENT_SECRET),
21
19
  network: 'testnet',
22
- spendCap: { daily: '1.00', asset: 'USDC' }, // optional local guard
20
+ spendCap: { daily: '1.00', asset: 'USDC' },
23
21
  })
24
22
 
25
23
  // Single call — SDK reads manifest, picks mode automatically
26
- const result = await client.pay('https://provider.railway.app/price')
24
+ const result = await client.pay('https://provider.example.com/price')
27
25
  // result.data — response body
28
26
  // result.txHash — settlement hash (or null for session vouchers)
29
27
  // result.mode — 'x402' | 'mpp-charge' | 'mpp-session'
30
28
 
31
29
  // Sustained streaming access
32
- const session = await client.openSession('https://provider.railway.app/stream/orderbook')
30
+ const session = await client.openSession('https://provider.example.com/stream/orderbook')
33
31
  for await (const update of session.stream()) {
34
32
  console.log(update)
35
33
  if (done) break
@@ -37,51 +35,315 @@ for await (const update of session.stream()) {
37
35
  await session.close() // triggers on-chain settlement
38
36
  ```
39
37
 
38
+ ### Durable spend cap
39
+
40
+ The `spendCap` is enforced via a pluggable `SpendStore` (`read()` / `write()`).
41
+ By default the client uses an in-memory store that resets on every process
42
+ restart — and logs a startup warning — so the cap is **not** durable. For
43
+ production, inject a persistent implementation so a crash or restart can't reset
44
+ the accumulator and bypass the cap:
45
+
46
+ ```ts
47
+ import { RouteDockClient, type SpendStore, type DailySpend } from '@routedock/sdk/client'
48
+
49
+ const redisSpendStore: SpendStore = {
50
+ async read(): Promise<DailySpend | null> {
51
+ const raw = await redis.get('routedock:dailySpend')
52
+ return raw ? (JSON.parse(raw) as DailySpend) : null
53
+ },
54
+ async write(state: DailySpend): Promise<void> {
55
+ await redis.set('routedock:dailySpend', JSON.stringify(state))
56
+ },
57
+ }
58
+
59
+ const client = new RouteDockClient({
60
+ wallet: Keypair.fromSecret(process.env.AGENT_SECRET),
61
+ network: 'mainnet',
62
+ spendCap: { daily: '1.00', asset: 'USDC' },
63
+ spendStore: redisSpendStore,
64
+ })
65
+ ```
66
+
40
67
  ## Provider Usage
41
68
 
42
69
  ```ts
43
70
  import express from 'express'
44
- import { routedock, SupabaseSessionStore } from '@routedock/sdk/provider'
45
- import { createClient } from '@supabase/supabase-js'
71
+ import { routedock } from '@routedock/sdk/provider'
46
72
 
47
73
  const app = express()
48
74
 
49
- // Price endpoint — x402 + MPP charge
50
75
  app.use('/price', routedock({
51
76
  modes: ['x402', 'mpp-charge'],
52
77
  pricing: { x402: '0.001', 'mpp-charge': '0.0008' },
53
78
  asset: 'USDC',
79
+ assetContract: process.env.USDC_ASSET_CONTRACT,
54
80
  payee: process.env.STELLAR_PAYEE_ADDRESS,
55
- network: process.env.STELLAR_NETWORK as 'testnet' | 'mainnet',
56
- facilitatorApiKey: process.env.OPENZEPPELIN_API_KEY,
81
+ network: 'testnet',
82
+ payeeSecretKey: process.env.STELLAR_PAYEE_SECRET,
83
+ manifest,
84
+ onSettled: async (txHash, amount, mode) => {
85
+ console.log(`settled: ${mode} ${amount} USDC — ${txHash}`)
86
+ },
57
87
  }))
88
+ ```
89
+
90
+ ## Session Lifecycle Hooks
91
+
92
+ For `mpp-session` mode, the middleware exposes three hooks that fire at each stage of the payment channel lifecycle:
58
93
 
59
- // Stream endpoint — MPP session only
60
- const store = new SupabaseSessionStore(createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SERVICE_KEY))
61
- app.use('/stream/orderbook', routedock({
94
+ ```ts
95
+ app.use('/stream', routedock({
62
96
  modes: ['mpp-session'],
63
- pricing: { 'mpp-session': { rate: '0.0001', channelContract: process.env.CHANNEL_CONTRACT_ID } },
97
+ pricing: { 'mpp-session': { rate: '0.0001', channelContract: CHANNEL_CONTRACT } },
64
98
  asset: 'USDC',
65
- payee: process.env.STELLAR_PAYEE_ADDRESS,
66
- network: process.env.STELLAR_NETWORK as 'testnet' | 'mainnet',
67
- store,
99
+ assetContract: USDC_ASSET_CONTRACT,
100
+ payee: PAYEE_ADDRESS,
101
+ network: 'testnet',
102
+ payeeSecretKey: PAYEE_SECRET,
103
+ manifest,
104
+ commitmentPublicKey: COMMITMENT_PUBLIC_KEY,
105
+
106
+ // Fires once when the first voucher is verified for a new session
107
+ onSessionOpen: async (channelId) => {
108
+ await db.from('sessions').insert({
109
+ channel_id: channelId,
110
+ status: 'open',
111
+ voucher_count: 0,
112
+ })
113
+ },
114
+
115
+ // Fires after each verified ed25519 commitment (off-chain, no tx fee)
116
+ onVoucher: async (voucherIndex, cumulativeAmount) => {
117
+ await db.from('sessions').update({
118
+ voucher_count: voucherIndex,
119
+ cumulative_amount: parseFloat(cumulativeAmount),
120
+ }).eq('channel_id', channelId)
121
+ },
122
+
123
+ // Fires after the on-chain channel close settles the cumulative amount
124
+ onSettled: async (txHash, totalPaid, mode) => {
125
+ await db.from('sessions').update({
126
+ status: 'closed',
127
+ settlement_tx_hash: txHash,
128
+ }).eq('channel_id', channelId)
129
+ },
68
130
  }))
69
131
  ```
70
132
 
133
+ | Hook | When it fires | On-chain? |
134
+ |---|---|---|
135
+ | `onSessionOpen(channelId)` | First verified voucher in a new session | No |
136
+ | `onVoucher(index, cumulativeAmount)` | Each verified ed25519 commitment | No |
137
+ | `onSettled(txHash, amount, mode)` | Channel close transaction confirmed | Yes |
138
+
139
+ ## React Integration
140
+
141
+ > **Warning:** These hooks create a RouteDockClient in the browser, so the wallet secret and any commitmentSecret are readable by anyone who loads the page.
142
+ >
143
+ > Next.js inlines every `NEXT_PUBLIC_*` variable into the client bundle at build time, so never put a Stellar secret in one.
144
+ >
145
+ > Only use these hooks with a dedicated, low-balance testnet key, and spendCap does not protect a key that has leaked.
146
+ >
147
+ > For production or mainnet, keep RouteDockClient on the server (a route handler or server action) and read the key from a non-public variable such as `AGENT_SECRET`, as the Agent Usage section does.
148
+
149
+ `@routedock/sdk/react` provides hooks for client construction, payments, sessions, and live tx log subscription. Wrap your app with `RouteDockProvider`:
150
+
151
+ ```tsx
152
+ import { RouteDockProvider, useRouteDockClient } from '@routedock/sdk/react'
153
+ import { createClient } from '@supabase/supabase-js'
154
+
155
+ // Dedicated throwaway testnet key. Never a funded or mainnet key.
156
+ const THROWAWAY_TESTNET_SECRET = 'S...'
157
+
158
+ function App({ children }: { children: React.ReactNode }) {
159
+ const client = useRouteDockClient({
160
+ wallet: THROWAWAY_TESTNET_SECRET,
161
+ network: 'testnet',
162
+ spendCap: { daily: '1.00', asset: 'USDC' },
163
+ })
164
+ const supabase = createClient(
165
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
166
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
167
+ )
168
+ return (
169
+ <RouteDockProvider client={client} supabase={supabase}>
170
+ {children}
171
+ </RouteDockProvider>
172
+ )
173
+ }
174
+ ```
175
+
176
+ ### Paying from the server
177
+
178
+ ```ts
179
+ // app/api/price/route.ts (runs on the server only)
180
+ import { RouteDockClient } from '@routedock/sdk/client'
181
+
182
+ const client = new RouteDockClient({
183
+ wallet: process.env.AGENT_SECRET!,
184
+ network: 'testnet',
185
+ spendCap: { daily: '1.00', asset: 'USDC' },
186
+ })
187
+
188
+ export async function POST() {
189
+ const result = await client.pay('https://provider.example.com/price')
190
+ return Response.json({ mode: result.mode, txHash: result.txHash, data: result.data })
191
+ }
192
+ ```
193
+
194
+ ### `usePay(url, options?)`
195
+
196
+ ```tsx
197
+ import { usePay } from '@routedock/sdk/react'
198
+
199
+ function PriceButton() {
200
+ const { pay, result, loading, error } = usePay('https://provider.example.com/price')
201
+ if (error) return <span>Error: {error.message}</span>
202
+ return (
203
+ <button onClick={pay} disabled={loading}>
204
+ {loading ? 'Paying…' : result ? `Paid: ${result.txHash}` : 'Pay 0.001 USDC'}
205
+ </button>
206
+ )
207
+ }
208
+ ```
209
+
210
+ ### `useSession(url)`
211
+
212
+ ```tsx
213
+ import { useEffect } from 'react'
214
+ import { useSession } from '@routedock/sdk/react'
215
+
216
+ function StreamingFeed() {
217
+ const { open, close, status, vouchers, cumulative } = useSession(
218
+ 'https://provider.example.com/stream/orderbook',
219
+ )
220
+ useEffect(() => { void open() }, [])
221
+ return (
222
+ <div>
223
+ <p>Status: {status} — vouchers: {vouchers} — paid: {cumulative} USDC</p>
224
+ <button onClick={close} disabled={status !== 'open'}>Close & settle</button>
225
+ </div>
226
+ )
227
+ }
228
+ ```
229
+
230
+ The hook automatically fires `session.close()` in the background on unmount when status is `open` (best-effort settlement).
231
+
232
+ ### `useTxLog(filter?)`
233
+
234
+ ```tsx
235
+ import { useTxLog } from '@routedock/sdk/react'
236
+
237
+ function ActivityFeed({ channelId }: { channelId: string }) {
238
+ const txLog = useTxLog({ channelId, limit: 25 })
239
+ return (
240
+ <ul>
241
+ {txLog.map((row) => (
242
+ <li key={row.id}>{row.mode} — {row.amount} USDC — {row.tx_hash}</li>
243
+ ))}
244
+ </ul>
245
+ )
246
+ }
247
+ ```
248
+
249
+ | Hook | Returns | Requires from `RouteDockProvider` |
250
+ |---|---|---|
251
+ | `useRouteDockClient(config)` | Memoized `RouteDockClient` | — (creator hook) |
252
+ | `usePay(url, options?)` | `{ pay, result, loading, error }` | `client` |
253
+ | `useSession(url)` | `{ session, status, vouchers, cumulative, error, open, close }` | `client` (with `commitmentSecret`) |
254
+ | `useTxLog(filter?)` | `TxLogRow[]` (newest first) | `supabase` |
255
+
256
+ React is a peer dependency — install `react@^18` (or `^19`) in your app.
257
+
258
+ ---
259
+
260
+ ## Error Handling
261
+
262
+ All SDK failures extend `RouteDockError` with a stable `code`, `retryable` flag, and optional `cause`. Transient failures (network timeouts, facilitator 5xx, Horizon RPC errors) are retried automatically with exponential backoff unless you disable retries.
263
+
264
+ ```ts
265
+ import {
266
+ RouteDockClient,
267
+ RouteDockError,
268
+ RouteDockFacilitatorError,
269
+ RouteDockManifestError,
270
+ RouteDockPolicyRejectError,
271
+ } from '@routedock/sdk/client'
272
+
273
+ const client = new RouteDockClient({
274
+ wallet: process.env.AGENT_SECRET!,
275
+ network: 'testnet',
276
+ retryPolicy: { maxAttempts: 4, baseDelayMs: 250 },
277
+ })
278
+
279
+ try {
280
+ const result = await client.pay('https://provider.example.com/price')
281
+ console.log(result.mode, result.txHash)
282
+ } catch (err) {
283
+ if (err instanceof RouteDockPolicyRejectError) {
284
+ console.error('Spend cap exceeded:', err.reason)
285
+ } else if (err instanceof RouteDockFacilitatorError) {
286
+ console.error(`Facilitator HTTP ${err.status} (retryable=${err.retryable})`)
287
+ } else if (err instanceof RouteDockManifestError) {
288
+ console.error('Manifest or config problem:', err.message)
289
+ } else if (err instanceof RouteDockError) {
290
+ console.error(`[${err.code}] ${err.message} (retryable=${err.retryable})`)
291
+ } else {
292
+ throw err
293
+ }
294
+ }
295
+ ```
296
+
297
+ | Error | `retryable` | Typical cause |
298
+ |---|---|---|
299
+ | `RouteDockManifestError` | no | Invalid manifest, missing pricing fields |
300
+ | `RouteDockNoSupportedModeError` | no | Provider has no compatible payment mode |
301
+ | `RouteDockFacilitatorError` | yes | Facilitator/provider 429 or 5xx |
302
+ | `RouteDockNetworkError` | yes | Timeouts, connection failures |
303
+ | `RouteDockSignatureError` | no | Signing or commitment failure |
304
+ | `RouteDockVoucherMonotonicityError` | no | Non-increasing voucher amount |
305
+ | `RouteDockPolicyRejectError` | no | Local spend cap exceeded |
306
+ | `RouteDockChannelStateError` | no | Channel simulate/close invariant violation |
307
+
71
308
  ## Mode Selection Logic
72
309
 
73
310
  The `ModeRouter` uses this decision tree (deterministic, no randomness):
74
311
 
75
312
  1. If `{ sustained: true }` passed AND manifest supports `mpp-session` → session
76
313
  2. Else if manifest supports `mpp-charge` AND network is Stellar → MPP charge (lower fees)
77
- 3. Else if manifest supports `x402` → x402 with OZ facilitator
314
+ 3. Else if manifest supports `x402` → x402 with facilitator
78
315
  4. Else throw `RouteDockNoSupportedModeError`
79
316
 
80
317
  Override with `{ forceMode: 'x402' }` to bypass auto-selection.
81
318
 
319
+ ## Dispute Resolution
320
+
321
+ For `mpp-session` mode, the session handle exposes three methods to handle server unavailability or crashes:
322
+
323
+ ```ts
324
+ const session = await client.openSession('https://provider.example.com/stream')
325
+
326
+ // Check the current dispute status (open, in-refund-window, refundable, settled)
327
+ const status = await session.getDisputeStatus()
328
+
329
+ // If server is unresponsive, request a refund (starts the refund window)
330
+ const refundTxHash = await session.requestRefund()
331
+
332
+ // Server-side: settle with the latest signed voucher before refund window expires
333
+ const settleTxHash = await session.settleWithLatestVoucher()
334
+ ```
335
+
336
+ | Method | Purpose | Requires | Returns |
337
+ |--------|---------|----------|---------|
338
+ | `getDisputeStatus()` | Query channel state | None | `'open' \| 'in-refund-window' \| 'refundable' \| 'settled'` |
339
+ | `requestRefund()` | Initiate refund process | Signed agent keypair | Transaction hash (string) |
340
+ | `settleWithLatestVoucher()` | Server counter-settle | Latest voucher signature | Transaction hash (string) |
341
+
342
+ Raises: `RouteDockDisputeError`, `RouteDockChannelStateError`, `RouteDockRefundWindowError`
343
+
82
344
  ## Security
83
345
 
84
- The `one-way-channel` Soroban contract wrapped by `MppSessionClient` is **unaudited** (`stellar-experimental/one-way-channel`). Safe defaults are enforced: 17280 ledger refund window, durable `SupabaseSessionStore` with monotonic invariant. Production use should await a formal audit.
346
+ The `one-way-channel` Soroban contract wrapped by `MppSessionClient` is **unaudited** (`stellar-experimental/one-way-channel`). Safe defaults are enforced: 17280 ledger refund window, monotonic cumulative enforcement at application, database, and contract layers. Production mainnet use should await a formal audit.
85
347
 
86
348
  ## License
87
349
 
@@ -0,0 +1,121 @@
1
+ import { RequestHandler } from 'express';
2
+ import { Store } from 'mppx';
3
+ import { R as RouteDockManifest } from './types-DmhmfLzP.cjs';
4
+
5
+ /**
6
+ * Idempotency store for payment settlement.
7
+ *
8
+ * `x402Handler` and `MppChargeHandler` settle on every request carrying a
9
+ * payment header. An agent that retries after a post-settlement network
10
+ * timeout resends the *same* signed payment, which would settle a second time
11
+ * and invoke `onSettled` twice — double-counting in billing.
12
+ *
13
+ * Before settling, a handler derives an idempotency key from the inbound
14
+ * payment header(s) and checks this store. On a hit it replays the cached
15
+ * settlement response and skips both the on-chain settle and `onSettled`.
16
+ *
17
+ * The default {@link InMemorySeenTxStore} is per-handler and per-process. For
18
+ * multi-instance deployments, supply a shared implementation backed by Redis,
19
+ * Supabase, etc.
20
+ */
21
+ /** Cached outcome of a settlement, replayed on a duplicate payment. */
22
+ interface SettlementRecord {
23
+ /** On-chain transaction hash (or settlement reference), if known. */
24
+ txHash: string | null;
25
+ /** Response headers to re-apply on a replay (e.g. `X-Payment-Response`). */
26
+ headers?: Record<string, string>;
27
+ }
28
+ interface SeenTxStore {
29
+ /** Returns the cached settlement for a key, or `undefined` if unseen. */
30
+ get(key: string): Promise<SettlementRecord | undefined> | SettlementRecord | undefined;
31
+ /** Records the settlement outcome for a key. */
32
+ set(key: string, record: SettlementRecord): Promise<void> | void;
33
+ }
34
+ interface InMemorySeenTxStoreOptions {
35
+ /** Maximum number of entries before FIFO eviction. Defaults to 10,000. */
36
+ maxEntries?: number;
37
+ /** Log a startup warning about non-durability. Defaults to true. */
38
+ warn?: boolean;
39
+ }
40
+ /**
41
+ * Default in-memory {@link SeenTxStore}. Bounded by `maxEntries` with FIFO
42
+ * eviction so a long-running process cannot grow without limit.
43
+ *
44
+ * **Not durable.** On serverless/edge runtimes each request may land on a
45
+ * fresh isolate with an empty cache, which causes duplicate settlements.
46
+ * Supply a persistent implementation (Supabase, Redis, …) in production.
47
+ */
48
+ declare class InMemorySeenTxStore implements SeenTxStore {
49
+ private readonly map;
50
+ private readonly order;
51
+ constructor(maxEntriesOrOptions?: number | InMemorySeenTxStoreOptions);
52
+ private readonly maxEntries;
53
+ get(key: string): SettlementRecord | undefined;
54
+ set(key: string, record: SettlementRecord): void;
55
+ }
56
+ /**
57
+ * Derive a collision-resistant idempotency key from the inbound
58
+ * payment-bearing headers. Uses SHA-256 instead of 32-bit FNV-1a
59
+ * (which has a 2^32 keyspace — birthday collisions become likely
60
+ * well before the 10_000-entry default store is full).
61
+ *
62
+ * x402 clients send the signed payment in `payment-signature` / `x-payment`;
63
+ * mppx clients send it in `authorization` (the `Payment` scheme). A retry
64
+ * resends byte-identical headers, so hashing the first present one yields a
65
+ * stable key. Returns `null` when no payment header is present (nothing to
66
+ * dedupe — e.g. the initial 402 challenge request).
67
+ */
68
+ declare function paymentIdempotencyKey(getHeader: (name: string) => string | undefined): Promise<string | null>;
69
+
70
+ /** Store shape extended with optional atomic update operation. */
71
+ type ChannelStore = Store.Store & {
72
+ update?: (key: string, fn: (prev: unknown) => unknown) => Promise<unknown>;
73
+ };
74
+ /** Expected payload shape for voucher cumulative amount entries. */
75
+ interface VoucherStoreValue {
76
+ amount: string;
77
+ [key: string]: unknown;
78
+ }
79
+ /** Validates that a stored store value is an object containing a numeric string amount. */
80
+ declare function isVoucherStoreValue(value: unknown): value is VoucherStoreValue;
81
+ type Network = 'testnet' | 'mainnet';
82
+ /** Why a live session was flagged as orphaned (never cleanly closed). */
83
+ type OrphanReason = 'connection-closed' | 'idle-timeout';
84
+ /** Latest signed state of an orphaned session, enough for the reconciler to settle. */
85
+ interface OrphanedSessionInfo {
86
+ /** Cumulative amount of the highest voucher seen (human-readable, 7 dp). */
87
+ cumulativeAmount: string;
88
+ /** Hex-encoded signature of the highest voucher seen. */
89
+ lastSignature: string;
90
+ /** Number of vouchers received before teardown. */
91
+ voucherCount: number;
92
+ /** What triggered the orphan flag. */
93
+ reason: OrphanReason;
94
+ }
95
+ interface MppSessionHandlerOptions {
96
+ payeeSecretKey: string;
97
+ network: Network;
98
+ channelFactory: string;
99
+ rate: string;
100
+ assetContract: string;
101
+ manifest: RouteDockManifest;
102
+ commitmentPublicKey: string;
103
+ onSettled?: (txHash: string, totalPaid: string, mode: string, payer: string | null) => Promise<void>;
104
+ onSessionOpen?: (channelId: string, payer: string | null) => Promise<void>;
105
+ onVoucher?: (channelId: string, voucherIndex: number, cumulativeAmount: string, signature: string) => Promise<void>;
106
+ onCallbackError?: (err: unknown, cb: string) => void;
107
+ /**
108
+ * Called when the client connection drops mid-session or the session goes
109
+ * idle, before a clean close. Persist the session as `closing` so the
110
+ * SessionReconciler can settle it with the latest signed voucher.
111
+ */
112
+ onOrphaned?: (channelId: string, info: OrphanedSessionInfo) => Promise<void>;
113
+ /**
114
+ * Flag the session orphaned after this many milliseconds with no voucher
115
+ * activity. Disabled when unset.
116
+ */
117
+ idleTimeoutMs?: number;
118
+ }
119
+ declare function createMppSessionHandler(opts: MppSessionHandlerOptions): RequestHandler;
120
+
121
+ export { type ChannelStore as C, InMemorySeenTxStore as I, type MppSessionHandlerOptions as M, type OrphanedSessionInfo as O, type SeenTxStore as S, type VoucherStoreValue as V, type SettlementRecord as a, type InMemorySeenTxStoreOptions as b, type OrphanReason as c, createMppSessionHandler as d, isVoucherStoreValue as i, paymentIdempotencyKey as p };
@@ -0,0 +1,121 @@
1
+ import { RequestHandler } from 'express';
2
+ import { Store } from 'mppx';
3
+ import { R as RouteDockManifest } from './types-DmhmfLzP.js';
4
+
5
+ /**
6
+ * Idempotency store for payment settlement.
7
+ *
8
+ * `x402Handler` and `MppChargeHandler` settle on every request carrying a
9
+ * payment header. An agent that retries after a post-settlement network
10
+ * timeout resends the *same* signed payment, which would settle a second time
11
+ * and invoke `onSettled` twice — double-counting in billing.
12
+ *
13
+ * Before settling, a handler derives an idempotency key from the inbound
14
+ * payment header(s) and checks this store. On a hit it replays the cached
15
+ * settlement response and skips both the on-chain settle and `onSettled`.
16
+ *
17
+ * The default {@link InMemorySeenTxStore} is per-handler and per-process. For
18
+ * multi-instance deployments, supply a shared implementation backed by Redis,
19
+ * Supabase, etc.
20
+ */
21
+ /** Cached outcome of a settlement, replayed on a duplicate payment. */
22
+ interface SettlementRecord {
23
+ /** On-chain transaction hash (or settlement reference), if known. */
24
+ txHash: string | null;
25
+ /** Response headers to re-apply on a replay (e.g. `X-Payment-Response`). */
26
+ headers?: Record<string, string>;
27
+ }
28
+ interface SeenTxStore {
29
+ /** Returns the cached settlement for a key, or `undefined` if unseen. */
30
+ get(key: string): Promise<SettlementRecord | undefined> | SettlementRecord | undefined;
31
+ /** Records the settlement outcome for a key. */
32
+ set(key: string, record: SettlementRecord): Promise<void> | void;
33
+ }
34
+ interface InMemorySeenTxStoreOptions {
35
+ /** Maximum number of entries before FIFO eviction. Defaults to 10,000. */
36
+ maxEntries?: number;
37
+ /** Log a startup warning about non-durability. Defaults to true. */
38
+ warn?: boolean;
39
+ }
40
+ /**
41
+ * Default in-memory {@link SeenTxStore}. Bounded by `maxEntries` with FIFO
42
+ * eviction so a long-running process cannot grow without limit.
43
+ *
44
+ * **Not durable.** On serverless/edge runtimes each request may land on a
45
+ * fresh isolate with an empty cache, which causes duplicate settlements.
46
+ * Supply a persistent implementation (Supabase, Redis, …) in production.
47
+ */
48
+ declare class InMemorySeenTxStore implements SeenTxStore {
49
+ private readonly map;
50
+ private readonly order;
51
+ constructor(maxEntriesOrOptions?: number | InMemorySeenTxStoreOptions);
52
+ private readonly maxEntries;
53
+ get(key: string): SettlementRecord | undefined;
54
+ set(key: string, record: SettlementRecord): void;
55
+ }
56
+ /**
57
+ * Derive a collision-resistant idempotency key from the inbound
58
+ * payment-bearing headers. Uses SHA-256 instead of 32-bit FNV-1a
59
+ * (which has a 2^32 keyspace — birthday collisions become likely
60
+ * well before the 10_000-entry default store is full).
61
+ *
62
+ * x402 clients send the signed payment in `payment-signature` / `x-payment`;
63
+ * mppx clients send it in `authorization` (the `Payment` scheme). A retry
64
+ * resends byte-identical headers, so hashing the first present one yields a
65
+ * stable key. Returns `null` when no payment header is present (nothing to
66
+ * dedupe — e.g. the initial 402 challenge request).
67
+ */
68
+ declare function paymentIdempotencyKey(getHeader: (name: string) => string | undefined): Promise<string | null>;
69
+
70
+ /** Store shape extended with optional atomic update operation. */
71
+ type ChannelStore = Store.Store & {
72
+ update?: (key: string, fn: (prev: unknown) => unknown) => Promise<unknown>;
73
+ };
74
+ /** Expected payload shape for voucher cumulative amount entries. */
75
+ interface VoucherStoreValue {
76
+ amount: string;
77
+ [key: string]: unknown;
78
+ }
79
+ /** Validates that a stored store value is an object containing a numeric string amount. */
80
+ declare function isVoucherStoreValue(value: unknown): value is VoucherStoreValue;
81
+ type Network = 'testnet' | 'mainnet';
82
+ /** Why a live session was flagged as orphaned (never cleanly closed). */
83
+ type OrphanReason = 'connection-closed' | 'idle-timeout';
84
+ /** Latest signed state of an orphaned session, enough for the reconciler to settle. */
85
+ interface OrphanedSessionInfo {
86
+ /** Cumulative amount of the highest voucher seen (human-readable, 7 dp). */
87
+ cumulativeAmount: string;
88
+ /** Hex-encoded signature of the highest voucher seen. */
89
+ lastSignature: string;
90
+ /** Number of vouchers received before teardown. */
91
+ voucherCount: number;
92
+ /** What triggered the orphan flag. */
93
+ reason: OrphanReason;
94
+ }
95
+ interface MppSessionHandlerOptions {
96
+ payeeSecretKey: string;
97
+ network: Network;
98
+ channelFactory: string;
99
+ rate: string;
100
+ assetContract: string;
101
+ manifest: RouteDockManifest;
102
+ commitmentPublicKey: string;
103
+ onSettled?: (txHash: string, totalPaid: string, mode: string, payer: string | null) => Promise<void>;
104
+ onSessionOpen?: (channelId: string, payer: string | null) => Promise<void>;
105
+ onVoucher?: (channelId: string, voucherIndex: number, cumulativeAmount: string, signature: string) => Promise<void>;
106
+ onCallbackError?: (err: unknown, cb: string) => void;
107
+ /**
108
+ * Called when the client connection drops mid-session or the session goes
109
+ * idle, before a clean close. Persist the session as `closing` so the
110
+ * SessionReconciler can settle it with the latest signed voucher.
111
+ */
112
+ onOrphaned?: (channelId: string, info: OrphanedSessionInfo) => Promise<void>;
113
+ /**
114
+ * Flag the session orphaned after this many milliseconds with no voucher
115
+ * activity. Disabled when unset.
116
+ */
117
+ idleTimeoutMs?: number;
118
+ }
119
+ declare function createMppSessionHandler(opts: MppSessionHandlerOptions): RequestHandler;
120
+
121
+ export { type ChannelStore as C, InMemorySeenTxStore as I, type MppSessionHandlerOptions as M, type OrphanedSessionInfo as O, type SeenTxStore as S, type VoucherStoreValue as V, type SettlementRecord as a, type InMemorySeenTxStoreOptions as b, type OrphanReason as c, createMppSessionHandler as d, isVoucherStoreValue as i, paymentIdempotencyKey as p };