Commit 72289cf6 by github-actions

Merge upstream openzeppelin-contracts into upstream-patched

parents 91dc5604 d5194725
...@@ -3,7 +3,13 @@ ...@@ -3,7 +3,13 @@
pragma solidity ^0.8.0; pragma solidity ^0.8.0;
/** /**
* @dev These functions deal with verification of Merkle trees (hash trees), * @dev These functions deal with verification of Merkle Trees proofs.
*
* The proofs can be generated using the JavaScript library
* https://github.com/miguelmota/merkletreejs[merkletreejs].
* Note: the hashing algorithm should be keccak256 and pair sorting should be enabled.
*
* See `test/utils/cryptography/MerkleProof.test.js` for some examples.
*/ */
library MerkleProof { library MerkleProof {
/** /**
......
...@@ -89,7 +89,7 @@ library EnumerableSet { ...@@ -89,7 +89,7 @@ library EnumerableSet {
// Move the last value to the index where the value to delete is // Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastvalue; set._values[toDeleteIndex] = lastvalue;
// Update the index for the moved value // Update the index for the moved value
set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex
// Delete the slot where the moved value was stored // Delete the slot where the moved value was stored
set._values.pop(); set._values.pop();
......
...@@ -14,8 +14,8 @@ contract('MerkleProof', function (accounts) { ...@@ -14,8 +14,8 @@ contract('MerkleProof', function (accounts) {
describe('verify', function () { describe('verify', function () {
it('returns true for a valid Merkle proof', async function () { it('returns true for a valid Merkle proof', async function () {
const elements = ['a', 'b', 'c', 'd']; const elements = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split('');
const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true }); const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true, sortPairs: true });
const root = merkleTree.getHexRoot(); const root = merkleTree.getHexRoot();
...@@ -28,7 +28,7 @@ contract('MerkleProof', function (accounts) { ...@@ -28,7 +28,7 @@ contract('MerkleProof', function (accounts) {
it('returns false for an invalid Merkle proof', async function () { it('returns false for an invalid Merkle proof', async function () {
const correctElements = ['a', 'b', 'c']; const correctElements = ['a', 'b', 'c'];
const correctMerkleTree = new MerkleTree(correctElements, keccak256, { hashLeaves: true }); const correctMerkleTree = new MerkleTree(correctElements, keccak256, { hashLeaves: true, sortPairs: true });
const correctRoot = correctMerkleTree.getHexRoot(); const correctRoot = correctMerkleTree.getHexRoot();
...@@ -37,14 +37,14 @@ contract('MerkleProof', function (accounts) { ...@@ -37,14 +37,14 @@ contract('MerkleProof', function (accounts) {
const badElements = ['d', 'e', 'f']; const badElements = ['d', 'e', 'f'];
const badMerkleTree = new MerkleTree(badElements); const badMerkleTree = new MerkleTree(badElements);
const badProof = badMerkleTree.getHexProof(badElements[0], keccak256, { hashLeaves: true }); const badProof = badMerkleTree.getHexProof(badElements[0], keccak256, { hashLeaves: true, sortPairs: true });
expect(await this.merkleProof.verify(badProof, correctRoot, correctLeaf)).to.equal(false); expect(await this.merkleProof.verify(badProof, correctRoot, correctLeaf)).to.equal(false);
}); });
it('returns false for a Merkle proof of invalid length', async function () { it('returns false for a Merkle proof of invalid length', async function () {
const elements = ['a', 'b', 'c']; const elements = ['a', 'b', 'c'];
const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true }); const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true, sortPairs: true });
const root = merkleTree.getHexRoot(); const root = merkleTree.getHexRoot();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment