Skip to content

Commit

Permalink
tendermint: PrivateKey and PublicKey have From impls for `ed255…
Browse files Browse the repository at this point in the history
…19-consensus` types (#1401)

* tendermint: `VerificationKey` is `From<ed25519_consensus::VerificationKey>`

* tendermint: `PublicKey` is `From<ed25519_consensus::VerificationKey>`

* tendermint: `PublicKey::from_ed25519_consensus`

* tendermint: private `SigningKey::new()` constructor

this mimics the same constructor found on `VerificationKey`, for
consistency.

* tendermint: `SigningKey` is `From<ed25519_consensus::SigningKey>`

* tendermint: `PrivateKey` is `From<ed25519_consensus::SigningKey>`

* tendermint: `PrivateKey::from_ed25519_consensus`

* Add changelog entry

---------

Co-authored-by: Romain Ruetschi <romain@informal.systems>
  • Loading branch information
cratelyn and romac committed Mar 19, 2024
1 parent 99ed0b9 commit f11a1be
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
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

0 comments on commit f11a1be

Please sign in to comment.