Skip to content

Commit

Permalink
Update SendDestination::Bolt12 to include LNOffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Nov 13, 2024
1 parent 43a0bfe commit bb18d10
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 108 deletions.
2 changes: 1 addition & 1 deletion lib/Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,42 @@ typedef struct wire_cst_SendDestination_Bolt11 {
struct wire_cst_ln_invoice *invoice;
} wire_cst_SendDestination_Bolt11;

typedef struct wire_cst_list_String {
struct wire_cst_list_prim_u_8_strict **ptr;
int32_t len;
} wire_cst_list_String;

typedef struct wire_cst_Amount_Bitcoin {
uint64_t amount_msat;
} wire_cst_Amount_Bitcoin;

typedef struct wire_cst_Amount_Currency {
struct wire_cst_list_prim_u_8_strict *iso4217_code;
uint64_t fractional_amount;
} wire_cst_Amount_Currency;

typedef union AmountKind {
struct wire_cst_Amount_Bitcoin Bitcoin;
struct wire_cst_Amount_Currency Currency;
} AmountKind;

typedef struct wire_cst_amount {
int32_t tag;
union AmountKind kind;
} wire_cst_amount;

typedef struct wire_cst_ln_offer {
struct wire_cst_list_prim_u_8_strict *bolt12;
struct wire_cst_list_String *chains;
struct wire_cst_amount *amount;
struct wire_cst_list_prim_u_8_strict *description;
uint64_t *absolute_expiry;
struct wire_cst_list_prim_u_8_strict *issuer;
struct wire_cst_list_prim_u_8_strict *signing_pubkey;
} wire_cst_ln_offer;

typedef struct wire_cst_SendDestination_Bolt12 {
struct wire_cst_list_prim_u_8_strict *offer;
struct wire_cst_ln_offer *offer;
uint64_t receiver_amount_sat;
} wire_cst_SendDestination_Bolt12;

Expand Down Expand Up @@ -480,25 +514,6 @@ typedef struct wire_cst_aes_success_action_data_result {
union AesSuccessActionDataResultKind kind;
} wire_cst_aes_success_action_data_result;

typedef struct wire_cst_Amount_Bitcoin {
uint64_t amount_msat;
} wire_cst_Amount_Bitcoin;

typedef struct wire_cst_Amount_Currency {
struct wire_cst_list_prim_u_8_strict *iso4217_code;
uint64_t fractional_amount;
} wire_cst_Amount_Currency;

typedef union AmountKind {
struct wire_cst_Amount_Bitcoin Bitcoin;
struct wire_cst_Amount_Currency Currency;
} AmountKind;

typedef struct wire_cst_amount {
int32_t tag;
union AmountKind kind;
} wire_cst_amount;

