From 99251ab9c2fdbfae3c92b634dc9bbe194550e206 Mon Sep 17 00:00:00 2001 From: Cryptomiester <77497858+crypt0miester@users.noreply.github.com> Date: Wed, 26 Apr 2023 18:12:40 +0700 Subject: [PATCH] Smol changes (#17) * add correct length, comments, and data checking * remove todo --- programs/gpl_nameservice/src/nameservice/ans.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/programs/gpl_nameservice/src/nameservice/ans.rs b/programs/gpl_nameservice/src/nameservice/ans.rs index 07f5fc6..2032b5f 100644 --- a/programs/gpl_nameservice/src/nameservice/ans.rs +++ b/programs/gpl_nameservice/src/nameservice/ans.rs @@ -13,12 +13,11 @@ pub struct ANSNameRecord { // If `Pubkey::default()` the data is unspecified. pub class: Pubkey, - //TODO: Check with ANS team for the right type here. pub expires_at: u64, } impl ANSNameRecord { - pub const LEN: usize = 104; + pub const LEN: usize = 200; } pub struct ANSNameService; @@ -31,8 +30,9 @@ impl NameServiceParser for ANSNameService { } fn unpack(record: &AccountInfo) -> Result { - // TODO: Check disciminator - let name_record = ANSNameRecord::try_from_slice(&mut &record.data.borrow_mut()[..])?; + // discriminators are unique to programs, since we have validations below. + // unless we hard code the discriminator value we will have to ignore them for now. + let name_record = ANSNameRecord::try_from_slice(&mut &record.data.borrow_mut()[8..])?; Ok(name_record) } @@ -57,10 +57,16 @@ impl NameServiceParser for ANSNameService { // Validate the owner Self::validate_owner(record)?; - if record.data_len() != ANSNameRecord::LEN { + // Data size may be longer than the fixed ans record name. + if ANSNameRecord::LEN >= record.data_len() { return Err(NameServiceError::InvalidDataLength.into()); } + // Check if the owner of the account is ANS program. + if record.owner.to_string() != Self::id_str() { + return Err(ProgramError::InvalidAccountData.into()); + } + let name_record = Self::unpack(record)?; // The authority should be the same as the owner in the record