@thecryptodonkey/toll-booth 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 (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +205 -0
  3. package/dist/adapters/express.d.ts +51 -0
  4. package/dist/adapters/express.d.ts.map +1 -0
  5. package/dist/adapters/express.js +237 -0
  6. package/dist/adapters/express.js.map +1 -0
  7. package/dist/adapters/proxy-headers.d.ts +7 -0
  8. package/dist/adapters/proxy-headers.d.ts.map +1 -0
  9. package/dist/adapters/proxy-headers.js +58 -0
  10. package/dist/adapters/proxy-headers.js.map +1 -0
  11. package/dist/adapters/web-standard.d.ts +60 -0
  12. package/dist/adapters/web-standard.d.ts.map +1 -0
  13. package/dist/adapters/web-standard.js +252 -0
  14. package/dist/adapters/web-standard.js.map +1 -0
  15. package/dist/backends/alby.d.ts +25 -0
  16. package/dist/backends/alby.d.ts.map +1 -0
  17. package/dist/backends/alby.js +137 -0
  18. package/dist/backends/alby.js.map +1 -0
  19. package/dist/backends/cln.d.ts +22 -0
  20. package/dist/backends/cln.d.ts.map +1 -0
  21. package/dist/backends/cln.js +55 -0
  22. package/dist/backends/cln.js.map +1 -0
  23. package/dist/backends/lnbits.d.ts +23 -0
  24. package/dist/backends/lnbits.d.ts.map +1 -0
  25. package/dist/backends/lnbits.js +58 -0
  26. package/dist/backends/lnbits.js.map +1 -0
  27. package/dist/backends/lnd.d.ts +21 -0
  28. package/dist/backends/lnd.d.ts.map +1 -0
  29. package/dist/backends/lnd.js +59 -0
  30. package/dist/backends/lnd.js.map +1 -0
  31. package/dist/backends/phoenixd.d.ts +19 -0
  32. package/dist/backends/phoenixd.d.ts.map +1 -0
  33. package/dist/backends/phoenixd.js +59 -0
  34. package/dist/backends/phoenixd.js.map +1 -0
  35. package/dist/booth.d.ts +54 -0
  36. package/dist/booth.d.ts.map +1 -0
  37. package/dist/booth.js +200 -0
  38. package/dist/booth.js.map +1 -0
  39. package/dist/core/cashu-redeem.d.ts +9 -0
  40. package/dist/core/cashu-redeem.d.ts.map +1 -0
  41. package/dist/core/cashu-redeem.js +85 -0
  42. package/dist/core/cashu-redeem.js.map +1 -0
  43. package/dist/core/create-invoice.d.ts +19 -0
  44. package/dist/core/create-invoice.d.ts.map +1 -0
  45. package/dist/core/create-invoice.js +66 -0
  46. package/dist/core/create-invoice.js.map +1 -0
  47. package/dist/core/invoice-status.d.ts +24 -0
  48. package/dist/core/invoice-status.d.ts.map +1 -0
  49. package/dist/core/invoice-status.js +74 -0
  50. package/dist/core/invoice-status.js.map +1 -0
  51. package/dist/core/nwc-pay.d.ts +8 -0
  52. package/dist/core/nwc-pay.d.ts.map +1 -0
  53. package/dist/core/nwc-pay.js +23 -0
  54. package/dist/core/nwc-pay.js.map +1 -0
  55. package/dist/core/toll-booth.d.ts +9 -0
  56. package/dist/core/toll-booth.d.ts.map +1 -0
  57. package/dist/core/toll-booth.js +172 -0
  58. package/dist/core/toll-booth.js.map +1 -0
  59. package/dist/core/types.d.ts +101 -0
  60. package/dist/core/types.d.ts.map +1 -0
  61. package/dist/core/types.js +3 -0
  62. package/dist/core/types.js.map +1 -0
  63. package/dist/free-tier.d.ts +14 -0
  64. package/dist/free-tier.d.ts.map +1 -0
  65. package/dist/free-tier.js +41 -0
  66. package/dist/free-tier.js.map +1 -0
  67. package/dist/index.d.ts +38 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +27 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/macaroon.d.ts +39 -0
  72. package/dist/macaroon.d.ts.map +1 -0
  73. package/dist/macaroon.js +111 -0
  74. package/dist/macaroon.js.map +1 -0
  75. package/dist/payment-page.d.ts +18 -0
  76. package/dist/payment-page.d.ts.map +1 -0
  77. package/dist/payment-page.js +391 -0
  78. package/dist/payment-page.js.map +1 -0
  79. package/dist/stats.d.ts +63 -0
  80. package/dist/stats.d.ts.map +1 -0
  81. package/dist/stats.js +75 -0
  82. package/dist/stats.js.map +1 -0
  83. package/dist/storage/interface.d.ts +58 -0
  84. package/dist/storage/interface.d.ts.map +1 -0
  85. package/dist/storage/interface.js +3 -0
  86. package/dist/storage/interface.js.map +1 -0
  87. package/dist/storage/memory.d.ts +3 -0
  88. package/dist/storage/memory.d.ts.map +1 -0
  89. package/dist/storage/memory.js +139 -0
  90. package/dist/storage/memory.js.map +1 -0
  91. package/dist/storage/sqlite.d.ts +6 -0
  92. package/dist/storage/sqlite.d.ts.map +1 -0
  93. package/dist/storage/sqlite.js +264 -0
  94. package/dist/storage/sqlite.js.map +1 -0
  95. package/dist/types.d.ts +198 -0
  96. package/dist/types.d.ts.map +1 -0
  97. package/dist/types.js +8 -0
  98. package/dist/types.js.map +1 -0
  99. package/llms.txt +91 -0
  100. package/package.json +100 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../../src/storage/sqlite.ts"],"names":[],"mappings":"AAAA,wBAAwB;AACxB,OAAO,QAAQ,MAAM,gBAAgB,CAAA;AAGrC,MAAM,gBAAgB,GAAG,MAAM,CAAA;AAM/B,MAAM,UAAU,aAAa,CAAC,MAA4B;IACxD,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,MAAM,EAAE,IAAI,IAAI,UAAU,CAAC,CAAA;IACnD,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAA;IAE/B,EAAE,CAAC,IAAI,CAAC;;;;;;;GAOP,CAAC,CAAA;IAEF,EAAE,CAAC,IAAI,CAAC;;;;;;;;;GASP,CAAC,CAAA;IAEF,EAAE,CAAC,IAAI,CAAC;;;;;;GAMP,CAAC,CAAA;IAEF,EAAE,CAAC,IAAI,CAAC;;;;;;;GAOP,CAAC,CAAA;IAEF,wEAAwE;IACxE,IAAI,CAAC;QACH,EAAE,CAAC,IAAI,CAAC,qDAAqD,CAAC,CAAA;IAChE,CAAC;IAAC,MAAM,CAAC;QACP,iCAAiC;IACnC,CAAC;IAED,8DAA8D;IAC9D,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 *
4
+ * toll-booth is a generic L402 Lightning payment middleware for gating
5
+ * any HTTP API behind a Lightning Network paywall.
6
+ */
7
+ /**
8
+ * A Lightning invoice returned from a backend after creation.
9
+ */
10
+ export interface Invoice {
11
+ /** The BOLT 11 payment request string. */
12
+ bolt11: string;
13
+ /** The payment hash that identifies this invoice. */
14
+ paymentHash: string;
15
+ }
16
+ /**
17
+ * The current status of a Lightning invoice.
18
+ */
19
+ export interface InvoiceStatus {
20
+ /** Whether the invoice has been paid. */
21
+ paid: boolean;
22
+ /** The payment preimage, present only if the invoice has been paid. */
23
+ preimage?: string;
24
+ }
25
+ /**
26
+ * Abstraction over a Lightning node backend.
27
+ *
28
+ * Implement this interface to add support for a new Lightning node type
29
+ * (e.g. phoenixd, LND, CLN).
30
+ */
31
+ export interface LightningBackend {
32
+ /**
33
+ * Create a new Lightning invoice for the given amount.
34
+ * @param amountSats - Amount in satoshis.
35
+ * @param memo - Optional human-readable invoice description.
36
+ * @returns The created invoice.
37
+ */
38
+ createInvoice(amountSats: number, memo?: string): Promise<Invoice>;
39
+ /**
40
+ * Check the payment status of an existing invoice.
41
+ * @param paymentHash - The payment hash of the invoice to check.
42
+ * @returns The current invoice status.
43
+ */
44
+ checkInvoice(paymentHash: string): Promise<InvoiceStatus>;
45
+ }
46
+ /**
47
+ * Pricing table mapping route patterns to amounts in satoshis.
48
+ *
49
+ * Keys are route patterns (e.g. `"/route"`) and values are the
50
+ * required payment amount in satoshis.
51
+ */
52
+ export type PricingTable = Record<string, number>;
53
+ /**
54
+ * A credit tier offering volume discounts.
55
+ *
56
+ * The `amountSats` is what the user pays; `creditSats` is what they receive.
57
+ * When `creditSats > amountSats`, the difference is the volume discount.
58
+ */
59
+ export interface CreditTier {
60
+ /** Amount the user pays in satoshis. */
61
+ amountSats: number;
62
+ /** Credits received in satoshis (may exceed amountSats for volume discounts). */
63
+ creditSats: number;
64
+ /** Human-readable label for this tier. */
65
+ label: string;
66
+ }
67
+ /**
68
+ * Configuration for a toll-booth instance.
69
+ */
70
+ export interface BoothConfig {
71
+ /**
72
+ * The Lightning backend to use for invoice creation and status checks.
73
+ * Optional when using Cashu-only mode (`redeemCashu` provided without a backend).
74
+ */
75
+ backend?: LightningBackend;
76
+ /**
77
+ * Pricing table mapping route patterns to amounts in satoshis.
78
+ * Routes not listed here use `defaultInvoiceAmount` if set.
79
+ */
80
+ pricing: PricingTable;
81
+ /** The upstream URL to proxy authorised requests to. */
82
+ upstream: string;
83
+ /**
84
+ * Optional free-tier configuration. Requests matching these criteria
85
+ * bypass the payment requirement.
86
+ */
87
+ freeTier?: {
88
+ requestsPerDay: number;
89
+ };
90
+ /**
91
+ * Default invoice amount in satoshis. Controls how many credits are
92
+ * minted per invoice, and is also used as the route cost when
93
+ * `strictPricing` challenges an unpriced route.
94
+ */
95
+ defaultInvoiceAmount?: number;
96
+ /**
97
+ * When true, unpriced routes are challenged (402) using `defaultInvoiceAmount`
98
+ * as the cost, instead of being passed through for free. This prevents
99
+ * mount-prefix mismatches or typos from silently bypassing billing.
100
+ */
101
+ strictPricing?: boolean;
102
+ /**
103
+ * Root key used for macaroon generation and verification, as a
104
+ * hex-encoded 32-byte string.
105
+ * If omitted, a random key is generated at startup (not recommended for
106
+ * production use, as tokens will be invalidated on restart).
107
+ */
108
+ rootKey?: string;
109
+ /**
110
+ * Path to the SQLite database file used for invoice persistence.
111
+ * Defaults to `./toll-booth.db` in the current working directory.
112
+ */
113
+ dbPath?: string;
114
+ /**
115
+ * Trust reverse-proxy headers (`X-Forwarded-For` / `X-Real-IP`) for client IP.
116
+ * Keep disabled unless a trusted proxy overwrites these headers.
117
+ */
118
+ trustProxy?: boolean;
119
+ /**
120
+ * Custom callback to resolve the client IP for adapters that cannot infer
121
+ * it directly (for example Deno, Bun, or Cloudflare Workers).
122
+ * The callback receives the adapter-specific request object.
123
+ */
124
+ getClientIp?: (request: unknown) => string;
125
+ /**
126
+ * Extra headers to include on every response (e.g. `{ 'X-Coverage': 'GB' }`).
127
+ * Replaces the previously hardcoded `X-Coverage: GB` header.
128
+ */
129
+ responseHeaders?: Record<string, string>;
130
+ /**
131
+ * Credit tiers with optional volume discounts.
132
+ * Used by the payment page tier selector and the `/create-invoice` endpoint.
133
+ */
134
+ creditTiers?: CreditTier[];
135
+ /**
136
+ * Pay a Lightning invoice via Nostr Wallet Connect.
137
+ * Accepts NWC URI + bolt11, returns the payment preimage.
138
+ * When provided, the payment page shows an NWC option.
139
+ */
140
+ nwcPayInvoice?: (nwcUri: string, bolt11: string) => Promise<string>;
141
+ /**
142
+ * Redeem a Cashu token as payment.
143
+ * Returns the credited amount in satoshis. Implementations should be
144
+ * idempotent for the same `paymentHash` if you rely on retry/recovery.
145
+ * When provided, the payment page shows a Cashu option.
146
+ */
147
+ redeemCashu?: (token: string, paymentHash: string) => Promise<number>;
148
+ /**
149
+ * Timeout in milliseconds for upstream proxy requests.
150
+ * Defaults to 30000 (30 seconds).
151
+ */
152
+ upstreamTimeout?: number;
153
+ /**
154
+ * Maximum age of stored invoices in milliseconds. Invoices older than
155
+ * this are periodically pruned. Set to 0 to disable pruning.
156
+ * Default: 86400000 (24 hours).
157
+ */
158
+ invoiceMaxAgeMs?: number;
159
+ }
160
+ /**
161
+ * A record of a payment event stored for audit and idempotency purposes.
162
+ */
163
+ export interface PaymentEvent {
164
+ timestamp: string;
165
+ paymentHash: string;
166
+ amountSats: number;
167
+ }
168
+ /**
169
+ * A record of an incoming HTTP request, used for analytics and
170
+ * free-tier tracking.
171
+ */
172
+ export interface RequestEvent {
173
+ timestamp: string;
174
+ endpoint: string;
175
+ satsDeducted: number;
176
+ remainingBalance: number;
177
+ latencyMs: number;
178
+ authenticated: boolean;
179
+ clientIp: string;
180
+ }
181
+ /**
182
+ * A record of a 402 challenge issued to a client.
183
+ */
184
+ export interface ChallengeEvent {
185
+ timestamp: string;
186
+ endpoint: string;
187
+ amountSats: number;
188
+ clientIp: string;
189
+ }
190
+ /**
191
+ * Optional event handlers for observing toll-booth lifecycle events.
192
+ */
193
+ export type EventHandler = {
194
+ onPayment?: (event: PaymentEvent) => void;
195
+ onRequest?: (event: RequestEvent) => void;
196
+ onChallenge?: (event: ChallengeEvent) => void;
197
+ };
198
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,OAAO;IACtB,0CAA0C;IAC1C,MAAM,EAAE,MAAM,CAAA;IACd,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAA;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,yCAAyC;IACzC,IAAI,EAAE,OAAO,CAAA;IACb,uEAAuE;IACvE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;;OAKG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAElE;;;;OAIG;IACH,YAAY,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAAA;CAC1D;AAED;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;AAEjD;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,wCAAwC;IACxC,UAAU,EAAE,MAAM,CAAA;IAClB,iFAAiF;IACjF,UAAU,EAAE,MAAM,CAAA;IAClB,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B;;;OAGG;IACH,OAAO,CAAC,EAAE,gBAAgB,CAAA;IAE1B;;;OAGG;IACH,OAAO,EAAE,YAAY,CAAA;IAErB,wDAAwD;IACxD,QAAQ,EAAE,MAAM,CAAA;IAEhB;;;OAGG;IACH,Q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OAAO,CAAA;IACtB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IACzC,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,CAAA;IACzC,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAA;CAC9C,CAAA"}
package/dist/types.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Core type definitions for toll-booth.
3
+ *
4
+ * toll-booth is a generic L402 Lightning payment middleware for gating
5
+ * any HTTP API behind a Lightning Network paywall.
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
package/llms.txt ADDED
@@ -0,0 +1,91 @@
1
+ # toll-booth
2
+
3
+ > APIs gate access through accounts, API keys, and credit cards — mechanisms that assume a human on the other end. Machines and AI agents can't sign up for accounts. toll-booth is L402 payment middleware that lets any HTTP API accept instant payments — Lightning, Cashu ecash, or Nostr Wallet Connect — with no accounts, no API keys, and no PII exchanged.
4
+
5
+ toll-booth embeds in your existing Node.js application as middleware (not a separate proxy). It supports Express 5, Deno, Bun, and Cloudflare Workers. Five Lightning backends are supported (Phoenixd, LND, Core Lightning, LNbits, Alby), or you can run in Cashu-only mode with no Lightning node at all — ideal for serverless and edge deployments.
6
+
7
+ The closest alternative is Aperture (Lightning Labs), a Go reverse proxy that requires LND. toll-booth is TypeScript middleware that works with any Lightning backend, runs serverless, and supports Cashu ecash payments.
8
+
9
+ ## Getting Started
10
+
11
+ ```bash
12
+ npm install @thecryptodonkey/toll-booth
13
+ ```
14
+
15
+ ```typescript
16
+ import express from 'express'
17
+ import { Booth } from '@thecryptodonkey/toll-booth'
18
+ import { phoenixdBackend } from '@thecryptodonkey/toll-booth/backends/phoenixd'
19
+
20
+ const app = express()
21
+ app.use(express.json())
22
+
23
+ const booth = new Booth({
24
+ adapter: 'express',
25
+ backend: phoenixdBackend({
26
+ url: 'http://localhost:9740',
27
+ password: process.env.PHOENIXD_PASSWORD!,
28
+ }),
29
+ pricing: { '/api': 10 },
30
+ upstream: 'http://localhost:8080',
31
+ })
32
+
33
+ app.get('/invoice-status/:paymentHash', booth.invoiceStatusHandler as any)
34
+ app.post('/create-invoice', booth.createInvoiceHandler as any)
35
+ app.use('/', booth.middleware as any)
36
+ app.listen(3000)
37
+ ```
38
+
39
+ For Cashu-only mode (no Lightning node required):
40
+
41
+ ```typescript
42
+ const booth = new Booth({
43
+ adapter: 'web-standard',
44
+ redeemCashu: async (token, paymentHash) => {
45
+ // Verify and redeem the ecash token with your Cashu mint
46
+ return amountRedeemed
47
+ },
48
+ pricing: { '/api': 5 },
49
+ upstream: 'http://localhost:8080',
50
+ })
51
+ ```
52
+
53
+ ## Key Concepts
54
+
55
+ - **Booth** — the main facade class. Instantiate with a config object, then wire `.middleware`, `.invoiceStatusHandler`, and `.createInvoiceHandler` into your router.
56
+ - **LightningBackend** — interface with two methods: `createInvoice(amountSats, memo?)` and `checkInvoice(paymentHash)`. Five implementations provided; implement your own for custom backends.
57
+ - **L402 flow** — client requests a priced endpoint, receives HTTP 402 with a Lightning invoice and macaroon, pays the invoice, then sends `Authorization: L402 <macaroon>:<preimage>` to access the API.
58
+ - **Payment methods** — Lightning (via any backend), Cashu ecash tokens (via `redeemCashu` callback), or Nostr Wallet Connect (via `nwcPayInvoice` callback). Methods can be combined.
59
+ - **Credit system** — payments grant a credit balance. Each request deducts from the balance. Volume discount tiers available via `creditTiers`.
60
+ - **Free tier** — optional per-IP daily request allowance (`freeTier: { requestsPerDay: N }`). Requests under the limit bypass payment.
61
+ - **Adapters** — `'express'` for Express 4/5, `'web-standard'` for Deno, Bun, and Cloudflare Workers.
62
+
63
+ ## API Surface
64
+
65
+ Main entry point: `import { Booth } from '@thecryptodonkey/toll-booth'`
66
+
67
+ Subpath imports for tree-shaking:
68
+ - `@thecryptodonkey/toll-booth/backends/phoenixd` — Phoenixd backend
69
+ - `@thecryptodonkey/toll-booth/backends/lnd` — LND backend
70
+ - `@thecryptodonkey/toll-booth/backends/cln` — Core Lightning backend
71
+ - `@thecryptodonkey/toll-booth/backends/lnbits` — LNbits backend
72
+ - `@thecryptodonkey/toll-booth/backends/alby` — Alby / NWC backend
73
+ - `@thecryptodonkey/toll-booth/storage/sqlite` — SQLite storage (default)
74
+ - `@thecryptodonkey/toll-booth/storage/memory` — in-memory storage (testing)
75
+ - `@thecryptodonkey/toll-booth/adapters/express` — Express middleware factory
76
+ - `@thecryptodonkey/toll-booth/adapters/web-standard` — Web Standard handler factory
77
+
78
+ Booth instance properties:
79
+ - `.middleware` — main request handler (checks auth, proxies upstream)
80
+ - `.invoiceStatusHandler` — GET /invoice-status/:paymentHash
81
+ - `.createInvoiceHandler` — POST /create-invoice
82
+ - `.nwcPayHandler` — POST /nwc-pay (when nwcPayInvoice configured)
83
+ - `.cashuRedeemHandler` — POST /cashu-redeem (when redeemCashu configured)
84
+ - `.resetFreeTier()` — reset daily free-tier counters
85
+ - `.close()` — clean up timers and database connections
86
+
87
+ ## Optional
88
+
89
+ - [Vision: The Vending Machine Web](docs/vision.md) — why L402 and permissionless machine-to-machine payments matter
90
+ - [Example: Valhalla routing proxy](examples/valhalla-proxy/) — complete Docker Compose deployment gating a routing engine behind Lightning payments
91
+ - [Production checklist](README.md#production-checklist) — deployment best practices
package/package.json ADDED
@@ -0,0 +1,100 @@
1
+ {
2
+ "name": "@thecryptodonkey/toll-booth",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "L402 Lightning payment middleware for any Node.js framework — Express, Deno, Bun",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "l402", "lightning", "paywall", "micropayments", "middleware",
9
+ "cashu", "ecash", "nostr", "nwc", "bitcoin",
10
+ "express", "deno", "bun", "cloudflare-workers", "serverless",
11
+ "macaroon", "http-402", "api-monetisation", "payment-gateway"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/TheCryptoDonkey/toll-booth.git"
16
+ },
17
+ "homepage": "https://github.com/TheCryptoDonkey/toll-booth#readme",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ },
23
+ "./backends/phoenixd": {
24
+ "import": "./dist/backends/phoenixd.js",
25
+ "types": "./dist/backends/phoenixd.d.ts"
26
+ },
27
+ "./backends/lnd": {
28
+ "import": "./dist/backends/lnd.js",
29
+ "types": "./dist/backends/lnd.d.ts"
30
+ },
31
+ "./backends/cln": {
32
+ "import": "./dist/backends/cln.js",
33
+ "types": "./dist/backends/cln.d.ts"
34
+ },
35
+ "./backends/alby": {
36
+ "import": "./dist/backends/alby.js",
37
+ "types": "./dist/backends/alby.d.ts"
38
+ },
39
+ "./backends/lnbits": {
40
+ "import": "./dist/backends/lnbits.js",
41
+ "types": "./dist/backends/lnbits.d.ts"
42
+ },
43
+ "./storage/sqlite": {
44
+ "import": "./dist/storage/sqlite.js",
45
+ "types": "./dist/storage/sqlite.d.ts"
46
+ },
47
+ "./storage/memory": {
48
+ "import": "./dist/storage/memory.js",
49
+ "types": "./dist/storage/memory.d.ts"
50
+ },
51
+ "./adapters/express": {
52
+ "import": "./dist/adapters/express.js",
53
+ "types": "./dist/adapters/express.d.ts"
54
+ },
55
+ "./adapters/web-standard": {
56
+ "import": "./dist/adapters/web-standard.js",
57
+ "types": "./dist/adapters/web-standard.d.ts"
58
+ }
59
+ },
60
+ "files": [
61
+ "dist",
62
+ "llms.txt"
63
+ ],
64
+ "scripts": {
65
+ "build": "rm -rf dist && tsc",
66
+ "test": "vitest run",
67
+ "test:integration": "bash scripts/test-integration.sh",
68
+ "test:integration:ln": "bash scripts/test-integration.sh --ln-only",
69
+ "test:integration:cashu": "bash scripts/test-integration.sh --cashu-only",
70
+ "typecheck": "tsc --noEmit",
71
+ "prepublishOnly": "npm run build && npm test"
72
+ },
73
+ "dependencies": {
74
+ "better-sqlite3": "^11.8.0",
75
+ "macaroon": "^3.0.4",
76
+ "qrcode": "^1.5.4"
77
+ },
78
+ "peerDependencies": {
79
+ "express": "^4.0.0 || ^5.0.0"
80
+ },
81
+ "peerDependenciesMeta": {
82
+ "express": {
83
+ "optional": true
84
+ }
85
+ },
86
+ "devDependencies": {
87
+ "@cashu/cashu-ts": "^3.5.0",
88
+ "@types/better-sqlite3": "^7.6.0",
89
+ "@types/express": "^5.0.6",
90
+ "@types/qrcode": "^1.5.6",
91
+ "@types/ws": "^8.18.1",
92
+ "express": "^5.2.1",
93
+ "tsx": "^4.21.0",
94
+ "typescript": "^5.7.0",
95
+ "vitest": "^3.0.0"
96
+ },
97
+ "optionalDependencies": {
98
+ "ws": "^8.19.0"
99
+ }
100
+ }