Skip to content

Commit

Permalink
Update example (#50)
Browse files Browse the repository at this point in the history
* Enable non-anonymous voting example

* Fix chainID for ballot signing

* Address PR comments

* Upgrade csp to stage
  • Loading branch information
NateWilliams2 authored Mar 14, 2022
1 parent f0ab644 commit 927f929
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
58 changes: 34 additions & 24 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createAnonymousElection, createOrganization, createSignedElection, deleteOrganization, getElectionPriv, getOrganizationPriv, getOrganizationListPriv, listElectionsPriv, setOrganizationMetadata } from "./sections/integrator"
import { createIntegrator, deleteIntegrator } from "./sections/superadmin"
import { getElectionSecretInfoPub, getElectionListPub, getElectionInfoPub, getOrganizationPub, getElectionSharedKey, getElectionSharedKeyCustom, getCspSigningTokenPlain, getCspSigningTokenBlind, getCspSigningTokenPlainCustom, getCspSigningTokenBlindCustom, getCspPlainSignature, getCspBlindSignature, getBlindedPayload, getProofFromBlindSignature, getBallotPayload, submitBallot, getBallot } from "./sections/voter"
import { getElectionSecretInfoPub, getElectionListPub, getElectionInfoPub, getOrganizationPub, getElectionSharedKey, getElectionSharedKeyCustom, getCspSigningTokenPlain, getCspSigningTokenBlind, getCspSigningTokenPlainCustom, getCspSigningTokenBlindCustom, getCspPlainSignature,getProofFromPlainSignature, getCspBlindSignature, getBlindedPayload, getProofFromBlindSignature, getBallotPayload, submitBallot, getBallot, getPlainPayload } from "./sections/voter"
import { Wallet } from "@ethersproject/wallet"
import { wait } from "./util/wait"
import { fakeSign } from "./sections/fake-csp"

