File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed
template/shank/base/program/src Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " create-solana-program " : patch
3
+ ---
4
+
5
+ Adds extra assertion helpers for shank programs
Original file line number Diff line number Diff line change @@ -4,6 +4,26 @@ use solana_program::{
4
4
pubkey:: Pubkey ,
5
5
} ;
6
6
7
+ /// Assert that the given account is owned by the given program or one of the given owners.
8
+ /// Useful for dealing with program interfaces.
9
+ pub fn assert_program_owner_either (
10
+ account_name : & str ,
11
+ account : & AccountInfo ,
12
+ owners : & [ Pubkey ] ,
13
+ ) -> ProgramResult {
14
+ if !owners. iter ( ) . any ( |owner| account. owner == owner) {
15
+ msg ! (
16
+ "Account \" {}\" [{}] must be owned by either {:?}" ,
17
+ account_name,
18
+ account. key,
19
+ owners
20
+ ) ;
21
+ Err ( CounterError :: InvalidProgramOwner . into ( ) )
22
+ } else {
23
+ Ok ( ( ) )
24
+ }
25
+ }
26
+
7
27
/// Assert that the given account is owned by the given program.
8
28
pub fn assert_program_owner (
9
29
account_name : & str ,
@@ -44,6 +64,27 @@ pub fn assert_pda(
44
64
Ok ( bump)
45
65
}
46
66
67
+ /// Assert the derivation of the seeds plus bump against the given account.
68
+ pub fn assert_pda_with_bump (
69
+ account_name : & str ,
70
+ account : & AccountInfo ,
71
+ program_id : & Pubkey ,
72
+ seeds_with_bump : & [ & [ u8 ] ] ,
73
+ ) -> ProgramResult {
74
+ let key = Pubkey :: create_program_address ( seeds_with_bump, program_id) ?;
75
+ if * account. key != key {
76
+ msg ! (
77
+ "Account \" {}\" [{}] is an invalid PDA. Expected the following valid PDA [{}]" ,
78
+ account_name,
79
+ account. key,
80
+ key,
81
+ ) ;
82
+ Err ( CounterError :: InvalidPda . into ( ) )
83
+ } else {
84
+ Ok ( ( ) )
85
+ }
86
+ }
87
+
47
88
/// Assert that the given account is empty.
48
89
pub fn assert_empty ( account_name : & str , account : & AccountInfo ) -> ProgramResult {
49
90
if !account. data_is_empty ( ) {
You can’t perform that action at this time.
0 commit comments