Skip to content

Commit

Permalink
Github Action: Update contract
Browse files Browse the repository at this point in the history
  • Loading branch information
critesjosh committed Sep 26, 2024
1 parent cd7fe09 commit 25b645f
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/main.nr
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
mod test;
use dep::aztec::macros::aztec;

#[aztec]
contract EasyPrivateVoting {
use dep::aztec::prelude::{AztecAddress, Map, PublicMutable, SharedImmutable};
use dep::aztec::keys::getters::get_historical_public_keys;
#[aztec(storage)]
struct Storage {
admin: PublicMutable<AztecAddress>, // admin can end vote
tally: Map<Field, PublicMutable<Field>>, // we will store candidate as key and number of votes as value
vote_ended: PublicMutable<bool>, // vote_ended is boolean
active_at_block: SharedImmutable<u32>, // when people can start voting
use dep::aztec::{
keys::getters::get_public_keys,
macros::{storage::storage, functions::{public, initializer, private, internal}}
};
#[storage]
struct Storage<Context> {
admin: PublicMutable<AztecAddress, Context>, // admin can end vote
tally: Map<Field, PublicMutable<Field, Context>, Context>, // we will store candidate as key and number of votes as value
vote_ended: PublicMutable<bool, Context>, // vote_ended is boolean
active_at_block: SharedImmutable<u32, Context>, // when people can start voting
}

#[aztec(public)]
#[aztec(initializer)] // annotation to mark function as a constructor
#[public]
#[initializer] // annotation to mark function as a constructor
fn constructor(admin: AztecAddress) {
storage.admin.write(admin);
storage.vote_ended.write(false);
storage.active_at_block.initialize(context.block_number() as u32);
}

#[aztec(private)] // annotation to mark function as private and expose private context
#[private] // annotation to mark function as private and expose private context
fn cast_vote(candidate: Field) {
// Below, we make sure to get our nullifier public key at a specific block. By pinning the nullifier public key at a specific block,
// rotating keys will have no effect on the nullifier being produced, and voting again after will fail because the same nullifier is computed each time the user votes.
let header_at_active_at_block = context.get_header_at(storage.active_at_block.read_private());
let msg_sender_npk_m_hash = get_historical_public_keys(header_at_active_at_block, context.msg_sender()).npk_m.hash();
let msg_sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash();

let secret = context.request_nsk_app(msg_sender_npk_m_hash); // get secret key of caller of function
let nullifier = std::hash::pedersen_hash([context.msg_sender().to_field(), secret]); // derive nullifier from sender and secret
context.push_nullifier(nullifier);
EasyPrivateVoting::at(context.this_address()).add_to_tally_public(candidate).enqueue(&mut context);
}

#[aztec(public)]
#[aztec(internal)]
#[public]
#[internal]
fn add_to_tally_public(candidate: Field) {
assert(storage.vote_ended.read() == false, "Vote has ended"); // assert that vote has not ended
let new_tally = storage.tally.at(candidate).read() + 1;
storage.tally.at(candidate).write(new_tally);
}

#[aztec(public)]
#[public]
fn end_vote() {
assert(storage.admin.read().eq(context.msg_sender()), "Only admin can end votes"); // assert that caller is admin
storage.vote_ended.write(true);
Expand Down

0 comments on commit 25b645f

Please sign in to comment.