Skip to content

Commit 636e961

Browse files
committed
bindings/boltz: use Bolt11Invoice type around
1 parent cb51fa9 commit 636e961

6 files changed

Lines changed: 17 additions & 14 deletions

File tree

lwk_bindings/src/lightning.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,16 @@ impl PreparePayResponse {
195195

196196
#[uniffi::export]
197197
impl InvoiceResponse {
198-
pub fn bolt11_invoice(&self) -> Result<String, LwkError> {
199-
Ok(self
198+
pub fn bolt11_invoice(&self) -> Result<Bolt11Invoice, LwkError> {
199+
let bolt11_invoice = self
200200
.inner
201201
.lock()?
202202
.as_ref()
203203
.ok_or_else(|| LwkError::Generic {
204204
msg: "This InvoiceResponse already called complete_pay or errored".to_string(),
205205
})?
206-
.bolt11_invoice())
206+
.bolt11_invoice();
207+
Ok(Bolt11Invoice::from(bolt11_invoice))
207208
}
208209

209210
pub fn complete_pay(&self) -> Result<bool, LwkError> {

lwk_bindings/tests/bindings/lightning.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ def log(self, level: LogLevel, message: str):
2626
lightning_session = LightningSession(network, client, 10, logger)
2727

2828
invoice_response = lightning_session.invoice(1000, "ciao", claim_address)
29-
bolt11_invoice = invoice_response.bolt11_invoice()
29+
bolt11_invoice_obj = invoice_response.bolt11_invoice()
30+
bolt11_invoice = str(bolt11_invoice_obj)
3031
print(bolt11_invoice)
3132
assert bolt11_invoice.startswith("lnbc1")
3233

@@ -37,7 +38,6 @@ def log(self, level: LogLevel, message: str):
3738
## in the real world any invoice generated from a Boltz-enabled wallet will contain a MRH
3839
try:
3940
refund_address = wollet.address(3).address()
40-
bolt11_invoice_obj = Bolt11Invoice(bolt11_invoice)
4141
prepare_pay_response = lightning_session.prepare_pay(bolt11_invoice_obj, refund_address)
4242
except LwkError.MagicRoutingHint as e:
4343
# Handle the specific MagicRoutingHint error

lwk_boltz/src/blocking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl InvoiceResponse {
7979
Ok(inner)
8080
}
8181

82-
pub fn bolt11_invoice(&self) -> String {
82+
pub fn bolt11_invoice(&self) -> Bolt11Invoice {
8383
self.inner.bolt11_invoice.clone()
8484
}
8585
}

lwk_boltz/src/reverse.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::str::FromStr;
12
use std::sync::Arc;
23
use std::time::Duration;
34

@@ -12,7 +13,7 @@ use boltz_client::swaps::SwapTransactionParams;
1213
use boltz_client::swaps::TransactionOptions;
1314
use boltz_client::util::secrets::Preimage;
1415
use boltz_client::Secp256k1;
15-
use boltz_client::{Keypair, PublicKey};
16+
use boltz_client::{Bolt11Invoice, Keypair, PublicKey};
1617
use lwk_wollet::elements;
1718
use lwk_wollet::secp256k1::rand::thread_rng;
1819

@@ -23,7 +24,7 @@ pub struct InvoiceResponse {
2324
pub swap_id: String,
2425
/// The invoice to show to the payer, the invoice amount will be exactly like the amount parameter,
2526
/// However, the receiver will receive `amount - fee`
26-
pub bolt11_invoice: String,
27+
pub bolt11_invoice: Bolt11Invoice,
2728

2829
/// The fee of the swap provider
2930
pub fee: u64,
@@ -69,16 +70,17 @@ impl LightningSession {
6970
};
7071

7172
let reverse_resp = self.api.post_reverse_req(create_reverse_req).await?;
72-
let invoice = reverse_resp
73+
let invoice_str = reverse_resp
7374
.invoice
7475
.as_ref()
7576
.ok_or(Error::MissingInvoiceInResponse(reverse_resp.id.clone()))?
7677
.clone();
78+
let invoice = Bolt11Invoice::from_str(&invoice_str)?;
7779
let fee = amount.checked_sub(reverse_resp.onchain_amount).ok_or(
7880
Error::ExpectedAmountLowerThanInvoice(amount, reverse_resp.id.clone()),
7981
)?;
8082

81-
let _ = check_for_mrh(&self.api, &invoice, chain).await?.ok_or(
83+
let _ = check_for_mrh(&self.api, &invoice_str, chain).await?.ok_or(
8284
Error::InvoiceWithoutMagicRoutingHint(reverse_resp.id.clone()),
8385
)?;
8486

lwk_boltz/tests/mrh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod tests {
4646
let boltz_api = BoltzApiClientV2::new(utils::BOLTZ_REGTEST.to_string(), Some(TIMEOUT));
4747
let mrh_result = check_for_mrh(
4848
&boltz_api,
49-
&invoice.bolt11_invoice,
49+
&invoice.bolt11_invoice.to_string(),
5050
Chain::Liquid(LiquidChain::LiquidRegtest),
5151
)
5252
.await
@@ -88,7 +88,7 @@ mod tests {
8888

8989
// Sender: Detect MRH in the invoice
9090
let sender_session = LightningSession::new(network, client.clone(), Some(TIMEOUT));
91-
let bolt11_parsed = Bolt11Invoice::from_str(&invoice.bolt11_invoice).unwrap();
91+
let bolt11_parsed = invoice.bolt11_invoice.clone();
9292
let prepare_pay_response = sender_session
9393
.prepare_pay(&bolt11_parsed, &claim_address)
9494
.await;

lwk_boltz/tests/reverse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod tests {
4747
.await;
4848
match invoice_response {
4949
Ok(invoice_response) => {
50-
assert!(invoice_response.bolt11_invoice.starts_with("lnbc1"));
50+
assert!(invoice_response.bolt11_invoice.to_string().starts_with("lnbc1"));
5151
return;
5252
}
5353
Err(e) => {
@@ -125,7 +125,7 @@ mod tests {
125125
let claim_address = elements::Address::from_str(&claim_address).unwrap();
126126
let invoice = session.invoice(100000, None, &claim_address).await.unwrap();
127127
log::info!("Invoice: {}", invoice.bolt11_invoice);
128-
utils::start_pay_invoice_lnd(invoice.bolt11_invoice.clone());
128+
utils::start_pay_invoice_lnd(invoice.bolt11_invoice.to_string());
129129
invoice.complete_pay().await.unwrap();
130130
}
131131

0 commit comments

Comments
 (0)