Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #43

Merged
merged 46 commits into from
Apr 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
f43bacb
Ag 17 commitlint fix (#1)
g3k0 Mar 24, 2024
9260287
feat(@agora): AG-35 basic workflow (#3)
g3k0 Mar 25, 2024
3424f31
docs(@agora): AG-35 updloaded image (#5)
g3k0 Mar 26, 2024
56f40f9
Ag 18 eoa implementation (#7)
g3k0 Mar 28, 2024
db20673
Ag 19 DECsRegistry smart contract (#8)
g3k0 Mar 29, 2024
0dc0d57
Ag 20 election smart contract (#9)
g3k0 Mar 30, 2024
fb1dc90
Ag 20 election smart contract (#10)
g3k0 Mar 30, 2024
07a2bad
Merge branch 'main' into develop
g3k0 Mar 30, 2024
ed96d8d
Ag 25 deploy contracts (#13)
g3k0 Mar 31, 2024
202b9fa
Ag 26 register election script (#14)
g3k0 Apr 7, 2024
b7d36ec
Merge branch 'main' into develop
g3k0 Apr 7, 2024
c3eb2ba
Merge branch 'main' into develop
g3k0 Apr 7, 2024
2639ecd
Ag 37 actions api layer (#16)
g3k0 Apr 7, 2024
c9596a4
docs(@docs): updated README
g3k0 Apr 7, 2024
81f457a
Merge branch 'main' into develop
g3k0 Apr 7, 2024
3e5dd41
Merge branch 'main' into develop
g3k0 Apr 7, 2024
03e502d
fix(@ci): AG-36 fixed github username (#19)
g3k0 Apr 7, 2024
93631f7
Ag 39 encrypt dec data (#21)
g3k0 Apr 9, 2024
14581fc
Ag 40 dec registration (#22)
g3k0 Apr 11, 2024
7aeb927
Merge branch 'main' into develop
g3k0 Apr 11, 2024
82bb5c4
fix(@CICD): AG-42 fix github actions (#24)
g3k0 Apr 11, 2024
9241c8e
fix(CICD): AG-42 workflow fixes (#26)
g3k0 Apr 11, 2024
37ecada
Merge branch 'main' into develop
g3k0 Apr 11, 2024
86f3780
Merge branch 'main' into develop
g3k0 Apr 11, 2024
3dad309
Ag 42 fixes 2 (#28)
g3k0 Apr 11, 2024
8dec442
Merge branch 'main' into develop
g3k0 Apr 11, 2024
e79d573
Merge branch 'develop' of https://github.com/nova-collective/agora in…
g3k0 Apr 11, 2024
a3ea86f
Merge branch 'main' into develop
g3k0 Apr 11, 2024
3f87a38
AG-42 fixes (#30)
g3k0 Apr 11, 2024
e7463b6
Merge branch 'main' into develop
g3k0 Apr 11, 2024
3d205f3
fixes (#32)
g3k0 Apr 11, 2024
112da11
Merge branch 'main' into develop
g3k0 Apr 11, 2024
5314b97
Merge branch 'main' into develop
g3k0 Apr 11, 2024
af2ab5e
Merge branch 'main' into develop
g3k0 Apr 11, 2024
33a7097
Merge branch 'main' into develop
g3k0 Apr 12, 2024
cf01269
Merge branch 'main' into develop
g3k0 Apr 12, 2024
6575a17
Ag 32 zokrates init (#36)
g3k0 Apr 14, 2024
d1d6fdf
Merge branch 'main' into develop
g3k0 Apr 14, 2024
970496f
fix(CI/CD): AG-45 added set -e parameter to workflow (#38)
g3k0 Apr 17, 2024
c166750
feat(zk-snarks): AG-32 defined voting program (#39)
g3k0 Apr 18, 2024
91fc545
updated version
g3k0 Apr 18, 2024
6a9023f
Merge branch 'main' into develop
g3k0 Apr 20, 2024
a08b208
Merge branch 'main' into develop
g3k0 Apr 20, 2024
b2a7637
Merge branch 'main' into develop
g3k0 Apr 20, 2024
aec0591
Ag 50 crypto lib integration (#42)
g3k0 Apr 21, 2024
748162e
updated version number
g3k0 Apr 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
g3k0 authored Mar 29, 2024
commit db20673eb74bc1dcd224754fb18b6f0f89b28ac8
62 changes: 62 additions & 0 deletions contracts/DEC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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);
}
}
61 changes: 61 additions & 0 deletions contracts/DECsRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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;
}
}
34 changes: 0 additions & 34 deletions contracts/Lock.sol

This file was deleted.

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 modified docs/assets/electoral_card_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/registration_class.png
Binary file not shown.
Binary file removed docs/assets/voting_class.png
Binary file not shown.
7 changes: 0 additions & 7 deletions script/create-voter-eoa.test.ts
Original file line number Diff line number Diff line change
@@ -3,13 +3,6 @@ import { ethers } from "hardhat";
import { main } from "./create-voter-eoa";

describe("Main function", () => {
/*beforeEach(() => {
ethers.Wallet.createRandom = jest.fn().mockReturnValueOnce({
address: "mockedAddress",
privateKey: "mockedPrivateKey",
});
});*/

afterEach(() => {
jest.clearAllMocks();
});
35 changes: 35 additions & 0 deletions test/DEC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ethers } from "hardhat";
import { expect } from "chai";

describe("DEC Contract", function () {
let DEC: any;
let decContract: any;
let ownerAddress: any;

before(async function () {
DEC = await ethers.getContractFactory("DEC");
[ownerAddress] = await ethers.getSigners();
});

beforeEach(async function () {
decContract = await DEC.deploy();
});

it("should deploy the contract and set the owner", async function () {
expect(await decContract.owner()).to.equal(ownerAddress.address);
});

it("should encrypt DEC data correctly", async function () {
const decData = {
taxCode: "123456789",
municipality: "Sample Municipality",
province: "Sample Province",
region: "Sample Region",
country: "Sample Country",
};

const encryptedData = await decContract.encryptDEC(decData);

expect(encryptedData.data).to.not.be.null;
});
});
84 changes: 84 additions & 0 deletions test/DECsRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { ethers } from "hardhat";
import { expect } from "chai";
import { Signer } from "ethers";
import { DECsRegistry } from "../typechain-types/DECsRegistry";
import { DecData } from "./types";

describe("DECs Registry Contract", function () {
let contract: DECsRegistry;
let owner: Signer;
let voter: Signer;

const electionAddress = "0xae92d5aD7583AD66E49A0c67BAd18F6ba52dDDc1";
const decData: DecData = {
taxCode: "1234567890",
municipality: "mockMunicipality",
province: "mockProvince",
region: "mockRegion",
country: "mockCountry",
};

beforeEach(async () => {
const ContractFactory = await ethers.getContractFactory("DECsRegistry");
[owner, voter] = await ethers.getSigners();
contract = await ContractFactory.deploy();
});

it("Should deploy the contract", async function () {
expect(contract.address).to.not.equal(0);
});

it("Should register DEC", async function () {
const response = await contract
.connect(owner)
.registerDEC(decData, await voter.getAddress());

expect(response.blockHash).to.not.equal(null);
expect(response.blockHash).to.not.equal(undefined);
expect(response.data.length).to.be.greaterThan(0);
});

it("Should not register DEC if already registered", async function () {
await contract
.connect(owner)
.registerDEC(decData, await voter.getAddress());
await expect(
contract.connect(owner).registerDEC(decData, await voter.getAddress()),
).to.be.revertedWith("The Voter's DEC has been already registered");
});

it("Should get DEC", async function () {
await contract
.connect(owner)
.registerDEC(decData, await voter.getAddress());
const retrievedDEC = await contract.getDEC(await voter.getAddress());

expect(retrievedDEC.length).to.be.greaterThan(0);
});

it("Should revert if DEC not registered", async function () {
await expect(contract.getDEC(await voter.getAddress())).to.be.revertedWith(
"The Voter don't have a registered DEC",
);
});

it("Should return true if voter already voted", async function () {
await contract
.connect(owner)
.stampsTheDEC(electionAddress, await voter.getAddress());

const hasVoted = await contract.hasVoterAlreadyVoted(
await voter.getAddress(),
electionAddress,
);
expect(hasVoted).to.be.true;
});

it("Should return false if voter hasn't voted", async function () {
const hasVoted = await contract.hasVoterAlreadyVoted(
await voter.getAddress(),
electionAddress,
);
expect(hasVoted).to.be.false;
});
});
Loading
Loading