14 Commits
Author SHA1 Message Date
Chris Troutner 6fe0e01e8b Merge pull request #12 from Permissionless-Software-Foundation/ct-unstable
Improved debugging of interaction with x402 Facilitator
2025-12-22 06:48:35 -07:00
Chris Troutner f33b39ef01 fix(x402-bch-express): Updating to latest version 2025-12-22 06:35:47 -07:00
Chris Troutner 6d27630393 fix(debug): Debugging networking issues 2025-12-22 06:03:28 -07:00
Chris Troutner eac4916415 Updating .env-example for docker 2025-12-22 05:13:07 -07:00
Chris Troutner baa1170b89 Removing unneeded files 2025-12-22 05:10:59 -07:00
Chris Troutner 23ce276e19 fix(docker): Using .env rather than .env-example in docker file 2025-12-22 05:10:37 -07:00
Chris Troutner f28e2c6a1a Fixing bug in dockerfile 2025-12-21 16:38:43 -07:00
Chris Troutner 50a1a83a82 Merge pull request #11 from Permissionless-Software-Foundation/ct-unstable
Working towards x402 production
2025-12-21 16:34:26 -07:00
Chris Troutner eb2dda6955 fix(deps): Updating dependencies 2025-12-21 16:33:42 -07:00
Chris Troutner fe8d2ab051 Updating .env-example for docker container 2025-12-21 16:31:37 -07:00
Chris Troutner 66123de679 fix(fulcrum): Using basic auth token when psf-bch-api calls itself 2025-12-21 15:14:48 -07:00
Chris Troutner e2860d08b1 Changing default cost to 200 sats per call 2025-12-21 11:22:44 -07:00
Chris Troutner fa14af624f Merge pull request #10 from Permissionless-Software-Foundation/ct-unstable
fix(console.logs()): Removing terminal noise
2025-12-17 13:47:03 -07:00
Chris Troutner 1eb787e0d4 fix(console.logs()): Removing terminal noise 2025-12-17 13:46:08 -07:00
13 changed files with 1047 additions and 716 deletions
+1
View File
@@ -25,6 +25,7 @@ PORT=5942
X402_ENABLED=true
SERVER_BCH_ADDRESS=bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d
FACILITATOR_URL=http://localhost:4345/facilitator
X402_PRICE_SAT=200
# Basic Authentication required to access this API?
USE_BASIC_AUTH=true
+2 -2
View File
@@ -8,7 +8,7 @@ This is a REST API for communicating with Bitcoin Cash infrastructure. It replac
## x402-bch Payments
All REST endpoints exposed under the `/v6` prefix are protected by the [`x402-bch-express`](https://www.npmjs.com/package/x402-bch-express) middleware. Each API call requires a BCH payment authorization for **2000 satoshis**. The middleware advertises payment requirements via HTTP 402 responses and validates incoming `X-PAYMENT` headers with a configured Facilitator.
All REST endpoints exposed under the `/v6` prefix are protected by the [`x402-bch-express`](https://www.npmjs.com/package/x402-bch-express) middleware. Each API call requires a BCH payment authorization for **200 satoshis**. The middleware advertises payment requirements via HTTP 402 responses and validates incoming `X-PAYMENT` headers with a configured Facilitator.
### Configuration
@@ -17,7 +17,7 @@ Environment variables control the payment flow:
- `X402_ENABLED` — set to `false` (case-insensitive) to disable the middleware. Defaults to enabled.
- `SERVER_BCH_ADDRESS` — BCH cash address that receives funding transactions. Defaults to `bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d`.
- `FACILITATOR_URL` — Root URL of the facilitator service (e.g., `http://localhost:4345/facilitator`).
- `X402_PRICE_SAT` — Optional; override the satoshi price per call (defaults to `2000`).
- `X402_PRICE_SAT` — Optional; override the satoshi price per call (defaults to `200`).
When `X402_ENABLED=false`, the server continues to operate without payment headers for local development or trusted deployments.
+19 -2
View File
@@ -83,8 +83,10 @@ class Server {
// Apply x402 middleware based on configuration
// Logic:
// - If X402_ENABLED=false OR USE_BASIC_AUTH=false: Don't apply x402 (no rate limits)
// - If X402_ENABLED=true AND USE_BASIC_AUTH=true: Apply x402 conditionally (bypass if basic auth valid)
// - If X402_ENABLED=true AND USE_BASIC_AUTH=false: Apply x402 unconditionally (no basic auth bypass)
// - If X402_ENABLED=false AND USE_BASIC_AUTH=true: Require basic auth only
// - If X402_ENABLED=false AND USE_BASIC_AUTH=false: No access control
// Apply access control middleware based on configuration
if (x402Settings.enabled && basicAuthSettings.enabled) {
@@ -112,6 +114,21 @@ class Server {
}
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)
const facilitatorOptions = x402Settings.facilitatorUrl
? { url: x402Settings.facilitatorUrl }
: undefined
wlogger.info(`x402 middleware enabled (basic auth disabled); enforcing ${x402Settings.priceSat} satoshis per request`)
// Apply x402 middleware unconditionally - no basic auth bypass
app.use(x402PaymentMiddleware(
x402Settings.serverAddress,
routes,
facilitatorOptions
))
} 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)')
@@ -144,7 +161,7 @@ class Server {
// Endpoint logging middleware
app.use((req, res, next) => {
console.log(`Endpoint called: ${req.method} ${req.path}`)
console.log(`Endpoint called: ${req.method} ${req.path} by ${req.ip}`)
res.on('finish', () => {
console.log(`Endpoint responded: ${req.method} ${req.path} - ${res.statusCode}`)
})
+1000 -685
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -15,17 +15,17 @@
"license": "MIT",
"description": "REST API proxy to Bitcoin Cash infrastructure",
"dependencies": {
"@psf/bch-js": "7.1.0",
"@psf/bch-js": "7.1.2",
"axios": "1.7.7",
"cors": "2.8.5",
"dotenv": "16.3.1",
"express": "5.1.0",
"minimal-slp-wallet": "7.0.1",
"minimal-slp-wallet": "7.0.2",
"psffpp": "1.2.1",
"slp-token-media": "1.2.10",
"winston": "3.11.0",
"winston-daily-rotate-file": "4.7.1",
"x402-bch-express": "1.1.1"
"x402-bch-express": "1.1.3"
},
"devDependencies": {
"apidoc": "1.2.0",
@@ -9,10 +9,10 @@ RPC_PASSWORD=password
FULCRUM_API=http://172.17.0.1:3001/v1
# SLP Indexer
SLP_INDEXER_API=http://localhost:5010
SLP_INDEXER_API=http://172.17.0.1:5010
# REST API URL for wallet operations
LOCAL_RESTURL=http://localhost:5942/v6
LOCAL_RESTURL=http://172.17.0.1:5942/v6
# END INFRASTRUCTURE SETUP
@@ -22,13 +22,16 @@ LOCAL_RESTURL=http://localhost:5942/v6
PORT=5942
# x402 payments required to access this API?
X402_ENABLED=true
SERVER_BCH_ADDRESS=bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d
FACILITATOR_URL=http://localhost:4345/facilitator
X402_ENABLED=false
#X402_ENABLED=true
#SERVER_BCH_ADDRESS=bitcoincash:qqlrzp23w08434twmvr4fxw672whkjy0py26r63g3d
#FACILITATOR_URL=http://localhost:4345/facilitator
#X402_PRICE_SAT=200
# Basic Authentication required to access this API?
USE_BASIC_AUTH=true
BASIC_AUTH_TOKEN=some-random-token
USE_BASIC_AUTH=false
#USE_BASIC_AUTH=true
#BASIC_AUTH_TOKEN=some-random-token
# END ACCESS CONTROL
+3 -1
View File
@@ -51,6 +51,8 @@ RUN git clone https://github.com/Permissionless-Software-Foundation/psf-bch-api
# and `stage` has the most up-to-date changes.
WORKDIR /home/safeuser/psf-bch-api
RUN git checkout ct-unstable
# Install dependencies
RUN npm install
RUN npm install minimal-slp-wallet
@@ -58,7 +60,7 @@ RUN npm install minimal-slp-wallet
# Generate the API docs
RUN npm run docs
COPY .env-local .env
COPY .env .env
CMD ["npm", "start"]
-3
View File
@@ -1,3 +0,0 @@
#!/bin/bash
npm start
-7
View File
@@ -1,7 +0,0 @@
// Simple Node.js app that prints 'hello world' every 10 seconds
setInterval(() => {
console.log('hello world')
}, 10000)
console.log('Timer started. Printing "hello world" every 10 seconds...')
+2 -2
View File
@@ -26,10 +26,10 @@ const normalizeBoolean = (value, defaultValue) => {
return defaultValue
}
// By default, the price per API call is 2000 satoshis.
// By default, the price per API call is 200 satoshis.
// But the user can override this value by setting the X402_PRICE_SAT environment variable.
const parsedPriceSat = Number(process.env.X402_PRICE_SAT)
const priceSat = Number.isFinite(parsedPriceSat) && parsedPriceSat > 0 ? parsedPriceSat : 2000
const priceSat = Number.isFinite(parsedPriceSat) && parsedPriceSat > 0 ? parsedPriceSat : 200
const x402Defaults = {
enabled: normalizeBoolean(process.env.X402_ENABLED, true),
+1 -1
View File
@@ -26,7 +26,7 @@ export function buildX402Routes (apiPrefix = '/v6') {
price: config.x402.priceSat,
network: NETWORK,
config: {
description: `${DEFAULT_DESCRIPTION} (2000 satoshis)`,
description: `${DEFAULT_DESCRIPTION} (${config.x402.priceSat} satoshis)`,
maxTimeoutSeconds: DEFAULT_TIMEOUT_SECONDS
}
}
+5 -2
View File
@@ -6,7 +6,10 @@ import wlogger from '../adapters/wlogger.js'
import BCHJS from '@psf/bch-js'
import config from '../config/index.js'
const bchjs = new BCHJS({ restURL: config.restURL })
const bchjs = new BCHJS({
restURL: config.restURL,
bearerToken: config.basicAuth.token
})
class FulcrumUseCases {
constructor (localConfig = {}) {
@@ -56,7 +59,7 @@ class FulcrumUseCases {
async getTransactionDetails ({ txid }) {
try {
const response = await this.fulcrum.get(`electrumx/tx/data/${txid}`)
console.log(`getTransactionDetails() TXID ${txid}: ${JSON.stringify(response, null, 2)}`)
// console.log(`getTransactionDetails() TXID ${txid}: ${JSON.stringify(response, null, 2)}`)
return response
} catch (err) {
wlogger.error('Error in FulcrumUseCases.getTransactionDetails()', err)
+1 -1
View File
@@ -274,7 +274,7 @@ class SlpUseCases {
// Get transaction data
console.log('Decoding OP_RETURN for TXID: ', txid)
const txData = await this.bchjs.Electrumx.txData(txid)
console.log(`TXID ${txid}: ${JSON.stringify(txData, null, 2)}`)
// console.log(`TXID ${txid}: ${JSON.stringify(txData, null, 2)}`)
let data = false
// Map the vout of the transaction in search of an OP_RETURN