-
Notifications
You must be signed in to change notification settings - Fork 140
fix(standards): rework SWAP to support public payback notes #2949
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
JereSalo
wants to merge
14
commits into
next
Choose a base branch
from
jere/swap-v2
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.
+436
−120
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fc29b7c
rework SWAP to support public payback notes
JereSalo 9c57457
simplify swap.masm comments
JereSalo 9854da6
Merge remote-tracking branch 'origin/next' into jere/swap-v2
JereSalo 0f819e0
refactor(standards): hide private SWAP payback recipient
JereSalo 4571ef9
fix(standards): clippy and rustfmt in swap.rs
JereSalo 97c9a2e
docs: refresh SWAP storage description in note.md
JereSalo 1bcb235
docs(swap): explain why creator id is stored explicitly
JereSalo ae52be2
rename SWAP creator id field to payback target id
JereSalo 97858e0
docs(swap): consolidate per-mode payback docs
JereSalo 82c70d1
style(standards): rustfmt swap.rs
JereSalo c255eac
Merge remote-tracking branch 'origin/next' into jere/swap-v2
JereSalo 5b38771
test(standards): adapt dummy_target_id to new AccountId::dummy signature
JereSalo f30e3de
feat(standards): re-export SwapPayback from note module
JereSalo f638d8e
Apply suggestion from @partylikeits1983
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,58 @@ | ||
| use miden::protocol::active_note | ||
| use miden::protocol::asset | ||
| use miden::protocol::output_note | ||
| use miden::protocol::note::NOTE_TYPE_PRIVATE | ||
| use miden::standards::notes::p2id | ||
| use miden::standards::wallets::basic->wallet | ||
|
|
||
| # CONSTANTS | ||
| # ================================================================================================= | ||
|
|
||
| const SWAP_NOTE_NUM_STORAGE_ITEMS=14 | ||
|
|
||
| const SWAP_NOTE_NUM_STORAGE_ITEMS=16 | ||
|
|
||
| # Note storage layout (16 felts, loaded at STORAGE_PTR by get_storage): | ||
| # - requested_asset_key [0..3] | ||
| # - requested_asset_value [4..7] | ||
| # - payback_recipient [8..11] (private mode only; zero in public mode) | ||
| # - payback_note_type [12] | ||
| # - payback_tag [13] | ||
| # - payback_target_prefix [14] (public mode only; zero in private mode) | ||
| # - payback_target_suffix [15] (public mode only; zero in private mode) | ||
| # | ||
| # In private mode, the recipient digest and tag are precomputed off-chain by the creator and | ||
| # embedded as opaque values; the consumer of the SWAP cannot learn who the payback targets from | ||
| # storage alone. In public mode, the recipient must be reconstructible by any consumer, so the | ||
| # payback target account id is embedded in plaintext and the MASM derives the recipient at | ||
| # consume time. The payback tag is stored explicitly in both modes; the creator is responsible | ||
| # for picking a tag that targets the payback receiver in public mode. | ||
| # | ||
| # The payback target id could be derived from `active_note::get_sender` since today the target | ||
| # equals the SWAP sender, but it's stored explicitly because this slot is meant to represent an | ||
| # arbitrary payback target (not necessarily the creator) in a future iteration. | ||
| const STORAGE_PTR=0 | ||
| const REQUESTED_ASSET_PTR=0 | ||
| const PAYBACK_RECIPIENT_PTR=8 | ||
| const PAYBACK_NOTE_TYPE_PTR=12 | ||
| const PAYBACK_NOTE_TAG_PTR=13 | ||
| const PAYBACK_TAG_PTR=13 | ||
| const PAYBACK_TARGET_PREFIX_PTR=14 | ||
| const PAYBACK_TARGET_SUFFIX_PTR=15 | ||
|
|
||
| # Storage is exactly 16 felts = 4 words, so the asset region starts right after. | ||
| const ASSET_PTR=16 | ||
|
|
||
| # ERRORS | ||
| # ERRORS | ||
| # ================================================================================================= | ||
|
|
||
| const ERR_SWAP_UNEXPECTED_NUMBER_OF_STORAGE_ITEMS="SWAP script expects exactly 14 note storage items" | ||
| const ERR_SWAP_UNEXPECTED_NUMBER_OF_STORAGE_ITEMS="SWAP script expects exactly 16 note storage items" | ||
|
|
||
| const ERR_SWAP_WRONG_NUMBER_OF_ASSETS="SWAP script requires exactly 1 note asset" | ||
|
|
||
| #! Swap script: adds an asset from the note into consumers account and | ||
| #! creates a note consumable by note issuer containing requested asset. | ||
| #! Swap script: adds the offered asset from the note into the consumer's account and creates a | ||
| #! P2ID payback note addressed to the creator carrying the requested asset. | ||
| #! | ||
| #! The payback note type (selected by the creator) determines how the payback recipient is | ||
| #! derived: private uses an opaque precomputed digest, public derives it from the payback target | ||
| #! account id stored in plaintext. | ||
| #! | ||
| #! Requires that the account exposes: | ||
| #! - miden::standards::wallets::basic::receive_asset procedure. | ||
|
|
@@ -31,47 +61,67 @@ const ERR_SWAP_WRONG_NUMBER_OF_ASSETS="SWAP script requires exactly 1 note asset | |
| #! Inputs: [ARGS] | ||
| #! Outputs: [] | ||
| #! | ||
| #! Note storage is assumed to be as follows: | ||
| #! - REQUESTED_ASSET_KEY | ||
| #! - REQUESTED_ASSET_VALUE | ||
| #! - PAYBACK_RECIPIENT | ||
| #! - payback_note_type | ||
| #! - payback_note_tag | ||
| #! | ||
| #! Panics if: | ||
| #! - account does not expose miden::standards::wallets::basic::receive_asset procedure. | ||
| #! - account does not expose miden::standards::wallets::basic::move_asset_to_note procedure. | ||
| #! - account does not expose the required wallet procedures. | ||
| #! - account vault does not contain the requested asset. | ||
| #! - adding a fungible asset would result in amount overflow, i.e., the total amount would be | ||
| #! greater than 2^63. | ||
| @note_script | ||
| pub proc main | ||
| # dropping note args | ||
| # drop note args | ||
| dropw | ||
| # => [] | ||
|
|
||
| # --- create a payback note with the requested asset ---------------- | ||
|
|
||
| # store note storage into memory starting at address 0 | ||
| push.0 exec.active_note::get_storage | ||
| # store note storage into memory starting at STORAGE_PTR | ||
| push.STORAGE_PTR exec.active_note::get_storage | ||
|
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. nice thx |
||
| # => [num_storage_items] | ||
|
|
||
| # check number of storage items | ||
| eq.SWAP_NOTE_NUM_STORAGE_ITEMS assert.err=ERR_SWAP_UNEXPECTED_NUMBER_OF_STORAGE_ITEMS | ||
| # => [] | ||
|
|
||
| padw mem_loadw_le.PAYBACK_RECIPIENT_PTR | ||
| # => [PAYBACK_NOTE_RECIPIENT] | ||
| # --- create payback P2ID note | ||
|
|
||
| # load payback P2ID details | ||
| # Branch on payback note type. | ||
| mem_load.PAYBACK_NOTE_TYPE_PTR | ||
| mem_load.PAYBACK_NOTE_TAG_PTR | ||
| # => [tag, note_type, PAYBACK_NOTE_RECIPIENT] | ||
|
|
||
| # create payback P2ID note | ||
| exec.output_note::create | ||
| eq.NOTE_TYPE_PRIVATE | ||
| # => [is_private] | ||
|
|
||
| if.true | ||
| # --- PRIVATE PAYBACK --- | ||
| # Load the precomputed payback recipient (4 felts, word-aligned). | ||
| padw mem_loadw_le.PAYBACK_RECIPIENT_PTR | ||
| # => [PAYBACK_RECIPIENT] | ||
|
|
||
| mem_load.PAYBACK_NOTE_TYPE_PTR | ||
| mem_load.PAYBACK_TAG_PTR | ||
| # => [tag, note_type, PAYBACK_RECIPIENT] | ||
|
|
||
| exec.output_note::create | ||
| # => [note_idx] | ||
| else | ||
| # --- PUBLIC PAYBACK --- | ||
| # Derive P2ID serial = SWAP_SERIAL with the least significant element +1. | ||
| exec.active_note::get_serial_number | ||
| add.1 | ||
| # => [s0', s1, s2, s3] | ||
|
|
||
| mem_load.PAYBACK_NOTE_TYPE_PTR | ||
| mem_load.PAYBACK_TAG_PTR | ||
| # => [tag, note_type, s0', s1, s2, s3] | ||
|
|
||
| # Load payback target id (prefix then suffix) to match `p2id::new`'s signature | ||
| mem_load.PAYBACK_TARGET_PREFIX_PTR | ||
| mem_load.PAYBACK_TARGET_SUFFIX_PTR | ||
| # => [target_suffix, target_prefix, tag, note_type, s0', s1, s2, s3] | ||
|
|
||
| exec.p2id::new | ||
| # => [note_idx] | ||
| end | ||
| # => [note_idx] | ||
|
|
||
| # --- move requested asset into the payback P2ID note | ||
|
|
||
| padw push.0.0.0 movup.7 | ||
| # => [note_idx, pad(7)] | ||
|
|
||
|
|
@@ -85,7 +135,7 @@ pub proc main | |
| dropw dropw | ||
| # => [pad(8)] | ||
|
|
||
| # --- move assets from the SWAP note into the account ------------------------- | ||
| # --- move offered asset from the SWAP note into the consumer's account | ||
|
|
||
| # store the number of note assets to memory starting at address ASSET_PTR | ||
| push.ASSET_PTR exec.active_note::get_assets | ||
|
|
||
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.
same as below, I think this is already supported in this iteration unless I'm missing sth