diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a1941bb71..6334e7b8a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). parameterized by the lifetime of the input byte slice. - Altered hashes to implement `AsRef<[u8]>` instead of `AsRef>`. - Renamed `hash::Signature` to `hash::UnknownSignature`. +- All hash structs no longer publicly expose the underlying `Hash`. Instead, use `Into>` and `AsRef<[u8]>`. +- `ToBase58Check` no longer returns a `Result` type. ### Deprecated @@ -25,6 +27,7 @@ parameterized by the lifetime of the input byte slice. ### Removed - Removed legacy `SecretKeyEd25519` encoding. +- Removed `ToBase58CheckError`. ### Fixed diff --git a/crypto/src/base58.rs b/crypto/src/base58.rs index 1efe832934..e5470b18ff 100644 --- a/crypto/src/base58.rs +++ b/crypto/src/base58.rs @@ -24,14 +24,6 @@ pub enum FromBase58CheckError { IncorrectBase58Prefix, } -/// Possible errors for ToBase58Check -#[derive(Debug, Error)] -pub enum ToBase58CheckError { - /// Data is too long - #[error("data too long")] - DataTooLong, -} - /// Create double hash of given binary data fn double_sha256(data: &[u8]) -> [u8; 32] { let digest = sha256(data); @@ -41,7 +33,7 @@ fn double_sha256(data: &[u8]) -> [u8; 32] { /// A trait for converting a value to base58 encoded string. pub trait ToBase58Check { /// Converts a value of `self` to a base58 value, returning the owned string. - fn to_base58check(&self) -> Result; + fn to_base58check(&self) -> String; } /// A trait for converting base58check encoded values. @@ -55,14 +47,14 @@ pub trait FromBase58Check { } impl ToBase58Check for [u8] { - fn to_base58check(&self) -> Result { + fn to_base58check(&self) -> String { // 4 bytes checksum let mut payload = Vec::with_capacity(self.len() + 4); payload.extend(self); let checksum = double_sha256(self); payload.extend(&checksum[..4]); - Ok(bs58::encode(payload).into_string()) + bs58::encode(payload).into_string() } } @@ -99,7 +91,7 @@ mod tests { #[test] fn test_encode() -> Result<(), anyhow::Error> { - let decoded = hex::decode("8eceda2f")?.to_base58check().unwrap(); + let decoded = hex::decode("8eceda2f")?.to_base58check(); let expected = "QtRAcc9FSRg"; assert_eq!(expected, &decoded); diff --git a/crypto/src/hash.rs b/crypto/src/hash.rs index 42a7dac383..7adf2c9ab6 100644 --- a/crypto/src/hash.rs +++ b/crypto/src/hash.rs @@ -86,7 +86,7 @@ pub enum FromBytesError { macro_rules! define_hash { ($name:ident) => { #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct $name(pub Hash); + pub struct $name(pub(crate) Hash); impl $name { fn from_bytes(data: &[u8]) -> Result { @@ -473,12 +473,8 @@ impl HashType { hash.extend(self.base58check_prefix()); } hash.extend(data); - hash.to_base58check() - // currently the error is returned if the input lenght exceeds 128 bytes - // that is the limitation of base58 crate, and there are no hash types - // exceeding that limit. - // TODO: remove this when TE-373 is implemented - .map_err(|_| unreachable!("Hash size should not exceed allowed 128 bytes")) + + Ok(hash.to_base58check()) } } diff --git a/crypto/src/signature.rs b/crypto/src/signature.rs index 4866e662c9..915b8bcfee 100644 --- a/crypto/src/signature.rs +++ b/crypto/src/signature.rs @@ -11,8 +11,8 @@ use crate::base58::FromBase58CheckError; use crate::hash::{ - BlsSignature, Ed25519Signature, FromBytesError, HashTrait, HashType, P256Signature, - Secp256k1Signature, UnknownSignature, + BlsSignature, Ed25519Signature, FromBytesError, HashTrait, P256Signature, Secp256k1Signature, + UnknownSignature, }; use serde::{Deserialize, Serialize}; @@ -57,11 +57,11 @@ impl Signature { impl AsRef<[u8]> for Signature { fn as_ref(&self) -> &[u8] { match self { - Self::Ed25519(s) => s.0.as_ref(), - Self::Secp256k1(s) => s.0.as_ref(), - Self::P256(s) => s.0.as_ref(), - Self::Bls(s) => s.0.as_ref(), - Self::Unknown(s) => s.0.as_ref(), + Self::Ed25519(s) => s.as_ref(), + Self::Secp256k1(s) => s.as_ref(), + Self::P256(s) => s.as_ref(), + Self::Bls(s) => s.as_ref(), + Self::Unknown(s) => s.as_ref(), } } } @@ -69,11 +69,11 @@ impl AsRef<[u8]> for Signature { impl From for Vec { fn from(s: Signature) -> Self { match s { - Signature::Ed25519(s) => s.0, - Signature::Secp256k1(s) => s.0, - Signature::P256(s) => s.0, - Signature::Bls(s) => s.0, - Signature::Unknown(s) => s.0, + Signature::Ed25519(s) => s.into(), + Signature::Secp256k1(s) => s.into(), + Signature::P256(s) => s.into(), + Signature::Bls(s) => s.into(), + Signature::Unknown(s) => s.into(), } } } @@ -82,12 +82,10 @@ impl TryFrom> for Signature { type Error = FromBytesError; fn try_from(hash: Vec) -> Result { - if hash.len() == HashType::BlsSignature.size() { - Ok(Signature::Bls(BlsSignature(hash))) - } else if hash.len() == HashType::UnknownSignature.size() { - Ok(Signature::Unknown(UnknownSignature(hash))) + if hash.len() == BlsSignature::hash_size() { + Ok(Signature::Bls(BlsSignature::try_from(hash)?)) } else { - Err(FromBytesError::InvalidSize) + Ok(Signature::Unknown(UnknownSignature::try_from(hash)?)) } } } @@ -96,12 +94,10 @@ impl TryFrom<&[u8]> for Signature { type Error = FromBytesError; fn try_from(hash: &[u8]) -> Result { - if hash.len() == HashType::BlsSignature.size() { - Ok(Signature::Bls(BlsSignature(hash.to_vec()))) - } else if hash.len() == HashType::UnknownSignature.size() { - Ok(Signature::Unknown(UnknownSignature(hash.to_vec()))) + if let Ok(s) = BlsSignature::try_from(hash) { + Ok(Signature::Bls(s)) } else { - Err(FromBytesError::InvalidSize) + Ok(Signature::Unknown(UnknownSignature::try_from(hash)?)) } } }