Skip to content

Commit

Permalink
Added new anonymous function hasRegisteredSIK
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Nov 29, 2023
1 parent a2fe1c0 commit bacce14
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- New anonymous function `hasRegisteredSIK` for checking if a user has registered a SIK.

## [0.6.0] - 2023-11-28

### Added
Expand Down
10 changes: 8 additions & 2 deletions src/services/anonymous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@ export class AnonymousService extends Service implements AnonymousServicePropert
);
}

async fetchAccountSIK(address: string) {
async fetchAccountSIK(address: string): Promise<string> {
invariant(this.url, 'No URL set');
return ZkAPI.sik(this.url, address);
return ZkAPI.sik(this.url, address).then((response) => response.sik);
}

async hasRegisteredSIK(address: string, signature: string, password?: string): Promise<boolean> {
return Promise.all([AnonymousService.calcSik(address, signature, password), this.fetchAccountSIK(address)])
.then(([calcSik, fetchSik]) => calcSik === fetchSik)
.catch(() => false);
}

async fetchZKProof(address: string) {
Expand Down
53 changes: 53 additions & 0 deletions test/integration/zk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,59 @@ describe('zkSNARK test', () => {
expect(votesLeftCount).toEqual(8); // The user voted twice
});
}, 285000);
it('should create an anonymous election, vote and check if the user has the SIK registered', async () => {
const census = new PlainCensus();
const voter = VocdoniSDKClient.generateWalletFromData('just dummy data' + Math.random());
census.add(voter.address);

const election = createElection(
census,
{
anonymous: true,
},
{
maxVoteOverwrites: 9,
}
);

let nullifier: string;
let signature: string;

await client.createAccount();

await client
.createElection(election)
.then((electionId) => {
expect(electionId).toMatch(/^[0-9a-fA-F]{64}$/);
client.setElectionId(electionId);
return client.fetchElection();
})
.then((publishedElection) => {
expect(publishedElection.electionType.anonymous).toBeTruthy();
return waitForElectionReady(client, publishedElection.id);
})
.then(async () => {
client.wallet = voter;
signature = await client.anonymousService.signSIKPayload(voter);

const vote = new AnonymousVote([0], signature, 'realpassword');
nullifier = await AnonymousService.calcVoteId(signature, 'realpassword', client.electionId);

const hasAlreadyVoted = await client.hasAlreadyVoted({ voteId: nullifier });
expect(hasAlreadyVoted).toBeFalsy();

return client.submitVote(vote);
})
.then(async (voteId) => {
expect(voteId).toEqual(nullifier);
const hasAlreadyVoted = await client.hasAlreadyVoted({ voteId });
expect(hasAlreadyVoted).toBeTruthy();
const votesLeftCount = await client.votesLeftCount({ voteId });
expect(votesLeftCount).toEqual(9);
expect(await client.anonymousService.hasRegisteredSIK(voter.address, signature, 'realpassword')).toBeTruthy();
expect(await client.anonymousService.hasRegisteredSIK(voter.address, signature, 'wrongpassword')).toBeFalsy();
});
}, 285000);
it('should create a weighted anonymous election and vote successfully', async () => {
const census = new WeightedCensus();
const voter1 = Wallet.createRandom();
Expand Down

0 comments on commit bacce14

Please sign in to comment.