pub use libp2p::identity::SigningError;
pub enum PublicKey {
Libp2p(libp2p::identity::PublicKey),
Litep2p(litep2p::crypto::PublicKey),
}
impl PublicKey {
pub fn encode_protobuf(&self) -> Vec<u8> {
match self {
Self::Libp2p(public) => public.encode_protobuf(),
Self::Litep2p(public) => public.to_protobuf_encoding(),
}
}
pub fn to_peer_id(&self) -> sc_network_types::PeerId {
match self {
Self::Libp2p(public) => public.to_peer_id().into(),
Self::Litep2p(public) => public.to_peer_id().into(),
}
}
}
pub enum Keypair {
Libp2p(libp2p::identity::Keypair),
Litep2p(litep2p::crypto::ed25519::Keypair),
}
impl Keypair {
pub fn generate_ed25519() -> Self {
Keypair::Litep2p(litep2p::crypto::ed25519::Keypair::generate())
}
pub fn public(&self) -> PublicKey {
match self {
Keypair::Libp2p(keypair) => PublicKey::Libp2p(keypair.public()),
Keypair::Litep2p(keypair) => PublicKey::Litep2p(keypair.public().into()),
}
}
}
pub struct Signature {
pub public_key: PublicKey,
pub bytes: Vec<u8>,
}
impl Signature {
pub fn new(public_key: PublicKey, bytes: Vec<u8>) -> Self {
Self { public_key, bytes }
}
pub fn sign_message(
message: impl AsRef<[u8]>,
keypair: &Keypair,
) -> Result<Self, SigningError> {
match keypair {
Keypair::Libp2p(keypair) => {
let public_key = keypair.public();
let bytes = keypair.sign(message.as_ref())?;
Ok(Signature { public_key: PublicKey::Libp2p(public_key), bytes })
},
Keypair::Litep2p(keypair) => {
let public_key = keypair.public();
let bytes = keypair.sign(message.as_ref());
Ok(Signature { public_key: PublicKey::Litep2p(public_key.into()), bytes })
},
}
}
}