Skip to main content

hopper_core/accounts/
signer.rs

1//! Verified signer account wrapper.
2//!
3//! A thin wrapper proving that the account is a signer. Does not require
4//! any layout or owner validation -- just the signer flag. Used for
5//! authority/payer accounts in instruction contexts.
6
7use hopper_runtime::error::ProgramError;
8use hopper_runtime::{AccountView, Address};
9
10use super::traits::ValidateAccount;
11use crate::check;
12
13/// A verified signer account.
14///
15/// Construction validates `is_signer`. After construction, the signer
16/// property is proven by the type system.
17#[derive(Clone, Copy)]
18pub struct SignerAccount<'a> {
19    view: &'a AccountView,
20}
21
22impl<'a> SignerAccount<'a> {
23    /// Construct from an AccountView, validating the signer flag.
24    #[inline]
25    pub fn from_account(account: &'a AccountView) -> Result<Self, ProgramError> {
26        check::check_signer(account)?;
27        Ok(Self { view: account })
28    }
29
30    /// The account's address.
31    #[inline(always)]
32    pub fn address(&self) -> &Address {
33        self.view.address()
34    }
35
36    /// The underlying AccountView.
37    #[inline(always)]
38    pub fn to_account_view(&self) -> &'a AccountView {
39        self.view
40    }
41
42    /// Whether the account is also writable.
43    #[inline(always)]
44    pub fn is_writable(&self) -> bool {
45        self.view.is_writable()
46    }
47
48    /// Current lamports.
49    #[inline(always)]
50    pub fn lamports(&self) -> u64 {
51        self.view.lamports()
52    }
53}
54
55impl<'a> ValidateAccount for SignerAccount<'a> {
56    #[inline]
57    fn validate(&self) -> Result<(), ProgramError> {
58        check::check_signer(self.view)
59    }
60}