Update server/status.js

Add paginated address transaction API
This commit is contained in:
2026-08-29 15:28:26 +00:00
parent 9593b8ec62
commit 515ecf0ea0
+336 -67
View File
@@ -27,6 +27,7 @@ const path = require('path')
const addressCacheDir = '/data/cache/addresses' const addressCacheDir = '/data/cache/addresses'
const addressCacheMaxAgeMs = 60000 const addressCacheMaxAgeMs = 60000
const addressTransactionPageSize = 50
const addressRefreshes = new Map() const addressRefreshes = new Map()
function ensureAddressCacheDir() { function ensureAddressCacheDir() {
@@ -149,7 +150,11 @@ function refreshAddressCache(
return refresh return refresh
} }
function queryFulcrum(fulcrumUrl, method, params = []) { function queryFulcrum(
fulcrumUrl,
method,
params = [],
) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (!fulcrumUrl) { if (!fulcrumUrl) {
resolve({ resolve({
@@ -490,34 +495,89 @@ async function getAddressData(address) {
} }
} }
function getAddressTransactionPage(
history,
offset = 0,
limit = addressTransactionPageSize,
) {
const safeOffset =
Math.max(
0,
Number(offset) || 0,
)
const safeLimit =
Math.min(
addressTransactionPageSize,
Math.max(
1,
Number(limit) ||
addressTransactionPageSize,
),
)
return history.slice(
safeOffset,
safeOffset + safeLimit,
)
}
async function getAddressesWithData() { async function getAddressesWithData() {
const addresses = await listAddresses() const addresses = await listAddresses()
const results = [] const results = []
for (const entry of addresses) { for (const entry of addresses) {
try { try {
const data = await getAddressData(entry.address) const data =
await getAddressData(
entry.address,
)
const history =
Array.isArray(data.history)
? data.history
: []
results.push({ results.push({
...entry, ...entry,
scripthash: data.scripthash, scripthash:
confirmedBalance: data.confirmedBalance, data.scripthash,
unconfirmedBalance: data.unconfirmedBalance, confirmedBalance:
utxos: data.utxos, data.confirmedBalance,
history: data.history, unconfirmedBalance:
data.unconfirmedBalance,
utxos:
data.utxos,
history:
getAddressTransactionPage(
history,
),
transactionCount:
history.length,
transactionOffset: 0,
transactionLimit:
addressTransactionPageSize,
lastSynced: lastSynced:
data.lastSynced || null, data.lastSynced || null,
cacheAgeMs: cacheAgeMs:
Number(data.cacheAgeMs || 0), Number(
data.cacheAgeMs || 0,
),
error: null, error: null,
}) })
} catch (error) { } catch (error) {
results.push({ results.push({
...entry, ...entry,
confirmedBalance: null, confirmedBalance:
unconfirmedBalance: null, null,
unconfirmedBalance:
null,
utxos: [], utxos: [],
history: [], history: [],
transactionCount: 0,
transactionOffset: 0,
transactionLimit:
addressTransactionPageSize,
lastSynced: null, lastSynced: null,
cacheAgeMs: null, cacheAgeMs: null,
error: error:
@@ -531,8 +591,12 @@ async function getAddressesWithData() {
return results return results
} }
const server = http.createServer(async (req, res) => { const server = http.createServer(
if (req.method === 'POST' && req.url === '/api/login') { async (req, res) => {
if (
req.method === 'POST' &&
req.url === '/api/login'
) {
let body = '' let body = ''
req.on('data', (chunk) => { req.on('data', (chunk) => {
@@ -541,26 +605,35 @@ const server = http.createServer(async (req, res) => {
req.on('end', () => { req.on('end', () => {
try { try {
const input = JSON.parse(body) const input =
JSON.parse(body)
if (!checkPassword(input.password)) { if (
!checkPassword(
input.password,
)
) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Invalid password', error:
'Invalid password',
}), }),
) )
return return
} }
const token = createSession() const token =
createSession()
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
'Set-Cookie': 'Set-Cookie':
'munin_session=' + 'munin_session=' +
encodeURIComponent(token) + encodeURIComponent(token) +
@@ -574,12 +647,14 @@ const server = http.createServer(async (req, res) => {
) )
} catch { } catch {
res.writeHead(400, { res.writeHead(400, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Invalid request', error:
'Invalid request',
}), }),
) )
} }
@@ -591,12 +666,14 @@ const server = http.createServer(async (req, res) => {
if (req.url === '/api/status') { if (req.url === '/api/status') {
if (!isValidSession(req)) { if (!isValidSession(req)) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -604,13 +681,17 @@ const server = http.createServer(async (req, res) => {
} }
try { try {
const status = await getStatus() const status =
await getStatus()
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end(JSON.stringify(status)) res.end(
JSON.stringify(status),
)
} catch (error) { } catch (error) {
console.error( console.error(
'Failed to get service status:', 'Failed to get service status:',
@@ -618,13 +699,15 @@ const server = http.createServer(async (req, res) => {
) )
res.writeHead(500, { res.writeHead(500, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
service: 'running', service: 'running',
error: 'Failed to determine service status', error:
'Failed to determine service status',
}), }),
) )
} }
@@ -632,15 +715,20 @@ const server = http.createServer(async (req, res) => {
return return
} }
if (req.method === 'GET' && req.url === '/api/addresses') { if (
req.method === 'GET' &&
req.url === '/api/addresses'
) {
if (!isValidSession(req)) { if (!isValidSession(req)) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -648,10 +736,12 @@ const server = http.createServer(async (req, res) => {
} }
try { try {
const addresses = await listAddresses() const addresses =
await listAddresses()
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
@@ -666,12 +756,14 @@ const server = http.createServer(async (req, res) => {
) )
res.writeHead(500, { res.writeHead(500, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Failed to list addresses', error:
'Failed to list addresses',
}), }),
) )
} }
@@ -685,12 +777,14 @@ const server = http.createServer(async (req, res) => {
) { ) {
if (!isValidSession(req)) { if (!isValidSession(req)) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -698,10 +792,12 @@ const server = http.createServer(async (req, res) => {
} }
try { try {
const addresses = await getAddressesWithData() const addresses =
await getAddressesWithData()
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
@@ -716,12 +812,149 @@ const server = http.createServer(async (req, res) => {
) )
res.writeHead(500, { res.writeHead(500, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Failed to get address data', error:
'Failed to get address data',
}),
)
}
return
}
if (
req.method === 'GET' &&
req.url.startsWith(
'/api/addresses/transactions',
)
) {
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type':
'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
}),
)
return
}
try {
const url =
new URL(
req.url,
`http://${req.headers.host || 'localhost'}`,
)
const address =
url.searchParams.get(
'address',
)
if (!address) {
throw new Error(
'Address is required',
)
}
const offset =
Math.max(
0,
Number(
url.searchParams.get(
'offset',
),
) || 0,
)
const limit =
Math.min(
addressTransactionPageSize,
Math.max(
1,
Number(
url.searchParams.get(
'limit',
),
) ||
addressTransactionPageSize,
),
)
const data =
await getAddressData(
address,
)
const history =
Array.isArray(
data.history,
)
? data.history
: []
const transactions =
getAddressTransactionPage(
history,
offset,
limit,
)
res.writeHead(200, {
'Content-Type':
'application/json',
})
res.end(
JSON.stringify({
address,
scripthash:
data.scripthash,
transactions,
total:
history.length,
offset,
limit,
hasMore:
offset +
transactions.length <
history.length,
lastSynced:
data.lastSynced ||
null,
cacheAgeMs:
Number(
data.cacheAgeMs || 0,
),
}),
)
} catch (error) {
console.error(
'Failed to get address transactions:',
error,
)
res.writeHead(400, {
'Content-Type':
'application/json',
})
res.end(
JSON.stringify({
error:
error instanceof Error
? error.message
: 'Failed to get address transactions',
}), }),
) )
} }
@@ -735,12 +968,14 @@ const server = http.createServer(async (req, res) => {
) { ) {
if (!isValidSession(req)) { if (!isValidSession(req)) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -753,23 +988,31 @@ const server = http.createServer(async (req, res) => {
body += chunk body += chunk
}) })
req.on('end', async () => { req.on(
'end',
async () => {
try { try {
const input = JSON.parse(body) const input =
JSON.parse(body)
const entry = await addAddress( const entry =
await addAddress(
input.address, input.address,
input.label, input.label,
) )
res.writeHead(201, { res.writeHead(201, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end(JSON.stringify(entry)) res.end(
JSON.stringify(entry),
)
} catch (error) { } catch (error) {
res.writeHead(400, { res.writeHead(400, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
@@ -781,7 +1024,8 @@ const server = http.createServer(async (req, res) => {
}), }),
) )
} }
}) },
)
return return
} }
@@ -792,12 +1036,14 @@ const server = http.createServer(async (req, res) => {
) { ) {
if (!isValidSession(req)) { if (!isValidSession(req)) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -810,14 +1056,20 @@ const server = http.createServer(async (req, res) => {
body += chunk body += chunk
}) })
req.on('end', async () => { req.on(
'end',
async () => {
try { try {
const input = JSON.parse(body) const input =
JSON.parse(body)
await removeAddress(input.address) await removeAddress(
input.address,
)
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
@@ -827,7 +1079,8 @@ const server = http.createServer(async (req, res) => {
) )
} catch (error) { } catch (error) {
res.writeHead(400, { res.writeHead(400, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
@@ -839,44 +1092,59 @@ const server = http.createServer(async (req, res) => {
}), }),
) )
} }
}) },
)
return return
} }
if ( if (
req.method === 'GET' && req.method === 'GET' &&
req.url.startsWith('/api/transactions/') req.url.startsWith(
'/api/transactions/',
)
) { ) {
if (!isValidSession(req)) { if (!isValidSession(req)) {
res.writeHead(401, { res.writeHead(401, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
return return
} }
const txid = decodeURIComponent( const txid =
req.url.slice('/api/transactions/'.length), decodeURIComponent(
req.url.slice(
'/api/transactions/'.length,
),
) )
try { try {
const transaction = await getTransaction(txid) const transaction =
await getTransaction(txid)
res.writeHead(200, { res.writeHead(200, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end(JSON.stringify(transaction)) res.end(
JSON.stringify(
transaction,
),
)
} catch (error) { } catch (error) {
res.writeHead(400, { res.writeHead(400, {
'Content-Type': 'application/json', 'Content-Type':
'application/json',
}) })
res.end( res.end(
@@ -894,7 +1162,8 @@ const server = http.createServer(async (req, res) => {
res.writeHead(404) res.writeHead(404)
res.end('Not found') res.end('Not found')
}) },
)
server.listen(port, () => { server.listen(port, () => {
console.log( console.log(