Skip to main content

hopper_core/accounts/
program.rs

1//! Verified executable program reference.
2//!
3//! Wraps an AccountView and validates that it is executable. Used for
4//! program accounts passed to instructions (e.g. token_program, system_program).
5
6use hopper_runtime::error::ProgramError;
7use hopper_runtime::{AccountView, Address};
8
9use crate::check;
10
11/// A verified executable program reference.
12///
13/// Construction validates `is_executable`. After construction, the
14/// executable property is proven by the type system.
15pub struct ProgramRef<'a> {
16    view: &'a AccountView,
17}
18
19impl<'a> ProgramRef<'a> {
20    /// Construct from an AccountView, validating the executable flag.
21    #[inline]
22    pub fn from_account(account: &'a AccountView) -> Result<Self, ProgramError> {
23        check::check_executable(account)?;
24        Ok(Self { view: account })
25    }
26
27    /// Construct and also verify the program's address matches expected.
28    #[inline]
29    pub fn from_account_checked(
30        account: &'a AccountView,
31        expected_key: &Address,
32    ) -> Result<Self, ProgramError> {
33        check::check_executable(account)?;
34        check::check_address(account, expected_key)?;
35        Ok(Self { view: account })
36    }
37
38    /// The program's address.
39    #[inline(always)]
40    pub fn address(&self) -> &Address {
41        self.view.address()
42    }
43
44    /// The underlying AccountView.
45    #[inline(always)]
46    pub fn to_account_view(&self) -> &'a AccountView {
47        self.view
48    }
49}