@@ -788,7 +788,78 @@ fn process_extend_program(
788788/// Processes a
789789/// [SetAuthorityChecked](enum.LoaderV3Instruction.html)
790790/// instruction.
791- fn process_set_authority_checked ( _program_id : & Pubkey , _accounts : & [ AccountInfo ] ) -> ProgramResult {
791+ fn process_set_authority_checked ( _program_id : & Pubkey , accounts : & [ AccountInfo ] ) -> ProgramResult {
792+ let accounts_iter = & mut accounts. iter ( ) ;
793+
794+ let buffer_or_programdata_info = next_account_info ( accounts_iter) ?;
795+ let current_authority_info = next_account_info ( accounts_iter) ?;
796+ let new_authority_info = next_account_info ( accounts_iter) ?;
797+
798+ match UpgradeableLoaderState :: deserialize ( & buffer_or_programdata_info. try_borrow_data ( ) ?) ? {
799+ UpgradeableLoaderState :: Buffer { authority_address } => {
800+ if authority_address. is_none ( ) {
801+ msg ! ( "Buffer is immutable" ) ;
802+ return Err ( ProgramError :: Immutable ) ;
803+ }
804+ if authority_address != Some ( * current_authority_info. key ) {
805+ msg ! ( "Incorrect buffer authority provided" ) ;
806+ return Err ( ProgramError :: IncorrectAuthority ) ;
807+ }
808+ if !current_authority_info. is_signer {
809+ msg ! ( "Buffer authority did not sign" ) ;
810+ return Err ( ProgramError :: MissingRequiredSignature ) ;
811+ }
812+ if !new_authority_info. is_signer {
813+ msg ! ( "New authority did not sign" ) ;
814+ return Err ( ProgramError :: MissingRequiredSignature ) ;
815+ }
816+ let mut buffer_data = buffer_or_programdata_info. try_borrow_mut_data ( ) ?;
817+ bincode:: serialize_into (
818+ & mut buffer_data[ ..] ,
819+ & UpgradeableLoaderState :: Buffer {
820+ authority_address : Some ( * new_authority_info. key ) ,
821+ } ,
822+ )
823+ . map_err ( |_| ProgramError :: InvalidAccountData ) ?;
824+ }
825+ UpgradeableLoaderState :: ProgramData {
826+ upgrade_authority_address,
827+ slot,
828+ } => {
829+ if upgrade_authority_address. is_none ( ) {
830+ msg ! ( "Program not upgradeable" ) ;
831+ return Err ( ProgramError :: Immutable ) ;
832+ }
833+ if upgrade_authority_address != Some ( * current_authority_info. key ) {
834+ msg ! ( "Incorrect upgrade authority provided" ) ;
835+ return Err ( ProgramError :: IncorrectAuthority ) ;
836+ }
837+ if !current_authority_info. is_signer {
838+ msg ! ( "Upgrade authority did not sign" ) ;
839+ return Err ( ProgramError :: MissingRequiredSignature ) ;
840+ }
841+ if !new_authority_info. is_signer {
842+ msg ! ( "New authority did not sign" ) ;
843+ return Err ( ProgramError :: MissingRequiredSignature ) ;
844+ }
845+ let mut programdata_data = buffer_or_programdata_info. try_borrow_mut_data ( ) ?;
846+ bincode:: serialize_into (
847+ & mut programdata_data[ ..] ,
848+ & UpgradeableLoaderState :: ProgramData {
849+ upgrade_authority_address : Some ( * new_authority_info. key ) ,
850+ slot,
851+ } ,
852+ )
853+ . map_err ( |_| ProgramError :: InvalidAccountData ) ?;
854+ }
855+ _ => {
856+ msg ! ( "Account does not support authorities" ) ;
857+ return Err ( ProgramError :: InvalidArgument ) ;
858+ }
859+ }
860+
861+ msg ! ( "New authority: {:?}" , new_authority_info. key) ;
862+
792863 Ok ( ( ) )
793864}
794865
0 commit comments