diff --git a/packages/minting-backend/sdk/src/index.ts b/packages/minting-backend/sdk/src/index.ts index 9b418f6480..544ef5260a 100644 --- a/packages/minting-backend/sdk/src/index.ts +++ b/packages/minting-backend/sdk/src/index.ts @@ -1,6 +1,6 @@ import { ImmutableConfiguration, ModuleConfiguration } from '@imtbl/config'; import { BlockchainData } from '@imtbl/blockchain-data'; -import { init } from '@imtbl/webhook'; +import { handle } from '@imtbl/webhook'; import { setEnvironment, setPublishableApiKey } from '@imtbl/metrics'; import { trackUncaughtException } from 'analytics'; import { mintingPersistence as mintingPersistencePg } from './persistence/pg/postgres'; @@ -28,7 +28,7 @@ const noopHandlers = { // eslint-disable-next-line @typescript-eslint/no-unused-vars zkevmMintRequestUpdated: async (event: MintRequestEvent) => { }, // eslint-disable-next-line @typescript-eslint/no-unused-vars - others: async (..._args: any) => { } + all: async (..._args: any) => { } }; export class MintingBackendModule { @@ -71,12 +71,12 @@ export class MintingBackendModule { } async processMint(body: string | Record, otherHandlers = noopHandlers) { - await init(body, this.baseConfig.environment, { + await handle(body, this.baseConfig.environment, { zkevmMintRequestUpdated: async (event: MintRequestEvent) => { await processMint(this.persistence, event, this.logger); otherHandlers.zkevmMintRequestUpdated(event); }, - others: otherHandlers.others + all: otherHandlers.all }); } } diff --git a/packages/webhook/sdk/src/index.ts b/packages/webhook/sdk/src/index.ts index a1b9f5d9d8..a60ae14064 100644 --- a/packages/webhook/sdk/src/index.ts +++ b/packages/webhook/sdk/src/index.ts @@ -1,5 +1,5 @@ -import { init } from './init'; +import { handle } from './init'; export { - init, + handle, }; diff --git a/packages/webhook/sdk/src/init.ts b/packages/webhook/sdk/src/init.ts index 7ab5a98cd8..dabac2292e 100644 --- a/packages/webhook/sdk/src/init.ts +++ b/packages/webhook/sdk/src/init.ts @@ -8,12 +8,19 @@ const allowedTopicArnPrefix = { [Environment.SANDBOX]: 'arn:aws:sns:us-east-2:783421985614:' }; -export const init = async ( +/** + * handle will validate webhook message origin and verify signature of the message and calls corresponding handlers passed in. + * @param body The request body to a webhook endpoint in json string or js object form. + * @param env The Immutable environment the webhook is set up for. + * @param handlers The optional handlers object for different events. The `all` handler will be triggered for all event types. + * @returns The event object from the webhook message after validation and verification. + */ +export const handle = async ( body: string | Record, env: Environment, - handlers: { - zkevmMintRequestUpdated: (event: any) => Promise; - others?: (event: any) => Promise; + handlers?: { + zkevmMintRequestUpdated?: (event: any) => Promise; + all?: (event: any) => Promise; } ) => { const msg: any = await new Promise((resolve, reject) => { @@ -38,17 +45,21 @@ export const init = async ( }); }); + const event = JSON.parse(msg.Message); if (msg.Type === 'Notification') { - const event = JSON.parse(msg.Message); switch (event.event_name) { case 'imtbl_zkevm_mint_request_updated': - await handlers.zkevmMintRequestUpdated(event); + if (handlers?.zkevmMintRequestUpdated) { + await handlers?.zkevmMintRequestUpdated(event); + } break; default: - if (handlers.others) { - await handlers.others(event); - } break; } + if (handlers?.all) { + await handlers?.all(event); + } } + + return event; };