Skip to content

Add P2idNoteStorage and P2ideNoteStorage #2368

@PhilippGackstatter

Description

@PhilippGackstatter

As part of the parent issue #2283, we'd like to have a type that represents NoteStorage for a specific note, e.g. how we already have MintNoteStorage for MintNote. The task here is to do the same for P2idNote and P2ideNote (later also SWAP and BURN note, but we can do this separately).

For now, I think it is sufficient to have:

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct P2idNoteStorage {
    target: AccountId,
}

impl P2idNoteStorage {
    /// Consumes the storage and returns a P2ID [`NoteRecipient`] with the provided serial number.
    ///
    /// Notes created with this recipient will be P2ID notes consumable by the specified target
    /// account stored in [`P2idNoteStorage`].
    pub fn into_recipient(self, serial_num: Word) -> NoteRecipient {
        NoteRecipient::new(serial_num, StandardNote::P2ID.script(), NoteStorage::from(self))
    }
}

impl From<P2idNoteStorage> for NoteStorage {
    fn from(storage: P2idNoteStorage) -> Self {
        NoteStorage::new(vec![storage.target.suffix(), storage.target.prefix().as_felt()])
            .expect("number of storage items should be below max")
    }
}

impl TryFrom<&[Felt]> for P2idNoteStorage {
    type Error = NoteError;

    fn try_from(note_storage: &[Felt]) -> Result<Self, Self::Error> {
        if note_storage.len() != StandardNote::P2ID.expected_num_storage_items() {
            return Err(todo!());
        }

        try_read_account_id_from_storage(note_storage)
    }
}

Importantly, the layout of note storage should ideally only be defined once in From<P2idNoteStorage> for NoteStorage and then be reused from there.

The impl TryFrom<&[Felt]> should replace parse_p2id_storage and parse_p2ide_storage, respectively.

Tasks:

  • Add P2idNoteStorage and P2ideNoteStorage following the above pattern.
  • Replace parse_p2id_storage and parse_p2ide_storage.
  • Replace recipient and/or storage construction in P2id(e)::create with calls to P2id(e)Storage. I.e. we should be able to remove P2idNote::build_recipient and P2ideNote::build_recipient.
  • Check where we manually construct P2ID(E) note storage layouts and reuse P2id(e)NoteStorage instead.

Metadata

Metadata

Assignees

Labels

good first issueGood for newcomersstandardsRelated to standard note scripts or account components

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions