-
Notifications
You must be signed in to change notification settings - Fork 165
feat: add multisig state
#221
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
Merged
febo
merged 1 commit into
anza-xyz:febo/token2022-multisig
from
Nagaprasadvr:feat-add-multisig-state
Aug 18, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| mod account_state; | ||
| mod mint; | ||
| mod multisig; | ||
| mod token; | ||
|
|
||
| pub use account_state::*; | ||
| pub use mint::*; | ||
| pub use multisig::*; | ||
| pub use token::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| use core::mem::size_of; | ||
| use pinocchio::{ | ||
| account_info::{AccountInfo, Ref}, | ||
| program_error::ProgramError, | ||
| pubkey::Pubkey, | ||
| }; | ||
|
|
||
| use crate::{instructions::MAX_MULTISIG_SIGNERS, ID}; | ||
|
|
||
| /// Multisignature data. | ||
| #[repr(C)] | ||
| pub struct Multisig { | ||
| /// Number of signers required | ||
| m: u8, | ||
| /// Number of valid signers | ||
| n: u8, | ||
| /// Is `true` if this structure has been initialized | ||
| is_initialized: u8, | ||
| /// Signer public keys | ||
| signers: [Pubkey; MAX_MULTISIG_SIGNERS], | ||
| } | ||
|
|
||
| impl Multisig { | ||
| /// The length of the `Multisig` account data. | ||
| pub const LEN: usize = size_of::<Multisig>(); | ||
|
|
||
| /// Return a `Multisig` from the given account info. | ||
| /// | ||
| /// This method performs owner and length validation on `AccountInfo`, safe borrowing | ||
| /// the account data. | ||
| #[inline] | ||
| pub fn from_account_info(account_info: &AccountInfo) -> Result<Ref<Multisig>, ProgramError> { | ||
| if account_info.data_len() != Self::LEN { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
| if !account_info.is_owned_by(&ID) { | ||
| return Err(ProgramError::InvalidAccountOwner); | ||
| } | ||
| Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe { | ||
| Self::from_bytes_unchecked(data) | ||
| })) | ||
| } | ||
|
|
||
| /// Return a `Multisig` from the given account info. | ||
| /// | ||
| /// This method performs owner and length validation on `AccountInfo`, but does not | ||
| /// perform the borrow check. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must ensure that it is safe to borrow the account data (e.g., there are | ||
| /// no mutable borrows of the account data). | ||
| #[inline] | ||
| pub unsafe fn from_account_info_unchecked( | ||
| account_info: &AccountInfo, | ||
| ) -> Result<&Self, ProgramError> { | ||
| if account_info.data_len() != Self::LEN { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
| if account_info.owner() != &ID { | ||
| return Err(ProgramError::InvalidAccountOwner); | ||
| } | ||
| Ok(Self::from_bytes_unchecked( | ||
| account_info.borrow_data_unchecked(), | ||
| )) | ||
| } | ||
|
|
||
| /// Return a `Multisig` from the given bytes. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must ensure that `bytes` contains a valid representation of `Multisig`, and | ||
| /// it has the correct length to be interpreted as an instance of `Multisig`. | ||
| #[inline(always)] | ||
| pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { | ||
| &*(bytes.as_ptr() as *const Multisig) | ||
| } | ||
|
|
||
| /// Number of signers required to validate the `Multisig` signature. | ||
| #[inline(always)] | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub const fn required_signers(&self) -> u8 { | ||
| self.m | ||
| } | ||
|
|
||
| /// Number of signer addresses present on the `Multisig`. | ||
| #[inline(always)] | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub const fn signers_len(&self) -> usize { | ||
| self.n as usize | ||
| } | ||
|
|
||
| /// Return the signer addresses of the `Multisig`. | ||
| #[inline(always)] | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub fn signers(&self) -> &[Pubkey] { | ||
| // SAFETY: `self.signers` is an array of `Pubkey` with a fixed size of | ||
| // `MAX_MULTISIG_SIGNERS` and `n` field indicates how many of these signers | ||
| // are valid. | ||
| unsafe { self.signers.get_unchecked(..self.signers_len()) } | ||
| } | ||
|
|
||
| /// Check whether the multisig is initialized or not. | ||
| // | ||
| // It will return a boolean value indicating whether [`self.is_initialized`] | ||
| // is different than `0` or not. | ||
| #[inline(always)] | ||
| pub fn is_initialized(&self) -> bool { | ||
| self.is_initialized != 0 | ||
| } | ||
| } | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| mod account_state; | ||
| mod mint; | ||
| mod multisig; | ||
| mod token; | ||
|
|
||
| pub use account_state::*; | ||
| pub use mint::*; | ||
| pub use multisig::*; | ||
| pub use token::*; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| use core::mem::size_of; | ||
| use pinocchio::{ | ||
| account_info::{AccountInfo, Ref}, | ||
| program_error::ProgramError, | ||
| pubkey::Pubkey, | ||
| }; | ||
|
|
||
| use crate::{instructions::MAX_MULTISIG_SIGNERS, ID}; | ||
|
|
||
| /// Multisignature data. | ||
| #[repr(C)] | ||
| pub struct Multisig { | ||
| /// Number of signers required | ||
| m: u8, | ||
| /// Number of valid signers | ||
| n: u8, | ||
| /// Is `true` if this structure has been initialized | ||
| is_initialized: u8, | ||
| /// Signer public keys | ||
| signers: [Pubkey; MAX_MULTISIG_SIGNERS], | ||
| } | ||
|
|
||
| impl Multisig { | ||
| /// The length of the `Multisig` account data. | ||
| pub const LEN: usize = size_of::<Multisig>(); | ||
|
|
||
| /// Return a `Multisig` from the given account info. | ||
| /// | ||
| /// This method performs owner and length validation on `AccountInfo`, safe borrowing | ||
| /// the account data. | ||
| #[inline] | ||
| pub fn from_account_info(account_info: &AccountInfo) -> Result<Ref<Multisig>, ProgramError> { | ||
| if account_info.data_len() != Self::LEN { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
| if !account_info.is_owned_by(&ID) { | ||
| return Err(ProgramError::InvalidAccountOwner); | ||
| } | ||
| Ok(Ref::map(account_info.try_borrow_data()?, |data| unsafe { | ||
| Self::from_bytes_unchecked(data) | ||
| })) | ||
| } | ||
|
|
||
| /// Return a `Multisig` from the given account info. | ||
| /// | ||
| /// This method performs owner and length validation on `AccountInfo`, but does not | ||
| /// perform the borrow check. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must ensure that it is safe to borrow the account data (e.g., there are | ||
| /// no mutable borrows of the account data). | ||
| #[inline] | ||
| pub unsafe fn from_account_info_unchecked( | ||
| account_info: &AccountInfo, | ||
| ) -> Result<&Self, ProgramError> { | ||
| if account_info.data_len() != Self::LEN { | ||
| return Err(ProgramError::InvalidAccountData); | ||
| } | ||
| if account_info.owner() != &ID { | ||
| return Err(ProgramError::InvalidAccountOwner); | ||
| } | ||
| Ok(Self::from_bytes_unchecked( | ||
| account_info.borrow_data_unchecked(), | ||
| )) | ||
| } | ||
|
|
||
| /// Return a `Multisig` from the given bytes. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// The caller must ensure that `bytes` contains a valid representation of `Multisig`, and | ||
| /// it has the correct length to be interpreted as an instance of `Multisig`. | ||
| #[inline(always)] | ||
| pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { | ||
| &*(bytes.as_ptr() as *const Multisig) | ||
| } | ||
|
|
||
| /// Number of signers required to validate the `Multisig` signature. | ||
| #[inline(always)] | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub const fn required_signers(&self) -> u8 { | ||
| self.m | ||
| } | ||
|
|
||
| /// Number of signer addresses present on the `Multisig`. | ||
| #[inline(always)] | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub const fn signers_len(&self) -> usize { | ||
| self.n as usize | ||
| } | ||
|
|
||
| /// Return the signer addresses of the `Multisig`. | ||
| #[inline(always)] | ||
Nagaprasadvr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub fn signers(&self) -> &[Pubkey] { | ||
| // SAFETY: `self.signers` is an array of `Pubkey` with a fixed size of | ||
| // `MAX_MULTISIG_SIGNERS` and `n` field indicates how many of these signers | ||
| // are valid. | ||
| unsafe { self.signers.get_unchecked(..self.signers_len()) } | ||
| } | ||
|
|
||
| /// Check whether the multisig is initialized or not. | ||
| // | ||
| // It will return a boolean value indicating whether [`self.is_initialized`] | ||
| // is different than `0` or not. | ||
| #[inline(always)] | ||
| pub fn is_initialized(&self) -> bool { | ||
| self.is_initialized != 0 | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.