@puga-labs/x402-mantle-sdk 0.3.8 → 0.3.9
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.
- package/dist/chunk-CKBTOS7X.js +86 -0
- package/dist/chunk-IEJB5W26.js +113 -0
- package/dist/chunk-IXIFGPJ2.js +250 -0
- package/dist/chunk-T4DIHTBP.js +83 -0
- package/dist/express-DqyVgO5n.d.cts +68 -0
- package/dist/express-DxxlKmmF.d.ts +68 -0
- package/dist/index.cjs +32 -9
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/nextjs-DGcN_MGa.d.ts +89 -0
- package/dist/nextjs-EoISXVEo.d.cts +89 -0
- package/dist/server-express.cjs +32 -9
- package/dist/server-express.d.cts +2 -2
- package/dist/server-express.d.ts +2 -2
- package/dist/server-express.js +2 -2
- package/dist/server-nextjs.cjs +33 -10
- package/dist/server-nextjs.d.cts +2 -2
- package/dist/server-nextjs.d.ts +2 -2
- package/dist/server-nextjs.js +2 -2
- package/dist/server-web.cjs +33 -10
- package/dist/server-web.d.cts +2 -2
- package/dist/server-web.d.ts +2 -2
- package/dist/server-web.js +2 -2
- package/dist/server.cjs +58 -11
- package/dist/server.d.cts +6 -6
- package/dist/server.d.ts +6 -6
- package/dist/server.js +4 -4
- package/dist/types-B87bD2yo.d.cts +102 -0
- package/dist/types-DvKDSdL6.d.ts +106 -0
- package/dist/types-X6DeBEgb.d.cts +106 -0
- package/dist/types-vicT7qsY.d.ts +102 -0
- package/dist/web-standards-BJcdcxD6.d.ts +77 -0
- package/dist/web-standards-C6JwCDmd.d.cts +77 -0
- package/package.json +1 -1
package/dist/server.cjs
CHANGED
|
@@ -240,12 +240,13 @@ async function checkPayment(input) {
|
|
|
240
240
|
isValid: false
|
|
241
241
|
};
|
|
242
242
|
}
|
|
243
|
-
|
|
243
|
+
let baseLogEntry = null;
|
|
244
|
+
if (onPaymentSettled || telemetry) {
|
|
244
245
|
try {
|
|
245
246
|
const headerObj = decodePaymentHeader(paymentHeader);
|
|
246
247
|
const { authorization } = headerObj.payload;
|
|
247
248
|
const assetConfig = getDefaultAssetForNetwork(network);
|
|
248
|
-
|
|
249
|
+
baseLogEntry = {
|
|
249
250
|
id: authorization.nonce,
|
|
250
251
|
from: authorization.from,
|
|
251
252
|
to: authorization.to,
|
|
@@ -257,12 +258,8 @@ async function checkPayment(input) {
|
|
|
257
258
|
facilitatorUrl,
|
|
258
259
|
paymentRequirements
|
|
259
260
|
};
|
|
260
|
-
onPaymentSettled
|
|
261
|
-
|
|
262
|
-
const event = createTelemetryEvent(logEntry, telemetry);
|
|
263
|
-
sendTelemetry(event, telemetry.endpoint).catch(
|
|
264
|
-
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
265
|
-
);
|
|
261
|
+
if (onPaymentSettled) {
|
|
262
|
+
onPaymentSettled(baseLogEntry);
|
|
266
263
|
}
|
|
267
264
|
} catch (err) {
|
|
268
265
|
console.error(
|
|
@@ -271,11 +268,25 @@ async function checkPayment(input) {
|
|
|
271
268
|
);
|
|
272
269
|
}
|
|
273
270
|
}
|
|
271
|
+
const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
|
|
272
|
+
try {
|
|
273
|
+
const event = createTelemetryEvent(baseLogEntry, telemetry);
|
|
274
|
+
event.responseStatus = responseStatus;
|
|
275
|
+
event.errorMessage = error;
|
|
276
|
+
event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
|
|
277
|
+
sendTelemetry(event, telemetry.endpoint).catch(
|
|
278
|
+
(err) => console.error("[x402-telemetry] Async send failed:", err)
|
|
279
|
+
);
|
|
280
|
+
} catch (err) {
|
|
281
|
+
console.error("[x402-telemetry] Error creating telemetry event:", err);
|
|
282
|
+
}
|
|
283
|
+
} : void 0;
|
|
274
284
|
return {
|
|
275
285
|
status: "verified",
|
|
276
286
|
statusCode: 200,
|
|
277
287
|
responseBody: null,
|
|
278
|
-
isValid: true
|
|
288
|
+
isValid: true,
|
|
289
|
+
sendTelemetryAfterResponse
|
|
279
290
|
};
|
|
280
291
|
} catch (err) {
|
|
281
292
|
console.error(
|
|
@@ -351,6 +362,18 @@ function createPaymentMiddleware(config) {
|
|
|
351
362
|
res.status(result.statusCode).json(result.responseBody);
|
|
352
363
|
return;
|
|
353
364
|
}
|
|
365
|
+
if (result.sendTelemetryAfterResponse) {
|
|
366
|
+
res.on("finish", () => {
|
|
367
|
+
const statusCode = res.statusCode;
|
|
368
|
+
const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
|
|
369
|
+
result.sendTelemetryAfterResponse(statusCode, errorMessage);
|
|
370
|
+
});
|
|
371
|
+
res.on("close", () => {
|
|
372
|
+
if (!res.writableEnded) {
|
|
373
|
+
result.sendTelemetryAfterResponse(500, "Response closed without finishing");
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|
|
354
377
|
next();
|
|
355
378
|
};
|
|
356
379
|
}
|
|
@@ -428,7 +451,19 @@ function mantlePaywall2(opts) {
|
|
|
428
451
|
status: result.statusCode
|
|
429
452
|
});
|
|
430
453
|
}
|
|
431
|
-
|
|
454
|
+
try {
|
|
455
|
+
const response = await handler(req);
|
|
456
|
+
if (result.sendTelemetryAfterResponse) {
|
|
457
|
+
result.sendTelemetryAfterResponse(response.status);
|
|
458
|
+
}
|
|
459
|
+
return response;
|
|
460
|
+
} catch (err) {
|
|
461
|
+
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
|
462
|
+
if (result.sendTelemetryAfterResponse) {
|
|
463
|
+
result.sendTelemetryAfterResponse(500, errorMessage);
|
|
464
|
+
}
|
|
465
|
+
throw err;
|
|
466
|
+
}
|
|
432
467
|
};
|
|
433
468
|
};
|
|
434
469
|
}
|
|
@@ -486,7 +521,19 @@ function mantlePaywall3(opts) {
|
|
|
486
521
|
}
|
|
487
522
|
});
|
|
488
523
|
}
|
|
489
|
-
|
|
524
|
+
try {
|
|
525
|
+
const response = await handler(request);
|
|
526
|
+
if (result.sendTelemetryAfterResponse) {
|
|
527
|
+
result.sendTelemetryAfterResponse(response.status);
|
|
528
|
+
}
|
|
529
|
+
return response;
|
|
530
|
+
} catch (err) {
|
|
531
|
+
const errorMessage = err instanceof Error ? err.message : "Unknown error";
|
|
532
|
+
if (result.sendTelemetryAfterResponse) {
|
|
533
|
+
result.sendTelemetryAfterResponse(500, errorMessage);
|
|
534
|
+
}
|
|
535
|
+
throw err;
|
|
536
|
+
}
|
|
490
537
|
};
|
|
491
538
|
};
|
|
492
539
|
}
|
package/dist/server.d.cts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-
|
|
2
|
-
export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-
|
|
1
|
+
import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-B87bD2yo.cjs';
|
|
2
|
+
export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-B87bD2yo.cjs';
|
|
3
3
|
import { c as PaymentHeaderObject } from './types-BFUqKBBO.cjs';
|
|
4
|
-
export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-
|
|
5
|
-
export { n as nextjs } from './nextjs-
|
|
6
|
-
export { w as web } from './web-standards-
|
|
7
|
-
import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-
|
|
4
|
+
export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-DqyVgO5n.cjs';
|
|
5
|
+
export { n as nextjs } from './nextjs-EoISXVEo.cjs';
|
|
6
|
+
export { w as web } from './web-standards-C6JwCDmd.cjs';
|
|
7
|
+
import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-X6DeBEgb.cjs';
|
|
8
8
|
import 'express';
|
|
9
9
|
import 'next/server';
|
|
10
10
|
|
package/dist/server.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-
|
|
2
|
-
export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-
|
|
1
|
+
import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-vicT7qsY.js';
|
|
2
|
+
export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-vicT7qsY.js';
|
|
3
3
|
import { c as PaymentHeaderObject } from './types-BFUqKBBO.js';
|
|
4
|
-
export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-
|
|
5
|
-
export { n as nextjs } from './nextjs-
|
|
6
|
-
export { w as web } from './web-standards-
|
|
7
|
-
import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-
|
|
4
|
+
export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-DxxlKmmF.js';
|
|
5
|
+
export { n as nextjs } from './nextjs-DGcN_MGa.js';
|
|
6
|
+
export { w as web } from './web-standards-BJcdcxD6.js';
|
|
7
|
+
import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-DvKDSdL6.js';
|
|
8
8
|
import 'express';
|
|
9
9
|
import 'next/server';
|
|
10
10
|
|
package/dist/server.js
CHANGED
|
@@ -3,13 +3,13 @@ import {
|
|
|
3
3
|
createPaymentMiddleware,
|
|
4
4
|
express_exports,
|
|
5
5
|
mantlePaywall
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-IEJB5W26.js";
|
|
7
7
|
import {
|
|
8
8
|
nextjs_exports
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-CKBTOS7X.js";
|
|
10
10
|
import {
|
|
11
11
|
web_standards_exports
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-T4DIHTBP.js";
|
|
13
13
|
import {
|
|
14
14
|
DEFAULT_TELEMETRY_ENDPOINT,
|
|
15
15
|
buildRouteKey,
|
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
decodePaymentHeader,
|
|
19
19
|
sendTelemetry,
|
|
20
20
|
validateAddress
|
|
21
|
-
} from "./chunk-
|
|
21
|
+
} from "./chunk-IXIFGPJ2.js";
|
|
22
22
|
import "./chunk-HEZZ74SI.js";
|
|
23
23
|
export {
|
|
24
24
|
DEFAULT_TELEMETRY_ENDPOINT,
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { N as NetworkId, P as PaymentRequirements } from './types-BFUqKBBO.cjs';
|
|
2
|
+
|
|
3
|
+
/** Unique key for a protected route, e.g. "GET /api/protected". */
|
|
4
|
+
type RouteKey = string;
|
|
5
|
+
/** Pricing config for a single route. */
|
|
6
|
+
interface RoutePricingConfig {
|
|
7
|
+
/** Price in USD cents, e.g. 1 => $0.01. */
|
|
8
|
+
priceUsdCents: number;
|
|
9
|
+
/** Network identifier (e.g. "mantle-mainnet"). */
|
|
10
|
+
network: NetworkId;
|
|
11
|
+
}
|
|
12
|
+
/** Map of route keys to pricing config. */
|
|
13
|
+
type RoutesConfig = Record<RouteKey, RoutePricingConfig>;
|
|
14
|
+
/** Log entry for a successfully settled payment. */
|
|
15
|
+
interface PaymentLogEntry {
|
|
16
|
+
id: string;
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
valueAtomic: string;
|
|
20
|
+
network: NetworkId;
|
|
21
|
+
asset: string;
|
|
22
|
+
route?: RouteKey;
|
|
23
|
+
txHash?: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
facilitatorUrl?: string;
|
|
26
|
+
paymentRequirements?: PaymentRequirements;
|
|
27
|
+
}
|
|
28
|
+
/** Config for optional telemetry (billing/analytics). */
|
|
29
|
+
interface TelemetryConfig {
|
|
30
|
+
/** Project key from nosubs.ai dashboard. */
|
|
31
|
+
projectKey: string;
|
|
32
|
+
/**
|
|
33
|
+
* Telemetry endpoint URL.
|
|
34
|
+
* If not specified, uses DEFAULT_TELEMETRY_ENDPOINT (see server/constants.ts).
|
|
35
|
+
*/
|
|
36
|
+
endpoint?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Minimal config for mantlePaywall() - simplified API for single-route protection.
|
|
40
|
+
* This is the "sweet path" for Mantle mainnet + USDC with sensible defaults.
|
|
41
|
+
*/
|
|
42
|
+
interface MinimalPaywallOptions {
|
|
43
|
+
/** Price in USD (e.g. 0.01 for 1 cent). */
|
|
44
|
+
priceUsd: number;
|
|
45
|
+
/** Recipient address (developer wallet). Validated at runtime. */
|
|
46
|
+
payTo: string;
|
|
47
|
+
/** Optional facilitator URL (defaults to localhost:8080 or NEXT_PUBLIC_FACILITATOR_URL). */
|
|
48
|
+
facilitatorUrl?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Optional API key for hosted facilitator billing.
|
|
51
|
+
* Required when using hosted facilitator (e.g., https://facilitator.x402mantlesdk.xyz).
|
|
52
|
+
* Not needed for self-hosted facilitator.
|
|
53
|
+
*/
|
|
54
|
+
apiKey?: string;
|
|
55
|
+
/** Optional telemetry config. */
|
|
56
|
+
telemetry?: TelemetryConfig;
|
|
57
|
+
/** Optional payment settled hook. */
|
|
58
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
59
|
+
}
|
|
60
|
+
/** Input for checkPayment function. */
|
|
61
|
+
interface PaymentCheckInput {
|
|
62
|
+
/** X-PAYMENT header value (base64 encoded), or null if not present. */
|
|
63
|
+
paymentHeader: string | null;
|
|
64
|
+
/** Payment requirements for this route. */
|
|
65
|
+
paymentRequirements: PaymentRequirements;
|
|
66
|
+
/** Facilitator URL for verification. */
|
|
67
|
+
facilitatorUrl: string;
|
|
68
|
+
/** Optional API key for hosted facilitator billing. */
|
|
69
|
+
apiKey?: string;
|
|
70
|
+
/** Route key for logging, e.g. "POST /api/generate-image". */
|
|
71
|
+
routeKey?: RouteKey;
|
|
72
|
+
/** Network ID for this payment. */
|
|
73
|
+
network: NetworkId;
|
|
74
|
+
/** Asset address (ERC-20 token). */
|
|
75
|
+
asset: string;
|
|
76
|
+
/** Telemetry config (optional). */
|
|
77
|
+
telemetry?: TelemetryConfig;
|
|
78
|
+
/** Payment settled callback (optional). */
|
|
79
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
80
|
+
}
|
|
81
|
+
/** Result from checkPayment function. */
|
|
82
|
+
interface PaymentCheckResult {
|
|
83
|
+
/** Status of the payment check. */
|
|
84
|
+
status: "require_payment" | "verification_error" | "invalid_payment" | "verified";
|
|
85
|
+
/** HTTP status code to return. */
|
|
86
|
+
statusCode: 402 | 500 | 200;
|
|
87
|
+
/** Response body to return (null if verified). */
|
|
88
|
+
responseBody: any | null;
|
|
89
|
+
/** Whether payment is valid (only true if status is "verified"). */
|
|
90
|
+
isValid: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Callback to send telemetry after handler execution.
|
|
93
|
+
* Only present when payment is verified and telemetry is enabled.
|
|
94
|
+
* Adapters should call this after the protected route handler completes.
|
|
95
|
+
*
|
|
96
|
+
* @param responseStatus - HTTP status code from handler (200, 500, etc.)
|
|
97
|
+
* @param error - Optional error message if handler threw
|
|
98
|
+
*/
|
|
99
|
+
sendTelemetryAfterResponse?: (responseStatus: number, error?: string) => void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type { MinimalPaywallOptions as M, PaymentLogEntry as P, RouteKey as R, TelemetryConfig as T, RoutePricingConfig as a, RoutesConfig as b, PaymentCheckInput as c, PaymentCheckResult as d };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { N as NetworkId, P as PaymentRequirements } from './types-BFUqKBBO.js';
|
|
3
|
+
|
|
4
|
+
/** Unique key for a protected route, e.g. "GET /api/protected". */
|
|
5
|
+
type RouteKey = string;
|
|
6
|
+
/** Pricing config for a single route. */
|
|
7
|
+
interface RoutePricingConfig {
|
|
8
|
+
/** Price in USD cents, e.g. 1 => $0.01. */
|
|
9
|
+
priceUsdCents: number;
|
|
10
|
+
/** Network identifier (e.g. "mantle-mainnet"). */
|
|
11
|
+
network: NetworkId;
|
|
12
|
+
}
|
|
13
|
+
/** Map of route keys to pricing config. */
|
|
14
|
+
type RoutesConfig = Record<RouteKey, RoutePricingConfig>;
|
|
15
|
+
/** Log entry for a successfully settled payment. */
|
|
16
|
+
interface PaymentLogEntry {
|
|
17
|
+
id: string;
|
|
18
|
+
from: string;
|
|
19
|
+
to: string;
|
|
20
|
+
valueAtomic: string;
|
|
21
|
+
network: NetworkId;
|
|
22
|
+
asset: string;
|
|
23
|
+
route?: RouteKey;
|
|
24
|
+
txHash?: string;
|
|
25
|
+
timestamp: number;
|
|
26
|
+
facilitatorUrl?: string;
|
|
27
|
+
paymentRequirements?: PaymentRequirements;
|
|
28
|
+
}
|
|
29
|
+
/** Config for optional telemetry (billing/analytics). */
|
|
30
|
+
interface TelemetryConfig {
|
|
31
|
+
/** Project key from nosubs.ai dashboard. */
|
|
32
|
+
projectKey: string;
|
|
33
|
+
/**
|
|
34
|
+
* Telemetry endpoint URL.
|
|
35
|
+
* If not specified, uses DEFAULT_TELEMETRY_ENDPOINT (see server/constants.ts).
|
|
36
|
+
* If both are undefined, telemetry is not sent.
|
|
37
|
+
*/
|
|
38
|
+
endpoint?: string;
|
|
39
|
+
}
|
|
40
|
+
/** Telemetry event payload for payment_verified. */
|
|
41
|
+
interface TelemetryEvent {
|
|
42
|
+
event: "payment_verified";
|
|
43
|
+
ts: number;
|
|
44
|
+
projectKey: string;
|
|
45
|
+
network: string;
|
|
46
|
+
buyer: string;
|
|
47
|
+
payTo: string;
|
|
48
|
+
amountAtomic: string;
|
|
49
|
+
asset: string;
|
|
50
|
+
decimals: number;
|
|
51
|
+
nonce: string;
|
|
52
|
+
route: string;
|
|
53
|
+
facilitatorType: "hosted" | "self-hosted";
|
|
54
|
+
facilitatorUrl?: string;
|
|
55
|
+
facilitatorAddress?: string;
|
|
56
|
+
txHash?: string;
|
|
57
|
+
priceUsd?: string;
|
|
58
|
+
responseStatus?: number;
|
|
59
|
+
errorMessage?: string;
|
|
60
|
+
serviceDelivered?: boolean;
|
|
61
|
+
}
|
|
62
|
+
/** Config for createPaymentMiddleware. */
|
|
63
|
+
interface PaymentMiddlewareConfig {
|
|
64
|
+
/** Base URL of facilitator, e.g. https://facilitator.nosubs.ai */
|
|
65
|
+
facilitatorUrl: string;
|
|
66
|
+
/** Recipient address (developer). Validated at runtime. */
|
|
67
|
+
receiverAddress: string;
|
|
68
|
+
/** Map of protected routes and their pricing. */
|
|
69
|
+
routes: RoutesConfig;
|
|
70
|
+
/**
|
|
71
|
+
* Optional hook called whenever a payment is successfully settled.
|
|
72
|
+
* You can use this to push logs into your DB / analytics pipeline.
|
|
73
|
+
*/
|
|
74
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
75
|
+
/** Optional: Send usage telemetry for billing/analytics. */
|
|
76
|
+
telemetry?: TelemetryConfig;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Minimal config for mantlePaywall() - simplified API for single-route protection.
|
|
80
|
+
* This is the "sweet path" for Mantle mainnet + USDC with sensible defaults.
|
|
81
|
+
*/
|
|
82
|
+
interface MinimalPaywallOptions {
|
|
83
|
+
/** Price in USD (e.g. 0.01 for 1 cent). */
|
|
84
|
+
priceUsd: number;
|
|
85
|
+
/** Recipient address (developer wallet). Validated at runtime. */
|
|
86
|
+
payTo: string;
|
|
87
|
+
/** Optional facilitator URL (defaults to localhost:8080 or NEXT_PUBLIC_FACILITATOR_URL). */
|
|
88
|
+
facilitatorUrl?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Optional API key for hosted facilitator billing.
|
|
91
|
+
* Required when using hosted facilitator (e.g., https://facilitator.x402mantlesdk.xyz).
|
|
92
|
+
* Not needed for self-hosted facilitator.
|
|
93
|
+
*/
|
|
94
|
+
apiKey?: string;
|
|
95
|
+
/** Optional telemetry config. */
|
|
96
|
+
telemetry?: TelemetryConfig;
|
|
97
|
+
/** Optional payment settled hook. */
|
|
98
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Express middleware function type for Mantle paywall.
|
|
102
|
+
* Async function that returns Promise<void>.
|
|
103
|
+
*/
|
|
104
|
+
type MantleMiddleware = (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
105
|
+
|
|
106
|
+
export type { MantleMiddleware as M, PaymentLogEntry as P, RouteKey as R, TelemetryConfig as T, RoutePricingConfig as a, RoutesConfig as b, TelemetryEvent as c, PaymentMiddlewareConfig as d, MinimalPaywallOptions as e };
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { N as NetworkId, P as PaymentRequirements } from './types-BFUqKBBO.cjs';
|
|
3
|
+
|
|
4
|
+
/** Unique key for a protected route, e.g. "GET /api/protected". */
|
|
5
|
+
type RouteKey = string;
|
|
6
|
+
/** Pricing config for a single route. */
|
|
7
|
+
interface RoutePricingConfig {
|
|
8
|
+
/** Price in USD cents, e.g. 1 => $0.01. */
|
|
9
|
+
priceUsdCents: number;
|
|
10
|
+
/** Network identifier (e.g. "mantle-mainnet"). */
|
|
11
|
+
network: NetworkId;
|
|
12
|
+
}
|
|
13
|
+
/** Map of route keys to pricing config. */
|
|
14
|
+
type RoutesConfig = Record<RouteKey, RoutePricingConfig>;
|
|
15
|
+
/** Log entry for a successfully settled payment. */
|
|
16
|
+
interface PaymentLogEntry {
|
|
17
|
+
id: string;
|
|
18
|
+
from: string;
|
|
19
|
+
to: string;
|
|
20
|
+
valueAtomic: string;
|
|
21
|
+
network: NetworkId;
|
|
22
|
+
asset: string;
|
|
23
|
+
route?: RouteKey;
|
|
24
|
+
txHash?: string;
|
|
25
|
+
timestamp: number;
|
|
26
|
+
facilitatorUrl?: string;
|
|
27
|
+
paymentRequirements?: PaymentRequirements;
|
|
28
|
+
}
|
|
29
|
+
/** Config for optional telemetry (billing/analytics). */
|
|
30
|
+
interface TelemetryConfig {
|
|
31
|
+
/** Project key from nosubs.ai dashboard. */
|
|
32
|
+
projectKey: string;
|
|
33
|
+
/**
|
|
34
|
+
* Telemetry endpoint URL.
|
|
35
|
+
* If not specified, uses DEFAULT_TELEMETRY_ENDPOINT (see server/constants.ts).
|
|
36
|
+
* If both are undefined, telemetry is not sent.
|
|
37
|
+
*/
|
|
38
|
+
endpoint?: string;
|
|
39
|
+
}
|
|
40
|
+
/** Telemetry event payload for payment_verified. */
|
|
41
|
+
interface TelemetryEvent {
|
|
42
|
+
event: "payment_verified";
|
|
43
|
+
ts: number;
|
|
44
|
+
projectKey: string;
|
|
45
|
+
network: string;
|
|
46
|
+
buyer: string;
|
|
47
|
+
payTo: string;
|
|
48
|
+
amountAtomic: string;
|
|
49
|
+
asset: string;
|
|
50
|
+
decimals: number;
|
|
51
|
+
nonce: string;
|
|
52
|
+
route: string;
|
|
53
|
+
facilitatorType: "hosted" | "self-hosted";
|
|
54
|
+
facilitatorUrl?: string;
|
|
55
|
+
facilitatorAddress?: string;
|
|
56
|
+
txHash?: string;
|
|
57
|
+
priceUsd?: string;
|
|
58
|
+
responseStatus?: number;
|
|
59
|
+
errorMessage?: string;
|
|
60
|
+
serviceDelivered?: boolean;
|
|
61
|
+
}
|
|
62
|
+
/** Config for createPaymentMiddleware. */
|
|
63
|
+
interface PaymentMiddlewareConfig {
|
|
64
|
+
/** Base URL of facilitator, e.g. https://facilitator.nosubs.ai */
|
|
65
|
+
facilitatorUrl: string;
|
|
66
|
+
/** Recipient address (developer). Validated at runtime. */
|
|
67
|
+
receiverAddress: string;
|
|
68
|
+
/** Map of protected routes and their pricing. */
|
|
69
|
+
routes: RoutesConfig;
|
|
70
|
+
/**
|
|
71
|
+
* Optional hook called whenever a payment is successfully settled.
|
|
72
|
+
* You can use this to push logs into your DB / analytics pipeline.
|
|
73
|
+
*/
|
|
74
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
75
|
+
/** Optional: Send usage telemetry for billing/analytics. */
|
|
76
|
+
telemetry?: TelemetryConfig;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Minimal config for mantlePaywall() - simplified API for single-route protection.
|
|
80
|
+
* This is the "sweet path" for Mantle mainnet + USDC with sensible defaults.
|
|
81
|
+
*/
|
|
82
|
+
interface MinimalPaywallOptions {
|
|
83
|
+
/** Price in USD (e.g. 0.01 for 1 cent). */
|
|
84
|
+
priceUsd: number;
|
|
85
|
+
/** Recipient address (developer wallet). Validated at runtime. */
|
|
86
|
+
payTo: string;
|
|
87
|
+
/** Optional facilitator URL (defaults to localhost:8080 or NEXT_PUBLIC_FACILITATOR_URL). */
|
|
88
|
+
facilitatorUrl?: string;
|
|
89
|
+
/**
|
|
90
|
+
* Optional API key for hosted facilitator billing.
|
|
91
|
+
* Required when using hosted facilitator (e.g., https://facilitator.x402mantlesdk.xyz).
|
|
92
|
+
* Not needed for self-hosted facilitator.
|
|
93
|
+
*/
|
|
94
|
+
apiKey?: string;
|
|
95
|
+
/** Optional telemetry config. */
|
|
96
|
+
telemetry?: TelemetryConfig;
|
|
97
|
+
/** Optional payment settled hook. */
|
|
98
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Express middleware function type for Mantle paywall.
|
|
102
|
+
* Async function that returns Promise<void>.
|
|
103
|
+
*/
|
|
104
|
+
type MantleMiddleware = (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
105
|
+
|
|
106
|
+
export type { MantleMiddleware as M, PaymentLogEntry as P, RouteKey as R, TelemetryConfig as T, RoutePricingConfig as a, RoutesConfig as b, TelemetryEvent as c, PaymentMiddlewareConfig as d, MinimalPaywallOptions as e };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { N as NetworkId, P as PaymentRequirements } from './types-BFUqKBBO.js';
|
|
2
|
+
|
|
3
|
+
/** Unique key for a protected route, e.g. "GET /api/protected". */
|
|
4
|
+
type RouteKey = string;
|
|
5
|
+
/** Pricing config for a single route. */
|
|
6
|
+
interface RoutePricingConfig {
|
|
7
|
+
/** Price in USD cents, e.g. 1 => $0.01. */
|
|
8
|
+
priceUsdCents: number;
|
|
9
|
+
/** Network identifier (e.g. "mantle-mainnet"). */
|
|
10
|
+
network: NetworkId;
|
|
11
|
+
}
|
|
12
|
+
/** Map of route keys to pricing config. */
|
|
13
|
+
type RoutesConfig = Record<RouteKey, RoutePricingConfig>;
|
|
14
|
+
/** Log entry for a successfully settled payment. */
|
|
15
|
+
interface PaymentLogEntry {
|
|
16
|
+
id: string;
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
valueAtomic: string;
|
|
20
|
+
network: NetworkId;
|
|
21
|
+
asset: string;
|
|
22
|
+
route?: RouteKey;
|
|
23
|
+
txHash?: string;
|
|
24
|
+
timestamp: number;
|
|
25
|
+
facilitatorUrl?: string;
|
|
26
|
+
paymentRequirements?: PaymentRequirements;
|
|
27
|
+
}
|
|
28
|
+
/** Config for optional telemetry (billing/analytics). */
|
|
29
|
+
interface TelemetryConfig {
|
|
30
|
+
/** Project key from nosubs.ai dashboard. */
|
|
31
|
+
projectKey: string;
|
|
32
|
+
/**
|
|
33
|
+
* Telemetry endpoint URL.
|
|
34
|
+
* If not specified, uses DEFAULT_TELEMETRY_ENDPOINT (see server/constants.ts).
|
|
35
|
+
*/
|
|
36
|
+
endpoint?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Minimal config for mantlePaywall() - simplified API for single-route protection.
|
|
40
|
+
* This is the "sweet path" for Mantle mainnet + USDC with sensible defaults.
|
|
41
|
+
*/
|
|
42
|
+
interface MinimalPaywallOptions {
|
|
43
|
+
/** Price in USD (e.g. 0.01 for 1 cent). */
|
|
44
|
+
priceUsd: number;
|
|
45
|
+
/** Recipient address (developer wallet). Validated at runtime. */
|
|
46
|
+
payTo: string;
|
|
47
|
+
/** Optional facilitator URL (defaults to localhost:8080 or NEXT_PUBLIC_FACILITATOR_URL). */
|
|
48
|
+
facilitatorUrl?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Optional API key for hosted facilitator billing.
|
|
51
|
+
* Required when using hosted facilitator (e.g., https://facilitator.x402mantlesdk.xyz).
|
|
52
|
+
* Not needed for self-hosted facilitator.
|
|
53
|
+
*/
|
|
54
|
+
apiKey?: string;
|
|
55
|
+
/** Optional telemetry config. */
|
|
56
|
+
telemetry?: TelemetryConfig;
|
|
57
|
+
/** Optional payment settled hook. */
|
|
58
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
59
|
+
}
|
|
60
|
+
/** Input for checkPayment function. */
|
|
61
|
+
interface PaymentCheckInput {
|
|
62
|
+
/** X-PAYMENT header value (base64 encoded), or null if not present. */
|
|
63
|
+
paymentHeader: string | null;
|
|
64
|
+
/** Payment requirements for this route. */
|
|
65
|
+
paymentRequirements: PaymentRequirements;
|
|
66
|
+
/** Facilitator URL for verification. */
|
|
67
|
+
facilitatorUrl: string;
|
|
68
|
+
/** Optional API key for hosted facilitator billing. */
|
|
69
|
+
apiKey?: string;
|
|
70
|
+
/** Route key for logging, e.g. "POST /api/generate-image". */
|
|
71
|
+
routeKey?: RouteKey;
|
|
72
|
+
/** Network ID for this payment. */
|
|
73
|
+
network: NetworkId;
|
|
74
|
+
/** Asset address (ERC-20 token). */
|
|
75
|
+
asset: string;
|
|
76
|
+
/** Telemetry config (optional). */
|
|
77
|
+
telemetry?: TelemetryConfig;
|
|
78
|
+
/** Payment settled callback (optional). */
|
|
79
|
+
onPaymentSettled?: (entry: PaymentLogEntry) => void;
|
|
80
|
+
}
|
|
81
|
+
/** Result from checkPayment function. */
|
|
82
|
+
interface PaymentCheckResult {
|
|
83
|
+
/** Status of the payment check. */
|
|
84
|
+
status: "require_payment" | "verification_error" | "invalid_payment" | "verified";
|
|
85
|
+
/** HTTP status code to return. */
|
|
86
|
+
statusCode: 402 | 500 | 200;
|
|
87
|
+
/** Response body to return (null if verified). */
|
|
88
|
+
responseBody: any | null;
|
|
89
|
+
/** Whether payment is valid (only true if status is "verified"). */
|
|
90
|
+
isValid: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Callback to send telemetry after handler execution.
|
|
93
|
+
* Only present when payment is verified and telemetry is enabled.
|
|
94
|
+
* Adapters should call this after the protected route handler completes.
|
|
95
|
+
*
|
|
96
|
+
* @param responseStatus - HTTP status code from handler (200, 500, etc.)
|
|
97
|
+
* @param error - Optional error message if handler threw
|
|
98
|
+
*/
|
|
99
|
+
sendTelemetryAfterResponse?: (responseStatus: number, error?: string) => void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type { MinimalPaywallOptions as M, PaymentLogEntry as P, RouteKey as R, TelemetryConfig as T, RoutePricingConfig as a, RoutesConfig as b, PaymentCheckInput as c, PaymentCheckResult as d };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { M as MinimalPaywallOptions } from './types-vicT7qsY.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Web Standards route handler type.
|
|
5
|
+
* Takes a Request and returns a Response.
|
|
6
|
+
*/
|
|
7
|
+
type WebHandler = (request: Request) => Promise<Response>;
|
|
8
|
+
/**
|
|
9
|
+
* Wrapper function that adds x402 payment verification to Web Standards handler.
|
|
10
|
+
*/
|
|
11
|
+
type WebPaywallWrapper = (handler: WebHandler) => WebHandler;
|
|
12
|
+
/**
|
|
13
|
+
* Create Web Standards middleware for x402 payment verification.
|
|
14
|
+
* Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
|
|
15
|
+
*
|
|
16
|
+
* Compatible with:
|
|
17
|
+
* - Hono
|
|
18
|
+
* - Cloudflare Workers
|
|
19
|
+
* - Deno
|
|
20
|
+
* - Bun
|
|
21
|
+
* - Any platform supporting Web Request/Response API
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* // Hono example
|
|
26
|
+
* import { Hono } from 'hono'
|
|
27
|
+
* import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/web'
|
|
28
|
+
*
|
|
29
|
+
* const app = new Hono()
|
|
30
|
+
* const pay = mantlePaywall({
|
|
31
|
+
* priceUsd: 0.01,
|
|
32
|
+
* payTo: '0x...',
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* app.post('/api/generate', pay(async (req) => {
|
|
36
|
+
* const body = await req.json();
|
|
37
|
+
* // Your handler code here
|
|
38
|
+
* return new Response(JSON.stringify({ success: true }), {
|
|
39
|
+
* headers: { 'Content-Type': 'application/json' }
|
|
40
|
+
* });
|
|
41
|
+
* }));
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // Cloudflare Workers example
|
|
47
|
+
* import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/web'
|
|
48
|
+
*
|
|
49
|
+
* const pay = mantlePaywall({
|
|
50
|
+
* priceUsd: 0.01,
|
|
51
|
+
* payTo: '0x...',
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* export default {
|
|
55
|
+
* async fetch(request: Request) {
|
|
56
|
+
* const handler = pay(async (req) => {
|
|
57
|
+
* // Your handler code here
|
|
58
|
+
* return new Response('Hello World');
|
|
59
|
+
* });
|
|
60
|
+
* return handler(request);
|
|
61
|
+
* }
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
|
|
66
|
+
* @returns Function that wraps Web Standards handlers with payment verification.
|
|
67
|
+
*/
|
|
68
|
+
declare function mantlePaywall(opts: MinimalPaywallOptions): WebPaywallWrapper;
|
|
69
|
+
|
|
70
|
+
type webStandards_WebHandler = WebHandler;
|
|
71
|
+
type webStandards_WebPaywallWrapper = WebPaywallWrapper;
|
|
72
|
+
declare const webStandards_mantlePaywall: typeof mantlePaywall;
|
|
73
|
+
declare namespace webStandards {
|
|
74
|
+
export { type webStandards_WebHandler as WebHandler, type webStandards_WebPaywallWrapper as WebPaywallWrapper, webStandards_mantlePaywall as mantlePaywall };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export { type WebHandler as W, type WebPaywallWrapper as a, mantlePaywall as m, webStandards as w };
|