File tree Expand file tree Collapse file tree 1 file changed +30
-2
lines changed Expand file tree Collapse file tree 1 file changed +30
-2
lines changed Original file line number Diff line number Diff line change 11//! Validations for accounts.
22
33use anchor_lang:: prelude:: * ;
4+ use anchor_spl:: token:: { Mint , TokenAccount } ;
45
5- /// Validator for [Accounts] structs.
6- pub trait Validate < ' info > : Accounts < ' info > {
6+ use crate :: assert_owner;
7+
8+ /// Validates the contents of a variable. Generally used for [Accounts] structs and struct members.
9+ pub trait Validate < ' info > {
710 /// Validates the account struct.
811 fn validate ( & self ) -> ProgramResult ;
912}
13+
14+ /// Represents an account owned by a program.
15+ pub trait ProgramOwned : AccountDeserialize + Clone {
16+ /// Gets the expected owner of the program owned account.
17+ fn expected_owner ( & self ) -> & ' static Pubkey ;
18+ }
19+
20+ impl < ' info , T : ProgramOwned > Validate < ' info > for CpiAccount < ' info , T > {
21+ fn validate ( & self ) -> ProgramResult {
22+ assert_owner ! ( * self , * self . expected_owner( ) ) ;
23+ Ok ( ( ) )
24+ }
25+ }
26+
27+ impl ProgramOwned for TokenAccount {
28+ fn expected_owner ( & self ) -> & ' static Pubkey {
29+ & crate :: program_ids:: token:: ID
30+ }
31+ }
32+
33+ impl ProgramOwned for Mint {
34+ fn expected_owner ( & self ) -> & ' static Pubkey {
35+ & crate :: program_ids:: token:: ID
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments