diff --git a/packages/server/src/validator/constants.ts b/packages/server/src/validator/constants.ts index 3274cb7d2f..767a6fba87 100644 --- a/packages/server/src/validator/constants.ts +++ b/packages/server/src/validator/constants.ts @@ -21,6 +21,7 @@ export const BASE_HEX_REGEX = '^0[xX][a-fA-F0-9]'; export const ERROR_CODE = -32602; export const DEFAULT_HEX_ERROR = 'Expected 0x prefixed hexadecimal value'; +export const EVEN_HEX_ERROR = `${DEFAULT_HEX_ERROR} with even length`; export const HASH_ERROR = 'Expected 0x prefixed string representing the hash (32 bytes)'; export const ADDRESS_ERROR = 'Expected 0x prefixed string representing the address (20 bytes)'; export const BLOCK_NUMBER_ERROR = diff --git a/packages/server/src/validator/objectTypes.ts b/packages/server/src/validator/objectTypes.ts index ef0d81f2cd..9e99ecd2fb 100644 --- a/packages/server/src/validator/objectTypes.ts +++ b/packages/server/src/validator/objectTypes.ts @@ -162,7 +162,7 @@ export const OBJECTS_VALIDATIONS: { [key: string]: IObjectSchema } = { nullable: false, }, data: { - type: 'hex', + type: 'hexEvenLength', nullable: true, }, type: { diff --git a/packages/server/src/validator/types.ts b/packages/server/src/validator/types.ts index b4643377ec..273bce0592 100644 --- a/packages/server/src/validator/types.ts +++ b/packages/server/src/validator/types.ts @@ -90,6 +90,10 @@ export const TYPES: { [key: string]: ITypeValidation } = { test: (param: string) => new RegExp(Constants.BASE_HEX_REGEX + '*$').test(param), error: Constants.DEFAULT_HEX_ERROR, }, + hexEvenLength: { + test: (param: string) => new RegExp(Constants.BASE_HEX_REGEX + '*$').test(param) && !(param.length % 2), + error: Constants.EVEN_HEX_ERROR, + }, hex64: { test: (param: string) => new RegExp(Constants.BASE_HEX_REGEX + '{1,64}$').test(param), error: Constants.HASH_ERROR, diff --git a/packages/server/tests/integration/server.spec.ts b/packages/server/tests/integration/server.spec.ts index aa4caa3631..b3d96e8544 100644 --- a/packages/server/tests/integration/server.spec.ts +++ b/packages/server/tests/integration/server.spec.ts @@ -859,7 +859,7 @@ describe('RPC Server', function () { BaseTest.invalidParamError( error.response, Validator.ERROR_CODE, - `Invalid parameter 'data' for TransactionObject: ${Validator.DEFAULT_HEX_ERROR}, value: 123`, + `Invalid parameter 'data' for TransactionObject: ${Validator.EVEN_HEX_ERROR}, value: 123`, ); } }); @@ -1503,7 +1503,7 @@ describe('RPC Server', function () { BaseTest.invalidParamError( error.response, Validator.ERROR_CODE, - `Invalid parameter 'data' for TransactionObject: ${Validator.DEFAULT_HEX_ERROR}, value: 123`, + `Invalid parameter 'data' for TransactionObject: ${Validator.EVEN_HEX_ERROR}, value: 123`, ); } }); diff --git a/packages/server/tests/integration/validator.spec.ts b/packages/server/tests/integration/validator.spec.ts index bcde823836..d8f14d090d 100644 --- a/packages/server/tests/integration/validator.spec.ts +++ b/packages/server/tests/integration/validator.spec.ts @@ -564,7 +564,10 @@ describe('Validator', async () => { expectInvalidObject('value', Validator.DEFAULT_HEX_ERROR, object, '123456'), ); expect(() => Validator.validateParams([{ data: '123456' }], validation)).to.throw( - expectInvalidObject('data', Validator.DEFAULT_HEX_ERROR, object, '123456'), + expectInvalidObject('data', Validator.EVEN_HEX_ERROR, object, '123456'), + ); + expect(() => Validator.validateParams([{ data: '0x1234567' }], validation)).to.throw( + expectInvalidObject('data', Validator.EVEN_HEX_ERROR, object, '0x1234567'), ); }); });