2019-07-15 13:17:17 -06:00
|
|
|
use std::convert::TryFrom;
|
2019-03-19 15:19:50 -06:00
|
|
|
use std::error;
|
2018-09-26 17:55:36 -06:00
|
|
|
use std::fmt;
|
2019-03-08 18:29:08 -08:00
|
|
|
use std::mem;
|
|
|
|
use std::str::FromStr;
|
2018-09-26 17:55:36 -06:00
|
|
|
|
2019-07-30 16:22:20 -07:00
|
|
|
pub use bs58;
|
|
|
|
|
2019-07-15 13:17:17 -06:00
|
|
|
#[repr(transparent)]
|
2018-09-26 17:55:36 -06:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
2019-07-15 13:17:17 -06:00
|
|
|
pub struct Pubkey([u8; 32]);
|
2018-09-26 17:55:36 -06:00
|
|
|
|
2019-03-08 18:29:08 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ParsePubkeyError {
|
|
|
|
WrongSize,
|
|
|
|
Invalid,
|
|
|
|
}
|
|
|
|
|
2019-03-19 15:19:50 -06:00
|
|
|
impl fmt::Display for ParsePubkeyError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "ParsePubkeyError: {:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for ParsePubkeyError {}
|
|
|
|
|
2019-03-08 18:29:08 -08:00
|
|
|
impl FromStr for Pubkey {
|
|
|
|
type Err = ParsePubkeyError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let pubkey_vec = bs58::decode(s)
|
|
|
|
.into_vec()
|
|
|
|
.map_err(|_| ParsePubkeyError::Invalid)?;
|
|
|
|
if pubkey_vec.len() != mem::size_of::<Pubkey>() {
|
|
|
|
Err(ParsePubkeyError::WrongSize)
|
|
|
|
} else {
|
|
|
|
Ok(Pubkey::new(&pubkey_vec))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:55:36 -06:00
|
|
|
impl Pubkey {
|
|
|
|
pub fn new(pubkey_vec: &[u8]) -> Self {
|
2019-07-15 13:17:17 -06:00
|
|
|
Self(
|
|
|
|
<[u8; 32]>::try_from(<&[u8]>::clone(&pubkey_vec))
|
|
|
|
.expect("Slice must be the same length as a Pubkey"),
|
|
|
|
)
|
2018-09-26 17:55:36 -06:00
|
|
|
}
|
2019-03-30 21:37:33 -06:00
|
|
|
|
2019-12-03 20:55:18 -05:00
|
|
|
pub const fn new_from_array(pubkey_array: [u8; 32]) -> Self {
|
|
|
|
Self(pubkey_array)
|
|
|
|
}
|
|
|
|
|
2019-09-06 09:20:14 -07:00
|
|
|
#[cfg(not(feature = "program"))]
|
2019-03-30 21:37:33 -06:00
|
|
|
pub fn new_rand() -> Self {
|
|
|
|
Self::new(&rand::random::<[u8; 32]>())
|
|
|
|
}
|
2019-09-06 12:40:01 -07:00
|
|
|
|
|
|
|
pub fn log(&self) {
|
|
|
|
use crate::log::sol_log_64;
|
|
|
|
for (i, k) in self.0.iter().enumerate() {
|
|
|
|
sol_log_64(0, 0, 0, i as u64, u64::from(*k));
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 06:23:14 -07:00
|
|
|
pub fn to_bytes(self) -> [u8; 32] {
|
|
|
|
self.0
|
|
|
|
}
|
2018-09-26 17:55:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for Pubkey {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Pubkey {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", bs58::encode(self.0).into_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Pubkey {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", bs58::encode(self.0).into_string())
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 18:29:08 -08:00
|
|
|
|
2019-09-06 09:20:14 -07:00
|
|
|
#[cfg(not(feature = "program"))]
|
2019-11-06 11:18:25 -08:00
|
|
|
pub fn write_pubkey_file(outfile: &str, pubkey: Pubkey) -> Result<(), Box<dyn 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-03-19 15:19:50 -06:00
|
|
|
f.write_all(&serialized.clone().into_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-09-06 09:20:14 -07:00
|
|
|
#[cfg(not(feature = "program"))]
|
2019-11-06 11:18:25 -08:00
|
|
|
pub fn read_pubkey_file(infile: &str) -> Result<Pubkey, Box<dyn 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)?;
|
|
|
|
Ok(Pubkey::from_str(&printable)?)
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:29:08 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-19 15:19:50 -06:00
|
|
|
use std::fs::remove_file;
|
2019-03-08 18:29:08 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pubkey_fromstr() {
|
2019-03-30 21:37:33 -06:00
|
|
|
let pubkey = Pubkey::new_rand();
|
2019-03-08 18:29:08 -08:00
|
|
|
let mut pubkey_base58_str = bs58::encode(pubkey.0).into_string();
|
|
|
|
|
|
|
|
assert_eq!(pubkey_base58_str.parse::<Pubkey>(), Ok(pubkey));
|
|
|
|
|
|
|
|
pubkey_base58_str.push_str(&bs58::encode(pubkey.0).into_string());
|
|
|
|
assert_eq!(
|
|
|
|
pubkey_base58_str.parse::<Pubkey>(),
|
|
|
|
Err(ParsePubkeyError::WrongSize)
|
|
|
|
);
|
|
|
|
|
|
|
|
pubkey_base58_str.truncate(pubkey_base58_str.len() / 2);
|
|
|
|
assert_eq!(pubkey_base58_str.parse::<Pubkey>(), Ok(pubkey));
|
|
|
|
|
|
|
|
pubkey_base58_str.truncate(pubkey_base58_str.len() / 2);
|
|
|
|
assert_eq!(
|
|
|
|
pubkey_base58_str.parse::<Pubkey>(),
|
|
|
|
Err(ParsePubkeyError::WrongSize)
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut pubkey_base58_str = bs58::encode(pubkey.0).into_string();
|
|
|
|
assert_eq!(pubkey_base58_str.parse::<Pubkey>(), Ok(pubkey));
|
|
|
|
|
|
|
|
// throw some non-base58 stuff in there
|
|
|
|
pubkey_base58_str.replace_range(..1, "I");
|
|
|
|
assert_eq!(
|
|
|
|
pubkey_base58_str.parse::<Pubkey>(),
|
|
|
|
Err(ParsePubkeyError::Invalid)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-19 15:19:50 -06:00
|
|
|
#[test]
|
2019-07-14 13:37:55 -07:00
|
|
|
fn test_read_write_pubkey() -> Result<(), Box<dyn error::Error>> {
|
2019-03-19 15:19:50 -06:00
|
|
|
let filename = "test_pubkey.json";
|
2019-03-30 21:37:33 -06:00
|
|
|
let pubkey = 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
|
|
|
}
|