Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand InputType with new variant: Bolt12 offer #1121

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion libs/sdk-common/src/input_parser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +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 serde::{Deserialize, Serialize};
use std::collections::HashMap;
use LnUrlRequestData::*;

use crate::prelude::*;
Expand Down Expand Up @@ -418,6 +418,10 @@ pub enum InputType {
Bolt11 {
invoice: LNInvoice,
},
#[cfg(feature = "liquid")]
Bolt12Offer {
offer: LNOffer,
},
NodeId {
node_id: String,
},
Expand Down
57 changes: 57 additions & 0 deletions libs/sdk-common/src/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::num::ParseIntError;
use std::str::FromStr;
use std::time::{SystemTimeError, UNIX_EPOCH};

use anyhow::anyhow;
use bitcoin::secp256k1::{self, PublicKey};
use hex::ToHex;
use lightning::routing::gossip::RoutingFees;
Expand Down Expand Up @@ -101,6 +102,62 @@ 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,
},
/// An amount of currency specified using ISO 4712.
Currency {
/// The currency that the amount is denominated in.
iso4217_code: String,
ok300 marked this conversation as resolved.
Show resolved Hide resolved
/// The amount in the currency unit adjusted by the ISO 4712 exponent (e.g., USD cents).
fractional_amount: u64,
ok300 marked this conversation as resolved.
Show resolved Hide resolved
},
}

impl TryFrom<lightning::offers::offer::Amount> for Amount {
type Error = anyhow::Error;

fn try_from(amount: lightning::offers::offer::Amount) -> Result<Self, Self::Error> {
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,
}),
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct LNOffer {
/// String representation of the Bolt12 offer
pub offer: String,
pub chains: Vec<String>,
/// If set, it represents the minimum amount that an invoice must have to be valid for this offer
pub min_amount: Option<Amount>,
pub description: Option<String>,
/// Epoch time from which an invoice should no longer be requested. If None, the offer does not expire.
pub absolute_expiry: Option<u64>,
pub issuer: Option<String>,
/// The public key used by the recipient to sign invoices.
pub signing_pubkey: Option<String>,
pub paths: Vec<LNOfferBlindedPath>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct LNOfferBlindedPath {
/// For each blinded hop, we store the node ID (pubkey as hex).
pub blinded_hops: Vec<String>,
}

/// Wrapper for a BOLT11 LN invoice
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct LNInvoice {
Expand Down
Loading