sdk: Check owner when verifying nonce accounts

This commit is contained in:
Trent Nelson 2020-11-25 17:03:43 -07:00 committed by mergify[bot]
parent be7760caa1
commit 274312ebb5

View File

@ -20,6 +20,9 @@ pub fn create_account(lamports: u64) -> RefCell<Account> {
}
pub fn verify_nonce_account(acc: &Account, hash: &Hash) -> bool {
if acc.owner != crate::system_program::id() {
return false;
}
match StateMut::<Versions>::state(acc).map(|v| v.convert_to_current()) {
Ok(State::Initialized(ref data)) => *hash == data.blockhash,
_ => false,
@ -35,3 +38,23 @@ pub fn fee_calculator_of(account: &Account) -> Option<FeeCalculator> {
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pubkey::Pubkey;
#[test]
fn test_verify_bad_account_owner_fails() {
let program_id = Pubkey::new_unique();
assert_ne!(program_id, crate::system_program::id());
let account = Account::new_data_with_space(
42,
&Versions::new_current(State::Uninitialized),
State::size(),
&program_id,
)
.expect("nonce_account");
assert!(!verify_nonce_account(&account, &Hash::default()));
}
}