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

Commit d65d2ea

Browse files
Token cli: Improve authorize command (#1445)
* Return clear error message if auth type is incompatible with account * Report real current authority * Prevent reauth of owner's ATA * Fix typo, messaging improvements Co-authored-by: Trent Nelson <[email protected]> Co-authored-by: Trent Nelson <[email protected]>
1 parent 7886d62 commit d65d2ea

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

token/cli/src/main.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use solana_sdk::{
2929
instruction::Instruction,
3030
message::Message,
3131
native_token::*,
32+
program_option::COption,
3233
program_pack::Pack,
3334
pubkey::Pubkey,
3435
signature::{Keypair, Signer},
@@ -365,18 +366,67 @@ fn command_authorize(
365366
account: Pubkey,
366367
authority_type: AuthorityType,
367368
new_owner: Option<Pubkey>,
369+
force_authorize: bool,
368370
) -> CommandResult {
369371
let auth_str = match authority_type {
370372
AuthorityType::MintTokens => "mint authority",
371373
AuthorityType::FreezeAccount => "freeze authority",
372374
AuthorityType::AccountOwner => "owner",
373375
AuthorityType::CloseAccount => "close authority",
374376
};
377+
let target_account = config.rpc_client.get_account(&account)?;
378+
let previous_authority = if let Ok(mint) = Mint::unpack(&target_account.data) {
379+
match authority_type {
380+
AuthorityType::AccountOwner | AuthorityType::CloseAccount => Err(format!(
381+
"Authority type `{}` not supported for SPL Token mints",
382+
auth_str
383+
)),
384+
AuthorityType::MintTokens => Ok(mint.mint_authority),
385+
AuthorityType::FreezeAccount => Ok(mint.freeze_authority),
386+
}
387+
} else if let Ok(token_account) = Account::unpack(&target_account.data) {
388+
let check_associated_token_account = || -> Result<(), Error> {
389+
let maybe_associated_token_account =
390+
get_associated_token_address(&config.owner, &token_account.mint);
391+
if account == maybe_associated_token_account
392+
&& !force_authorize
393+
&& Some(config.owner) != new_owner
394+
{
395+
Err(
396+
format!("Error: attempting to change the `{}` of an associated token account of `--owner`", auth_str)
397+
.into(),
398+
)
399+
} else {
400+
Ok(())
401+
}
402+
};
403+
404+
match authority_type {
405+
AuthorityType::MintTokens | AuthorityType::FreezeAccount => Err(format!(
406+
"Authority type `{}` not supported for SPL Token accounts",
407+
auth_str
408+
)),
409+
AuthorityType::AccountOwner => {
410+
check_associated_token_account()?;
411+
Ok(COption::Some(token_account.owner))
412+
}
413+
AuthorityType::CloseAccount => {
414+
check_associated_token_account()?;
415+
Ok(COption::Some(
416+
token_account.close_authority.unwrap_or(token_account.owner),
417+
))
418+
}
419+
}
420+
} else {
421+
Err("Unsupported account data format".to_string())
422+
}?;
375423
println!(
376424
"Updating {}\n Current {}: {}\n New {}: {}",
377425
account,
378426
auth_str,
379-
config.owner,
427+
previous_authority
428+
.map(|pubkey| pubkey.to_string())
429+
.unwrap_or_else(|| "disabled".to_string()),
380430
auth_str,
381431
new_owner
382432
.map(|pubkey| pubkey.to_string())
@@ -1319,6 +1369,12 @@ fn main() {
13191369
.conflicts_with("new_authority")
13201370
.help("Disable mint, freeze, or close functionality by setting authority to None.")
13211371
)
1372+
.arg(
1373+
Arg::with_name("force")
1374+
.long("force")
1375+
.hidden(true)
1376+
.help("Force re-authorize the wallet's associate token account. Don't use this flag"),
1377+
)
13221378
.arg(multisig_signer_arg())
13231379
.nonce_args(true)
13241380
.offline_args(),
@@ -1862,7 +1918,14 @@ fn main() {
18621918
};
18631919
let new_authority =
18641920
pubkey_of_signer(arg_matches, "new_authority", &mut wallet_manager).unwrap();
1865-
command_authorize(&config, address, authority_type, new_authority)
1921+
let force_authorize = arg_matches.is_present("force");
1922+
command_authorize(
1923+
&config,
1924+
address,
1925+
authority_type,
1926+
new_authority,
1927+
force_authorize,
1928+
)
18661929
}
18671930
("transfer", Some(arg_matches)) => {
18681931
let sender = pubkey_of_signer(arg_matches, "sender", &mut wallet_manager)

0 commit comments

Comments
 (0)