signer.rs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. use crate::error::ErrorCode;
  2. use crate::*;
  3. use solana_program::account_info::AccountInfo;
  4. use solana_program::entrypoint::ProgramResult;
  5. use solana_program::instruction::AccountMeta;
  6. use solana_program::program_error::ProgramError;
  7. use solana_program::pubkey::Pubkey;
  8. use std::ops::Deref;
  9. /// Type validating that the account signed the transaction. No other ownership
  10. /// or type checks are done. If this is used, one should not try to access the
  11. /// underlying account data.
  12. #[derive(Clone)]
  13. pub struct Signer<'info> {
  14. info: AccountInfo<'info>,
  15. }
  16. impl<'info> Signer<'info> {
  17. fn new(info: AccountInfo<'info>) -> Signer<'info> {
  18. Self { info }
  19. }
  20. /// Deserializes the given `info` into a `Signer`.
  21. #[inline(never)]
  22. pub fn try_from(info: &AccountInfo<'info>) -> Result<Signer<'info>, ProgramError> {
  23. if !info.is_signer {
  24. return Err(ErrorCode::AccountNotSigner.into());
  25. }
  26. Ok(Signer::new(info.clone()))
  27. }
  28. }
  29. impl<'info> Accounts<'info> for Signer<'info> {
  30. #[inline(never)]
  31. fn try_accounts(
  32. _program_id: &Pubkey,
  33. accounts: &mut &[AccountInfo<'info>],
  34. _ix_data: &[u8],
  35. ) -> Result<Self, ProgramError> {
  36. if accounts.is_empty() {
  37. return Err(ErrorCode::AccountNotEnoughKeys.into());
  38. }
  39. let account = &accounts[0];
  40. *accounts = &accounts[1..];
  41. Signer::try_from(account)
  42. }
  43. }
  44. impl<'info> AccountsExit<'info> for Signer<'info> {
  45. fn exit(&self, _program_id: &Pubkey) -> ProgramResult {
  46. // No-op.
  47. Ok(())
  48. }
  49. }
  50. impl<'info> ToAccountMetas for Signer<'info> {
  51. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<AccountMeta> {
  52. let is_signer = is_signer.unwrap_or(self.info.is_signer);
  53. let meta = match self.info.is_writable {
  54. false => AccountMeta::new_readonly(*self.info.key, is_signer),
  55. true => AccountMeta::new(*self.info.key, is_signer),
  56. };
  57. vec![meta]
  58. }
  59. }
  60. impl<'info> ToAccountInfos<'info> for Signer<'info> {
  61. fn to_account_infos(&self) -> Vec<AccountInfo<'info>> {
  62. vec![self.info.clone()]
  63. }
  64. }
  65. impl<'info> ToAccountInfo<'info> for Signer<'info> {
  66. fn to_account_info(&self) -> AccountInfo<'info> {
  67. self.info.clone()
  68. }
  69. }
  70. impl<'info> AsRef<AccountInfo<'info>> for Signer<'info> {
  71. fn as_ref(&self) -> &AccountInfo<'info> {
  72. &self.info
  73. }
  74. }
  75. impl<'info> Deref for Signer<'info> {
  76. type Target = AccountInfo<'info>;
  77. fn deref(&self) -> &Self::Target {
  78. &self.info
  79. }
  80. }
  81. impl<'info> Key for Signer<'info> {
  82. fn key(&self) -> Pubkey {
  83. *self.info.key
  84. }
  85. }