-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f616231
commit 09ad0d9
Showing
3 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |