use crate::imp::core::types::{Bytes48, Bytes96};
use crate::imp::crypto::bls::BlsSecretKey;
use crate::imp::host::error::HostError;
use std::sync::Arc;
pub trait AttestationBackend: Send + Sync + 'static {
fn attest(&self, challenge: &[u8]) -> Result<Bytes96, HostError>;
fn public_key(&self) -> Bytes48;
}
pub type SharedBackend = Arc<dyn AttestationBackend>;
pub struct BlsAttestationBackend {
secret: Arc<BlsSecretKey>,
public: Bytes48,
}
impl BlsAttestationBackend {
pub fn new(secret: BlsSecretKey, public: Bytes48) -> Self {
BlsAttestationBackend {
secret: Arc::new(secret),
public,
}
}
pub fn from_shared(secret: Arc<BlsSecretKey>, public: Bytes48) -> Self {
BlsAttestationBackend { secret, public }
}
}
impl AttestationBackend for BlsAttestationBackend {
fn attest(&self, challenge: &[u8]) -> Result<Bytes96, HostError> {
Ok(crate::imp::crypto::bls::bls_sign(
self.secret.as_ref(),
challenge,
))
}
fn public_key(&self) -> Bytes48 {
self.public
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use super::*;
use crate::imp::core::types::{Bytes48, Bytes96};
struct ConstBackend;
impl AttestationBackend for ConstBackend {
fn attest(&self, _challenge: &[u8]) -> Result<Bytes96, crate::imp::host::error::HostError> {
Ok(Bytes96([0x5Au8; 96]))
}
fn public_key(&self) -> Bytes48 {
Bytes48([0x11u8; 48])
}
}
#[test]
fn custom_backend_signs() {
let b = ConstBackend;
let sig = b.attest(b"challenge").unwrap();
assert_eq!(sig.0, [0x5Au8; 96]);
assert_eq!(b.public_key().0, [0x11u8; 48]);
}
}