2020-10-23 17:22:10 -07:00
|
|
|
pub use solana_program::pubkey::*;
|
2019-03-08 18:29:08 -08:00
|
|
|
|
2020-10-19 11:28:42 -07:00
|
|
|
/// New random Pubkey for tests and benchmarks.
|
2020-10-24 08:39:28 -07:00
|
|
|
#[cfg(feature = "full")]
|
2020-10-19 11:28:42 -07:00
|
|
|
pub fn new_rand() -> Pubkey {
|
|
|
|
Pubkey::new(&rand::random::<[u8; 32]>())
|
|
|
|
}
|
|
|
|
|
2020-10-24 08:39:28 -07:00
|
|
|
#[cfg(feature = "full")]
|
2020-10-19 10:17:29 -07:00
|
|
|
pub fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box<dyn std::error::Error>> {
|
2019-09-06 09:20:14 -07:00
|
|
|
use std::io::Write;
|
|
|
|
|
2019-03-19 15:19:50 -06:00
|
|
|
let printable = format!("{}", pubkey);
|
|
|
|
let serialized = serde_json::to_string(&printable)?;
|
|
|
|
|
2019-09-06 09:20:14 -07:00
|
|
|
if let Some(outdir) = std::path::Path::new(&outfile).parent() {
|
|
|
|
std::fs::create_dir_all(outdir)?;
|
2019-03-19 15:19:50 -06:00
|
|
|
}
|
2019-09-06 09:20:14 -07:00
|
|
|
let mut f = std::fs::File::create(outfile)?;
|
2019-12-19 23:27:54 -08:00
|
|
|
f.write_all(&serialized.into_bytes())?;
|
2019-03-19 15:19:50 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-24 08:39:28 -07:00
|
|
|
#[cfg(feature = "full")]
|
2020-10-19 10:17:29 -07:00
|
|
|
pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, Box<dyn std::error::Error>> {
|
2019-09-06 09:20:14 -07:00
|
|
|
let f = std::fs::File::open(infile.to_string())?;
|
2019-03-19 15:19:50 -06:00
|
|
|
let printable: String = serde_json::from_reader(f)?;
|
2020-10-19 13:19:24 -07:00
|
|
|
|
|
|
|
use std::str::FromStr;
|
2019-03-19 15:19:50 -06:00
|
|
|
Ok(Pubkey::from_str(&printable)?)
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:29:08 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-10-19 13:19:24 -07:00
|
|
|
use std::fs::remove_file;
|
2020-08-05 16:35:54 -07:00
|
|
|
|
2019-03-19 15:19:50 -06:00
|
|
|
#[test]
|
2020-10-19 10:17:29 -07:00
|
|
|
fn test_read_write_pubkey() -> Result<(), Box<dyn std::error::Error>> {
|
2019-03-19 15:19:50 -06:00
|
|
|
let filename = "test_pubkey.json";
|
2020-10-19 12:12:08 -07:00
|
|
|
let pubkey = solana_sdk::pubkey::new_rand();
|
2019-11-06 11:18:25 -08:00
|
|
|
write_pubkey_file(filename, pubkey)?;
|
|
|
|
let read = read_pubkey_file(filename)?;
|
2019-03-19 15:19:50 -06:00
|
|
|
assert_eq!(read, pubkey);
|
|
|
|
remove_file(filename)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-03-08 18:29:08 -08:00
|
|
|
}
|