github.com/ethereum-optimism/optimism@v1.7.2/packages/contracts-bedrock/src/libraries/trie/SecureMerkleTrie.sol (about) 1 // SPDX-License-Identifier: MIT 2 pragma solidity ^0.8.0; 3 4 import { MerkleTrie } from "./MerkleTrie.sol"; 5 6 /// @title SecureMerkleTrie 7 /// @notice SecureMerkleTrie is a thin wrapper around the MerkleTrie library that hashes the input 8 /// keys. Ethereum's state trie hashes input keys before storing them. 9 library SecureMerkleTrie { 10 /// @notice Verifies a proof that a given key/value pair is present in the Merkle trie. 11 /// @param _key Key of the node to search for, as a hex string. 12 /// @param _value Value of the node to search for, as a hex string. 13 /// @param _proof Merkle trie inclusion proof for the desired node. Unlike traditional Merkle 14 /// trees, this proof is executed top-down and consists of a list of RLP-encoded 15 /// nodes that make a path down to the target node. 16 /// @param _root Known root of the Merkle trie. Used to verify that the included proof is 17 /// correctly constructed. 18 /// @return valid_ Whether or not the proof is valid. 19 function verifyInclusionProof( 20 bytes memory _key, 21 bytes memory _value, 22 bytes[] memory _proof, 23 bytes32 _root 24 ) 25 internal 26 pure 27 returns (bool valid_) 28 { 29 bytes memory key = _getSecureKey(_key); 30 valid_ = MerkleTrie.verifyInclusionProof(key, _value, _proof, _root); 31 } 32 33 /// @notice Retrieves the value associated with a given key. 34 /// @param _key Key to search for, as hex bytes. 35 /// @param _proof Merkle trie inclusion proof for the key. 36 /// @param _root Known root of the Merkle trie. 37 /// @return value_ Value of the key if it exists. 38 function get(bytes memory _key, bytes[] memory _proof, bytes32 _root) internal pure returns (bytes memory value_) { 39 bytes memory key = _getSecureKey(_key); 40 value_ = MerkleTrie.get(key, _proof, _root); 41 } 42 43 /// @notice Computes the hashed version of the input key. 44 /// @param _key Key to hash. 45 /// @return hash_ Hashed version of the key. 46 function _getSecureKey(bytes memory _key) private pure returns (bytes memory hash_) { 47 hash_ = abi.encodePacked(keccak256(_key)); 48 } 49 }