From 5b340e5a617353a34218c0cad74671d1d1d05f2d Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 18 Oct 2023 15:37:21 +0200 Subject: [PATCH] token 2022: add `InitializeGroup` instruction from SPL Token Group interface --- Cargo.lock | 1 + .../tests/token_group_initialize_group.rs | 281 ++++++++++++++++++ token/program-2022/Cargo.toml | 1 + token/program-2022/src/extension/mod.rs | 7 + .../src/extension/token_group/mod.rs | 11 + .../src/extension/token_group/processor.rs | 87 ++++++ token/program-2022/src/processor.rs | 5 +- 7 files changed, 392 insertions(+), 1 deletion(-) create mode 100644 token/program-2022-test/tests/token_group_initialize_group.rs create mode 100644 token/program-2022/src/extension/token_group/mod.rs create mode 100644 token/program-2022/src/extension/token_group/processor.rs diff --git a/Cargo.lock b/Cargo.lock index 4019f31f9e4..ac0ad88f51b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7067,6 +7067,7 @@ dependencies = [ "spl-memo 4.0.0", "spl-pod 0.1.0", "spl-token 4.0.0", + "spl-token-group-interface", "spl-token-metadata-interface 0.2.0", "spl-transfer-hook-interface 0.3.0", "spl-type-length-value 0.3.0", diff --git a/token/program-2022-test/tests/token_group_initialize_group.rs b/token/program-2022-test/tests/token_group_initialize_group.rs new file mode 100644 index 00000000000..d9723650d0e --- /dev/null +++ b/token/program-2022-test/tests/token_group_initialize_group.rs @@ -0,0 +1,281 @@ +#![cfg(feature = "test-sbf")] + +mod program_test; +use { + borsh::BorshDeserialize, + 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_token_2022::{error::TokenError, extension::BaseStateWithExtensions, processor::Processor}, + spl_token_client::token::{ExtensionInitializationParams, TokenError as TokenClientError}, + spl_token_metadata_interface::state::TokenMetadata, + std::{convert::TryInto, 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 +} + +async fn setup(mint: Keypair, authority: &Pubkey) -> TestContext { + 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 context = TestContext { + context, + token_context: None, + }; + let metadata_address = Some(mint.pubkey()); + context + .init_token_with_mint_keypair_and_freeze_authority( + mint, + vec![ExtensionInitializationParams::MetadataPointer { + authority: Some(*authority), + metadata_address, + }], + None, + ) + .await + .unwrap(); + context +} + +#[tokio::test] +async fn success_initialize_group() { + let group = Keypair::new(); + let group_mint = Keypair::new(); + let group_mint_authority = Keypair::new(); + + let mut test_context = setup(group_mint, &group_mint_authority).await; + let payer_pubkey = test_context.context.lock().await.payer.pubkey(); + let token_context = test_context.token_context.take().unwrap(); + + // + + // fails without more lamports for new rent-exemption + let error = token_context + .token + .token_metadata_initialize( + &update_authority, + &token_context.mint_authority.pubkey(), + token_metadata.name.clone(), + token_metadata.symbol.clone(), + token_metadata.uri.clone(), + &[&token_context.mint_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InsufficientFundsForRent { account_index: 2 } + ))) + ); + + // fail wrong signer + let not_mint_authority = Keypair::new(); + let error = token_context + .token + .token_metadata_initialize_with_rent_transfer( + &payer_pubkey, + &update_authority, + ¬_mint_authority.pubkey(), + token_metadata.name.clone(), + token_metadata.symbol.clone(), + token_metadata.uri.clone(), + &[¬_mint_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 1, + InstructionError::Custom(TokenError::IncorrectMintAuthority as u32) + ) + ))) + ); + + token_context + .token + .token_metadata_initialize_with_rent_transfer( + &payer_pubkey, + &update_authority, + &token_context.mint_authority.pubkey(), + token_metadata.name.clone(), + token_metadata.symbol.clone(), + token_metadata.uri.clone(), + &[&token_context.mint_authority], + ) + .await + .unwrap(); + + // check that the data is correct + let mint_info = token_context.token.get_mint_info().await.unwrap(); + let metadata_bytes = mint_info.get_extension_bytes::().unwrap(); + let fetched_metadata = TokenMetadata::try_from_slice(metadata_bytes).unwrap(); + assert_eq!(fetched_metadata, token_metadata); + + // fail double-init + let error = token_context + .token + .token_metadata_initialize_with_rent_transfer( + &payer_pubkey, + &update_authority, + &token_context.mint_authority.pubkey(), + token_metadata.name.clone(), + token_metadata.symbol.clone(), + token_metadata.uri.clone(), + &[&token_context.mint_authority], + ) + .await + .unwrap_err(); + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError( + 0, + InstructionError::Custom(TokenError::ExtensionAlreadyInitialized as u32) + ) + ))) + ); +} + +#[tokio::test] +async fn fail_without_metadata_pointer() { + let mut test_context = { + let mint_keypair = Keypair::new(); + 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 context = TestContext { + context, + token_context: None, + }; + context + .init_token_with_mint_keypair_and_freeze_authority(mint_keypair, vec![], None) + .await + .unwrap(); + context + }; + + let payer_pubkey = test_context.context.lock().await.payer.pubkey(); + let token_context = test_context.token_context.take().unwrap(); + + let error = token_context + .token + .token_metadata_initialize_with_rent_transfer( + &payer_pubkey, + &Pubkey::new_unique(), + &token_context.mint_authority.pubkey(), + "Name".to_string(), + "Symbol".to_string(), + "URI".to_string(), + &[&token_context.mint_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 authority = Pubkey::new_unique(); + let first_mint_keypair = Keypair::new(); + let first_mint = first_mint_keypair.pubkey(); + let mut test_context = setup(first_mint_keypair, &authority).await; + let second_mint_keypair = Keypair::new(); + let second_mint = second_mint_keypair.pubkey(); + test_context + .init_token_with_mint_keypair_and_freeze_authority( + second_mint_keypair, + vec![ExtensionInitializationParams::MetadataPointer { + authority: Some(authority), + metadata_address: Some(second_mint), + }], + None, + ) + .await + .unwrap(); + + let token_context = test_context.token_context.take().unwrap(); + + let error = token_context + .token + .process_ixs( + &[spl_token_metadata_interface::instruction::initialize( + &spl_token_2022::id(), + &first_mint, + &Pubkey::new_unique(), + token_context.token.get_address(), + &token_context.mint_authority.pubkey(), + "Name".to_string(), + "Symbol".to_string(), + "URI".to_string(), + )], + &[&token_context.mint_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_signature() { + let authority = Pubkey::new_unique(); + let mint_keypair = Keypair::new(); + let mut test_context = setup(mint_keypair, &authority).await; + + let token_context = test_context.token_context.take().unwrap(); + + let mut instruction = spl_token_metadata_interface::instruction::initialize( + &spl_token_2022::id(), + token_context.token.get_address(), + &Pubkey::new_unique(), + token_context.token.get_address(), + &token_context.mint_authority.pubkey(), + "Name".to_string(), + "Symbol".to_string(), + "URI".to_string(), + ); + instruction.accounts[3].is_signer = false; + let error = token_context + .token + .process_ixs(&[instruction], &[] as &[&dyn Signer; 0]) // yuck, but the compiler needs it + .await + .unwrap_err(); + + assert_eq!( + error, + TokenClientError::Client(Box::new(TransportError::TransactionError( + TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature) + ))) + ); +} diff --git a/token/program-2022/Cargo.toml b/token/program-2022/Cargo.toml index d1aa8e58dbe..badd7929cdb 100644 --- a/token/program-2022/Cargo.toml +++ b/token/program-2022/Cargo.toml @@ -27,6 +27,7 @@ solana-program = "1.16.16" solana-zk-token-sdk = "1.16.16" spl-memo = { version = "4.0.0", path = "../../memo/program", features = [ "no-entrypoint" ] } spl-token = { version = "4.0", path = "../program", features = ["no-entrypoint"] } +spl-token-group-interface = { version = "0.1.0", path = "../../token-group/interface" } spl-token-metadata-interface = { version = "0.2.0", path = "../../token-metadata/interface" } spl-transfer-hook-interface = { version = "0.3.0", path = "../transfer-hook-interface" } spl-type-length-value = { version = "0.3.0", path = "../../libraries/type-length-value" } diff --git a/token/program-2022/src/extension/mod.rs b/token/program-2022/src/extension/mod.rs index 23afe0a3694..4c9d43ef8ef 100644 --- a/token/program-2022/src/extension/mod.rs +++ b/token/program-2022/src/extension/mod.rs @@ -29,6 +29,7 @@ use { program_error::ProgramError, program_pack::{IsInitialized, Pack}, }, + spl_token_group_interface::state::TokenGroup, spl_pod::{ bytemuck::{pod_from_bytes, pod_from_bytes_mut, pod_get_packed_len}, primitives::PodU16, @@ -68,6 +69,8 @@ pub mod non_transferable; pub mod permanent_delegate; /// Utility to reallocate token accounts pub mod reallocate; +/// Token-group extension +pub mod token_group; /// Token-metadata extension pub mod token_metadata; /// Transfer Fee extension @@ -904,6 +907,8 @@ pub enum ExtensionType { ConfidentialTransferFeeAmount, /// Mint contains a pointer to another account (or the same account) that holds metadata MetadataPointer, + /// Mint contains token group configurations + TokenGroup, /// Mint contains token-metadata TokenMetadata, /// Test variable-length mint extension @@ -979,6 +984,7 @@ impl ExtensionType { pod_get_packed_len::() } ExtensionType::MetadataPointer => pod_get_packed_len::(), + ExtensionType::TokenGroup => pod_get_packed_len::(), ExtensionType::TokenMetadata => unreachable!(), #[cfg(test)] ExtensionType::AccountPaddingTest => pod_get_packed_len::(), @@ -1039,6 +1045,7 @@ impl ExtensionType { | ExtensionType::TransferHook | ExtensionType::ConfidentialTransferFeeConfig | ExtensionType::MetadataPointer + | ExtensionType::TokenGroup | ExtensionType::TokenMetadata => AccountType::Mint, ExtensionType::ImmutableOwner | ExtensionType::TransferFeeAmount diff --git a/token/program-2022/src/extension/token_group/mod.rs b/token/program-2022/src/extension/token_group/mod.rs new file mode 100644 index 00000000000..986bda89999 --- /dev/null +++ b/token/program-2022/src/extension/token_group/mod.rs @@ -0,0 +1,11 @@ +use { + crate::extension::{Extension, ExtensionType}, + spl_token_group_interface::state::TokenGroup, +}; + +/// Instruction processor for the TokenGroup extensions +pub mod processor; + +impl Extension for TokenGroup { + const TYPE: ExtensionType = ExtensionType::TokenGroup; +} diff --git a/token/program-2022/src/extension/token_group/processor.rs b/token/program-2022/src/extension/token_group/processor.rs new file mode 100644 index 00000000000..fec911afe9f --- /dev/null +++ b/token/program-2022/src/extension/token_group/processor.rs @@ -0,0 +1,87 @@ +//! Token-group processor + +use { + crate::{ + check_program_account, + error::TokenError, + extension::StateWithExtensions, + state::Mint, + }, + solana_program::{ + account_info::{next_account_info, AccountInfo}, + entrypoint::ProgramResult, + msg, + program_error::ProgramError, + program_option::COption, + pubkey::Pubkey, + }, + spl_token_group_interface::{ + instruction::{ + InitializeGroup, TokenGroupInstruction, + }, + state::TokenGroup, + }, + spl_type_length_value::state::TlvStateMut, +}; + +/// Processes a [InitializeGroup](enum.TokenGroupInstruction.html) instruction. +pub fn process_initialize_group( + _program_id: &Pubkey, + accounts: &[AccountInfo], + data: InitializeGroup, +) -> ProgramResult { + let account_info_iter = &mut accounts.iter(); + + let group_info = next_account_info(account_info_iter)?; + let mint_info = next_account_info(account_info_iter)?; + let mint_authority_info = next_account_info(account_info_iter)?; + + // check that the mint and group accounts are the same, since the group + // extension should only describe itself + if group_info.key != mint_info.key { + msg!("Group 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(mint_info.owner)?; + let mint_data = mint_info.try_borrow_data()?; + let mint = StateWithExtensions::::unpack(&mint_data)?; + + if !mint_authority_info.is_signer { + return Err(ProgramError::MissingRequiredSignature); + } + if mint.base.mint_authority.as_ref() != COption::Some(mint_authority_info.key) { + return Err(TokenError::IncorrectMintAuthority.into()); + } + } + + // Allocate a TLV entry for the space and write it in + // Assumes that there's enough SOL for the new rent-exemption + let mut buffer = group_info.try_borrow_mut_data()?; + let mut state = TlvStateMut::unpack(&mut buffer)?; + let (group, _) = state.init_value::(false)?; + *group = TokenGroup::new(data.update_authority, data.max_size.into()); + + Ok(()) +} + +/// Processes an [Instruction](enum.Instruction.html). +pub fn process_instruction( + program_id: &Pubkey, + accounts: &[AccountInfo], + instruction: TokenGroupInstruction, +) -> ProgramResult { + match instruction { + TokenGroupInstruction::InitializeGroup(data) => { + msg!("TokenGroupInstruction: InitializeGroup"); + process_initialize_group(program_id, accounts, data) + } + _ => { + Err(ProgramError::InvalidInstructionData) + } + } +} diff --git a/token/program-2022/src/processor.rs b/token/program-2022/src/processor.rs index 27b58ec3a88..f023d927b3c 100644 --- a/token/program-2022/src/processor.rs +++ b/token/program-2022/src/processor.rs @@ -18,7 +18,7 @@ use { mint_close_authority::MintCloseAuthority, non_transferable::{NonTransferable, NonTransferableAccount}, permanent_delegate::{get_permanent_delegate, PermanentDelegate}, - reallocate, token_metadata, + reallocate, token_group, token_metadata, transfer_fee::{self, TransferFeeAmount, TransferFeeConfig}, transfer_hook::{self, TransferHook, TransferHookAccount}, AccountType, BaseStateWithExtensions, ExtensionType, StateWithExtensions, @@ -41,6 +41,7 @@ use { system_instruction, system_program, sysvar::{rent::Rent, Sysvar}, }, + spl_token_group_interface::instruction::TokenGroupInstruction, spl_token_metadata_interface::instruction::TokenMetadataInstruction, std::convert::{TryFrom, TryInto}, }; @@ -1664,6 +1665,8 @@ impl Processor { } } else if let Ok(instruction) = TokenMetadataInstruction::unpack(input) { token_metadata::processor::process_instruction(program_id, accounts, instruction) + } else if let Ok(instruction) = TokenGroupInstruction::unpack(input) { + token_group::processor::process_instruction(program_id, accounts, instruction) } else { Err(TokenError::InvalidInstruction.into()) }