Skip to content

Commit

Permalink
Merge commit '72fc4584597ab936b2ea45d2b7f2baf5b26eb393'
Browse files Browse the repository at this point in the history
  • Loading branch information
lucagiorgino committed Feb 13, 2024
2 parents 6a16dd7 + 72fc458 commit 06f920c
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 50 deletions.
32 changes: 9 additions & 23 deletions addresses/contractAddresses.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
{
"addresses": [
{
"Deployer": "0x7a2088a1bFc9d81c55368AE168C2C02570cB814F"
},
{
"ERC721Base": "0x09635F643e140090A9A8Dcd712eD6285858ceBef"
},
{
"ERC20Base": "0xc5a5C42992dECbae36851359345FE25997F5C42d"
},
{
"RouterFactory": "0x67d269191c92Caf3cD7723F116c85e6E9bf55933"
},
{
"FixedRateExchange": "0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E"
},
{
"IDentity": "0xc3e53F4d16Ae77Db1c982e75a937B9f60FE63690"
},
{
"ERC721Factory": "0x84eA74d481Ee0A5332c457a4d796187F6Ba67fEB"
}
]
"addresses": {
"Deployer": "0xd17dD3803786E6c7E5B466bd2c22eD6718bA36D7",
"ERC721Base": "0x5cF677D2D7844B7CE367093e2EAE01F36AdC7953",
"ERC20Base": "0xf26d2BCF934AF6Ac388E0F3c4888Bd26B76857Fc",
"RouterFactory": "0x371D33e3e61AD98dA65361BbBCB81d26af03bF4C",
"FixedRateExchange": "0xf74A8E86D90E90eafb468d6bF5c142Ac17E1bfcc",
"IDentityAddress": "0xa8f364E1829eBf480e738057953b38f79fe2E17A",
"ERC721Factory": "0x90A98F282Db0d6df64e83AF48011C46D83B8F8cE"
}
}
20 changes: 16 additions & 4 deletions contracts/ERC721Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";
import "../interfaces/IERC721Base.sol";
import "../interfaces/IERC20Base.sol";
import "../interfaces/IERC721Factory.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract ERC721Base is
Initializable,
Expand Down Expand Up @@ -155,22 +156,33 @@ contract ERC721Base is

function verifyPoP(
bytes calldata _eth_signature,
bytes32 _hash
bytes calldata _challenge // bytes32 _hash
) external view returns(bool) {
address extractedAddress = extractSourceFromSignature(_hash, _eth_signature);
address extractedAddress = extractSourceFromSignature(_challenge, _eth_signature);
require(extractedAddress != address(0), "ERC721: extracted signature is 0x00");

IERC20Base ierc20Instance = IERC20Base(deployedERC20Tokens[0]);
// >= 1 ether cause the price is fixed to 1 for now. This needs to be changed if prices will be added.
return (ierc20Instance.balanceOf(extractedAddress) >= 1 ether);
}

function extractSourceFromSignature(bytes32 _hash, bytes calldata _pseudo_signature) internal pure returns(address) {
bytes32 signedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash));
// TODO: define this in a library Smart Contract (almost common to IDentity.sol)
// function extractSourceFromSignature(bytes32 _hash, bytes calldata _pseudo_signature) internal pure returns(address) {
// bytes32 signedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _hash));
// (bytes32 r, bytes32 s, uint8 v) = splitSignature(_pseudo_signature);
// return ecrecover(signedHash, v, r, s);
// }
function extractSourceFromSignature(bytes calldata _challenge, bytes calldata _pseudo_signature) internal pure returns(address) {
bytes32 signedHash = keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n",
Strings.toString(_challenge.length),
_challenge)
);
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_pseudo_signature);
return ecrecover(signedHash, v, r, s);
}

