Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 45cebaf

Browse files
committed
backport #435 to v2
1 parent 2b1b9de commit 45cebaf

File tree

1 file changed

+113
-8
lines changed

1 file changed

+113
-8
lines changed

token/program/src/processor.rs

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,8 @@ impl Processor {
327327
let authority_info = next_account_info(account_info_iter)?;
328328

329329
if account_info.data_len() == Account::get_packed_len() {
330-
let mut account_data = account_info.data.borrow_mut();
331-
Account::unpack_mut(&mut account_data, &mut |account: &mut Account| {
330+
let mut account = Account::unpack(&account_info.data.borrow())?;
331+
332332
if account.is_frozen() {
333333
return Err(TokenError::AccountFrozen.into());
334334
}
@@ -362,11 +362,9 @@ impl Processor {
362362
return Err(TokenError::AuthorityTypeNotSupported.into());
363363
}
364364
}
365-
Ok(())
366-
})?;
365+
Account::pack(account, &mut account_info.data.borrow_mut())?;
367366
} else if account_info.data_len() == Mint::get_packed_len() {
368-
let mut mint_data = account_info.data.borrow_mut();
369-
Mint::unpack_mut(&mut mint_data, &mut |mint: &mut Mint| {
367+
let mut mint = Mint::unpack(&account_info.data.borrow())?;
370368
match authority_type {
371369
AuthorityType::MintTokens => {
372370
// Once a mint's supply is fixed, it cannot be undone by setting a new
@@ -400,8 +398,7 @@ impl Processor {
400398
return Err(TokenError::AuthorityTypeNotSupported.into());
401399
}
402400
}
403-
Ok(())
404-
})?;
401+
Mint::pack(mint, &mut account_info.data.borrow_mut())?;
405402
} else {
406403
return Err(ProgramError::InvalidArgument);
407404
}
@@ -2184,6 +2181,114 @@ mod tests {
21842181
.unwrap();
21852182
}
21862183

2184+
#[test]
2185+
fn test_set_authority_dups() {
2186+
let program_id = pubkey_rand();
2187+
let account1_key = pubkey_rand();
2188+
let mut account1_account = SolanaAccount::new(
2189+
account_minimum_balance(),
2190+
Account::get_packed_len(),
2191+
&program_id,
2192+
);
2193+
let account1_info: AccountInfo = (&account1_key, true, &mut account1_account).into();
2194+
let owner_key = pubkey_rand();
2195+
let mint_key = pubkey_rand();
2196+
let mut mint_account =
2197+
SolanaAccount::new(mint_minimum_balance(), Mint::get_packed_len(), &program_id);
2198+
let mint_info: AccountInfo = (&mint_key, true, &mut mint_account).into();
2199+
let rent_key = rent::id();
2200+
let mut rent_sysvar = rent_sysvar();
2201+
let rent_info: AccountInfo = (&rent_key, false, &mut rent_sysvar).into();
2202+
2203+
// create mint
2204+
do_process_instruction_dups(
2205+
initialize_mint(&program_id, &mint_key, &mint_key, Some(&mint_key), 2).unwrap(),
2206+
vec![mint_info.clone(), rent_info.clone()],
2207+
)
2208+
.unwrap();
2209+
2210+
// create account
2211+
do_process_instruction_dups(
2212+
initialize_account(&program_id, &account1_key, &mint_key, &account1_key).unwrap(),
2213+
vec![
2214+
account1_info.clone(),
2215+
mint_info.clone(),
2216+
account1_info.clone(),
2217+
rent_info.clone(),
2218+
],
2219+
)
2220+
.unwrap();
2221+
2222+
// set mint_authority when currently self
2223+
do_process_instruction_dups(
2224+
set_authority(
2225+
&program_id,
2226+
&mint_key,
2227+
Some(&owner_key),
2228+
AuthorityType::MintTokens,
2229+
&mint_key,
2230+
&[],
2231+
)
2232+
.unwrap(),
2233+
vec![mint_info.clone(), mint_info.clone()],
2234+
)
2235+
.unwrap();
2236+
2237+
// set freeze_authority when currently self
2238+
do_process_instruction_dups(
2239+
set_authority(
2240+
&program_id,
2241+
&mint_key,
2242+
Some(&owner_key),
2243+
AuthorityType::FreezeAccount,
2244+
&mint_key,
2245+
&[],
2246+
)
2247+
.unwrap(),
2248+
vec![mint_info.clone(), mint_info.clone()],
2249+
)
2250+
.unwrap();
2251+
2252+
// set account owner when currently self
2253+
do_process_instruction_dups(
2254+
set_authority(
2255+
&program_id,
2256+
&account1_key,
2257+
Some(&owner_key),
2258+
AuthorityType::AccountOwner,
2259+
&account1_key,
2260+
&[],
2261+
)
2262+
.unwrap(),
2263+
vec![account1_info.clone(), account1_info.clone()],
2264+
)
2265+
.unwrap();
2266+
2267+
// set close_authority when currently self
2268+
Account::unpack_unchecked_mut(
2269+
&mut account1_info.data.borrow_mut(),
2270+
&mut |account: &mut Account| {
2271+
account.close_authority = COption::Some(account1_key);
2272+
Ok(())
2273+
},
2274+
)
2275+
.unwrap();
2276+
2277+
do_process_instruction_dups(
2278+
set_authority(
2279+
&program_id,
2280+
&account1_key,
2281+
Some(&owner_key),
2282+
AuthorityType::CloseAccount,
2283+
&account1_key,
2284+
&[],
2285+
)
2286+
.unwrap(),
2287+
vec![account1_info.clone(), account1_info.clone()],
2288+
)
2289+
.unwrap();
2290+
}
2291+
21872292
#[test]
21882293
fn test_set_authority() {
21892294
let program_id = pubkey_rand();

0 commit comments

Comments
 (0)