use std::{fmt::Display, str::FromStr, sync::Arc};
use crate::LwkError;
#[derive(uniffi::Object)]
pub struct Bip321 {
inner: lwk_payment_instructions::Bip321,
}
impl Display for Bip321 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
impl From<lwk_payment_instructions::Bip321> for Bip321 {
fn from(inner: lwk_payment_instructions::Bip321) -> Self {
Self { inner }
}
}
#[uniffi::export]
impl Bip321 {
#[uniffi::constructor]
pub fn new(s: &str) -> Result<Arc<Self>, LwkError> {
let inner = lwk_payment_instructions::Bip321::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 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())
}
pub fn ark(&self) -> Option<String> {
self.inner.ark()
}
}