1use std::{fmt::Display, str::FromStr, sync::Arc};
4
5use crate::LwkError;
6
7#[derive(uniffi::Object)]
14pub struct Bip321 {
15 inner: lwk_payment_instructions::Bip321,
16}
17
18impl Display for Bip321 {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 write!(f, "{}", self.inner)
21 }
22}
23
24impl From<lwk_payment_instructions::Bip321> for Bip321 {
25 fn from(inner: lwk_payment_instructions::Bip321) -> Self {
26 Self { inner }
27 }
28}
29
30#[uniffi::export]
31impl Bip321 {
32 #[uniffi::constructor]
34 pub fn new(s: &str) -> Result<Arc<Self>, LwkError> {
35 let inner = lwk_payment_instructions::Bip321::from_str(s)
36 .map_err(|e| LwkError::Generic { msg: e })?;
37 Ok(Arc::new(Self { inner }))
38 }
39
40 pub fn as_str(&self) -> String {
42 self.inner.as_str().to_string()
43 }
44
45 pub fn amount(&self) -> Option<u64> {
47 self.inner.amount()
48 }
49
50 pub fn label(&self) -> Option<String> {
52 self.inner.label()
53 }
54
55 pub fn message(&self) -> Option<String> {
57 self.inner.message()
58 }
59
60 #[cfg(feature = "lightning")]
62 pub fn lightning(&self) -> Option<Arc<crate::Bolt11Invoice>> {
63 self.inner
64 .lightning()
65 .and_then(|inv| crate::Bolt11Invoice::new(&inv.to_string()).ok())
66 }
67
68 pub fn offer(&self) -> Option<String> {
70 self.inner.offer().map(|o| o.to_string())
71 }
72
73 pub fn payjoin(&self) -> Option<String> {
75 self.inner.payjoin().map(|u| u.to_string())
76 }
77
78 pub fn payjoin_output_substitution(&self) -> bool {
80 self.inner.payjoin_output_substitution()
81 }
82
83 pub fn silent_payment_address(&self) -> Option<String> {
85 self.inner.silent_payment_address().map(|sp| sp.to_string())
86 }
87
88 pub fn ark(&self) -> Option<String> {
90 self.inner.ark()
91 }
92}