Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 4, 2024
1 parent 6561909 commit cfb20f8
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 31 deletions.
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ library Constants {
uint256 internal constant FEE_JUICE_ADDRESS =
10248142274714515101077825679585135641434041564851038865006795089686437446849;
uint256 internal constant ROUTER_ADDRESS =
1063406426795385693869347374716646040831772088487068062386500675099194233623;
7268799613082469933251235702514160327341161584122631177360064643484764773587;
uint256 internal constant AZTEC_ADDRESS_LENGTH = 1;
uint256 internal constant GAS_FEES_LENGTH = 2;
uint256 internal constant GAS_LENGTH = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ type = "contract"
aztec = { path = "../../../aztec-nr/aztec" }
authwit = { path = "../../../aztec-nr/authwit" }
token = { path = "../token_contract" }
router = { path = "../router_contract" }
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ mod subscription_note;
mod dapp_payload;

contract AppSubscription {
use crate::{dapp_payload::DAppPayload, subscription_note::{SubscriptionNote, SUBSCRIPTION_NOTE_LEN}};
use crate::{dapp_payload::DAppPayload, subscription_note::SubscriptionNote};

use aztec::{
prelude::{
AztecAddress, FunctionSelector, PrivateContext, NoteHeader, Map, PrivateMutable, PublicMutable,
SharedImmutable
},
prelude::{AztecAddress, Map, PrivateMutable, SharedImmutable},
encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys},
keys::getters::get_current_public_keys, protocol_types::constants::MAX_FIELD_VALUE
keys::getters::get_current_public_keys,
protocol_types::constants::{MAX_FIELD_VALUE, ROUTER_ADDRESS}
};
use authwit::{auth_witness::get_auth_witness, auth::assert_current_call_valid_authwit};
use authwit::auth::assert_current_call_valid_authwit;
use token::Token;
use router::Router;
use aztec::note::note_getter_options::Comparator;

#[aztec(storage)]
struct Storage {
Expand Down Expand Up @@ -53,7 +53,9 @@ contract AppSubscription {

context.end_setup();

AppSubscription::at(context.this_address()).assert_not_expired(note.expiry_block_number).enqueue_view(&mut context);
// We check that the note is not expired. We do that via the router contract to conceal which contract
// is performing the check.
Router::at(ROUTER_ADDRESS).check_block_number(Comparator.GT, note.expiry_block_number).call(&mut context);

payload.execute_calls(&mut context, storage.target_address.read_private());
}
Expand All @@ -74,23 +76,6 @@ contract AppSubscription {
storage.fee_juice_limit_per_tx.initialize(fee_juice_limit_per_tx);
}

#[aztec(public)]
#[aztec(internal)]
#[aztec(view)]
fn assert_not_expired(expiry_block_number: Field) {
assert((context.block_number()) as u64 < expiry_block_number as u64);
}

#[aztec(public)]
#[aztec(internal)]
#[aztec(view)]
fn assert_block_number(expiry_block_number: Field) {
assert(
(context.block_number() + SUBSCRIPTION_DURATION_IN_BLOCKS) as u64
>= expiry_block_number as u64
);
}

#[aztec(private)]
fn subscribe(subscriber: AztecAddress, nonce: Field, expiry_block_number: Field, tx_count: Field) {
assert(tx_count as u64 <= SUBSCRIPTION_TXS as u64);
Expand All @@ -102,8 +87,13 @@ contract AppSubscription {
nonce
).call(&mut context);

// Assert that the given expiry_block_number < current_block_number + SUBSCRIPTION_DURATION_IN_BLOCKS.
AppSubscription::at(context.this_address()).assert_block_number(expiry_block_number).enqueue_view(&mut context);
// Assert that the given expiry_block_number - SUBSCRIPTION_DURATION_IN_BLOCKS < current_block_number.
// We do that via the router contract to conceal which contract is performing the check.
Router::at(ROUTER_ADDRESS).check_block_number(
Comparator.LT,
expiry_block_number - SUBSCRIPTION_DURATION_IN_BLOCKS
).call(&mut context);

let subscriber_keys = get_current_public_keys(&mut context, subscriber);
let msg_sender_ovpk_m = get_current_public_keys(&mut context, context.msg_sender()).ovpk_m;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ contract Router {

#[aztec(private)]
fn check_timestamp(operation: u8, value: u64) {
Router::at(context.this_address())._check_timestamp(operation, value).enqueue(&mut context);
Router::at(context.this_address())._check_timestamp(operation, value).enqueue_view(&mut context);
}

#[aztec(public)]
#[aztec(internal)]
#[aztec(view)]
fn _check_timestamp(operation: u8, value: u64) {
let current_timestamp = context.timestamp();

Expand All @@ -32,4 +33,35 @@ contract Router {
assert(!is_lt, "Timestamp mismatch.");
}
}

#[aztec(private)]
fn check_block_number(operation: u8, value: Field) {
Router::at(context.this_address())._check_block_number(operation, value).enqueue_view(&mut context);
}

#[aztec(public)]
#[aztec(internal)]
#[aztec(view)]
fn _check_block_number(operation: u8, value: Field) {
let current_block_number = context.block_number();

// Values are computed ahead of time because circuits evaluate all branches
let is_equal = value == current_block_number;
// TODO(#5345): represent block number everywhere as u32?
let is_lt = value as u32 < current_block_number as u32;

if operation == Comparator.EQ {
assert(is_equal, "Block number mismatch.");
} else if operation == Comparator.NEQ {
assert(!is_equal, "Block number mismatch.");
} else if operation == Comparator.LT {
assert(is_lt, "Block number mismatch.");
} else if operation == Comparator.LTE {
assert(is_lt | is_equal, "Block number mismatch.");
} else if operation == Comparator.GT {
assert(!is_lt & !is_equal, "Block number mismatch.");
} else if operation == Comparator.GTE {
assert(!is_lt, "Block number mismatch.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ global CANONICAL_AUTH_REGISTRY_ADDRESS = AztecAddress::from_field(0x24877c50868f
global DEPLOYER_CONTRACT_ADDRESS = AztecAddress::from_field(0x2ab1a2bd6d07d8d61ea56d85861446349e52c6b7c0612b702cb1e6db6ad0b089);
global REGISTERER_CONTRACT_ADDRESS = AztecAddress::from_field(0x05d15342d76e46e5be07d3cda0d753158431cdc5e39d29ce4e8fe1f5c070564a);
global FEE_JUICE_ADDRESS = AztecAddress::from_field(0x16a83e3395bc921a2441db55dce24f0e0932636901a2e676fa68b9b2b9a644c1);
global ROUTER_ADDRESS = AztecAddress::from_field(0x0259dde096e975185dd026b0aade4fc9ff1478c361dbbe0b3922b8c206e31317);
global ROUTER_ADDRESS = AztecAddress::from_field(0x1011feaa54609098a884322267ec754c637b280c15aa79c3be9f1394e2b29cd3);

// LENGTH OF STRUCTS SERIALIZED TO FIELDS
global AZTEC_ADDRESS_LENGTH = 1;
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const DEPLOYER_CONTRACT_ADDRESS = 193109947607833303683371634801986023939
export const REGISTERER_CONTRACT_ADDRESS =
2631409926445785927331173506476539962589925110142857699603561302478860342858n;
export const FEE_JUICE_ADDRESS = 10248142274714515101077825679585135641434041564851038865006795089686437446849n;
export const ROUTER_ADDRESS = 1063406426795385693869347374716646040831772088487068062386500675099194233623n;
export const ROUTER_ADDRESS = 7268799613082469933251235702514160327341161584122631177360064643484764773587n;
export const AZTEC_ADDRESS_LENGTH = 1;
export const GAS_FEES_LENGTH = 2;
export const GAS_LENGTH = 2;
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/pxe/src/pxe_service/create_pxe_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getCanonicalFeeJuice } from '@aztec/protocol-contracts/fee-juice';
import { getCanonicalInstanceDeployer } from '@aztec/protocol-contracts/instance-deployer';
import { getCanonicalKeyRegistry } from '@aztec/protocol-contracts/key-registry';
import { getCanonicalMultiCallEntrypointContract } from '@aztec/protocol-contracts/multi-call-entrypoint';
import { getCanonicalRouter } from '@aztec/protocol-contracts/router';

import { join } from 'path';

Expand Down Expand Up @@ -54,6 +55,7 @@ export async function createPXEService(
getCanonicalFeeJuice(),
getCanonicalKeyRegistry(),
getCanonicalAuthRegistry(),
getCanonicalRouter(),
]) {
await server.registerContract(contract);
}
Expand Down

0 comments on commit cfb20f8

Please sign in to comment.