-
Notifications
You must be signed in to change notification settings - Fork 115
feat(AggLayer): B2AGG note consumption check
#2334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
d4ffea1
feat: put target account in attachment
mmagician 371efae
feat: extract b2agg creation to helper
mmagician 1b044d3
lint: regen error file
mmagician bfe8055
feat: use NetworkAccountTarget for attachments
mmagician b61a1f5
fix: still need LET slot
mmagician 38132c8
chore: test the target mismatch logic
mmagician 0d83087
chore: apply target checks to UPDATE_GER
mmagician d59c8a3
Apply suggestion from @mmagician
mmagician 55a8792
chore: add TODOs
mmagician ed6c582
chore: lint
mmagician 26476ac
docs(masm): trim dangling B2AGG note description
cursoragent 2217b38
docs(masm): clarify B2AGG attachment layout
cursoragent 4c742ec
feat: use network account target helper
cursoragent bbecf8c
feat: make B2AGG notes always public
cursoragent 0e20ba8
feat: mark B2AGG notes always consumable
cursoragent b262861
style: use NoteScript import for B2AGG
cursoragent 851ecea
docs: add changelog entry for PR 2334
cursoragent 3750112
Apply suggestions from code review
mmagician 8857e67
chore: move helper to network_account_target
mmagician 3b2a537
chore: note script section headers
mmagician 6f90bd9
chore: comment simplify and wrap
mmagician b0041bb
Merge branch 'agglayer-fixed-2' into mmagician-bagg-check
mmagician 9ece869
lint
mmagician 0282672
fix: use `with_account_target` instead of `0` for `NoteTag`
mmagician ec8f116
chore: add missing panic to get_id
mmagician 3c6e32f
chore: mention active note
mmagician 185cb51
docs: how active_account_matches_target_account panics
mmagician a7a3809
Revert "fix: use `with_account_target` instead of `0` for `NoteTag`"
mmagician ad7bc84
Merge branch 'agglayer-fixed-2' into mmagician-bagg-check
mmagician 495dc2b
feat: extract assert_is_network_account_target helper
mmagician f8e4fd2
feat: change is_network_account_target to return bool, not panic
mmagician aad47e5
feat: swap kind/scheme in signatures
mmagician 30d1fd0
lint: regen errors
mmagician 1b9c98b
feat: encapsulate note logic in structs
mmagician 5a09fda
chore: rename file to bagg_note
mmagician 457bcd9
chore: remove redundant comments
mmagician f9ae94d
Merge branch 'agglayer-fixed-2' into mmagician-bagg-check
mmagician 1a5a49f
Merge branch 'agglayer-fixed-2' into mmagician-bagg-check
mmagician 37e8392
Merge branch 'agglayer-fixed-2' into mmagician-bagg-check
mmagician 779a1bf
Revert "feat: swap kind/scheme in signatures"
mmagician 3603dd8
Merge branch 'agglayer-fixed-2' into mmagician-bagg-check
mmagician File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| //! Bridge Out note creation utilities. | ||
| //! | ||
| //! This module provides helpers for creating B2AGG (Bridge to AggLayer) notes, | ||
| //! which are used to bridge assets out from Miden to the AggLayer network. | ||
| use alloc::string::ToString; | ||
| use alloc::vec::Vec; | ||
|
|
||
| use miden_assembly::utils::Deserializable; | ||
| use miden_core::{Felt, Program, Word}; | ||
| use miden_protocol::account::AccountId; | ||
| use miden_protocol::crypto::rand::FeltRng; | ||
| use miden_protocol::errors::NoteError; | ||
| use miden_protocol::note::{ | ||
| Note, | ||
| NoteAssets, | ||
| NoteAttachment, | ||
| NoteExecutionHint, | ||
| NoteMetadata, | ||
| NoteRecipient, | ||
| NoteScript, | ||
| NoteStorage, | ||
| NoteType, | ||
| }; | ||
| use miden_standards::note::NetworkAccountTarget; | ||
| use miden_utils_sync::LazyLock; | ||
|
|
||
| use crate::EthAddressFormat; | ||
|
|
||
| // NOTE SCRIPT | ||
| // ================================================================================================ | ||
|
|
||
| // Initialize the B2AGG note script only once | ||
| static B2AGG_SCRIPT: LazyLock<NoteScript> = LazyLock::new(|| { | ||
| let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/B2AGG.masb")); | ||
| let program = Program::read_from_bytes(bytes).expect("Shipped B2AGG script is well-formed"); | ||
| NoteScript::new(program) | ||
| }); | ||
|
|
||
| // B2AGG NOTE | ||
| // ================================================================================================ | ||
|
|
||
| /// B2AGG (Bridge to AggLayer) note. | ||
| /// | ||
| /// This note is used to bridge assets from Miden to another network via the AggLayer. | ||
| /// When consumed by a bridge account, the assets are burned and a corresponding | ||
| /// claim can be made on the destination network. B2AGG notes are always public. | ||
| pub struct B2AggNote; | ||
|
|
||
| impl B2AggNote { | ||
| // CONSTANTS | ||
| // -------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Expected number of storage items for a B2AGG note. | ||
| pub const NUM_STORAGE_ITEMS: usize = 6; | ||
|
|
||
| // PUBLIC ACCESSORS | ||
| // -------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Returns the B2AGG (Bridge to AggLayer) note script. | ||
| pub fn script() -> NoteScript { | ||
| B2AGG_SCRIPT.clone() | ||
| } | ||
|
|
||
| /// Returns the B2AGG note script root. | ||
| pub fn script_root() -> Word { | ||
| B2AGG_SCRIPT.root() | ||
| } | ||
|
|
||
| // BUILDERS | ||
| // -------------------------------------------------------------------------------------------- | ||
|
|
||
| /// Creates a B2AGG (Bridge to AggLayer) note. | ||
| /// | ||
| /// This note is used to bridge assets from Miden to another network via the AggLayer. | ||
| /// When consumed by a bridge account, the assets are burned and a corresponding | ||
| /// claim can be made on the destination network. B2AGG notes are always public. | ||
| /// | ||
| /// # Parameters | ||
| /// - `destination_network`: The AggLayer-assigned network ID for the destination chain | ||
| /// - `destination_address`: The Ethereum address on the destination network | ||
| /// - `assets`: The assets to bridge (must be fungible assets from a network faucet) | ||
| /// - `target_account_id`: The account ID that will consume this note (bridge account) | ||
| /// - `sender_account_id`: The account ID of the note creator | ||
| /// - `rng`: Random number generator for creating the note serial number | ||
| /// | ||
| /// # Errors | ||
| /// Returns an error if note creation fails. | ||
| pub fn create<R: FeltRng>( | ||
| destination_network: u32, | ||
| destination_address: EthAddressFormat, | ||
| assets: NoteAssets, | ||
| target_account_id: AccountId, | ||
| sender_account_id: AccountId, | ||
| rng: &mut R, | ||
| ) -> Result<Note, NoteError> { | ||
| let note_storage = build_note_storage(destination_network, destination_address)?; | ||
|
|
||
| let attachment = NoteAttachment::from( | ||
| NetworkAccountTarget::new(target_account_id, NoteExecutionHint::Always) | ||
| .map_err(|e| NoteError::other(e.to_string()))?, | ||
| ); | ||
|
|
||
| let metadata = | ||
| NoteMetadata::new(sender_account_id, NoteType::Public).with_attachment(attachment); | ||
|
|
||
| let recipient = NoteRecipient::new(rng.draw_word(), Self::script(), note_storage); | ||
|
|
||
| Ok(Note::new(assets, metadata, recipient)) | ||
| } | ||
| } | ||
|
|
||
| // HELPER FUNCTIONS | ||
| // ================================================================================================ | ||
|
|
||
| /// Builds the note storage for a B2AGG note. | ||
| /// | ||
| /// The storage layout is: | ||
| /// - 1 felt: destination_network | ||
| /// - 5 felts: destination_address (20 bytes as 5 u32 values) | ||
| fn build_note_storage( | ||
| destination_network: u32, | ||
| destination_address: EthAddressFormat, | ||
| ) -> Result<NoteStorage, NoteError> { | ||
| let mut elements = Vec::with_capacity(6); | ||
|
|
||
| elements.push(Felt::new(destination_network as u64)); | ||
| elements.extend(destination_address.to_elements()); | ||
|
|
||
| NoteStorage::new(elements) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.