Skip to content

Commit

Permalink
hard-code elgamal keypair support
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Sep 24, 2023
1 parent f616231 commit 09ad0d9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions token/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version = "3.1.1"
walkdir = "2"

[dependencies]
base64 = "0.21.4"
clap = "2.33.3"
console = "0.15.7"
serde = "1.0.188"
Expand All @@ -26,12 +27,20 @@ solana-logger = "=1.16.13"
solana-remote-wallet = "=1.16.13"
solana-sdk = "=1.16.13"
solana-transaction-status = "=1.16.13"
spl-token = { version = "4.0", path="../program", features = [ "no-entrypoint" ] }
spl-token-2022 = { version = "0.8", path="../program-2022", features = [ "no-entrypoint" ] }
spl-token-client = { version = "0.6", path="../client" }
spl-token-metadata-interface = { version = "0.2", path="../../token-metadata/interface" }
spl-associated-token-account = { version = "2.0", path="../../associated-token-account/program", features = [ "no-entrypoint" ] }
spl-memo = { version = "4.0.0", path="../../memo/program", features = ["no-entrypoint"] }
spl-token = { version = "4.0", path = "../program", features = [
"no-entrypoint",
] }
spl-token-2022 = { version = "0.8", path = "../program-2022", features = [
"no-entrypoint",
] }
spl-token-client = { version = "0.6", path = "../client" }
spl-token-metadata-interface = { version = "0.2", path = "../../token-metadata/interface" }
spl-associated-token-account = { version = "2.0", path = "../../associated-token-account/program", features = [
"no-entrypoint",
] }
spl-memo = { version = "4.0.0", path = "../../memo/program", features = [
"no-entrypoint",
] }
strum = "0.25"
strum_macros = "0.25"
tokio = "1.14"
Expand Down
55 changes: 55 additions & 0 deletions token/cli/src/encryption_keypair.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use {
base64::{prelude::BASE64_STANDARD, Engine},
clap::ArgMatches,
spl_token_2022::solana_zk_token_sdk::{
encryption::elgamal::{ElGamalKeypair, ElGamalPubkey},
zk_token_elgamal::pod::ElGamalPubkey as PodElGamalPubkey,
},
};

const ELGAMAL_PUBKEY_MAX_BASE64_LEN: usize = 44;

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

pub(crate) fn elgamal_pubkey_of(
matches: &ArgMatches,
name: &str,
) -> Result<PodElGamalPubkey, String> {
if let Ok(keypair) = elgamal_keypair_of(matches, name) {
let elgamal_pubkey = (*keypair.pubkey()).into();
Ok(elgamal_pubkey)
} else {
let arg_str = matches.value_of(name).unwrap();
if let Some(pubkey) = elgamal_pubkey_from_str(arg_str) {
Ok(pubkey)
} else {
Err("failed to read ElGamal pubkey".to_string())
}
}
}

pub(crate) fn elgamal_keypair_of(
matches: &ArgMatches,
name: &str,
) -> Result<ElGamalKeypair, String> {
let path = matches.value_of(name).unwrap();
ElGamalKeypair::read_json_file(path).map_err(|e| e.to_string())
}

fn elgamal_pubkey_from_str(s: &str) -> Option<PodElGamalPubkey> {
if s.len() > ELGAMAL_PUBKEY_MAX_BASE64_LEN {
return None;
}
let pubkey_vec = BASE64_STANDARD.decode(s).ok()?;
let elgamal_pubkey = ElGamalPubkey::from_bytes(&pubkey_vec)?;
Some(elgamal_pubkey.into())
}

0 comments on commit 09ad0d9

Please sign in to comment.