Skip to content

Commit

Permalink
feat: add min_satoshi filter param to address balance
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Mar 4, 2024
1 parent 7e2527a commit 226bffb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/plugins/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
28 changes: 20 additions & 8 deletions src/routes/bitcoin/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { Server } from 'http';
import { Balance, BalanceType, Transaction, UTXO, UTXOType } from './types';
import validateBitcoinAddress from '../../utils/validators';

const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = (
fastify,
_,
done,
) => {
const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBoxTypeProvider> = (fastify, _, done) => {
fastify.addHook('preHandler', (request, _, done) => {
const { address } = request.params as { address: string };
const valid = validateBitcoinAddress(address);
Expand All @@ -25,27 +21,36 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBox
params: Type.Object({
address: Type.String(),
}),
querystring: Type.Object({
min_satoshi: Type.Optional(Type.Number()),
}),
response: {
200: Balance,
},
},
},
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,
},
);
Expand All @@ -59,14 +64,21 @@ const addressRoutes: FastifyPluginCallback<Record<never, never>, Server, TypeBox
params: Type.Object({
address: Type.String(),
}),
querystring: Type.Object({
min_satoshi: Type.Optional(Type.Number()),
}),
response: {
200: Type.Array(UTXO),
},
},
},
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;
},
);
Expand Down
1 change: 1 addition & 0 deletions src/routes/bitcoin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});

Expand Down

0 comments on commit 226bffb

Please sign in to comment.