mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api.git
synced 2026-09-22 09:02:01 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e32723b29 | ||
|
|
3b08fc3b62 | ||
|
|
2c1f02d6c7 | ||
|
|
6cf03ca0fe | ||
|
|
5ea7bc9c29 | ||
|
|
39021aba46 | ||
|
|
de34547951 | ||
|
|
b8f34fb40e | ||
|
|
3778894ce2 | ||
|
|
02eb8afd21 | ||
|
|
e708fc1228 | ||
|
|
fed4b2da59 | ||
|
|
f30c2ede04 | ||
|
|
09e05d51e5 | ||
|
|
22cbc54dee | ||
|
|
6fe0e01e8b | ||
|
|
f33b39ef01 | ||
|
|
6d27630393 | ||
|
|
eac4916415 | ||
|
|
baa1170b89 | ||
|
|
23ce276e19 | ||
|
|
f28e2c6a1a |
+14
-1
@@ -74,6 +74,19 @@ class Server {
|
||||
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
|
||||
}))
|
||||
|
||||
// URL normalization middleware - collapse multiple slashes
|
||||
app.use((req, res, next) => {
|
||||
if (req.url && req.url.includes('//')) {
|
||||
// Split URL into path and query string
|
||||
const [path, queryString] = req.url.split('?')
|
||||
// Collapse multiple consecutive slashes into a single slash
|
||||
const normalizedPath = path.replace(/\/+/g, '/')
|
||||
// Reconstruct req.url with normalized path (req.path is read-only and will auto-update)
|
||||
req.url = queryString ? `${normalizedPath}?${queryString}` : normalizedPath
|
||||
}
|
||||
next()
|
||||
})
|
||||
|
||||
// Apply basic auth middleware if enabled
|
||||
// This must run before x402 middleware to set req.locals.basicAuthValid
|
||||
if (basicAuthSettings.enabled) {
|
||||
@@ -161,7 +174,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}`)
|
||||
})
|
||||
|
||||
Generated
+1089
-989
File diff suppressed because it is too large
Load Diff
+3
-3
@@ -15,17 +15,17 @@
|
||||
"license": "MIT",
|
||||
"description": "REST API proxy to Bitcoin Cash infrastructure",
|
||||
"dependencies": {
|
||||
"@psf/bch-js": "7.1.2",
|
||||
"@psf/bch-js": "7.1.11",
|
||||
"axios": "1.7.7",
|
||||
"cors": "2.8.5",
|
||||
"dotenv": "16.3.1",
|
||||
"express": "5.1.0",
|
||||
"minimal-slp-wallet": "7.0.2",
|
||||
"minimal-slp-wallet": "7.1.4",
|
||||
"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": "2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"apidoc": "1.2.0",
|
||||
|
||||
@@ -22,14 +22,16 @@ LOCAL_RESTURL=http://172.17.0.1: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_PRICE_SAT=200
|
||||
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
|
||||
|
||||
|
||||
@@ -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"]
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
npm start
|
||||
@@ -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...')
|
||||
@@ -424,7 +424,20 @@ class FulcrumRESTController {
|
||||
|
||||
const cashAddr = this._validateAndConvertAddress(address)
|
||||
|
||||
const result = await this.fulcrumUseCases.getTransactions({ address: cashAddr, allTxs })
|
||||
// Extract bearer token from request header if present
|
||||
let bearerToken = null
|
||||
if (req.headers && req.headers.authorization) {
|
||||
const parts = req.headers.authorization.split(' ')
|
||||
if (parts.length === 2 && parts[0] === 'Bearer') {
|
||||
bearerToken = parts[1]
|
||||
}
|
||||
}
|
||||
|
||||
const result = await this.fulcrumUseCases.getTransactions({
|
||||
address: cashAddr,
|
||||
allTxs,
|
||||
bearerToken
|
||||
})
|
||||
return res.status(200).json(result)
|
||||
} catch (err) {
|
||||
return this.handleError(err, res)
|
||||
@@ -470,9 +483,19 @@ class FulcrumRESTController {
|
||||
}
|
||||
}
|
||||
|
||||
// Extract bearer token from request header if present
|
||||
let bearerToken = null
|
||||
if (req.headers && req.headers.authorization) {
|
||||
const parts = req.headers.authorization.split(' ')
|
||||
if (parts.length === 2 && parts[0] === 'Bearer') {
|
||||
bearerToken = parts[1]
|
||||
}
|
||||
}
|
||||
|
||||
const result = await this.fulcrumUseCases.getTransactionsBulk({
|
||||
addresses: validatedAddresses,
|
||||
allTxs
|
||||
allTxs,
|
||||
bearerToken
|
||||
})
|
||||
return res.status(200).json(result)
|
||||
} catch (err) {
|
||||
|
||||
@@ -6,9 +6,14 @@ 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,
|
||||
bearerToken: config.basicAuth.token
|
||||
// Use RESTURL (from test) or REST_URL (from psf-bch-api config) or fallback to config
|
||||
const restURL = process.env.RESTURL || process.env.REST_URL || process.env.LOCAL_RESTURL || config.restURL
|
||||
// Use BCHJSBEARERTOKEN (from test) or BASIC_AUTH_TOKEN (from psf-bch-api config) or fallback to config
|
||||
const bearerToken = process.env.BCHJSBEARERTOKEN || process.env.BASIC_AUTH_TOKEN || config.basicAuth.token
|
||||
|
||||
const bchjs = new BCHJS({
|
||||
restURL,
|
||||
bearerToken
|
||||
})
|
||||
|
||||
class FulcrumUseCases {
|
||||
@@ -101,13 +106,24 @@ class FulcrumUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
async getTransactions ({ address, allTxs }) {
|
||||
async getTransactions ({ address, allTxs, bearerToken = null }) {
|
||||
try {
|
||||
const response = await this.fulcrum.get(`electrumx/transactions/${address}`)
|
||||
|
||||
// Sort transactions in descending order, so that newest transactions are first.
|
||||
if (response.transactions && Array.isArray(response.transactions)) {
|
||||
response.transactions = await this.bchjs.Electrumx.sortAllTxs(response.transactions, 'DESCENDING')
|
||||
// Use bearer token from request if provided, otherwise use the default bchjs instance
|
||||
let bchjsInstance = this.bchjs
|
||||
if (bearerToken) {
|
||||
// Create a temporary bchjs instance with the bearer token from the request
|
||||
const restURL = process.env.RESTURL || process.env.REST_URL || process.env.LOCAL_RESTURL || config.restURL
|
||||
bchjsInstance = new BCHJS({
|
||||
restURL,
|
||||
bearerToken
|
||||
})
|
||||
}
|
||||
|
||||
response.transactions = await bchjsInstance.Electrumx.sortAllTxs(response.transactions, 'DESCENDING')
|
||||
|
||||
if (!allTxs) {
|
||||
// Return only the first 100 transactions of the history.
|
||||
@@ -122,16 +138,28 @@ class FulcrumUseCases {
|
||||
}
|
||||
}
|
||||
|
||||
async getTransactionsBulk ({ addresses, allTxs }) {
|
||||
async getTransactionsBulk ({ addresses, allTxs, bearerToken = null }) {
|
||||
try {
|
||||
const response = await this.fulcrum.post('electrumx/transactions/', { addresses })
|
||||
|
||||
// Sort transactions in descending order for each address entry.
|
||||
if (response.transactions && Array.isArray(response.transactions)) {
|
||||
// Use bearer token from request if provided, otherwise use the default bchjs instance
|
||||
let bchjsInstance = this.bchjs
|
||||
console.log('getTransactionsBulk() bearerToken: ', bearerToken)
|
||||
if (bearerToken) {
|
||||
// Create a temporary bchjs instance with the bearer token from the request
|
||||
const restURL = process.env.RESTURL || process.env.REST_URL || process.env.LOCAL_RESTURL || config.restURL
|
||||
bchjsInstance = new BCHJS({
|
||||
restURL,
|
||||
bearerToken
|
||||
})
|
||||
}
|
||||
|
||||
for (let i = 0; i < response.transactions.length; i++) {
|
||||
const thisEntry = response.transactions[i]
|
||||
if (thisEntry.transactions && Array.isArray(thisEntry.transactions)) {
|
||||
thisEntry.transactions = await this.bchjs.Electrumx.sortAllTxs(thisEntry.transactions, 'DESCENDING')
|
||||
thisEntry.transactions = await bchjsInstance.Electrumx.sortAllTxs(thisEntry.transactions, 'DESCENDING')
|
||||
|
||||
if (!allTxs && thisEntry.transactions.length > 100) {
|
||||
// Extract only the first 100 transactions.
|
||||
|
||||
Reference in New Issue
Block a user