Compare commits

..
2 Commits
Author SHA1 Message Date
Unheard0840 810bca8154 Merge remote server updates with address cache 2026-08-29 16:44:42 +02:00
Unheard0840 0c78b1ddd5 Add persistent server-side address cache 2026-08-29 16:39:44 +02:00
+140 -360
View File
@@ -149,11 +149,7 @@ function refreshAddressCache(
return refresh
}
function queryFulcrum(
fulcrumUrl,
method,
params = [],
) {
function queryFulcrum(fulcrumUrl, method, params = []) {
return new Promise((resolve) => {
if (!fulcrumUrl) {
resolve({
@@ -229,9 +225,7 @@ function queryFulcrum(
params,
}) + '\n'
console.log(
`Sending Fulcrum request: ${request.trim()}`,
)
console.log(`Sending Fulcrum request: ${request.trim()}`)
socket.write(request)
})
@@ -239,9 +233,7 @@ 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
@@ -324,17 +316,14 @@ 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,
) {
async function getFulcrumStatus(fulcrumUrl) {
const version = await queryFulcrum(
fulcrumUrl,
'server.version',
@@ -367,20 +356,13 @@ async function getFulcrumStatus(
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',
@@ -445,23 +427,15 @@ 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:
@@ -469,16 +443,12 @@ 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
@@ -488,9 +458,7 @@ async function getAddressData(
}
const cached =
readAddressCache(
scripthash,
)
readAddressCache(scripthash)
if (cached) {
if (
@@ -523,49 +491,31 @@ async function getAddressData(
}
async function getAddressesWithData() {
const addresses =
await listAddresses()
const addresses = await listAddresses()
const results = []
for (
const entry of addresses
) {
for (const entry of addresses) {
try {
const data =
await getAddressData(
entry.address,
)
const data = await getAddressData(entry.address)
results.push({
...entry,
scripthash:
data.scripthash,
confirmedBalance:
data.confirmedBalance,
unconfirmedBalance:
data.unconfirmedBalance,
utxos:
data.utxos,
history:
data.history,
scripthash: data.scripthash,
confirmedBalance: data.confirmedBalance,
unconfirmedBalance: data.unconfirmedBalance,
utxos: data.utxos,
history: data.history,
lastSynced:
data.lastSynced ||
null,
data.lastSynced || null,
cacheAgeMs:
Number(
data.cacheAgeMs ||
0,
),
Number(data.cacheAgeMs || 0),
error: null,
})
} catch (error) {
results.push({
...entry,
confirmedBalance:
null,
unconfirmedBalance:
null,
confirmedBalance: null,
unconfirmedBalance: null,
utxos: [],
history: [],
lastSynced: null,
@@ -581,13 +531,8 @@ async function getAddressesWithData() {
return results
}
const server =
http.createServer(
async (req, res) => {
if (
req.method === 'POST' &&
req.url === '/api/login'
) {
const server = http.createServer(async (req, res) => {
if (req.method === 'POST' && req.url === '/api/login') {
let body = ''
req.on('data', (chunk) => {
@@ -596,48 +541,31 @@ const server =
req.on('end', () => {
try {
const input =
JSON.parse(body)
const input = JSON.parse(body)
if (
!checkPassword(
input.password,
)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (!checkPassword(input.password)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Invalid password',
error: 'Invalid password',
}),
)
return
}
const token =
createSession()
const token = createSession()
res.writeHead(
200,
{
'Content-Type':
'application/json',
res.writeHead(200, {
'Content-Type': 'application/json',
'Set-Cookie':
'munin_session=' +
encodeURIComponent(
token,
) +
encodeURIComponent(token) +
'; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400',
},
)
})
res.end(
JSON.stringify({
@@ -645,18 +573,13 @@ const server =
}),
)
} catch {
res.writeHead(
400,
{
'Content-Type':
'application/json',
},
)
res.writeHead(400, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Invalid request',
error: 'Invalid request',
}),
)
}
@@ -665,25 +588,15 @@ const server =
return
}
if (
req.url ===
'/api/status'
) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (req.url === '/api/status') {
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
error: 'Authentication required',
}),
)
@@ -691,42 +604,27 @@ const server =
}
try {
const status =
await getStatus()
const status = await getStatus()
res.writeHead(
200,
{
'Content-Type':
'application/json',
},
)
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify(
status,
),
)
res.end(JSON.stringify(status))
} catch (error) {
console.error(
'Failed to get service status:',
error,
)
res.writeHead(
500,
{
'Content-Type':
'application/json',
},
)
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
service:
'running',
error:
'Failed to determine service status',
service: 'running',
error: 'Failed to determine service status',
}),
)
}
@@ -734,26 +632,15 @@ const server =
return
}
if (
req.method === 'GET' &&
req.url ===
'/api/addresses'
) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (req.method === 'GET' && req.url === '/api/addresses') {
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
error: 'Authentication required',
}),
)
@@ -761,16 +648,11 @@ const server =
}
try {
const addresses =
await listAddresses()
const addresses = await listAddresses()
res.writeHead(
200,
{
'Content-Type':
'application/json',
},
)
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
@@ -783,18 +665,13 @@ const server =
error,
)
res.writeHead(
500,
{
'Content-Type':
'application/json',
},
)
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Failed to list addresses',
error: 'Failed to list addresses',
}),
)
}
@@ -804,24 +681,16 @@ const server =
if (
req.method === 'GET' &&
req.url ===
'/api/addresses/data'
req.url === '/api/addresses/data'
) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
error: 'Authentication required',
}),
)
@@ -829,16 +698,11 @@ const server =
}
try {
const addresses =
await getAddressesWithData()
const addresses = await getAddressesWithData()
res.writeHead(
200,
{
'Content-Type':
'application/json',
},
)
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
@@ -851,18 +715,13 @@ const server =
error,
)
res.writeHead(
500,
{
'Content-Type':
'application/json',
},
)
res.writeHead(500, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Failed to get address data',
error: 'Failed to get address data',
}),
)
}
@@ -872,24 +731,16 @@ const server =
if (
req.method === 'POST' &&
req.url ===
'/api/addresses'
req.url === '/api/addresses'
) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
error: 'Authentication required',
}),
)
@@ -902,40 +753,24 @@ const server =
body += chunk
})
req.on(
'end',
async () => {
req.on('end', async () => {
try {
const input =
JSON.parse(body)
const input = JSON.parse(body)
const entry =
await addAddress(
const entry = await addAddress(
input.address,
input.label,
)
res.writeHead(
201,
{
'Content-Type':
'application/json',
},
)
res.writeHead(201, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify(
entry,
),
)
res.end(JSON.stringify(entry))
} catch (error) {
res.writeHead(
400,
{
'Content-Type':
'application/json',
},
)
res.writeHead(400, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
@@ -946,32 +781,23 @@ const server =
}),
)
}
},
)
})
return
}
if (
req.method === 'DELETE' &&
req.url ===
'/api/addresses'
req.url === '/api/addresses'
) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
error: 'Authentication required',
}),
)
@@ -984,24 +810,15 @@ const server =
body += chunk
})
req.on(
'end',
async () => {
req.on('end', async () => {
try {
const input =
JSON.parse(body)
const input = JSON.parse(body)
await removeAddress(
input.address,
)
await removeAddress(input.address)
res.writeHead(
200,
{
'Content-Type':
'application/json',
},
)
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
@@ -1009,13 +826,9 @@ const server =
}),
)
} catch (error) {
res.writeHead(
400,
{
'Content-Type':
'application/json',
},
)
res.writeHead(400, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
@@ -1026,74 +839,45 @@ const server =
}),
)
}
},
)
})
return
}
if (
req.method === 'GET' &&
req.url.startsWith(
'/api/transactions/',
)
req.url.startsWith('/api/transactions/')
) {
if (
!isValidSession(req)
) {
res.writeHead(
401,
{
'Content-Type':
'application/json',
},
)
if (!isValidSession(req)) {
res.writeHead(401, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
error:
'Authentication required',
error: 'Authentication required',
}),
)
return
}
const txid =
decodeURIComponent(
req.url.slice(
'/api/transactions/'
.length,
),
const txid = decodeURIComponent(
req.url.slice('/api/transactions/'.length),
)
try {
const transaction =
await getTransaction(
txid,
)
const transaction = await getTransaction(txid)
res.writeHead(
200,
{
'Content-Type':
'application/json',
},
)
res.writeHead(200, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify(
transaction,
),
)
res.end(JSON.stringify(transaction))
} catch (error) {
res.writeHead(
400,
{
'Content-Type':
'application/json',
},
)
res.writeHead(400, {
'Content-Type': 'application/json',
})
res.end(
JSON.stringify({
@@ -1110,14 +894,10 @@ const server =
res.writeHead(404)
res.end('Not found')
},
)
})
server.listen(
port,
() => {
server.listen(port, () => {
console.log(
`Status API listening on port ${port}`,
)
},
)
})