Update server/status.js
Add paginated address transaction API
This commit is contained in:
+145
-57
@@ -230,7 +230,9 @@ function queryFulcrum(
|
||||
params,
|
||||
}) + '\n'
|
||||
|
||||
console.log(`Sending Fulcrum request: ${request.trim()}`)
|
||||
console.log(
|
||||
`Sending Fulcrum request: ${request.trim()}`,
|
||||
)
|
||||
|
||||
socket.write(request)
|
||||
})
|
||||
@@ -238,7 +240,9 @@ function queryFulcrum(
|
||||
socket.on('data', (data) => {
|
||||
const chunk = data.toString()
|
||||
|
||||
console.log(`Fulcrum response data: ${chunk.trim()}`)
|
||||
console.log(
|
||||
`Fulcrum response data: ${chunk.trim()}`,
|
||||
)
|
||||
|
||||
response += chunk
|
||||
|
||||
@@ -321,15 +325,19 @@ function queryFulcrum(
|
||||
finish({
|
||||
ok: false,
|
||||
result: null,
|
||||
error: 'Connection closed before a response was received',
|
||||
error:
|
||||
'Connection closed before a response was received',
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function getFulcrumStatus(fulcrumUrl) {
|
||||
const version = await queryFulcrum(
|
||||
async function getFulcrumStatus(
|
||||
fulcrumUrl,
|
||||
) {
|
||||
const version =
|
||||
await queryFulcrum(
|
||||
fulcrumUrl,
|
||||
'server.version',
|
||||
['Munin Bitcoin', '1.4'],
|
||||
@@ -337,7 +345,9 @@ async function getFulcrumStatus(fulcrumUrl) {
|
||||
|
||||
if (!version.ok) {
|
||||
return {
|
||||
configured: Boolean(fulcrumUrl),
|
||||
configured: Boolean(
|
||||
fulcrumUrl,
|
||||
),
|
||||
connected: false,
|
||||
url: fulcrumUrl,
|
||||
serverVersion: null,
|
||||
@@ -346,28 +356,43 @@ async function getFulcrumStatus(fulcrumUrl) {
|
||||
}
|
||||
}
|
||||
|
||||
const headers = await queryFulcrum(
|
||||
const headers =
|
||||
await queryFulcrum(
|
||||
fulcrumUrl,
|
||||
'blockchain.headers.subscribe',
|
||||
[],
|
||||
)
|
||||
|
||||
return {
|
||||
configured: Boolean(fulcrumUrl),
|
||||
configured: Boolean(
|
||||
fulcrumUrl,
|
||||
),
|
||||
connected: true,
|
||||
url: fulcrumUrl,
|
||||
serverVersion: version.result,
|
||||
serverVersion:
|
||||
version.result,
|
||||
blockchainHeight:
|
||||
headers.ok && headers.result
|
||||
? headers.result.height ?? null
|
||||
headers.ok &&
|
||||
headers.result
|
||||
? headers.result.height ??
|
||||
null
|
||||
: null,
|
||||
error: headers.ok ? null : headers.error,
|
||||
error:
|
||||
headers.ok
|
||||
? null
|
||||
: headers.error,
|
||||
}
|
||||
}
|
||||
|
||||
async function getStatus() {
|
||||
const fulcrumUrl = process.env.MUNIN_FULCRUM_URL || null
|
||||
const fulcrum = await getFulcrumStatus(fulcrumUrl)
|
||||
const fulcrumUrl =
|
||||
process.env.MUNIN_FULCRUM_URL ||
|
||||
null
|
||||
|
||||
const fulcrum =
|
||||
await getFulcrumStatus(
|
||||
fulcrumUrl,
|
||||
)
|
||||
|
||||
return {
|
||||
service: 'running',
|
||||
@@ -384,7 +409,8 @@ async function getAddressDataFromFulcrum(
|
||||
scripthash,
|
||||
) {
|
||||
const fulcrumUrl =
|
||||
process.env.MUNIN_FULCRUM_URL || null
|
||||
process.env.MUNIN_FULCRUM_URL ||
|
||||
null
|
||||
|
||||
if (!fulcrumUrl) {
|
||||
throw new Error(
|
||||
@@ -392,7 +418,8 @@ async function getAddressDataFromFulcrum(
|
||||
)
|
||||
}
|
||||
|
||||
const balance = await queryFulcrum(
|
||||
const balance =
|
||||
await queryFulcrum(
|
||||
fulcrumUrl,
|
||||
'blockchain.scripthash.get_balance',
|
||||
[scripthash],
|
||||
@@ -404,7 +431,8 @@ async function getAddressDataFromFulcrum(
|
||||
)
|
||||
}
|
||||
|
||||
const utxos = await queryFulcrum(
|
||||
const utxos =
|
||||
await queryFulcrum(
|
||||
fulcrumUrl,
|
||||
'blockchain.scripthash.listunspent',
|
||||
[scripthash],
|
||||
@@ -416,7 +444,8 @@ async function getAddressDataFromFulcrum(
|
||||
)
|
||||
}
|
||||
|
||||
const history = await queryFulcrum(
|
||||
const history =
|
||||
await queryFulcrum(
|
||||
fulcrumUrl,
|
||||
'blockchain.scripthash.get_history',
|
||||
[scripthash],
|
||||
@@ -432,15 +461,25 @@ async function getAddressDataFromFulcrum(
|
||||
address,
|
||||
scripthash,
|
||||
confirmedBalance:
|
||||
Number(balance.result?.confirmed || 0),
|
||||
Number(
|
||||
balance.result?.confirmed ||
|
||||
0,
|
||||
),
|
||||
unconfirmedBalance:
|
||||
Number(balance.result?.unconfirmed || 0),
|
||||
Number(
|
||||
balance.result?.unconfirmed ||
|
||||
0,
|
||||
),
|
||||
utxos:
|
||||
Array.isArray(utxos.result)
|
||||
Array.isArray(
|
||||
utxos.result,
|
||||
)
|
||||
? utxos.result
|
||||
: [],
|
||||
history:
|
||||
Array.isArray(history.result)
|
||||
Array.isArray(
|
||||
history.result,
|
||||
)
|
||||
? history.result
|
||||
: [],
|
||||
lastSynced:
|
||||
@@ -448,12 +487,16 @@ async function getAddressDataFromFulcrum(
|
||||
}
|
||||
}
|
||||
|
||||
async function getAddressData(address) {
|
||||
async function getAddressData(
|
||||
address,
|
||||
) {
|
||||
let scripthash
|
||||
|
||||
try {
|
||||
scripthash =
|
||||
addressToScripthash(address)
|
||||
addressToScripthash(
|
||||
address,
|
||||
)
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
error instanceof Error
|
||||
@@ -463,7 +506,9 @@ async function getAddressData(address) {
|
||||
}
|
||||
|
||||
const cached =
|
||||
readAddressCache(scripthash)
|
||||
readAddressCache(
|
||||
scripthash,
|
||||
)
|
||||
|
||||
if (cached) {
|
||||
if (
|
||||
@@ -498,7 +543,8 @@ async function getAddressData(address) {
|
||||
function getAddressTransactionPage(
|
||||
history,
|
||||
offset = 0,
|
||||
limit = addressTransactionPageSize,
|
||||
limit =
|
||||
addressTransactionPageSize,
|
||||
) {
|
||||
const safeOffset =
|
||||
Math.max(
|
||||
@@ -523,7 +569,9 @@ function getAddressTransactionPage(
|
||||
}
|
||||
|
||||
async function getAddressesWithData() {
|
||||
const addresses = await listAddresses()
|
||||
const addresses =
|
||||
await listAddresses()
|
||||
|
||||
const results = []
|
||||
|
||||
for (const entry of addresses) {
|
||||
@@ -534,7 +582,9 @@ async function getAddressesWithData() {
|
||||
)
|
||||
|
||||
const history =
|
||||
Array.isArray(data.history)
|
||||
Array.isArray(
|
||||
data.history,
|
||||
)
|
||||
? data.history
|
||||
: []
|
||||
|
||||
@@ -558,7 +608,8 @@ async function getAddressesWithData() {
|
||||
transactionLimit:
|
||||
addressTransactionPageSize,
|
||||
lastSynced:
|
||||
data.lastSynced || null,
|
||||
data.lastSynced ||
|
||||
null,
|
||||
cacheAgeMs:
|
||||
Number(
|
||||
data.cacheAgeMs || 0,
|
||||
@@ -568,10 +619,8 @@ async function getAddressesWithData() {
|
||||
} catch (error) {
|
||||
results.push({
|
||||
...entry,
|
||||
confirmedBalance:
|
||||
null,
|
||||
unconfirmedBalance:
|
||||
null,
|
||||
confirmedBalance: null,
|
||||
unconfirmedBalance: null,
|
||||
utxos: [],
|
||||
history: [],
|
||||
transactionCount: 0,
|
||||
@@ -591,7 +640,8 @@ async function getAddressesWithData() {
|
||||
return results
|
||||
}
|
||||
|
||||
const server = http.createServer(
|
||||
const server =
|
||||
http.createServer(
|
||||
async (req, res) => {
|
||||
if (
|
||||
req.method === 'POST' &&
|
||||
@@ -636,7 +686,9 @@ const server = http.createServer(
|
||||
'application/json',
|
||||
'Set-Cookie':
|
||||
'munin_session=' +
|
||||
encodeURIComponent(token) +
|
||||
encodeURIComponent(
|
||||
token,
|
||||
) +
|
||||
'; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400',
|
||||
})
|
||||
|
||||
@@ -663,8 +715,13 @@ const server = http.createServer(
|
||||
return
|
||||
}
|
||||
|
||||
if (req.url === '/api/status') {
|
||||
if (!isValidSession(req)) {
|
||||
if (
|
||||
req.url ===
|
||||
'/api/status'
|
||||
) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -690,7 +747,9 @@ const server = http.createServer(
|
||||
})
|
||||
|
||||
res.end(
|
||||
JSON.stringify(status),
|
||||
JSON.stringify(
|
||||
status,
|
||||
),
|
||||
)
|
||||
} catch (error) {
|
||||
console.error(
|
||||
@@ -705,7 +764,8 @@ const server = http.createServer(
|
||||
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
service: 'running',
|
||||
service:
|
||||
'running',
|
||||
error:
|
||||
'Failed to determine service status',
|
||||
}),
|
||||
@@ -717,9 +777,12 @@ const server = http.createServer(
|
||||
|
||||
if (
|
||||
req.method === 'GET' &&
|
||||
req.url === '/api/addresses'
|
||||
req.url ===
|
||||
'/api/addresses'
|
||||
) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
if (!isValidSession(req)) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -773,9 +836,12 @@ const server = http.createServer(
|
||||
|
||||
if (
|
||||
req.method === 'GET' &&
|
||||
req.url === '/api/addresses/data'
|
||||
req.url ===
|
||||
'/api/addresses/data'
|
||||
) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
if (!isValidSession(req)) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -833,7 +899,9 @@ const server = http.createServer(
|
||||
'/api/addresses/transactions',
|
||||
)
|
||||
) {
|
||||
if (!isValidSession(req)) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -853,7 +921,10 @@ const server = http.createServer(
|
||||
const url =
|
||||
new URL(
|
||||
req.url,
|
||||
`http://${req.headers.host || 'localhost'}`,
|
||||
`http://${
|
||||
req.headers.host ||
|
||||
'localhost'
|
||||
}`,
|
||||
)
|
||||
|
||||
const address =
|
||||
@@ -934,7 +1005,8 @@ const server = http.createServer(
|
||||
null,
|
||||
cacheAgeMs:
|
||||
Number(
|
||||
data.cacheAgeMs || 0,
|
||||
data.cacheAgeMs ||
|
||||
0,
|
||||
),
|
||||
}),
|
||||
)
|
||||
@@ -964,9 +1036,12 @@ const server = http.createServer(
|
||||
|
||||
if (
|
||||
req.method === 'POST' &&
|
||||
req.url === '/api/addresses'
|
||||
req.url ===
|
||||
'/api/addresses'
|
||||
) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
if (!isValidSession(req)) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -1007,7 +1082,9 @@ const server = http.createServer(
|
||||
})
|
||||
|
||||
res.end(
|
||||
JSON.stringify(entry),
|
||||
JSON.stringify(
|
||||
entry,
|
||||
),
|
||||
)
|
||||
} catch (error) {
|
||||
res.writeHead(400, {
|
||||
@@ -1032,9 +1109,12 @@ const server = http.createServer(
|
||||
|
||||
if (
|
||||
req.method === 'DELETE' &&
|
||||
req.url === '/api/addresses'
|
||||
req.url ===
|
||||
'/api/addresses'
|
||||
) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
if (!isValidSession(req)) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -1104,7 +1184,9 @@ const server = http.createServer(
|
||||
'/api/transactions/',
|
||||
)
|
||||
) {
|
||||
if (!isValidSession(req)) {
|
||||
if (
|
||||
!isValidSession(req)
|
||||
) {
|
||||
res.writeHead(401, {
|
||||
'Content-Type':
|
||||
'application/json',
|
||||
@@ -1123,13 +1205,16 @@ const server = http.createServer(
|
||||
const txid =
|
||||
decodeURIComponent(
|
||||
req.url.slice(
|
||||
'/api/transactions/'.length,
|
||||
'/api/transactions/'
|
||||
.length,
|
||||
),
|
||||
)
|
||||
|
||||
try {
|
||||
const transaction =
|
||||
await getTransaction(txid)
|
||||
await getTransaction(
|
||||
txid,
|
||||
)
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type':
|
||||
@@ -1163,10 +1248,13 @@ const server = http.createServer(
|
||||
res.writeHead(404)
|
||||
res.end('Not found')
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
server.listen(port, () => {
|
||||
server.listen(
|
||||
port,
|
||||
() => {
|
||||
console.log(
|
||||
`Status API listening on port ${port}`,
|
||||
)
|
||||
})
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user