feat: update inscription transfer schemas

This commit is contained in:
Rafael Cardenas
2023-07-02 21:42:37 -06:00
parent 98d1a0a70b
commit f80e983481
2 changed files with 17 additions and 3 deletions

View File

@@ -23,9 +23,7 @@ export const BitcoinInscriptionRevealedSchema = Type.Object({
export type BitcoinInscriptionRevealed = Static<typeof BitcoinInscriptionRevealedSchema>;
export const BitcoinInscriptionTransferredSchema = Type.Object({
inscription_number: Type.Integer(),
inscription_id: Type.String(),
ordinal_number: Type.Integer(),
updated_address: Nullable(Type.String()),
satpoint_pre_transfer: Type.String(),
satpoint_post_transfer: Type.String(),

View File

@@ -1,4 +1,5 @@
import { Static, Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';
import Fastify, {
FastifyInstance,
FastifyPluginCallback,
@@ -22,7 +23,13 @@ const ServerOptionsSchema = Type.Object({
port: Type.Integer(),
auth_token: Type.String(),
external_base_url: Type.String(),
/** Wait for the chainhook node to be available before submitting predicates */
wait_for_chainhook_node: Type.Boolean({ default: true }),
/** Validate the JSON schema of received chainhook payloads and report errors when invalid */
validate_chainhook_payloads: Type.Boolean({ default: false }),
/** Size limit for received chainhook payloads (default 40MB) */
body_limit: Type.Number({ default: 41943040 }),
});
/** Local event server connection and authentication options */
export type ServerOptions = Static<typeof ServerOptionsSchema>;
@@ -174,6 +181,7 @@ export async function buildServer(
Server,
TypeBoxTypeProvider
> = (fastify, options, done) => {
const compiledSchema = TypeCompiler.Compile(PayloadSchema);
fastify.addHook('preHandler', isEventAuthorized);
fastify.post(
'/chainhook/:uuid',
@@ -186,6 +194,14 @@ export async function buildServer(
},
},
async (request, reply) => {
if (serverOpts.validate_chainhook_payloads && !compiledSchema.Check(request.body)) {
logger.error(
[...compiledSchema.Errors(request.body)],
`ChainhookEventObserver received an invalid payload`
);
await reply.code(422).send();
return;
}
try {
await callback(request.params.uuid, request.body);
await reply.code(200).send();
@@ -202,7 +218,7 @@ export async function buildServer(
trustProxy: true,
logger: PINO_CONFIG,
pluginTimeout: 0, // Disable so ping can retry indefinitely
bodyLimit: 41943040, // 40 MB
bodyLimit: serverOpts.body_limit,
}).withTypeProvider<TypeBoxTypeProvider>();
if (serverOpts.wait_for_chainhook_node) {