pub use super::system::{SectionSig, SectionSigShare};
use super::{Error, Result};
use crate::types::{PublicKey, Signature};
use bls::PublicKey as BlsPublicKey;
use ed25519_dalek::{PublicKey as EdPublicKey, Signature as EdSignature, Verifier as _};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct ClientAuth {
pub public_key: PublicKey,
pub signature: Signature,
}
#[derive(Clone, Eq, PartialEq, custom_debug::Debug, serde::Deserialize, serde::Serialize)]
pub struct NodeSig {
pub section_pk: BlsPublicKey,
#[debug(with = "PublicKey::fmt_ed25519")]
pub node_ed_pk: EdPublicKey,
#[debug(with = "Signature::fmt_ed25519")]
#[serde(with = "serde_bytes")]
pub signature: EdSignature,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct AuthorityProof<T>(pub T);
impl<T: VerifyAuthority> AuthorityProof<T> {
pub fn verify(inner: T, payload: impl AsRef<[u8]>) -> Result<Self> {
inner.verify_authority(payload).map(Self)
}
pub fn into_inner(self) -> T {
self.0
}
}
impl<T> core::ops::Deref for AuthorityProof<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub trait VerifyAuthority: Sized + sealed::Sealed {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self>;
}
impl VerifyAuthority for ClientAuth {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
self.public_key
.verify(&self.signature, payload)
.map_err(|_| Error::InvalidSignature)?;
Ok(self)
}
}
impl sealed::Sealed for ClientAuth {}
impl VerifyAuthority for NodeSig {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
self.node_ed_pk
.verify(payload.as_ref(), &self.signature)
.map_err(|_| Error::InvalidSignature)?;
Ok(self)
}
}
impl sealed::Sealed for NodeSig {}
impl VerifyAuthority for SectionSigShare {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
if !self.verify(payload.as_ref()) {
return Err(Error::InvalidSignature);
}
Ok(self)
}
}
impl sealed::Sealed for SectionSigShare {}
impl VerifyAuthority for SectionSig {
fn verify_authority(self, payload: impl AsRef<[u8]>) -> Result<Self> {
if !self.public_key.verify(&self.signature, payload) {
return Err(Error::InvalidSignature);
}
Ok(self)
}
}
impl sealed::Sealed for SectionSig {}
mod sealed {
pub trait Sealed {}
}