typedef struct wire_cst_bitcoin_address_data {
struct wire_cst_list_prim_u_8_strict *address;
int32_t network;
Expand All @@ -507,21 +522,6 @@ typedef struct wire_cst_bitcoin_address_data {
struct wire_cst_list_prim_u_8_strict *message;
} wire_cst_bitcoin_address_data;

typedef struct wire_cst_list_String {
struct wire_cst_list_prim_u_8_strict **ptr;
int32_t len;
} wire_cst_list_String;

typedef struct wire_cst_ln_offer {
struct wire_cst_list_prim_u_8_strict *bolt12;
struct wire_cst_list_String *chains;
struct wire_cst_amount *amount;
struct wire_cst_list_prim_u_8_strict *description;
uint64_t *absolute_expiry;
struct wire_cst_list_prim_u_8_strict *issuer;
struct wire_cst_list_prim_u_8_strict *signing_pubkey;
} wire_cst_ln_offer;

typedef struct wire_cst_ln_url_error_data {
struct wire_cst_list_prim_u_8_strict *reason;
} wire_cst_ln_url_error_data;
Expand Down
2 changes: 1 addition & 1 deletion lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ dictionary PrepareSendRequest {
interface SendDestination {
LiquidAddress(LiquidAddressData address_data);
Bolt11(LNInvoice invoice);
Bolt12(string offer, u64 receiver_amount_sat);
Bolt12(LNOffer offer, u64 receiver_amount_sat);
};

dictionary PrepareSendResponse {
Expand Down
6 changes: 3 additions & 3 deletions lib/core/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3999,7 +3999,7 @@ impl SseDecode for crate::model::SendDestination {
};
}
2 => {
let mut var_offer = <String>::sse_decode(deserializer);
let mut var_offer = <crate::bindings::LNOffer>::sse_decode(deserializer);
let mut var_receiverAmountSat = <u64>::sse_decode(deserializer);
return crate::model::SendDestination::Bolt12 {
offer: var_offer,
Expand Down Expand Up @@ -7803,7 +7803,7 @@ impl SseEncode for crate::model::SendDestination {
receiver_amount_sat,
} => {
<i32>::sse_encode(2, serializer);
<String>::sse_encode(offer, serializer);
<crate::bindings::LNOffer>::sse_encode(offer, serializer);
<u64>::sse_encode(receiver_amount_sat, serializer);
}
_ => {
Expand Down Expand Up @@ -12882,7 +12882,7 @@ mod io {
#[repr(C)]
#[derive(Clone, Copy)]
pub struct wire_cst_SendDestination_Bolt12 {
offer: *mut wire_cst_list_prim_u_8_strict,
offer: *mut wire_cst_ln_offer,
receiver_amount_sat: u64,
}
#[repr(C)]
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub enum SendDestination {
invoice: LNInvoice,
},
Bolt12 {
offer: String,
offer: LNOffer,
receiver_amount_sat: u64,
},
}
Expand Down
52 changes: 44 additions & 8 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
use std::collections::HashMap;
use std::time::Instant;
use std::{fs, path::PathBuf, str::FromStr, sync::Arc, time::Duration};

use anyhow::{anyhow, Result};
use boltz_client::{swaps::boltz::*, util::secrets::Preimage};
use buy::{BuyBitcoinApi, BuyBitcoinService};
Expand All @@ -19,9 +23,6 @@ use sdk_common::input_parser::InputType;
use sdk_common::liquid::LiquidAddressData;
use sdk_common::prelude::{FiatAPI, FiatCurrency, LnUrlPayError, LnUrlWithdrawError, Rate};
use signer::SdkSigner;
use std::collections::HashMap;
use std::time::Instant;
use std::{fs, path::PathBuf, str::FromStr, sync::Arc, time::Duration};
use tokio::sync::{watch, Mutex, RwLock};
use tokio::time::MissedTickBehavior;
use tokio_stream::wrappers::BroadcastStream;
Expand All @@ -45,6 +46,7 @@ use crate::{
persist::Persister,
utils, *,
};
use ::lightning::offers::offer::Offer;

pub const DEFAULT_DATA_DIR: &str = ".data";
/// Number of blocks to monitor a swap after its timeout block height
Expand Down Expand Up @@ -766,7 +768,7 @@ impl LiquidSdk {
let receiver_amount_sat;
let payment_destination;

match sdk_common::input_parser::parse(&req.destination).await {
match Self::parse(&req.destination).await {
Ok(InputType::LiquidAddress {
address: mut liquid_address_data,
}) => {
Expand Down Expand Up @@ -860,7 +862,7 @@ impl LiquidSdk {
};
payment_destination = SendDestination::Bolt11 { invoice };
}
Ok(InputType::Bolt12Offer { offer: _ }) => {
Ok(InputType::Bolt12Offer { offer }) => {
receiver_amount_sat = match req.amount {
Some(PayAmount::Receiver { amount_sat }) => Ok(amount_sat),
_ => Err(PaymentError::amount_missing(
Expand All @@ -877,7 +879,7 @@ impl LiquidSdk {
fees_sat = boltz_fees_total + lockup_fees_sat;

payment_destination = SendDestination::Bolt12 {
offer: req.destination.clone(),
offer,
receiver_amount_sat,
};
}
Expand Down Expand Up @@ -968,7 +970,7 @@ impl LiquidSdk {
} => {
let bolt12_invoice = self
.swapper
.get_bolt12_invoice(offer, *receiver_amount_sat)?;
.get_bolt12_invoice(&offer.bolt12, *receiver_amount_sat)?;
self.pay_bolt12_invoice(&bolt12_invoice, *fees_sat).await
}
}
Expand Down Expand Up @@ -2582,9 +2584,43 @@ impl LiquidSdk {

/// Parses a string into an [InputType]. See [input_parser::parse].
pub async fn parse(input: &str) -> Result<InputType, PaymentError> {
if let Ok(offer) = input.parse::<Offer>() {
return Ok(InputType::Bolt12Offer {
offer: LNOffer {
bolt12: input.to_string(),
chains: offer
.chains()
.iter()
.map(|chain| chain.to_string())
.collect(),
// TODO This conversion (between lightning-v0.0.125 to -v0.0.118 Amount types)
// won't be needed when Liquid SDK uses the same lightning crate version as sdk-common
amount: offer.amount().map(|amount| match amount {
::lightning::offers::offer::Amount::Bitcoin { amount_msats } => {
Amount::Bitcoin {
amount_msat: amount_msats,
}
}
::lightning::offers::offer::Amount::Currency {
iso4217_code,
amount,
} => Amount::Currency {
iso4217_code: String::from_utf8(iso4217_code.to_vec())
.expect("Expecting a valid ISO 4217 character sequence"),
fractional_amount: amount,
},
}),
description: offer.description().map(|d| d.to_string()),
absolute_expiry: offer.absolute_expiry().map(|expiry| expiry.as_secs()),
issuer: offer.issuer().map(|s| s.to_string()),
signing_pubkey: offer.signing_pubkey().map(|pk| pk.to_string()),
},
});
}

parse(input)
.await
.map_err(|e| PaymentError::Generic { err: e.to_string() })
.map_err(|e| PaymentError::generic(&e.to_string()))
}

/// Parses a string into an [LNInvoice]. See [invoice::parse_invoice].
Expand Down
6 changes: 3 additions & 3 deletions packages/dart/lib/src/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2912,7 +2912,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
);
case 2:
return SendDestination_Bolt12(
offer: dco_decode_String(raw[1]),
offer: dco_decode_box_autoadd_ln_offer(raw[1]),
receiverAmountSat: dco_decode_u_64(raw[2]),
);
default:
Expand Down Expand Up @@ -4828,7 +4828,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
var var_invoice = sse_decode_box_autoadd_ln_invoice(deserializer);
return SendDestination_Bolt11(invoice: var_invoice);
case 2:
var var_offer = sse_decode_String(deserializer);
var var_offer = sse_decode_box_autoadd_ln_offer(deserializer);
var var_receiverAmountSat = sse_decode_u_64(deserializer);
return SendDestination_Bolt12(offer: var_offer, receiverAmountSat: var_receiverAmountSat);
default:
Expand Down Expand Up @@ -6596,7 +6596,7 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_box_autoadd_ln_invoice(invoice, serializer);
case SendDestination_Bolt12(offer: final offer, receiverAmountSat: final receiverAmountSat):
sse_encode_i_32(2, serializer);
sse_encode_String(offer, serializer);
sse_encode_box_autoadd_ln_offer(offer, serializer);
sse_encode_u_64(receiverAmountSat, serializer);
default:
throw UnimplementedError('');
Expand Down
Loading

0 comments on commit bb18d10

Please sign in to comment.