diff --git a/token/client/src/token.rs b/token/client/src/token.rs index 9f7fb2f7dc4..51232ac9593 100644 --- a/token/client/src/token.rs +++ b/token/client/src/token.rs @@ -61,7 +61,7 @@ use { }, state::{Account, AccountState, Mint, Multisig}, }, - spl_token_group_interface::state::TokenGroup, + spl_token_group_interface::state::{TokenGroup, TokenGroupMember}, spl_token_metadata_interface::state::{Field, TokenMetadata}, std::{ fmt, io, @@ -3801,4 +3801,58 @@ where ) .await } + + /// Initialize a token-group member on a mint + pub async fn token_group_initialize_member( + &self, + mint_authority: &Pubkey, + group_mint: &Pubkey, + group_update_authority: &Pubkey, + signing_keypairs: &S, + ) -> TokenResult { + self.process_ixs( + &[spl_token_group_interface::instruction::initialize_member( + &self.program_id, + &self.pubkey, + &self.pubkey, + mint_authority, + group_mint, + group_update_authority, + )], + signing_keypairs, + ) + .await + } + + /// Initialize a token-group member on a mint + #[allow(clippy::too_many_arguments)] + pub async fn token_group_initialize_member_with_rent_transfer( + &self, + payer: &Pubkey, + mint_authority: &Pubkey, + group_mint: &Pubkey, + group_update_authority: &Pubkey, + signing_keypairs: &S, + ) -> TokenResult { + let additional_lamports = self + .get_additional_rent_for_fixed_len_extension::() + .await?; + let mut instructions = vec![]; + if additional_lamports > 0 { + instructions.push(system_instruction::transfer( + payer, + &self.pubkey, + additional_lamports, + )); + } + instructions.push(spl_token_group_interface::instruction::initialize_member( + &self.program_id, + &self.pubkey, + &self.pubkey, + mint_authority, + group_mint, + group_update_authority, + )); + self.process_ixs(&instructions, signing_keypairs).await + } } diff --git a/token/program-2022-test/tests/token_group_initialize_member.rs b/token/program-2022-test/tests/token_group_initialize_member.rs new file mode 100644 index 00000000000..53413010032 --- /dev/null +++ b/token/program-2022-test/tests/token_group_initialize_member.rs @@ -0,0 +1,490 @@ +#![cfg(feature = "test-sbf")] + +mod program_test; +use { + program_test::TestContext, + solana_program_test::{processor, tokio, ProgramTest}, + solana_sdk::{ + instruction::InstructionError, pubkey::Pubkey, signature::Signer, signer::keypair::Keypair, + transaction::TransactionError, transport::TransportError, + }, + spl_pod::bytemuck::pod_from_bytes, + spl_token_2022::{error::TokenError, extension::BaseStateWithExtensions, processor::Processor}, + spl_token_client::token::{ExtensionInitializationParams, TokenError as TokenClientError}, + spl_token_group_interface::{error::TokenGroupError, state::TokenGroupMember}, + std::sync::Arc, +}; + +fn setup_program_test() -> ProgramTest { + let mut program_test = ProgramTest::default(); + program_test.add_program( + "spl_token_2022", + spl_token_2022::id(), + processor!(Processor::process), + ); + program_test +} + +type SetupConfig = (Keypair, Pubkey); // Mint, Authority + +async fn setup(group: SetupConfig, members: Vec) -> (TestContext, Vec) { + let program_test = setup_program_test(); + + let context = program_test.start_with_context().await; + let context = Arc::new(tokio::sync::Mutex::new(context)); + let mut group_context = TestContext { + context: context.clone(), + token_context: None, + }; + + let (group_mint, group_authority) = group; + let group_address = Some(group_mint.pubkey()); + group_context + .init_token_with_mint_keypair_and_freeze_authority( + group_mint, + vec![ExtensionInitializationParams::GroupPointer { + authority: Some(group_authority), + group_address, + }], + None, + ) + .await + .unwrap(); + + let mut member_contexts = vec![]; + for member in members.into_iter() { + let (member_mint, member_authority) = member; + let member_address = Some(member_mint.pubkey()); + let mut member_context = TestContext { + context: context.clone(), + token_context: None, + }; + member_context + .init_token_with_mint_keypair_and_freeze_authority( + member_mint, + vec![ExtensionInitializationParams::GroupMemberPointer { + authority: Some(member_authority), + member_address, + }], + None, + ) + .await + .unwrap(); + member_contexts.push(member_context); + } + + let payer_pubkey = group_context.context.lock().await.payer.pubkey(); + let group_token_context = group_context.token_context.as_ref().unwrap(); + group_token_context + .token + .token_group_initialize_with_rent_transfer( + &payer_pubkey, + &group_token_context.mint_authority.pubkey(), + &group_authority, + 2, + &[&group_token_context.mint_authority], + ) + .await + .unwrap(); + + (group_context, member_contexts) +} + +#[tokio::test] +async fn success_initialize() { + let group_authority = Keypair::new(); + let group_mint_keypair = Keypair::new(); + let member1_authority = Keypair::new(); + let member1_mint_keypair = Keypair::new(); + let member2_authority = Keypair::new(); + let member2_mint_keypair = Keypair::new(); + let member3_authority = Keypair::new(); + let member3_mint_keypair = Keypair::new(); + + let (_, mut member_contexts) = setup( + ( + group_mint_keypair.insecure_clone(), + group_authority.pubkey(), + ), + vec![ + ( + member1_mint_keypair.insecure_clone(), + member1_authority.pubkey(), + ), + ( + member2_mint_keypair.insecure_clone(), + member2_authority.pubkey(), + ), + ( + member3_mint_keypair.insecure_clone(), + member3_authority.pubkey(), + ), + ], + ) + .await; + + let member1_token_context = member_contexts[0].token_context.take().unwrap(); + + // fails without more lamports for new rent-exemption + let error = member1_token_context + .token + .token_group_initialize_member( + &member1_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[&member1_token_context.mint_authority, &group_authority], + ) + .await + .unwrap_err(); + let member_index = if group_mint_keypair + .pubkey() + .cmp(&member1_mint_keypair.pubkey()) + .is_le() + { + 4 + } else { + 3 + }; + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InsufficientFundsForRent { + account_index: member_index + } + ))) + ); + + // fail wrong mint authority signer + let payer_pubkey = member_contexts[0].context.lock().await.payer.pubkey(); + let not_mint_authority = Keypair::new(); + let error = member1_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + ¬_mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[¬_mint_authority, &group_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenGroupError::IncorrectMintAuthority as u32) + ) + ))) + ); + + // fail wrong group update authority signer + let not_group_update_authority = Keypair::new(); + let error = member1_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + &member1_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + ¬_group_update_authority.pubkey(), + &[ + &member1_token_context.mint_authority, + ¬_group_update_authority, + ], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenGroupError::IncorrectUpdateAuthority as u32) + ) + ))) + ); + + // fail group and member same mint + let error = member1_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + &member1_token_context.mint_authority.pubkey(), + member1_token_context.token.get_address(), + &group_authority.pubkey(), + &[&member1_token_context.mint_authority, &group_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenGroupError::MemberAccountIsGroupAccount as u32) + ) + ))) + ); + + member1_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + &member1_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[&member1_token_context.mint_authority, &group_authority], + ) + .await + .unwrap(); + + // check that the data is correct + let mint_info = member1_token_context.token.get_mint_info().await.unwrap(); + let member_bytes = mint_info.get_extension_bytes::().unwrap(); + let fetched_member = pod_from_bytes::(member_bytes).unwrap(); + assert_eq!( + fetched_member, + &TokenGroupMember { + mint: member1_mint_keypair.pubkey(), + group: group_mint_keypair.pubkey(), + member_number: 1.try_into().unwrap(), + } + ); + + // fail double-init + { + let mut context = member_contexts[0].context.lock().await; + context.get_new_latest_blockhash().await.unwrap(); + context.get_new_latest_blockhash().await.unwrap(); + } + let error = member1_token_context + .token + .token_group_initialize_member( + &member1_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[&member1_token_context.mint_authority, &group_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 0, + InstructionError::Custom(TokenError::ExtensionAlreadyInitialized as u32) + ) + ))) + ); + + // Now the second + let member2_token_context = member_contexts[1].token_context.take().unwrap(); + member2_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + &member2_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[&member2_token_context.mint_authority, &group_authority], + ) + .await + .unwrap(); + let mint_info = member2_token_context.token.get_mint_info().await.unwrap(); + let member_bytes = mint_info.get_extension_bytes::().unwrap(); + let fetched_member = pod_from_bytes::(member_bytes).unwrap(); + assert_eq!( + fetched_member, + &TokenGroupMember { + mint: member2_mint_keypair.pubkey(), + group: group_mint_keypair.pubkey(), + member_number: 2.try_into().unwrap(), + } + ); + + // Third should fail on max size + let member3_token_context = member_contexts[2].token_context.take().unwrap(); + let error = member3_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + &member3_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[&member3_token_context.mint_authority, &group_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenGroupError::SizeExceedsMaxSize as u32) + ) + ))) + ); +} + +#[tokio::test] +async fn fail_without_member_pointer() { + let group_authority = Keypair::new(); + let group_mint_keypair = Keypair::new(); + let member_mint_keypair = Keypair::new(); + + let (group_context, _) = setup( + ( + group_mint_keypair.insecure_clone(), + group_authority.pubkey(), + ), + vec![], + ) + .await; + + let mut member_test_context = TestContext { + context: group_context.context.clone(), + token_context: None, + }; + member_test_context + .init_token_with_mint_keypair_and_freeze_authority(member_mint_keypair, vec![], None) + .await + .unwrap(); + + let payer_pubkey = member_test_context.context.lock().await.payer.pubkey(); + let member_token_context = member_test_context.token_context.take().unwrap(); + + let error = member_token_context + .token + .token_group_initialize_member_with_rent_transfer( + &payer_pubkey, + &member_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + &[&member_token_context.mint_authority, &group_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenError::InvalidExtensionCombination as u32) + ) + ))) + ); +} + +#[tokio::test] +async fn fail_init_in_another_mint() { + let group_authority = Keypair::new(); + let group_mint_keypair = Keypair::new(); + let member_authority = Keypair::new(); + let first_member_mint_keypair = Keypair::new(); + let second_member_mint_keypair = Keypair::new(); + + let (_, mut member_contexts) = setup( + ( + group_mint_keypair.insecure_clone(), + group_authority.pubkey(), + ), + vec![( + second_member_mint_keypair.insecure_clone(), + member_authority.pubkey(), + )], + ) + .await; + + let member_token_context = member_contexts[0].token_context.take().unwrap(); + let error = member_token_context + .token + .process_ixs( + &[spl_token_group_interface::instruction::initialize_member( + &spl_token_2022::id(), + &first_member_mint_keypair.pubkey(), + member_token_context.token.get_address(), + &member_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + )], + &[&member_token_context.mint_authority, &group_authority], + ) + .await + .unwrap_err(); + + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 0, + InstructionError::Custom(TokenError::MintMismatch as u32) + ) + ))) + ); +} + +#[tokio::test] +async fn fail_without_signatures() { + let group_authority = Keypair::new(); + let group_mint_keypair = Keypair::new(); + let member_authority = Keypair::new(); + let member_mint_keypair = Keypair::new(); + + let (_, mut member_contexts) = setup( + ( + group_mint_keypair.insecure_clone(), + group_authority.pubkey(), + ), + vec![( + member_mint_keypair.insecure_clone(), + member_authority.pubkey(), + )], + ) + .await; + + let member_token_context = member_contexts[0].token_context.take().unwrap(); + + // Missing mint authority + let mut instruction = spl_token_group_interface::instruction::initialize_member( + &spl_token_2022::id(), + &member_mint_keypair.pubkey(), + member_token_context.token.get_address(), + &member_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + ); + instruction.accounts[2].is_signer = false; + let error = member_token_context + .token + .process_ixs(&[instruction], &[&group_authority]) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature) + ))) + ); + + // Missing group update authority + let mut instruction = spl_token_group_interface::instruction::initialize_member( + &spl_token_2022::id(), + &member_mint_keypair.pubkey(), + member_token_context.token.get_address(), + &member_token_context.mint_authority.pubkey(), + &group_mint_keypair.pubkey(), + &group_authority.pubkey(), + ); + instruction.accounts[4].is_signer = false; + let error = member_token_context + .token + .process_ixs(&[instruction], &[&member_token_context.mint_authority]) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature) + ))) + ); +} diff --git a/token/program-2022/src/extension/mod.rs b/token/program-2022/src/extension/mod.rs index 5af568f4298..e896bdd9cb6 100644 --- a/token/program-2022/src/extension/mod.rs +++ b/token/program-2022/src/extension/mod.rs @@ -37,7 +37,7 @@ use { bytemuck::{pod_from_bytes, pod_from_bytes_mut, pod_get_packed_len}, primitives::PodU16, }, - spl_token_group_interface::state::TokenGroup, + spl_token_group_interface::state::{TokenGroup, TokenGroupMember}, spl_type_length_value::variable_len_pack::VariableLenPack, std::{ cmp::Ordering, @@ -959,6 +959,8 @@ pub enum ExtensionType { /// Mint contains a pointer to another account (or the same account) that /// holds group member configurations GroupMemberPointer, + /// Mint contains token group member configurations + TokenGroupMember, /// Test variable-length mint extension #[cfg(test)] VariableLenMintTest = u16::MAX - 2, @@ -1038,6 +1040,7 @@ impl ExtensionType { ExtensionType::GroupPointer => pod_get_packed_len::(), ExtensionType::TokenGroup => pod_get_packed_len::(), ExtensionType::GroupMemberPointer => pod_get_packed_len::(), + ExtensionType::TokenGroupMember => pod_get_packed_len::(), #[cfg(test)] ExtensionType::AccountPaddingTest => pod_get_packed_len::(), #[cfg(test)] @@ -1100,7 +1103,8 @@ impl ExtensionType { | ExtensionType::TokenMetadata | ExtensionType::GroupPointer | ExtensionType::TokenGroup - | ExtensionType::GroupMemberPointer => AccountType::Mint, + | ExtensionType::GroupMemberPointer + | ExtensionType::TokenGroupMember => AccountType::Mint, ExtensionType::ImmutableOwner | ExtensionType::TransferFeeAmount | ExtensionType::ConfidentialTransferAccount diff --git a/token/program-2022/src/extension/token_group/mod.rs b/token/program-2022/src/extension/token_group/mod.rs index 986bda89999..1546ec92975 100644 --- a/token/program-2022/src/extension/token_group/mod.rs +++ b/token/program-2022/src/extension/token_group/mod.rs @@ -1,6 +1,6 @@ use { crate::extension::{Extension, ExtensionType}, - spl_token_group_interface::state::TokenGroup, + spl_token_group_interface::state::{TokenGroup, TokenGroupMember}, }; /// Instruction processor for the TokenGroup extensions @@ -9,3 +9,7 @@ pub mod processor; impl Extension for TokenGroup { const TYPE: ExtensionType = ExtensionType::TokenGroup; } + +impl Extension for TokenGroupMember { + const TYPE: ExtensionType = ExtensionType::TokenGroupMember; +} diff --git a/token/program-2022/src/extension/token_group/processor.rs b/token/program-2022/src/extension/token_group/processor.rs index 5fce2def9b8..cdfb5dc4a9f 100644 --- a/token/program-2022/src/extension/token_group/processor.rs +++ b/token/program-2022/src/extension/token_group/processor.rs @@ -5,8 +5,9 @@ use { check_program_account, error::TokenError, extension::{ - alloc_and_serialize, group_pointer::GroupPointer, BaseStateWithExtensions, - StateWithExtensions, StateWithExtensionsMut, + alloc_and_serialize, group_member_pointer::GroupMemberPointer, + group_pointer::GroupPointer, BaseStateWithExtensions, StateWithExtensions, + StateWithExtensionsMut, }, state::Mint, }, @@ -24,7 +25,7 @@ use { instruction::{ InitializeGroup, TokenGroupInstruction, UpdateGroupAuthority, UpdateGroupMaxSize, }, - state::TokenGroup, + state::{TokenGroup, TokenGroupMember}, }, }; @@ -95,7 +96,7 @@ pub fn process_initialize_group( } /// Processes an -/// [UpdateGroupMaxSize](enum.GroupInterfaceInstruction.html) +/// [UpdateGroupMaxSize](enum.TokenGroupInstruction.html) /// instruction pub fn process_update_group_max_size( _program_id: &Pubkey, @@ -119,7 +120,7 @@ pub fn process_update_group_max_size( } /// Processes an -/// [UpdateGroupAuthority](enum.GroupInterfaceInstruction.html) +/// [UpdateGroupAuthority](enum.TokenGroupInstruction.html) /// instruction pub fn process_update_group_authority( _program_id: &Pubkey, @@ -142,6 +143,69 @@ pub fn process_update_group_authority( Ok(()) } +/// Processes an [InitializeMember](enum.TokenGroupInstruction.html) +/// instruction +pub fn process_initialize_member(_program_id: &Pubkey, accounts: &[AccountInfo]) -> ProgramResult { + let account_info_iter = &mut accounts.iter(); + + let member_info = next_account_info(account_info_iter)?; + let member_mint_info = next_account_info(account_info_iter)?; + let member_mint_authority_info = next_account_info(account_info_iter)?; + let group_info = next_account_info(account_info_iter)?; + let group_update_authority_info = next_account_info(account_info_iter)?; + + // check that the mint and member accounts are the same, since the member + // extension should only describe itself + if member_info.key != member_mint_info.key { + msg!("Group member configurations for a mint must be initialized in the mint itself."); + return Err(TokenError::MintMismatch.into()); + } + + // scope the mint authority check, since the mint is in the same account! + { + // This check isn't really needed since we'll be writing into the account, + // but auditors like it + check_program_account(member_mint_info.owner)?; + let member_mint_data = member_mint_info.try_borrow_data()?; + let member_mint = StateWithExtensions::::unpack(&member_mint_data)?; + + if !member_mint_authority_info.is_signer { + return Err(ProgramError::MissingRequiredSignature); + } + if member_mint.base.mint_authority.as_ref() != COption::Some(member_mint_authority_info.key) + { + return Err(TokenGroupError::IncorrectMintAuthority.into()); + } + + if member_mint.get_extension::().is_err() { + msg!( + "A mint with group member configurations must have the group-member-pointer \ + extension initialized" + ); + return Err(TokenError::InvalidExtensionCombination.into()); + } + } + + // Make sure the member mint is not the same as the group mint + if member_info.key == group_info.key { + return Err(TokenGroupError::MemberAccountIsGroupAccount.into()); + } + + // Increment the size of the group + let mut buffer = group_info.try_borrow_mut_data()?; + let mut state = StateWithExtensionsMut::::unpack(&mut buffer)?; + let group = state.get_extension_mut::()?; + + check_update_authority(group_update_authority_info, &group.update_authority)?; + let member_number = group.increment_size()?; + + // Allocate a TLV entry for the space and write it in + let member = TokenGroupMember::new(member_mint_info.key, group_info.key, member_number); + alloc_and_serialize::(member_info, &member, false)?; + + Ok(()) +} + /// Processes an [Instruction](enum.Instruction.html). pub fn process_instruction( program_id: &Pubkey, @@ -161,6 +225,9 @@ pub fn process_instruction( msg!("TokenGroupInstruction: UpdateGroupAuthority"); process_update_group_authority(program_id, accounts, data) } - _ => Err(ProgramError::InvalidInstructionData), + TokenGroupInstruction::InitializeMember(_) => { + msg!("TokenGroupInstruction: InitializeMember"); + process_initialize_member(program_id, accounts) + } } }