Skip to content

Commit

Permalink
test(integration): ensure integration tests are using the correct use…
Browse files Browse the repository at this point in the history
…r keys
  • Loading branch information
ctrlc03 committed Jan 8, 2024
1 parent b0ee748 commit ab5e24c
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions integrationTests/ts/__tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,39 +167,49 @@ describe("integration tests", function test() {

// loop through all users and generate keypair + signup
for (let i = 0; i < users.length; i += 1) {
// generate a keypair
const keypair = new Keypair();
const user = users[i];
const timestamp = Date.now();
// signup
const stateIndex = await signup(keypair.pubKey.serialize(), contracts.maciAddress, SG_DATA, ivcpData, true);
const stateIndex = await signup(
user.keypair.pubKey.serialize(),
contracts.maciAddress,
SG_DATA,
ivcpData,
true,
);

// signup on local maci state
maciState.signUp(keypair.pubKey, BigInt(initialVoiceCredits), BigInt(timestamp));
maciState.signUp(user.keypair.pubKey, BigInt(initialVoiceCredits), BigInt(timestamp));

// publish messages
for (let j = 0; j < users[i].votes.length; j += 1) {
const user = users[i];
for (let j = 0; j < user.votes.length; j += 1) {
const isKeyChange = testCase.changeUsersKeys && j in testCase.changeUsersKeys[i];
const voteOptionIndex = isKeyChange
? testCase.changeUsersKeys?.[i][j].voteOptionIndex
: user.votes[j].voteOptionIndex;
const newVoteWeight = isKeyChange ? testCase.changeUsersKeys?.[i][j].voteWeight : user.votes[j].voteWeight;
const { nonce } = user.votes[j];
const salt = `0x${genRandomSalt().toString(16)}`;
const userPrivKey = isKeyChange ? user.changeKeypair() : keypair.privKey;

// store the previous keypair
const oldKeypair = user.keypair;
// change
if (isKeyChange) {
user.changeKeypair();
}

// actually publish it
// @todo if key change we also need a new pub key
const encryptionKey = await publish(
keypair.pubKey.serialize(),
user.keypair.pubKey.serialize(),
Number(stateIndex),
voteOptionIndex!,
nonce,
pollId,
newVoteWeight!,
contracts.maciAddress,
salt,
userPrivKey.serialize(),
// if it's a key change command, then we pass the old private key otherwise just pass the current
isKeyChange ? oldKeypair.privKey.serialize() : user.keypair.privKey.serialize(),
true,
);

Expand All @@ -209,14 +219,14 @@ describe("integration tests", function test() {
// create the command to add to the local state
const command = new PCommand(
BigInt(stateIndex),
keypair.pubKey,
user.keypair.pubKey,
BigInt(voteOptionIndex!),
BigInt(newVoteWeight!),
BigInt(nonce),
BigInt(pollId),
BigInt(salt),
);
const signature = command.sign(keypair.privKey);
const signature = command.sign(isKeyChange ? oldKeypair.privKey : user.keypair.privKey);
const message = command.encrypt(signature, Keypair.genEcdhSharedKey(encPrivKey, coordinatorKeypair.pubKey));
maciState.polls[pollId].publishMessage(message, encPubKey);
}
Expand Down

0 comments on commit ab5e24c

Please sign in to comment.