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

tendermint: PrivateKey and PublicKey have From impls for ed25519-consensus types #1401

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- `[tendermint]` Add the following impls for `ed25519-consensus`:
* `From<ed25519_consensus::SigningKey` for `tendermint::PrivateKey`
* `From<ed25519_consensus::SigningKey>` for `tendermint::SigningKey`
* `From<ed25519_consensus::VerificationKey>` for `tendermint::PublicKey`
* `From<ed25519_consensus::VerificationKey>` for `tendermint::VerificationKey`
([\#1401](https://github.com/informalsystems/tendermint-rs/pull/1401))
12 changes: 12 additions & 0 deletions tendermint/src/crypto/ed25519/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -41,3 +46,10 @@ impl TryFrom<SigningKey> for ed25519_consensus::SigningKey {
Ok(ed25519_consensus::SigningKey::from(src.0))
}
}

#[cfg(feature = "rust-crypto")]
impl From<ed25519_consensus::SigningKey> for SigningKey {
fn from(sk: ed25519_consensus::SigningKey) -> Self {
Self::new(sk.to_bytes())
}
}
7 changes: 7 additions & 0 deletions tendermint/src/crypto/ed25519/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@ impl TryFrom<VerificationKey> for ed25519_consensus::VerificationKey {
.map_err(|_| Error::invalid_key("malformed Ed25519 public key".into()))
}
}

#[cfg(feature = "rust-crypto")]
impl From<ed25519_consensus::VerificationKey> for VerificationKey {
fn from(vk: ed25519_consensus::VerificationKey) -> Self {
Self::new(vk.to_bytes())
}
}
13 changes: 13 additions & 0 deletions tendermint/src/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ed25519_consensus::SigningKey> for PrivateKey {
fn from(sk: ed25519_consensus::SigningKey) -> Self {
Self::Ed25519(sk.into())
}
}

/// Serialize a Secp256k1 privkey as Base64
Expand Down
13 changes: 13 additions & 0 deletions tendermint/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ed25519> {
#[allow(unreachable_patterns)]
Expand Down Expand Up @@ -240,6 +246,13 @@ impl From<Secp256k1> for PublicKey {
}
}

#[cfg(feature = "rust-crypto")]
impl From<ed25519_consensus::VerificationKey> for PublicKey {
fn from(vk: ed25519_consensus::VerificationKey) -> PublicKey {
PublicKey::Ed25519(vk.into())
}
}

impl PartialOrd for PublicKey {
fn partial_cmp(&self, other: &PublicKey) -> Option<Ordering> {
Some(self.cmp(other))
Expand Down
Loading