#[cfg(all(feature = "ed25519", feature = "k256"))]
mod combined;
#[cfg(feature = "ed25519")]
mod ed25519;
#[cfg(feature = "k256")]
mod k256_key;
#[cfg(feature = "rust-secp256k1")]
mod rust_secp256k1;
#[cfg(all(feature = "ed25519", feature = "k256"))]
pub use combined::{CombinedKey, CombinedPublicKey};
#[cfg(feature = "ed25519")]
pub use ed25519_dalek;
#[cfg(feature = "k256")]
pub use k256;
#[cfg(feature = "rust-secp256k1")]
pub use secp256k1;
use crate::Key;
use alloy_rlp::Error as DecoderError;
use bytes::Bytes;
use std::{
collections::BTreeMap,
error::Error,
fmt::{self, Debug, Display},
};
pub trait EnrKey: Send + Sync + Unpin + 'static {
type PublicKey: EnrPublicKey + Clone;
fn sign_v4(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError>;
fn public(&self) -> Self::PublicKey;
fn enr_to_public(content: &BTreeMap<Key, Bytes>) -> Result<Self::PublicKey, DecoderError>;
}
pub trait EnrKeyUnambiguous: EnrKey {
fn decode_public(bytes: &[u8]) -> Result<Self::PublicKey, DecoderError>;
}
pub trait EnrPublicKey: Clone + Debug + Send + Sync + Unpin + 'static {
type Raw: AsRef<[u8]>;
type RawUncompressed: AsRef<[u8]>;
fn verify_v4(&self, msg: &[u8], sig: &[u8]) -> bool;
fn encode(&self) -> Self::Raw;
fn encode_uncompressed(&self) -> Self::RawUncompressed;
fn enr_key(&self) -> Key;
}
#[derive(Debug)]
pub struct SigningError {
msg: String,
source: Option<Box<dyn Error + Send + Sync>>,
}
#[allow(dead_code)]
impl SigningError {
pub(crate) fn new<S: Display>(msg: S) -> Self {
Self {
msg: msg.to_string(),
source: None,
}
}
}
impl fmt::Display for SigningError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Key signing error: {}", self.msg)
}
}
impl Error for SigningError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source.as_ref().map(|s| &**s as &dyn Error)
}
}