Skip to content

Commit

Permalink
note: implement note types
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto committed Mar 7, 2024
1 parent 0be5fc5 commit 285be39
Show file tree
Hide file tree
Showing 25 changed files with 404 additions and 108 deletions.
2 changes: 1 addition & 1 deletion docs/src/network/miden-clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ The purpose of this method is to create a note which sends a single asset to the
```
send_asset(asset, recipient)
self.remove_asset(asset)
tx.create_note(recipient, asset)
tx.create_network_note(recipient, asset)
end
```

Expand Down
68 changes: 65 additions & 3 deletions miden-lib/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const.ACCOUNT_VAULT_ADD_ASSET_EVENT=131072
# Event emitted to signal that an asset is being removed from the account vault.
const.ACCOUNT_VAULT_REMOVE_ASSET_EVENT=131073

# CONSTANTS
# =================================================================================================

# Constants for different note types
const.OFFCHAIN_NOTE=0
const.ENCRYPTED_NOTE=1
const.LOCAL_NOTE=2
const.NETWORK_NOTE=3

# AUTHENTICATION
# =================================================================================================

Expand Down Expand Up @@ -419,7 +428,9 @@ export.get_output_notes_hash
# => [COM]
end

#! Creates a new note and returns a pointer to the memory address at which the note is stored.
#! Creates a new offchain note and returns a pointer to the memory address at which the note is stored.
#!
#! Offchain notes have only their hash published to the network.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Outputs: [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
Expand All @@ -428,12 +439,63 @@ end
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_note
export.create_offchain_note
# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [ASSET, tag, RECIPIENT]

# create the note
push.OFFCHAIN_NOTE
# => [note_type, ASSET, tag, RECIPIENT]

# create the offchain note
exec.tx::create_note
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
end

#! Creates a new local note and returns a pointer to the memory address at which the note is stored.
#!
#! Local notes have their contents published to the network, but are not consumed by the network nodes.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Outputs: [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
#!
#! ASSET is the asset to be included in the note.
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_local_note
# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [ASSET, tag, RECIPIENT]

push.LOCAL_NOTE
# => [note_type, ASSET, tag, RECIPIENT]

# create the local note
exec.tx::create_note
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
end

#! Creates a new network note and returns a pointer to the memory address at which the note is stored.
#!
#! Network notes have their contents published to the network and are consumed by the network nodes.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Outputs: [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
#!
#! ASSET is the asset to be included in the note.
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_network_note
# authenticate that the procedure invocation originates from the account context
exec.authenticate_account_origin
# => [ASSET, tag, RECIPIENT]

push.NETWORK_NOTE
# => [note_type, ASSET, tag, RECIPIENT]

# create the network note
exec.tx::create_note
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
end
Expand Down
2 changes: 1 addition & 1 deletion miden-lib/asm/miden/contracts/faucets/basic_fungible.masm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export.distribute
# => [ASSET, tag, RECIPIENT, ...]

# create a note containing the asset
exec.tx::create_note
exec.tx::create_local_note
# => [note_ptr, ZERO, ZERO, ...]
end

Expand Down
3 changes: 1 addition & 2 deletions miden-lib/asm/miden/contracts/wallets/basic.masm
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export.send_asset.1
push.0 swap loc_store.0 padw push.0.0.0 swapdw loc_load.0
# => [ASSET, tag, RECIPIENT, ZERO, ZERO, ...]


exec.tx::create_note
exec.tx::create_local_note
# => [note_ptr, ZERO, ZERO, ...]
end
16 changes: 10 additions & 6 deletions miden-lib/asm/miden/kernels/tx/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,33 @@ end

#! Creates a new note and returns a pointer to the memory address at which the note is stored.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Inputs: [note_type, ASSET, tag, RECIPIENT]
#! Outputs: [ptr, 0, 0, 0, 0, 0, 0, 0, 0]
#!
#! ASSET is the asset to be included in the note.
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_note
# Move the note type down the stack
movdn.6
# => [ASSET, tag, note_type, RECIPIENT]

# validate the asset
exec.asset::validate_asset
# => [ASSET, tag, RECIPIENT]
# => [ASSET, tag, note_type, RECIPIENT]

# get the index for the next note to be created and increment counter
exec.increment_num_created_notes
# => [note_idx, ASSET, tag, RECIPIENT]
# => [note_idx, ASSET, tag, note_type, RECIPIENT]

# get a pointer to the memory address at which the note will be stored
exec.memory::get_created_note_ptr
# => [note_ptr, ASSET, tag, RECIPIENT]
# => [note_ptr, ASSET, tag, note_type, RECIPIENT]

