diff --git a/contracts/WakuRln.sol b/contracts/WakuRln.sol index 8ea1b92..dc081ee 100644 --- a/contracts/WakuRln.sol +++ b/contracts/WakuRln.sol @@ -5,16 +5,25 @@ import {IPoseidonHasher} from "rln-contract/PoseidonHasher.sol"; import {RlnBase, DuplicateIdCommitment, FullTree, InvalidIdCommitment} from "rln-contract/RlnBase.sol"; import {Ownable} from "openzeppelin-contracts/contracts/access/Ownable.sol"; +import {LazyIMT, LazyIMTData} from "@zk-kit/imt.sol/LazyIMT.sol"; + error NotImplemented(); contract WakuRln is Ownable, RlnBase { uint16 public immutable contractIndex; + LazyIMTData tree; + using LazyIMT for LazyIMTData; + constructor( address _poseidonHasher, uint16 _contractIndex ) Ownable() RlnBase(0, 20, _poseidonHasher, address(0)) { contractIndex = _contractIndex; + + // TODO: This errors + uint8 merkleTreeDepth = 32; + tree.init(merkleTreeDepth); } /// Registers a member @@ -24,6 +33,7 @@ contract WakuRln is Ownable, RlnBase { members[idCommitment] = idCommitmentIndex; memberExists[idCommitment] = true; + tree.insert(idCommitment); emit MemberRegistered(idCommitment, idCommitmentIndex); idCommitmentIndex += 1; @@ -71,4 +81,20 @@ contract WakuRln is Ownable, RlnBase { function withdraw() external pure override { revert NotImplemented(); } + + // TODO Some quick getter functions, unsure if needed + function merkleRoot() public view returns(uint256) { + return tree.root(); + } + + function numOfLeaves() public view returns(uint40) { + return tree.numberOfLeaves; + } + + // Unsure how viable is to fetch elements one by one. + // Perhaps we just want the leafs and the client can reconstruct + // the hashes. + function getElement(uint256 elementIndex) public view returns(uint256) { + return tree.elements[elementIndex]; + } } diff --git a/docs/index.md b/docs/index.md index 407a629..25a803a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -14,13 +14,19 @@ error NotImplemented() uint16 contractIndex ``` +### tree + +```solidity +struct LazyIMTData tree +``` + ### constructor ```solidity constructor(address _poseidonHasher, uint16 _contractIndex) public ``` -### _register +### \_register ```solidity function _register(uint256 idCommitment) internal @@ -30,8 +36,8 @@ Registers a member #### Parameters -| Name | Type | Description | -| ---- | ---- | ----------- | +| Name | Type | Description | +| ------------ | ------- | ------------------------------ | | idCommitment | uint256 | The idCommitment of the member | ### register @@ -50,8 +56,8 @@ Allows a user to register as a member #### Parameters -| Name | Type | Description | -| ---- | ---- | ----------- | +| Name | Type | Description | +| ------------ | ------- | ------------------------------ | | idCommitment | uint256 | The idCommitment of the member | ### slash @@ -64,13 +70,13 @@ _Allows a user to slash a member_ #### Parameters -| Name | Type | Description | -| ---- | ---- | ----------- | -| idCommitment | uint256 | The idCommitment of the member | -| receiver | address payable | | -| proof | uint256[8] | | +| Name | Type | Description | +| ------------ | --------------- | ------------------------------ | +| idCommitment | uint256 | The idCommitment of the member | +| receiver | address payable | | +| proof | uint256[8] | | -### _validateRegistration +### \_validateRegistration ```solidity function _validateRegistration(uint256 idCommitment) internal view @@ -78,7 +84,7 @@ function _validateRegistration(uint256 idCommitment) internal view _Inheriting contracts MUST override this function_ -### _validateSlash +### \_validateSlash ```solidity function _validateSlash(uint256 idCommitment, address payable receiver, uint256[8] proof) internal pure @@ -92,6 +98,24 @@ function withdraw() external pure Allows a user to withdraw funds allocated to them upon slashing a member +### merkleRoot + +```solidity +function merkleRoot() public view returns (uint256) +``` + +### numOfLeaves + +```solidity +function numOfLeaves() public view returns (uint40) +``` + +### getElement + +```solidity +function getElement(uint256 elementIndex) public view returns (uint256) +``` + ## StorageAlreadyExists ```solidity @@ -160,18 +184,18 @@ modifier onlyUsableStorage() function initialize(address _poseidonHasher) external ``` -### _authorizeUpgrade +### \_authorizeUpgrade ```solidity function _authorizeUpgrade(address newImplementation) internal ``` -_Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by +\_Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by {upgradeTo} and {upgradeToAndCall}. Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}. -```solidity +````solidity function _authorizeUpgrade(address) internal override onlyOwner {} ```_ @@ -179,7 +203,7 @@ function _authorizeUpgrade(address) internal override onlyOwner {} ```solidity function _insertIntoStorageMap(address storageAddress) internal -``` +```` ### registerStorage @@ -216,4 +240,3 @@ function register(uint16 storageIndex, uint256 commitment) external ```solidity function forceProgress() external ``` - diff --git a/package.json b/package.json index f0da90e..2a28cf4 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,8 @@ "typescript": "^4.7.4" }, "dependencies": { + "@zk-kit/imt.sol": "^2.0.0-beta", + "@zk-kit/incremental-merkle-tree.sol": "^1.7.0", "dotenv": "^16.0.1" }, "lint-staged": { diff --git a/yarn.lock b/yarn.lock index 91be2ee..6ef058a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1043,6 +1043,20 @@ resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31" integrity sha512-GpSwvyXOcOOlV70vbnzjj4fW5xW/FdUF6nQEt1ENy7m4ZCczi1+/buVUPAqmGfqznsORNFzUMjctTIp8a9tuCQ== +"@zk-kit/imt.sol@^2.0.0-beta": + version "2.0.0-beta" + resolved "https://registry.yarnpkg.com/@zk-kit/imt.sol/-/imt.sol-2.0.0-beta.tgz#102f88a52bd6848783fddc0db8219de2f163e684" + integrity sha512-bH7RvI5WHAEswUwPspUY582O2+71xbYv5aL+DM4xkaA0GdMyMLUwf5c1yJ4wrt46hp07iXCXJsLXdtLNsTnvZw== + dependencies: + poseidon-solidity "0.0.5" + +"@zk-kit/incremental-merkle-tree.sol@^1.7.0": + version "1.7.0" + resolved "https://registry.yarnpkg.com/@zk-kit/incremental-merkle-tree.sol/-/incremental-merkle-tree.sol-1.7.0.tgz#9d6166c2d84b8bb4fae709c7d729aae6c8c6474f" + integrity sha512-1HF5HEQ2GVKYBAKeNlhiQ7Fb7g2x6iBQ/bBovxBQYx8xuJlD8jPJU/fn2foMVI42+BQtz2V/V4tVtn+0uNL1Rg== + dependencies: + poseidon-solidity "0.0.4" + abort-controller@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392" @@ -6828,6 +6842,16 @@ pinkie@^2.0.0: resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870" integrity sha512-MnUuEycAemtSaeFSjXKW/aroV7akBbY+Sv+RkyqFjgAe73F+MR0TBWKBRDkmfWq/HiFmdavfZ1G7h4SPZXaCSg== +poseidon-solidity@0.0.4: + version "0.0.4" + resolved "https://registry.yarnpkg.com/poseidon-solidity/-/poseidon-solidity-0.0.4.tgz#fb80181fb2b17756d98449d54e4cc2ecd8daa4d0" + integrity sha512-+S6jlCXKARCFbw0MMOjfR8gcKUVeB8F3Vu5S+1zDvb8N8cVjiW6WsP9kPqNaZCxjRKakt6LKVRPdA39YbpWfdg== + +poseidon-solidity@0.0.5: + version "0.0.5" + resolved "https://registry.yarnpkg.com/poseidon-solidity/-/poseidon-solidity-0.0.5.tgz#3f93e01cfe25f6d2f2fac49734fbb00961b84655" + integrity sha512-NzrvSwHzvZgT4hvg2GyGqeR+UOU/eLSEt4wAoXEua+VaR7NTKKwx1X9bPlh1VMBEVEno+IWvkRBbidFGzTeAqQ== + posix-character-classes@^0.1.0: version "0.1.1" resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"