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
863ad48a
Commit
863ad48a
authored
Jul 28, 2017
by
Yondon Fu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Check proof length multiple of 32. Use keccak256 instead of sha3
parent
bc3db5d4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
5 deletions
+25
-5
MerkleProof.sol
contracts/MerkleProof.sol
+5
-2
MerkleProof.js
test/MerkleProof.js
+20
-3
No files found.
contracts/MerkleProof.sol
View file @
863ad48a
...
@@ -14,6 +14,9 @@ library MerkleProof {
...
@@ -14,6 +14,9 @@ library MerkleProof {
* @param _leaf Leaf of Merkle tree
* @param _leaf Leaf of Merkle tree
*/
*/
function verifyProof(bytes _proof, bytes32 _root, bytes32 _leaf) constant returns (bool) {
function verifyProof(bytes _proof, bytes32 _root, bytes32 _leaf) constant returns (bool) {
// Check if proof length is a multiple of 32
if (_proof.length % 32 != 0) return false;
bytes32 proofElement;
bytes32 proofElement;
bytes32 computedHash = _leaf;
bytes32 computedHash = _leaf;
...
@@ -25,10 +28,10 @@ library MerkleProof {
...
@@ -25,10 +28,10 @@ library MerkleProof {
if (computedHash < proofElement) {
if (computedHash < proofElement) {
// Hash(current computed hash + current element of the proof)
// Hash(current computed hash + current element of the proof)
computedHash =
sha3
(computedHash, proofElement);
computedHash =
keccak256
(computedHash, proofElement);
} else {
} else {
// Hash(current element of the proof + current computed hash)
// Hash(current element of the proof + current computed hash)
computedHash =
sha3
(proofElement, computedHash);
computedHash =
keccak256
(proofElement, computedHash);
}
}
}
}
...
...
test/MerkleProof.js
View file @
863ad48a
...
@@ -26,18 +26,35 @@ contract('MerkleProof', function(accounts) {
...
@@ -26,18 +26,35 @@ contract('MerkleProof', function(accounts) {
});
});
it
(
"should return false for an invalid Merkle proof"
,
async
function
()
{
it
(
"should return false for an invalid Merkle proof"
,
async
function
()
{
const
correctElements
=
[
"a"
,
"b"
,
"c"
].
map
(
el
=>
sha3
(
el
));
const
correctMerkleTree
=
new
MerkleTree
(
correctElements
);
const
correctRoot
=
correctMerkleTree
.
getHexRoot
();
const
correctLeaf
=
correctMerkleTree
.
bufToHex
(
correctElements
[
0
]);
const
badElements
=
[
"d"
,
"e"
,
"f"
].
map
(
el
=>
sha3
(
el
))
const
badMerkleTree
=
new
MerkleTree
(
badElements
)
const
badProof
=
badMerkleTree
.
getHexProof
(
badElements
[
0
])
const
result
=
await
merkleProof
.
verifyProof
(
badProof
,
correctRoot
,
correctLeaf
);
assert
.
isNotOk
(
result
,
"verifyProof did not return false for an invalid proof"
);
});
it
(
"should return false for a Merkle proof of invalid length"
,
async
function
()
{
const
elements
=
[
"a"
,
"b"
,
"c"
].
map
(
el
=>
sha3
(
el
));
const
elements
=
[
"a"
,
"b"
,
"c"
].
map
(
el
=>
sha3
(
el
));
const
merkleTree
=
new
MerkleTree
(
elements
);
const
merkleTree
=
new
MerkleTree
(
elements
);
const
root
=
merkleTree
.
getHexRoot
();
const
root
=
merkleTree
.
getHexRoot
();
const
proof
=
merkleTree
.
getHexProof
(
elements
[
0
]);
const
proof
=
merkleTree
.
getHexProof
(
elements
[
0
]);
const
badProof
=
proof
.
slice
(
0
,
proof
.
length
-
32
);
const
badProof
=
proof
.
slice
(
0
,
proof
.
length
-
5
);
const
leaf
=
merkleTree
.
bufToHex
(
elements
[
0
]);
const
leaf
=
merkleTree
.
bufToHex
(
elements
[
0
]);
const
result
=
await
merkleProof
.
verifyProof
(
badProof
,
root
,
leaf
);
const
result
=
await
merkleProof
.
verifyProof
(
badProof
,
root
,
leaf
);
assert
.
isNotOk
(
result
,
"verifyProof did not return false for
an invalid proof
"
);
assert
.
isNotOk
(
result
,
"verifyProof did not return false for
proof of invalid length
"
);
})
;
})
});
});
});
});
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