Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support array of U8 in seeds #6

Merged
merged 4 commits into from Aug 11, 2022
Merged

Support array of U8 in seeds #6

merged 4 commits into from Aug 11, 2022

Conversation

mcintyre94
Copy link
Contributor

Anchor supports a (reference to an) array of U8 values as a seed value. Using this allows the client-side code to be more straightforward than having each value as its own seed, since we can pack the array into a single buffer: Buffer.from([x, y]).

This PR adds support for this to Seahorse, such that this becomes valid:

@instruction
def create_pixel(
  pixel: Empty[Pixel],
  user: Signer,
  pos_x: u8,
  pos_y: u8
):
  # Initialise pixel account
  pixel_account = pixel.init(
    payer = user,
    seeds = ["pixel", [pos_x, pos_y]]
  )

The generated accounts context has seeds:

#[derive(Accounts)]
# [instruction (pos_x : u8 , pos_y : u8)]
pub struct CreatePixel<'info> {
    #[account(
        init,
        payer = user,
        seeds = ["pixel".as_bytes().as_ref(), [pos_x, pos_y].as_ref()],
        bump,
        space = 8 + std::mem::size_of::<Pixel>()
    )]
    pub pixel: Box<Account<'info, Pixel>>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

The PDA can then be generated in JS:

const [pixelPublicKey] = web3.PublicKey.findProgramAddressSync(
  [Buffer.from("pixel"), Buffer.from([x, y])],
  program.programId,
)

From experimenting with Anchor I think only an array of U8 needs to be supported. A string/pubkey/longer number would take multiple bytes and then you'd have a nested list which Anchor doesn't seem to like. So I think it's only Array of U8 that should be supported.

Closes #4

@ameliatastic
Copy link
Owner

hey this looks great!

small nit - can you change it to just use the SeedsUnsupportedType for the error instead of introducing a new error type specifically for arrays? after all, arrays are just another type haha.

@mcintyre94
Copy link
Contributor Author

hey this looks great!

small nit - can you change it to just use the SeedsUnsupportedType for the error instead of introducing a new error type specifically for arrays? after all, arrays are just another type haha.

Yep sure, updated it! I refactored a little bit to make the outer ty available in that error message, so you get eg. Hint: initializer/signer seeds may be other accounts, strings, integers and arrays of U8. Found: Array(String, Any) if you try to use an array of the wrong type.

@ameliatastic ameliatastic merged commit 2e43024 into ameliatastic:main Aug 11, 2022
@ameliatastic
Copy link
Owner

looks good! this will be included in the next patch :) thanks for contributing!

@ameliatastic ameliatastic deleted the seeds-lists branch August 11, 2022 21:54
acheroncrypto pushed a commit to acheroncrypto/seahorse-lang that referenced this pull request Dec 30, 2023
…adme-explainer

chore: add readme explainer for migration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to use lists in seeds
2 participants