Skip to content

Typed note inputs DX #698

@greenhat

Description

@greenhat

With the introduction of the #[note_script] attribute macro and the P2ID note script looks like:

#[note_script]
fn run(_arg: Word) {
let inputs = note::get_inputs();
let target_account_id_prefix = inputs[0];
let target_account_id_suffix = inputs[1];
let account_id = account::get_id();
assert_eq(account_id.prefix, target_account_id_prefix);
assert_eq(account_id.suffix, target_account_id_suffix);
let assets = note::get_assets();
for asset in assets {
receive_asset(asset);
}
}

The next step up is the typed note inputs. Instead of Vec<Felt> we could return the note inputs as T given the deserialization from Vec<Felt> is implemented for T.

#[derive(Deserialize)]
struct MyNote {
    account_id: AccountId,
}


#[note_script]
fn run(_arg: Word) {
    let my_note = note::get_inputs_as::<MyNote>();
    ... or 
    let my_note: MyNote = note::get_inputs_as();
    ...
    // access my_note.account_id
    ...
}

Where AccountId implements the deserialization from Vec<Felt>. We could implement the deserialization for the Miden SDK types, but for custom types the deserialization is expected to be implemented by the user.

@bitwalker @otrho @bobbinth

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions