Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merkle root index validation #60

Merged
merged 1 commit into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions zp-relayer/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
checkGetLimits,
checkGetTransactions,
checkGetTransactionsV2,
checkMerkleRootErrors,
checkSendTransactionErrors,
checkSendTransactionsErrors,
} from './validation/validation'
Expand Down Expand Up @@ -73,6 +74,13 @@ async function sendTransaction(req: Request, res: Response, next: NextFunction)
}

async function merkleRoot(req: Request, res: Response, next: NextFunction) {
const errors = checkMerkleRootErrors(req.params)
if (errors) {
logger.info('Request errors: %o', errors)
res.status(400).json({ errors })
return
}

const index = req.params.index
const root = await pool.getContractMerkleRoot(index)
res.json(root)
Expand Down
13 changes: 13 additions & 0 deletions zp-relayer/validation/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ const AjvGetLimitsSchema: JSONSchemaType<{
required: [],
}

const AjvMerkleRootSchema: JSONSchemaType<{
index: string | number
}> = {
type: 'object',
properties: {
index: {
type: 'integer',
},
},
required: ['index'],
}

function checkErrors<T>(schema: JSONSchemaType<T>) {
const validate = ajv.compile(schema)
return (data: any) => {
Expand All @@ -147,6 +159,7 @@ function checkErrors<T>(schema: JSONSchemaType<T>) {
}
}

export const checkMerkleRootErrors = checkErrors(AjvMerkleRootSchema)
export const checkSendTransactionErrors = checkErrors(AjvSendTransactionSchema)
export const checkSendTransactionsErrors = checkErrors(AjvSendTransactionsSchema)
export const checkGetTransactions = checkErrors(AjvGetTransactionsSchema)
Expand Down