Skip to content

Commit

Permalink
Merge pull request #408 from breez/addtional-invoice-fields
Browse files Browse the repository at this point in the history
Addtional invoice fields
  • Loading branch information
roeierez authored Aug 28, 2023
2 parents aea7f61 + dd78645 commit 4fc3115
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 44 deletions.
7 changes: 5 additions & 2 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,11 @@ dictionary ReverseSwapFeesRequest {
dictionary ReceivePaymentRequest {
u64 amount_sats;
string description;
sequence<u8>? preimage;
OpeningFeeParams? opening_fee_params;
sequence<u8>? preimage = null;
OpeningFeeParams? opening_fee_params = null;
boolean? use_description_hash = null;
u64? expiry = null;
u32? cltv = null;
};

dictionary ReceivePaymentResponse {
Expand Down
15 changes: 12 additions & 3 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ impl BreezServices {
description: description.unwrap_or_default(),
preimage: None,
opening_fee_params: None,
use_description_hash: Some(false),
expiry: None,
cltv: None,
})
.await
.map_err(|_| anyhow!("Failed to receive payment"))?
Expand Down Expand Up @@ -1567,11 +1570,14 @@ impl Receiver for PaymentReceiver {
destination_invoice_amount_sats,
req_data.description,
req_data.preimage,
req_data.use_description_hash,
req_data.expiry,
req_data.cltv,
)
.await?;
info!("Invoice created {}", invoice.bolt11);
info!("Invoice created {}", invoice);

let mut parsed_invoice = parse_invoice(&invoice.bolt11)?;
let mut parsed_invoice = parse_invoice(invoice)?;

// check if the lsp hint already exists
info!("Existing routing hints {:?}", parsed_invoice.routing_hints);
Expand Down Expand Up @@ -1606,7 +1612,7 @@ impl Receiver for PaymentReceiver {
if lsp_hint.is_some() || amount_sats != destination_invoice_amount_sats {
// create the large amount invoice
let raw_invoice_with_hint =
add_lsp_routing_hints(invoice.bolt11.clone(), lsp_hint, amount_sats * 1000)?;
add_lsp_routing_hints(invoice.clone(), lsp_hint, amount_sats * 1000)?;

info!("Routing hint added");
let signed_invoice_with_hint = self.node_api.sign_invoice(raw_invoice_with_hint)?;
Expand Down Expand Up @@ -1862,6 +1868,9 @@ pub(crate) mod tests {
description: "should populate lsp hints".to_string(),
preimage: None,
opening_fee_params: None,
use_description_hash: Some(false),
expiry: None,
cltv: None,
})
.await?
.ln_invoice;
Expand Down
30 changes: 30 additions & 0 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ pub extern "C" fn wire_execute_command(port_: i64, command: *mut wire_uint_8_lis

// Section: allocate functions

#[no_mangle]
pub extern "C" fn new_box_autoadd_bool_0(value: bool) -> *mut bool {
support::new_leak_box_ptr(value)
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_buy_bitcoin_request_0() -> *mut wire_BuyBitcoinRequest {
support::new_leak_box_ptr(wire_BuyBitcoinRequest::new_with_null_ptr())
Expand Down Expand Up @@ -339,6 +344,11 @@ pub extern "C" fn new_box_autoadd_sign_message_request_0() -> *mut wire_SignMess
support::new_leak_box_ptr(wire_SignMessageRequest::new_with_null_ptr())
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_u32_0(value: u32) -> *mut u32 {
support::new_leak_box_ptr(value)
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_u64_0(value: u64) -> *mut u64 {
support::new_leak_box_ptr(value)
Expand All @@ -363,6 +373,12 @@ impl Wire2Api<String> for *mut wire_uint_8_list {
String::from_utf8_lossy(&vec).into_owned()
}
}

impl Wire2Api<bool> for *mut bool {
fn wire2api(self) -> bool {
unsafe { *support::box_from_leak_ptr(self) }
}
}
impl Wire2Api<BuyBitcoinRequest> for *mut wire_BuyBitcoinRequest {
fn wire2api(self) -> BuyBitcoinRequest {
let wrap = unsafe { support::box_from_leak_ptr(self) };
Expand Down Expand Up @@ -452,6 +468,11 @@ impl Wire2Api<SignMessageRequest> for *mut wire_SignMessageRequest {
Wire2Api::<SignMessageRequest>::wire2api(*wrap).into()
}
}
impl Wire2Api<u32> for *mut u32 {
fn wire2api(self) -> u32 {
unsafe { *support::box_from_leak_ptr(self) }
}
}
impl Wire2Api<u64> for *mut u64 {
fn wire2api(self) -> u64 {
unsafe { *support::box_from_leak_ptr(self) }
Expand Down Expand Up @@ -584,6 +605,9 @@ impl Wire2Api<ReceivePaymentRequest> for wire_ReceivePaymentRequest {
description: self.description.wire2api(),
preimage: self.preimage.wire2api(),
opening_fee_params: self.opening_fee_params.wire2api(),
use_description_hash: self.use_description_hash.wire2api(),
expiry: self.expiry.wire2api(),
cltv: self.cltv.wire2api(),
}
}
}
Expand Down Expand Up @@ -710,6 +734,9 @@ pub struct wire_ReceivePaymentRequest {
description: *mut wire_uint_8_list,
preimage: *mut wire_uint_8_list,
opening_fee_params: *mut wire_OpeningFeeParams,
use_description_hash: *mut bool,
expiry: *mut u64,
cltv: *mut u32,
}

#[repr(C)]
Expand Down Expand Up @@ -963,6 +990,9 @@ impl NewWithNullPtr for wire_ReceivePaymentRequest {
description: core::ptr::null_mut(),
preimage: core::ptr::null_mut(),
opening_fee_params: core::ptr::null_mut(),
use_description_hash: core::ptr::null_mut(),
expiry: core::ptr::null_mut(),
cltv: core::ptr::null_mut(),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,12 @@ where
}
}

impl Wire2Api<bool> for bool {
fn wire2api(self) -> bool {
self
}
}

impl Wire2Api<BuyBitcoinProvider> for i32 {
fn wire2api(self) -> BuyBitcoinProvider {
match self {
Expand Down
32 changes: 21 additions & 11 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ use gl_client::pb::cln::{
self, CloseRequest, ListclosedchannelsClosedchannels, ListclosedchannelsRequest,
ListpeerchannelsRequest,
};
use gl_client::pb::{
Amount, Invoice, InvoiceRequest, InvoiceStatus, OffChainPayment, PayStatus, Peer,
WithdrawResponse,
};
use gl_client::pb::cln::{AmountOrAny, InvoiceRequest};
use gl_client::pb::{Amount, InvoiceStatus, OffChainPayment, PayStatus, Peer, WithdrawResponse};

use gl_client::scheduler::Scheduler;
use gl_client::signer::Signer;
use gl_client::tls::TlsConfig;
Expand Down Expand Up @@ -243,22 +242,33 @@ impl NodeAPI for Greenlight {
amount_sats: u64,
description: String,
preimage: Option<Vec<u8>>,
) -> Result<Invoice> {
let mut client = self.get_client().await?;

use_description_hash: Option<bool>,
expiry: Option<u64>,
cltv: Option<u32>,
) -> Result<String> {
let mut client = self.get_node_client().await?;
let request = InvoiceRequest {
amount: Some(Amount {
unit: Some(Unit::Satoshi(amount_sats)),
amount_msat: Some(AmountOrAny {
value: Some(gl_client::pb::cln::amount_or_any::Value::Amount(
gl_client::pb::cln::Amount {
msat: amount_sats * 1000,
},
)),
}),
label: format!(
"breez-{}",
SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis()
),
description,
preimage: preimage.unwrap_or_default(),
preimage,
deschashonly: use_description_hash,
expiry,
fallbacks: vec![],
cltv,
};

Ok(client.create_invoice(request).await?.into_inner())
let res = client.invoice(request).await?.into_inner();
Ok(res.bolt11)
}

// implemenet pull changes from greenlight
Expand Down
19 changes: 17 additions & 2 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use bitcoin::util::bip32::{ChildNumber, ExtendedPrivKey};
use bitcoin::{Address, Script};
use chrono::{DateTime, Utc};
use gl_client::pb::{Invoice, WithdrawResponse};
use gl_client::pb::WithdrawResponse;
use lightning_invoice::RawInvoice;
use ripemd::Digest;
use ripemd::Ripemd160;
Expand Down Expand Up @@ -50,7 +50,10 @@ pub trait NodeAPI: Send + Sync {
amount_sats: u64,
description: String,
preimage: Option<Vec<u8>>,
) -> Result<Invoice>;
use_description_hash: Option<bool>,
expiry: Option<u64>,
cltv: Option<u32>,
) -> Result<String>;
async fn pull_changed(&self, since_timestamp: i64) -> Result<SyncResponse>;
/// As per the `pb::PayRequest` docs, `amount_sats` is only needed when the invoice doesn't specify an amount
async fn send_payment(
Expand Down Expand Up @@ -682,10 +685,22 @@ pub struct ReverseSwapFeesRequest {
/// Represents a receive payment request.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ReceivePaymentRequest {
/// The amount in satoshis for this payment request
pub amount_sats: u64,
/// The description for this payment request.
pub description: String,
/// Optional preimage for this payment request.
/// If specified, it will be used instead of generating a new one.
pub preimage: Option<Vec<u8>>,
/// If set and valid, these fess options are used when a new channels is needed.
/// Otherwise the default fee options will be used.
pub opening_fee_params: Option<OpeningFeeParams>,
/// If set to true, then the bolt11 invoice returned includes the description hash.
pub use_description_hash: Option<bool>,
/// if specified, set the time the invoice is valid for, in seconds.
pub expiry: Option<u64>,
/// if specified, sets the min_final_cltv_expiry for the invoice
pub cltv: Option<u32>,
}

/// Represents a receive payment response.
Expand Down
3 changes: 3 additions & 0 deletions libs/sdk-core/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ impl BTCReceiveSwap {
description: String::from("Bitcoin Transfer"),
preimage: Some(swap_info.preimage),
opening_fee_params: None,
use_description_hash: Some(false),
expiry: None,
cltv: None,
})
.await?
.ln_invoice;
Expand Down
24 changes: 7 additions & 17 deletions libs/sdk-core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bitcoin::util::bip32::{ChildNumber, ExtendedPrivKey};
use bitcoin::Network;
use chrono::{SecondsFormat, Utc};
use gl_client::pb::amount::Unit;
use gl_client::pb::{Amount, Invoice, Peer, WithdrawResponse};
use gl_client::pb::{Amount, Peer, WithdrawResponse};
use lightning::ln::PaymentSecret;
use lightning_invoice::{Currency, InvoiceBuilder, RawInvoice};
use rand::distributions::{Alphanumeric, DistString, Standard};
Expand Down Expand Up @@ -254,22 +254,12 @@ impl NodeAPI for MockNodeAPI {
amount_sats: u64,
description: String,
preimage: Option<Vec<u8>>,
) -> Result<Invoice> {
let invoice = create_invoice(description.clone(), amount_sats * 1000, vec![], preimage);
Ok(Invoice {
label: "".to_string(),
description,
amount: Some(Amount {
unit: Some(Unit::Satoshi(amount_sats)),
}),
received: None,
status: 0,
payment_time: 0,
expiry_time: invoice.expiry as u32,
bolt11: invoice.bolt11,
payment_hash: hex::decode(invoice.payment_hash).unwrap(),
payment_preimage: vec![],
})
_use_description_hash: Option<bool>,
_expiry: Option<u64>,
_cltv: Option<u32>,
) -> Result<String> {
let invoice = create_invoice(description, amount_sats * 1000, vec![], preimage);
Ok(invoice.bolt11)
}

async fn pull_changed(&self, _since_timestamp: i64) -> Result<SyncResponse> {
Expand Down
9 changes: 9 additions & 0 deletions libs/sdk-flutter/ios/Classes/bridge_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ typedef struct wire_ReceivePaymentRequest {
struct wire_uint_8_list *description;
struct wire_uint_8_list *preimage;
struct wire_OpeningFeeParams *opening_fee_params;
bool *use_description_hash;
uint64_t *expiry;
uint32_t *cltv;
} wire_ReceivePaymentRequest;

typedef struct wire_LnUrlPayRequestData {
Expand Down Expand Up @@ -236,6 +239,8 @@ void wire_recommended_fees(int64_t port_);

void wire_execute_command(int64_t port_, struct wire_uint_8_list *command);

bool *new_box_autoadd_bool_0(bool value);

struct wire_BuyBitcoinRequest *new_box_autoadd_buy_bitcoin_request_0(void);

struct wire_CheckMessageRequest *new_box_autoadd_check_message_request_0(void);
Expand Down Expand Up @@ -266,6 +271,8 @@ struct wire_ReverseSwapFeesRequest *new_box_autoadd_reverse_swap_fees_request_0(

struct wire_SignMessageRequest *new_box_autoadd_sign_message_request_0(void);

uint32_t *new_box_autoadd_u32_0(uint32_t value);

uint64_t *new_box_autoadd_u64_0(uint64_t value);

struct wire_uint_8_list *new_uint_8_list_0(int32_t len);
Expand Down Expand Up @@ -318,6 +325,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) wire_fetch_reverse_swap_fees);
dummy_var ^= ((int64_t) (void*) wire_recommended_fees);
dummy_var ^= ((int64_t) (void*) wire_execute_command);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_bool_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_buy_bitcoin_request_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_check_message_request_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_config_0);
Expand All @@ -333,6 +341,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) new_box_autoadd_receive_payment_request_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_reverse_swap_fees_request_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_sign_message_request_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_u32_0);
dummy_var ^= ((int64_t) (void*) new_box_autoadd_u64_0);
dummy_var ^= ((int64_t) (void*) new_uint_8_list_0);
dummy_var ^= ((int64_t) (void*) inflate_NodeConfig_Greenlight);
Expand Down
Loading

0 comments on commit 4fc3115

Please sign in to comment.