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

Parse liquid addresses #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions waila/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ crate-type = ["cdylib", "rlib"]
bech32 = "0.9.1"
bitcoin = { version = "0.30.2", default-features = false, features = ["serde"] }
bip21 = "0.3.1"
elements = { version = "0.23.0", optional = true }
itertools = "0.12.0"
nostr = { version = "0.26.0", default-features = false, features = ["nip47"] }
lnurl-rs = { version = "0.4.0", default-features = false }
Expand All @@ -32,6 +33,7 @@ default = ["std"]
std = ["bitcoin/std", "lightning-invoice/std", "lightning/std", "nostr/std"]
no-std = ["bitcoin/no-std", "lightning-invoice/no-std", "lightning/no-std", "nostr/alloc"]
rgb = ["rgb-std", "rgb-wallet"]
liquid = ["dep:elements"]

[package.metadata.wasm-pack.profile.release]
wasm-opt = true
92 changes: 92 additions & 0 deletions waila/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use bitcoin::blockdata::constants::ChainHash;
use bitcoin::key::XOnlyPublicKey;
use bitcoin::secp256k1::PublicKey;
use bitcoin::{Address, Amount, Network};
#[cfg(feature = "liquid")]
use elements::AddressParams;
use lightning::offers::offer;
use lightning::offers::offer::Offer;
use lightning::offers::refund::Refund;
Expand Down Expand Up @@ -40,6 +42,8 @@ pub enum PaymentParams<'a> {
NostrWalletAuth(NIP49URI),
#[cfg(feature = "rgb")]
Rgb(RgbInvoice),
#[cfg(feature = "liquid")]
Liquid(elements::Address),
}

#[cfg(feature = "rgb")]
Expand Down Expand Up @@ -70,6 +74,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -88,6 +94,13 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(invoice) => invoice.chain.and_then(map_chain_to_network),
#[cfg(feature = "liquid")]
PaymentParams::Liquid(l) => match l.params.clone() {
AddressParams::LIQUID => Some(Network::Bitcoin),
AddressParams::LIQUID_TESTNET => Some(Network::Testnet),
AddressParams::ELEMENTS => None,
_ => None,
},
}
}

Expand Down Expand Up @@ -115,6 +128,8 @@ impl PaymentParams<'_> {
.chain
.and_then(map_chain_to_network)
.map(|n| n == network),
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => self.network().map(|n| n == network),
}
}

Expand All @@ -141,6 +156,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -159,6 +176,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -177,6 +196,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -195,6 +216,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -213,6 +236,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -235,6 +260,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -253,6 +280,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -277,6 +306,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -295,6 +326,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -313,6 +346,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(_) => None,
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -331,6 +366,8 @@ impl PaymentParams<'_> {
PaymentParams::NostrWalletAuth(a) => Some(a.clone()),
#[cfg(feature = "rgb")]
PaymentParams::Rgb(_) => None,
#[cfg(feature = "liquid")]
PaymentParams::Liquid(_) => None,
}
}

Expand All @@ -353,6 +390,15 @@ impl PaymentParams<'_> {
pub fn payjoin_supported(&self) -> bool {
self.payjoin_endpoint().is_some()
}

#[cfg(feature = "liquid")]
pub fn liquid_address(&self) -> Option<elements::Address> {
if let PaymentParams::Liquid(addr) = self {
Some(addr.clone())
} else {
None
}
}
}

// just checks if it has correct HRP and variant
Expand Down Expand Up @@ -412,6 +458,16 @@ impl FromStr for PaymentParams<'_> {
.map_err(|_| ());
}

#[cfg(feature = "liquid")]
if let Ok(addr) = elements::Address::from_str(str) {
return Ok(PaymentParams::Liquid(addr));
} else if lower.starts_with("bitcoin:") {
let substr = &str[8..];
if let Ok(addr) = elements::Address::from_str(substr) {
return Ok(PaymentParams::Liquid(addr));
}
}

Address::from_str(str)
.map(|a| PaymentParams::OnChain(a.assume_checked()))
.or_else(|_| Bolt11Invoice::from_str(str).map(PaymentParams::Bolt11))
Expand Down Expand Up @@ -449,6 +505,9 @@ mod tests {
const SAMPLE_NWA: &str = "nostr+walletauth://b889ff5b1513b641e2a139f661a661364979c5beee91842f8f0ef42ab558e9d4?relay=wss%3A%2F%2Frelay.damus.io&secret=b8a30fafa48d4795b6c0eec169a383de&required_commands=pay_invoice&optional_commands=get_balance&budget=10000%2Fdaily";
#[cfg(feature = "rgb")]
const SAMPLE_RGB_INVOICE: &str = "rgb:Cbw1h3zbHgRhA6sxb4FS3Z7GTpdj9MLb7Do88qh5TUH1/RGB20/1+utxob0KPoUVTWL3WqyY6zsJY5giaugWHt5n4hEeWMQymQJmPRFPXL2n";
#[cfg(feature = "liquid")]
const SAMPLE_LIQUID_ADDR: &str =
"VJLFq37TtUBqkTf1cwxvMkHhaERzhbEsS3K5bGBFWqVCpf2EkvFTyVZ2yvm23oPBQzg2BMJvfYkFSqo7";

#[test]
fn parse_node_pubkey() {
Expand Down Expand Up @@ -480,6 +539,39 @@ mod tests {
assert_eq!(parsed.lnurl(), None);
}

#[cfg(feature = "liquid")]
#[test]
fn parse_liquid_address() {
let address = elements::Address::from_str(SAMPLE_LIQUID_ADDR).unwrap();
let parsed = PaymentParams::from_str(&address.to_string()).unwrap();

assert_eq!(parsed.liquid_address(), Some(address));
assert_eq!(parsed.address(), None);
assert_eq!(parsed.amount(), None);
assert_eq!(parsed.memo(), None);
assert_eq!(parsed.network(), Some(Network::Bitcoin));
assert_eq!(parsed.invoice(), None);
assert_eq!(parsed.node_pubkey(), None);
assert_eq!(parsed.lnurl(), None);
}

#[cfg(feature = "liquid")]
#[test]
fn parse_bip21_liquid_address() {
let string = format!("bitcoin:{SAMPLE_LIQUID_ADDR}");
let address = elements::Address::from_str(SAMPLE_LIQUID_ADDR).unwrap();
let parsed = PaymentParams::from_str(&string).unwrap();

assert_eq!(parsed.liquid_address(), Some(address));
assert_eq!(parsed.address(), None);
assert_eq!(parsed.amount(), None);
assert_eq!(parsed.memo(), None);
assert_eq!(parsed.network(), Some(Network::Bitcoin));
assert_eq!(parsed.invoice(), None);
assert_eq!(parsed.node_pubkey(), None);
assert_eq!(parsed.lnurl(), None);
}

#[test]
fn parse_invoice() {
let parsed = PaymentParams::from_str(SAMPLE_INVOICE).unwrap();
Expand Down