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

Upgraded protobuf and added temporary SIKs to election properties #297

Merged
merged 2 commits into from
Nov 2, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Removed faucet path from default URLs.
- Updated `@vocdoni/proto` dependency to `1.15.4`.

### Added

- Import, export and delete census functionality in census service.
- Added new election parameter `temporarySecretIdentity` for deleting temporary SIKs once election is finished.

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
"@ethersproject/units": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@size-limit/file": "^8.2.4",
"@vocdoni/proto": "1.15.3",
"@vocdoni/proto": "1.15.4",
"axios": "0.27.2",
"blake2b": "^2.1.4",
"iso-language-codes": "^1.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/core/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export abstract class ElectionCore extends TransactionCore {
censusOrigin: this.censusOriginFromCensusType(election.census.type),
metadata: cid,
maxCensusSize: election.maxCensusSize ?? election.census.size ?? undefined,
tempSIKs: election.temporarySecretIdentity ?? false,
},
},
};
Expand Down
11 changes: 11 additions & 0 deletions src/types/election/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ export interface IElectionParameters {
*/
maxCensusSize?: number;

/**
* Is used to remove the secret identities of the voters once the process is done.
*/
temporarySecretIdentity?: boolean;

/**
* Used to add the SDK version to the election metadata
*/
Expand All @@ -166,6 +171,7 @@ export abstract class Election {
protected _questions: IQuestion[];
protected _census: Census;
protected _maxCensusSize: number;
protected _temporarySecretIdentity: boolean;
protected _addSDKVersion: boolean;

/**
Expand All @@ -187,6 +193,7 @@ export abstract class Election {
this._questions = params.questions ?? [];
this._census = params.census;
this._maxCensusSize = params.maxCensusSize;
this._temporarySecretIdentity = params.temporarySecretIdentity ?? false;
this._addSDKVersion = params.addSDKVersion ?? true;
}
}
Expand Down Expand Up @@ -248,6 +255,10 @@ export abstract class Election {
return this._maxCensusSize;
}

get temporarySecretIdentity(): boolean {
return this._temporarySecretIdentity;
}

get addSDKVersion(): boolean {
return this._addSDKVersion;
}
Expand Down
9 changes: 9 additions & 0 deletions src/types/election/unpublished.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class UnpublishedElection extends Election {
this.voteType = UnpublishedElection.fullVoteType(params.voteType);
this.questions = params.questions ?? [];
this.maxCensusSize = params.maxCensusSize;
this.temporarySecretIdentity = params.temporarySecretIdentity;
this.addSDKVersion = params.addSDKVersion ?? true;
}

Expand Down Expand Up @@ -220,6 +221,14 @@ export class UnpublishedElection extends Election {
this._maxCensusSize = value;
}

get temporarySecretIdentity(): boolean {
return super.temporarySecretIdentity;
}

set temporarySecretIdentity(value: boolean) {
this._temporarySecretIdentity = value;
}

get addSDKVersion(): boolean {
return super.addSDKVersion;
}
Expand Down
69 changes: 67 additions & 2 deletions test/integration/zk.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// @ts-ignore
import { clientParams, setFaucetURL } from './util/client.params';
import { Election, PlainCensus, WeightedCensus, VocdoniSDKClient, Vote } from '../../src';
import {
AnonymousVote,
Election,
ElectionStatus,
PlainCensus,
VocdoniSDKClient,
Vote,
WeightedCensus,
} from '../../src';
import { Wallet } from '@ethersproject/wallet';
// @ts-ignore
import { waitForElectionReady } from './util/client.utils';
import { AnonymousVote } from '../../src';

let client: VocdoniSDKClient;
let wallet: Wallet;
Expand Down Expand Up @@ -230,4 +237,62 @@ describe('zkSNARK test', () => {
expect(election.voteCount).toEqual(numVotes);
});
}, 720000);
it('should create an anonymous election with temporary SIKs and they should be removed once the election is finished', async () => {
const census = new PlainCensus();
const creator = client.wallet;
const voter1 = Wallet.createRandom();
const voter2 = Wallet.createRandom();
census.add(voter1.address);
census.add(voter2.address);

const election = createElection(census, {
anonymous: true,
});
election.temporarySecretIdentity = true;

await client.createAccount();

await client
.createElection(election)
.then((electionId) => {
expect(electionId).toMatch(/^[0-9a-fA-F]{64}$/);
client.setElectionId(electionId);
return waitForElectionReady(client, electionId);
})
.then(async () => {
await expect(async () => {
await client.anonymousService.fetchAccountSIK(voter1.address);
}).rejects.toThrow();
await expect(async () => {
await client.anonymousService.fetchAccountSIK(voter2.address);
}).rejects.toThrow();

client.wallet = voter1;
await client.submitVote(new Vote([0]));
client.wallet = voter2;
await client.submitVote(new Vote([1]));

await expect(async () => {
await client.anonymousService.fetchAccountSIK(voter1.address);
}).resolves;
await expect(async () => {
await client.anonymousService.fetchAccountSIK(voter2.address);
}).resolves;

client.wallet = creator;
return client.endElection();
})
.then(async () => {
let electionInfo = await client.fetchElection();
while (electionInfo.status === ElectionStatus.ENDED) {
electionInfo = await client.fetchElection();
}
await expect(async () => {
await client.anonymousService.fetchAccountSIK(voter1.address);
}).rejects.toThrow();
await expect(async () => {
await client.anonymousService.fetchAccountSIK(voter2.address);
}).rejects.toThrow();
});
}, 720000);
});
Loading