Skip to content

Commit

Permalink
crypto: Remove unused Error type from PublicKeyWithHash trait
Browse files Browse the repository at this point in the history
  • Loading branch information
lierdakil committed Jan 2, 2024
1 parent 7935bac commit f105e32
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ parameterized by the lifetime of the input byte slice.
never possible.
- `blake2b::merkle_tree` returns plain `Vec<u8>` instead of `Result<Vec<u8>,
Blake2bError>`, the error was never possible.
- `tezos_crypto_rs`: `PublicKeyWithHash::pk_hash` now returns `Self::Hash`
instead of `Result`.

### Deprecated

Expand All @@ -41,6 +43,7 @@ parameterized by the lifetime of the input byte slice.
- Removed impossible `TryFromPKError`.
- `tezos_data_encoding`: Removed unused `DecodeErrorKind::Hash` and
`DecodeError::hash_error`
- `tezos_crypto_rs`: Removed unused `Error` type from `PublicKeyWithHash`

### Fixed

Expand Down
6 changes: 2 additions & 4 deletions crypto/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ pub fn keypair_with_hash_from_ikm(
) -> Result<(SecretKeyBls, PublicKeyBls, ContractTz4Hash), CryptoError> {
let (sk, pk) = keypair_from_ikm(ikm)?;

let hash = pk
.pk_hash()
.map_err(|e| CryptoError::AlgorithmError(format!("BLS_PK_HASH: {:?}", e)))?;
let hash = pk.pk_hash();

Ok((sk, pk, hash))
}
Expand Down Expand Up @@ -447,7 +445,7 @@ mod tests {
.expect("pk_bytes should be 48 bytes long");
let pk = PublicKeyBls(pk_bytes.to_vec());

let tz4 = pk.pk_hash().expect("PublicKey hashable");
let tz4 = pk.pk_hash();

let expected_tz4 = ContractTz4Hash::from_b58check(tz4_b58).expect("Valid tz4 b58");

Expand Down
8 changes: 3 additions & 5 deletions crypto/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,20 +526,18 @@ macro_rules! pk_with_hash {
($pk:ident, $pkh:ident) => {
impl PublicKeyWithHash for $pk {
type Hash = $pkh;
type Error = ::std::convert::Infallible;

fn pk_hash(&self) -> Result<Self::Hash, Self::Error> {
fn pk_hash(&self) -> Self::Hash {
let hash = blake2b::digest_160(&self.0);
// hash size is 20 bytes (160 bits), exactly how many
// ContractTz*Hash expect, safe to unwrap
let typed_hash = Self::Hash::from_bytes(&hash).unwrap();
Ok(typed_hash)
Self::Hash::from_bytes(&hash).unwrap()
}
}

impl From<$pk> for $pkh {
fn from(source: $pk) -> Self {
source.pk_hash().unwrap()
source.pk_hash()
}
}
};
Expand Down
3 changes: 1 addition & 2 deletions crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ pub enum CryptoError {
/// Public key that support hashing.
pub trait PublicKeyWithHash {
type Hash;
type Error;

fn pk_hash(&self) -> Result<Self::Hash, Self::Error>;
fn pk_hash(&self) -> Self::Hash;
}

/// Public key that supports signature verification
Expand Down

0 comments on commit f105e32

Please sign in to comment.