diff --git a/Cargo.lock b/Cargo.lock index d9fc78c2b7ca74..7492ea20dc02f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6876,6 +6876,7 @@ dependencies = [ "clap 3.2.23", "dirs-next", "num_cpus", + "serde_json", "solana-clap-v3-utils", "solana-cli-config", "solana-derivation-path", diff --git a/keygen/Cargo.toml b/keygen/Cargo.toml index 4dd74305996883..93c5e4f2e4f064 100644 --- a/keygen/Cargo.toml +++ b/keygen/Cargo.toml @@ -14,6 +14,7 @@ bs58 = { workspace = true } clap = { version = "3.1.5", features = ["cargo"] } dirs-next = { workspace = true } num_cpus = { workspace = true } +serde_json = { workspace = true } solana-clap-v3-utils = { workspace = true } solana-cli-config = { workspace = true } solana-derivation-path = { workspace = true } diff --git a/keygen/src/keygen.rs b/keygen/src/keygen.rs index cbccb33e9a9095..6f0b9ff93adc18 100644 --- a/keygen/src/keygen.rs +++ b/keygen/src/keygen.rs @@ -30,7 +30,7 @@ use { solana_sdk::{ instruction::{AccountMeta, Instruction}, message::Message, - pubkey::{write_pubkey_file, Pubkey}, + pubkey::Pubkey, signature::{ keypair_from_seed, keypair_from_seed_and_derivation_path, write_keypair, write_keypair_file, Keypair, Signer, @@ -424,6 +424,21 @@ fn app<'a>(num_threads: &'a str, crate_version: &'a str) -> Command<'a> { ) } +fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box> { + use std::io::Write; + + let printable = format!("{pubkey}"); + let serialized = serde_json::to_string(&printable)?; + + 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(()) +} + fn main() -> Result<(), Box> { let default_num_threads = num_cpus::get().to_string(); let matches = app(&default_num_threads, solana_version::version!()) @@ -768,6 +783,14 @@ mod tests { tempfile::{tempdir, TempDir}, }; + fn read_pubkey_file(infile: &str) -> Result> { + let f = std::fs::File::open(infile)?; + let printable: String = serde_json::from_reader(f)?; + + use std::str::FromStr; + Ok(Pubkey::from_str(&printable)?) + } + fn process_test_command(args: &[&str]) -> Result<(), Box> { let default_num_threads = num_cpus::get().to_string(); let solana_version = solana_version::version!(); @@ -919,7 +942,7 @@ mod tests { ]) .unwrap(); - let result_pubkey = solana_sdk::pubkey::read_pubkey_file(&outfile_path).unwrap(); + let result_pubkey = read_pubkey_file(&outfile_path).unwrap(); assert_eq!(result_pubkey, expected_pubkey); } @@ -938,7 +961,7 @@ mod tests { ]) .unwrap(); - let result_pubkey = solana_sdk::pubkey::read_pubkey_file(&outfile_path).unwrap(); + let result_pubkey = read_pubkey_file(&outfile_path).unwrap(); assert_eq!(result_pubkey, expected_pubkey); } @@ -962,7 +985,7 @@ mod tests { ]) .unwrap(); - let result_pubkey = solana_sdk::pubkey::read_pubkey_file(&outfile_path).unwrap(); + let result_pubkey = read_pubkey_file(&outfile_path).unwrap(); assert_eq!(result_pubkey, expected_pubkey); } @@ -1129,4 +1152,15 @@ mod tests { ]) .unwrap(); } + + #[test] + fn test_read_write_pubkey() -> Result<(), std::boxed::Box> { + let filename = "test_pubkey.json"; + let pubkey = solana_sdk::pubkey::new_rand(); + write_pubkey_file(filename, pubkey)?; + let read = read_pubkey_file(filename)?; + assert_eq!(read, pubkey); + std::fs::remove_file(filename)?; + Ok(()) + } } diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index a6a832fbbd3621..e16c7d4a3a926c 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -5828,6 +5828,7 @@ dependencies = [ "getrandom 0.2.10", "js-sys", "num-traits", + "rand 0.8.5", "serde", "serde_derive", "solana-atomic-u64", diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 10eb2b0f8a6c8a..938e21a2aed8c0 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -33,6 +33,7 @@ full = [ "libsecp256k1", "sha3", "digest", + "solana-pubkey/rand", ] borsh = ["dep:borsh", "solana-program/borsh", "solana-secp256k1-recover/borsh"] dev-context-only-utils = ["qualifier_attr", "solana-account/dev-context-only-utils"] @@ -97,7 +98,7 @@ solana-frozen-abi-macro = { workspace = true, optional = true, features = [ ] } solana-program = { workspace = true } solana-program-memory = { workspace = true } -solana-pubkey = { workspace = true } +solana-pubkey = { workspace = true, default-features = false, features = ["std"] } solana-sanitize = { workspace = true } solana-sdk-macro = { workspace = true } solana-secp256k1-recover = { workspace = true } diff --git a/sdk/pubkey/Cargo.toml b/sdk/pubkey/Cargo.toml index 50d7a5ed6bdef7..cdafef8a939f15 100644 --- a/sdk/pubkey/Cargo.toml +++ b/sdk/pubkey/Cargo.toml @@ -18,6 +18,7 @@ bytemuck = { workspace = true, optional = true } bytemuck_derive = { workspace = true, optional = true } five8_const = { workspace = true } num-traits = { workspace = true } +rand = { workspace = true, optional = true } serde = { workspace = true, optional = true } serde_derive = { workspace = true, optional = true } solana-atomic-u64 = { workspace = true } @@ -47,7 +48,6 @@ wasm-bindgen = { workspace = true } anyhow = { workspace = true } arbitrary = { workspace = true, features = ["derive"] } bs58 = { workspace = true, features = ["alloc"] } -rand = { workspace = true } # circular dev deps need to be path deps for `cargo publish` to be happy, # and for now the doc tests need solana-program solana-program = { path = "../program" } @@ -65,11 +65,12 @@ borsh = ["dep:borsh", "dep:borsh0-10", "std"] bytemuck = ["dep:bytemuck", "dep:bytemuck_derive"] curve25519 = ["dep:curve25519-dalek", "sha2"] default = ["std"] -dev-context-only-utils = ["dep:arbitrary", "std"] +dev-context-only-utils = ["dep:arbitrary", "rand"] frozen-abi = [ "dep:solana-frozen-abi", "dep:solana-frozen-abi-macro" ] +rand = ["dep:rand", "std"] serde = ["dep:serde", "dep:serde_derive"] sha2 = ["dep:solana-sha256-hasher", "solana-sha256-hasher/sha2"] std = [] diff --git a/sdk/pubkey/src/lib.rs b/sdk/pubkey/src/lib.rs index e378c44caf3f06..d603d6a97b3b9b 100644 --- a/sdk/pubkey/src/lib.rs +++ b/sdk/pubkey/src/lib.rs @@ -1114,6 +1114,12 @@ macro_rules! pubkey { }; } +/// 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]>()) +} + #[cfg(test)] mod tests { use {super::*, strum::IntoEnumIterator}; diff --git a/sdk/src/pubkey.rs b/sdk/src/pubkey.rs index 92d1365d03c5bf..344f0698444cb2 100644 --- a/sdk/src/pubkey.rs +++ b/sdk/src/pubkey.rs @@ -1,13 +1,13 @@ -//! Solana account addresses. - -pub use solana_program::pubkey::*; - -/// New random Pubkey for tests and benchmarks. #[cfg(feature = "full")] -pub fn new_rand() -> Pubkey { - Pubkey::from(rand::random::<[u8; PUBKEY_BYTES]>()) -} - +pub use solana_pubkey::new_rand; +#[cfg(target_os = "solana")] +pub use solana_pubkey::syscalls; +pub use solana_pubkey::{ + bytes_are_curve_point, ParsePubkeyError, Pubkey, PubkeyError, MAX_SEEDS, MAX_SEED_LEN, + PUBKEY_BYTES, +}; + +#[deprecated(since = "2.1.0")] #[cfg(feature = "full")] pub fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box> { use std::io::Write; @@ -24,6 +24,7 @@ pub fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box Result> { let f = std::fs::File::open(infile)?; @@ -32,19 +33,3 @@ pub fn read_pubkey_file(infile: &str) -> Result Result<(), Box> { - let filename = "test_pubkey.json"; - let pubkey = solana_sdk::pubkey::new_rand(); - write_pubkey_file(filename, pubkey)?; - let read = read_pubkey_file(filename)?; - assert_eq!(read, pubkey); - remove_file(filename)?; - Ok(()) - } -}