Skip to content

Commit

Permalink
Rename mint owner => mint_authority
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyera Eulberg committed Aug 21, 2020
1 parent e160caf commit 6d09b9f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
37 changes: 19 additions & 18 deletions token/program2/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ pub enum TokenInstruction {
amount: u64,
/// Number of base 10 digits to the right of the decimal place.
decimals: u8,
/// The mint-tokens authority/multisignature of the mint if supply is non-zero. If present,
/// The authority/multisignature to mint tokens, if supply is non-zero. If present,
/// further minting is supported.
owner: COption<Pubkey>,
mint_authority: COption<Pubkey>,
/// The freeze authority/multisignature of the mint.
freeze_authority: COption<Pubkey>,
},
Expand Down Expand Up @@ -234,17 +234,18 @@ impl TokenInstruction {
let decimals = unsafe { *(&input[input_len] as *const u8) };
input_len += size_of::<u8>();

let owner = match input[input_len] {
let mint_authority = match input[input_len] {
0 => {
input_len += size_of::<u8>();
COption::None
}
1 => {
input_len += size_of::<u8>();
#[allow(clippy::cast_ptr_alignment)]
let owner = unsafe { *(&input[input_len] as *const u8 as *const Pubkey) };
let mint_authority =
unsafe { *(&input[input_len] as *const u8 as *const Pubkey) };
input_len += size_of::<Pubkey>();
COption::Some(owner)
COption::Some(mint_authority)
}
_ => {
return Err(TokenError::InvalidInstruction.into());
Expand All @@ -266,7 +267,7 @@ impl TokenInstruction {
};

Self::InitializeMint {
owner,
mint_authority,
freeze_authority,
amount,
decimals,
Expand Down Expand Up @@ -345,7 +346,7 @@ impl TokenInstruction {
let mut output_len = 0;
match self {
Self::InitializeMint {
owner,
mint_authority,
freeze_authority,
amount,
decimals,
Expand All @@ -362,15 +363,15 @@ impl TokenInstruction {
*value = *decimals;
output_len += size_of::<u8>();

match owner {
COption::Some(owner) => {
match mint_authority {
COption::Some(mint_authority) => {
output[output_len] = 1;
output_len += size_of::<u8>();

#[allow(clippy::cast_ptr_alignment)]
let value =
unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut Pubkey) };
*value = *owner;
*value = *mint_authority;
output_len += size_of::<Pubkey>();
}
COption::None => {
Expand Down Expand Up @@ -498,23 +499,23 @@ pub fn initialize_mint(
token_program_id: &Pubkey,
mint_pubkey: &Pubkey,
account_pubkey: Option<&Pubkey>,
owner_pubkey: Option<&Pubkey>,
freeze_pubkey: Option<&Pubkey>,
mint_authority_pubkey: Option<&Pubkey>,
freeze_authority_pubkey: Option<&Pubkey>,
amount: u64,
decimals: u8,
) -> Result<Instruction, ProgramError> {
let owner = if let Some(owner) = owner_pubkey {
COption::Some(*owner)
let mint_authority = if let Some(mint_authority) = mint_authority_pubkey {
COption::Some(*mint_authority)
} else {
COption::None
};
let freeze_authority = if let Some(freeze_authority) = freeze_pubkey {
let freeze_authority = if let Some(freeze_authority) = freeze_authority_pubkey {
COption::Some(*freeze_authority)
} else {
COption::None
};
let data = TokenInstruction::InitializeMint {
owner,
mint_authority,
freeze_authority,
amount,
decimals,
Expand Down Expand Up @@ -828,7 +829,7 @@ mod test {
let check = TokenInstruction::InitializeMint {
amount: 1,
decimals: 2,
owner: COption::None,
mint_authority: COption::None,
freeze_authority: COption::None,
};
let packed = check.pack().unwrap();
Expand All @@ -840,7 +841,7 @@ mod test {
let check = TokenInstruction::InitializeMint {
amount: 1,
decimals: 2,
owner: COption::Some(Pubkey::new(&[2u8; 32])),
mint_authority: COption::Some(Pubkey::new(&[2u8; 32])),
freeze_authority: COption::Some(Pubkey::new(&[3u8; 32])),
};
let packed = check.pack().unwrap();
Expand Down
41 changes: 26 additions & 15 deletions token/program2/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Processor {
accounts: &[AccountInfo],
amount: u64,
decimals: u8,
owner: COption<Pubkey>,
mint_authority: COption<Pubkey>,
freeze_authority: COption<Pubkey>,
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
Expand All @@ -49,11 +49,11 @@ impl Processor {
}

dest_account.amount = amount;
} else if owner.is_none() {
} else if mint_authority.is_none() {
return Err(TokenError::OwnerRequiredIfNoInitialSupply.into());
}

mint.owner = owner;
mint.mint_authority = mint_authority;
mint.decimals = decimals;
mint.is_initialized = true;
mint.freeze_authority = freeze_authority;
Expand Down Expand Up @@ -249,7 +249,7 @@ impl Processor {
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let account_info = next_account_info(account_info_iter)?;
let new_owner_info = next_account_info(account_info_iter)?;
let new_authority_info = next_account_info(account_info_iter)?;
let authority_info = next_account_info(account_info_iter)?;

if account_info.data_len() == size_of::<Account>() {
Expand All @@ -270,25 +270,25 @@ impl Processor {
account_info_iter.as_slice(),
)?;

account.owner = *new_owner_info.key;
account.owner = *new_authority_info.key;
} else if account_info.data_len() == size_of::<Mint>() {
let mut account_data = account_info.data.borrow_mut();
let mut mint: &mut Mint = state::unpack(&mut account_data)?;

match authority_type {
AuthorityType::MintTokens => {
match mint.owner {
COption::Some(ref owner) => {
match mint.mint_authority {
COption::Some(ref mint_authority) => {
Self::validate_owner(
program_id,
owner,
mint_authority,
authority_info,
account_info_iter.as_slice(),
)?;
}
COption::None => return Err(TokenError::FixedSupply.into()),
}
mint.owner = COption::Some(*new_owner_info.key);
mint.mint_authority = COption::Some(*new_authority_info.key);
}
AuthorityType::FreezeAccount => match mint.freeze_authority {
COption::Some(ref freeze_authority) => {
Expand Down Expand Up @@ -340,9 +340,14 @@ impl Processor {
let mut mint_info_data = mint_info.data.borrow_mut();
let mint: &mut Mint = state::unpack(&mut mint_info_data)?;

match mint.owner {
COption::Some(owner) => {
Self::validate_owner(program_id, &owner, owner_info, account_info_iter.as_slice())?;
match mint.mint_authority {
COption::Some(mint_authority) => {
Self::validate_owner(
program_id,
&mint_authority,
owner_info,
account_info_iter.as_slice(),
)?;
}
COption::None => {
return Err(TokenError::FixedSupply.into());
Expand Down Expand Up @@ -496,11 +501,17 @@ impl Processor {
TokenInstruction::InitializeMint {
amount,
decimals,
owner,
mint_authority,
freeze_authority,
} => {
info!("Instruction: InitializeMint");
Self::process_initialize_mint(accounts, amount, decimals, owner, freeze_authority)
Self::process_initialize_mint(
accounts,
amount,
decimals,
mint_authority,
freeze_authority,
)
}
TokenInstruction::InitializeAccount => {
info!("Instruction: InitializeAccount");
Expand Down Expand Up @@ -1252,7 +1263,7 @@ mod tests {
assert_eq!(
*mint,
Mint {
owner: COption::Some(owner_key),
mint_authority: COption::Some(owner_key),
decimals,
is_initialized: true,
freeze_authority: COption::None,
Expand Down
8 changes: 4 additions & 4 deletions token/program2/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::mem::size_of;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Mint {
/// Optional owner, used to mint new tokens. The owner may only
/// be provided during mint creation. If no owner is present then the mint
/// has a fixed supply and no further tokens may be minted.
pub owner: COption<Pubkey>,
/// Optional authority used to mint new tokens. The mint authority may only be provided during
/// mint creation. If no mint authority is present then the mint has a fixed supply and no
/// further tokens may be minted.
pub mint_authority: COption<Pubkey>,
/// Number of base 10 digits to the right of the decimal place.
pub decimals: u8,
/// Is `true` if this structure has been initialized
Expand Down

0 comments on commit 6d09b9f

Please sign in to comment.