bright_lightning/lnd/models/
invoice.rs1use base64::prelude::*;
2use serde::{Deserialize, Serialize};
3use std::fmt::Display;
4
5use super::OnchainAddressType;
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
8pub enum LndInvoiceState {
9 #[serde(rename = "OPEN")]
10 Open,
11 #[serde(rename = "SETTLED")]
12 Settled,
13 #[serde(rename = "CANCELED")]
14 Canceled,
15 #[serde(rename = "ACCEPTED")]
16 Accepted,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
20pub struct LndPaymentInvoice {
21 pub r_hash: String,
22 pub payment_request: String,
23 pub add_index: String,
24 pub payment_addr: String,
25}
26impl TryFrom<String> for LndPaymentInvoice {
27 type Error = anyhow::Error;
28 fn try_from(value: String) -> Result<Self, Self::Error> {
29 Ok(serde_json::from_str(&value)?)
30 }
31}
32impl TryInto<String> for LndPaymentInvoice {
33 type Error = anyhow::Error;
34 fn try_into(self) -> Result<String, Self::Error> {
35 Ok(serde_json::to_string(&self)?)
36 }
37}
38impl Display for LndPaymentInvoice {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
41 }
42}
43impl LndPaymentInvoice {
44 #[must_use]
45 pub fn r_hash_url_safe(&self) -> String {
46 let unsafe_str = BASE64_STANDARD.decode(&self.r_hash).unwrap();
47
48 BASE64_URL_SAFE.encode(unsafe_str)
49 }
50 #[must_use]
51 pub fn r_hash_hex(&self) -> String {
52 let unsafe_str = BASE64_STANDARD.decode(&self.r_hash).unwrap();
53 let hex = unsafe_str.iter().fold(String::new(), |mut acc, b| {
54 acc.push_str(&format!("{b:02x}"));
55 acc
56 });
57 hex
58 }
59 #[must_use]
60 pub fn payment_hash(&self) -> Vec<u8> {
61 BASE64_STANDARD.decode(&self.payment_addr).unwrap()
62 }
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
66pub struct LndInvoice {
67 pub r_preimage: String,
68 pub r_hash: String,
69 pub payment_request: String,
70 pub add_index: String,
71 pub payment_addr: String,
72 #[serde(skip_serializing_if = "Option::is_none")]
73 pub memo: Option<String>,
74 pub value: String,
75 pub value_msat: String,
76 pub settled: bool,
77 pub creation_date: String,
78 pub settle_date: String,
79 pub state: LndInvoiceState,
80}
81impl TryFrom<String> for LndInvoice {
82 type Error = anyhow::Error;
83 fn try_from(value: String) -> Result<Self, Self::Error> {
84 Ok(serde_json::from_str(&value)?)
85 }
86}
87impl Display for LndInvoice {
88 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89 write!(f, "{}", serde_json::to_string(self).unwrap_or_default())
90 }
91}
92impl LndInvoice {
93 #[must_use]
94 pub fn r_hash_url_safe(&self) -> String {
95 let unsafe_str = BASE64_STANDARD.decode(&self.r_hash).unwrap_or_default();
96
97 BASE64_URL_SAFE.encode(unsafe_str)
98 }
99 #[must_use]
100 pub fn r_hash_hex(&self) -> String {
101 let unsafe_str = BASE64_STANDARD.decode(&self.r_hash).unwrap_or_default();
102 let hex = unsafe_str.iter().fold(String::new(), |mut acc, b| {
103 acc.push_str(&format!("{b:02x}"));
104 acc
105 });
106 hex
107 }
108 #[must_use]
109 pub fn payment_hash(&self) -> Vec<u8> {
110 BASE64_STANDARD
111 .decode(&self.payment_addr)
112 .unwrap_or_default()
113 }
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
117pub struct LndInvoiceList {
118 pub invoices: Vec<LndInvoice>,
119 pub last_index_offset: String,
120 pub first_index_offset: String,
121}
122impl TryFrom<String> for LndInvoiceList {
123 type Error = anyhow::Error;
124 fn try_from(value: String) -> Result<Self, Self::Error> {
125 Ok(serde_json::from_str(&value)?)
126 }
127}
128impl std::fmt::Display for LndInvoiceList {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
131 }
132}
133#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
134pub struct LndNewAddress {
135 pub addr: String,
136}
137impl TryFrom<String> for LndNewAddress {
138 type Error = anyhow::Error;
139 fn try_from(value: String) -> Result<Self, Self::Error> {
140 Ok(serde_json::from_str(&value)?)
141 }
142}
143impl std::fmt::Display for LndNewAddress {
144 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145 write!(f, "{}", serde_json::to_string(self).unwrap_or_default())
146 }
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
150pub struct LndNextAddressRequest {
151 account: String,
152 #[serde(rename = "type")]
153 address_type: OnchainAddressType,
154 change: bool,
155}
156impl TryFrom<String> for LndNextAddressRequest {
157 type Error = anyhow::Error;
158 fn try_from(value: String) -> Result<Self, Self::Error> {
159 Ok(serde_json::from_str(&value)?)
160 }
161}
162impl std::fmt::Display for LndNextAddressRequest {
163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
164 write!(f, "{}", serde_json::to_string(self).unwrap_or_default())
165 }
166}
167impl Default for LndNextAddressRequest {
168 fn default() -> Self {
169 Self {
170 account: String::new(),
171 address_type: OnchainAddressType::TaprootPubkey,
172 change: false,
173 }
174 }
175}