diff --git a/.changelog/unreleased/features/1401-ed25519-consensus-from-impls.md b/.changelog/unreleased/features/1401-ed25519-consensus-from-impls.md new file mode 100644 index 000000000..be5bc5b21 --- /dev/null +++ b/.changelog/unreleased/features/1401-ed25519-consensus-from-impls.md @@ -0,0 +1,6 @@ +- `[tendermint]` Add the following impls for `ed25519-consensus`: + * `From` for `tendermint::SigningKey` + * `From` for `tendermint::PublicKey` + * `From` for `tendermint::VerificationKey` + ([\#1401](https://github.com/informalsystems/tendermint-rs/pull/1401)) diff --git a/tendermint/src/crypto/ed25519/signing_key.rs b/tendermint/src/crypto/ed25519/signing_key.rs index 034557859..a7a18007e 100644 --- a/tendermint/src/crypto/ed25519/signing_key.rs +++ b/tendermint/src/crypto/ed25519/signing_key.rs @@ -7,6 +7,11 @@ use crate::Error; pub struct SigningKey([u8; 32]); impl SigningKey { + #[allow(dead_code)] + pub(super) fn new(bytes: [u8; 32]) -> Self { + Self(bytes) + } + pub fn as_bytes(&self) -> &[u8] { &self.0 } @@ -41,3 +46,10 @@ impl TryFrom for ed25519_consensus::SigningKey { Ok(ed25519_consensus::SigningKey::from(src.0)) } } + +#[cfg(feature = "rust-crypto")] +impl From for SigningKey { + fn from(sk: ed25519_consensus::SigningKey) -> Self { + Self::new(sk.to_bytes()) + } +} diff --git a/tendermint/src/crypto/ed25519/verification_key.rs b/tendermint/src/crypto/ed25519/verification_key.rs index 305614f07..9ef90c3e8 100644 --- a/tendermint/src/crypto/ed25519/verification_key.rs +++ b/tendermint/src/crypto/ed25519/verification_key.rs @@ -51,3 +51,10 @@ impl TryFrom for ed25519_consensus::VerificationKey { .map_err(|_| Error::invalid_key("malformed Ed25519 public key".into())) } } + +#[cfg(feature = "rust-crypto")] +impl From for VerificationKey { + fn from(vk: ed25519_consensus::VerificationKey) -> Self { + Self::new(vk.to_bytes()) + } +} diff --git a/tendermint/src/private_key.rs b/tendermint/src/private_key.rs index 052899d56..4b2bc5510 100644 --- a/tendermint/src/private_key.rs +++ b/tendermint/src/private_key.rs @@ -68,6 +68,19 @@ impl PrivateKey { PrivateKey::Secp256k1(_signing_key) => None, } } + + /// From an [`ed25519_consensus::SigningKey`] + #[cfg(feature = "rust-crypto")] + pub fn from_ed25519_consensus(sk: ed25519_consensus::SigningKey) -> Self { + Self::Ed25519(sk.into()) + } +} + +#[cfg(feature = "rust-crypto")] +impl From for PrivateKey { + fn from(sk: ed25519_consensus::SigningKey) -> Self { + Self::Ed25519(sk.into()) + } } /// Serialize a Secp256k1 privkey as Base64 diff --git a/tendermint/src/public_key.rs b/tendermint/src/public_key.rs index dc287d4d0..8b35ede57 100644 --- a/tendermint/src/public_key.rs +++ b/tendermint/src/public_key.rs @@ -175,6 +175,12 @@ impl PublicKey { Ed25519::try_from(bytes).map(PublicKey::Ed25519).ok() } + /// From an [`ed25519_consensus::VerificationKey`] + #[cfg(feature = "rust-crypto")] + pub fn from_ed25519_consensus(vk: ed25519_consensus::VerificationKey) -> Self { + Self::from(vk) + } + /// Get Ed25519 public key pub fn ed25519(self) -> Option { #[allow(unreachable_patterns)] @@ -240,6 +246,13 @@ impl From for PublicKey { } } +#[cfg(feature = "rust-crypto")] +impl From for PublicKey { + fn from(vk: ed25519_consensus::VerificationKey) -> PublicKey { + PublicKey::Ed25519(vk.into()) + } +} + impl PartialOrd for PublicKey { fn partial_cmp(&self, other: &PublicKey) -> Option { Some(self.cmp(other))