From b102f60f3b7570e0848815dd73079033989571b9 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Fri, 10 Jan 2025 12:18:07 +0100 Subject: [PATCH] [wip]: Fix use of `Pubkey::try_from` --- program/src/instruction.rs | 4 ++-- program/src/processor/close.rs | 4 +++- program/src/processor/mod.rs | 4 ++-- program/src/processor/set_authority.rs | 2 +- program/src/processor/set_immutable.rs | 2 +- program/src/processor/withdraw_excess_lamports.rs | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/program/src/instruction.rs b/program/src/instruction.rs index 64d26e7..959cd42 100644 --- a/program/src/instruction.rs +++ b/program/src/instruction.rs @@ -94,8 +94,8 @@ pub enum ProgramMetadataInstruction { /// Withdraws excess lamports from a metadata account. /// /// ### Accounts - /// 0. `[ w ]` Account to close. - /// 1. `[ s ]` Metadata authority or buffer account. + /// 0. `[ w ]` Metadata account. + /// 1. `[ s ]` Metadata authority account. /// 2. `[ o ]` (optional) Program account. /// 3. `[ o ]` (optional) Program data account. /// 5. `[ w ]` Destination account. diff --git a/program/src/processor/close.rs b/program/src/processor/close.rs index 58b4939..ad6a8fe 100644 --- a/program/src/processor/close.rs +++ b/program/src/processor/close.rs @@ -8,6 +8,8 @@ use super::validate_authority; /// /// ## Validation /// The following validation checks are performed: +/// +/// - [implicit] The `account` to close is owned by the Program Metadata program. Implicitly checked by closing to the account. pub fn close(accounts: &[AccountInfo]) -> ProgramResult { let [account, authority, program, program_data, destination] = accounts else { return Err(ProgramError::NotEnoughAccountKeys); @@ -32,7 +34,7 @@ pub fn close(accounts: &[AccountInfo]) -> ProgramResult { } } AccountDiscriminator::Metadata => { - // Metadata and authority validation is done in the `validate_update`. + // Metadata and authority validation is done in the `validate_authority`. validate_authority(account, authority, program, program_data)? } _ => return Err(ProgramError::InvalidAccountData), diff --git a/program/src/processor/mod.rs b/program/src/processor/mod.rs index 62c88b9..ec02518 100644 --- a/program/src/processor/mod.rs +++ b/program/src/processor/mod.rs @@ -33,7 +33,7 @@ fn is_program_authority( match (data.first(), program.executable()) { (Some(2 /* program discriminator */), true) => { let offset: usize = 4 /* discriminator */; - Pubkey::try_from(data[offset..]).map_err(|_| ProgramError::InvalidAccountData)? + Pubkey::try_from(&data[offset..]).map_err(|_| ProgramError::InvalidAccountData)? } _ => { // TODO: use custom error (invalid program state) @@ -56,7 +56,7 @@ fn is_program_authority( let option_offset: usize = 4 /* discriminator */ + 8 /* slot */; if data[option_offset] == 1 { let pubkey_offset: usize = option_offset + 1 /* option */; - let authority_key = Pubkey::try_from(data[pubkey_offset..]) + let authority_key = Pubkey::try_from(&data[pubkey_offset..]) .map_err(|_| ProgramError::InvalidAccountData)?; authority == &authority_key } else { diff --git a/program/src/processor/set_authority.rs b/program/src/processor/set_authority.rs index 3396b2d..9040426 100644 --- a/program/src/processor/set_authority.rs +++ b/program/src/processor/set_authority.rs @@ -21,7 +21,7 @@ pub fn set_authority(accounts: &[AccountInfo], instruction_data: &[u8]) -> Progr .split_first() .ok_or(ProgramError::InvalidInstructionData)?; - // Accounts validation is done in the `validate_update` function. + // Accounts validation is done in the `validate_authority` function. // - metadata: program owned is implicitly checked since we are writing to // the account validate_authority(metadata, authority, program, program_data)?; diff --git a/program/src/processor/set_immutable.rs b/program/src/processor/set_immutable.rs index 478b556..93f0b2b 100644 --- a/program/src/processor/set_immutable.rs +++ b/program/src/processor/set_immutable.rs @@ -15,7 +15,7 @@ pub fn set_immutable(accounts: &[AccountInfo]) -> ProgramResult { return Err(ProgramError::NotEnoughAccountKeys); }; - // Accounts validation is done in the `validate_update` function. + // Accounts validation is done in the `validate_authority` function. validate_authority(metadata, authority, program, program_data)?; // Make the metadata account immutable. diff --git a/program/src/processor/withdraw_excess_lamports.rs b/program/src/processor/withdraw_excess_lamports.rs index 5654935..44979e0 100644 --- a/program/src/processor/withdraw_excess_lamports.rs +++ b/program/src/processor/withdraw_excess_lamports.rs @@ -15,7 +15,7 @@ pub fn withdraw_excess_lamports(accounts: &[AccountInfo]) -> ProgramResult { return Err(ProgramError::NotEnoughAccountKeys); }; - // Accounts validation is done in the `validate_update` function. + // Accounts validation is done in the `validate_authority` function. validate_authority(metadata, authority, program, program_data)?; // Withdraw the excess lamports in the account.