mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api-base.git
synced 2026-09-21 16:52:00 -07:00
Resolving merge conflicts
This commit is contained in:
+44
-33
@@ -7,7 +7,9 @@
|
||||
import express from 'express'
|
||||
import cors from 'cors'
|
||||
import dotenv from 'dotenv'
|
||||
import { paymentMiddleware as x402PaymentMiddleware } from 'x402-express'
|
||||
import { paymentMiddleware as x402PaymentMiddleware } from '@x402/express'
|
||||
import { HTTPFacilitatorClient, x402ResourceServer } from '@x402/core/server'
|
||||
import { registerExactEvmScheme } from '@x402/evm/exact/server'
|
||||
|
||||
import { fileURLToPath } from 'url'
|
||||
import { dirname, join } from 'path'
|
||||
@@ -74,7 +76,22 @@ class Server {
|
||||
origin: true, // Allow all origins (more reliable than '*')
|
||||
credentials: false, // Set to true if you need to support credentials
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
|
||||
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
|
||||
allowedHeaders: [
|
||||
'Content-Type',
|
||||
'Authorization',
|
||||
'X-Requested-With',
|
||||
'PAYMENT-SIGNATURE',
|
||||
'PAYMENT-REQUIRED',
|
||||
'PAYMENT-RESPONSE',
|
||||
'X-PAYMENT',
|
||||
'X-PAYMENT-RESPONSE'
|
||||
],
|
||||
exposedHeaders: [
|
||||
'PAYMENT-REQUIRED',
|
||||
'PAYMENT-RESPONSE',
|
||||
'PAYMENT-SIGNATURE',
|
||||
'X-PAYMENT-RESPONSE'
|
||||
]
|
||||
}))
|
||||
|
||||
// URL normalization middleware - collapse multiple slashes
|
||||
@@ -108,57 +125,51 @@ class Server {
|
||||
if (x402Settings.enabled && basicAuthSettings.enabled) {
|
||||
// X402_ENABLED=true AND USE_BASIC_AUTH=true: Apply x402 conditionally
|
||||
const routes = buildX402Routes(this.config.apiPrefix)
|
||||
let facilitatorOptions = x402Settings.facilitatorUrl
|
||||
|
||||
// Support for multiple facilitators (CDP and Dexter)
|
||||
const primaryFacilitator = x402Settings.primaryFacilitator || 'cdp'
|
||||
const facilitatorConfig = getFacilitatorConfig(primaryFacilitator)
|
||||
|
||||
// Use configured facilitator URL
|
||||
facilitatorOptions = {
|
||||
const facilitatorOptions = {
|
||||
url: facilitatorConfig.url,
|
||||
createAuthHeaders: facilitatorConfig.requiresAuth ? createAuthHeader : null
|
||||
}
|
||||
|
||||
wlogger.info(`x402 middleware enabled with basic auth bypass; enforcing ${x402Settings.priceUSDC} USDC per request (unless basic auth provided) [facilitator: ${facilitatorConfig.name}]`)
|
||||
wlogger.info(`x402 v2 middleware enabled with basic auth bypass; enforcing ${x402Settings.priceUSDC} USDC per request (unless basic auth provided) [facilitator: ${facilitatorConfig.name}]`)
|
||||
|
||||
const facilitatorClient = new HTTPFacilitatorClient(facilitatorOptions)
|
||||
// x402 v2 exports use lowercase class names (x402ResourceServer).
|
||||
// eslint-disable-next-line new-cap
|
||||
const resourceServer = new x402ResourceServer(facilitatorClient)
|
||||
registerExactEvmScheme(resourceServer, {})
|
||||
const x402Mw = x402PaymentMiddleware(routes, resourceServer)
|
||||
|
||||
// Create conditional x402 middleware that bypasses if basic auth is valid
|
||||
const conditionalX402Middleware = (req, res, next) => {
|
||||
// req.headers['accept'] = 'application/json';
|
||||
// If basic auth is valid, bypass x402
|
||||
if (req.locals?.basicAuthValid === true) {
|
||||
return next()
|
||||
}
|
||||
|
||||
// Otherwise, apply x402 middleware
|
||||
return x402PaymentMiddleware(
|
||||
x402Settings.serverAddress,
|
||||
routes,
|
||||
facilitatorOptions
|
||||
)(req, res, next)
|
||||
return x402Mw(req, res, next)
|
||||
}
|
||||
|
||||
app.use(conditionalX402Middleware)
|
||||
} else if (x402Settings.enabled && !basicAuthSettings.enabled) {
|
||||
// X402_ENABLED=true AND USE_BASIC_AUTH=false: Apply x402 unconditionally (no basic auth bypass)
|
||||
const routes = buildX402Routes(this.config.apiPrefix)
|
||||
let facilitatorOptions = x402Settings.facilitatorUrl
|
||||
|
||||
if (facilitatorOptions) {
|
||||
facilitatorOptions = {
|
||||
url: x402Settings.facilitatorUrl,
|
||||
createAuthHeaders: createAuthHeader
|
||||
}
|
||||
// Display facilitator running url
|
||||
console.log(`Facilitator running on: ${facilitatorOptions.url}`)
|
||||
} else if (x402Settings.enabled && !basicAuthSettings.enabled) {
|
||||
const routes = buildX402Routes(this.config.apiPrefix)
|
||||
const primaryFacilitator = x402Settings.primaryFacilitator || 'cdp'
|
||||
const facilitatorConfig = getFacilitatorConfig(primaryFacilitator)
|
||||
const facilitatorOptions = {
|
||||
url: facilitatorConfig.url,
|
||||
createAuthHeaders: facilitatorConfig.requiresAuth ? createAuthHeader : null
|
||||
}
|
||||
|
||||
wlogger.info(`x402 middleware enabled (basic auth disabled); enforcing ${x402Settings.priceUSDC} satoshis per request`)
|
||||
wlogger.info(`x402 v2 middleware enabled (basic auth disabled); enforcing ${x402Settings.priceUSDC} USDC per request [facilitator: ${facilitatorConfig.name}]`)
|
||||
|
||||
// Apply x402 middleware unconditionally - no basic auth bypass
|
||||
app.use(x402PaymentMiddleware(
|
||||
x402Settings.serverAddress,
|
||||
routes,
|
||||
facilitatorOptions
|
||||
))
|
||||
const facilitatorClient = new HTTPFacilitatorClient(facilitatorOptions)
|
||||
// eslint-disable-next-line new-cap
|
||||
const resourceServer = new x402ResourceServer(facilitatorClient)
|
||||
registerExactEvmScheme(resourceServer, {})
|
||||
app.use(x402PaymentMiddleware(routes, resourceServer))
|
||||
} else if (basicAuthSettings.enabled && !x402Settings.enabled) {
|
||||
// USE_BASIC_AUTH=true AND X402_ENABLED=false: Require basic auth, reject unauthenticated requests
|
||||
wlogger.info('Basic auth enforcement enabled (x402 disabled)')
|
||||
|
||||
Reference in New Issue
Block a user