use std::{fmt::Display, str::FromStr, sync::Arc};
use crate::LwkError;
#[derive(uniffi::Object)]
pub struct Bip21 {
inner: lwk_payment_instructions::Bip21,
}
impl Display for Bip21 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl From<lwk_payment_instructions::Bip21> for Bip21 {
fn from(inner: lwk_payment_instructions::Bip21) -> Self {
Self { inner }
}
}
#[uniffi::export]
impl Bip21 {
#[uniffi::constructor]
pub fn new(s: &str) -> Result<Arc<Self>, LwkError> {
let inner = lwk_payment_instructions::Bip21::from_str(s)
.map_err(|e| LwkError::Generic { msg: e })?;
Ok(Arc::new(Self { inner }))
}
pub fn as_str(&self) -> String {
self.inner.as_str().to_string()
}
pub fn address(&self) -> Arc<crate::blockdata::address::BitcoinAddress> {
Arc::new(self.inner.address().into())
}
pub fn amount(&self) -> Option<u64> {
self.inner.amount()
}
pub fn label(&self) -> Option<String> {
self.inner.label()
}
pub fn message(&self) -> Option<String> {
self.inner.message()
}
#[cfg(feature = "lightning")]
pub fn lightning(&self) -> Option<Arc<crate::Bolt11Invoice>> {
self.inner
.lightning()
.and_then(|inv| crate::Bolt11Invoice::new(&inv.to_string()).ok())
}
pub fn offer(&self) -> Option<String> {
self.inner.offer().map(|o| o.to_string())
}
pub fn payjoin(&self) -> Option<String> {
self.inner.payjoin().map(|u| u.to_string())
}
pub fn payjoin_output_substitution(&self) -> bool {
self.inner.payjoin_output_substitution()
}
pub fn silent_payment_address(&self) -> Option<String> {
self.inner.silent_payment_address().map(|sp| sp.to_string())
}
}