Skip to content

Commit

Permalink
Develop (#11)
Browse files Browse the repository at this point in the history
* Ag 17 commitlint fix (#1)

* fix(@script): AG-17 changed script logic

changed script login the commit script

* fix(@script): AG-17 changed script

modified some logic in the script folder

* docs(@agora): modified readme

modified readme

* feat(@agora): AG-35 basic workflow (#3)

* feat(@agora): AG-35 basic workflow

basic GitHub Actions workflow implementation

* feat(@agora): AG-35 duplicated code

inserted duplicated code check in ci-cd

* docs(@agora): AG-35 updloaded image (#5)

uploaded image for functional analysis

* Ag 18 eoa implementation (#7)

* feat(@script): AG-18 EOA script implementation

implemented script in order to generate an EOA over different ethereum networks configured

* test(@script): AG-18 unit test configuration

unit test configuration for scripts using jest

* test(@agora): AG-18 configured test step in workflow

configured workflow to run script unit test

* test(@script): AG-18 configured commit script

configured pre-commit hook script to run unit test

* test(@script): AG-18 unit test implementation

implemented unit test for eoa script

* test(@script): AG-18 code coverage

unit test code coverage report implementation

* refactor(@script): AG-18 precommit hook

added check in pre-commit hook

* fix(@script): AG-18 fixed unit test threshold

fixed unit test code coverage threshold

* fix(@agora): AG-18 fixed package.json

fixed package json property not used

* fix(@agora): AG-18 fixed unit test

n

* fix(@agora): AG-18 fixed workflow

fixed workflow

* refactor(@script): AG-18 test code coverage threshold

test code coverage threshold

* docs(@agora): AG-18 updated readme

updated readme file

* Ag 19 DECsRegistry smart contract (#8)

* fix(@script): AG-19 removed comments

removed code comments

* docs(@docs): AG-19 updated sequence diagrams

updated the sequence diagrams images for the functional analysis

* docs(@docs): AG-19 updated smart contract classes

updated docs about the smart contracts classes

* feat(@contracts): AG-19 DEC contract implementation

implemented a first version of the DEC contract

* feat(@contracts): AG-19 added events to DEC sc

emitted event in the DEC smart contract implementation

* feat(@contracts): AG-19 implemented DEC contract

implemented DEC contract and related unit tests

* fix(@contracts): AG-19 removed event

removed event not used in DEC contract

* feat(@contracts): AG-19 DECs registry implementation

implemented the register of the DECs with the required methods

* test(@contracts): AG-19 defined test for DECsRegistry contract

defined test structure for the DECsRegistry smart contract

* test(@contracts): AG-19 implemented unit test

implemented unit tests for DECsRegistry smart contract

* feat(@contracts): AG-19 added events to smart contract

added events to the DECsRegistry smart contract

* Ag 20 election smart contract (#9)

* refactor(@agora): AG-20 switched test network

switched from legacy goerli test network to sepolia

* docs(@docs): AG-20 updated analysis diagrams

updated diagrams in the functional analysis

* feat(@contracts): AG-20 Election smart contract

implemented Election smart contract with unit tests

* feat(@contracts): AG-20 Elections smart contracts

implemeneted different smart contracts for different kind of elections

* docs(@docs): AG-20 addded diagrams files to docs

added diagrams file to the docs folder

* docs(@docs): AG-20 updated class diagrams

updated the smart contracts class diagrams

* Ag 20 election smart contract (#10)

* refactor(@agora): AG-20 switched test network

switched from legacy goerli test network to sepolia

* docs(@docs): AG-20 updated analysis diagrams

updated diagrams in the functional analysis

* feat(@contracts): AG-20 Election smart contract

implemented Election smart contract with unit tests

* feat(@contracts): AG-20 Elections smart contracts

implemeneted different smart contracts for different kind of elections

* docs(@docs): AG-20 addded diagrams files to docs

added diagrams file to the docs folder

* docs(@docs): AG-20 updated class diagrams

updated the smart contracts class diagrams

* fix(@agora): AG-20 updated version number

updated version number in package json
  • Loading branch information
g3k0 authored Mar 30, 2024
1 parent f6be396 commit f34a355
Show file tree
Hide file tree
Showing 37 changed files with 10,224 additions and 4,624 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
codeQuality:

runs-on: ubuntu-latest
env:
ALCHEMY_PRIVATE_KEY: ${{ secrets.ALCHEMY_PRIVATE_KEY }}
SEPOLIA_URL: ${{ secrets.SEPOLIA_URL }}

steps:
- name: Checkout repository
Expand All @@ -33,5 +36,8 @@ jobs:
- name: Run duplications check
run: npm run duplicated

- name: Run unit tests
- name: Run smart contracts unit tests
run: npm run test-contracts

- name: Run scripts unit tests
run: npm run test-scripts
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ To setup the application follow these steps:
2. from the root folder of the application run: `npm i`
3. run `npm run prepare`
4. Write a `.env` file in the root of the project and configure:
* `GOERLI_URL` You can find the data in your Alchemy account
* `PRIVATE_KEY` You can find the data in your Alchemy account
* `ALCHEMY_API_KEY` You can find the data in your Alchemy account
* `REPORT_GAS` enable or disable the gas report on smart contracts unit tests executions
* `SEPOLIA_URL` You can find the data in your Alchemy account, after you create an app there;
* `ALCHEMY_API_KEY` You can find the data in your Alchemy account;
* `REPORT_GAS` enable or disable the gas report on smart contracts unit tests executions;

## How to commit

Expand All @@ -46,9 +45,12 @@ Remember to follow this convention for commit messages: `AG-<jira id> <descripti

1. Run the unit tests for smart contracts: `npm run test-contracts`
2. Run the unit test code coverage for smart contracts: `npm run coverage-contracts`
3. Run the unit test for scripts: `npm run test-scripts`

Smart contracts code coverage documentation [here](https://www.npmjs.com/package/solidity-coverage).

CI/CD workflow fails if the unit test code coverage threshold (**80% of lines of code**) for scripts is not met.

# Donations
Support this project and offer me a crypto-coffee!!

Expand Down
15 changes: 15 additions & 0 deletions contracts/CountryElection.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.24;

import "./Election.sol";

/// @title The Country Election smart contract
/// @author Christian Palazzo <palazzochristian@yahoo.it>
/// @custom:experimental This is an experimental contract.
contract CountryElection is Election {

constructor(uint256 _electionStart, uint256 _electionEnd) Election(_electionStart, _electionEnd) {

}

}
66 changes: 66 additions & 0 deletions contracts/DEC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.24;

/// @title The Voter's Digital Electoral Cards
/// @author Christian Palazzo <palazzochristian@yahoo.it>
/// @custom:experimental This is an experimental contract.
contract DEC {
address public owner;

constructor() {
/// @dev only the owner of the contract has write permissions
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}

/// @notice This is the Digital Electoral Card, emitted by a public third-party authority and owned by the Voter
/// @dev This data is encrypted with the Voter's public address and only the Voter can decrypt it using the private key
struct decData {
string taxCode;
string municipality;
string province;
string region;
string country;
}

event DECEncrypted(address indexed owner, bytes encryptedData);

/// @notice This function is used to encrypt ad digitally sign a DEC
function encryptDEC(
decData memory dec
) public onlyOwner returns (bytes memory) {
bytes memory encodedData = abi.encodePacked(
dec.taxCode,
dec.municipality,
dec.province,
dec.region,
dec.country
);
bytes32 hashedData = keccak256(encodedData);
bytes memory signature = signData(hashedData);

emit DECEncrypted(msg.sender, abi.encodePacked(hashedData, signature));

return abi.encodePacked(hashedData, signature);
}

/// @notice This function is used to digitally sign the data
function signData(bytes32 data) private pure returns (bytes memory) {
bytes32 hash = keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n32", data)
);
bytes1 v = bytes1(0);
bytes32 r = bytes32(0);
bytes32 s = uintToBytes32(1);
return abi.encodePacked(ecrecover(hash, uint8(v), r, s), r, s);
}

/// @notice this function is used in signData function
function uintToBytes32(uint256 x) private pure returns (bytes32) {
return bytes32(x);
}
}
64 changes: 64 additions & 0 deletions contracts/DECsRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.24;

import "./DEC.sol";

/// @title The Registry of the Digital Electoral Cards
/// @author Christian Palazzo <palazzochristian@yahoo.it>
/// @custom:experimental This is an experimental contract.
contract DECsRegistry is DEC {
constructor() DEC() {}

/// @notice this is the list of stamps of elections in which the voter participated
/// @dev the first address is related to the Voter's EOA, the second array is the Voter's stamps list
mapping(address => address[]) electoralStamps;

/// @notice this function contains the list of DECs
/// @dev the address is related to the Voter's EOA
mapping(address => bytes) registry;

event DECRegistered(address indexed voter, bytes dec);
event DECStamped(address indexed election, address indexed voter);

/// @notice this function is used by the third party authority to register a Voter's DEC in the registry
/// @dev the DEC contains sensitive data that must be encrypted
function registerDEC(decData memory dec, address voter) public onlyOwner {
require(
registry[voter].length == 0,
"The Voter's DEC has been already registered"
);
registry[voter] = encryptDEC(dec);
emit DECRegistered(voter, registry[voter]);
return;
}

/// @notice this function returns an encrypted DEC in order to check if a Voter has the voting rights
function getDEC(address voter) public view returns (bytes memory) {
require(
registry[voter].length != 0,
"The Voter don't have a registered DEC"
);
return registry[voter];
}

/// @notice this function checks in the registry if the Voter already voted in a certail election
function hasVoterAlreadyVoted(
address voter,
address election
) public view returns (bool) {
for (uint i = 0; i < electoralStamps[voter].length; i++) {
if (electoralStamps[voter][i] == election) {
return true;
}
}
return false;
}

/// @notice this function put the election stamp on the Voter's DEC after the vote
/// @dev the owner of the DECs registry is the same of the election smart contract (third party authority)
function stampsTheDEC(address election, address voter) public onlyOwner {
electoralStamps[voter].push(election);
emit DECStamped(election, voter);
return;
}
}
67 changes: 67 additions & 0 deletions contracts/Election.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.24;

/// @title The Election smart contract
/// @author Christian Palazzo <palazzochristian@yahoo.it>
/// @notice the Election contract is abstract. Different kind of elections contracts inherits form this one.
/// @custom:experimental This is an experimental contract.
contract Election {
address public owner;
uint256 private electionStart;
uint256 private electionEnd;
mapping (uint256 => string) private ballotBox; // change the data types later
mapping (uint256 => string) results; // change the data types later

constructor(uint256 _electionStart, uint256 _electionEnd) {
owner = msg.sender;
electionStart = _electionStart;
electionEnd = _electionEnd;
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}

/// @notice this function checks if the transaction occours when the elections are open
function isIvokedInElectionPeriod() private view returns (bool) {
return (block.timestamp >= electionStart && block.timestamp <= electionEnd);
}

/// @notice this function checks if the transaction occours after the election is closed
function isElectionClosed() private view returns (bool) {
return block.timestamp > electionEnd;
}

function setElectionStart(uint256 _electionStart) public onlyOwner {
require(!isIvokedInElectionPeriod(), "Elections have already started, it's too late for changing the start of the elections");
require(!isElectionClosed(), "Elections are closed, it's not possible to change the start of the elections");
electionStart = _electionStart;
}

function getElectionStart() public view returns (uint256) {
return electionStart;
}

function setElectionEnd(uint256 _electionEnd) public onlyOwner {
require(!isIvokedInElectionPeriod(), "Elections have already started, it's too late for changing the end of the elections");
require(!isElectionClosed(), "Elections are closed, it's not possible to change the end of the elections");
electionEnd = _electionEnd;
}

function getElectionEnd() public view returns (uint256) {
return electionEnd;
}

/// @notice this function collects the ballots
function vote() public view {
require(isIvokedInElectionPeriod(), "Elections are not open");
require(!isElectionClosed(), "Elections are closed");
}

/// @notice this function calculates the elections results
function scrutiny() public view onlyOwner {
require(!isIvokedInElectionPeriod(), "Elections are in progress, it's not possible to calculate the results");
require(isElectionClosed(), "Scrutiny is possible only after the elections end");
}
}
34 changes: 0 additions & 34 deletions contracts/Lock.sol

This file was deleted.

15 changes: 15 additions & 0 deletions contracts/MunicipalityElection.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.24;

import "./Election.sol";

/// @title The Municipality Election smart contract
/// @author Christian Palazzo <palazzochristian@yahoo.it>
/// @custom:experimental This is an experimental contract.
contract MunicipalityElection is Election {

constructor(uint256 _electionStart, uint256 _electionEnd) Election(_electionStart, _electionEnd) {

}

}
15 changes: 15 additions & 0 deletions contracts/RegionalElection.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.24;

import "./Election.sol";

/// @title The Regional Election smart contract
/// @author Christian Palazzo <palazzochristian@yahoo.it>
/// @custom:experimental This is an experimental contract.
contract RegionalElection is Election {

constructor(uint256 _electionStart, uint256 _electionEnd) Election(_electionStart, _electionEnd) {

}

}
Binary file modified docs/assets/agora-main-flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/agora_main_sequence_diagrams-Phase 0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/agora_main_sequence_diagrams-Phase 2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/agora_main_sequence_diagrams-Phase 3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/agora_main_sequence_diagrams-Phase 4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/decs_registry_class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/election_class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/assets/electoral_card_class.png
Binary file not shown.
Binary file removed docs/assets/registration_class.png
Binary file not shown.
Binary file removed docs/assets/voting_class.png
Binary file not shown.
Loading

0 comments on commit f34a355

Please sign in to comment.