|
| 1 | +use dep::protocol_types::{ |
| 2 | + abis::{ |
| 3 | + complete_address::CompleteAddress, |
| 4 | + new_contract_data::NewContractData as ContractLeafPreimage, |
| 5 | + }, |
| 6 | + address::{AztecAddress, EthAddress}, |
| 7 | + point::Point, |
| 8 | +}; |
| 9 | +use dep::std::merkle::compute_merkle_root; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + context::PrivateContext, |
| 13 | + oracle::get_membership_witness::get_contract_membership_witness, |
| 14 | +}; |
| 15 | + |
| 16 | +// Proves that a contract exists at block `block_number` and returns its address. |
| 17 | +// Note: This can be used to approximate a factory pattern --> a factory contract could perform this proof and that |
| 18 | +// way verify that a contract at a given address is what it expects. Then it could store it in an internal |
| 19 | +// map of contracts (like what Uniswap Factory does with pool contracts - it stores them in a mapping). |
| 20 | +// By passing in the construct hash the factory can also verify that the contract was constructed with the |
| 21 | +// correct constructor arguments. Typically the factory would store the expected construct hash and assert that |
| 22 | +// it is what it expects. The constructor param check is the reason of why we pass in the preimage of contract's |
| 23 | +// aztec address instead of just the address. |
| 24 | +pub fn prove_contract_inclusion( |
| 25 | + deployer_public_key: Point, |
| 26 | + contract_address_salt: Field, |
| 27 | + function_tree_root: Field, |
| 28 | + constructor_hash: Field, |
| 29 | + portal_contract_address: EthAddress, |
| 30 | + block_number: u32, // The block at which we'll prove that the public value exists |
| 31 | + context: PrivateContext |
| 32 | +) -> AztecAddress { |
| 33 | + // 1) Get block header from oracle and ensure that the block is included in the archive. |
| 34 | + let block_header = context.get_block_header(block_number); |
| 35 | + |
| 36 | + // 2) Compute the contract address |
| 37 | + let contract_address = CompleteAddress::compute( |
| 38 | + deployer_public_key, |
| 39 | + contract_address_salt, |
| 40 | + function_tree_root, |
| 41 | + constructor_hash |
| 42 | + ).address; |
| 43 | + |
| 44 | + // 3) Form the contract tree leaf preimage |
| 45 | + let preimage = ContractLeafPreimage { contract_address, portal_contract_address, function_tree_root }; |
| 46 | + |
| 47 | + // 4) Get the contract tree leaf by hashing the preimage |
| 48 | + let contract_leaf = preimage.hash(); |
| 49 | + |
| 50 | + // 5) Get the membership witness of the leaf in the contract tree |
| 51 | + let witness = get_contract_membership_witness(block_number, contract_leaf); |
| 52 | + |
| 53 | + // 6) Prove that the leaf is in the contract tree |
| 54 | + assert( |
| 55 | + block_header.contract_tree_root |
| 56 | + == compute_merkle_root(contract_leaf, witness.index, witness.path), "Proving contract inclusion failed" |
| 57 | + ); |
| 58 | + |
| 59 | + // --> Now we have traversed the trees all the way up to archive root. |
| 60 | + |
| 61 | + contract_address |
| 62 | +} |
0 commit comments