-
Notifications
You must be signed in to change notification settings - Fork 598
refactor: token partial notes refactor pt. 2 - bridging #9600
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
Changes from all commits
f436596
c267525
aa5567f
c254760
1170deb
47b6039
e3703dd
cd18586
db7b1ce
d8b97cd
bf93cea
4855a43
90ecd6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ use dep::aztec::macros::aztec; | |
|
|
||
| #[aztec] | ||
| contract TokenBridge { | ||
| use dep::aztec::prelude::{AztecAddress, EthAddress, PublicMutable, SharedImmutable}; | ||
| use dep::aztec::prelude::{AztecAddress, EthAddress, SharedImmutable}; | ||
|
|
||
| use dep::token_portal_content_hash_lib::{ | ||
| get_mint_private_content_hash, get_mint_public_content_hash, get_withdraw_content_hash, | ||
|
|
@@ -27,15 +27,15 @@ contract TokenBridge { | |
| // Storage structure, containing all storage, and specifying what slots they use. | ||
| #[storage] | ||
| struct Storage<Context> { | ||
| token: PublicMutable<AztecAddress, Context>, | ||
| token: SharedImmutable<AztecAddress, Context>, | ||
|
Contributor
Author
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. We now need to read this in private as well so changed PublicMutable to Shared. |
||
| portal_address: SharedImmutable<EthAddress, Context>, | ||
| } | ||
|
|
||
| // Constructs the contract. | ||
| #[public] | ||
| #[initializer] | ||
| fn constructor(token: AztecAddress, portal_address: EthAddress) { | ||
| storage.token.write(token); | ||
| storage.token.initialize(token); | ||
| storage.portal_address.initialize(portal_address); | ||
| } | ||
| // docs:end:token_bridge_storage_and_constructor | ||
|
|
@@ -65,7 +65,7 @@ contract TokenBridge { | |
| ); | ||
|
|
||
| // Mint tokens | ||
| Token::at(storage.token.read()).mint_public(to, amount).call(&mut context); | ||
| Token::at(storage.token.read_public()).mint_public(to, amount).call(&mut context); | ||
| } | ||
| // docs:end:claim_public | ||
|
|
||
|
|
@@ -84,39 +84,42 @@ contract TokenBridge { | |
| context.message_portal(storage.portal_address.read_public(), content); | ||
|
|
||
| // Burn tokens | ||
| Token::at(storage.token.read()).burn_public(context.msg_sender(), amount, nonce).call( | ||
| &mut context, | ||
| ); | ||
| Token::at(storage.token.read_public()) | ||
| .burn_public(context.msg_sender(), amount, nonce) | ||
| .call(&mut context); | ||
| } | ||
| // docs:end:exit_to_l1_public | ||
|
|
||
| // docs:start:claim_private | ||
| // Consumes a L1->L2 message and calls the token contract to mint the appropriate amount in private assets | ||
| // User needs to call token.redeem_shield() to get the private assets | ||
| // TODO(#8416): Consider creating a truly private claim flow. | ||
| /// Claims the bridged tokens and makes them accessible in private. Note that recipient's address is not revealed | ||
| /// but the amount is. Hence it's most likely possible to determine to which L1 deposit this claim corresponds to | ||
| /// (unless there are multiple pending deposits of the same amount). | ||
| /// TODO(#8416): Consider creating a truly private claim flow. | ||
| #[private] | ||
| fn claim_private( | ||
| secret_hash_for_redeeming_minted_notes: Field, // secret hash used to redeem minted notes at a later time. This enables anyone to call this function and mint tokens to a user on their behalf | ||
| recipient: AztecAddress, // recipient of the bridged tokens | ||
|
Contributor
Author
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. We are no longer redeeming "to" a TransparentNote and instead we just mint a real ValueNote to a recipient. |
||
| amount: Field, | ||
| secret_for_L1_to_L2_message_consumption: Field, // secret used to consume the L1 to L2 message | ||
| message_leaf_index: Field, | ||
| ) { | ||
| // Consume L1 to L2 message and emit nullifier | ||
| let content_hash = | ||
| get_mint_private_content_hash(secret_hash_for_redeeming_minted_notes, amount); | ||
| let content_hash = get_mint_private_content_hash(amount); | ||
| context.consume_l1_to_l2_message( | ||
| content_hash, | ||
| secret_for_L1_to_L2_message_consumption, | ||
| storage.portal_address.read_private(), | ||
| message_leaf_index, | ||
| ); | ||
|
|
||
| // Mint tokens on L2 | ||
| // `mint_private` on token is public. So we call an internal public function | ||
| // which then calls the public method on the token contract. | ||
| // Since the secret_hash is passed, no secret is leaked. | ||
| TokenBridge::at(context.this_address()) | ||
| ._call_mint_on_token(amount, secret_hash_for_redeeming_minted_notes) | ||
| .enqueue(&mut context); | ||
| // Read the token address from storage | ||
| let token_address = storage.token.read_private(); | ||
|
|
||
| // At last we mint the tokens | ||
| // docs:start:call_mint_on_token | ||
| Token::at(token_address).mint_to_private(context.msg_sender(), recipient, amount).call( | ||
| &mut context, | ||
| ); | ||
| // docs:end:call_mint_on_token | ||
| } | ||
| // docs:end:claim_private | ||
|
|
||
|
|
@@ -147,26 +150,18 @@ contract TokenBridge { | |
| #[public] | ||
| #[view] | ||
| fn get_token() -> AztecAddress { | ||
| storage.token.read() | ||
| storage.token.read_public() | ||
| } | ||
| // docs:end:get_token | ||
|
|
||
| // docs:start:call_mint_on_token | ||
| // This is a public call as we need to read from public storage. | ||
| // Also, note that user hashes their secret in private and only sends the hash in public | ||
| // meaning only user can `redeem_shield` at a later time with their secret. | ||
| #[public] | ||
| #[internal] | ||
| fn _call_mint_on_token(amount: Field, secret_hash: Field) { | ||
| Token::at(storage.token.read()).mint_private_old(amount, secret_hash).call(&mut context); | ||
| } | ||
| // docs:end:call_mint_on_token | ||
|
|
||
| // docs:start:assert_token_is_same | ||
| #[public] | ||
| #[internal] | ||
| fn _assert_token_is_same(token: AztecAddress) { | ||
| assert(storage.token.read().eq(token), "Token address is not the same as seen in storage"); | ||
| assert( | ||
| storage.token.read_public().eq(token), | ||
| "Token address is not the same as seen in storage", | ||
| ); | ||
| } | ||
| // docs:end:assert_token_is_same | ||
| } | ||
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.
This ^ was the hash in the transparent note. It's no longer there (and neither is L2 address) meaning that only the message secret "owns" the bridged tokens.