diff --git a/noir-projects/aztec-nr/address-note/src/address_note.nr b/noir-projects/aztec-nr/address-note/src/address_note.nr index ebfa9abf6c16..e68afdd7bf80 100644 --- a/noir-projects/aztec-nr/address-note/src/address_note.nr +++ b/noir-projects/aztec-nr/address-note/src/address_note.nr @@ -42,18 +42,6 @@ impl NoteInterface for AddressNote { GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - // docs:start:encrypted - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - // docs:end:encrypted - } } impl AddressNote { diff --git a/noir-projects/aztec-nr/aztec/src/context/private_context.nr b/noir-projects/aztec-nr/aztec/src/context/private_context.nr index b122c33cec3b..181d5d63e53f 100644 --- a/noir-projects/aztec-nr/aztec/src/context/private_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/private_context.nr @@ -351,29 +351,6 @@ impl PrivateContext { emit_encrypted_event_log(contract_address, randomness, encrypted_log, counter); } - pub fn encrypt_and_emit_note( - &mut self, - storage_slot: Field, - ovpk_m: GrumpkinPoint, - ivpk_m: GrumpkinPoint, - note: Note - ) where Note: NoteInterface, [Field; N]: LensForEncryptedLog { - let note_hash_counter = note.get_header().note_hash_counter; - let note_exists_index = find_index( - self.new_note_hashes.storage, - |n: NoteHash| n.counter == note_hash_counter - ); - assert( - note_exists_index as u32 != MAX_NEW_NOTE_HASHES_PER_CALL, "Can only emit a note log for an existing note." - ); - - let contract_address = self.this_address(); - let ovsk_app = self.request_ovsk_app(ovpk_m.hash()); - - let encrypted_log: [u8; M] = compute_encrypted_note_log(contract_address, storage_slot, ovsk_app, ovpk_m, ivpk_m, note); - self.emit_raw_note_log(note_hash_counter, encrypted_log); - } - pub fn emit_raw_note_log(&mut self, note_hash_counter: u32, encrypted_log: [u8; M]) { let counter = self.next_counter(); let len = encrypted_log.len() as Field + 4; diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr index 738edf18edb2..8c77c0d685a5 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr @@ -1,6 +1,6 @@ use crate::{ context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface}, - encrypted_logs::payload::compute_encrypted_note_log + encrypted_logs::payload::compute_encrypted_note_log, oracle::logs_traits::LensForEncryptedLog }; use dep::protocol_types::{ address::AztecAddress, grumpkin_point::GrumpkinPoint, abis::note_hash::NoteHash, @@ -9,12 +9,13 @@ use dep::protocol_types::{ fn emit_with_keys( context: &mut PrivateContext, - storage_slot: Field, note: Note, ovpk: GrumpkinPoint, ivpk: GrumpkinPoint -) where Note: NoteInterface { - let note_hash_counter = note.get_header().note_hash_counter; +) where Note: NoteInterface, [Field; N]: LensForEncryptedLog { + let note_header = note.get_header(); + let note_hash_counter = note_header.note_hash_counter; + let storage_slot = note_header.storage_slot; let note_exists_index = find_index( context.new_note_hashes.storage, @@ -32,25 +33,25 @@ fn emit_with_keys( context.emit_raw_note_log(note_hash_counter, encrypted_log); } -pub fn with_az_enc( +pub fn with_az_enc( context: &mut PrivateContext, ov: AztecAddress, iv: AztecAddress -) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission) -> () where Note: NoteInterface { +) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission) -> () where Note: NoteInterface, [Field; N]: LensForEncryptedLog { | e: NoteEmission | { let header = context.get_header(); let ovpk = header.get_ovpk_m(context, ov); let ivpk = header.get_ivpk_m(context, iv); - emit_with_keys(context, e.storage_slot, e.note, ovpk, ivpk); + emit_with_keys(context, e.note, ovpk, ivpk); } } -pub fn with_az_enc_with_keys( +pub fn with_az_enc_with_keys( context: &mut PrivateContext, ovpk: GrumpkinPoint, ivpk: GrumpkinPoint -) -> fn[(&mut PrivateContext, GrumpkinPoint, GrumpkinPoint)](NoteEmission) -> () where Note: NoteInterface { +) -> fn[(&mut PrivateContext, GrumpkinPoint, GrumpkinPoint)](NoteEmission) -> () where Note: NoteInterface, [Field; N]: LensForEncryptedLog { | e: NoteEmission | { - emit_with_keys(context, e.storage_slot, e.note, ovpk, ivpk); + emit_with_keys(context, e.note, ovpk, ivpk); } } diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr index 920ad95dd4f3..3d9c408afd77 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr @@ -70,8 +70,6 @@ mod test { fn compute_nullifier_without_context(self) -> Field {1} - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {} - fn serialize_content(self) -> [Field; ADDRESS_NOTE_LEN] { [self.address.to_field(), self.owner.to_field(), self.randomness]} fn deserialize_content(fields: [Field; ADDRESS_NOTE_LEN]) -> Self { diff --git a/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr b/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr index 0915fd23b66b..bba4c4ad97a0 100644 --- a/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr +++ b/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr @@ -3,7 +3,7 @@ use crate::context::{PrivateContext, PublicContext}; use crate::note::{ note_header::NoteHeader, note_interface::NoteInterface, utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption}, - note_emission::NoteEmission + note_emission::{NoteEmission, OuterNoteEmission} }; use crate::oracle::notes::{notify_created_note, notify_nullified_note}; @@ -35,7 +35,7 @@ pub fn create_note( context.push_new_note_hash(inner_note_hash); - NoteEmission::new(storage_slot, *note) + NoteEmission::new(*note) } pub fn create_note_hash_from_public( diff --git a/noir-projects/aztec-nr/aztec/src/note/note_emission.nr b/noir-projects/aztec-nr/aztec/src/note/note_emission.nr index 5ac684ecff12..e5a7f46cabc3 100644 --- a/noir-projects/aztec-nr/aztec/src/note/note_emission.nr +++ b/noir-projects/aztec-nr/aztec/src/note/note_emission.nr @@ -1,15 +1,43 @@ +/** + * A note emission struct containing the information required when to emit a note. + * The exact `emit` logic is passed in by the application code + */ struct NoteEmission { - storage_slot: Field, note: Note } impl NoteEmission { - pub fn new(storage_slot: Field, note: Note) -> Self { - Self { storage_slot, note } + pub fn new(note: Note) -> Self { + Self { note } } pub fn emit(self, _emit: fn[Env](Self) -> ()) { - _emit(self) + _emit(self); + } + + pub fn discard(self) {} +} + +/** + * A wrapper of the note emission + * This is the struct provided to application codes, which can be used to emit + * only when a note was actually inserted. + * It is fairly common to have cases where a function conditionally inserts, + * and this allows us to keep the same API for emission in both cases. + */ +struct OuterNoteEmission { + emission: Option>, +} + +impl OuterNoteEmission { + pub fn new(emission: Option>) -> Self { + Self { emission } + } + + pub fn emit(self, _emit: fn[Env](NoteEmission) -> ()) { + if self.emission.is_some() { + _emit(self.emission.unwrap()); + } } pub fn discard(self) {} diff --git a/noir-projects/aztec-nr/aztec/src/note/note_interface.nr b/noir-projects/aztec-nr/aztec/src/note/note_interface.nr index 03bfd1c4ff7d..c9749de15f89 100644 --- a/noir-projects/aztec-nr/aztec/src/note/note_interface.nr +++ b/noir-projects/aztec-nr/aztec/src/note/note_interface.nr @@ -7,9 +7,7 @@ trait NoteInterface { fn compute_nullifier(self, context: &mut PrivateContext) -> Field; fn compute_nullifier_without_context(self) -> Field; - - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) -> (); - + // Autogenerated by the #[aztec(note)] macro unless it is overridden by a custom implementation fn serialize_content(self) -> [Field; N]; diff --git a/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr b/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr index beb67311aee2..8a7b853c794f 100644 --- a/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr +++ b/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr @@ -47,12 +47,6 @@ impl NoteInterface for MockNote { 0 } - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - // MockNote does not support broadcasting. Since this function gets called in various places anyway we will verify - // that the dev really did not intend to broadcast by checking that zero keys were passed in. - assert(ovpk_m.is_zero() & ivpk_m.is_zero(), "MockNote does not support broadcast."); - } - fn to_be_bytes(self, storage_slot: Field) -> [u8; MOCK_NOTE_BYTES_LENGTH] { let serialized_note = self.serialize_content(); diff --git a/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr b/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr index fc4a6f30b30d..17b8ed571e15 100644 --- a/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr +++ b/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr @@ -1,6 +1,7 @@ use dep::aztec::{ context::PrivateContext, protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint}, - note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet + note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet, + encrypted_logs::encrypted_note_emission::with_az_enc }; use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote}; @@ -24,20 +25,12 @@ impl EasyPrivateUint<&mut PrivateContext> { pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) { let header = self.context.get_header(); let owner_npk_m_hash = header.get_npk_m_hash(self.context, owner); - let outgoing_viewer = header.get_ovpk_m(self.context, outgoing_viewer); - let owner_ivpk_m = header.get_ivpk_m(self.context, owner); // Creates new note for the owner. let mut addend_note = ValueNote::new(addend as Field, owner_npk_m_hash); // Insert the new note to the owner's set of notes. // docs:start:insert - self.set.insert(&mut addend_note); - addend_note.broadcast( - self.context, - self.set.storage_slot, - outgoing_viewer, - owner_ivpk_m - ); + self.set.insert(&mut addend_note).emit(with_az_enc(self.context, outgoing_viewer, owner)); // docs:end:insert } @@ -45,8 +38,6 @@ impl EasyPrivateUint<&mut PrivateContext> { pub fn sub(self, subtrahend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) { let header = self.context.get_header(); let owner_npk_m_hash = header.get_npk_m_hash(self.context, owner); - let outgoing_viewer_ovpk_m = header.get_ovpk_m(self.context, outgoing_viewer); - let owner_ivpk_m = header.get_ivpk_m(self.context, owner); // docs:start:get_notes let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend as Field); @@ -72,12 +63,6 @@ impl EasyPrivateUint<&mut PrivateContext> { // Creates change note for the owner. let result_value = minuend - subtrahend; let mut result_note = ValueNote::new(result_value as Field, owner_npk_m_hash); - self.set.insert(&mut result_note); - result_note.broadcast( - self.context, - self.set.storage_slot, - outgoing_viewer_ovpk_m, - owner_ivpk_m - ); + self.set.insert(&mut result_note).emit(with_az_enc(self.context, outgoing_viewer, owner)); } } diff --git a/noir-projects/aztec-nr/value-note/src/value_note.nr b/noir-projects/aztec-nr/value-note/src/value_note.nr index bdce09e6f9a8..d0bf0c65d047 100644 --- a/noir-projects/aztec-nr/value-note/src/value_note.nr +++ b/noir-projects/aztec-nr/value-note/src/value_note.nr @@ -45,16 +45,6 @@ impl NoteInterface for ValueNote { GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } } impl ValueNote { diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr index b157c5b32fab..389cf3278d5a 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr @@ -38,16 +38,6 @@ impl NoteInterface for Subsc GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } } impl SubscriptionNote { diff --git a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr index 844a1e367ef4..b51eaecc6684 100644 --- a/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr +++ b/noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr @@ -49,16 +49,6 @@ impl NoteInterface for CardNote { GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } } impl Serialize<3> for CardNote { diff --git a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr index b2e834d2d186..0bc2689891eb 100644 --- a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr @@ -84,16 +84,6 @@ impl NoteInterface f GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } } impl EcdsaPublicKeyNote { diff --git a/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr b/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr index 0d81a34bef83..319d1094b2f2 100644 --- a/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr @@ -9,6 +9,7 @@ contract PendingNoteHashes { use dep::aztec::protocol_types::grumpkin_point::GrumpkinPoint; use dep::aztec::protocol_types::constants::{MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NEW_NOTE_HASHES_PER_CALL}; use dep::aztec::encrypted_logs::encrypted_note_emission::{with_az_enc, with_az_enc_with_keys}; + use dep::aztec::note::note_emission::NoteEmission; #[aztec(storage)] struct Storage { @@ -355,12 +356,7 @@ contract PendingNoteHashes { let existing_note_header = good_note.get_header(); bad_note.set_header(existing_note_header); - context.encrypt_and_emit_note( - existing_note_header.storage_slot, - outgoing_viewer_ovpk_m, - owner_ivpk_m, - bad_note - ); + NoteEmission::new(bad_note).emit(with_az_enc_with_keys(&mut context, outgoing_viewer_ovpk_m, owner_ivpk_m)); } #[contract_library_method] diff --git a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr index db9b3d26f0a1..fdc55f5e3630 100644 --- a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr +++ b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr @@ -38,16 +38,6 @@ impl NoteInterface for PublicKey GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } } impl PublicKeyNote { diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr b/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr index f5569783319b..bcca5adbab6d 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr @@ -27,12 +27,6 @@ impl NoteInterface for TestNote { // This note is expected to be shared between users and for this reason can't be nullified using a secret. 0 } - - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - // TestNote does not support broadcasting. Since this function gets called in various places anyway we will verify - // that the dev really did not intend to broadcast by checking that zero keys were passed in. - assert(ovpk_m.is_zero() & ivpk_m.is_zero(), "TestNote does not support broadcast."); - } } impl TestNote { diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr index 6d1ffe37838d..ca6cfc201260 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr @@ -2,7 +2,7 @@ use dep::aztec::prelude::{AztecAddress, NoteGetterOptions, NoteViewerOptions, No use dep::aztec::{ context::{PrivateContext, UnconstrainedContext}, hash::pedersen_hash, protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, - note::{note_getter::view_notes, note_getter_options::SortOrder, note_emission::NoteEmission} + note::{note_getter::view_notes, note_getter_options::SortOrder, note_emission::OuterNoteEmission} }; use crate::types::{token_note::{TokenNote, OwnedNote}}; @@ -60,24 +60,28 @@ impl BalancesMap { self: Self, owner: AztecAddress, addend: U128 - ) -> NoteEmission where T: NoteInterface + OwnedNote { - let context = self.map.context; - let header = context.get_header(); + ) -> OuterNoteEmission where T: NoteInterface + OwnedNote { + if addend == U128::from_integer(0) { + OuterNoteEmission::new(Option::none()) + } else { + let context = self.map.context; + let header = context.get_header(); - // We fetch the nullifier public key hash from the registry / from our PXE - let owner_npk_m_hash = header.get_npk_m_hash(context, owner); - let mut addend_note = T::new(addend, owner_npk_m_hash); + // We fetch the nullifier public key hash from the registry / from our PXE + let owner_npk_m_hash = header.get_npk_m_hash(context, owner); + let mut addend_note = T::new(addend, owner_npk_m_hash); - // docs:start:insert - self.map.at(owner).insert(&mut addend_note) - // docs:end:insert + // docs:start:insert + OuterNoteEmission::new(Option::some(self.map.at(owner).insert(&mut addend_note))) + // docs:end:insert + } } pub fn sub( self: Self, owner: AztecAddress, subtrahend: U128 - ) -> NoteEmission where T: NoteInterface + OwnedNote { + ) -> OuterNoteEmission where T: NoteInterface + OwnedNote { // docs:start:get_notes let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend); let maybe_notes = self.map.at(owner).get_notes(options); diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr index f1afad92e9cf..0a65ce2d54ea 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/token_note.nr @@ -47,20 +47,6 @@ impl NoteInterface for TokenNote { GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - // We only bother inserting the note if non-empty to save funds on gas. - // TODO: (#5901) This will be changed a lot, as it should use the updated encrypted log format - if !(self.amount == U128::from_integer(0)) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } - } } impl OwnedNote for TokenNote { diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr index b59dea760c40..c93b9b18956f 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/transparent_note.nr @@ -60,10 +60,6 @@ impl NoteInterface for Transpa GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - assert(false, "TransparentNote does not support broadcast"); - } } impl TransparentNote { diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index 6f28fb6d0773..455b001c071b 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -17,7 +17,7 @@ contract Token { use dep::aztec::{ hash::compute_secret_hash, prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}, - encrypted_logs::encrypted_note_emission::with_az_enc + encrypted_logs::encrypted_note_emission::{with_az_enc, with_az_enc_with_keys} }; // docs:start:import_authwit @@ -319,9 +319,16 @@ contract Token { } // docs:end:assert_current_call_valid_authwit + // By fetching the keys here, we can avoid doing an extra read from the storage, since from_ovpk would + // be needed twice. + let header = context.get_header(); + let from_ovpk = header.get_ovpk_m(&mut context, from); + let from_ivpk = header.get_ivpk_m(&mut context, from); + let to_ivpk = header.get_ivpk_m(&mut context, to); + let amount = U128::from_integer(amount); - storage.balances.sub(from, amount).emit(with_az_enc(&mut context, from, from)); - storage.balances.add(to, amount).emit(with_az_enc(&mut context, from, to)); + storage.balances.sub(from, amount).emit(with_az_enc_with_keys(&mut context, from_ovpk, from_ivpk)); + storage.balances.add(to, amount).emit(with_az_enc_with_keys(&mut context, from_ovpk, to_ivpk)); } // docs:end:transfer @@ -369,4 +376,4 @@ contract Token { } // docs:end:balance_of_private } -// docs:end:token_all +// docs:end:token_all \ No newline at end of file diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr index 6d1ffe37838d..cec609fc5859 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr @@ -2,7 +2,10 @@ use dep::aztec::prelude::{AztecAddress, NoteGetterOptions, NoteViewerOptions, No use dep::aztec::{ context::{PrivateContext, UnconstrainedContext}, hash::pedersen_hash, protocol_types::constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, - note::{note_getter::view_notes, note_getter_options::SortOrder, note_emission::NoteEmission} + note::{ + note_getter::view_notes, note_getter_options::SortOrder, + note_emission::{NoteEmission, OuterNoteEmission} +} }; use crate::types::{token_note::{TokenNote, OwnedNote}}; @@ -60,24 +63,28 @@ impl BalancesMap { self: Self, owner: AztecAddress, addend: U128 - ) -> NoteEmission where T: NoteInterface + OwnedNote { - let context = self.map.context; - let header = context.get_header(); + ) -> OuterNoteEmission where T: NoteInterface + OwnedNote { + if addend == U128::from_integer(0) { + OuterNoteEmission::new(Option::none()) + } else { + let context = self.map.context; + let header = context.get_header(); - // We fetch the nullifier public key hash from the registry / from our PXE - let owner_npk_m_hash = header.get_npk_m_hash(context, owner); - let mut addend_note = T::new(addend, owner_npk_m_hash); + // We fetch the nullifier public key hash from the registry / from our PXE + let owner_npk_m_hash = header.get_npk_m_hash(context, owner); + let mut addend_note = T::new(addend, owner_npk_m_hash); - // docs:start:insert - self.map.at(owner).insert(&mut addend_note) - // docs:end:insert + // docs:start:insert + OuterNoteEmission::new(Option::some(self.map.at(owner).insert(&mut addend_note))) + // docs:end:insert + } } pub fn sub( self: Self, owner: AztecAddress, subtrahend: U128 - ) -> NoteEmission where T: NoteInterface + OwnedNote { + ) -> OuterNoteEmission where T: NoteInterface + OwnedNote { // docs:start:get_notes let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend); let maybe_notes = self.map.at(owner).get_notes(options); diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr index 9d4302125f9d..2a797ec83e8c 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/token_note.nr @@ -46,20 +46,6 @@ impl NoteInterface for TokenNote { GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - // Broadcasts the note as an encrypted log on L1. - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - // We only bother inserting the note if non-empty to save funds on gas. - // TODO: (#5901) This will be changed a lot, as it should use the updated encrypted log format - if !(self.amount == U128::from_integer(0)) { - context.encrypt_and_emit_note( - slot, - ovpk_m, - ivpk_m, - self, - ); - } - } } impl OwnedNote for TokenNote { diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr index b59dea760c40..c93b9b18956f 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/transparent_note.nr @@ -60,10 +60,6 @@ impl NoteInterface for Transpa GENERATOR_INDEX__NOTE_NULLIFIER as Field, ]) } - - fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - assert(false, "TransparentNote does not support broadcast"); - } } impl TransparentNote {