# Custom P2ID note script
#
# This note script asserts that the note args are exactly the same as passed
# (currently defined as {expected_note_arg_1} and {expected_note_arg_2}).
# Since the args are too big to fit in a single note arg, we provide them via advice inputs and
# address them via their commitment (noted as NOTE_ARG)
# This note script is based off of the P2ID note script because notes currently need to have
# assets, otherwise it could have been boiled down to the assert.
use miden::protocol::native_account
use miden::protocol::active_note
use miden::core::sys
use miden::core::mem
use miden::standards::wallets::basic->basic_wallet
begin
# push data from the advice map into the advice stack
adv.push_mapval
# => [NOTE_ARG]
# memory address where to write the data
push.{mem_address}
# => [target_mem_addr, NOTE_ARG_COMMITMENT]
# number of words
push.2
# => [number_of_words, target_mem_addr, NOTE_ARG_COMMITMENT]
exec.mem::pipe_preimage_to_memory
# => [target_mem_addr']
dropw
# => []
# read first word
push.{mem_address}
# => [data_mem_address]
mem_loadw_be
# => [NOTE_ARG_1]
push.{expected_note_arg_1} assert_eqw.err="First note argument didn't match expected"
# => []
# read second word
push.{mem_address_2}
# => [data_mem_address_2]
mem_loadw_be
# => [NOTE_ARG_2]
push.{expected_note_arg_2} assert_eqw.err="Second note argument didn't match expected"
# => []
# store the note inputs to memory starting at address 0
push.0 exec.active_note::get_inputs
# => [num_inputs, inputs_ptr]
# make sure the number of inputs is 2
eq.2 assert.err="P2ID script expects exactly 2 note inputs"
# => [inputs_ptr]
# read the target account id from the note inputs
mem_load
# => [target_account_id_prefix]
exec.native_account::get_id swap drop
# => [account_id_prefix, target_account_id_prefix, ...]
# ensure account_id = target_account_id, fails otherwise
assert_eq.err="P2ID's target account address and transaction address do not match"
# => [...]
exec.basic_wallet::add_assets_to_account
# => [...]
end