-
Notifications
You must be signed in to change notification settings - Fork 166
fix(protocol): reject asset callbacks on private accounts #3812
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
Open
onurinanc
wants to merge
5
commits into
next
Choose a base branch
from
fix-reject-callbacks-private-accounts
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
56b6250
fix(protocol): reject asset callbacks on private accounts
onurinanc 25b4a78
chore: link the PR in the asset callback changelog entry
onurinanc 906d373
docs: reference the asset callback type-level docs instead of restati…
onurinanc 3a15dc6
Merge remote-tracking branch 'origin' into fix-reject-callbacks-priva…
onurinanc f599588
fix comments
onurinanc 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,8 @@ use crate::{Felt, Word}; | |
| /// installing a callback slot, so that the account retains the ability to add a callback slot via | ||
| /// an account upgrade later. This is particularly useful if new types of callbacks are introduced. | ||
| /// | ||
| /// An account with an enabled flag must be of type [`AccountType::Public`]. | ||
| /// | ||
| /// [`AccountBuilder::with_component`] (or [`AccountBuilder::with_components`]) must be called at | ||
| /// least once, and exactly one of the added components must be an authentication component (i.e. a | ||
| /// component exporting a procedure marked with the `@auth_script` attribute). The auth component is | ||
|
|
@@ -197,6 +199,20 @@ impl AccountBuilder { | |
| AssetCallbackFlag::from(self.asset_callbacks.is_enabled() || storage.has_callback_slots()) | ||
| } | ||
|
|
||
| /// Derives the account's [`AssetCallbackFlag`] and rejects enabling it on a private account. | ||
| fn validated_asset_callbacks( | ||
| &self, | ||
| storage: &AccountStorage, | ||
| ) -> Result<AssetCallbackFlag, AccountError> { | ||
| let asset_callbacks = self.derive_asset_callbacks(storage); | ||
|
|
||
| if asset_callbacks.is_enabled() && self.account_type.is_private() { | ||
| return Err(AccountError::AssetCallbacksOnPrivateAccount); | ||
| } | ||
|
|
||
| Ok(asset_callbacks) | ||
| } | ||
|
Comment on lines
+203
to
+214
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: do we still need to keep the non-validated one, |
||
|
|
||
| /// Grinds a new [`AccountId`] using the `init_seed` as a starting point. | ||
| fn grind_account_id( | ||
| &self, | ||
|
|
@@ -235,6 +251,8 @@ impl AccountBuilder { | |
| /// - The number of [`StorageSlot`](crate::account::StorageSlot)s of all components exceeds 255. | ||
| /// - [`MastForest::merge`](miden_processor::mast::MastForest::merge) fails on the given | ||
| /// components. | ||
| /// - The account's asset callback flag is enabled while its account type is | ||
| /// [`AccountType::Private`]. | ||
| /// - If duplicate assets were added to the builder (only under the `testing` feature). | ||
| /// - If the vault is not empty on new accounts (only under the `testing` feature). | ||
| pub fn build(mut self) -> Result<Account, AccountError> { | ||
|
|
@@ -248,7 +266,7 @@ impl AccountBuilder { | |
| )); | ||
| } | ||
|
|
||
| let asset_callbacks = self.derive_asset_callbacks(&storage); | ||
| let asset_callbacks = self.validated_asset_callbacks(&storage)?; | ||
|
|
||
| let seed = self.grind_account_id( | ||
| self.init_seed, | ||
|
|
@@ -313,7 +331,7 @@ impl AccountBuilder { | |
| bytes, | ||
| AccountIdVersion::Version1, | ||
| self.account_type, | ||
| self.derive_asset_callbacks(&storage), | ||
| self.validated_asset_callbacks(&storage)?, | ||
| ) | ||
| }; | ||
|
|
||
|
|
@@ -612,6 +630,7 @@ mod tests { | |
| .into_storage_slots(), | ||
| ] { | ||
| let account = Account::builder([7; 32]) | ||
| .account_type(AccountType::Public) | ||
| .with_component(NoopAuthComponent) | ||
| .with_component(callback_component(slots)) | ||
| .build() | ||
|
|
@@ -621,6 +640,36 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| /// An enabled [`AssetCallbackFlag`] on a private account is rejected at build time. | ||
| #[test] | ||
| fn account_builder_rejects_asset_callbacks_on_private_account() { | ||
| let callback_component = AccountComponent::new( | ||
| CUSTOM_PACKAGE1.clone(), | ||
| AssetCallbacks::new() | ||
| .on_before_asset_added_to_account(Word::from([1u32, 2, 3, 4])) | ||
| .into_storage_slots(), | ||
| AccountComponentMetadata::new("test::callback_component"), | ||
| ) | ||
| .expect("component should be valid"); | ||
|
|
||
| let error = Account::builder([7; 32]) | ||
| .account_type(AccountType::Private) | ||
| .with_component(NoopAuthComponent) | ||
| .with_component(callback_component) | ||
| .build() | ||
| .expect_err("private account with a callback slot should be rejected"); | ||
| assert_matches!(error, AccountError::AssetCallbacksOnPrivateAccount); | ||
|
|
||
| let error = Account::builder([7; 32]) | ||
| .account_type(AccountType::Private) | ||
| .with_component(NoopAuthComponent) | ||
| .with_component(CustomComponent1 { slot0: 25 }) | ||
| .enable_asset_callbacks() | ||
| .build() | ||
| .expect_err("private account with enabled callbacks should be rejected"); | ||
| assert_matches!(error, AccountError::AssetCallbacksOnPrivateAccount); | ||
| } | ||
|
|
||
| /// Without an installed callback slot the flag is disabled, unless callbacks are explicitly | ||
| /// enabled to reserve the capability for the account's lifetime. | ||
| #[test] | ||
|
|
@@ -632,7 +681,11 @@ mod tests { | |
| let account = builder.clone().build().unwrap(); | ||
| assert_eq!(account.id().asset_callback_flag(), AssetCallbackFlag::Disabled); | ||
|
|
||
| let account = builder.enable_asset_callbacks().build().unwrap(); | ||
| let account = builder | ||
| .account_type(AccountType::Public) | ||
| .enable_asset_callbacks() | ||
| .build() | ||
| .unwrap(); | ||
| assert_eq!(account.id().asset_callback_flag(), AssetCallbackFlag::Enabled); | ||
| } | ||
|
|
||
|
|
||
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
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.