From 226bffbe5f6e0a30546e480099f2a47cc886dce6 Mon Sep 17 00:00:00 2001 From: ahonn Date: Mon, 4 Mar 2024 21:17:35 +1100 Subject: [PATCH] feat: add min_satoshi filter param to address balance --- src/plugins/jwt.ts | 13 ++++++++++--- src/routes/bitcoin/address.ts | 28 ++++++++++++++++++++-------- src/routes/bitcoin/types.ts | 1 + 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/plugins/jwt.ts b/src/plugins/jwt.ts index e0fee6e6..3aca8ee2 100644 --- a/src/plugins/jwt.ts +++ b/src/plugins/jwt.ts @@ -17,10 +17,17 @@ export default fp(async (fastify) => { } try { await request.jwtVerify(); - const { origin } = request.headers; const jwt = (await request.jwtDecode()) as { aud: string }; - if (!origin || new URL(origin).hostname !== jwt.aud) { - reply.status(401).send('Invalid token'); + + const { origin, referer } = request.headers; + let domain = ''; + if (origin) { + domain = new URL(origin).hostname; + } else if (referer) { + domain = new URL(referer).hostname; + } + if (!domain || domain !== jwt.aud) { + reply.status(401).send('Invalid request origin or referer'); } } catch (err) { reply.status(401).send(err); diff --git a/src/routes/bitcoin/address.ts b/src/routes/bitcoin/address.ts index 3ff63068..04a8f3a7 100644 --- a/src/routes/bitcoin/address.ts +++ b/src/routes/bitcoin/address.ts @@ -4,11 +4,7 @@ import { Server } from 'http'; import { Balance, BalanceType, Transaction, UTXO, UTXOType } from './types'; import validateBitcoinAddress from '../../utils/validators'; -const addressRoutes: FastifyPluginCallback, Server, TypeBoxTypeProvider> = ( - fastify, - _, - done, -) => { +const addressRoutes: FastifyPluginCallback, Server, TypeBoxTypeProvider> = (fastify, _, done) => { fastify.addHook('preHandler', (request, _, done) => { const { address } = request.params as { address: string }; const valid = validateBitcoinAddress(address); @@ -25,6 +21,9 @@ const addressRoutes: FastifyPluginCallback, Server, TypeBox params: Type.Object({ address: Type.String(), }), + querystring: Type.Object({ + min_satoshi: Type.Optional(Type.Number()), + }), response: { 200: Balance, }, @@ -32,20 +31,26 @@ const addressRoutes: FastifyPluginCallback, Server, TypeBox }, async (request) => { const { address } = request.params; + const { min_satoshi } = request.query; const utxos = await fastify.electrs.getUtxoByAddress(address); return utxos.reduce( (acc: BalanceType, utxo: UTXOType) => { if (utxo.status.confirmed) { - acc.satoshi += utxo.value; - } else { - acc.pending_satoshi += utxo.value; + if (min_satoshi && utxo.value < min_satoshi) { + acc.dust_satoshi += utxo.value; + } else { + acc.satoshi += utxo.value; + } + return acc; } + acc.pending_satoshi += utxo.value; return acc; }, { address, satoshi: 0, pending_satoshi: 0, + dust_satoshi: 0, utxo_count: utxos.length, }, ); @@ -59,6 +64,9 @@ const addressRoutes: FastifyPluginCallback, Server, TypeBox params: Type.Object({ address: Type.String(), }), + querystring: Type.Object({ + min_satoshi: Type.Optional(Type.Number()), + }), response: { 200: Type.Array(UTXO), }, @@ -66,7 +74,11 @@ const addressRoutes: FastifyPluginCallback, Server, TypeBox }, async function (request) { const { address } = request.params; + const { min_satoshi } = request.query; const utxos = await fastify.electrs.getUtxoByAddress(address); + if (min_satoshi) { + return utxos.filter((utxo) => utxo.value >= min_satoshi); + } return utxos; }, ); diff --git a/src/routes/bitcoin/types.ts b/src/routes/bitcoin/types.ts index bed7a4e2..8bde178c 100644 --- a/src/routes/bitcoin/types.ts +++ b/src/routes/bitcoin/types.ts @@ -36,6 +36,7 @@ export const Balance = Type.Object({ address: Type.String(), satoshi: Type.Number(), pending_satoshi: Type.Number(), + dust_satoshi: Type.Number(), utxo_count: Type.Number(), });