Skip to content

Commit

Permalink
deposit amounts validation
Browse files Browse the repository at this point in the history
  • Loading branch information
HananINouman committed Mar 13, 2024
1 parent 47664f2 commit b4e19bb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
23 changes: 22 additions & 1 deletion src/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
import Ajv, { type ErrorObject } from 'ajv'
import { ether_to_gwei } from './constants';

export function validatePayload (

function validDpositAmounts(data: boolean, deposits: string[]) {
let sum = 0;
for (let i = 0; i < deposits.length; i++) {
const amount = parseInt(deposits[i]);
if (amount % ether_to_gwei !== 0 || amount > 32 * ether_to_gwei) {
return false
}
sum += amount;
}
if (sum !== 32 * ether_to_gwei) { return false } {
return true
}
}

export function validatePayload(
data: any,
schema: any,
): ErrorObject[] | undefined | null | boolean {
const ajv = new Ajv()
ajv.addKeyword({
keyword: 'validDpositAmounts',
validate: validDpositAmounts,
errors: true,
});
const validate = ajv.compile(schema)
const isValid = validate(data)
if (!isValid) {
Expand Down
10 changes: 9 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export const definitionSchema = {
required: ['fee_recipient_address', 'withdrawal_address'],
},
},
deposit_amounts: {
type: 'array',
items: {
type: 'string',
pattern: "^[0-9]+$",
},
validDpositAmounts:true
},
},
required: ['name', 'operators', 'validators'],
required: ['name','operators', 'validators'],
}
9 changes: 5 additions & 4 deletions src/verification/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ export const signingRoot = (


const verifyLockData = async (clusterLock: ClusterLock): Promise<boolean> => {
const ec = new elliptic.ec('secp256k1')
const validators = clusterLock.distributed_validators
const nodeSignatures = clusterLock.node_signatures
const ec = new elliptic.ec('secp256k1');
const validators = clusterLock.distributed_validators;
const nodeSignatures = clusterLock.node_signatures;

const depositDomain = computeDomain(
fromHexString(DOMAIN_DEPOSIT),
clusterLock.cluster_definition.fork_version,
Expand All @@ -344,7 +345,7 @@ const verifyLockData = async (clusterLock: ClusterLock): Promise<boolean> => {
const validatorPublicShares = validator.public_shares;
const distributedPublicKey = validator.distributed_public_key;


//Needed in signature_aggregate verification
for (const element of validatorPublicShares) {
pubShares.push(fromHexString(element))
}
Expand Down
32 changes: 31 additions & 1 deletion test/methods.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ethers } from 'ethers'
import { Client, validateClusterLock } from '../src/index'
import { clusterConfigV1X8, clusterLockV1X7, clusterLockV1X8 } from './fixtures.js'
import { clusterConfigV1X7, clusterConfigV1X8, clusterLockV1X7, clusterLockV1X8 } from './fixtures.js'
import { SDK_VERSION } from '../src/constants'
import { Base } from '../src/base'
import { validatePayload } from '../src/ajv'
Expand Down Expand Up @@ -70,6 +70,36 @@ describe('Cluster Client', () => {
}
})

test('createClusterDefinition should accept a configuration without deposit_amounts ', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve({ config_hash: mockConfigHash }))

const configHash = await clientInstance.createClusterDefinition({
...clusterConfigV1X7,
})

expect(configHash).toEqual(mockConfigHash)
})

test('createClusterDefinition should throw on not valid deposit_amounts ', async () => {
clientInstance['request'] = jest
.fn()
.mockReturnValue(Promise.resolve({ config_hash: mockConfigHash }))
try {
await clientInstance.createClusterDefinition({
...clusterConfigV1X7,
deposit_amounts: ["34000000"]
})

} catch (error: any) {
console.log(error.message)
expect(error.message).toEqual(
"Schema compilation errors', must pass \"validDpositAmounts\" keyword validation",
)
}
})

test('validatePayload should throw an error on empty schema', async () => {
try {
validatePayload({ ...clusterConfigV1X8, operators: [] }, '')
Expand Down

0 comments on commit b4e19bb

Please sign in to comment.