Compare commits

..
6 Commits
Author SHA1 Message Date
Unheard0840 83070ce195 Connect wallet transaction pagination to server API 2026-08-29 17:48:19 +02:00
Unheard0840 e56c9fde81 Update server/status.js
Add paginated address transaction API
2026-08-29 15:34:16 +00:00
Unheard0840 515ecf0ea0 Update server/status.js
Add paginated address transaction API
2026-08-29 15:28:26 +00:00
Unheard0840 9593b8ec62 Update web/index.html
Paginate wallet transactions
2026-08-29 15:17:54 +00:00
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
2 changed files with 487 additions and 279 deletions
+267 -130
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() {
@@ -335,7 +336,8 @@ function queryFulcrum(
async function getFulcrumStatus( async function getFulcrumStatus(
fulcrumUrl, fulcrumUrl,
) { ) {
const version = await queryFulcrum( const version =
await queryFulcrum(
fulcrumUrl, fulcrumUrl,
'server.version', 'server.version',
['Munin Bitcoin', '1.4'], ['Munin Bitcoin', '1.4'],
@@ -343,7 +345,9 @@ async function getFulcrumStatus(
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,
@@ -352,22 +356,29 @@ async function getFulcrumStatus(
} }
} }
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 error:
headers.ok
? null ? null
: headers.error, : headers.error,
} }
@@ -375,7 +386,8 @@ async function getFulcrumStatus(
async function getStatus() { async function getStatus() {
const fulcrumUrl = const fulcrumUrl =
process.env.MUNIN_FULCRUM_URL || null process.env.MUNIN_FULCRUM_URL ||
null
const fulcrum = const fulcrum =
await getFulcrumStatus( await getFulcrumStatus(
@@ -397,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(
@@ -405,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],
@@ -417,7 +431,8 @@ async function getAddressDataFromFulcrum(
) )
} }
const utxos = await queryFulcrum( const utxos =
await queryFulcrum(
fulcrumUrl, fulcrumUrl,
'blockchain.scripthash.listunspent', 'blockchain.scripthash.listunspent',
[scripthash], [scripthash],
@@ -429,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],
@@ -446,11 +462,13 @@ async function getAddressDataFromFulcrum(
scripthash, scripthash,
confirmedBalance: confirmedBalance:
Number( Number(
balance.result?.confirmed || 0, balance.result?.confirmed ||
0,
), ),
unconfirmedBalance: unconfirmedBalance:
Number( Number(
balance.result?.unconfirmed || 0, balance.result?.unconfirmed ||
0,
), ),
utxos: utxos:
Array.isArray( Array.isArray(
@@ -522,21 +540,54 @@ async function getAddressData(
} }
} }
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 = const addresses =
await listAddresses() await listAddresses()
const results = [] const results = []
for ( for (const entry of addresses) {
const entry of addresses
) {
try { try {
const data = const data =
await getAddressData( await getAddressData(
entry.address, entry.address,
) )
const history =
Array.isArray(
data.history,
)
? data.history
: []
results.push({ results.push({
...entry, ...entry,
scripthash: scripthash:
@@ -548,26 +599,34 @@ async function getAddressesWithData() {
utxos: utxos:
data.utxos, data.utxos,
history: history:
data.history, getAddressTransactionPage(
history,
),
transactionCount:
history.length,
transactionOffset: 0,
transactionLimit:
addressTransactionPageSize,
lastSynced: lastSynced:
data.lastSynced || data.lastSynced ||
null, null,
cacheAgeMs: cacheAgeMs:
Number( Number(
data.cacheAgeMs || data.cacheAgeMs || 0,
0,
), ),
error: null, error: null,
}) })
} catch (error) { } catch (error) {
results.push({ results.push({
...entry, ...entry,
confirmedBalance: confirmedBalance: null,
null, unconfirmedBalance: null,
unconfirmedBalance:
null,
utxos: [], utxos: [],
history: [], history: [],
transactionCount: 0,
transactionOffset: 0,
transactionLimit:
addressTransactionPageSize,
lastSynced: null, lastSynced: null,
cacheAgeMs: null, cacheAgeMs: null,
error: error:
@@ -604,13 +663,10 @@ const server =
input.password, input.password,
) )
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -625,9 +681,7 @@ const server =
const token = const token =
createSession() createSession()
res.writeHead( res.writeHead(200, {
200,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
'Set-Cookie': 'Set-Cookie':
@@ -636,8 +690,7 @@ const server =
token, 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({
@@ -645,13 +698,10 @@ const server =
}), }),
) )
} catch { } catch {
res.writeHead( res.writeHead(400, {
400,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -672,13 +722,10 @@ const server =
if ( if (
!isValidSession(req) !isValidSession(req)
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -694,13 +741,10 @@ const server =
const status = const status =
await getStatus() await getStatus()
res.writeHead( res.writeHead(200, {
200,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify( JSON.stringify(
@@ -713,13 +757,10 @@ const server =
error, error,
) )
res.writeHead( res.writeHead(500, {
500,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -742,13 +783,10 @@ const server =
if ( if (
!isValidSession(req) !isValidSession(req)
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -764,13 +802,10 @@ const server =
const addresses = const addresses =
await listAddresses() await listAddresses()
res.writeHead( res.writeHead(200, {
200,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -783,13 +818,10 @@ const server =
error, error,
) )
res.writeHead( res.writeHead(500, {
500,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -810,13 +842,10 @@ const server =
if ( if (
!isValidSession(req) !isValidSession(req)
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -832,13 +861,10 @@ const server =
const addresses = const addresses =
await getAddressesWithData() await getAddressesWithData()
res.writeHead( res.writeHead(200, {
200,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -851,13 +877,10 @@ const server =
error, error,
) )
res.writeHead( res.writeHead(500, {
500,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -870,6 +893,147 @@ const server =
return 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',
}),
)
}
return
}
if ( if (
req.method === 'POST' && req.method === 'POST' &&
req.url === req.url ===
@@ -878,13 +1042,10 @@ const server =
if ( if (
!isValidSession(req) !isValidSession(req)
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -915,13 +1076,10 @@ const server =
input.label, input.label,
) )
res.writeHead( res.writeHead(201, {
201,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify( JSON.stringify(
@@ -929,13 +1087,10 @@ const server =
), ),
) )
} catch (error) { } catch (error) {
res.writeHead( res.writeHead(400, {
400,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -960,13 +1115,10 @@ const server =
if ( if (
!isValidSession(req) !isValidSession(req)
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -995,13 +1147,10 @@ const server =
input.address, input.address,
) )
res.writeHead( res.writeHead(200, {
200,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -1009,13 +1158,10 @@ const server =
}), }),
) )
} catch (error) { } catch (error) {
res.writeHead( res.writeHead(400, {
400,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -1041,13 +1187,10 @@ const server =
if ( if (
!isValidSession(req) !isValidSession(req)
) { ) {
res.writeHead( res.writeHead(401, {
401,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
@@ -1073,13 +1216,10 @@ const server =
txid, txid,
) )
res.writeHead( res.writeHead(200, {
200,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify( JSON.stringify(
@@ -1087,13 +1227,10 @@ const server =
), ),
) )
} catch (error) { } catch (error) {
res.writeHead( res.writeHead(400, {
400,
{
'Content-Type': 'Content-Type':
'application/json', 'application/json',
}, })
)
res.end( res.end(
JSON.stringify({ JSON.stringify({
+149 -78
View File
@@ -221,7 +221,7 @@
.wallet-header { .wallet-header {
display: grid; display: grid;
grid-template-columns: minmax(0, 1fr) auto auto; grid-template-columns: minmax(0, 1fr) auto auto auto;
align-items: center; align-items: center;
gap: 24px; gap: 24px;
width: 100%; width: 100%;
@@ -281,6 +281,13 @@
font-weight: 650; font-weight: 650;
} }
.wallet-chevron {
width: 20px;
color: var(--muted);
text-align: center;
font-size: 18px;
}
.wallet-content { .wallet-content {
padding: 0 18px 18px; padding: 0 18px 18px;
border-top: 1px solid var(--border); border-top: 1px solid var(--border);
@@ -503,9 +510,6 @@
text-align: right; text-align: right;
} }
.transaction-chevron {
display: none;
}
/* Login */ /* Login */
@@ -870,6 +874,7 @@
const expandedWallets = new Set(); const expandedWallets = new Set();
const expandedTransactions = new Set(); const expandedTransactions = new Set();
const visibleTransactionCounts = new Map();
const transactionCache = new Map(); const transactionCache = new Map();
@@ -1074,38 +1079,6 @@
} }
} }
function formatDateDateOnly(timestamp) {
const value = Number(timestamp);
if (!Number.isFinite(value)) {
return 'Unknown';
}
const date = new Date(value * 1000);
if (Number.isNaN(date.getTime())) {
return 'Unknown';
}
const pad = (number) => String(number).padStart(2, '0');
const year = date.getFullYear();
const shortYear = String(year).slice(-2);
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
switch (dateFormat) {
case 'DD.MM.YYYY HH:MM':
case 'DD.MM.YY HH:MM':
return `${day}.${month}.${dateFormat.includes('YYYY') ? year : shortYear}`;
case 'YYYY-MM-DD HH:MM':
return `${year}-${month}-${day}`;
case 'MM/DD/YY HH:MM':
return `${month}/${day}/${shortYear}`;
default:
return `${day}.${month}.${shortYear}`;
}
}
function formatSyncAge(date) { function formatSyncAge(date) {
if (!date) { if (!date) {
return 'Not synced'; return 'Not synced';
@@ -1426,7 +1399,9 @@
const lastActivity = getTransactionDateForLatest(wallet, latest); const lastActivity = getTransactionDateForLatest(wallet, latest);
const lastActivityText = lastActivity const lastActivityText = lastActivity
? formatDateDateOnly(lastActivity) ? formatDate(lastActivity)
: latest
? `Block ${latest.height}`
: 'No activity'; : 'No activity';
return ` return `
@@ -1465,6 +1440,10 @@
${escapeHtml(lastActivityText)} ${escapeHtml(lastActivityText)}
</div> </div>
</div> </div>
<div class="wallet-chevron">
${expanded ? 'v' : '>'}
</div>
</button> </button>
${ ${
@@ -1498,8 +1477,115 @@
}); });
} }
function renderWalletContent(wallet, transactionCount) { async function showMoreTransactions(encodedAddress) {
const transactions = getWalletTransactions(wallet); const address =
decodeURIComponent(
encodedAddress,
);
const wallet =
wallets.find(
(item) =>
item.address === address,
);
if (!wallet) {
return;
}
const current =
getWalletTransactions(wallet).length;
const total =
Number(wallet.transactionCount) ||
current;
if (current >= total) {
return;
}
const button =
document.querySelector(
`[data-show-more-address="${encodeURIComponent(address)}"]`,
);
if (button) {
button.disabled = true;
button.textContent = 'Loading...';
}
try {
const response =
await fetch(
`/api/addresses/transactions?address=${encodeURIComponent(address)}&offset=${current}&limit=50`,
{
credentials:
'same-origin',
},
);
if (
response.status === 401
) {
showLogin();
return;
}
if (!response.ok) {
throw new Error(
`HTTP ${response.status}`,
);
}
const data =
await response.json();
const transactions =
Array.isArray(
data.transactions,
)
? data.transactions
: [];
wallet.history = [
...getWalletTransactions(wallet),
...transactions,
];
wallet.transactionCount =
Number(data.total) ||
wallet.history.length;
wallet.transactionOffset =
current;
renderWallets();
} catch (error) {
console.error(
'Failed to load more transactions:',
error,
);
renderWallets();
}
}
function renderWalletContent(
wallet,
transactionCount,
) {
const transactions =
getWalletTransactions(wallet);
const visibleCount =
transactions.length;
const total =
Number(wallet.transactionCount) ||
transactions.length;
const hasMore =
visibleCount < total;
return ` return `
<div class="wallet-content"> <div class="wallet-content">
@@ -1509,13 +1595,32 @@
${ ${
transactions.length transactions.length
? renderTransactions(wallet, transactions) ? renderTransactions(
wallet,
transactions,
)
: ` : `
<div class="empty"> <div class="empty">
No transactions found for this wallet. No transactions found for this wallet.
</div> </div>
` `
} }
${
hasMore
? `
<button
class="button"
type="button"
data-show-more-address="${encodeURIComponent(wallet.address)}"
onclick="showMoreTransactions('${encodeURIComponent(wallet.address)}')"
style="width: 100%; margin-top: 14px;"
>
Show more (${visibleCount} of ${total})
</button>
`
: ''
}
</div> </div>
`; `;
} }
@@ -1539,7 +1644,10 @@
const dateText = timestamp const dateText = timestamp
? formatDate(timestamp) ? formatDate(timestamp)
: 'Loading...'; : transaction.height === null ||
transaction.height === undefined
? 'Pending'
: `Block ${transaction.height}`;
const directionLabel = const directionLabel =
display.direction === 'sent' ? 'Sent' : 'Received'; display.direction === 'sent' ? 'Sent' : 'Received';
@@ -1710,19 +1818,6 @@
renderWallets(); renderWallets();
const wallet =
wallets.find(
(item) =>
item.address === address,
);
if (!wallet) {
return;
}
await loadWalletTransactionDetails(
wallet,
);
} }
async function toggleTransaction( async function toggleTransaction(
@@ -1760,30 +1855,6 @@
await loadTransaction(txid); await loadTransaction(txid);
} }
async function loadWalletTransactionDetails(
wallet,
) {
const transactions =
getWalletTransactions(wallet);
if (!transactions.length) {
return;
}
/*
* Load transaction details in parallel so the
* list can populate dates and amounts without
* requiring the user to open each transaction.
*
* Cached transactions are skipped by loadTransaction().
*/
await Promise.all(
transactions.map((transaction) =>
loadTransaction(transaction.txid),
),
);
}
async function loadTransaction( async function loadTransaction(
txid, txid,
) { ) {