Census generation

Our entity is ready, now it can handle voting processes. But before we can create one, we need a census of the people who can vote.

As we said, digital identities arise from a cryptographic keypair. The ideal scenario is when the identity is generated on the user's device and only the public key is shared, but for the sake of simplicity, we will create a few identities ourselves.

let votersPublicKeys: string[] = []
function populateCensusPublicKeys() {
for (let i = 0; i < 10; i++) {
const wallet = Wallet.createRandom()
console.log("Voter's public keys", votersPublicKeys)

Which results in

Voter's public keys [

Then we can upload the census claims to a gateway and publish it:

import { addCensus, addClaimBulk, getRoot, publishCensus } from "dvote-js/dist/api/census"
let merkleTreeOrigin: string
let merkleRoot: string
async function publishVoteCensus() {
// Prepare the census parameters
const censusName = "Vilafourier all members #" + Math.random().toString().substr(2, 6)
const adminPublicKeys = [await entityWallet["signingKey"].publicKey]
const publicKeyClaims = votersPublicKeys.map(k => digestHexClaim(k)) // hash the keys
// As the census does not exist yet, we create it (optional when it exists)
let { censusId } = await addCensus(censusName, adminPublicKeys, pool, entityWallet)
console.log(`Census added: "${censusName}" with ID ${censusId}`)
// Add claims to the new census
let result = await addClaimBulk(censusId, publicKeyClaims, true, pool, entityWallet)
console.log("Added", votersPublicKeys.length, "claims to", censusId)
if (result.invalidClaims.length > 0) console.error("Invalid claims", result.invalidClaims)
merkleRoot = await getRoot(censusId, pool)
console.log("Census Merkle Root", merkleRoot)
// Make it available publicly
merkleTreeOrigin = await publishCensus(censusId, pool, entityWallet)
console.log("Census published on", merkleTreeOrigin)

Which results in:

Census added: "Vilafourier all members #550262" with ID 0x1048d8cB20fE806389802ba48E2647dc85aB779a/0x8e0c00cda17a132e2ce6c89072cc2037b0800c280c1ddba318eb2620fae2ff16
Added 10 claims to 0x1048d8cB20fE806389802ba48E2647dc85aB779a/0x8e0c00cda17a132e2ce6c89072cc2037b0800c280c1ddba318eb2620fae2ff16
Census Merkle Root 0xbf8572159929b4e37c40e2ce18fd46156e750a4aac1f06dbda3237edccc81115
Census published on ipfs://QmcTuUJbN1tEWA3nqHFU8N8iuUN2ymJoqW4UcKBzHuYrPw

Cool! Our entity is ready, and our first census is now public.