Skip to content

Commit

Permalink
token-cli: Add command authorize support for TokenGroup (#6165)
Browse files Browse the repository at this point in the history
* Add command Authorize support for TokenGroup

* Add TokenGroup CliAuthorityType

* Add test for changing token update_authority

* fixed err for wrong extension

* fixed var names in test

* Update token/cli/src/clap_app.rs

Co-authored-by: Jon C <[email protected]>

* Update token/cli/src/clap_app.rs

Co-authored-by: Jon C <[email protected]>

* Renamed instances of TokenGroup to Group for CliAuthorityType

---------

Co-authored-by: Jon C <[email protected]>
  • Loading branch information
tonton-sol and joncinque authored Jan 23, 2024
1 parent 1a59767 commit 9888848
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
4 changes: 4 additions & 0 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub enum CliAuthorityType {
Metadata,
GroupPointer,
GroupMemberPointer,
Group,
}
impl TryFrom<CliAuthorityType> for AuthorityType {
type Error = Error;
Expand Down Expand Up @@ -218,6 +219,9 @@ impl TryFrom<CliAuthorityType> for AuthorityType {
}
CliAuthorityType::GroupPointer => Ok(AuthorityType::GroupPointer),
CliAuthorityType::GroupMemberPointer => Ok(AuthorityType::GroupMemberPointer),
CliAuthorityType::Group => {
Err("Group update authority does not map to a token authority type".into())
}
}
}
}
Expand Down
45 changes: 31 additions & 14 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ use {
client::{ProgramRpcClientSendTransaction, RpcClientResponse},
token::{ExtensionInitializationParams, Token},
},
spl_token_group_interface::state::TokenGroup,
spl_token_metadata_interface::state::{Field, TokenMetadata},
std::{collections::HashMap, fmt::Display, process::exit, rc::Rc, str::FromStr, sync::Arc},
};
Expand Down Expand Up @@ -1016,6 +1017,13 @@ async fn command_authorize(
))
}
}
CliAuthorityType::Group => {
if let Ok(extension) = mint.get_extension::<TokenGroup>() {
Ok(Option::<Pubkey>::from(extension.update_authority))
} else {
Err(format!("Mint `{}` does not support token groups", account))
}
}
}?;

Ok((account, previous_authority))
Expand Down Expand Up @@ -1056,6 +1064,7 @@ async fn command_authorize(
| CliAuthorityType::MetadataPointer
| CliAuthorityType::Metadata
| CliAuthorityType::GroupPointer
| CliAuthorityType::Group
| CliAuthorityType::GroupMemberPointer => Err(format!(
"Authority type `{auth_str}` not supported for SPL Token accounts",
)),
Expand Down Expand Up @@ -1107,20 +1116,28 @@ async fn command_authorize(
),
);

let res = if let CliAuthorityType::Metadata = authority_type {
token
.token_metadata_update_authority(&authority, new_authority, &bulk_signers)
.await?
} else {
token
.set_authority(
&account,
&authority,
new_authority.as_ref(),
authority_type.try_into()?,
&bulk_signers,
)
.await?
let res = match authority_type {
CliAuthorityType::Metadata => {
token
.token_metadata_update_authority(&authority, new_authority, &bulk_signers)
.await?
}
CliAuthorityType::Group => {
token
.token_group_update_authority(&authority, new_authority, &bulk_signers)
.await?
}
_ => {
token
.set_authority(
&account,
&authority,
new_authority.as_ref(),
authority_type.try_into()?,
&bulk_signers,
)
.await?
}
};

let tx_return = finish_tx(config, &res, false).await?;
Expand Down
20 changes: 20 additions & 0 deletions token/cli/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3912,4 +3912,24 @@ async fn group(test_validator: &TestValidator, payer: &Keypair) {
assert_eq!(extension.group, mint);
assert_eq!(extension.mint, member_mint);
assert_eq!(u32::from(extension.member_number), 1);

// update authority
process_test_command(
&config,
payer,
&[
"spl-token",
CommandName::Authorize.into(),
&mint.to_string(),
"group",
&mint.to_string(),
],
)
.await
.unwrap();

let account = config.rpc_client.get_account(&mint).await.unwrap();
let mint_state = StateWithExtensionsOwned::<Mint>::unpack(account.data).unwrap();
let extension = mint_state.get_extension::<TokenGroup>().unwrap();
assert_eq!(extension.update_authority, Some(mint).try_into().unwrap());
}

0 comments on commit 9888848

Please sign in to comment.