Skip to content
Verified contractPartial

Vote

Address0xbdae4cb65e1c68cd9cd41b73c93ae3d331acad5c
Balance

0 ETH

 

Transactions

62

 

Compiler

solc 0.4.21

 

Same code

Only this contract

 

Details

Compiler
solc 0.4.21+commit.dfe3193c
Language
solidity
Optimization
Yes, 200 runs
License
Not stated
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
5,614,833
Creation transaction
0x57315347a5…fed3a024fd

Vote.sol

pragma solidity ^0.4.21;

contract Ownable {
    address public owner;


    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);


    /**
    * @dev The Ownable constructor sets the original `owner` of the contract to the sender
    * account.
    */
    function Ownable() public {
        owner = msg.sender;
    }

    /**
    * @dev Throws if called by any account other than the owner.
    */
    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    /**
    * @dev Allows the current owner to transfer control of the contract to a newOwner.
    * @param newOwner The address to transfer ownership to.
    */
    function transferOwnership(address newOwner) public onlyOwner {
        require(newOwner != address(0));
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

}

contract Vote is Ownable {
    // Candidate registered
    event CandidateRegistered(uint candidateId, string candidateName, string candidateDescription);
    // Vote cast
    event VoteCast(uint candidateId);

    struct Candidate {
        uint candidateId;
        string candidateName;
        string candidateDescription;
    }

    uint internal salt;
    string public voteName;
    uint public totalVotes;

    // mapping of candidate IDs to votes
    mapping (uint => uint) public voteCount;
    // mapping of scerets to vote status
    mapping (bytes32 => bool) internal canVote;
    // counter/mapping of candidates
    uint public nextCandidateId = 1;
    mapping (uint => Candidate) public candidateDirectory;

    function Vote(uint _salt, string _voteName, bytes32[] approvedHashes) public {
        salt = _salt;
        voteName = _voteName;
        totalVotes = approvedHashes.length;
        for (uint i; i < approvedHashes.length; i++) {
            canVote[approvedHashes[i]] = true;
        }
    }

    // Allows the owner to register new candidates
    function registerCandidate(string candidateName, string candidateDescription) public onlyOwner {
        uint candidateId = nextCandidateId++;
        candidateDirectory[candidateId] = Candidate(candidateId, candidateName, candidateDescription);
        emit CandidateRegistered(candidateId, candidateName, candidateDescription);
    }

    // get candidate information by id
    function candidateInformation(uint candidateId) public view returns (string name, string description) {
        Candidate storage candidate = candidateDirectory[candidateId];
        return (candidate.candidateName, candidate.candidateDescription);
    }

    // Users can only vote by providing a secret uint s.t. candidateDirectory[keccak256(uint, salt)] == true
    function castVote(uint secret, uint candidateId) public {
        bytes32 claimedApprovedHash = keccak256(secret, salt); // keccak256(secret) vulnerable to a rainbow table attack
        require(canVote[claimedApprovedHash]);
        canVote[claimedApprovedHash] = false;
        voteCount[candidateId] += 1;

        emit VoteCast(candidateId);
    }
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.