use std::{future::Future, pin::Pin, sync::Arc, task::Poll}; use keyfork_derive_util::request::{DerivationError, DerivationRequest, DerivationResponse}; use keyfork_mnemonic_util::Mnemonic; use tower::Service; // 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 takes &mut self. #[derive(Clone, Debug)] pub struct Keyforkd { mnemonic: Arc, } impl Keyforkd { pub fn new(mnemonic: Mnemonic) -> Self { Self { mnemonic: Arc::new(mnemonic), } } } impl Service for Keyforkd { type Response = DerivationResponse; type Error = DerivationError; type Future = Pin> + Send>>; fn poll_ready( &mut self, _cx: &mut std::task::Context<'_>, ) -> std::task::Poll> { Poll::Ready(Ok(())) } #[cfg_attr(feature = "tracing", tracing::instrument(skip(self)))] fn call(&mut self, req: DerivationRequest) -> Self::Future { let mnemonic = self.mnemonic.clone(); Box::pin(async { req.derive_with_mnemonic(&mnemonic) }) } }