From 0d635cfd24a59af3c54a88caad69f01519b83e46 Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Thu, 2 Nov 2023 19:16:40 +0100 Subject: [PATCH 1/2] Added temporary SIKs to elections --- package.json | 2 +- src/core/election.ts | 1 + src/types/election/election.ts | 11 +++++ src/types/election/unpublished.ts | 9 ++++ test/integration/zk.test.ts | 69 ++++++++++++++++++++++++++++++- 5 files changed, 89 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 886c90aa..6cd5616e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/election.ts b/src/core/election.ts index 06287897..8fa89f30 100644 --- a/src/core/election.ts +++ b/src/core/election.ts @@ -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, }, }, }; diff --git a/src/types/election/election.ts b/src/types/election/election.ts index 362323a7..ba71f609 100644 --- a/src/types/election/election.ts +++ b/src/types/election/election.ts @@ -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 */ @@ -166,6 +171,7 @@ export abstract class Election { protected _questions: IQuestion[]; protected _census: Census; protected _maxCensusSize: number; + protected _temporarySecretIdentity: boolean; protected _addSDKVersion: boolean; /** @@ -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; } } @@ -248,6 +255,10 @@ export abstract class Election { return this._maxCensusSize; } + get temporarySecretIdentity(): boolean { + return this._temporarySecretIdentity; + } + get addSDKVersion(): boolean { return this._addSDKVersion; } diff --git a/src/types/election/unpublished.ts b/src/types/election/unpublished.ts index 4423289b..51e9c0fb 100644 --- a/src/types/election/unpublished.ts +++ b/src/types/election/unpublished.ts @@ -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; } @@ -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; } diff --git a/test/integration/zk.test.ts b/test/integration/zk.test.ts index 52844e2e..fd057f05 100644 --- a/test/integration/zk.test.ts +++ b/test/integration/zk.test.ts @@ -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; @@ -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); }); From f7fc3039cc0971f4f0e250fe5338ce01b3ece148 Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Thu, 2 Nov 2023 20:19:14 +0100 Subject: [PATCH 2/2] Updated `CHANGELOG` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2622ab8b..6c267363 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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