Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"solidity.defaultCompiler": "localNodeModule"
}
75 changes: 75 additions & 0 deletions contract/contracts/Diamond.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
*
* Implementation of a diamond.
/******************************************************************************/

import {LibDiamond} from "./libraries/LibDiamond.sol";
import {IDiamondCut} from "./interfaces/IDiamondCut.sol";

contract Diamond {
constructor(address _contractOwner, address _diamondCutFacet) payable {
LibDiamond.setContractOwner(_contractOwner);

// Add the diamondCut external function from the diamondCutFacet
IDiamondCut.FacetCut[] memory cut = new IDiamondCut.FacetCut[](1);
bytes4[] memory functionSelectors = new bytes4[](1);
functionSelectors[0] = IDiamondCut.diamondCut.selector;
cut[0] = IDiamondCut.FacetCut({facetAddress: _diamondCutFacet, action: IDiamondCut.FacetCutAction.Add, functionSelectors: functionSelectors});
LibDiamond.diamondCut(cut, address(0), "");
}

/**
DS
*/
struct DiamondStorageMerkleTree {
//
}

function diamondStorage() internal pure returns (DiamondStorage storage ds) {
bytes32 storagePosition = keccak256("diamond.storage.MerkleTree");
assembly {
ds.slot := storagePosition
}
}


// Find facet for function that is called and execute the
// function if a facet is found and return any value.
fallback() external payable {
LibDiamond.DiamondStorage storage ds;
bytes32 position = LibDiamond.DIAMOND_STORAGE_POSITION;
// get diamond storage
assembly {
ds.slot := position
}
// get facet from function selector
address facet = ds.facetAddressAndSelectorPosition[msg.sig].facetAddress;
require(facet != address(0), "Diamond: Function does not exist");
// Execute external function from facet using delegatecall and return any value.
assembly {
// copy function selector and any arguments
// the selectore stores in the stack
calldatacopy(0, 0, calldatasize())

// calldatasize: byte size of the calldata.
let result := delegatecall(gas(), facet, 0, calldatasize(), 0, 0)
// get any return value
returndatacopy(0, 0, returndatasize())
// return any return value or error back to the caller
switch result
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}

receive() external payable {}
}
4 changes: 2 additions & 2 deletions contract/contracts/YoMinter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import '@appliedzkp/semaphore-contracts/interfaces/IVerifier.sol';


/// @title YoNFTMinter
contract YoMinter is IYoMinter, SemaphoreCore, ERC1238, ERC1238URIStorage, Ownable {
contract YoMinter is IYoMinter, SemaphoreCore, ERC1238, ERC1238URIStorage, Ownable, Initializable {

/************************************************
* Library & Variables
Expand All @@ -27,7 +27,7 @@ contract YoMinter is IYoMinter, SemaphoreCore, ERC1238, ERC1238URIStorage, Ownab
* Constructor
***********************************************/

constructor(address verifierAddress, string memory baseURI_) ERC1238(baseURI_) {
constructor(address verifierAddress, string memory baseURI_) initializer ERC1238(baseURI_) {
verifier = IVerifier(verifierAddress);
}

Expand Down
74 changes: 74 additions & 0 deletions contract/contracts/facets/Commitment.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import '../interface/ICommitment.sol';
import {AppStorage} from "../libraries/LibAppStorage.sol";


contract Commitment is ICommitment {

AppStorage internal s;

/************************************************
* Getters
***********************************************/

// commitment is so long
// commitments => coms

/// get user's commitment length
function getComsLength(address _address) public view returns (uint256 length) {
length = s.userCommitments[_address].length;
}

/// get user's all commitments
function getAllComs(address _address) public view returns (Commitment[] memory commitments) {
uint256 length = getComsLength(_address);
for (uint256 i = 0; i < length; i++) {
commitments[i] = s.userCommitments[_address][i];
}
}

/// get target commitment
function getTargetCom(address _address, uint256 _id) private view returns (Commitment memory commitment) {
uint256 length = getComsLength(_address);
Commitment[] memory commtiments = getAllComs(_address);
uint256 targetIndex;
for (uint256 i = 0; i < length; i++) {
if (commtiments[i].id == _id) targetIndex = i;
}
commitment = s.userCommitments[_address][targetIndex];
}

/************************************************
* Add
***********************************************/

/// add commitment
function addCom(
address _address,
uint256 _id,
bytes32 _data,
uint256 _groupId,
string calldata _userId,
uint256 _createdAt
) external {
s.userCommitments[_address].push(Commitment(_id, _userId, _groupId, _data, '', address(0), _createdAt));
}

/************************************************
* Update
***********************************************/

/// update commitment
function updateCom(
string calldata _metadta,
address _mintAddress,
address _address,
uint256 _id
) external view {
Commitment memory commitment = getTargetCom(_address, _id);
commitment.metadata = _metadta;
commitment.mintAddress = _mintAddress;
}
}
21 changes: 21 additions & 0 deletions contract/contracts/facets/DiamondCutFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

import {IDiamondCut} from "../interfaces/IDiamondCut.sol";
import {LibDiamond} from "../libraries/LibDiamond.sol";

contract DiamondCutFacet is IDiamondCut {
function diamondCut(
FacetCut[] calldata _diamondCut,
address _init,
bytes calldata _calldata
) external override {
LibDiamond.enforceIsContractOwner();
LibDiamond.diamondCut(_diamondCut, _init, _calldata);
}
}
154 changes: 154 additions & 0 deletions contract/contracts/facets/DiamondLoupeFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/******************************************************************************\
* Author: Nick Mudge <nick@perfectabstractions.com> (https://twitter.com/mudgen)
* EIP-2535 Diamonds: https://eips.ethereum.org/EIPS/eip-2535
/******************************************************************************/

// The functions in DiamondLoupeFacet MUST be added to a diamond.
// The EIP-2535 Diamond standard requires these functions.

import {LibDiamond} from "../libraries/LibDiamond.sol";
import {IDiamondLoupe} from "../interfaces/IDiamondLoupe.sol";
import {IERC165} from "../utils/IERC165.sol";

contract DiamondLoupeFacet is IDiamondLoupe, IERC165 {
// Diamond Loupe Functions
////////////////////////////////////////////////////////////////////
/// These functions are expected to be called frequently by tools.
//
// struct Facet {
// address facetAddress;
// bytes4[] functionSelectors;
// }
/// @notice Gets all facets and their selectors.
/// @return facets_ Facet
function facets() external view override returns (Facet[] memory facets_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 selectorCount = ds.selectors.length;
// create an array set to the maximum size possible
facets_ = new Facet[](selectorCount);
// create an array for counting the number of selectors for each facet
uint8[] memory numFacetSelectors = new uint8[](selectorCount);
// total number of facets
uint256 numFacets;
// loop through function selectors
for (uint256 selectorIndex; selectorIndex < selectorCount; selectorIndex++) {
bytes4 selector = ds.selectors[selectorIndex];
address facetAddress_ = ds.facetAddressAndSelectorPosition[selector].facetAddress;
bool continueLoop = false;
// find the functionSelectors array for selector and add selector to it
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facets_[facetIndex].facetAddress == facetAddress_) {
facets_[facetIndex].functionSelectors[numFacetSelectors[facetIndex]] = selector;
// probably will never have more than 256 functions from one facet contract
require(numFacetSelectors[facetIndex] < 255);
numFacetSelectors[facetIndex]++;
continueLoop = true;
break;
}
}
// if functionSelectors array exists for selector then continue loop
if (continueLoop) {
continueLoop = false;
continue;
}
// create a new functionSelectors array for selector
facets_[numFacets].facetAddress = facetAddress_;
facets_[numFacets].functionSelectors = new bytes4[](selectorCount);
facets_[numFacets].functionSelectors[0] = selector;
numFacetSelectors[numFacets] = 1;
numFacets++;
}
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
uint256 numSelectors = numFacetSelectors[facetIndex];
bytes4[] memory selectors = facets_[facetIndex].functionSelectors;
// setting the number of selectors
assembly {
mstore(selectors, numSelectors)
}
}
// setting the number of facets
assembly {
mstore(facets_, numFacets)
}
}

/// @notice Gets all the function selectors supported by a specific facet.
/// @param _facet The facet address.
/// @return _facetFunctionSelectors The selectors associated with a facet address.
function facetFunctionSelectors(address _facet) external view override returns (bytes4[] memory _facetFunctionSelectors) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
// all selectors
uint256 selectorCount = ds.selectors.length;
// target selector number
uint256 numSelectors;
_facetFunctionSelectors = new bytes4[](selectorCount);
// loop through function selectors
for (uint256 selectorIndex; selectorIndex < selectorCount; selectorIndex++) {
bytes4 selector = ds.selectors[selectorIndex];
address facetAddress_ = ds.facetAddressAndSelectorPosition[selector].facetAddress;
if (_facet == facetAddress_) {
// 同じでは?
// mstore(_facetFunctionSelectors, numSelectors)
_facetFunctionSelectors[numSelectors] = selector;
numSelectors++;
}
}
// Set the number of selectors in the array
// 最後にnumSelecotorsを返す lengthみたいな感じ
assembly {
mstore(_facetFunctionSelectors, numSelectors)
}
}

/// @notice Get all the facet addresses used by a diamond.
/// @return facetAddresses_
function facetAddresses() external view override returns (address[] memory facetAddresses_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
uint256 selectorCount = ds.selectors.length;
// create an array set to the maximum size possible
facetAddresses_ = new address[](selectorCount);
uint256 numFacets;
// loop through function selectors
for (uint256 selectorIndex; selectorIndex < selectorCount; selectorIndex++) {
bytes4 selector = ds.selectors[selectorIndex];
address facetAddress_ = ds.facetAddressAndSelectorPosition[selector].facetAddress;
bool continueLoop = false;
// see if we have collected the address already and break out of loop if we have
for (uint256 facetIndex; facetIndex < numFacets; facetIndex++) {
if (facetAddress_ == facetAddresses_[facetIndex]) {
continueLoop = true;
break;
}
}
// continue loop if we already have the address
if (continueLoop) {
continueLoop = false;
continue;
}
// include address
facetAddresses_[numFacets] = facetAddress_;
numFacets++;
}
// Set the number of facet addresses in the array
assembly {
mstore(facetAddresses_, numFacets)
}
}

/// @notice Gets the facet address that supports the given selector.
/// @dev If facet is not found return address(0).
/// @param _functionSelector The function selector.
/// @return facetAddress_ The facet address.
function facetAddress(bytes4 _functionSelector) external view override returns (address facetAddress_) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
facetAddress_ = ds.facetAddressAndSelectorPosition[_functionSelector].facetAddress;
}

