use std::any::Any;
use std::fmt::Debug;
use nostr::prelude::*;
use crate::error::Error;
use crate::future::BoxedFuture;
pub trait Authenticator: Any + Debug + Send + Sync {
fn make_auth_event<'a>(
&'a self,
relay_url: &'a RelayUrl,
challenge: &'a str,
) -> BoxedFuture<'a, Result<Event, Error>>;
}
#[derive(Debug)]
pub struct SignerAuthenticator<T>
where
T: AsyncGetPublicKey + AsyncSignEvent,
{
signer: T,
}
impl<T> SignerAuthenticator<T>
where
T: AsyncGetPublicKey + AsyncSignEvent,
{
#[inline]
pub fn new(signer: T) -> Self {
Self { signer }
}
}
impl<T> From<T> for SignerAuthenticator<T>
where
T: AsyncGetPublicKey + AsyncSignEvent,
{
#[inline]
fn from(signer: T) -> Self {
Self::new(signer)
}
}
impl<T> Authenticator for SignerAuthenticator<T>
where
T: AsyncGetPublicKey + AsyncSignEvent,
{
fn make_auth_event<'a>(
&'a self,
relay_url: &'a RelayUrl,
challenge: &'a str,
) -> BoxedFuture<'a, Result<Event, Error>> {
Box::pin(async move {
Ok(ClientAuthentication::new(challenge, relay_url.clone())
.finalize_async(&self.signer)
.await?)
})
}
}