Flesh out SetAuthority capabilities (#531)

This commit is contained in:
Tyera Eulberg 2020-09-25 12:27:45 -06:00 committed by GitHub
parent e825b9d4fe
commit 72ec14a190
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 13 deletions

View File

@ -172,20 +172,35 @@ fn command_create_account(
Ok(Some(transaction)) Ok(Some(transaction))
} }
fn command_assign(config: &Config, account: Pubkey, new_owner: Pubkey) -> CommandResult { fn command_authorize(
config: &Config,
account: Pubkey,
authority_type: AuthorityType,
new_owner: Option<Pubkey>,
) -> CommandResult {
let auth_str = match authority_type {
AuthorityType::MintTokens => "mint authority",
AuthorityType::FreezeAccount => "freeze authority",
AuthorityType::AccountOwner => "owner",
AuthorityType::CloseAccount => "close authority",
};
println!( println!(
"Assigning {}\n Current owner: {}\n New owner: {}", "Updating {}\n Current {}: {}\n New {}: {}",
account, account,
auth_str,
config.owner.pubkey(), config.owner.pubkey(),
auth_str,
new_owner new_owner
.map(|pubkey| pubkey.to_string())
.unwrap_or_else(|| "disabled".to_string())
); );
let mut transaction = Transaction::new_with_payer( let mut transaction = Transaction::new_with_payer(
&[set_authority( &[set_authority(
&spl_token::id(), &spl_token::id(),
&account, &account,
Some(&new_owner), new_owner.as_ref(),
AuthorityType::AccountOwner, authority_type,
&config.owner.pubkey(), &config.owner.pubkey(),
&[], &[],
)?], )?],
@ -731,8 +746,8 @@ fn main() {
), ),
) )
.subcommand( .subcommand(
SubCommand::with_name("assign") SubCommand::with_name("authorize")
.about("Assign a token or token account to a new owner") .about("Authorize a new signing keypair to a token or token account")
.arg( .arg(
Arg::with_name("address") Arg::with_name("address")
.validator(is_pubkey_or_keypair) .validator(is_pubkey_or_keypair)
@ -743,13 +758,31 @@ fn main() {
.help("The address of the token account"), .help("The address of the token account"),
) )
.arg( .arg(
Arg::with_name("new_owner") Arg::with_name("authority_type")
.validator(is_pubkey_or_keypair) .value_name("AUTHORITY_TYPE")
.value_name("OWNER_ADDRESS")
.takes_value(true) .takes_value(true)
.possible_values(&["mint", "freeze", "owner", "close"])
.index(2) .index(2)
.required(true) .required(true)
.help("The address of the new owner"), .help("The new authority type. \
Token mints support `mint` and `freeze` authorities;\
Token accounts support `owner` and `close` authorities."),
)
.arg(
Arg::with_name("new_authority")
.validator(is_pubkey_or_keypair)
.value_name("AUTHORITY_ADDRESS")
.takes_value(true)
.index(3)
.required_unless("disable")
.help("The address of the new authority"),
)
.arg(
Arg::with_name("disable")
.long("disable")
.takes_value(false)
.conflicts_with("new_authority")
.help("Disable mint, freeze, or close functionality by setting authority to None.")
), ),
) )
.subcommand( .subcommand(
@ -1035,10 +1068,18 @@ fn main() {
command_create_account(&config, token, account) command_create_account(&config, token, account)
} }
("assign", Some(arg_matches)) => { ("authorize", Some(arg_matches)) => {
let address = pubkey_of(arg_matches, "address").unwrap(); let address = pubkey_of(arg_matches, "address").unwrap();
let new_owner = pubkey_of(arg_matches, "new_owner").unwrap(); let authority_type = arg_matches.value_of("authority_type").unwrap();
command_assign(&config, address, new_owner) let authority_type = match authority_type {
"mint" => AuthorityType::MintTokens,
"freeze" => AuthorityType::FreezeAccount,
"owner" => AuthorityType::AccountOwner,
"close" => AuthorityType::CloseAccount,
_ => unreachable!(),
};
let new_authority = pubkey_of(arg_matches, "new_authority");
command_authorize(&config, address, authority_type, new_authority)
} }
("transfer", Some(arg_matches)) => { ("transfer", Some(arg_matches)) => {
let sender = pubkey_of(arg_matches, "sender").unwrap(); let sender = pubkey_of(arg_matches, "sender").unwrap();