mirror of
https://github.com/Permissionless-Software-Foundation/psf-bch-api.git
synced 2026-09-22 01:01:59 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e32723b29 | ||
|
|
3b08fc3b62 | ||
|
|
2c1f02d6c7 |
@@ -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) {
|
||||
|
||||
@@ -106,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.
|
||||
@@ -127,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