Skip to content

Commit

Permalink
Expose Bolt12Offer input type behind "liquid" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Nov 13, 2024
1 parent 36ce80c commit c7abd3d
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 1,120 deletions.
17 changes: 0 additions & 17 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -730,27 +730,10 @@ 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(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,
Amount, BackupFailedData, BackupStatus, BitcoinAddressData, BreezEvent, BreezServices,
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, LNOffer, ListPaymentsRequest, LnPaymentDetails,
InputType, InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails,
LnUrlAuthError, LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayError,
LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData, LnUrlWithdrawError,
LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData,
Expand Down
35 changes: 2 additions & 33 deletions libs/sdk-common/src/input_parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::collections::HashMap;
use std::str::FromStr;

use ::bip21::Uri;
use anyhow::{anyhow, Result};
use bitcoin::bech32;
use bitcoin::bech32::FromBase32;
use lightning::offers::offer::Offer;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use LnUrlRequestData::*;

use crate::prelude::*;
Expand Down Expand Up @@ -186,24 +185,6 @@ pub async fn parse(input: &str) -> Result<InputType> {
return Ok(InputType::Bolt11 { invoice });
}

if let Ok(offer) = input.parse::<Offer>() {
return Ok(InputType::Bolt12 {
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(),
},
});
}

// Public key serialized in compressed form (66 hex chars)
if let Ok(_node_id) = bitcoin::secp256k1::PublicKey::from_str(input) {
return Ok(InputType::NodeId {
Expand Down Expand Up @@ -437,6 +418,7 @@ pub enum InputType {
Bolt11 {
invoice: LNInvoice,
},
#[cfg(feature = "liquid")]
Bolt12 {
offer: LNOffer,
},
Expand Down Expand Up @@ -948,19 +930,6 @@ pub(crate) mod tests {
Ok(())
}

#[tokio::test]
async fn test_bolt12_offer() -> Result<()> {
let bolt12_offer = "lno1pqpzwyq2p32x2um5ypmx2cm5dae8x93pqthvwfzadd7jejes8q9lhc4rvjxd022zv5l44g6qah82ru5rdpnpj";

assert!(matches!(
parse(bolt12_offer).await?,
InputType::Bolt12 { offer }
if offer == bolt12_offer
));

Ok(())
}

#[tokio::test]
async fn test_url() -> Result<()> {
assert!(matches!(
Expand Down
42 changes: 0 additions & 42 deletions libs/sdk-common/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,48 +101,6 @@ 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
35 changes: 6 additions & 29 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, 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,
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,
};
use tokio::sync::Mutex;

Expand Down Expand Up @@ -158,33 +158,10 @@ 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: LNOffer },
NodeId { node_id: String },
Url { url: String },
LnUrlPay { data: LnUrlPayRequestData },
Expand Down
90 changes: 6 additions & 84 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,6 @@ 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 @@ -928,9 +925,6 @@ 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 @@ -1001,18 +995,6 @@ 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 @@ -1043,9 +1025,6 @@ const _: fn() = || {
InputType::Bolt11 { invoice } => {
let _: LNInvoice = invoice;
}
InputType::Bolt12 { offer } => {
let _: LNOffer = offer;
}
InputType::NodeId { node_id } => {
let _: String = node_id;
}
Expand Down Expand Up @@ -1080,16 +1059,6 @@ 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 @@ -1365,31 +1334,6 @@ 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 @@ -1669,18 +1613,17 @@ impl support::IntoDart for mirror_InputType {
InputType::Bolt11 { invoice } => {
vec![1.into_dart(), invoice.into_into_dart().into_dart()]
}
InputType::Bolt12 { offer } => vec![2.into_dart(), offer.into_into_dart().into_dart()],
InputType::NodeId { node_id } => {
vec![3.into_dart(), node_id.into_into_dart().into_dart()]
vec![2.into_dart(), node_id.into_into_dart().into_dart()]
}
InputType::Url { url } => vec![4.into_dart(), url.into_into_dart().into_dart()],
InputType::LnUrlPay { data } => vec![5.into_dart(), data.into_into_dart().into_dart()],
InputType::Url { url } => vec![3.into_dart(), url.into_into_dart().into_dart()],
InputType::LnUrlPay { data } => vec![4.into_dart(), data.into_into_dart().into_dart()],
InputType::LnUrlWithdraw { data } => {
vec![6.into_dart(), data.into_into_dart().into_dart()]
vec![5.into_dart(), data.into_into_dart().into_dart()]
}
InputType::LnUrlAuth { data } => vec![7.into_dart(), data.into_into_dart().into_dart()],
InputType::LnUrlAuth { data } => vec![6.into_dart(), data.into_into_dart().into_dart()],
InputType::LnUrlError { data } => {
vec![8.into_dart(), data.into_into_dart().into_dart()]
vec![7.into_dart(), data.into_into_dart().into_dart()]
}
}
.into_dart()
Expand Down Expand Up @@ -1739,27 +1682,6 @@ 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 c7abd3d

Please sign in to comment.