// TODO: define this in a library Smart Contract (almost common to IDentity.sol)
// https://solidity-by-example.org/signature/
function splitSignature(
bytes memory sig
Expand Down
6 changes: 4 additions & 2 deletions contracts/ERC721Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ contract ERC721Factory is Ownable, Deployer, IERC721Factory {
string dt_name;
string dt_symbol;
uint256 maxSupply_; // must be > 10 otherwise mint will fail
uint256 vc_id;
// uint256 vc_id; // TODO: remove this field, useless, we can get the VC id from the msg.sender and interacting with the identity contract
}

struct ContractBase {
Expand Down Expand Up @@ -89,7 +89,9 @@ contract ERC721Factory is Ownable, Deployer, IERC721Factory {
* 3. Not revoked
*/
IIDentity identity_token = IIDentity(_identity_addr);
require(!identity_token.isVCRevoked(_publishData.vc_id), "The user does not have a valid VC!");
require(!identity_token.isVCRevoked_Addr(msg.sender), "The user does not have a valid VC!");
// require(!identity_token.isVCRevoked(_publishData.vc_id), "The user does not have a valid VC!");
// TODO: ? check if it is active and not expired
/**
* deploy NFT token
*/
Expand Down
1 change: 1 addition & 0 deletions contracts/FixedRateExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ contract FixedRateExchange {
* @param dtamount amount of DTs that are requested to be sold
*/
function sellDT(bytes32 exchangeId, uint256 dtamount) payable external activeExchange(exchangeId) {
//TODO: add interactions with the Identity contract
require(dtamount > 0, "FIXEDRATE: PROVIDED A 0 DT AMOUNT");

uint256 swapPrice = calcDT_to_SMR(exchangeId, dtamount);
Expand Down
27 changes: 17 additions & 10 deletions contracts/IDentity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.18;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract IDentity is Ownable {

Expand Down Expand Up @@ -31,13 +32,13 @@ contract IDentity is Ownable {
return _free_vc_id;
}

function validate_and_store_VC(
function add_user (
uint256 _vc_id,
bytes calldata _pseudo_signature,
string calldata _did,
uint256 _expiration_date,
uint256 _issuance_date,
bytes32 _vc_hash
bytes calldata _challenge
) external onlyOwner {
require(_vc_id >= 0, "VC identitifier must be greater than 0");
require(_vc_id <= _free_vc_id, "Received VC id is invalid");
Expand All @@ -46,7 +47,7 @@ contract IDentity is Ownable {
require(block.timestamp < _expiration_date, "Got invalid/expired expiration date");
require(_issuance_date <= block.timestamp, "Issuance date is in the future");

address extractedAddress = extractSourceFromSignature(_vc_hash, _pseudo_signature);
address extractedAddress = extractSourceFromSignature(_challenge, _pseudo_signature);
require(extractedAddress != address(0), "Invalid Extracted address");
uint256 id = _addr_to_vcId[extractedAddress];
if(id != 0 && !_vcId_to_VC[id].revoked) { // holder already has a vc
Expand All @@ -65,13 +66,13 @@ contract IDentity is Ownable {
emit NewVCRequestRegistered(_vc_id, _vcId_to_VC[_vc_id].vc_owner, _vcId_to_VC[_vc_id].expiration_date, block.timestamp);
}

function activateVC(uint256 _vc_id) external {
function activateVC(uint256 _vc_id) external {
require(msg.sender != address(0), "Sender is invalid");
require(_vc_id >= 0, "VC identitifier must be greater than 0");
VerifiableCredential storage vc = _vcId_to_VC[_vc_id];
require(vc.status == false, "VC already activated");
require(block.timestamp < vc.expiration_date, "Cannot activate VC: VC has expired");
require(msg.sender == address(vc.vc_owner), "Cannot activate VC: sender is not who expcted");
require(msg.sender == address(vc.vc_owner), "Cannot activate VC: sender is not who expected"); // TODO: why don't user msg.sender to recover the VC.id?

// activate VC
vc.status = true;
Expand All @@ -88,12 +89,18 @@ contract IDentity is Ownable {
emit VC_Revoked(_vc_id);
}

function extractSourceFromSignature(bytes32 _vc_hash, bytes calldata _pseudo_signature) internal pure returns(address) {
bytes32 signedHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _vc_hash));
// TODO: define this in a library Smart Contract (almost common to ERC721Base.sol)
function extractSourceFromSignature(bytes calldata _challenge, bytes calldata _pseudo_signature) internal pure returns(address) {
bytes32 signedHash = keccak256(
abi.encodePacked("\x19Ethereum Signed Message:\n",
Strings.toString(_challenge.length),
_challenge)
);
(bytes32 r, bytes32 s, uint8 v) = splitSignature(_pseudo_signature);
return ecrecover(signedHash, v, r, s);
}

// TODO: define this in a library Smart Contract (almost common to ERC721Base.sol)
// https://solidity-by-example.org/signature/
function splitSignature(
bytes memory sig
Expand Down Expand Up @@ -124,7 +131,7 @@ contract IDentity is Ownable {
function isVCActive(uint256 _vc_id) external view returns(bool) {
return _vc_active(_vc_id);
}
function isVCActive_Addr(address vc_holder) public view returns(bool) {
function isVCActive_Addr(address vc_holder) external view returns(bool) {
return _vc_active(_addr_to_vcId[vc_holder]);
}

Expand All @@ -136,7 +143,7 @@ contract IDentity is Ownable {
function isVCExpired(uint256 _vc_id) external view returns(bool) {
return _vc_expired(_vc_id);
}
function isVCExpired_Addr(address vc_holder) public view returns(bool) {
function isVCExpired_Addr(address vc_holder) external view returns(bool) {
return _vc_expired(_addr_to_vcId[vc_holder]);
}

Expand All @@ -149,7 +156,7 @@ contract IDentity is Ownable {
function isVCRevoked(uint256 _vc_id) external view returns(bool) {
return _vc_revoked(_vc_id);
}
function isVCRevoked_Addr(address vc_holder) public view returns(bool) {
function isVCRevoked_Addr(address vc_holder) external view returns(bool) {
return _vc_revoked(_addr_to_vcId[vc_holder]);
}

Expand Down
9 changes: 9 additions & 0 deletions init.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
npx hardhat --network localhost faucet 0xF9692336D7f37336C2061a545D8b2895B1415EFe & ^
npx hardhat --network localhost faucet 0xACC7CE50Be9D1db2eF86f804F5bC03b53681Fcbe & ^
npx hardhat --network localhost faucet 0xBB3fb731CC4b103C072594E226A396F84C3E5DF4 & ^
npx hardhat --network localhost faucet 0x25B2E1942578bCBB1Aa6E8EAa038DfE73440fcbe & ^
npx hardhat --network localhost faucet 0xAba0719cc3E1187A4C9Caf6773D9766541C2Dd8D & ^
npx hardhat --network localhost faucet 0xd0300C2817624BDd6C34F1d8e44857046dE13110 & ^
npx hardhat --network localhost faucet 0xD12270A9054d045ba276C16Cb1C64386cDAA6AC7 & ^
npx hardhat --network localhost faucet 0x6d3fbD514D62E2D305e4435dDe99483f20f264BF & ^
npx hardhat run --network hardhat-issuer scripts/deploy.js
8 changes: 7 additions & 1 deletion interfaces/IIDentity.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity ^0.8.18;
interface IIDentity {
function getFreeVCid() external view returns(uint256);

function validate_and_store_VC(
function add_user(
uint256 _vc_id,
bytes calldata _pseudo_signature,
string calldata _did,
Expand All @@ -14,7 +14,13 @@ interface IIDentity {
) external;

function activateVC(uint256 _vc_id) external;

function isVCActive(uint256 _vc_id) external view returns(bool);
function isVCExpired(uint256 _vc_id) external view returns(bool);
function isVCRevoked(uint256 _vc_id) external view returns(bool);

function isVCActive_Addr(address vc_holder) external view returns(bool);
function isVCExpired_Addr(address vc_holder) external view returns(bool);
function isVCRevoked_Addr(address vc_holder) external view returns(bool);

}
26 changes: 16 additions & 10 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,61 @@ const { ethers } = require("hardhat");
const fs = require("fs");

async function main() {
var obtainedAddresses = {
addresses: []
};

let obtainedAddresses = {}
const name = "addresses"
obtainedAddresses[name] = {};

addresses = obtainedAddresses[name];

const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
console.log("Account balance:", (await deployer.getBalance()).toString());

const Deployer = await ethers.getContractFactory("Deployer");
let token = await Deployer.deploy();
obtainedAddresses.addresses.push({Deployer: token.address});
addresses.Deployer = token.address;
console.log("Deployer address:", token.address);

const ERC721Base = await ethers.getContractFactory("ERC721Base");
const baseAddress = await ERC721Base.deploy();
obtainedAddresses.addresses.push({ERC721Base: baseAddress.address});
addresses.ERC721Base = baseAddress.address;
console.log("ERC721Base address:", baseAddress.address);

const ERC20Base = await ethers.getContractFactory("ERC20Base");
const base20Address = await ERC20Base.deploy();
obtainedAddresses.addresses.push({ERC20Base: base20Address.address});
addresses.ERC20Base=base20Address.address;
console.log("ERC20Base address:", base20Address.address);

const RouterFactory = await ethers.getContractFactory("RouterFactory");
const router = await RouterFactory.deploy(deployer.address);
obtainedAddresses.addresses.push({RouterFactory: router.address});
addresses.RouterFactory = router.address;
console.log("RouterFactory address:", router.address);

const FixedRateExchange = await ethers.getContractFactory("FixedRateExchange");
const a = await FixedRateExchange.deploy(router.address);
obtainedAddresses.addresses.push({FixedRateExchange: a.address});
addresses.FixedRateExchange = a.address;
console.log("FixedRateExchange address:", a.address);

const IDentityFactory = await ethers.getContractFactory("IDentity");
const IDtoken = await IDentityFactory.deploy();
const IDentityAddress = await IDtoken.address
obtainedAddresses.addresses.push({IDentity: IDentityAddress});
addresses.IDentityAddress = IDentityAddress;
console.log("IDentitySC address:", IDentityAddress);

const ERC721Factory = await ethers.getContractFactory("ERC721Factory");
token = await ERC721Factory.deploy(baseAddress.address, base20Address.address, router.address, a.address, IDentityAddress);
obtainedAddresses.addresses.push({ERC721Factory: token.address});
addresses.ERC721Factory = token.address;
console.log("ERC721Factory address:", token.address);

// add factory and exchange address to router
const box = RouterFactory.attach(router.address);
await box.addFactoryAddress(token.address);
await box.addFixedRateAddress(a.address);


obtainedAddresses[name] = addresses;

const json = JSON.stringify(obtainedAddresses, null, 2);
await fs.promises.writeFile(__dirname.replace('scripts','addresses/contractAddresses.json'), json)
.catch((err) => {
Expand Down
15 changes: 15 additions & 0 deletions scripts/usage_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require("dotenv").config();
const ethers = require("ethers");
const fs =require("fs") ;


const addresses = JSON.parse(fs.readFileSync(process.env.ADDRESS_FILE)).addresses;

console.log(`Deployer address: ${addresses.Deployer}`)
console.log(`ERC721Base address: ${addresses.ERC721Base}`)
console.log(`ERC721Factory address: ${addresses.ERC721Factory}`)
console.log(`ERC20Base address: ${addresses.ERC20Base}`)
console.log(`FixedRateExchange address: ${addresses.FixedRateExchange}`)
console.log(`IDentityAddress address: ${addresses.IDentityAddress}`)


0 comments on commit 06f920c

Please sign in to comment.