diff --git a/src/blocking_client.rs b/src/blocking_client.rs new file mode 100644 index 00000000..3bcf11c7 --- /dev/null +++ b/src/blocking_client.rs @@ -0,0 +1,41 @@ +use std::path::PathBuf; +use tokio::runtime::{Builder, Runtime}; +use tonic_lnd::signrpc::{KeyDescriptor, KeyLocator, SignMessageReq, SignMessageResp}; +use tonic_lnd::{connect, Client}; + +// For some LND grpc calls, we need a blocking version of the client. Namely, in order to use LDK's "sign" +// API for signing an invoice request, the provided closure containing the LND signing API calls can't be +// asynchronous. +pub(crate) struct BlockingClient { + client: Client, + rt: Runtime, +} + +impl BlockingClient { + pub(crate) fn connect( + address: String, + cert_path: PathBuf, + macaroon_path: PathBuf, + ) -> Result { + let rt = Builder::new_multi_thread().enable_all().build().unwrap(); + let client = rt + .block_on(connect(address, cert_path, macaroon_path)) + .unwrap(); + + Ok(Self { client, rt }) + } + + pub(crate) fn derive_key( + &mut self, + request: KeyLocator, + ) -> Result, tonic_lnd::tonic::Status> { + self.rt.block_on(self.client.wallet().derive_key(request)) + } + + pub(crate) fn sign_message( + &mut self, + request: SignMessageReq, + ) -> Result, tonic_lnd::tonic::Status> { + self.rt.block_on(self.client.signer().sign_message(request)) + } +}