Skip to content

Commit

Permalink
feat: implemented fetchinvoice for bolt12 offers
Browse files Browse the repository at this point in the history
  • Loading branch information
vacwmX committed Nov 14, 2023
1 parent 9626d81 commit fb9b267
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 49 deletions.
93 changes: 59 additions & 34 deletions libs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions libs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,3 @@ resolver = "2"

[workspace.package]
version = "0.2.7"

[workspace.dependencies]
uniffi = "0.23.0"
uniffi_macros = "0.23.0"
4 changes: 1 addition & 3 deletions libs/sdk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ hex = "0.4"
# v0.3 requires bitcoin 0.30, but gl-client uses bitcoin 0.29.2, so we keep this at v0.2 which also uses bitcoin 0.29
bip21 = "0.2"
bitcoin = "0.29.2"
gl-client = { git = "https://github.com/Blockstream/greenlight.git", features = [
"permissive",
], rev = "c7ff67eb062c021105f5601df3ac8699ecbeb51c" }
zbase32 = "0.1.2"
base64 = "0.13.0"
chrono = "0.4"
Expand Down Expand Up @@ -64,6 +61,7 @@ miniz_oxide = "0.7.1"
tokio-stream = "0.1.14"
serde_with = "3.3.0"
regex = "1.8.1"
gl-client = { git = "https://github.com/hydra-yse/greenlight", branch = "20231109-fetchinvoice" }

[dev-dependencies]
mockito = "1"
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ impl support::IntoDart for InputType {
vec![0.into_dart(), address.into_into_dart().into_dart()]
}
Self::Bolt11 { invoice } => vec![1.into_dart(), invoice.into_into_dart().into_dart()],
Self::Bolt12Offer { offer } => todo!(),
Self::NodeId { node_id } => vec![2.into_dart(), node_id.into_into_dart().into_dart()],
Self::Url { url } => vec![3.into_dart(), url.into_into_dart().into_dart()],
Self::LnUrlPay { data } => vec![4.into_dart(), data.into_into_dart().into_dart()],
Expand Down
45 changes: 44 additions & 1 deletion libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ use gl_client::signer::model::greenlight::{amount, scheduler};
use gl_client::signer::Signer;
use gl_client::tls::TlsConfig;
use gl_client::{node, utils};
use lightning::offers::offer::Offer;
use lightning::util::message_signing::verify;
use lightning_invoice::{RawInvoice, SignedRawInvoice};
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
use tokio::sync::{mpsc, Mutex};
use tokio::time::sleep;
use tokio_stream::{Stream, StreamExt};
use tonic::Streaming;
use tonic::{Code, Streaming};

use crate::invoice::{parse_invoice, InvoiceError, RouteHintHop};
use crate::models::*;
Expand Down Expand Up @@ -1308,6 +1309,48 @@ impl NodeAPI for Greenlight {
debug!("send_custom_message returned status {:?}", resp.status);
Ok(())
}

#[allow(unused_variables)]
async fn fetch_invoice(
&self,
offer: String,
amount_msat: Option<u64>,
quantity: Option<u64>,
recurrence_counter: Option<u64>,
recurrence_start: Option<f64>,
recurrence_label: Option<String>,
timeout: Option<f64>,
payer_note: Option<String>,
) -> NodeResult<FetchInvoiceResponse> {
// Get the required pubkeys
let mut client = self.get_node_client().await?;

// Parse the offer locally, to avoid any unnecessary calls to the recipient
if let Err(parse_error) = offer.parse::<Offer>() {
return Err(NodeError::Generic(anyhow!("Invalid offer")));
}

let response = client
.fetch_invoice(cln::FetchinvoiceRequest {
offer,
amount_msat: amount_msat.map(|msat| cln::Amount { msat }),
quantity,
recurrence_counter,
recurrence_start,
recurrence_label,
timeout,
payer_note,
})
.await;

response
.map(|grpc_response| grpc_response.into())
.map_err(|status| match status.code() {
Code::NotFound => NodeError::RouteNotFound(anyhow!(status.message().to_string())),
Code::Unimplemented => NodeError::NotSupported(),
_ => NodeError::Generic(anyhow!("Could not fetch invoice")),
})
}
}

#[derive(Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize)]
Expand Down
Loading

0 comments on commit fb9b267

Please sign in to comment.