Skip to content

Commit

Permalink
Add blocking lnd client
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Oct 13, 2023
1 parent b8c1572 commit 767760c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/blocking_client.rs
Original file line number Diff line number Diff line change
@@ -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<Self, tonic::transport::Error> {
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::Response<KeyDescriptor>, 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::Response<SignMessageResp>, tonic_lnd::tonic::Status> {
self.rt.block_on(self.client.signer().sign_message(request))
}
}

0 comments on commit 767760c

Please sign in to comment.