ens-usernames/contracts/common/MerkleProof.sol
Ricardo Guilherme Schmidt 3d37a0a1c9
Update to use embark 6.0.0 (#99)
* trim trailing spaces

* update to embark 4.0.2

* bump to solc 0.5.4

* use .selector

* use solidity function selector

* update to embark 6.0.0

* use mocks instead of "instanceOf" tool+tests fix

* update to solidity 0.5.11

* update to solidity 0.5.11

* add spdx license identifiers

* update ENS contracts

* natspec fix return values
2020-07-24 19:16:01 -03:00

46 lines
1.4 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity 0.5.11;
/**
* @title MerkleProof
* @dev Merkle proof verification based on
* https://github.com/ameensol/merkle-tree-solidity/blob/master/src/MerkleProof.sol
*/
library MerkleProof {
/**
* @dev Verifies a Merkle proof proving the existence of a leaf in a Merkle tree. Assumes that each pair of leaves
* and each pair of pre-images are sorted.
* @param _proof Merkle proof containing sibling hashes on the branch from the leaf to the root of the Merkle tree
* @param _root Merkle root
* @param _leaf Leaf of Merkle tree
*/
function verifyProof(
bytes32[] memory _proof,
bytes32 _root,
bytes32 _leaf
)
internal
pure
returns (bool)
{
bytes32 computedHash = _leaf;
for (uint256 i = 0; i < _proof.length; i++) {
bytes32 proofElement = _proof[i];
if (computedHash < proofElement) {
// Hash(current computed hash + current element of the proof)
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
// Hash(current element of the proof + current computed hash)
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}
// Check if the computed hash (root) is equal to the provided root
return computedHash == _root;
}
}