GardenBlockchain

An older contract-voting application's signing boundaries

A Solidity 0.4.21 demo and three ways to connect a UI, signer, and event listener.

The original voting demo used web3 1.0.0-beta.33 and Solidity 0.4.21. It considered three placements for signing and event delivery:

Arrangement Where signing happens Important boundary
Server-centered Backend holds signing material Server custody and request authorization
Browser-centered Wallet provider signs in the browser User approval and provider behavior
Split UI/backend Browser signs; backend listens and relays Event identity, confirmation, and relay state

The original third arrangement did not implement its Socket.IO relay. The older provider-subscription limitation was an observation from that toolchain, not a current wallet capability claim.

The historical contract

pragma solidity 0.4.21;


contract Voting {

  // @nhancv: Define candidate 
  struct Candidate {
    uint8 id;
    string name;
    uint8 point;
  }

  // @nhancv: Candidate list
  Candidate[] public candidateList;

  // @nhancv: Event
  event AddCandidateEvent(address indexed from, uint8 candidateId);
  event VoteCandidateEvent(address indexed from, uint8 candidateId);

  // @nhancv: Get length of Candidate list
  function getNumberCandidate() public view returns (uint8) {
    return uint8(candidateList.length);
  }

  // @nhancv: Addition candidate
  function addCandidate(string name) public {
    Candidate memory candidate = Candidate(uint8(candidateList.length + 1), name, 0);
    candidateList.push(candidate);
    emit AddCandidateEvent(msg.sender, candidate.id);
  }

  // @nhancv: Get winner
  function getWinner() public view returns (uint8 candidateId) {
    Candidate memory candidate;
    for (uint i = 0; i < candidateList.length; i++) {
      if (candidate.point < candidateList[i].point) {
        candidate = candidateList[i];
      }
    }
    return candidate.id;
  }

  // @nhancv: Vote for candidate
  function voteCandidate(uint8 candidateId) public payable returns (uint8) {
    for (uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i].id == candidateId) {
        candidateList[i].point++;
        emit VoteCandidateEvent(msg.sender, candidateId);
        return candidateList[i].point;
      }
    }
    return 0;
  }

}

A small payable voting demo is not an election system. Voter eligibility, repeat voting, candidate access control, arithmetic width, ties, and event/finality handling need explicit rules beyond this listing.

Source: Gist revision and earlier demo repository. No contract was deployed, payment sent, or vote cast.

← Back to the garden