Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
Make AuthorityType more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Aug 21, 2020
1 parent 3e4659d commit e160caf
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
52 changes: 28 additions & 24 deletions token/program2/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub enum TokenInstruction {
amount: u64,
/// Number of base 10 digits to the right of the decimal place.
decimals: u8,
/// The owner/multisignature of the mint if supply is non-zero. If present, further minting
/// is supported.
/// The mint-tokens authority/multisignature of the mint if supply is non-zero. If present,
/// further minting is supported.
owner: COption<Pubkey>,
/// The freeze authority/multisignature of the mint.
freeze_authority: COption<Pubkey>,
Expand Down Expand Up @@ -122,19 +122,19 @@ pub enum TokenInstruction {
/// 1. '[]' The source account's multisignature owner.
/// 2. ..2+M '[signer]' M signer accounts
Revoke,
/// Sets a new owner of a mint or account.
/// Sets a new authority of a mint or account.
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// 0. `[writable]` The mint or account to change the owner of.
/// 1. `[]` The new owner/delegate/multisignature.
/// 2. `[signer]` The owner of the mint or account.
/// * Single authority
/// 0. `[writable]` The mint or account to change the authority of.
/// 1. `[]` The new authority/multisignature.
/// 2. `[signer]` The current authority of the mint or account.
///
/// * Multisignature owner
/// 0. `[writable]` The mint or account to change the owner of.
/// 1. `[]` The new owner/delegate/multisignature.
/// 2. `[]` The mint's or account's multisignature owner.
/// * Multisignature authority
/// 0. `[writable]` The mint or account to change the authority of.
/// 1. `[]` The new authority/multisignature.
/// 2. `[]` The mint's or account's multisignature authority.
/// 3. ..3+M '[signer]' M signer accounts
SetAuthority {
/// The type of authority to update.
Expand All @@ -144,15 +144,15 @@ pub enum TokenInstruction {
///
/// Accounts expected by this instruction:
///
/// * Single owner
/// * Single authority
/// 0. `[writable]` The mint.
/// 1. `[writable]` The account to mint tokens to.
/// 2. `[signer]` The mint's owner.
/// 2. `[signer]` The mint's mint-tokens authority.
///
/// * Multisignature owner
/// * Multisignature authority
/// 0. `[writable]` The mint.
/// 1. `[writable]` The account to mint tokens to.
/// 2. `[]` The mint's multisignature owner.
/// 2. `[]` The mint's multisignature mint-tokens authority.
/// 3. ..3+M '[signer]' M signer accounts.
MintTo {
/// The amount of new tokens to mint.
Expand Down Expand Up @@ -303,8 +303,9 @@ impl TokenInstruction {
return Err(TokenError::InvalidInstruction.into());
}
let authority_type = match input[1] {
0 => AuthorityType::Owner,
1 => AuthorityType::Freezer,
0 => AuthorityType::MintTokens,
1 => AuthorityType::FreezeAccount,
2 => AuthorityType::AccountHolder,
_ => return Err(TokenError::InvalidInstruction.into()),
};
Self::SetAuthority { authority_type }
Expand Down Expand Up @@ -435,8 +436,9 @@ impl TokenInstruction {
output_len += size_of::<u8>();

let byte = match authority_type {
AuthorityType::Owner => 0,
AuthorityType::Freezer => 1,
AuthorityType::MintTokens => 0,
AuthorityType::FreezeAccount => 1,
AuthorityType::AccountHolder => 2,
};
output[output_len] = byte;
output_len += size_of::<u8>();
Expand Down Expand Up @@ -483,10 +485,12 @@ impl TokenInstruction {
#[repr(u8)]
#[derive(Clone, Debug, PartialEq)]
pub enum AuthorityType {
/// General authority, valid for Account and Mint
Owner,
/// Freeze authority, only valid for Mint
Freezer,
/// Authority to mint new tokens
MintTokens,
/// Authority to freeze any account associated with the Mint
FreezeAccount,
/// Holder of a given token account
AccountHolder,
}

/// Creates a 'InitializeMint' instruction.
Expand Down Expand Up @@ -885,7 +889,7 @@ mod test {
assert_eq!(unpacked, check);

let check = TokenInstruction::SetAuthority {
authority_type: AuthorityType::Freezer,
authority_type: AuthorityType::FreezeAccount,
};
let packed = check.pack().unwrap();
let expect = Vec::from([6u8, 1]);
Expand Down
39 changes: 21 additions & 18 deletions token/program2/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl Processor {
let authority_info = next_account_info(account_info_iter)?;

if account_info.data_len() == size_of::<Account>() {
if authority_type != AuthorityType::Owner {
if authority_type != AuthorityType::AccountHolder {
return Err(TokenError::AuthorityTypeNotSupported.into());
}
let mut account_data = account_info.data.borrow_mut();
Expand All @@ -276,7 +276,7 @@ impl Processor {
let mut mint: &mut Mint = state::unpack(&mut account_data)?;

match authority_type {
AuthorityType::Owner => {
AuthorityType::MintTokens => {
match mint.owner {
COption::Some(ref owner) => {
Self::validate_owner(
Expand All @@ -290,7 +290,7 @@ impl Processor {
}
mint.owner = COption::Some(*new_owner_info.key);
}
AuthorityType::Freezer => match mint.freeze_authority {
AuthorityType::FreezeAccount => match mint.freeze_authority {
COption::Some(ref freeze_authority) => {
Self::validate_owner(
program_id,
Expand All @@ -301,6 +301,9 @@ impl Processor {
}
COption::None => return Err(TokenError::MintCannotFreeze.into()),
},
_ => {
return Err(TokenError::AuthorityTypeNotSupported.into());
}
}
} else {
return Err(ProgramError::InvalidArgument);
Expand Down Expand Up @@ -1413,7 +1416,7 @@ mod tests {
&program_id,
&account_key,
&owner2_key,
AuthorityType::Owner,
AuthorityType::AccountHolder,
&owner_key,
&[]
)
Expand Down Expand Up @@ -1452,7 +1455,7 @@ mod tests {
&program_id,
&account_key,
&owner_key,
AuthorityType::Owner,
AuthorityType::AccountHolder,
&owner2_key,
&[]
)
Expand All @@ -1470,7 +1473,7 @@ mod tests {
&program_id,
&account_key,
&owner2_key,
AuthorityType::Owner,
AuthorityType::AccountHolder,
&owner_key,
&[],
)
Expand All @@ -1496,7 +1499,7 @@ mod tests {
&program_id,
&account_key,
&owner2_key,
AuthorityType::Freezer,
AuthorityType::FreezeAccount,
&owner_key,
&[],
)
Expand All @@ -1515,7 +1518,7 @@ mod tests {
&program_id,
&account_key,
&owner2_key,
AuthorityType::Owner,
AuthorityType::AccountHolder,
&owner_key,
&[],
)
Expand Down Expand Up @@ -1552,7 +1555,7 @@ mod tests {
&program_id,
&mint_key,
&owner3_key,
AuthorityType::Owner,
AuthorityType::MintTokens,
&owner2_key,
&[]
)
Expand All @@ -1566,7 +1569,7 @@ mod tests {
&program_id,
&mint_key,
&owner2_key,
AuthorityType::Owner,
AuthorityType::MintTokens,
&owner_key,
&[],
)
Expand All @@ -1588,7 +1591,7 @@ mod tests {
&program_id,
&mint_key,
&owner2_key,
AuthorityType::Freezer,
AuthorityType::FreezeAccount,
&owner_key,
&[],
)
Expand All @@ -1603,7 +1606,7 @@ mod tests {
&program_id,
&mint_key,
&owner2_key,
AuthorityType::Owner,
AuthorityType::MintTokens,
&owner_key,
&[],
)
Expand Down Expand Up @@ -1636,7 +1639,7 @@ mod tests {
&program_id,
&mint2_key,
&owner2_key,
AuthorityType::Owner,
AuthorityType::MintTokens,
&owner_key,
&[]
)
Expand Down Expand Up @@ -1667,7 +1670,7 @@ mod tests {
&program_id,
&mint3_key,
&owner2_key,
AuthorityType::Freezer,
AuthorityType::FreezeAccount,
&owner_key,
&[],
)
Expand Down Expand Up @@ -2269,7 +2272,7 @@ mod tests {
&program_id,
&mint_key,
&owner_key,
AuthorityType::Owner,
AuthorityType::MintTokens,
&multisig_key,
&[&signer_keys[0]],
)
Expand All @@ -2290,7 +2293,7 @@ mod tests {
&program_id,
&account_key,
&owner_key,
AuthorityType::Owner,
AuthorityType::AccountHolder,
&multisig_key,
&[&signer_keys[0]],
)
Expand Down Expand Up @@ -2753,7 +2756,7 @@ mod tests {
)
);

// no set owner if account is frozen
// no set authority if account is frozen
let new_owner_key = pubkey_rand();
let mut new_owner_account = SolanaAccount::default();
assert_eq!(
Expand All @@ -2763,7 +2766,7 @@ mod tests {
&program_id,
&account_key,
&new_owner_key,
AuthorityType::Owner,
AuthorityType::AccountHolder,
&owner_key,
&[]
)
Expand Down

0 comments on commit e160caf

Please sign in to comment.