async function main() {
const encryptedResults = true
const confidential = true
const anonymous = true

// VOCDONI INTERNAL

Expand All @@ -23,49 +24,58 @@ async function main() {
await getOrganizationPriv(organizationId, integratorApiKey)
await getOrganizationListPriv(integratorApiKey)

// const { electionId: electionId1 } = await createSignedElection(organizationId, encryptedResults, confidential, integratorApiKey)
const { electionId: electionId1 } = await createAnonymousElection(organizationId, encryptedResults, confidential, integratorApiKey)
var electionId: string
if (anonymous) {
electionId = await createAnonymousElection(organizationId, encryptedResults, confidential, integratorApiKey)
} else {
electionId = await createSignedElection(organizationId, encryptedResults, confidential, integratorApiKey)
}

const electionList = await listElectionsPriv(organizationId, integratorApiKey)
const election1DetailsPriv = await getElectionPriv(electionId1, integratorApiKey)
// const election2Details = await getElectionPriv(electionId2, integratorApiKey)
const electionDetailsPriv = await getElectionPriv(electionId, integratorApiKey)

// VOTER ENDPOINTS (frontend)

// const orgData = await getOrganizationPub(organizationId, orgApiToken)
// const electionListPub = await getElectionListPub(electionId1, "active", orgApiToken)
// const electionInfo1 = await getElectionInfoPub(electionId1, orgApiToken)
// const electionListPub = await getElectionListPub(electionId, "active", orgApiToken)
// const electionInfo = await getElectionInfoPub(electionId, orgApiToken)

const wallet = Wallet.createRandom()
const voterId = "000000000000000000000000" + wallet.address.slice(2)
const signature = fakeSign(electionId1, voterId)
const signature = fakeSign(electionId, voterId)

// key for confidential election data
// const cspSharedKey = await getElectionSharedKey(electionId1, signedElectionId, orgApiToken)
const cspSharedKey = await getElectionSharedKeyCustom(electionId1, { voterId, signature }, orgApiToken)
// const electionInfo2 = await getElectionSecretInfoPub(electionId2, cspSharedKey, orgApiToken)
const cspSharedKey = await getElectionSharedKeyCustom(electionId, { voterId, signature }, orgApiToken)

let election1DetailsPubAuth = await getElectionSecretInfoPub(electionId1, cspSharedKey, orgApiToken)
let election1DetailsPubAuth = await getElectionSecretInfoPub(electionId, cspSharedKey, orgApiToken)
while (election1DetailsPubAuth.status == "UPCOMING") {
await wait(5)
election1DetailsPubAuth = await getElectionSecretInfoPub(electionId1, cspSharedKey, orgApiToken)
election1DetailsPubAuth = await getElectionSecretInfoPub(electionId, cspSharedKey, orgApiToken)
}

var proof
if (anonymous) {
// ANONYMOUS AUTH
const tokenR = await getCspSigningTokenBlindCustom(electionId, { voterId, signature }, orgApiToken)

const { hexBlinded: blindedPayload, userSecretData } = getBlindedPayload(electionId, tokenR, wallet)

const blindSignature = await getCspBlindSignature(electionId, tokenR, blindedPayload, orgApiToken)
proof = getProofFromBlindSignature(blindSignature, userSecretData, wallet)
} else {
// NON ANONYMOUS AUTH
const tokenR = await getCspSigningTokenPlain(electionId, signature, orgApiToken)
const payload = await getPlainPayload(electionId, tokenR, wallet)
const plainSignature = await getCspPlainSignature(electionId, tokenR, payload, orgApiToken)
proof = getProofFromPlainSignature(plainSignature, wallet)
}

// NON ANONYMOUS AUTH
// const tokenR = await getCspSigningTokenPlain(electionId1, signedElectionId, orgApiToken)
// const tokenR = await getCspSigningTokenPlainCustom(electionId1, { voterId, signature }, orgApiToken)
// const plainSignature = await getCspPlainSignature(electionId1, tokenR, payload, orgApiToken)

// ANONYMOUS AUTH
// const tokenR = await getCspSigningTokenBlind(electionId1, signedElectionId, orgApiToken)
const tokenR = await getCspSigningTokenBlindCustom(electionId1, { voterId, signature }, orgApiToken)

const { hexBlinded: blindedPayload, userSecretData } = getBlindedPayload(electionId1, tokenR, wallet)

const blindSignature = await getCspBlindSignature(electionId1, tokenR, blindedPayload, orgApiToken)
const proof = getProofFromBlindSignature(blindSignature, userSecretData, wallet)
const ballot = getBallotPayload(electionId1, proof, encryptedResults, election1DetailsPubAuth.encryptionPubKeys)
const { nullifier } = await submitBallot(electionId1, election1DetailsPubAuth.chainId, ballot, wallet, orgApiToken)
const ballot = getBallotPayload(electionId, proof, encryptedResults, election1DetailsPubAuth.encryptionPubKeys)
const { nullifier } = await submitBallot(electionId, election1DetailsPubAuth.chainId, ballot, wallet, orgApiToken)
let ballotDetails = await getBallot(nullifier, orgApiToken)
// optionally wait for the ballot to be registered if not already
// while (!ballotDetails.registered) {
Expand Down
4 changes: 2 additions & 2 deletions example/sections/integrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export async function createSignedElection(organizationId: string, hiddenResults
}

console.log("Created election with ID", electionId)
return { electionId }
return electionId
}

export async function createAnonymousElection(organizationId: string, hiddenResults: boolean, confidential: boolean, apiKey: string) {
Expand Down Expand Up @@ -341,7 +341,7 @@ export async function createAnonymousElection(organizationId: string, hiddenResu
}

console.log("Created anonymous election", electionId)
return { electionId }
return electionId
}

type ElectionSummary = {
Expand Down
26 changes: 23 additions & 3 deletions example/sections/voter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ export function getBlindedPayload(electionId: string, hexTokenR: string, ephemer
return { hexBlinded, userSecretData }
}

export function getPlainPayload(electionId: string, hexTokenR: string, ephemeralWallet: Wallet) {
const caBundle = CAbundle.fromPartial({
processId: new Uint8Array(hexStringToBuffer(electionId)),
address: new Uint8Array(hexStringToBuffer(ephemeralWallet.address)),
})

// hash(bundle)
const hexCaBundle = hexlify(CAbundle.encode(caBundle).finish())

return hexCaBundle
}

export function getProofFromPlainSignature(hexSignature: string, wallet: Wallet) {
const proof: IProofCA = {
type: ProofCaSignatureTypes.ECDSA_PIDSALTED,
signature: hexSignature,
voterAddress: wallet.address
}

return proof
}

export function getProofFromBlindSignature(hexBlindSignature: string, userSecretData: UserSecretData, wallet: Wallet) {
const unblindedSignature = CensusBlind.unblind(hexBlindSignature, userSecretData)

Expand Down Expand Up @@ -481,13 +503,11 @@ export async function getCspSigningTokenBlindCustom(electionId: string, proof: {
// Vote delivery
//////////////////////////////////////////////////////////////////////////

export async function submitBallot(electionId: string, chainId:string, ballot: VoteEnvelope, ephemeralWallet: Wallet, orgApiToken: string) {
export async function submitBallot(electionId: string, chainId: string, ballot: VoteEnvelope, ephemeralWallet: Wallet, orgApiToken: string) {
// Prepare
const tx = Tx.encode({ payload: { $case: "vote", vote: ballot } })
const txBytes = tx.finish()

// const chainId = await gateway.getVocdoniChainId()

const hexSignature = await BytesSignature.signTransaction(txBytes,chainId, ephemeralWallet)
const signature = new Uint8Array(Buffer.from(strip0x(hexSignature), "hex"))

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/timshannon/badgerhold/v3 v3.0.0 // indirect
github.com/vocdoni/arbo v0.0.0-20220204101222-688a2e814db0 // indirect
github.com/vocdoni/blind-csp v0.1.5-0.20220210182521-0b2d188aee8c
github.com/vocdoni/blind-csp v0.1.5-0.20220214165159-4620baa07fa4
github.com/whyrusleeping/cbor-gen v0.0.0-20220223114253-ebcc1e8ce85b // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.vocdoni.io/dvote v1.0.4-0.20220310171340-ae10a77cde5b
Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2394,8 +2394,8 @@ github.com/vocdoni/arbo v0.0.0-20211217085703-d56ab859f109/go.mod h1:6Z6HJMTIDGH
github.com/vocdoni/arbo v0.0.0-20220204101222-688a2e814db0 h1:XkX+MXxpwn14mE5daeEZnfXodXS8l8p29kOJi3IAdn4=
github.com/vocdoni/arbo v0.0.0-20220204101222-688a2e814db0/go.mod h1:4WbFeRVFdXmBwlAe6fYFj2YbF/vVecDvCVfPkfE8B24=
github.com/vocdoni/blind-ca v0.1.4/go.mod h1:4ouWDqlvXrrNS0Csf3hKA3cuDTmKh6nP7kSXF39nT4s=
github.com/vocdoni/blind-csp v0.1.5-0.20220210182521-0b2d188aee8c h1:4F7+aZxkxs+xFtHZjxyFJdkvd7UB87bdE9aqn7WaRfU=
github.com/vocdoni/blind-csp v0.1.5-0.20220210182521-0b2d188aee8c/go.mod h1:ICexXg/gDe4zFkPLuW6P0TmiPs7JnV6CmVdeFCXF4V4=
github.com/vocdoni/blind-csp v0.1.5-0.20220214165159-4620baa07fa4 h1:cXTw1cREtQHEvcUMwhtrfqiaoMb+c/f9/dgUzQKK82w=
github.com/vocdoni/blind-csp v0.1.5-0.20220214165159-4620baa07fa4/go.mod h1:xo1LOBMpTm2xWvSbBhDmSz/RSTn1Cy9/kjJ7pPx9040=
github.com/vocdoni/eth-storage-proof v0.1.4-0.20201128112323-de7513ce5e25/go.mod h1:NLA1A55raZ1VNMmKulPUm+Lu9CVetCDVuDCYk5bSYrE=
github.com/vocdoni/go-external-ip v0.0.0-20210705122950-fae6195a1d44/go.mod h1:o/kqzlz81Aq5/++p7zIRaaOiEUmCOiap7ulEAUeSQWI=
github.com/vocdoni/go-snark v0.0.0-20210709152824-f6e4c27d7319 h1:W8N7yfnWsVD3l2Sh0pTtXCDd4+LNh58lE427D1U9SVY=
Expand Down Expand Up @@ -2548,7 +2548,7 @@ go.vocdoni.io/dvote v0.6.1-0.20210206210936-a0407e833753/go.mod h1:HhMMtdnkLI3uj
go.vocdoni.io/dvote v1.0.0/go.mod h1:C9wjSWCzy33Bl2sNiF/2ie/oxcERR3uRD2G2c/NDeH0=
go.vocdoni.io/dvote v1.0.4-0.20210806163627-9494efbc5382/go.mod h1:kl66EQmAjR252HCRQL756bZQnuvcXZZ3HuaABItf+0E=
go.vocdoni.io/dvote v1.0.4-0.20211025120558-83c64f440044/go.mod h1:t2O6ExMry8na9bqrYiGH4AZ5ukXYAolUnIFGntmS63c=
go.vocdoni.io/dvote v1.0.4-0.20220207152532-2c10f1b191cd/go.mod h1:kMMB1sTNsNTDAm0vakM+51t0UcYgmhVm406sJqjqj6w=
go.vocdoni.io/dvote v1.0.4-0.20220210143454-e386915ab3d5/go.mod h1:V9ni6iQdsqGWqQzpUFKjXi1nYiJAiKPVffacoG8hYyc=
go.vocdoni.io/dvote v1.0.4-0.20220310171340-ae10a77cde5b h1:U9EwwN3zxSVc0Sn72ee+uyeL//ku5jx3wK6HqCgUeFA=
go.vocdoni.io/dvote v1.0.4-0.20220310171340-ae10a77cde5b/go.mod h1:V9ni6iQdsqGWqQzpUFKjXi1nYiJAiKPVffacoG8hYyc=
go.vocdoni.io/proto v0.1.7/go.mod h1:cyITrt7+sHmUJH06WLu69xB7LBY9c9FakFaBOe8gs/M=
Expand Down

0 comments on commit 927f929

Please sign in to comment.