Skip to content

Instantly share code, notes, and snippets.

@nikkaroraa
Last active May 17, 2022 07:28
Show Gist options
  • Save nikkaroraa/d3bd7fcf096dea50a86d4e214f5e6cc8 to your computer and use it in GitHub Desktop.
Save nikkaroraa/d3bd7fcf096dea50a86d4e214f5e6cc8 to your computer and use it in GitHub Desktop.

On Solana, a transaction must specify all accounts required for execution. And because an untrusted client specifies those accounts, a program must responsibly validate all such accounts are what the client claims they are--in addition to any instruction specific access control the program needs to do.

For example, you could imagine easily writing a faulty token program that forgets to check if the signer of a transaction claiming to be the owner of a Token Account actually matches the owner on that account. Furthermore, imagine what might happen if the program expects a Mint account but a malicious user gives a token Account.

To address these problems, Anchor provides several types, traits, and macros. It's easiest to understand by seeing how they're used in an example, but a couple include

  • Accounts: derive macro implementing the Accounts trait (opens new window), allowing a struct to transform from the untrusted &[AccountInfo] slice given to a Solana program into a validated struct of deserialized account types.
  • #[account]: attribute macro implementing AccountSerialize (opens new window)and AccountDeserialize (opens new window), automatically prepending a unique 8 byte discriminator to the account array. The discriminator is defined by the first 8 bytes of the Sha256 hash of the account's Rust identifier--i.e., the struct type name--and ensures no account can be substituted for another.
  • Account: a wrapper type for a deserialized account implementing AccountDeserialize. Using this type within an Accounts struct will ensure the account is owned by the address defined by declare_id! where the inner account was defined.

An attribute for a data structure representing a Solana account. Most importantly, the #[account] attribute sets the owner of that data to the ID.

Generates trait implementations for the following traits:

  • AccountSerialize
  • AccountDeserialize
  • AnchorSerialize
  • AnchorDeserialize
  • Clone
  • Discriminator
  • Owner

Implements an Accounts deserializer on the given struct. Can provide further functionality through the use of attributes.

  • Instruction Attribute
  • Constraints

A data structure of validated accounts that can be deserialized from the input to a Solana program. Implementations of this trait should perform any and all requisite constraint checks on accounts to ensure the accounts maintain any invariants required for the program to run securely. In most cases, it's recommended to use the Accounts derive macro to implement this trait.

Wrapper around AccountInfo that verifies program ownership and deserializes underlying data into a Rust type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment