@@ -57,8 +57,9 @@ pub fn validate_config<'a, 'b>(
57
57
require_writable : bool ,
58
58
) -> Result < & ' b AccountInfo < ' a > , ProgramError > {
59
59
let config = account. ok_or ( ProgramError :: NotEnoughAccountKeys ) ?;
60
- let config_pda = Pubkey :: create_program_address ( & [ CONFIG_SEED . as_bytes ( ) , & [ bump] ] , program_id)
61
- . map_err ( |_| ProgramError :: InvalidInstructionData ) ?;
60
+ let ( config_pda, expected_bump) =
61
+ Pubkey :: find_program_address ( & [ CONFIG_SEED . as_bytes ( ) ] , program_id) ;
62
+ ensure ! ( ProgramError :: InvalidInstructionData , bump == expected_bump) ;
62
63
ensure ! (
63
64
ProgramError :: InvalidArgument ,
64
65
pubkey_eq( config. key, & config_pda)
@@ -85,14 +86,17 @@ pub fn validate_authority<'a, 'b>(
85
86
Ok ( authority)
86
87
}
87
88
88
- pub fn validate_publisher_config < ' a , ' b > (
89
+ pub fn validate_publisher_config_for_access < ' a , ' b > (
89
90
account : Option < & ' b AccountInfo < ' a > > ,
90
91
bump : u8 ,
91
92
publisher : & Pubkey ,
92
93
program_id : & Pubkey ,
93
- require_writable : bool ,
94
94
) -> Result < & ' b AccountInfo < ' a > , ProgramError > {
95
95
let publisher_config = account. ok_or ( ProgramError :: NotEnoughAccountKeys ) ?;
96
+ // We use `create_program_address` to make the `submit_prices` instruction cheaper.
97
+ // `find_program_address` is used in `initialize_publisher`, so we'll always have
98
+ // only one publisher config per publisher. As long as we check the publisher key
99
+ // stored in the account in `submit_prices`, it should be safe.
96
100
let publisher_config_pda = Pubkey :: create_program_address (
97
101
& [
98
102
PUBLISHER_CONFIG_SEED . as_bytes ( ) ,
@@ -102,9 +106,28 @@ pub fn validate_publisher_config<'a, 'b>(
102
106
program_id,
103
107
)
104
108
. map_err ( |_| ProgramError :: InvalidInstructionData ) ?;
105
- if require_writable {
106
- ensure ! ( ProgramError :: InvalidArgument , publisher_config. is_writable) ;
107
- }
109
+ ensure ! (
110
+ ProgramError :: MissingRequiredSignature ,
111
+ pubkey_eq( publisher_config. key, & publisher_config_pda)
112
+ ) ;
113
+ Ok ( publisher_config)
114
+ }
115
+
116
+ pub fn validate_publisher_config_for_init < ' a , ' b > (
117
+ account : Option < & ' b AccountInfo < ' a > > ,
118
+ bump : u8 ,
119
+ publisher : & Pubkey ,
120
+ program_id : & Pubkey ,
121
+ ) -> Result < & ' b AccountInfo < ' a > , ProgramError > {
122
+ let publisher_config = account. ok_or ( ProgramError :: NotEnoughAccountKeys ) ?;
123
+ // We use `find_program_address` to guarantee that only one publisher_config
124
+ // is created per publisher.
125
+ let ( publisher_config_pda, expected_bump) = Pubkey :: find_program_address (
126
+ & [ PUBLISHER_CONFIG_SEED . as_bytes ( ) , & publisher. to_bytes ( ) ] ,
127
+ program_id,
128
+ ) ;
129
+ ensure ! ( ProgramError :: InvalidInstructionData , bump == expected_bump) ;
130
+ ensure ! ( ProgramError :: InvalidArgument , publisher_config. is_writable) ;
108
131
ensure ! (
109
132
ProgramError :: MissingRequiredSignature ,
110
133
pubkey_eq( publisher_config. key, & publisher_config_pda)
0 commit comments