sn_interface 0.16.20

Safe Network Interfaces. Messaging and Types.
Documentation
// Copyright 2023 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

//! Module providing keys, keypairs, and signatures.
//!
//! The easiest way to get a `PublicKey` is to create a random `Keypair` first through one of the
//! `new` functions. A `PublicKey` can't be generated by itself; it must always be derived from a
//! secret key.

use super::super::{Error, Result};
use bls::{self, serde_impl::SerdeSecret};
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display, Formatter};

// TODO: remove clones. We need to restructure to hold keypair ones and only require references for this.
/// Wrapper for different secret key types.
#[derive(Debug, Serialize, Deserialize)]
pub enum SecretKey {
    /// Ed25519 secretkey.
    Ed25519(ed25519_dalek::SecretKey),
    /// BLS secret key.
    Bls(SerdeSecret<bls::SecretKey>),
    /// BLS secretkey share.
    BlsShare(SerdeSecret<bls::SecretKeyShare>),
}

impl SecretKey {
    /// Construct a secret key from a hex string
    ///
    /// Similar to public key, it is often useful in user facing apps to be able to set your own
    /// secret key without depending on both the `ed25519_dalek` and hex crates just to reimplement
    /// this function.
    pub fn ed25519_from_hex(hex: &str) -> Result<Self> {
        let bytes = hex::decode(hex).map_err(|err| {
            Error::FailedToParse(format!(
                "Couldn't parse edd25519 secret key bytes from hex: {err}"
            ))
        })?;
        let ed25519_sk = ed25519_dalek::SecretKey::from_bytes(bytes.as_ref()).map_err(|err| {
            Error::FailedToParse(format!(
                "Couldn't parse ed25519 secret key from bytes: {err}",
            ))
        })?;
        Ok(Self::Ed25519(ed25519_sk))
    }

    pub fn bls(&self) -> Option<bls::SecretKey> {
        if let Self::Bls(key) = self {
            Some(key.0.clone())
        } else {
            None
        }
    }

    pub fn bls_share(&self) -> Option<bls::SecretKeyShare> {
        if let Self::BlsShare(key) = self {
            Some(key.0.clone())
        } else {
            None
        }
    }

    pub fn ed25519(&self) -> Option<ed25519_dalek::SecretKey> {
        if let Self::Ed25519(key) = self {
            match ed25519_dalek::SecretKey::from_bytes(&key.to_bytes()) {
                Ok(sk) => Some(sk),
                Err(_) => None,
            }
        } else {
            None
        }
    }
}

impl Display for SecretKey {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        Debug::fmt(self, formatter)
    }
}