From c34020df8ac1edb4ad22e2dc2ef7634ba1a6b471 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Mei=C3=9Fner?= Date: Wed, 26 Jan 2022 17:10:23 +0100 Subject: [PATCH] Exposes is_signer() and is_writable() in InstructionContext. --- sdk/src/transaction_context.rs | 42 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/sdk/src/transaction_context.rs b/sdk/src/transaction_context.rs index 8d29989f91426c..46821f48566ceb 100644 --- a/sdk/src/transaction_context.rs +++ b/sdk/src/transaction_context.rs @@ -347,6 +347,30 @@ impl InstructionContext { ) } + /// Returns whether an account is a signer + pub fn is_signer(&self, index_in_instruction: usize) -> bool { + if index_in_instruction < self.program_accounts.len() { + false + } else { + self.instruction_accounts[ + index_in_instruction + .saturating_sub(self.program_accounts.len())] + .is_signer + } + } + + /// Returns whether an account is writable + pub fn is_writable(&self, index_in_instruction: usize) -> bool { + if index_in_instruction < self.program_accounts.len() { + false + } else { + self.instruction_accounts[ + index_in_instruction + .saturating_sub(self.program_accounts.len())] + .is_writable + } + } + /// Calculates the set of all keys of signer accounts in this Instruction pub fn get_signers(&self, transaction_context: &TransactionContext) -> HashSet { let mut result = HashSet::new(); @@ -521,25 +545,11 @@ impl<'a> BorrowedAccount<'a> { /// Returns whether this account is a signer (instruction wide) pub fn is_signer(&self) -> bool { - if self.index_in_instruction < self.instruction_context.program_accounts.len() { - false - } else { - self.instruction_context.instruction_accounts[self - .index_in_instruction - .saturating_sub(self.instruction_context.program_accounts.len())] - .is_signer - } + self.instruction_context.is_signer(self.index_in_instruction) } /// Returns whether this account is writable (instruction wide) pub fn is_writable(&self) -> bool { - if self.index_in_instruction < self.instruction_context.program_accounts.len() { - false - } else { - self.instruction_context.instruction_accounts[self - .index_in_instruction - .saturating_sub(self.instruction_context.program_accounts.len())] - .is_writable - } + self.instruction_context.is_writable(self.index_in_instruction) } }