Skip to content

Commit

Permalink
feat: updated params of registerValidatorsWithKeyStores
Browse files Browse the repository at this point in the history
- keystores: array of keystore files json string
- passwords: array of keystore files password
- operatorIds: array of operator ids
- operatorKeys: array of operator key strings
  • Loading branch information
Mohsen-T committed Nov 8, 2023
1 parent 8479582 commit f74b717
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
57 changes: 37 additions & 20 deletions test/helpers/contract-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,48 +235,65 @@ export const reactivate = async (ownerId: number, operatorIds: number[], amount:
return reactivatedCluster.eventsByName.ClusterReactivated[0].args;
};

export const registerValidatorsWithKeystore = async (
export const registerValidatorsWithKeystores = async (
ownerId: number,
numberOfValidators: number,
amount: string,
keystore: string,
password: string,
keystores: string[],
passwords: string[],
operatorIds: number[],
operatorKeys: string[],
gasGroups?: GasGroup[],
) => {
const validators: any = [];
let args: any;

if (operatorIds.length === operatorKeys.length) {
if (keystores.length === passwords.length && operatorIds.length === operatorKeys.length) {
// keystores.length is same with the number of validators, because each validator has a corresponding keystore file.

const operators = operatorKeys.map((operatorKey, index) => ({
id: operatorIds[index],
operatorKey,
}));

const ssvKeys = new SSVKeys();
const { publicKey, privateKey } = await ssvKeys.extractKeys(keystore, password);

const threshold = await ssvKeys.createThreshold(privateKey, operators);
const encShares: EncryptShare[] = await ssvKeys.encryptShares(operators, threshold.shares);

// Register validators to contract
for (let i = 0; i < numberOfValidators; i++) {
const shares = DataGenerator.shares(DB.validators.length);
for (let i = 0; i < keystores.length; i++) {
const { publicKey, privateKey } = await ssvKeys.extractKeys(keystores[i], passwords[i]);

const threshold = await ssvKeys.createThreshold(privateKey, operators);
const encryptedShares: EncryptShare[] = await ssvKeys.encryptShares(operators, threshold.shares);

const keyShares = new KeyShares();
const payload = await keyShares.buildPayload(
{
publicKey,
operators,
encryptedShares,
},
{
ownerAddress: DB.owners[ownerId].address,
ownerNonce: 1,
privateKey,
},
);

await DB.ssvToken.connect(DB.owners[ownerId]).approve(DB.ssvNetwork.contract.address, amount);
const result = await trackGas(
DB.ssvNetwork.contract.connect(DB.owners[ownerId]).registerValidator(publicKey, operatorIds, shares, amount, {
validatorCount: 0,
networkFeeIndex: 0,
index: 0,
balance: 0,
active: true,
}),
DB.ssvNetwork.contract
.connect(DB.owners[ownerId])
.registerValidator(payload.publicKey, payload.operatorIds, payload.sharesData, amount, {
validatorCount: 0,
networkFeeIndex: 0,
index: 0,
balance: 0,
active: true,
}),
gasGroups,
);
args = result.eventsByName.ValidatorAdded[0].args;
DB.validators.push({ publicKey, operatorIds, shares });
validators.push({ publicKey, shares });
DB.validators.push(payload);
validators.push({ publicKey: payload.publicKey, shares: payload.sharesData });
}
}

Expand Down
7 changes: 3 additions & 4 deletions test/validators/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ describe('Register Validator Tests', () => {
});

it('Register validator with keyshare and operators json using ssv-keys', async () => {
await helpers.registerValidatorsWithKeystore(
await helpers.registerValidatorsWithKeystores(
2,
1,
minDepositAmount,
JSON.stringify(keystore),
'testtest',
[JSON.stringify(keystore)],
['testtest'],
operatorIds,
operatorKeys,
);
Expand Down

0 comments on commit f74b717

Please sign in to comment.