From c29f5bafe2ad66b30f097e28e1ac95b80a477612 Mon Sep 17 00:00:00 2001 From: sebastijankuzner Date: Thu, 6 Feb 2025 15:57:26 +0100 Subject: [PATCH] Add currentHeightHex keyword --- packages/api-evm/source/service-provider.ts | 8 +++++++- packages/api-evm/source/validation/index.ts | 1 + packages/api-evm/source/validation/keywords.ts | 15 +++++++++++++++ packages/api-evm/source/validation/schemas.ts | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/api-evm/source/validation/keywords.ts diff --git a/packages/api-evm/source/service-provider.ts b/packages/api-evm/source/service-provider.ts index 5e1da455b..750605349 100644 --- a/packages/api-evm/source/service-provider.ts +++ b/packages/api-evm/source/service-provider.ts @@ -33,10 +33,16 @@ import { } from "./actions/index.js"; import Handlers from "./handlers.js"; import { Server } from "./server.js"; -import { schemas } from "./validation/index.js"; +import { makeKeywords, schemas } from "./validation/index.js"; export class ServiceProvider extends AbstractServiceProvider { public async register(): Promise { + for (const keyword of Object.values( + makeKeywords(this.app.get(Identifiers.State.Store)), + )) { + this.app.get(Identifiers.Cryptography.Validator).addKeyword(keyword); + } + for (const schema of Object.values(schemas)) { this.app.get(Identifiers.Cryptography.Validator).addSchema(schema); } diff --git a/packages/api-evm/source/validation/index.ts b/packages/api-evm/source/validation/index.ts index 54c135cdc..bd379011e 100644 --- a/packages/api-evm/source/validation/index.ts +++ b/packages/api-evm/source/validation/index.ts @@ -1 +1,2 @@ +export * from "./keywords.js"; export * from "./schemas.js"; diff --git a/packages/api-evm/source/validation/keywords.ts b/packages/api-evm/source/validation/keywords.ts new file mode 100644 index 000000000..26bfd1df6 --- /dev/null +++ b/packages/api-evm/source/validation/keywords.ts @@ -0,0 +1,15 @@ +import { Contracts } from "@mainsail/contracts"; +import { FuncKeywordDefinition } from "ajv"; + +export const makeKeywords = (stateStore: Contracts.State.Store) => { + const currentHeight: FuncKeywordDefinition = { + compile: (schema) => (data) => Number(data) === stateStore.getHeight(), + errors: false, + keyword: "currentHeightHex", + metaSchema: { + type: "boolean", + }, + }; + + return { currentHeight }; +}; diff --git a/packages/api-evm/source/validation/schemas.ts b/packages/api-evm/source/validation/schemas.ts index f04f75b3e..e209d4c2a 100644 --- a/packages/api-evm/source/validation/schemas.ts +++ b/packages/api-evm/source/validation/schemas.ts @@ -10,6 +10,7 @@ export const schemas: Record<"blockTag", AnySchemaObject> = { }, { $ref: "prefixedHex", + currentHeightHex: true, }, ], },