Further expanding on @Paul Etscheit, sorting the hash pairs simplifies the verification of merkle proofs.
Example:
The open zeppelin merkle proof verification smart contract, requires the hash pairs to be sorted. This is made even clearer when you look at their tests.
Merkle inclusion proofs can be verified through a function function verify(bytes[] proof, bytes root, bytes leaf)
. The function will return true if root
is equal to the root computed from the leaf and proof nodes.
Using sorted hash pairs means your merkle proof does not needs to contain information about the order in which the child hashes should be combined in. i.e. Your proof can simply be an array of hashes.
For example:
Using merkletreejs and the following merkle tree:
└─ 7075152d03a5cd92104887b476862778ec0c87be5c2fa1c0a90f87c49fad6eff
├─ e5a01fee14e0ed5c48714f22180f25ad8365b53f9779f79dc4a3d7e93963f94a
│ ├─ ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
│ └─ 3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
└─ 2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6
└─ 2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6
Without sorting the hashpairs you need to use .getProof(<node>)
, which returns a proof of the form:
[
{
position: 'right',
data: <Buffer 3e 23 e8 16 00 39 59 4a 33 89 4f 65 64 e1 b1 34 8b bd 7a 00 88 d4 2c 4a cb 73 ee ae d5 9c 00 9d>
},
{
position: 'right',
data: <Buffer 2e 7d 2c 03 a9 50 7a e2 65 ec f5 b5 35 68 85 a5 33 93 a2 02 9d 24 13 94 99 72 65 a1 a2 5a ef c6>
}
]
When you sort the hashpairs, you can use .getHexProof(<node>)
, which returns a proof of the form:
[
'0x3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d',
'0x2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6'
]