Skip to content

Commit

Permalink
Replace Merkle verify function for OZ one
Browse files Browse the repository at this point in the history
  • Loading branch information
manumonti committed Jun 12, 2024
1 parent a4f6b71 commit 63d1651
Showing 1 changed file with 2 additions and 29 deletions.
31 changes: 2 additions & 29 deletions contracts/CumulativeMerkleDrop.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract CumulativeMerkleDrop is Ownable, ICumulativeMerkleDrop {
require(IERC20(token_).totalSupply() > 0, "Token contract must be set");
require(rewardsHolder_ != address(0), "Rewards Holder must be an address");
require(address(application_) != address(0), "Application must be an address");

transferOwnership(newOwner);
token = token_;
application = application_;
Expand Down Expand Up @@ -69,7 +69,7 @@ contract CumulativeMerkleDrop is Ownable, ICumulativeMerkleDrop {

// Verify the merkle proof
bytes32 leaf = keccak256(abi.encodePacked(stakingProvider, beneficiary, cumulativeAmount));
require(_verifyAsm(merkleProof, expectedMerkleRoot, leaf), "Invalid proof");
require(verify(merkleProof, expectedMerkleRoot, leaf), "Invalid proof");

// Mark it claimed
uint256 preclaimed = cumulativeClaimed[stakingProvider];
Expand Down Expand Up @@ -128,31 +128,4 @@ contract CumulativeMerkleDrop is Ownable, ICumulativeMerkleDrop {
function verify(bytes32[] calldata merkleProof, bytes32 root, bytes32 leaf) public pure returns (bool) {
return merkleProof.verify(root, leaf);
}

function _verifyAsm(bytes32[] calldata proof, bytes32 root, bytes32 leaf) private pure returns (bool valid) {
// solhint-disable-next-line no-inline-assembly
assembly {
let mem1 := mload(0x40)
let mem2 := add(mem1, 0x20)
let ptr := proof.offset

for { let end := add(ptr, mul(0x20, proof.length)) } lt(ptr, end) { ptr := add(ptr, 0x20) } {
let node := calldataload(ptr)

switch lt(leaf, node)
case 1 {
mstore(mem1, leaf)
mstore(mem2, node)
}
default {
mstore(mem1, node)
mstore(mem2, leaf)
}

leaf := keccak256(mem1, 0x40)
}

valid := eq(root, leaf)
}
}
}

0 comments on commit 63d1651

Please sign in to comment.