|
| 1 | +use crate::{ |
| 2 | + context::{calls::PublicStaticCall, PrivateContext}, |
| 3 | + protocol::{abis::function_selector::FunctionSelector, address::AztecAddress, constants::PUBLIC_CHECKS_ADDRESS}, |
| 4 | +}; |
| 5 | + |
| 6 | +/// Hand-written interface stub for the `PublicChecks` canonical contract. |
| 7 | +/// |
| 8 | +/// The `PublicChecks` contract exposes two view functions that can be enqueued |
| 9 | +/// from private context via `enqueue_view_incognito` to assert timestamp or |
| 10 | +/// block-number constraints without revealing the calling contract address. |
| 11 | +/// |
| 12 | +/// This stub exists so that the `public_checks_contract` crate itself does not |
| 13 | +/// need to depend on `canonical_addresses` (which would create a structural |
| 14 | +/// cycle). Consumer contracts call the helpers below directly via |
| 15 | +/// `use aztec::public_checks::*` without taking a dep on the contract crate. |
| 16 | +/// |
| 17 | +/// The selectors are derived with `comptime { FunctionSelector::from_signature(...) }`, |
| 18 | +/// which matches exactly what the `#[aztec]` macro generates for the real contract. |
| 19 | +struct PublicChecksInterface { |
| 20 | + target_contract: AztecAddress, |
| 21 | +} |
| 22 | + |
| 23 | +impl PublicChecksInterface { |
| 24 | + pub fn at(target_contract: AztecAddress) -> Self { |
| 25 | + Self { target_contract } |
| 26 | + } |
| 27 | + |
| 28 | + /// Returns a `PublicStaticCall` that asserts `timestamp <op> value`. |
| 29 | + pub fn check_timestamp(self, operation: u8, value: u64) -> PublicStaticCall<15, 2, ()> { |
| 30 | + let selector = comptime { FunctionSelector::from_signature("check_timestamp(u8,u64)") }; |
| 31 | + PublicStaticCall::new( |
| 32 | + self.target_contract, |
| 33 | + selector, |
| 34 | + "check_timestamp", |
| 35 | + [operation as Field, value as Field], |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + /// Returns a `PublicStaticCall` that asserts `block_number <op> value`. |
| 40 | + pub fn check_block_number(self, operation: u8, value: u32) -> PublicStaticCall<18, 2, ()> { |
| 41 | + let selector = comptime { FunctionSelector::from_signature("check_block_number(u8,u32)") }; |
| 42 | + PublicStaticCall::new( |
| 43 | + self.target_contract, |
| 44 | + selector, |
| 45 | + "check_block_number", |
| 46 | + [operation as Field, value as Field], |
| 47 | + ) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// docs:start:helper_public_checks_functions |
| 52 | +/// Asserts that the current timestamp in the enqueued public call enqueued by `check_timestamp` satisfies |
| 53 | +/// the `operation` with respect to the `value. Preserves privacy by performing the check via the public checks |
| 54 | +/// contract. |
| 55 | +/// This conceals an address of the calling contract by setting `context.msg_sender` to the public checks contract |
| 56 | +/// address. |
| 57 | +pub fn privately_check_timestamp(operation: u8, value: u64, context: &mut PrivateContext) { |
| 58 | + PublicChecksInterface::at(PUBLIC_CHECKS_ADDRESS).check_timestamp(operation, value).enqueue_view_incognito(context); |
| 59 | +} |
| 60 | + |
| 61 | +/// Asserts that the current block number in the enqueued public call enqueued by `check_block_number` satisfies |
| 62 | +/// the `operation` with respect to the `value. Preserves privacy by performing the check via the public checks |
| 63 | +/// contract. |
| 64 | +/// This conceals an address of the calling contract by setting `context.msg_sender` to the public checks contract |
| 65 | +/// address. |
| 66 | +pub fn privately_check_block_number(operation: u8, value: u32, context: &mut PrivateContext) { |
| 67 | + // docs:start:enqueueing |
| 68 | + PublicChecksInterface::at(PUBLIC_CHECKS_ADDRESS).check_block_number(operation, value).enqueue_view_incognito(context); |
| 69 | + // docs:end:enqueueing |
| 70 | +} |
| 71 | +// docs:end:helper_public_checks_functions |
0 commit comments