# populate the metadata
movup.5 exec.account::get_id push.0.0
# => [0, 0, acct_id, tag, note_ptr, ASSET, RECIPIENT]
movup.5 exec.account::get_id movup.8 push.0
# => [0, note_type, acct_id, tag, note_ptr, ASSET, RECIPIENT]

# set the metadata for the new created note
dup.4 exec.memory::set_created_note_metadata
Expand Down
46 changes: 43 additions & 3 deletions miden-lib/asm/miden/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,47 @@ export.get_output_notes_hash
# => [COM]
end

#! Creates a new note and returns a pointer to the memory address at which the note is stored.
#! Creates a new offchain note and returns a pointer to the memory address at which the note is stored.
#!
#! Offchain notes have only their hash published to the network.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Outputs: [ptr]
#!
#! ASSET is the asset to be included in the note.
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_offchain_note
syscall.create_offchain_note
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]

movdn.8 dropw dropw
# => [ptr]
end

#! Creates a new local note and returns a pointer to the memory address at which the note is stored.
#!
#! Local notes have their contents published to the network, but are not consumed by the network nodes.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Outputs: [ptr]
#!
#! ASSET is the asset to be included in the note.
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_local_note
syscall.create_local_note
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]

movdn.8 dropw dropw
# => [ptr]
end

#! Creates a new network note and returns a pointer to the memory address at which the note is stored.
#!
#! Network notes have their contents published to the network and are consumed by the network nodes.
#!
#! Inputs: [ASSET, tag, RECIPIENT]
#! Outputs: [ptr]
Expand All @@ -65,8 +105,8 @@ end
#! tag is the tag to be included in the note.
#! RECIPIENT is the recipient of the note.
#! ptr is the pointer to the memory address at which the note is stored.
export.create_note
syscall.create_note
export.create_network_note
syscall.create_network_note
# => [ptr, 0, 0, 0, 0, 0, 0, 0, 0]

movdn.8 dropw dropw
Expand Down
23 changes: 19 additions & 4 deletions miden-lib/src/notes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use miden_objects::{
accounts::AccountId, assets::Asset, crypto::rand::FeltRng, notes::Note, utils::collections::*,
accounts::AccountId,
assets::Asset,
crypto::rand::FeltRng,
notes::{Note, NoteType},
utils::collections::*,
Felt, NoteError, Word,
};

Expand All @@ -24,6 +28,7 @@ pub fn create_p2id_note<R: FeltRng>(
sender: AccountId,
target: AccountId,
assets: Vec<Asset>,
note_type: NoteType,
mut rng: R,
) -> Result<Note, NoteError> {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/P2ID.masb"));
Expand All @@ -33,7 +38,7 @@ pub fn create_p2id_note<R: FeltRng>(
let tag: Felt = target.into();
let serial_num = rng.draw_word();

Note::new(note_script, &inputs, &assets, serial_num, sender, tag)
Note::new(note_script, &inputs, &assets, serial_num, sender, tag, note_type)
}

/// Generates a P2IDR note - pay to id with recall after a certain block height.
Expand All @@ -52,6 +57,7 @@ pub fn create_p2idr_note<R: FeltRng>(
sender: AccountId,
target: AccountId,
assets: Vec<Asset>,
note_type: NoteType,
recall_height: u32,
mut rng: R,
) -> Result<Note, NoteError> {
Expand All @@ -62,7 +68,7 @@ pub fn create_p2idr_note<R: FeltRng>(
let tag: Felt = target.into();
let serial_num = rng.draw_word();

Note::new(note_script.clone(), &inputs, &assets, serial_num, sender, tag)
Note::new(note_script.clone(), &inputs, &assets, serial_num, sender, tag, note_type)
}

/// Generates a SWAP note - swap of assets between two accounts.
Expand All @@ -77,6 +83,7 @@ pub fn create_swap_note<R: FeltRng>(
sender: AccountId,
offered_asset: Asset,
requested_asset: Asset,
note_type: NoteType,
mut rng: R,
) -> Result<(Note, Word), NoteError> {
let bytes = include_bytes!(concat!(env!("OUT_DIR"), "/assets/note_scripts/SWAP.masb"));
Expand All @@ -101,7 +108,15 @@ pub fn create_swap_note<R: FeltRng>(
let tag: Felt = Felt::new(0);
let serial_num = rng.draw_word();

let note = Note::new(note_script.clone(), &inputs, &[offered_asset], serial_num, sender, tag)?;
let note = Note::new(
note_script.clone(),
&inputs,
&[offered_asset],
serial_num,
sender,
tag,
note_type,
)?;

Ok((note, payback_serial_num))
}
Loading

0 comments on commit 285be39

Please sign in to comment.