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

Commit 81d18e5

Browse files
committed
InitializeMint's mint authority is no longer optional
1 parent 5fccbc5 commit 81d18e5

File tree

3 files changed

+19
-41
lines changed

3 files changed

+19
-41
lines changed

token/program/src/error.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ pub enum TokenError {
2222
/// The account cannot be initialized because it is already being used.
2323
#[error("AlreadyInUse")]
2424
AlreadyInUse,
25-
/// An owner is required if initial supply is zero.
26-
#[error("An owner is required if supply is zero")]
27-
OwnerRequiredIfNoInitialSupply,
2825
/// Invalid number of provided signers.
2926
#[error("Invalid number of provided signers")]
3027
InvalidNumberOfProvidedSigners,

token/program/src/instruction.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub enum TokenInstruction {
3030
InitializeMint {
3131
/// Number of base 10 digits to the right of the decimal place.
3232
decimals: u8,
33-
/// The authority/multisignature to mint tokens. If present, further minting is supported.
34-
mint_authority: COption<Pubkey>,
33+
/// The authority/multisignature to mint tokens.
34+
mint_authority: Pubkey,
3535
/// The freeze authority/multisignature of the mint.
3636
freeze_authority: COption<Pubkey>,
3737
},
@@ -226,7 +226,7 @@ impl TokenInstruction {
226226
}
227227
Ok(match input[0] {
228228
0 => {
229-
if input.len() < size_of::<u8>() + size_of::<u8>() + size_of::<bool>() {
229+
if input.len() < size_of::<u8>() + size_of::<Pubkey>() + size_of::<bool>() {
230230
return Err(TokenError::InvalidInstruction.into());
231231
}
232232
let mut input_len = 0;
@@ -235,11 +235,9 @@ impl TokenInstruction {
235235
let decimals = unsafe { *(&input[input_len] as *const u8) };
236236
input_len += size_of::<u8>();
237237

238-
let mint_authority = COption::unpack_or(
239-
input,
240-
&mut input_len,
241-
Into::<ProgramError>::into(TokenError::InvalidInstruction),
242-
)?;
238+
let mint_authority = unsafe { *(&input[input_len] as *const u8 as *const Pubkey) };
239+
input_len += size_of::<Pubkey>();
240+
243241
let freeze_authority = COption::unpack_or(
244242
input,
245243
&mut input_len,
@@ -338,7 +336,11 @@ impl TokenInstruction {
338336
*value = *decimals;
339337
output_len += size_of::<u8>();
340338

341-
mint_authority.pack(&mut output, &mut output_len);
339+
#[allow(clippy::cast_ptr_alignment)]
340+
let value = unsafe { &mut *(&mut output[output_len] as *mut u8 as *mut Pubkey) };
341+
*value = *mint_authority;
342+
output_len += size_of::<Pubkey>();
343+
342344
freeze_authority.pack(&mut output, &mut output_len);
343345
}
344346
Self::InitializeAccount => {
@@ -468,10 +470,9 @@ pub fn initialize_mint(
468470
freeze_authority_pubkey: Option<&Pubkey>,
469471
decimals: u8,
470472
) -> Result<Instruction, ProgramError> {
471-
let mint_authority = COption::Some(*mint_authority_pubkey);
472473
let freeze_authority = freeze_authority_pubkey.cloned().into();
473474
let data = TokenInstruction::InitializeMint {
474-
mint_authority,
475+
mint_authority: *mint_authority_pubkey,
475476
freeze_authority,
476477
decimals,
477478
}
@@ -806,23 +807,24 @@ mod test {
806807
fn test_instruction_packing() {
807808
let check = TokenInstruction::InitializeMint {
808809
decimals: 2,
809-
mint_authority: COption::None,
810+
mint_authority: Pubkey::new(&[1u8; 32]),
810811
freeze_authority: COption::None,
811812
};
812813
let packed = check.pack().unwrap();
813-
let expect = Vec::from([0u8, 2, 0, 0]);
814+
let mut expect = Vec::from([0u8, 2]);
815+
expect.extend_from_slice(&[1u8; 32]);
816+
expect.extend_from_slice(&[0]);
814817
assert_eq!(packed, expect);
815818
let unpacked = TokenInstruction::unpack(&expect).unwrap();
816819
assert_eq!(unpacked, check);
817820

818821
let check = TokenInstruction::InitializeMint {
819822
decimals: 2,
820-
mint_authority: COption::Some(Pubkey::new(&[2u8; 32])),
823+
mint_authority: Pubkey::new(&[2u8; 32]),
821824
freeze_authority: COption::Some(Pubkey::new(&[3u8; 32])),
822825
};
823826
let packed = check.pack().unwrap();
824827
let mut expect = vec![0u8, 2];
825-
expect.extend_from_slice(&[1]);
826828
expect.extend_from_slice(&[2u8; 32]);
827829
expect.extend_from_slice(&[1]);
828830
expect.extend_from_slice(&[3u8; 32]);

token/program/src/processor.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Processor {
2626
pub fn process_initialize_mint(
2727
accounts: &[AccountInfo],
2828
decimals: u8,
29-
mint_authority: COption<Pubkey>,
29+
mint_authority: Pubkey,
3030
freeze_authority: COption<Pubkey>,
3131
) -> ProgramResult {
3232
let account_info_iter = &mut accounts.iter();
@@ -38,11 +38,7 @@ impl Processor {
3838
return Err(TokenError::AlreadyInUse.into());
3939
}
4040

41-
if mint_authority.is_none() {
42-
return Err(TokenError::OwnerRequiredIfNoInitialSupply.into());
43-
}
44-
45-
mint.mint_authority = mint_authority;
41+
mint.mint_authority = COption::Some(mint_authority);
4642
mint.decimals = decimals;
4743
mint.is_initialized = true;
4844
mint.freeze_authority = freeze_authority;
@@ -616,9 +612,6 @@ impl PrintProgramError for TokenError {
616612
TokenError::OwnerMismatch => info!("Error: owner does not match"),
617613
TokenError::FixedSupply => info!("Error: the total supply of this token is fixed"),
618614
TokenError::AlreadyInUse => info!("Error: account or token already in use"),
619-
TokenError::OwnerRequiredIfNoInitialSupply => {
620-
info!("Error: An owner is required if supply is zero")
621-
}
622615
TokenError::InvalidNumberOfProvidedSigners => {
623616
info!("Error: Invalid number of provided signers")
624617
}
@@ -714,20 +707,6 @@ mod tests {
714707
let mint2_key = pubkey_rand();
715708
let mut mint2_account = SolanaAccount::new(0, size_of::<Mint>(), &program_id);
716709

717-
// mint_authority not provided
718-
let mut instruction = initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap();
719-
instruction.data = TokenInstruction::InitializeMint {
720-
mint_authority: COption::None,
721-
freeze_authority: COption::None,
722-
decimals: 2,
723-
}
724-
.pack()
725-
.unwrap();
726-
assert_eq!(
727-
Err(TokenError::OwnerRequiredIfNoInitialSupply.into()),
728-
do_process_instruction(instruction, vec![&mut mint_account])
729-
);
730-
731710
// create new mint
732711
do_process_instruction(
733712
initialize_mint(&program_id, &mint_key, &owner_key, None, 2).unwrap(),

0 commit comments

Comments
 (0)