Skip to content
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

use five8 in pubkey, signature, hash and keypair crates #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ digest = "0.10.7"
ed25519-dalek = "=1.0.1"
ed25519-dalek-bip32 = "0.2.0"
env_logger = "0.9.3"
five8 = "0.2.1"
five8_const = "0.1.3"
getrandom = "0.2.10"
hex = "0.4.3"
Expand Down
3 changes: 2 additions & 1 deletion hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ rustdoc-args = ["--cfg=docsrs"]

[dependencies]
borsh = { workspace = true, optional = true }
bs58 = { workspace = true, default-features = false }
bytemuck = { workspace = true, optional = true }
bytemuck_derive = { workspace = true, optional = true }
five8 = { workspace = true }
serde = { workspace = true, optional = true }
serde_derive = { workspace = true, optional = true }
solana-atomic-u64 = { workspace = true }
Expand All @@ -31,6 +31,7 @@ solana-frozen-abi-macro = { workspace = true, optional = true, features = [
solana-sanitize = { workspace = true }

[dev-dependencies]
bs58 = { workspace = true, default-features = false }
solana-hash = { path = ".", features = ["dev-context-only-utils"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
29 changes: 14 additions & 15 deletions hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use std::string::ToString;
use {
core::{
convert::TryFrom,
fmt, mem,
str::{from_utf8, FromStr},
fmt,
str::{from_utf8_unchecked, FromStr},
},
solana_sanitize::Sanitize,
};
Expand Down Expand Up @@ -68,11 +68,9 @@ impl AsRef<[u8]> for Hash {

fn write_as_base58(f: &mut fmt::Formatter, h: &Hash) -> fmt::Result {
let mut out = [0u8; MAX_BASE58_LEN];
let out_slice: &mut [u8] = &mut out;
// This will never fail because the only possible error is BufferTooSmall,
// and we will never call it with too small a buffer.
let len = bs58::encode(h.0).onto(out_slice).unwrap();
let as_str = from_utf8(&out[..len]).unwrap();
let len = five8::encode_32(&h.0, &mut out) as usize;
// any sequence of base58 chars is valid utf8
let as_str = unsafe { from_utf8_unchecked(&out[..len]) };
f.write_str(as_str)
}

Expand Down Expand Up @@ -110,18 +108,19 @@ impl FromStr for Hash {
type Err = ParseHashError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
use five8::DecodeError;
if s.len() > MAX_BASE58_LEN {
return Err(ParseHashError::WrongSize);
}
let mut bytes = [0; HASH_BYTES];
let decoded_size = bs58::decode(s)
.onto(&mut bytes)
.map_err(|_| ParseHashError::Invalid)?;
if decoded_size != mem::size_of::<Hash>() {
Err(ParseHashError::WrongSize)
} else {
Ok(bytes.into())
}
five8::decode_32(s, &mut bytes).map_err(|e| match e {
DecodeError::InvalidChar(_) => ParseHashError::Invalid,
DecodeError::TooLong
| DecodeError::TooShort
| DecodeError::LargestTermTooHigh
| DecodeError::OutputTooLong => ParseHashError::WrongSize,
})?;
Ok(Self::from(bytes))
}
}

Expand Down
2 changes: 1 addition & 1 deletion keypair/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
bs58 = { workspace = true, features = ["std"] }
ed25519-dalek = { workspace = true }
ed25519-dalek-bip32 = { workspace = true, optional = true }
five8 = { workspace = true }
rand0-7 = { workspace = true }
solana-derivation-path = { workspace = true, optional = true }
solana-pubkey = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions keypair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,15 @@ impl Keypair {
/// Recovers a `Keypair` from a base58-encoded string
pub fn from_base58_string(s: &str) -> Self {
let mut buf = [0u8; ed25519_dalek::KEYPAIR_LENGTH];
bs58::decode(s).onto(&mut buf).unwrap();
five8::decode_64(s, &mut buf).unwrap();
Self::from_bytes(&buf).unwrap()
}

/// Returns this `Keypair` as a base58-encoded string
pub fn to_base58_string(&self) -> String {
bs58::encode(&self.0.to_bytes()).into_string()
let mut out = [0u8; five8::BASE58_ENCODED_64_MAX_LEN];
let len = five8::encode_64(&self.0.to_bytes(), &mut out);
out[..len as usize].iter().map(|c| *c as char).collect()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use from_uft8_unchecked too?

}

/// Gets this `Keypair`'s SecretKey
Expand Down
2 changes: 1 addition & 1 deletion pubkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ edition = { workspace = true }
arbitrary = { workspace = true, features = ["derive"], optional = true }
borsh = { workspace = true, optional = true }
borsh0-10 = { package = "borsh", version = "0.10.3", optional = true }
bs58 = { workspace = true }
bytemuck = { workspace = true, optional = true }
bytemuck_derive = { workspace = true, optional = true }
five8 = { workspace = true }
five8_const = { workspace = true }
num-traits = { workspace = true }
rand = { workspace = true, optional = true }
Expand Down
31 changes: 15 additions & 16 deletions pubkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use {
core::{
array,
convert::{Infallible, TryFrom},
fmt, mem,
str::{from_utf8, FromStr},
fmt,
str::{from_utf8_unchecked, FromStr},
},
num_traits::{FromPrimitive, ToPrimitive},
solana_decode_error::DecodeError,
Expand Down Expand Up @@ -233,18 +233,19 @@ impl FromStr for Pubkey {
type Err = ParsePubkeyError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
use five8::DecodeError;
if s.len() > MAX_BASE58_LEN {
return Err(ParsePubkeyError::WrongSize);
}
let mut bytes = [0; PUBKEY_BYTES];
let decoded_size = bs58::decode(s)
.onto(&mut bytes)
.map_err(|_| ParsePubkeyError::Invalid)?;
if decoded_size != mem::size_of::<Pubkey>() {
Err(ParsePubkeyError::WrongSize)
} else {
Ok(Pubkey(bytes))
}
five8::decode_32(s, &mut bytes).map_err(|e| match e {
DecodeError::InvalidChar(_) => ParsePubkeyError::Invalid,
DecodeError::TooLong
| DecodeError::TooShort
| DecodeError::LargestTermTooHigh
| DecodeError::OutputTooLong => ParsePubkeyError::WrongSize,
})?;
Ok(Pubkey(bytes))
}
}

Expand Down Expand Up @@ -811,11 +812,9 @@ impl AsMut<[u8]> for Pubkey {

fn write_as_base58(f: &mut fmt::Formatter, p: &Pubkey) -> fmt::Result {
let mut out = [0u8; MAX_BASE58_LEN];
let out_slice: &mut [u8] = &mut out;
// This will never fail because the only possible error is BufferTooSmall,
// and we will never call it with too small a buffer.
let len = bs58::encode(p.0).onto(out_slice).unwrap();
let as_str = from_utf8(&out[..len]).unwrap();
let len = five8::encode_32(&p.0, &mut out) as usize;
// any sequence of base58 chars is valid utf8
let as_str = unsafe { from_utf8_unchecked(&out[..len]) };
f.write_str(as_str)
}

Expand Down Expand Up @@ -1128,7 +1127,7 @@ pub fn new_rand() -> Pubkey {

#[cfg(test)]
mod tests {
use {super::*, strum::IntoEnumIterator};
use {super::*, core::str::from_utf8, strum::IntoEnumIterator};

#[test]
fn test_new_unique() {
Expand Down
2 changes: 1 addition & 1 deletion signature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ license = { workspace = true }
edition = { workspace = true }

[dependencies]
bs58 = { workspace = true }
ed25519-dalek = { workspace = true, optional = true }
five8 = { workspace = true }
rand = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
serde-big-array = { workspace = true, optional = true }
Expand Down
27 changes: 13 additions & 14 deletions signature/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use core::convert::TryInto;
use core::{
fmt,
str::{from_utf8, FromStr},
str::{from_utf8_unchecked, FromStr},
};
#[cfg(feature = "std")]
extern crate std;
Expand Down Expand Up @@ -71,11 +71,9 @@ impl AsRef<[u8]> for Signature {

fn write_as_base58(f: &mut fmt::Formatter, s: &Signature) -> fmt::Result {
let mut out = [0u8; MAX_BASE58_SIGNATURE_LEN];
let out_slice: &mut [u8] = &mut out;
// This will never fail because the only possible error is BufferTooSmall,
// and we will never call it with too small a buffer.
let len = bs58::encode(s.0).onto(out_slice).unwrap();
let as_str = from_utf8(&out[..len]).unwrap();
let len = five8::encode_64(&s.0, &mut out) as usize;
// any sequence of base58 chars is valid utf8
let as_str = unsafe { from_utf8_unchecked(&out[..len]) };
f.write_str(as_str)
}

Expand Down Expand Up @@ -147,18 +145,19 @@ impl FromStr for Signature {
type Err = ParseSignatureError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
use five8::DecodeError;
if s.len() > MAX_BASE58_SIGNATURE_LEN {
return Err(ParseSignatureError::WrongSize);
}
let mut bytes = [0; SIGNATURE_BYTES];
let decoded_size = bs58::decode(s)
.onto(&mut bytes)
.map_err(|_| ParseSignatureError::Invalid)?;
if decoded_size != SIGNATURE_BYTES {
Err(ParseSignatureError::WrongSize)
} else {
Ok(bytes.into())
}
five8::decode_64(s, &mut bytes).map_err(|e| match e {
DecodeError::InvalidChar(_) => ParseSignatureError::Invalid,
DecodeError::TooLong
| DecodeError::TooShort
| DecodeError::LargestTermTooHigh
| DecodeError::OutputTooLong => ParseSignatureError::WrongSize,
})?;
Ok(Self::from(bytes))
}
}

Expand Down
Loading