-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move solana_sdk::pubkey
functions to solana-pubkey
#2980
Conversation
2f95a37
to
39c2c85
Compare
39c2c85
to
0a6762a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple of comments of where to put some of these changes, let me know what you think!
sdk/pubkey/src/lib.rs
Outdated
#[cfg(all(feature = "std", not(target_os = "solana")))] | ||
pub fn write_pubkey_file( | ||
outfile: &str, | ||
pubkey: Pubkey, | ||
) -> Result<(), std::boxed::Box<dyn std::error::Error>> { | ||
use std::io::Write; | ||
let serialized = std::format!("\"{pubkey}\""); | ||
|
||
if let Some(outdir) = std::path::Path::new(&outfile).parent() { | ||
std::fs::create_dir_all(outdir)?; | ||
} | ||
let mut f = std::fs::File::create(outfile)?; | ||
f.write_all(&serialized.into_bytes())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[cfg(all(feature = "std", not(target_os = "solana")))] | ||
pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, std::boxed::Box<dyn std::error::Error>> { | ||
use std::io::Read; | ||
let mut f = std::fs::File::open(infile)?; | ||
let mut buffer = std::string::String::new(); | ||
f.read_to_string(&mut buffer)?; | ||
let trimmed = buffer.trim(); | ||
if !trimmed.starts_with('"') || !trimmed.ends_with('"') { | ||
return Err(std::io::Error::new( | ||
std::io::ErrorKind::InvalidData, | ||
"Input must be a JSON string.", | ||
) | ||
.into()); | ||
} | ||
let contents = &trimmed[1..trimmed.len() - 1]; | ||
Ok(Pubkey::from_str(contents)?) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering these are only used by solana-keygen, how about copying the implementations into there and deprecating them from sdk
rather than moving them down into here?
/// New random Pubkey for tests and benchmarks. | ||
#[cfg(all(feature = "rand", not(target_os = "solana")))] | ||
pub fn new_rand() -> Pubkey { | ||
Pubkey::from(rand::random::<[u8; PUBKEY_BYTES]>()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit torn about this one, since we should really favor using Pubkey::new_unique()
in almost all cases. On the flipside, there are some legit usecases, and this is the best place to put it, so let's go with it.
…icate in solana-keygen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks!
* move functions from solana_sdk::pubkey to solana_pubkey * fix test * remove serde_json from write_pubkey_file * remove serde_json * fix import * put new_rand behind a separate feature * lint * fix imports * fmt * deprecate sdk::pubkey::{read_pubkey_file, write_pubkey_file} and duplicate in solana-keygen * lint
Problem
solana_sdk::pubkey
defines three helper functions. These would be useful to have outside solana-sdk. I can't think of a anywhere else to move them so I put them insolana-pubkey
behind anot(target_os = "solana")
.Summary of Changes
solana-pubkey
and re-export insolana_sdk::pubkey
for backwards compatibilityserde_json
usage in these functions with a simple inline parser