2023-12-21 09:05:53 +00:00
|
|
|
# Nimbus
|
2024-01-24 17:18:45 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2023-12-21 09:05:53 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2024-01-22 09:11:37 +00:00
|
|
|
std/sequtils,
|
2023-12-21 09:05:53 +00:00
|
|
|
stint,
|
|
|
|
eth/[common, rlp, trie/hexary_proof_verification],
|
2024-01-09 15:09:02 +00:00
|
|
|
stew/results,
|
2024-01-22 09:11:37 +00:00
|
|
|
./state_proof_types
|
2023-12-21 09:05:53 +00:00
|
|
|
|
|
|
|
export results
|
|
|
|
|
|
|
|
proc verifyAccount*(
|
|
|
|
trustedStateRoot: KeccakHash,
|
|
|
|
address: EthAddress,
|
|
|
|
account: Account,
|
2024-02-28 17:31:45 +00:00
|
|
|
proof: AccountProof,
|
|
|
|
): Result[void, string] =
|
2024-01-09 15:09:02 +00:00
|
|
|
if proof.len() == 0:
|
|
|
|
return err("proof is empty")
|
|
|
|
|
2023-12-21 09:05:53 +00:00
|
|
|
let key = toSeq(keccakHash(address).data)
|
|
|
|
let value = rlp.encode(account)
|
|
|
|
|
|
|
|
let proofResult = verifyMptProof(proof.MptProof, trustedStateRoot, key, value)
|
|
|
|
|
|
|
|
case proofResult.kind
|
|
|
|
of ValidProof:
|
|
|
|
ok()
|
|
|
|
of MissingKey:
|
2024-01-09 15:09:02 +00:00
|
|
|
err("missing key")
|
2023-12-21 09:05:53 +00:00
|
|
|
of InvalidProof:
|
|
|
|
err(proofResult.errorMsg)
|
|
|
|
|
|
|
|
proc verifyContractStorageSlot*(
|
|
|
|
trustedStorageRoot: KeccakHash,
|
|
|
|
slotKey: UInt256,
|
|
|
|
slotValue: UInt256,
|
2024-02-28 17:31:45 +00:00
|
|
|
proof: StorageProof,
|
|
|
|
): Result[void, string] =
|
2024-01-09 15:09:02 +00:00
|
|
|
if proof.len() == 0:
|
|
|
|
return err("proof is empty")
|
2023-12-21 09:05:53 +00:00
|
|
|
|
|
|
|
let key = toSeq(keccakHash(toBytesBE(slotKey)).data)
|
|
|
|
let value = rlp.encode(slotValue)
|
|
|
|
|
|
|
|
let proofResult = verifyMptProof(proof.MptProof, trustedStorageRoot, key, value)
|
|
|
|
|
|
|
|
case proofResult.kind
|
|
|
|
of ValidProof:
|
|
|
|
ok()
|
|
|
|
of MissingKey:
|
2024-01-09 15:09:02 +00:00
|
|
|
err("missing key")
|
2023-12-21 09:05:53 +00:00
|
|
|
of InvalidProof:
|
|
|
|
err(proofResult.errorMsg)
|
|
|
|
|
|
|
|
func verifyContractBytecode*(
|
2024-02-28 17:31:45 +00:00
|
|
|
trustedCodeHash: KeccakHash, bytecode: openArray[byte]
|
|
|
|
): Result[void, string] =
|
2023-12-21 09:05:53 +00:00
|
|
|
if trustedCodeHash == keccakHash(bytecode):
|
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
err("hash of bytecode doesn't match the expected code hash")
|