// This implements ERC-165.
function supportsInterface(bytes4 _interfaceId) external view override returns (bool) {
LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage();
return ds.supportedInterfaces[_interfaceId];
}
}
47 changes: 47 additions & 0 deletions contract/contracts/facets/Group.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import '../interface/IGroup.sol';
import {AppStorage} from "../libraries/LibAppStorage.sol";


contract Group is IGroup {
/************************************************
* Variable and Constraint
***********************************************/

// uint256 public constant s.groups.length = 49;
// Group[size] public groups;
// uint256[size] public groupIds;
// uint256[size] public groupNullfiers;
AppStorage internal s;


/************************************************
* Initialization
***********************************************/

function initialGroups(Group[s.groups.length] calldata _groups) external {
for (uint256 i = 0; i < s.groups.length; i++) {
s.groups[i] = _groups[i];
s.groupIds[i] = _groups[i].id;
s.groupNullfiers[i] = _groups[i].nullfier;
}
}

/************************************************
* Getters
***********************************************/

function getGroups() external view returns (Group[49] memory _groups) {
_groups = groups;
}

/// get "group" from _id
function getGroup(uint256 _id) external view returns (Group memory group) {
uint256 idsIndex;
for (uint256 i = 0; i < s.groups.length; i++) {
if (_id == groupIds[i]) idsIndex = i;
}
group = s.groups[idsIndex];
}
}
Loading