2023-08-25 09:28:47 +00:00
|
|
|
use std::{future::Future, pin::Pin, sync::Arc, task::Poll};
|
2023-08-25 06:32:21 +00:00
|
|
|
|
2023-09-07 15:06:34 +00:00
|
|
|
use keyfork_derive_util::request::{DerivationError, DerivationRequest, DerivationResponse};
|
2023-08-25 09:28:47 +00:00
|
|
|
use keyfork_mnemonic_util::Mnemonic;
|
|
|
|
use tower::Service;
|
2023-08-25 06:32:21 +00:00
|
|
|
|
2023-09-07 17:40:17 +00:00
|
|
|
// NOTE: All values implemented in Keyforkd must implement Clone with low overhead, either by
|
|
|
|
// using an Arc or by having a small signature. This is because Service<T> takes &mut self.
|
|
|
|
|
2023-08-25 06:32:21 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Keyforkd {
|
|
|
|
mnemonic: Arc<Mnemonic>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keyforkd {
|
|
|
|
pub fn new(mnemonic: Mnemonic) -> Self {
|
2023-08-25 09:28:47 +00:00
|
|
|
Self {
|
|
|
|
mnemonic: Arc::new(mnemonic),
|
|
|
|
}
|
2023-08-25 06:32:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 15:06:34 +00:00
|
|
|
impl Service<DerivationRequest> for Keyforkd {
|
|
|
|
type Response = DerivationResponse;
|
2023-08-25 06:32:21 +00:00
|
|
|
|
|
|
|
type Error = DerivationError;
|
|
|
|
|
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
|
|
|
|
|
|
|
fn poll_ready(
|
|
|
|
&mut self,
|
|
|
|
_cx: &mut std::task::Context<'_>,
|
|
|
|
) -> std::task::Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))]
|
2023-09-07 15:06:34 +00:00
|
|
|
fn call(&mut self, req: DerivationRequest) -> Self::Future {
|
2023-09-07 17:40:17 +00:00
|
|
|
let mnemonic = self.mnemonic.clone();
|
|
|
|
Box::pin(async { req.derive_with_mnemonic(&mnemonic) })
|
2023-08-25 06:32:21 +00:00
|
|
|
}
|
|
|
|
}
|