Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
82e3ec3a
Unverified
Commit
82e3ec3a
authored
Mar 12, 2021
by
Hadrien Croubois
Committed by
GitHub
Mar 12, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix MerkleProof generation in tests and add some documentation (#2585)
parent
682def9f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
6 deletions
+12
-6
MerkleProof.sol
contracts/utils/cryptography/MerkleProof.sol
+7
-1
MerkleProof.test.js
test/utils/cryptography/MerkleProof.test.js
+5
-5
No files found.
contracts/utils/cryptography/MerkleProof.sol
View file @
82e3ec3a
...
@@ -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 {
/**
/**
...
...
test/utils/cryptography/MerkleProof.test.js
View file @
82e3ec3a
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment