From 67455a744c0e3c951b0bea1d68a56cf78678fc31 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Tue, 14 Sep 2021 03:20:09 +1000 Subject: [PATCH] fix: validation schema --- src/modules/api/api.controller.ts | 5 ++-- src/modules/api/api.validator.ts | 46 +++++++++++++++++++------------ 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/modules/api/api.controller.ts b/src/modules/api/api.controller.ts index f150383..b2c1c51 100644 --- a/src/modules/api/api.controller.ts +++ b/src/modules/api/api.controller.ts @@ -31,14 +31,15 @@ export class ApiController { @Post('/transaction') async transaction(@Res() res: Response, @Body() { body }: any) { - const inputError = validateTransactionRequest(body); + const params = JSON.parse(body); + const inputError = validateTransactionRequest(params); if (inputError) { console.log('Invalid input:', inputError); return res.status(HttpStatus.BAD_REQUEST).json({ error: inputError }); } - const jobId = await this.service.transaction(JSON.parse(body)); + const jobId = await this.service.transaction(params); return res.send(jobId); } diff --git a/src/modules/api/api.validator.ts b/src/modules/api/api.validator.ts index 0cb2e58..f85ba73 100644 --- a/src/modules/api/api.validator.ts +++ b/src/modules/api/api.validator.ts @@ -19,31 +19,43 @@ const addressType = { const proofType = { type: 'string', pattern: '^0x[a-fA-F0-9]{512}$' }; const bytes32Type = { type: 'string', pattern: '^0x[a-fA-F0-9]{64}$' }; -const arrayType = { type: 'array', pattern: '^0x[a-fA-F0-9]{64}$' }; - -const recipientType = { - type: 'object', - properties: { - recipient: addressType, - relayer: addressType, - encryptedOutput1: bytes32Type, - encryptedOutput2: bytes32Type, - }, -}; +const externalAmountType = { type: 'string', pattern: '^(0x[a-fA-F0-9]{64}|-0x[a-fA-F0-9]{63})$' }; +const encryptedOutputType = { type: 'string', pattern: '^0x[a-fA-F0-9]{312}$' }; +const arrayType = { type: 'array', items: bytes32Type }; const transactionSchema = { type: 'object', properties: { - proof: proofType, + amount: { + type: 'string', + }, + extData: { + type: 'object', + properties: { + encryptedOutput1: encryptedOutputType, + encryptedOutput2: encryptedOutputType, + extAmount: externalAmountType, + fee: bytes32Type, + recipient: addressType, + relayer: addressType, + }, + }, args: { - type: 'array', - maxItems: 9, - minItems: 9, - items: [bytes32Type, bytes32Type, arrayType, bytes32Type, bytes32Type, bytes32Type, bytes32Type, recipientType, bytes32Type], + type: 'object', + properties: { + extDataHash: bytes32Type, + inputNullifiers: arrayType, + newRoot: bytes32Type, + outPathIndices: bytes32Type, + outputCommitments: arrayType, + proof: proofType, + publicAmount: bytes32Type, + root: bytes32Type, + }, }, }, additionalProperties: false, - required: ['proof', 'args'], + required: ['extData', 'args', 'amount'], }; const validateTornadoTransaction = ajv.compile(transactionSchema);