diff --git a/components/client/typescript/package-lock.json b/components/client/typescript/package-lock.json index afb6ea776..85086b240 100644 --- a/components/client/typescript/package-lock.json +++ b/components/client/typescript/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hirosystems/chainhook-client", - "version": "1.4.1", + "version": "1.4.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@hirosystems/chainhook-client", - "version": "1.4.1", + "version": "1.4.2", "license": "Apache 2.0", "dependencies": { "@fastify/type-provider-typebox": "^3.2.0", diff --git a/components/client/typescript/package.json b/components/client/typescript/package.json index f9b1f26a1..27b750567 100644 --- a/components/client/typescript/package.json +++ b/components/client/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@hirosystems/chainhook-client", - "version": "1.4.1", + "version": "1.4.2", "description": "Chainhook TypeScript client", "main": "./dist/index.js", "typings": "./dist/index.d.ts", diff --git a/components/client/typescript/src/server.ts b/components/client/typescript/src/server.ts index f405dd402..7d952727a 100644 --- a/components/client/typescript/src/server.ts +++ b/components/client/typescript/src/server.ts @@ -30,6 +30,12 @@ const ServerOptionsSchema = Type.Object({ validate_chainhook_payloads: Type.Optional(Type.Boolean({ default: false })), /** Size limit for received chainhook payloads (default 40MB) */ body_limit: Type.Optional(Type.Number({ default: 41943040 })), + /** Node type: `chainhook` or `ordhook` */ + node_type: Type.Optional( + Type.Union([Type.Literal('chainhook'), Type.Literal('ordhook')], { + default: 'chainhook', + }) + ), }); /** Local event server connection and authentication options */ export type ServerOptions = Static; @@ -115,14 +121,14 @@ export async function buildServer( logger.info(`ChainhookEventObserver does not have predicates to register`); return; } - logger.info( - predicates, - `ChainhookEventObserver registering predicates at ${chainhookOpts.base_url}` - ); + const nodeType = serverOpts.node_type ?? 'chainhook'; + const path = nodeType === 'chainhook' ? `/v1/chainhooks` : `/v1/observers`; + const registerUrl = `${chainhookOpts.base_url}${path}`; + logger.info(predicates, `ChainhookEventObserver registering predicates at ${registerUrl}`); for (const predicate of predicates) { const thenThat: ThenThatHttpPost = { http_post: { - url: `${serverOpts.external_base_url}/chainhook/${encodeURIComponent(predicate.uuid)}`, + url: `${serverOpts.external_base_url}/payload`, authorization_header: `Bearer ${serverOpts.auth_token}`, }, }; @@ -130,7 +136,7 @@ export async function buildServer( const body = predicate as Predicate; if ('mainnet' in body.networks) body.networks.mainnet.then_that = thenThat; if ('testnet' in body.networks) body.networks.testnet.then_that = thenThat; - await request(`${chainhookOpts.base_url}/v1/chainhooks`, { + await request(registerUrl, { method: 'POST', body: JSON.stringify(body), headers: { 'content-type': 'application/json' }, @@ -151,19 +157,19 @@ export async function buildServer( return; } logger.info(`ChainhookEventObserver closing predicates at ${chainhookOpts.base_url}`); + const nodeType = serverOpts.node_type ?? 'chainhook'; const removals = predicates.map( predicate => new Promise((resolve, reject) => { - request( - `${chainhookOpts.base_url}/v1/chainhooks/${predicate.chain}/${encodeURIComponent( - predicate.uuid - )}`, - { - method: 'DELETE', - headers: { 'content-type': 'application/json' }, - throwOnError: true, - } - ) + const path = + nodeType === 'chainhook' + ? `/v1/chainhooks/${predicate.chain}/${encodeURIComponent(predicate.uuid)}` + : `/v1/observers/${encodeURIComponent(predicate.uuid)}`; + request(`${chainhookOpts.base_url}${path}`, { + method: 'DELETE', + headers: { 'content-type': 'application/json' }, + throwOnError: true, + }) .then(() => { logger.info( `ChainhookEventObserver removed '${predicate.name}' predicate (${predicate.uuid})` @@ -195,12 +201,9 @@ export async function buildServer( const compiledPayloadSchema = TypeCompiler.Compile(PayloadSchema); fastify.addHook('preHandler', isEventAuthorized); fastify.post( - '/chainhook/:uuid', + '/payload', { schema: { - params: Type.Object({ - uuid: Type.String({ format: 'uuid' }), - }), body: PayloadSchema, }, }, @@ -217,7 +220,7 @@ export async function buildServer( return; } try { - await callback(request.params.uuid, request.body); + await callback(request.body.chainhook.uuid, request.body); await reply.code(200).send(); } catch (error) { if (error instanceof BadPayloadRequestError) { @@ -237,7 +240,7 @@ export async function buildServer( trustProxy: true, logger: PINO_CONFIG, pluginTimeout: 0, // Disable so ping can retry indefinitely - bodyLimit: serverOpts.body_limit ?? 41943040, // 40MB + bodyLimit: serverOpts.body_limit ?? 41943040, // 40MB default }).withTypeProvider(); if (serverOpts.wait_for_chainhook_node ?? true) {