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