Skip to content

Commit

Permalink
Added CSP signature type
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Jan 31, 2024
1 parent fa67058 commit 3b6d15d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Buffer } from 'buffer';
import invariant from 'tiny-invariant';
import { AccountCore } from './core/account';
import { ElectionCore } from './core/election';
import { VoteCore } from './core/vote';
import { CspProofType, VoteCore } from './core/vote';
import {
Account,
AllElectionStatus,
Expand Down Expand Up @@ -863,6 +863,7 @@ export class VocdoniSDKClient {
censusProof = {
address: await this.wallet.getAddress(),
signature: vote.signature,
proof_type: vote.proof_type,
};
yield {
key: VoteSteps.GET_PROOF,
Expand Down Expand Up @@ -1005,8 +1006,8 @@ export class VocdoniSDKClient {
return this.cspService.cspSign(this.electionId, address, token);
}

cspVote(vote: Vote, signature: string) {
return this.cspService.cspVote(vote, signature);
cspVote(vote: Vote, signature: string, proof_type?: CspProofType) {
return this.cspService.cspVote(vote, signature, proof_type);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/core/vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export type VotePackage = {
votes: VoteValues;
};

export enum CspProofType {
ECDSA = ProofCA_Type.ECDSA,
ECDSA_PIDSALTED = ProofCA_Type.ECDSA_PIDSALTED,
ECDSA_BLIND = ProofCA_Type.ECDSA_BLIND,
ECDSA_BLIND_PIDSALTED = ProofCA_Type.ECDSA_BLIND_PIDSALTED,
}

export abstract class VoteCore extends TransactionCore {
/**
* Cannot be constructed.
Expand Down Expand Up @@ -130,7 +137,7 @@ export abstract class VoteCore extends TransactionCore {

// Populate the proof
const caProof = ProofCA.fromPartial({
type: ProofCA_Type.ECDSA_BLIND_PIDSALTED,
type: proof.proof_type ? (proof.proof_type as unknown as ProofCA_Type) : ProofCA_Type.ECDSA_BLIND_PIDSALTED,
signature: new Uint8Array(Buffer.from(strip0x(proof.signature), 'hex')),
bundle: this.cspCaBundle(electionId, proof.address),
});
Expand Down
2 changes: 2 additions & 0 deletions src/services/census.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CensusType, ICensusParticipant, PlainCensus, WeightedCensus } from '../
import { CensusAPI, ICensusImportResponse, ICensusPublishResponse, WalletAPI } from '../api';
import { Wallet } from '@ethersproject/wallet';
import invariant from 'tiny-invariant';
import { CspProofType } from '../core/vote';

interface CensusServiceProperties {
auth: CensusAuth;
Expand Down Expand Up @@ -57,6 +58,7 @@ export type CspCensusProof = {
address: string;
signature: string;
weight?: bigint;
proof_type?: CspProofType;
};

export class CensusService extends Service implements CensusServiceProperties {
Expand Down
9 changes: 5 additions & 4 deletions src/services/csp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import invariant from 'tiny-invariant';
import { CensusType, CspVote, Election, Vote } from '../types';
import { CspAPI, ICspInfoResponse } from '../api/csp';
import { CensusBlind, getBlindedPayload } from '../util/blind-signing';
import { CspProofType } from '../core/vote';

interface CspServiceProperties {
info: ICspInfoResponse;
Expand Down Expand Up @@ -64,11 +65,11 @@ export class CspService extends Service implements CspServiceProperties {
);
}

cspVote(vote: Vote, signature: string): CspVote {
return CspService.cspVote(vote, signature);
cspVote(vote: Vote, signature: string, proof_type?: CspProofType): CspVote {
return CspService.cspVote(vote, signature, proof_type);
}

static cspVote(vote: Vote, signature: string): CspVote {
return new CspVote(vote.votes, signature);
static cspVote(vote: Vote, signature: string, proof_type?: CspProofType): CspVote {
return new CspVote(vote.votes, signature, proof_type);
}
}
14 changes: 13 additions & 1 deletion src/types/vote/csp.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Vote } from './vote';
import { CspProofType } from '../../core/vote';

export class CspVote extends Vote {
private _signature: string;
private _proof_type: CspProofType;

/**
* Constructs a csp vote
*
* @param votes The list of votes values
* @param signature The CSP signature
* @param proof_type The CSP proof type
*/
public constructor(votes: Array<number | bigint>, signature: string) {
public constructor(votes: Array<number | bigint>, signature: string, proof_type?: CspProofType) {
super(votes);
this.signature = signature;
this.proof_type = proof_type;
}

get signature(): string {
Expand All @@ -21,4 +25,12 @@ export class CspVote extends Vote {
set signature(value: string) {
this._signature = value;
}

get proof_type(): CspProofType {
return this._proof_type;
}

set proof_type(value: CspProofType) {
this._proof_type = value;
}
}
3 changes: 2 additions & 1 deletion test/integration/csp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CspCensus } from '../../src';
import { clientParams, setFaucetURL } from './util/client.params';
// @ts-ignore
import { waitForElectionReady } from './util/client.utils';
import { CspProofType } from '../../src/core/vote';

const CSP_URL = process.env.BLINDCSP_URL ?? 'https://csp-dev-simplemath.vocdoni.net/v1';
const CSP_PUBKEY = process.env.BLINDCSP_PUBKEY ?? '025de8cb8de1005aa939c1403e37e1fa165ebc758da49cb37215c6237d01591104';
Expand Down Expand Up @@ -66,7 +67,7 @@ describe('CSP tests', () => {
step0.authToken
)) as ICspFinalStepResponse;
const signature = await pClient.cspSign(participant.address, step1.token);
const vote = pClient.cspVote(new Vote([index % 2]), signature);
const vote = pClient.cspVote(new Vote([index % 2]), signature, CspProofType.ECDSA_BLIND_PIDSALTED);
return pClient.submitVote(vote);
})
);
Expand Down

0 comments on commit 3b6d15d

Please sign in to comment.