x402-mantle-sdk 0.1.0 → 0.2.2
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/README.md +239 -44
- package/dist/client/index.cjs +40 -8
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +3 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.js +40 -8
- package/dist/client/index.js.map +1 -1
- package/dist/client/react.cjs +1178 -9
- package/dist/client/react.cjs.map +1 -1
- package/dist/client/react.d.cts +29 -1
- package/dist/client/react.d.ts +29 -1
- package/dist/client/react.js +1177 -9
- package/dist/client/react.js.map +1 -1
- package/dist/server/index.cjs +199 -43
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +80 -3
- package/dist/server/index.d.ts +80 -3
- package/dist/server/index.js +170 -16
- package/dist/server/index.js.map +1 -1
- package/package.json +15 -4
package/dist/server/index.d.cts
CHANGED
|
@@ -47,7 +47,7 @@ declare const NETWORKS: Record<NetworkId, NetworkConfig>;
|
|
|
47
47
|
/** Token addresses by network */
|
|
48
48
|
declare const TOKENS: Record<NetworkId, Record<string, TokenConfig>>;
|
|
49
49
|
/** Default platform URL (for project config) */
|
|
50
|
-
declare const DEFAULT_PLATFORM_URL = "https://
|
|
50
|
+
declare const DEFAULT_PLATFORM_URL = "https://mantle-x402.vercel.app";
|
|
51
51
|
/**
|
|
52
52
|
* Register a custom network configuration
|
|
53
53
|
*
|
|
@@ -128,6 +128,12 @@ interface X402Options {
|
|
|
128
128
|
customNetwork?: CustomNetworkConfig;
|
|
129
129
|
/** Custom token configurations for this network */
|
|
130
130
|
customTokens?: CustomTokenConfig;
|
|
131
|
+
/** API endpoint path for tracking (e.g., "/api/premium-data") */
|
|
132
|
+
endpoint?: string;
|
|
133
|
+
/** HTTP method for tracking (e.g., "GET", "POST") */
|
|
134
|
+
method?: string;
|
|
135
|
+
/** Enable analytics tracking (default: true) */
|
|
136
|
+
enableAnalytics?: boolean;
|
|
131
137
|
}
|
|
132
138
|
/** HTTP 402 Payment Required response */
|
|
133
139
|
interface PaymentRequiredResponse {
|
|
@@ -160,7 +166,7 @@ interface MiddlewareResult {
|
|
|
160
166
|
* 3. Returns 402 if no payment
|
|
161
167
|
* 4. Verifies payment on blockchain if present
|
|
162
168
|
*/
|
|
163
|
-
declare function processPaymentMiddleware(options: X402Options, headers: Headers | Record<string, string | string[] | undefined
|
|
169
|
+
declare function processPaymentMiddleware(options: X402Options, headers: Headers | Record<string, string | string[] | undefined>, requestPath?: string, requestMethod?: string): Promise<MiddlewareResult>;
|
|
164
170
|
|
|
165
171
|
/**
|
|
166
172
|
* Hono Framework Integration
|
|
@@ -179,6 +185,9 @@ declare function processPaymentMiddleware(options: X402Options, headers: Headers
|
|
|
179
185
|
interface HonoContext {
|
|
180
186
|
req: {
|
|
181
187
|
header(): Record<string, string>;
|
|
188
|
+
url?: string;
|
|
189
|
+
path?: string;
|
|
190
|
+
method?: string;
|
|
182
191
|
};
|
|
183
192
|
json(data: unknown, status?: number, headers?: Record<string, string>): Response;
|
|
184
193
|
}
|
|
@@ -289,4 +298,72 @@ interface BlockchainVerification {
|
|
|
289
298
|
*/
|
|
290
299
|
declare function verifyPaymentOnChain(transactionHash: string, config: ProjectConfig, requiredAmount: string, requiredToken: string, customRpcUrl?: string): Promise<BlockchainVerification>;
|
|
291
300
|
|
|
292
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Analytics & Payment Tracking
|
|
303
|
+
*
|
|
304
|
+
* Optional utility to log payment events to the platform for dashboard tracking
|
|
305
|
+
*/
|
|
306
|
+
/** Payment event data */
|
|
307
|
+
interface PaymentEvent {
|
|
308
|
+
transactionHash: string;
|
|
309
|
+
amount: string;
|
|
310
|
+
token: string;
|
|
311
|
+
network: string;
|
|
312
|
+
endpoint?: string;
|
|
313
|
+
method?: string;
|
|
314
|
+
fromAddress?: string;
|
|
315
|
+
toAddress: string;
|
|
316
|
+
blockNumber?: number;
|
|
317
|
+
status?: 'SUCCESS' | 'FAILED' | 'PENDING';
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Log a payment event to the platform
|
|
321
|
+
*
|
|
322
|
+
* This is optional - call this after successful payment verification
|
|
323
|
+
* to track endpoint usage in the dashboard.
|
|
324
|
+
*
|
|
325
|
+
* @param event - Payment event data
|
|
326
|
+
* @param requestPath - Optional: Request path (e.g., "/api/premium-data")
|
|
327
|
+
* @param requestMethod - Optional: HTTP method (e.g., "GET", "POST")
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* import { logPayment } from '@x402-devkit/sdk/server'
|
|
332
|
+
*
|
|
333
|
+
* // After successful payment verification
|
|
334
|
+
* await logPayment({
|
|
335
|
+
* transactionHash: receipt.transactionHash,
|
|
336
|
+
* amount: '0.001',
|
|
337
|
+
* token: 'MNT',
|
|
338
|
+
* network: 'mantle',
|
|
339
|
+
* toAddress: config.payTo,
|
|
340
|
+
* }, '/api/premium-data', 'GET')
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
declare function logPayment(event: PaymentEvent, requestPath?: string, requestMethod?: string): Promise<void>;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Endpoint Registry
|
|
347
|
+
*
|
|
348
|
+
* Automatically registers endpoints when x402 middleware is used
|
|
349
|
+
* This allows endpoints to appear in the dashboard even before payments are made
|
|
350
|
+
*/
|
|
351
|
+
/** Endpoint registration data */
|
|
352
|
+
interface EndpointRegistration {
|
|
353
|
+
endpoint: string;
|
|
354
|
+
method?: string;
|
|
355
|
+
price: string;
|
|
356
|
+
token: string;
|
|
357
|
+
network: string;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Register an endpoint with the platform
|
|
361
|
+
*
|
|
362
|
+
* This is called automatically when x402 middleware is first invoked for an endpoint.
|
|
363
|
+
* Endpoints appear in the dashboard even before any payments are made.
|
|
364
|
+
*
|
|
365
|
+
* @param registration - Endpoint registration data
|
|
366
|
+
*/
|
|
367
|
+
declare function registerEndpoint(registration: EndpointRegistration): Promise<void>;
|
|
368
|
+
|
|
369
|
+
export { type BlockchainVerification, type CustomNetworkConfig, type CustomTokenConfig, DEFAULT_PLATFORM_URL, type EndpointRegistration, type MiddlewareResult, NETWORKS, type NetworkConfig, type NetworkEnvironment, type NetworkId, type PaymentEvent, type PaymentReceipt, type PaymentRequiredResponse, type PaymentVerification, type ProjectConfig, TOKENS, type TokenConfig, type X402Options, clearCache, extractPaymentReceipt, getAvailableNetworks, getChainId, getNetworkConfig, getNetworksByEnvironment, getProjectConfig, getRpcUrl, getTokenConfig, initializePlatform, isMainnet, isTestnet, logPayment, processPaymentMiddleware, registerCustomNetwork, registerCustomTokens, registerEndpoint, verifyPayment, verifyPaymentOnChain, x402 };
|
package/dist/server/index.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ declare const NETWORKS: Record<NetworkId, NetworkConfig>;
|
|
|
47
47
|
/** Token addresses by network */
|
|
48
48
|
declare const TOKENS: Record<NetworkId, Record<string, TokenConfig>>;
|
|
49
49
|
/** Default platform URL (for project config) */
|
|
50
|
-
declare const DEFAULT_PLATFORM_URL = "https://
|
|
50
|
+
declare const DEFAULT_PLATFORM_URL = "https://mantle-x402.vercel.app";
|
|
51
51
|
/**
|
|
52
52
|
* Register a custom network configuration
|
|
53
53
|
*
|
|
@@ -128,6 +128,12 @@ interface X402Options {
|
|
|
128
128
|
customNetwork?: CustomNetworkConfig;
|
|
129
129
|
/** Custom token configurations for this network */
|
|
130
130
|
customTokens?: CustomTokenConfig;
|
|
131
|
+
/** API endpoint path for tracking (e.g., "/api/premium-data") */
|
|
132
|
+
endpoint?: string;
|
|
133
|
+
/** HTTP method for tracking (e.g., "GET", "POST") */
|
|
134
|
+
method?: string;
|
|
135
|
+
/** Enable analytics tracking (default: true) */
|
|
136
|
+
enableAnalytics?: boolean;
|
|
131
137
|
}
|
|
132
138
|
/** HTTP 402 Payment Required response */
|
|
133
139
|
interface PaymentRequiredResponse {
|
|
@@ -160,7 +166,7 @@ interface MiddlewareResult {
|
|
|
160
166
|
* 3. Returns 402 if no payment
|
|
161
167
|
* 4. Verifies payment on blockchain if present
|
|
162
168
|
*/
|
|
163
|
-
declare function processPaymentMiddleware(options: X402Options, headers: Headers | Record<string, string | string[] | undefined
|
|
169
|
+
declare function processPaymentMiddleware(options: X402Options, headers: Headers | Record<string, string | string[] | undefined>, requestPath?: string, requestMethod?: string): Promise<MiddlewareResult>;
|
|
164
170
|
|
|
165
171
|
/**
|
|
166
172
|
* Hono Framework Integration
|
|
@@ -179,6 +185,9 @@ declare function processPaymentMiddleware(options: X402Options, headers: Headers
|
|
|
179
185
|
interface HonoContext {
|
|
180
186
|
req: {
|
|
181
187
|
header(): Record<string, string>;
|
|
188
|
+
url?: string;
|
|
189
|
+
path?: string;
|
|
190
|
+
method?: string;
|
|
182
191
|
};
|
|
183
192
|
json(data: unknown, status?: number, headers?: Record<string, string>): Response;
|
|
184
193
|
}
|
|
@@ -289,4 +298,72 @@ interface BlockchainVerification {
|
|
|
289
298
|
*/
|
|
290
299
|
declare function verifyPaymentOnChain(transactionHash: string, config: ProjectConfig, requiredAmount: string, requiredToken: string, customRpcUrl?: string): Promise<BlockchainVerification>;
|
|
291
300
|
|
|
292
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Analytics & Payment Tracking
|
|
303
|
+
*
|
|
304
|
+
* Optional utility to log payment events to the platform for dashboard tracking
|
|
305
|
+
*/
|
|
306
|
+
/** Payment event data */
|
|
307
|
+
interface PaymentEvent {
|
|
308
|
+
transactionHash: string;
|
|
309
|
+
amount: string;
|
|
310
|
+
token: string;
|
|
311
|
+
network: string;
|
|
312
|
+
endpoint?: string;
|
|
313
|
+
method?: string;
|
|
314
|
+
fromAddress?: string;
|
|
315
|
+
toAddress: string;
|
|
316
|
+
blockNumber?: number;
|
|
317
|
+
status?: 'SUCCESS' | 'FAILED' | 'PENDING';
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Log a payment event to the platform
|
|
321
|
+
*
|
|
322
|
+
* This is optional - call this after successful payment verification
|
|
323
|
+
* to track endpoint usage in the dashboard.
|
|
324
|
+
*
|
|
325
|
+
* @param event - Payment event data
|
|
326
|
+
* @param requestPath - Optional: Request path (e.g., "/api/premium-data")
|
|
327
|
+
* @param requestMethod - Optional: HTTP method (e.g., "GET", "POST")
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```typescript
|
|
331
|
+
* import { logPayment } from '@x402-devkit/sdk/server'
|
|
332
|
+
*
|
|
333
|
+
* // After successful payment verification
|
|
334
|
+
* await logPayment({
|
|
335
|
+
* transactionHash: receipt.transactionHash,
|
|
336
|
+
* amount: '0.001',
|
|
337
|
+
* token: 'MNT',
|
|
338
|
+
* network: 'mantle',
|
|
339
|
+
* toAddress: config.payTo,
|
|
340
|
+
* }, '/api/premium-data', 'GET')
|
|
341
|
+
* ```
|
|
342
|
+
*/
|
|
343
|
+
declare function logPayment(event: PaymentEvent, requestPath?: string, requestMethod?: string): Promise<void>;
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Endpoint Registry
|
|
347
|
+
*
|
|
348
|
+
* Automatically registers endpoints when x402 middleware is used
|
|
349
|
+
* This allows endpoints to appear in the dashboard even before payments are made
|
|
350
|
+
*/
|
|
351
|
+
/** Endpoint registration data */
|
|
352
|
+
interface EndpointRegistration {
|
|
353
|
+
endpoint: string;
|
|
354
|
+
method?: string;
|
|
355
|
+
price: string;
|
|
356
|
+
token: string;
|
|
357
|
+
network: string;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Register an endpoint with the platform
|
|
361
|
+
*
|
|
362
|
+
* This is called automatically when x402 middleware is first invoked for an endpoint.
|
|
363
|
+
* Endpoints appear in the dashboard even before any payments are made.
|
|
364
|
+
*
|
|
365
|
+
* @param registration - Endpoint registration data
|
|
366
|
+
*/
|
|
367
|
+
declare function registerEndpoint(registration: EndpointRegistration): Promise<void>;
|
|
368
|
+
|
|
369
|
+
export { type BlockchainVerification, type CustomNetworkConfig, type CustomTokenConfig, DEFAULT_PLATFORM_URL, type EndpointRegistration, type MiddlewareResult, NETWORKS, type NetworkConfig, type NetworkEnvironment, type NetworkId, type PaymentEvent, type PaymentReceipt, type PaymentRequiredResponse, type PaymentVerification, type ProjectConfig, TOKENS, type TokenConfig, type X402Options, clearCache, extractPaymentReceipt, getAvailableNetworks, getChainId, getNetworkConfig, getNetworksByEnvironment, getProjectConfig, getRpcUrl, getTokenConfig, initializePlatform, isMainnet, isTestnet, logPayment, processPaymentMiddleware, registerCustomNetwork, registerCustomTokens, registerEndpoint, verifyPayment, verifyPaymentOnChain, x402 };
|
package/dist/server/index.js
CHANGED
|
@@ -196,7 +196,7 @@ var init_constants = __esm({
|
|
|
196
196
|
}
|
|
197
197
|
};
|
|
198
198
|
ERC20_TRANSFER_SIGNATURE = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef";
|
|
199
|
-
DEFAULT_PLATFORM_URL = "https://
|
|
199
|
+
DEFAULT_PLATFORM_URL = "https://mantle-x402.vercel.app";
|
|
200
200
|
AMOUNT_TOLERANCE = BigInt(1e15);
|
|
201
201
|
customNetworks = /* @__PURE__ */ new Map();
|
|
202
202
|
customTokens = /* @__PURE__ */ new Map();
|
|
@@ -204,15 +204,12 @@ var init_constants = __esm({
|
|
|
204
204
|
});
|
|
205
205
|
|
|
206
206
|
// src/server/platform.ts
|
|
207
|
-
init_constants();
|
|
208
|
-
var cachedConfig = null;
|
|
209
|
-
var validationPromise = null;
|
|
210
207
|
function getPlatformBaseUrl() {
|
|
211
208
|
return process.env.X402_PLATFORM_URL || process.env.NEXT_PUBLIC_X402_PLATFORM_URL || DEFAULT_PLATFORM_URL;
|
|
212
209
|
}
|
|
213
210
|
async function validateProject(appId) {
|
|
214
211
|
const baseUrl = getPlatformBaseUrl();
|
|
215
|
-
const url = `${baseUrl}/v1/validate?appId=${encodeURIComponent(appId)}`;
|
|
212
|
+
const url = `${baseUrl}/api/v1/validate?appId=${encodeURIComponent(appId)}`;
|
|
216
213
|
const response = await fetch(url, {
|
|
217
214
|
method: "GET",
|
|
218
215
|
headers: { "Content-Type": "application/json" }
|
|
@@ -271,6 +268,111 @@ function clearCache() {
|
|
|
271
268
|
cachedConfig = null;
|
|
272
269
|
validationPromise = null;
|
|
273
270
|
}
|
|
271
|
+
var cachedConfig, validationPromise;
|
|
272
|
+
var init_platform = __esm({
|
|
273
|
+
"src/server/platform.ts"() {
|
|
274
|
+
"use strict";
|
|
275
|
+
init_constants();
|
|
276
|
+
cachedConfig = null;
|
|
277
|
+
validationPromise = null;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// src/server/analytics.ts
|
|
282
|
+
var analytics_exports = {};
|
|
283
|
+
__export(analytics_exports, {
|
|
284
|
+
logPayment: () => logPayment
|
|
285
|
+
});
|
|
286
|
+
function getPlatformBaseUrl2() {
|
|
287
|
+
return process.env.X402_PLATFORM_URL || process.env.NEXT_PUBLIC_X402_PLATFORM_URL || DEFAULT_PLATFORM_URL;
|
|
288
|
+
}
|
|
289
|
+
async function logPayment(event, requestPath, requestMethod) {
|
|
290
|
+
try {
|
|
291
|
+
const config = getProjectConfig();
|
|
292
|
+
const baseUrl = getPlatformBaseUrl2();
|
|
293
|
+
const url = `${baseUrl}/api/payments`;
|
|
294
|
+
const payload = {
|
|
295
|
+
appId: config.appId,
|
|
296
|
+
transactionHash: event.transactionHash,
|
|
297
|
+
amount: event.amount,
|
|
298
|
+
token: event.token,
|
|
299
|
+
network: event.network || config.network,
|
|
300
|
+
endpoint: event.endpoint || requestPath || null,
|
|
301
|
+
method: event.method || requestMethod || null,
|
|
302
|
+
fromAddress: event.fromAddress || null,
|
|
303
|
+
toAddress: event.toAddress || config.payTo,
|
|
304
|
+
blockNumber: event.blockNumber || null,
|
|
305
|
+
status: event.status || "SUCCESS"
|
|
306
|
+
};
|
|
307
|
+
fetch(url, {
|
|
308
|
+
method: "POST",
|
|
309
|
+
headers: { "Content-Type": "application/json" },
|
|
310
|
+
body: JSON.stringify(payload)
|
|
311
|
+
}).catch((error) => {
|
|
312
|
+
console.warn("Failed to log payment event:", error);
|
|
313
|
+
});
|
|
314
|
+
} catch (error) {
|
|
315
|
+
console.warn("Failed to log payment event:", error);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
var init_analytics = __esm({
|
|
319
|
+
"src/server/analytics.ts"() {
|
|
320
|
+
"use strict";
|
|
321
|
+
init_platform();
|
|
322
|
+
init_constants();
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// src/server/endpoint-registry.ts
|
|
327
|
+
var endpoint_registry_exports = {};
|
|
328
|
+
__export(endpoint_registry_exports, {
|
|
329
|
+
registerEndpoint: () => registerEndpoint
|
|
330
|
+
});
|
|
331
|
+
function getPlatformBaseUrl3() {
|
|
332
|
+
return process.env.X402_PLATFORM_URL || process.env.NEXT_PUBLIC_X402_PLATFORM_URL || DEFAULT_PLATFORM_URL;
|
|
333
|
+
}
|
|
334
|
+
async function registerEndpoint(registration) {
|
|
335
|
+
try {
|
|
336
|
+
const config = getProjectConfig();
|
|
337
|
+
const baseUrl = getPlatformBaseUrl3();
|
|
338
|
+
const endpointKey = `${config.appId}:${registration.endpoint}:${registration.method || "ANY"}`;
|
|
339
|
+
if (registeredEndpoints.has(endpointKey)) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
registeredEndpoints.add(endpointKey);
|
|
343
|
+
const url = `${baseUrl}/api/endpoints/register`;
|
|
344
|
+
const payload = {
|
|
345
|
+
appId: config.appId,
|
|
346
|
+
endpoint: registration.endpoint,
|
|
347
|
+
method: registration.method || null,
|
|
348
|
+
price: registration.price,
|
|
349
|
+
token: registration.token,
|
|
350
|
+
network: registration.network || config.network
|
|
351
|
+
};
|
|
352
|
+
fetch(url, {
|
|
353
|
+
method: "POST",
|
|
354
|
+
headers: { "Content-Type": "application/json" },
|
|
355
|
+
body: JSON.stringify(payload)
|
|
356
|
+
}).catch((error) => {
|
|
357
|
+
console.warn("Failed to register endpoint:", error);
|
|
358
|
+
registeredEndpoints.delete(endpointKey);
|
|
359
|
+
});
|
|
360
|
+
} catch (error) {
|
|
361
|
+
console.warn("Failed to register endpoint:", error);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
var registeredEndpoints;
|
|
365
|
+
var init_endpoint_registry = __esm({
|
|
366
|
+
"src/server/endpoint-registry.ts"() {
|
|
367
|
+
"use strict";
|
|
368
|
+
init_platform();
|
|
369
|
+
init_constants();
|
|
370
|
+
registeredEndpoints = /* @__PURE__ */ new Set();
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
// src/server/middleware.ts
|
|
375
|
+
init_platform();
|
|
274
376
|
|
|
275
377
|
// src/server/blockchain.ts
|
|
276
378
|
init_constants();
|
|
@@ -295,11 +397,10 @@ async function callRPC(url, method, params) {
|
|
|
295
397
|
return data.result;
|
|
296
398
|
}
|
|
297
399
|
async function getTransactionReceipt(rpcUrl, txHash) {
|
|
298
|
-
const receipt = await
|
|
299
|
-
rpcUrl,
|
|
300
|
-
"
|
|
301
|
-
|
|
302
|
-
);
|
|
400
|
+
const [receipt, tx] = await Promise.all([
|
|
401
|
+
callRPC(rpcUrl, "eth_getTransactionReceipt", [txHash]),
|
|
402
|
+
callRPC(rpcUrl, "eth_getTransactionByHash", [txHash])
|
|
403
|
+
]);
|
|
303
404
|
if (!receipt) {
|
|
304
405
|
return null;
|
|
305
406
|
}
|
|
@@ -308,8 +409,8 @@ async function getTransactionReceipt(rpcUrl, txHash) {
|
|
|
308
409
|
blockNumber: parseInt(receipt.blockNumber, 16),
|
|
309
410
|
status: receipt.status === "0x1" ? "success" : "failed",
|
|
310
411
|
from: receipt.from,
|
|
311
|
-
to: receipt.to,
|
|
312
|
-
value:
|
|
412
|
+
to: tx?.to || receipt.to,
|
|
413
|
+
value: tx?.value || "0x0",
|
|
313
414
|
logs: receipt.logs || []
|
|
314
415
|
};
|
|
315
416
|
}
|
|
@@ -364,16 +465,19 @@ async function verifyPaymentOnChain(transactionHash, config, requiredAmount, req
|
|
|
364
465
|
if (requiredToken.toUpperCase() === "MNT") {
|
|
365
466
|
const valueWei = BigInt(receipt.value);
|
|
366
467
|
const requiredWei2 = parseAmountToWei(requiredAmount, 18);
|
|
468
|
+
const expectedMerchantAmount = requiredWei2 * 995n / 1000n;
|
|
469
|
+
const minAcceptable = expectedMerchantAmount - AMOUNT_TOLERANCE;
|
|
470
|
+
const maxAcceptable = requiredWei2 + AMOUNT_TOLERANCE;
|
|
367
471
|
if (receipt.to?.toLowerCase() !== recipient) {
|
|
368
472
|
return {
|
|
369
473
|
valid: false,
|
|
370
474
|
error: `Recipient mismatch: expected ${config.payTo}, got ${receipt.to}`
|
|
371
475
|
};
|
|
372
476
|
}
|
|
373
|
-
if (valueWei <
|
|
477
|
+
if (valueWei < minAcceptable || valueWei > maxAcceptable) {
|
|
374
478
|
return {
|
|
375
479
|
valid: false,
|
|
376
|
-
error: `Amount mismatch: expected
|
|
480
|
+
error: `Amount mismatch: expected ~${requiredAmount} MNT (99.5%-100%), got ${weiToTokenUnits(valueWei)} MNT`
|
|
377
481
|
};
|
|
378
482
|
}
|
|
379
483
|
return {
|
|
@@ -499,7 +603,7 @@ function createPaymentRequiredResponse(options, config, chainId) {
|
|
|
499
603
|
}
|
|
500
604
|
};
|
|
501
605
|
}
|
|
502
|
-
async function processPaymentMiddleware(options, headers) {
|
|
606
|
+
async function processPaymentMiddleware(options, headers, requestPath, requestMethod) {
|
|
503
607
|
try {
|
|
504
608
|
if (options.customNetwork) {
|
|
505
609
|
const networkId = options.network || "custom-network";
|
|
@@ -538,6 +642,28 @@ async function processPaymentMiddleware(options, headers) {
|
|
|
538
642
|
}
|
|
539
643
|
};
|
|
540
644
|
}
|
|
645
|
+
if (verification.transactionHash && options.enableAnalytics !== false) {
|
|
646
|
+
Promise.resolve().then(() => (init_analytics(), analytics_exports)).then(({ logPayment: logPayment2 }) => {
|
|
647
|
+
logPayment2(
|
|
648
|
+
{
|
|
649
|
+
transactionHash: verification.transactionHash,
|
|
650
|
+
amount: verification.amount || options.price,
|
|
651
|
+
token: verification.token || options.token,
|
|
652
|
+
network,
|
|
653
|
+
toAddress: config.payTo,
|
|
654
|
+
blockNumber: void 0,
|
|
655
|
+
// Could be extracted from verification if available
|
|
656
|
+
status: "SUCCESS"
|
|
657
|
+
},
|
|
658
|
+
options.endpoint || requestPath,
|
|
659
|
+
// Use provided endpoint or extracted path
|
|
660
|
+
options.method || requestMethod
|
|
661
|
+
// Use provided method or extracted method
|
|
662
|
+
).catch(() => {
|
|
663
|
+
});
|
|
664
|
+
}).catch(() => {
|
|
665
|
+
});
|
|
666
|
+
}
|
|
541
667
|
return { allowed: true };
|
|
542
668
|
} catch (error) {
|
|
543
669
|
return {
|
|
@@ -551,6 +677,7 @@ async function processPaymentMiddleware(options, headers) {
|
|
|
551
677
|
}
|
|
552
678
|
|
|
553
679
|
// src/server/hono.ts
|
|
680
|
+
init_platform();
|
|
554
681
|
function x402(options) {
|
|
555
682
|
if (!options.price || !options.token) {
|
|
556
683
|
throw new Error("x402 middleware requires price and token options");
|
|
@@ -572,7 +699,29 @@ function x402(options) {
|
|
|
572
699
|
return async (c, next) => {
|
|
573
700
|
try {
|
|
574
701
|
await ensureInitialized();
|
|
575
|
-
const
|
|
702
|
+
const requestPath = c.req.path || (c.req.url ? new URL(c.req.url).pathname : void 0);
|
|
703
|
+
const requestMethod = c.req.method;
|
|
704
|
+
if (requestPath && options.enableAnalytics !== false) {
|
|
705
|
+
Promise.resolve().then(() => (init_endpoint_registry(), endpoint_registry_exports)).then(({ registerEndpoint: registerEndpoint2 }) => {
|
|
706
|
+
const config = getProjectConfig();
|
|
707
|
+
const network = options.network || (options.testnet ? "mantle-sepolia" : config.network);
|
|
708
|
+
registerEndpoint2({
|
|
709
|
+
endpoint: requestPath,
|
|
710
|
+
method: requestMethod || options.method,
|
|
711
|
+
price: options.price,
|
|
712
|
+
token: options.token,
|
|
713
|
+
network
|
|
714
|
+
}).catch(() => {
|
|
715
|
+
});
|
|
716
|
+
}).catch(() => {
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
const result = await processPaymentMiddleware(
|
|
720
|
+
options,
|
|
721
|
+
c.req.header(),
|
|
722
|
+
requestPath,
|
|
723
|
+
requestMethod
|
|
724
|
+
);
|
|
576
725
|
if (!result.allowed) {
|
|
577
726
|
if (result.paymentRequired) {
|
|
578
727
|
return c.json(result.paymentRequired.body, 402, result.paymentRequired.headers);
|
|
@@ -590,7 +739,10 @@ function x402(options) {
|
|
|
590
739
|
}
|
|
591
740
|
|
|
592
741
|
// src/server/index.ts
|
|
742
|
+
init_platform();
|
|
593
743
|
init_constants();
|
|
744
|
+
init_analytics();
|
|
745
|
+
init_endpoint_registry();
|
|
594
746
|
export {
|
|
595
747
|
DEFAULT_PLATFORM_URL,
|
|
596
748
|
NETWORKS,
|
|
@@ -607,9 +759,11 @@ export {
|
|
|
607
759
|
initializePlatform,
|
|
608
760
|
isMainnet,
|
|
609
761
|
isTestnet,
|
|
762
|
+
logPayment,
|
|
610
763
|
processPaymentMiddleware,
|
|
611
764
|
registerCustomNetwork,
|
|
612
765
|
registerCustomTokens,
|
|
766
|
+
registerEndpoint,
|
|
613
767
|
verifyPayment,
|
|
614
768
|
verifyPaymentOnChain,
|
|
615
769
|
x402
|