bright_lightning/lnd/models/
invoice.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use base64::prelude::*;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct LndInvoice {
    r_hash: String,
    payment_request: String,
    add_index: String,
    payment_addr: String,
}
impl TryFrom<String> for LndInvoice {
    type Error = anyhow::Error;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Ok(serde_json::from_str(&value)?)
    }
}
impl TryInto<String> for LndInvoice {
    type Error = anyhow::Error;
    fn try_into(self) -> Result<String, Self::Error> {
        Ok(serde_json::to_string(&self)?)
    }
}
impl Display for LndInvoice {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
    }
}
impl LndInvoice {
    pub fn r_hash(&self) -> String {
        self.r_hash.clone()
    }
    pub fn r_hash_url_safe(&self) -> String {
        let unsafe_str = BASE64_STANDARD.decode(&self.r_hash).unwrap();
        let url_safe = BASE64_URL_SAFE.encode(unsafe_str);
        url_safe
    }
    pub fn r_hash_hex(&self) -> String {
        let unsafe_str = BASE64_STANDARD.decode(&self.r_hash).unwrap();
        let hex = unsafe_str.iter().map(|b| format!("{:02x}", b)).collect::<String>();
        hex
    }
    pub fn payment_hash(&self) -> Vec<u8> {
        BASE64_STANDARD.decode(&self.payment_addr).unwrap()
    }
    pub fn payment_request(&self) -> String {
        self.payment_request.clone()
    }
    pub fn add_index(&self) -> String {
        self.add_index.clone()
    }
}