From 18db0e806b1a28dd54ac4935ae26a0cb6e90a096 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Fri, 25 Jan 2019 15:32:07 +0100 Subject: [PATCH] Add SecioKeypair::ed25519_raw_key (#890) --- protocols/secio/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/protocols/secio/src/lib.rs b/protocols/secio/src/lib.rs index 999fb17c..758d3577 100644 --- a/protocols/secio/src/lib.rs +++ b/protocols/secio/src/lib.rs @@ -246,6 +246,24 @@ impl SecioKeyPair { }) } + /// Builds a `SecioKeyPair` from a raw ed25519 32 bytes private key. + /// + /// Returns an error if the slice doesn't have the correct length. + pub fn ed25519_raw_key(key: impl AsRef<[u8]>) -> Result> { + let secret = ed25519_dalek::SecretKey::from_bytes(key.as_ref()) + .map_err(|err| err.to_string())?; + let public = ed25519_dalek::PublicKey::from_secret::(&secret); + + Ok(SecioKeyPair { + inner: SecioKeyPairInner::Ed25519 { + key_pair: Arc::new(Ed25519KeyPair { + secret, + public, + }), + } + }) + } + /// Generates a new random sec256k1 key pair. #[cfg(feature = "secp256k1")] pub fn secp256k1_generated() -> Result> {