Skip to content

Commit

Permalink
make confidential transfer mint fields to optional when updating
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Sep 27, 2023
1 parent fee6600 commit dd18895
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 34 deletions.
22 changes: 19 additions & 3 deletions token/cli/src/encryption_keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ use {

const ELGAMAL_PUBKEY_MAX_BASE64_LEN: usize = 44;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ElGamalPubkeyOrNone {
ElGamalPubkey(PodElGamalPubkey),
None,
}

impl Into<Option<PodElGamalPubkey>> for ElGamalPubkeyOrNone {

Check failure on line 22 in token/cli/src/encryption_keypair.rs

View workflow job for this annotation

GitHub Actions / clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> Option<PodElGamalPubkey> {
match self {
ElGamalPubkeyOrNone::ElGamalPubkey(pubkey) => Some(pubkey),
ElGamalPubkeyOrNone::None => None,
}
}
}

pub(crate) fn elgamal_pubkey_or_none(
matches: &ArgMatches,
name: &str,
) -> Result<Option<PodElGamalPubkey>, String> {
) -> Result<ElGamalPubkeyOrNone, String> {
let arg_str = matches.value_of(name).unwrap();
if arg_str == "none" {
return Ok(None);
return Ok(ElGamalPubkeyOrNone::None);
}
elgamal_pubkey_of(matches, name).map(Some)
let elgamal_pubkey = elgamal_pubkey_of(matches, name)?;
Ok(ElGamalPubkeyOrNone::ElGamalPubkey(elgamal_pubkey))
}

pub(crate) fn elgamal_pubkey_of(
Expand Down
93 changes: 62 additions & 31 deletions token/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::integer_arithmetic)]
use clap::{
crate_description, crate_name, crate_version, value_t, value_t_or_exit, App, AppSettings, Arg,
ArgMatches, SubCommand,
ArgGroup, ArgMatches, SubCommand,
};
use serde::Serialize;
use solana_account_decoder::{
Expand Down Expand Up @@ -2752,11 +2752,11 @@ async fn command_update_confidential_transfer_settings(
config: &Config<'_>,
token_pubkey: Pubkey,
authority: Pubkey,
auto_approve: bool,
auditor_pubkey: Option<ElGamalPubkey>,
auto_approve: Option<bool>,
auditor_pubkey: Option<ElGamalPubkeyOrNone>,
bulk_signers: Vec<Arc<dyn Signer>>,
) -> CommandResult {
if !config.sign_only {
let (new_auto_approve, new_auditor_pubkey) = if !config.sign_only {
let confidential_transfer_account = config.get_account_checked(&token_pubkey).await?;

let mint_state =
Expand All @@ -2779,14 +2779,35 @@ async fn command_update_confidential_transfer_settings(
)
.into());
}

let current_auto_approve = if let Some(auto_approve) = auto_approve {
auto_approve
} else {
bool::from(confidential_transfer_mint.auto_approve_new_accounts)
};

let current_auditor_pubkey = if let Some(auditor_pubkey) = auditor_pubkey {
auditor_pubkey.into()
} else {
Option::<ElGamalPubkey>::from(confidential_transfer_mint.auditor_elgamal_pubkey)
};

(current_auto_approve, current_auditor_pubkey)
} else {
return Err(format!(
"Mint {} does not support confidential transfers",
token_pubkey
)
.into());
}
}
} else {
let new_auto_approve = auto_approve.expect("The approve policy must be provided");
let new_auditor_pubkey = auditor_pubkey
.expect("The auditor encryption pubkey must be provided")
.into();

(new_auto_approve, new_auditor_pubkey)
};

println_display(
config,
Expand All @@ -2796,29 +2817,38 @@ async fn command_update_confidential_transfer_settings(
),
);

println_display(
config,
format!(
" approve policy set to {}",
if auto_approve { "auto" } else { "manual" }
),
);

if let Some(auditor_pubkey) = auditor_pubkey {
if auto_approve.is_some() {
println_display(
config,
format!(
" auditor encryption pubkey set to {}",
auditor_pubkey.to_string(),
" approve policy set to {}",
if new_auto_approve { "auto" } else { "manual" }
),
);
} else {
println_display(config, format!(" auditability disabled",))
}

if auditor_pubkey.is_some() {
if let Some(new_auditor_pubkey) = new_auditor_pubkey {
println_display(
config,
format!(
" auditor encryption pubkey set to {}",
new_auditor_pubkey.to_string(),
),
);
} else {
println_display(config, format!(" auditability disabled",))
}
}

let token = token_client_from_config(config, &token_pubkey, None)?;
let res = token
.confidential_transfer_update_mint(&authority, auto_approve, auditor_pubkey, &bulk_signers)
.confidential_transfer_update_mint(
&authority,
new_auto_approve,
new_auditor_pubkey,
&bulk_signers,
)
.await?;

let tx_return = finish_tx(config, &res, false).await?;
Expand Down Expand Up @@ -4363,7 +4393,6 @@ fn app<'a, 'b>(
.long("approve-policy")
.value_name("APPROVE_POLICY")
.takes_value(true)
.index(2)
.possible_values(&["auto", "manual"])
.help(
"Policy for enabling accounts to make confidential transfers. If \"auto\" \
Expand All @@ -4378,7 +4407,6 @@ fn app<'a, 'b>(
.long("auditor-pubkey")
.value_name("AUDITOR_PUBKEY")
.takes_value(true)
.index(3)
.help(
"The auditor encryption public key. The corresponding private key for \
this auditor public key can be used to decrypt all confidential \
Expand All @@ -4389,6 +4417,10 @@ fn app<'a, 'b>(
feature for the token, use \"none\"."
)
)
.group(
ArgGroup::with_name("update_fields").args(&["approve_policy", "auditor_pubkey"])
.required(true)
)
.arg(
Arg::with_name("confidential_transfer_authority")
.long("confidential-transfer-authority")
Expand Down Expand Up @@ -5218,12 +5250,13 @@ async fn process_command<'a>(
.unwrap()
.unwrap();

let auto_approve = arg_matches
.value_of("approve_policy")
.map(|b| b == "auto")
.unwrap();
let auditor_encryption_pubkey =
elgamal_pubkey_or_none(arg_matches, "auditor_pubkey").unwrap();
let auto_approve = arg_matches.value_of("approve_policy").map(|b| b == "auto");

let auditor_encryption_pubkey = if arg_matches.is_present("auditor_pubkey") {
Some(elgamal_pubkey_or_none(arg_matches, "auditor_pubkey")?)
} else {
None
};

let (authority_signer, authority_pubkey) = config.signer_or_default(
arg_matches,
Expand Down Expand Up @@ -7830,14 +7863,12 @@ mod tests {
let auditor_pubkey: ElGamalPubkey = (*auditor_keypair.pubkey()).into();
let new_auto_approve = false;

println!("{}", auditor_keypair.pubkey());

command_update_confidential_transfer_settings(
&config,
token_pubkey,
confidential_transfer_mint_authority,
new_auto_approve,
Some(auditor_pubkey), // auditor pubkey
Some(new_auto_approve),
Some(ElGamalPubkeyOrNone::ElGamalPubkey(auditor_pubkey)), // auditor pubkey
bulk_signers.clone(),
)
.await
Expand Down

0 comments on commit dd18895

Please sign in to comment.