Skip to content

Commit

Permalink
Move bolt12 parsing to sdk-common
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Jan 6, 2025
1 parent ba6c411 commit 16fbb39
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 69 deletions.
4 changes: 2 additions & 2 deletions cli/Cargo.lock

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

4 changes: 2 additions & 2 deletions lib/Cargo.lock

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

4 changes: 1 addition & 3 deletions lib/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ env_logger = "0.11"
flutter_rust_bridge = { version = "=2.4.0", features = [
"chrono",
], optional = true }
# We need at least lightning v0.0.125 for the Bolt12 structs. The lightning version from sdk-common is too old (v0.0.118, matching vls-core).
lightning = "0.0.125"
log = { workspace = true }
lwk_common = "0.7.0"
lwk_signer = "0.7.0"
lwk_wollet = { git = "https://github.com/dangeross/lwk", branch = "savage-full-scan-to-index" }
#lwk_wollet = "0.7.0"
rusqlite = { version = "0.31", features = ["backup", "bundled"] }
rusqlite_migration = "1.0"
sdk-common = { git = "https://github.com/breez/breez-sdk", rev = "f77208acd34d74b571388889e856444908c59a85", features = ["liquid"] }
sdk-common = { git = "https://github.com/breez/breez-sdk", rev = "da61ce31e937fb7bad452aaf89ff53471b19bc3a", features = ["liquid"] }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.116"
strum = "0.25"
Expand Down
65 changes: 5 additions & 60 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ use crate::{
persist::Persister,
utils, *,
};
use ::lightning::offers::invoice::Bolt12Invoice;
use ::lightning::offers::offer::Offer;
use sdk_common::lightning_125::offers::invoice::Bolt12Invoice;

use self::sync::client::BreezSyncerClient;
use self::sync::SyncService;
Expand Down Expand Up @@ -2614,7 +2613,7 @@ impl LiquidSdk {

/// Prepares to pay to an LNURL encoded pay request or lightning address.
///
/// This is the second step of LNURL-pay flow. The first step is [parse], which also validates the LNURL
/// This is the second step of LNURL-pay flow. The first step is [LiquidSdk::parse], which also validates the LNURL
/// destination and generates the [LnUrlPayRequest] payload needed here.
///
/// This call will validate the `amount_msat` and `comment` parameters of `req` against the parameters
Expand All @@ -2625,7 +2624,7 @@ impl LiquidSdk {
/// # Arguments
///
/// * `req` - the [PrepareLnUrlPayRequest] containing:
/// * `data` - the [LnUrlPayRequestData] returned by [parse]
/// * `data` - the [LnUrlPayRequestData] returned by [LiquidSdk::parse]
/// * `amount_msat` - the amount in millisatoshis for this payment
/// * `comment` - an optional comment for this payment
/// * `validate_success_action_url` - validates that, if there is a URL success action, the URL domain matches
Expand Down Expand Up @@ -2784,7 +2783,7 @@ impl LiquidSdk {
})
}

/// Second step of LNURL-withdraw. The first step is [parse], which also validates the LNURL destination
/// Second step of LNURL-withdraw. The first step is [LiquidSdk::parse], which also validates the LNURL destination
/// and generates the [LnUrlWithdrawRequest] payload needed here.
///
/// This call will validate the given `amount_msat` against the parameters
Expand Down Expand Up @@ -2840,7 +2839,7 @@ impl LiquidSdk {
Ok(res)
}

/// Third and last step of LNURL-auth. The first step is [parse], which also validates the LNURL destination
/// Third and last step of LNURL-auth. The first step is [LiquidSdk::parse], which also validates the LNURL destination
/// and generates the [LnUrlAuthRequestData] payload needed here. The second step is user approval of auth action.
///
/// This call will sign `k1` of the LNURL endpoint (`req_data`) on `secp256k1` using `linkingPrivKey` and DER-encodes the signature.
Expand Down Expand Up @@ -2924,60 +2923,6 @@ impl LiquidSdk {
///
/// Can optionally be configured to use external input parsers by providing `external_input_parsers` in [Config].
pub async fn parse(&self, input: &str) -> Result<InputType, PaymentError> {
if let Ok(offer) = input.parse::<Offer>() {
// 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
let min_amount = offer
.amount()
.map(|amount| match amount {
::lightning::offers::offer::Amount::Bitcoin { amount_msats } => {
Ok(Amount::Bitcoin {
amount_msat: amount_msats,
})
}
::lightning::offers::offer::Amount::Currency {
iso4217_code,
amount,
} => Ok(Amount::Currency {
iso4217_code: String::from_utf8(iso4217_code.to_vec()).map_err(|_| {
anyhow!("Expecting a valid ISO 4217 character sequence")
})?,
fractional_amount: amount,
}),
})
.transpose()
.map_err(|e: anyhow::Error| {
PaymentError::generic(&format!("Failed to reconstruct amount: {e:?}"))
})?;

return Ok(InputType::Bolt12Offer {
offer: LNOffer {
offer: input.to_string(),
chains: offer
.chains()
.iter()
.map(|chain| chain.to_string())
.collect(),
min_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()),
paths: offer
.paths()
.iter()
.map(|path| LnOfferBlindedPath {
blinded_hops: path
.blinded_hops()
.iter()
.map(|hop| hop.blinded_node_id.to_hex())
.collect(),
})
.collect::<Vec<LnOfferBlindedPath>>(),
},
});
}

let external_parsers = &self.external_input_parsers;
parse(input, Some(external_parsers))
.await
Expand Down
4 changes: 2 additions & 2 deletions lib/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::time::{SystemTime, UNIX_EPOCH};

use crate::error::{PaymentError, SdkResult};
use anyhow::{anyhow, ensure, Result};
use lightning::offers::invoice::Bolt12Invoice;
use lwk_wollet::elements::encode::deserialize;
use lwk_wollet::elements::hex::FromHex;
use lwk_wollet::elements::{
Expand All @@ -12,6 +11,7 @@ use lwk_wollet::elements::{
};
use sdk_common::bitcoin::bech32;
use sdk_common::bitcoin::bech32::FromBase32;
use sdk_common::lightning_125::offers::invoice::Bolt12Invoice;

pub(crate) fn now() -> u32 {
SystemTime::now()
Expand Down Expand Up @@ -62,6 +62,6 @@ pub(crate) fn parse_bolt12_invoice(invoice: &str) -> Result<Bolt12Invoice> {

let data = Vec::<u8>::from_base32(&data)?;

lightning::offers::invoice::Bolt12Invoice::try_from(data)
sdk_common::lightning_125::offers::invoice::Bolt12Invoice::try_from(data)
.map_err(|e| anyhow!("Failed to parse BOLT12: {e:?}"))
}

0 comments on commit 16fbb39

Please sign in to comment.