Skip to content

Commit

Permalink
Expand inner structure of InputType::Bolt12
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Nov 12, 2024
1 parent fa01549 commit 36ce80c
Show file tree
Hide file tree
Showing 12 changed files with 871 additions and 56 deletions.
2 changes: 1 addition & 1 deletion libs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ aes = "0.8"
anyhow = { version = "1.0.79", features = ["backtrace"] }
base64 = "0.13.0"
bitcoin = "=0.29.2" # Same version as used in gl-client
# Pin the reqwest dependency until macOS linker issue is fixed: https://github.com/seanmonstar/reqwest/issues/2006
hex = "0.4"
lightning = "=0.0.118" # Same version as used in gl-client
lightning-invoice = "=0.26.0" # Same version as used in gl-client
Expand All @@ -30,6 +29,7 @@ mockito = "1"
once_cell = "1"
prost = "^0.11"
regex = "1.8.1"
# Pin the reqwest dependency until macOS linker issue is fixed: https://github.com/seanmonstar/reqwest/issues/2006
reqwest = { version = "=0.11.20", features = ["json"] }
rusqlite = { version = "0.29", features = [
"serde_json",
Expand Down
18 changes: 17 additions & 1 deletion libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -730,11 +730,27 @@ dictionary RecommendedFees {
u64 minimum_fee;
};

[Enum]
interface Amount {
Bitcoin(u64 amount_msat);
Currency(string iso4217_code, u64 fractional_amount);
};

dictionary LNOffer {
string bolt12;
sequence<string> chains;
string description;
string signing_pubkey;
Amount? amount;
u64? absolute_expiry;
string? issuer;
};

[Enum]
interface InputType {
BitcoinAddress(BitcoinAddressData address);
Bolt11(LNInvoice invoice);
Bolt12(string offer);
Bolt12(LNOffer offer);
NodeId(string node_id);
Url(string url);
LnUrlPay(LnUrlPayRequestData data);
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use breez_sdk_core::lnurl::pay::{LnUrlPayResult, LnUrlPaySuccessData};
use breez_sdk_core::{
error::*, mnemonic_to_seed as sdk_mnemonic_to_seed, parse as sdk_parse_input,
parse_invoice as sdk_parse_invoice, AesSuccessActionDataDecrypted, AesSuccessActionDataResult,
BackupFailedData, BackupStatus, BitcoinAddressData, BreezEvent, BreezServices,
Amount, BackupFailedData, BackupStatus, BitcoinAddressData, BreezEvent, BreezServices,
BuyBitcoinProvider, BuyBitcoinRequest, BuyBitcoinResponse, ChannelState, CheckMessageRequest,
CheckMessageResponse, ClosedChannelPaymentDetails, Config, ConfigureNodeRequest,
ConnectRequest, CurrencyInfo, EnvironmentType, EventListener, FeeratePreset, FiatCurrency,
GreenlightCredentials, GreenlightDeviceCredentials, GreenlightNodeConfig, HealthCheckStatus,
InputType, InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails,
InputType, InvoicePaidDetails, LNInvoice, LNOffer, ListPaymentsRequest, LnPaymentDetails,
LnUrlAuthError, LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayError,
LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData, LnUrlWithdrawError,
LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData,
Expand Down
16 changes: 14 additions & 2 deletions libs/sdk-common/src/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,19 @@ pub async fn parse(input: &str) -> Result<InputType> {

if let Ok(offer) = input.parse::<Offer>() {
return Ok(InputType::Bolt12 {
offer: offer.to_string(),
offer: LNOffer {
bolt12: input.to_string(),
chains: offer
.chains()
.iter()
.map(|chain| chain.to_string())
.collect(),
amount: offer.amount().map(|amount| amount.clone().into()),
description: offer.description().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().to_string(),
},
});
}

Expand Down Expand Up @@ -426,7 +438,7 @@ pub enum InputType {
invoice: LNInvoice,
},
Bolt12 {
offer: String,
offer: LNOffer,
},
NodeId {
node_id: String,
Expand Down
42 changes: 42 additions & 0 deletions libs/sdk-common/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,48 @@ fn format_short_channel_id(id: u64) -> String {
format!("{block_num}x{tx_num}x{tx_out}")
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum Amount {
Bitcoin {
amount_msat: u64,
},
Currency {
// Reference to [FiatCurrency.id]
iso4217_code: String,
fractional_amount: u64,
},
}

impl From<lightning::offers::offer::Amount> for Amount {
fn from(amount: lightning::offers::offer::Amount) -> Self {
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,
},
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct LNOffer {
pub bolt12: String,
pub chains: Vec<String>,
pub amount: Option<Amount>,
pub description: String,
pub absolute_expiry: Option<u64>,
pub issuer: Option<String>,
pub signing_pubkey: String,
// pub paths: Vec<BlindedPath>,
}

/// Wrapper for a BOLT11 LN invoice
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct LNInvoice {
Expand Down
36 changes: 29 additions & 7 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};
use sdk_common::invoice;
pub use sdk_common::prelude::{
parse, AesSuccessActionDataDecrypted, AesSuccessActionDataResult, BitcoinAddressData,
CurrencyInfo, FiatCurrency, InputType, LNInvoice, LnUrlAuthRequestData, LnUrlCallbackStatus,
LnUrlError, LnUrlErrorData, LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData,
LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData,
LocaleOverrides, LocalizedName, MessageSuccessActionData, Network, Rate, RouteHint,
RouteHintHop, SuccessActionProcessed, Symbol, UrlSuccessActionData,
parse, AesSuccessActionDataDecrypted, AesSuccessActionDataResult, Amount, BitcoinAddressData,
CurrencyInfo, FiatCurrency, InputType, LNInvoice, LNOffer, LnUrlAuthRequestData,
LnUrlCallbackStatus, LnUrlError, LnUrlErrorData, LnUrlPayErrorData, LnUrlPayRequest,
LnUrlPayRequestData, LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult,
LnUrlWithdrawSuccessData, LocaleOverrides, LocalizedName, MessageSuccessActionData, Network,
Rate, RouteHint, RouteHintHop, SuccessActionProcessed, Symbol, UrlSuccessActionData,
};
use tokio::sync::Mutex;

Expand Down Expand Up @@ -158,11 +158,33 @@ pub struct _LnUrlWithdrawRequestData {
pub max_withdrawable: u64,
}

#[frb(mirror(Amount))]
pub enum _Amount {
Bitcoin {
amount_msat: u64,
},
Currency {
iso4217_code: String,
fractional_amount: u64,
},
}

#[frb(mirror(LNOffer))]
pub struct _LNOffer {
pub bolt12: String,
pub chains: Vec<String>,
pub amount: Option<Amount>,
pub description: String,
pub absolute_expiry: Option<u64>,
pub issuer: Option<String>,
pub signing_pubkey: String,
}

#[frb(mirror(InputType))]
pub enum _InputType {
BitcoinAddress { address: BitcoinAddressData },
Bolt11 { invoice: LNInvoice },
Bolt12 { offer: String },
Bolt12 { offer: LNOffer },
NodeId { node_id: String },
Url { url: String },
LnUrlPay { data: LnUrlPayRequestData },
Expand Down
76 changes: 75 additions & 1 deletion libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,9 @@ pub struct mirror_AesSuccessActionDataDecrypted(AesSuccessActionDataDecrypted);
#[derive(Clone)]
pub struct mirror_AesSuccessActionDataResult(AesSuccessActionDataResult);

#[derive(Clone)]
pub struct mirror_Amount(Amount);

#[derive(Clone)]
pub struct mirror_BitcoinAddressData(BitcoinAddressData);

Expand All @@ -925,6 +928,9 @@ pub struct mirror_InputType(InputType);
#[derive(Clone)]
pub struct mirror_LNInvoice(LNInvoice);

#[derive(Clone)]
pub struct mirror_LNOffer(LNOffer);

#[derive(Clone)]
pub struct mirror_LnUrlAuthRequestData(LnUrlAuthRequestData);

Expand Down Expand Up @@ -995,6 +1001,18 @@ const _: fn() = || {
let _: String = reason;
}
}
match None::<Amount>.unwrap() {
Amount::Bitcoin { amount_msat } => {
let _: u64 = amount_msat;
}
Amount::Currency {
iso4217_code,
fractional_amount,
} => {
let _: String = iso4217_code;
let _: u64 = fractional_amount;
}
}
{
let BitcoinAddressData = None::<BitcoinAddressData>.unwrap();
let _: String = BitcoinAddressData.address;
Expand Down Expand Up @@ -1026,7 +1044,7 @@ const _: fn() = || {
let _: LNInvoice = invoice;
}
InputType::Bolt12 { offer } => {
let _: String = offer;
let _: LNOffer = offer;
}
InputType::NodeId { node_id } => {
let _: String = node_id;
Expand Down Expand Up @@ -1062,6 +1080,16 @@ const _: fn() = || {
let _: Vec<u8> = LNInvoice.payment_secret;
let _: u64 = LNInvoice.min_final_cltv_expiry_delta;
}
{
let LNOffer = None::<LNOffer>.unwrap();
let _: String = LNOffer.bolt12;
let _: Vec<String> = LNOffer.chains;
let _: Option<Amount> = LNOffer.amount;
let _: String = LNOffer.description;
let _: Option<u64> = LNOffer.absolute_expiry;
let _: Option<String> = LNOffer.issuer;
let _: String = LNOffer.signing_pubkey;
}
{
let LnUrlAuthRequestData = None::<LnUrlAuthRequestData>.unwrap();
let _: String = LnUrlAuthRequestData.k1;
Expand Down Expand Up @@ -1337,6 +1365,31 @@ impl rust2dart::IntoIntoDart<mirror_AesSuccessActionDataResult> for AesSuccessAc
}
}

impl support::IntoDart for mirror_Amount {
fn into_dart(self) -> support::DartAbi {
match self.0 {
Amount::Bitcoin { amount_msat } => {
vec![0.into_dart(), amount_msat.into_into_dart().into_dart()]
}
Amount::Currency {
iso4217_code,
fractional_amount,
} => vec![
1.into_dart(),
iso4217_code.into_into_dart().into_dart(),
fractional_amount.into_into_dart().into_dart(),
],
}
.into_dart()
}
}
impl support::IntoDartExceptPrimitive for mirror_Amount {}
impl rust2dart::IntoIntoDart<mirror_Amount> for Amount {
fn into_into_dart(self) -> mirror_Amount {
mirror_Amount(self)
}
}

impl support::IntoDart for BackupFailedData {
fn into_dart(self) -> support::DartAbi {
vec![self.error.into_into_dart().into_dart()].into_dart()
Expand Down Expand Up @@ -1686,6 +1739,27 @@ impl rust2dart::IntoIntoDart<mirror_LNInvoice> for LNInvoice {
}
}

impl support::IntoDart for mirror_LNOffer {
fn into_dart(self) -> support::DartAbi {
vec![
self.0.bolt12.into_into_dart().into_dart(),
self.0.chains.into_into_dart().into_dart(),
self.0.amount.map(|v| mirror_Amount(v)).into_dart(),
self.0.description.into_into_dart().into_dart(),
self.0.absolute_expiry.into_dart(),
self.0.issuer.into_dart(),
self.0.signing_pubkey.into_into_dart().into_dart(),
]
.into_dart()
}
}
impl support::IntoDartExceptPrimitive for mirror_LNOffer {}
impl rust2dart::IntoIntoDart<mirror_LNOffer> for LNOffer {
fn into_into_dart(self) -> mirror_LNOffer {
mirror_LNOffer(self)
}
}

impl support::IntoDart for LnPaymentDetails {
fn into_dart(self) -> support::DartAbi {
vec![
Expand Down
Loading

0 comments on commit 36ce80c

Please sign in to comment.