Update server/status.js

Add persistent server-side address cache
This commit is contained in:
2026-08-29 14:34:47 +00:00
parent c66bbfa618
commit 86b927b26d
+548 -150
View File
@@ -22,7 +22,138 @@ const {
const port = 8080 const port = 8080
const fulcrumTimeoutMs = 3000 const fulcrumTimeoutMs = 3000
function queryFulcrum(fulcrumUrl, method, params = []) { const fs = require('fs')
const path = require('path')
const addressCacheDir = '/data/cache/addresses'
const addressCacheMaxAgeMs = 60000
const addressRefreshes = new Map()
function ensureAddressCacheDir() {
fs.mkdirSync(addressCacheDir, {
recursive: true,
})
}
function getAddressCachePath(scripthash) {
return path.join(
addressCacheDir,
`${scripthash}.json`,
)
}
function readAddressCache(scripthash) {
try {
const cachePath =
getAddressCachePath(scripthash)
const stat = fs.statSync(cachePath)
const data =
JSON.parse(
fs.readFileSync(
cachePath,
'utf8',
),
)
if (
!data ||
typeof data !== 'object' ||
!data.address
) {
return null
}
return {
data,
ageMs: Math.max(
0,
Date.now() - stat.mtimeMs,
),
}
} catch {
return null
}
}
function writeAddressCache(data) {
try {
ensureAddressCacheDir()
const cachePath =
getAddressCachePath(
data.scripthash,
)
const tempPath =
`${cachePath}.${process.pid}.tmp`
fs.writeFileSync(
tempPath,
JSON.stringify(data),
'utf8',
)
fs.renameSync(
tempPath,
cachePath,
)
} catch (error) {
console.error(
'Failed to write address cache:',
error,
)
}
}
function refreshAddressCache(
address,
scripthash,
) {
const existing =
addressRefreshes.get(scripthash)
if (existing) {
return existing
}
const refresh =
getAddressDataFromFulcrum(
address,
scripthash,
)
.then((data) => {
writeAddressCache(data)
return data
})
.catch((error) => {
console.error(
`Failed to refresh address ${address}:`,
error,
)
throw error
})
.finally(() => {
addressRefreshes.delete(
scripthash,
)
})
addressRefreshes.set(
scripthash,
refresh,
)
return refresh
}
function queryFulcrum(
fulcrumUrl,
method,
params = [],
) {
return new Promise((resolve) => { return new Promise((resolve) => {
if (!fulcrumUrl) { if (!fulcrumUrl) {
resolve({ resolve({
@@ -98,7 +229,9 @@ function queryFulcrum(fulcrumUrl, method, params = []) {
params, params,
}) + '\n' }) + '\n'
console.log(`Sending Fulcrum request: ${request.trim()}`) console.log(
`Sending Fulcrum request: ${request.trim()}`,
)
socket.write(request) socket.write(request)
}) })
@@ -106,7 +239,9 @@ function queryFulcrum(fulcrumUrl, method, params = []) {
socket.on('data', (data) => { socket.on('data', (data) => {
const chunk = data.toString() const chunk = data.toString()
console.log(`Fulcrum response data: ${chunk.trim()}`) console.log(
`Fulcrum response data: ${chunk.trim()}`,
)
response += chunk response += chunk
@@ -189,14 +324,17 @@ function queryFulcrum(fulcrumUrl, method, params = []) {
finish({ finish({
ok: false, ok: false,
result: null, result: null,
error: 'Connection closed before a response was received', error:
'Connection closed before a response was received',
}) })
} }
}) })
}) })
} }
async function getFulcrumStatus(fulcrumUrl) { async function getFulcrumStatus(
fulcrumUrl,
) {
const version = await queryFulcrum( const version = await queryFulcrum(
fulcrumUrl, fulcrumUrl,
'server.version', 'server.version',
@@ -229,13 +367,20 @@ async function getFulcrumStatus(fulcrumUrl) {
headers.ok && headers.result headers.ok && headers.result
? headers.result.height ?? null ? headers.result.height ?? null
: null, : null,
error: headers.ok ? null : headers.error, error: headers.ok
? null
: headers.error,
} }
} }
async function getStatus() { async function getStatus() {
const fulcrumUrl = process.env.MUNIN_FULCRUM_URL || null const fulcrumUrl =
const fulcrum = await getFulcrumStatus(fulcrumUrl) process.env.MUNIN_FULCRUM_URL || null
const fulcrum =
await getFulcrumStatus(
fulcrumUrl,
)
return { return {
service: 'running', service: 'running',
@@ -247,23 +392,17 @@ async function getStatus() {
} }
} }
async function getAddressData(address) { async function getAddressDataFromFulcrum(
let scripthash address,
scripthash,
try { ) {
scripthash = addressToScripthash(address) const fulcrumUrl =
} catch (error) { process.env.MUNIN_FULCRUM_URL || null
throw new Error(
error instanceof Error
? error.message
: 'Invalid Bitcoin address',
)
}
const fulcrumUrl = process.env.MUNIN_FULCRUM_URL || null
if (!fulcrumUrl) { if (!fulcrumUrl) {
throw new Error('Fulcrum URL is not configured') throw new Error(
'Fulcrum URL is not configured',
)
} }
const balance = await queryFulcrum( const balance = await queryFulcrum(
@@ -306,42 +445,131 @@ async function getAddressData(address) {
address, address,
scripthash, scripthash,
confirmedBalance: confirmedBalance:
Number(balance.result?.confirmed || 0), Number(
balance.result?.confirmed || 0,
),
unconfirmedBalance: unconfirmedBalance:
Number(balance.result?.unconfirmed || 0), Number(
utxos: Array.isArray(utxos.result) balance.result?.unconfirmed || 0,
),
utxos:
Array.isArray(
utxos.result,
)
? utxos.result ? utxos.result
: [], : [],
history: Array.isArray(history.result) history:
Array.isArray(
history.result,
)
? history.result ? history.result
: [], : [],
lastSynced:
new Date().toISOString(),
}
}
async function getAddressData(
address,
) {
let scripthash
try {
scripthash =
addressToScripthash(
address,
)
} catch (error) {
throw new Error(
error instanceof Error
? error.message
: 'Invalid Bitcoin address',
)
}
const cached =
readAddressCache(
scripthash,
)
if (cached) {
if (
cached.ageMs >
addressCacheMaxAgeMs
) {
refreshAddressCache(
address,
scripthash,
).catch(() => {})
}
return {
...cached.data,
cacheAgeMs:
cached.ageMs,
}
}
const data =
await refreshAddressCache(
address,
scripthash,
)
return {
...data,
cacheAgeMs: 0,
} }
} }
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,
)
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:
data.history,
lastSynced:
data.lastSynced ||
null,
cacheAgeMs:
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: [],
lastSynced: null,
cacheAgeMs: null,
error: error:
error instanceof Error error instanceof Error
? error.message ? error.message
@@ -353,8 +581,13 @@ async function getAddressesWithData() {
return results return results
} }
const server = http.createServer(async (req, res) => { const server =
if (req.method === 'POST' && req.url === '/api/login') { http.createServer(
async (req, res) => {
if (
req.method === 'POST' &&
req.url === '/api/login'
) {
let body = '' let body = ''
req.on('data', (chunk) => { req.on('data', (chunk) => {
@@ -363,31 +596,48 @@ 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 (
res.writeHead(401, { !checkPassword(
'Content-Type': 'application/json', input.password,
}) )
) {
res.writeHead(
401,
{
'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(
'Content-Type': 'application/json', 200,
{
'Content-Type':
'application/json',
'Set-Cookie': 'Set-Cookie':
'munin_session=' + 'munin_session=' +
encodeURIComponent(token) + encodeURIComponent(
token,
) +
'; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400', '; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400',
}) },
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -395,13 +645,18 @@ const server = http.createServer(async (req, res) => {
}), }),
) )
} catch { } catch {
res.writeHead(400, { res.writeHead(
'Content-Type': 'application/json', 400,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Invalid request', error:
'Invalid request',
}), }),
) )
} }
@@ -410,15 +665,25 @@ const server = http.createServer(async (req, res) => {
return return
} }
if (req.url === '/api/status') { if (
if (!isValidSession(req)) { req.url ===
res.writeHead(401, { '/api/status'
'Content-Type': 'application/json', ) {
}) if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -426,27 +691,42 @@ const server = http.createServer(async (req, res) => {
} }
try { try {
const status = await getStatus() const status =
await getStatus()
res.writeHead(200, { res.writeHead(
'Content-Type': 'application/json', 200,
}) {
'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:',
error, error,
) )
res.writeHead(500, { res.writeHead(
'Content-Type': 'application/json', 500,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
service: 'running', service:
error: 'Failed to determine service status', 'running',
error:
'Failed to determine service status',
}), }),
) )
} }
@@ -454,15 +734,26 @@ const server = http.createServer(async (req, res) => {
return return
} }
if (req.method === 'GET' && req.url === '/api/addresses') { if (
if (!isValidSession(req)) { req.method === 'GET' &&
res.writeHead(401, { req.url ===
'Content-Type': 'application/json', '/api/addresses'
}) ) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -470,11 +761,16 @@ const server = http.createServer(async (req, res) => {
} }
try { try {
const addresses = await listAddresses() const addresses =
await listAddresses()
res.writeHead(200, { res.writeHead(
'Content-Type': 'application/json', 200,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -487,13 +783,18 @@ const server = http.createServer(async (req, res) => {
error, error,
) )
res.writeHead(500, { res.writeHead(
'Content-Type': 'application/json', 500,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Failed to list addresses', error:
'Failed to list addresses',
}), }),
) )
} }
@@ -503,16 +804,24 @@ const server = http.createServer(async (req, res) => {
if ( if (
req.method === 'GET' && req.method === 'GET' &&
req.url === '/api/addresses/data' req.url ===
'/api/addresses/data'
) { ) {
if (!isValidSession(req)) { if (
res.writeHead(401, { !isValidSession(req)
'Content-Type': 'application/json', ) {
}) res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -520,11 +829,16 @@ const server = http.createServer(async (req, res) => {
} }
try { try {
const addresses = await getAddressesWithData() const addresses =
await getAddressesWithData()
res.writeHead(200, { res.writeHead(
'Content-Type': 'application/json', 200,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -537,13 +851,18 @@ const server = http.createServer(async (req, res) => {
error, error,
) )
res.writeHead(500, { res.writeHead(
'Content-Type': 'application/json', 500,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Failed to get address data', error:
'Failed to get address data',
}), }),
) )
} }
@@ -553,16 +872,24 @@ const server = http.createServer(async (req, res) => {
if ( if (
req.method === 'POST' && req.method === 'POST' &&
req.url === '/api/addresses' req.url ===
'/api/addresses'
) { ) {
if (!isValidSession(req)) { if (
res.writeHead(401, { !isValidSession(req)
'Content-Type': 'application/json', ) {
}) res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -575,24 +902,40 @@ 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(
'Content-Type': 'application/json', 201,
}) {
'Content-Type':
'application/json',
},
)
res.end(JSON.stringify(entry)) res.end(
JSON.stringify(
entry,
),
)
} catch (error) { } catch (error) {
res.writeHead(400, { res.writeHead(
'Content-Type': 'application/json', 400,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -603,23 +946,32 @@ const server = http.createServer(async (req, res) => {
}), }),
) )
} }
}) },
)
return return
} }
if ( if (
req.method === 'DELETE' && req.method === 'DELETE' &&
req.url === '/api/addresses' req.url ===
'/api/addresses'
) { ) {
if (!isValidSession(req)) { if (
res.writeHead(401, { !isValidSession(req)
'Content-Type': 'application/json', ) {
}) res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
error: 'Authentication required', error:
'Authentication required',
}), }),
) )
@@ -632,15 +984,24 @@ 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(
'Content-Type': 'application/json', 200,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -648,9 +1009,13 @@ const server = http.createServer(async (req, res) => {
}), }),
) )
} catch (error) { } catch (error) {
res.writeHead(400, { res.writeHead(
'Content-Type': 'application/json', 400,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -661,45 +1026,74 @@ 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 (
res.writeHead(401, { !isValidSession(req)
'Content-Type': 'application/json', ) {
}) res.writeHead(
401,
{
'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(
'Content-Type': 'application/json', 200,
}) {
'Content-Type':
'application/json',
},
)
res.end(JSON.stringify(transaction)) res.end(
JSON.stringify(
transaction,
),
)
} catch (error) { } catch (error) {
res.writeHead(400, { res.writeHead(
'Content-Type': 'application/json', 400,
}) {
'Content-Type':
'application/json',
},
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -716,10 +1110,14 @@ 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(
`Status API listening on port ${port}`, `Status API listening on port ${port}`,
) )
}) },
)