Merge pull request #18 from Permissionless-Software-Foundation/ct-unstable

fix(auth token): Passing auth token to internal bch-js
This commit is contained in:
Chris Troutner
2026-02-04 14:32:04 -07:00
committed by GitHub
2 changed files with 51 additions and 6 deletions
+25 -2
View File
@@ -424,7 +424,20 @@ class FulcrumRESTController {
const cashAddr = this._validateAndConvertAddress(address) 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) return res.status(200).json(result)
} catch (err) { } catch (err) {
return this.handleError(err, res) 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({ const result = await this.fulcrumUseCases.getTransactionsBulk({
addresses: validatedAddresses, addresses: validatedAddresses,
allTxs allTxs,
bearerToken
}) })
return res.status(200).json(result) return res.status(200).json(result)
} catch (err) { } catch (err) {
+26 -4
View File
@@ -106,13 +106,24 @@ class FulcrumUseCases {
} }
} }
async getTransactions ({ address, allTxs }) { async getTransactions ({ address, allTxs, bearerToken = null }) {
try { try {
const response = await this.fulcrum.get(`electrumx/transactions/${address}`) const response = await this.fulcrum.get(`electrumx/transactions/${address}`)
// Sort transactions in descending order, so that newest transactions are first. // Sort transactions in descending order, so that newest transactions are first.
if (response.transactions && Array.isArray(response.transactions)) { 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) { if (!allTxs) {
// Return only the first 100 transactions of the history. // Return only the first 100 transactions of the history.
@@ -127,16 +138,27 @@ class FulcrumUseCases {
} }
} }
async getTransactionsBulk ({ addresses, allTxs }) { async getTransactionsBulk ({ addresses, allTxs, bearerToken = null }) {
try { try {
const response = await this.fulcrum.post('electrumx/transactions/', { addresses }) const response = await this.fulcrum.post('electrumx/transactions/', { addresses })
// Sort transactions in descending order for each address entry. // Sort transactions in descending order for each address entry.
if (response.transactions && Array.isArray(response.transactions)) { if (response.transactions && Array.isArray(response.transactions)) {
// 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
})
}
for (let i = 0; i < response.transactions.length; i++) { for (let i = 0; i < response.transactions.length; i++) {
const thisEntry = response.transactions[i] const thisEntry = response.transactions[i]
if (thisEntry.transactions && Array.isArray(thisEntry.transactions)) { 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) { if (!allTxs && thisEntry.transactions.length > 100) {
// Extract only the first 100 transactions. // Extract only the first 100 transactions.