Skip to content

Commit

Permalink
processor: set authority checked
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Oct 1, 2024
1 parent a6a3119 commit be8c4c3
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,78 @@ fn process_extend_program(
/// Processes a
/// [SetAuthorityChecked](enum.LoaderV3Instruction.html)
/// instruction.
fn process_set_authority_checked(_program_id: &Pubkey, _accounts: &[AccountInfo]) -> ProgramResult {
fn process_set_authority_checked(_program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult {
let accounts_iter = &mut accounts.iter();

let buffer_or_programdata_info = next_account_info(accounts_iter)?;
let current_authority_info = next_account_info(accounts_iter)?;
let new_authority_info = next_account_info(accounts_iter)?;

match UpgradeableLoaderState::deserialize(&buffer_or_programdata_info.try_borrow_data()?)? {
UpgradeableLoaderState::Buffer { authority_address } => {
if authority_address.is_none() {
msg!("Buffer is immutable");
return Err(ProgramError::Immutable);
}
if authority_address != Some(*current_authority_info.key) {
msg!("Incorrect buffer authority provided");
return Err(ProgramError::IncorrectAuthority);
}
if !current_authority_info.is_signer {
msg!("Buffer authority did not sign");
return Err(ProgramError::MissingRequiredSignature);
}
if !new_authority_info.is_signer {
msg!("New authority did not sign");
return Err(ProgramError::MissingRequiredSignature);
}
let mut buffer_data = buffer_or_programdata_info.try_borrow_mut_data()?;
bincode::serialize_into(
&mut buffer_data[..],
&UpgradeableLoaderState::Buffer {
authority_address: Some(*new_authority_info.key),
},
)
.map_err(|_| ProgramError::InvalidAccountData)?;
}
UpgradeableLoaderState::ProgramData {
upgrade_authority_address,
slot,
} => {
if upgrade_authority_address.is_none() {
msg!("Program not upgradeable");
return Err(ProgramError::Immutable);
}
if upgrade_authority_address != Some(*current_authority_info.key) {
msg!("Incorrect upgrade authority provided");
return Err(ProgramError::IncorrectAuthority);
}
if !current_authority_info.is_signer {
msg!("Upgrade authority did not sign");
return Err(ProgramError::MissingRequiredSignature);
}
if !new_authority_info.is_signer {
msg!("New authority did not sign");
return Err(ProgramError::MissingRequiredSignature);
}
let mut programdata_data = buffer_or_programdata_info.try_borrow_mut_data()?;
bincode::serialize_into(
&mut programdata_data[..],
&UpgradeableLoaderState::ProgramData {
upgrade_authority_address: Some(*new_authority_info.key),
slot,
},
)
.map_err(|_| ProgramError::InvalidAccountData)?;
}
_ => {
msg!("Account does not support authorities");
return Err(ProgramError::InvalidArgument);
}
}

msg!("New authority: {:?}", new_authority_info.key);

Ok(())
}

Expand Down

0 comments on commit be8c4c3

Please sign in to comment.