diff --git a/src/main.nr b/src/main.nr index 6519562..9debe37 100644 --- a/src/main.nr +++ b/src/main.nr @@ -1,29 +1,32 @@ 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, // admin can end vote - tally: Map>, // we will store candidate as key and number of votes as value - vote_ended: PublicMutable, // vote_ended is boolean - active_at_block: SharedImmutable, // when people can start voting + use dep::aztec::{ + keys::getters::get_public_keys, + macros::{storage::storage, functions::{public, initializer, private, internal}} + }; + #[storage] + struct Storage { + admin: PublicMutable, // admin can end vote + tally: Map, Context>, // we will store candidate as key and number of votes as value + vote_ended: PublicMutable, // vote_ended is boolean + active_at_block: SharedImmutable, // 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 @@ -31,15 +34,15 @@ contract EasyPrivateVoting { 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);