bright_lightning/lnd/models/
hodl_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use base64::prelude::*;
use lightning_invoice::Bolt11Invoice;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct LndHodlInvoice {
    payment_addr: String,
    payment_request: String,
    add_index: String,
}
impl LndHodlInvoice {
    pub fn payment_hash(&self) -> Vec<u8> {
        self.payment_addr.as_bytes().to_vec()
    }
    pub fn payment_request(&self) -> String {
        self.payment_request.clone()
    }
    pub fn r_hash_url_safe(&self) -> anyhow::Result<String> {
        // let r_hash = self
        //     .payment_request
        //     .parse::<Bolt11Invoice>()
        //     .map_err(|e| anyhow::anyhow!(e.to_string()))?
        //     .p();
        let url_safe = BASE64_URL_SAFE.encode(self.payment_addr.as_bytes());
        Ok(url_safe)
    }
    pub fn sat_amount(&self) -> u64 {
        let bolt11 = self.payment_request.clone();
        let bolt11 = bolt11.parse::<Bolt11Invoice>().unwrap();
        bolt11.amount_milli_satoshis().unwrap() / 1000
    }
}
impl TryFrom<String> for LndHodlInvoice {
    type Error = anyhow::Error;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Ok(serde_json::from_str(&value)?)
    }
}
impl TryInto<String> for LndHodlInvoice {
    type Error = anyhow::Error;
    fn try_into(self) -> Result<String, Self::Error> {
        Ok(serde_json::to_string(&self)?)
    }
}
impl Display for LndHodlInvoice {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum HodlState {
    OPEN,
    ACCEPTED,
    CANCELED,
    SETTLED,
}
impl TryFrom<String> for HodlState {
    type Error = anyhow::Error;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        match value.as_str() {
            "OPEN" => Ok(HodlState::OPEN),
            "ACCEPTED" => Ok(HodlState::ACCEPTED),
            "CANCELED" => Ok(HodlState::CANCELED),
            "SETTLED" => Ok(HodlState::SETTLED),
            _ => Err(anyhow::anyhow!("Invalid HodlState")),
        }
    }
}
impl TryInto<String> for HodlState {
    type Error = anyhow::Error;
    fn try_into(self) -> Result<String, Self::Error> {
        match self {
            HodlState::OPEN => Ok("OPEN".to_string()),
            HodlState::ACCEPTED => Ok("ACCEPTED".to_string()),
            HodlState::CANCELED => Ok("CANCELED".to_string()),
            HodlState::SETTLED => Ok("SETTLED".to_string()),
        }
    }
}

impl Display for HodlState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
    }
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LndHodlInvoiceState {
    settled: bool,
    state: HodlState,
    r_hash: String,
    payment_request: String,
}
impl TryFrom<String> for LndHodlInvoiceState {
    type Error = anyhow::Error;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Ok(serde_json::from_str(&value)?)
    }
}
impl TryInto<String> for LndHodlInvoiceState {
    type Error = anyhow::Error;
    fn try_into(self) -> Result<String, Self::Error> {
        Ok(serde_json::to_string(&self)?)
    }
}
impl Display for LndHodlInvoiceState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", serde_json::to_string_pretty(self).unwrap())
    }
}
impl LndHodlInvoiceState {
    pub fn settled(&self) -> bool {
        self.settled
    }
    pub fn state(&self) -> HodlState {
        self.state.clone()
    }
    pub fn r_hash(&self) -> String {
        self.r_hash.clone()
    }
    pub fn r_hash_url_safe(&self) -> String {
        let url_safe = BASE64_URL_SAFE.encode(self.r_hash.as_bytes());
        url_safe
    }
    pub fn payment_request(&self) -> String {
        self.payment_request.clone()
    }
}