2

So let's say I'm creating a notes app on-chain where an user can create any amount of notes it wants.

so i've an endpoint create_note and I need to create a note PDA everytime an user calls that endpoint, how can I do that? Every example i've seen so far relies on non-unique seeds e.g. signer pub key.

*I'm using Anchor

0

1 Answer 1

3

When creating a PDA you can number to each note to give it a unique PDA address and still have the signer key to ensure that the signer can only see their own notes. This number could be a user input or you could also implement a counter in the program. Having a number value here would also make it easier to index when displaying on an FE. It would look something like this:

#[derive(Accounts)]
#[instruction(number: u64, note: String)]
pub struct CreateEntry<'info> {
    #[account(
        init,
        seeds = [&number.to_le_bytes(), owner.key().as_ref()], 
        bump, 
        payer = owner, 
        space = 8 + 32 + 4 + 8 + 4 + note.len()
    )]
    pub note: Account<'info, NoteState>,
    #[account(mut)]
    pub owner: Signer<'info>,
    pub system_program: Program<'info, System>,
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.