Skip to content

Commit

Permalink
fix: validation for satoshi ordinal numbers (#223)
Browse files Browse the repository at this point in the history
* fix: validation for satoshi ordinal numbers

* fix: only catch correct err

* fix: specify error
  • Loading branch information
rafaelcr committed Sep 14, 2023
1 parent 0469aa3 commit c3cf8c0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/api/routes/sats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { FastifyPluginCallback } from 'fastify';
import { Server } from 'http';
import {
InscriptionResponse,
InvalidSatoshiNumberResponse,
LimitParam,
NotFoundResponse,
OffsetParam,
OrdinalParam,
PaginatedResponse,
Expand All @@ -32,12 +32,18 @@ export const SatRoutes: FastifyPluginCallback<Record<never, never>, Server, Type
}),
response: {
200: SatoshiResponse,
404: NotFoundResponse,
400: InvalidSatoshiNumberResponse,
},
},
},
async (request, reply) => {
const sat = new OrdinalSatoshi(request.params.ordinal);
let sat: OrdinalSatoshi;
try {
sat = new OrdinalSatoshi(request.params.ordinal);
} catch (error) {
await reply.code(400).send({ error: 'Invalid satoshi ordinal number' });
return;
}
const inscriptions = await fastify.db.getInscriptions(
{ limit: 1, offset: 0 },
{ sat_ordinal: BigInt(request.params.ordinal) }
Expand Down Expand Up @@ -76,15 +82,23 @@ export const SatRoutes: FastifyPluginCallback<Record<never, never>, Server, Type
}),
response: {
200: PaginatedResponse(InscriptionResponse, 'Paginated Satoshi Inscriptions Response'),
400: InvalidSatoshiNumberResponse,
},
},
},
async (request, reply) => {
let sat: OrdinalSatoshi;
try {
sat = new OrdinalSatoshi(request.params.ordinal);
} catch (error) {
await reply.code(400).send({ error: 'Invalid satoshi ordinal number' });
return;
}
const limit = request.query.limit ?? DEFAULT_API_LIMIT;
const offset = request.query.offset ?? 0;
const inscriptions = await fastify.db.getInscriptions(
{ limit, offset },
{ sat_ordinal: BigInt(request.params.ordinal) }
{ sat_ordinal: BigInt(sat.ordinal) }
);
await reply.send({
limit,
Expand Down
7 changes: 7 additions & 0 deletions src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ export const NotFoundResponse = Type.Object(
{ title: 'Not Found Response' }
);

export const InvalidSatoshiNumberResponse = Type.Object(
{
error: Type.Literal('Invalid satoshi ordinal number'),
},
{ title: 'Invalid Satoshi Number Response' }
);

export const InscriptionsPerBlock = Type.Object({
block_height: Type.String({ examples: ['778921'] }),
block_hash: Type.String({
Expand Down
1 change: 1 addition & 0 deletions src/api/util/ordinal-satoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class OrdinalSatoshi {
private third: number;

constructor(ordinal: number) {
if (ordinal > SAT_SUPPLY || ordinal < 0) throw Error('Invalid satoshi ordinal number');
let satAccum = 0;
let subsidy = INITIAL_SUBSIDY;
let epoch = 0;
Expand Down
6 changes: 6 additions & 0 deletions tests/sats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,11 @@ describe('/sats', () => {
url: '/ordinals/v1/sats/-1',
});
expect(response2.statusCode).toBe(400);

const response3 = await fastify.inject({
method: 'GET',
url: '/ordinals/v1/sats/Infinity',
});
expect(response3.statusCode).toBe(400);
});
});

0 comments on commit c3cf8c0

Please sign in to comment.