Skip to content

Commit 9152b15

Browse files
committed
WIP: Offer struct for BOLT 12
1 parent cadba18 commit 9152b15

File tree

1 file changed

+126
-1
lines changed

1 file changed

+126
-1
lines changed

lightning/src/offers/mod.rs

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,141 @@
1212
1313
use bitcoin::bech32;
1414
use bitcoin::bech32::FromBase32;
15+
use bitcoin::blockdata::constants::genesis_block;
1516
use bitcoin::hash_types::BlockHash;
17+
use bitcoin::network::constants::Network;
1618
use bitcoin::secp256k1::{PublicKey, XOnlyPublicKey};
1719
use bitcoin::secp256k1::schnorr::Signature;
1820
use core::str::FromStr;
21+
use core::time::Duration;
1922
use ln::PaymentHash;
2023
use ln::features::OfferFeatures;
2124
use ln::msgs::DecodeError;
2225
use util::ser::{HighZeroBytesDroppedVarInt, Readable, WithLength, WithoutLength};
2326

2427
use prelude::*;
2528

29+
#[cfg(feature = "std")]
30+
use std::time::SystemTime;
31+
32+
///
33+
#[derive(Debug)]
34+
pub struct Offer {
35+
chains: Option<Vec<BlockHash>>,
36+
amount: Option<Amount>,
37+
description: String,
38+
features: Option<OfferFeatures>,
39+
absolute_expiry: Option<Duration>,
40+
issuer: Option<String>,
41+
destination: Destination,
42+
quantity_min: Option<u64>,
43+
quantity_max: Option<u64>,
44+
recurrence: Option<Recurrence>,
45+
send_invoice: Option<SendInvoice>,
46+
signature: Option<Signature>,
47+
}
48+
49+
impl Offer {
50+
///
51+
pub fn chain(&self) -> BlockHash {
52+
self.chains
53+
.as_ref()
54+
.and_then(|chains| chains.first().copied())
55+
.unwrap_or_else(|| genesis_block(Network::Bitcoin).block_hash())
56+
}
57+
58+
///
59+
pub fn amount(&self) -> Option<&Amount> {
60+
self.amount.as_ref()
61+
}
62+
63+
///
64+
pub fn description(&self) -> &String {
65+
&self.description
66+
}
67+
68+
///
69+
pub fn features(&self) -> Option<&OfferFeatures> {
70+
self.features.as_ref()
71+
}
72+
73+
///
74+
pub fn absolute_expiry(&self) -> Option<Duration> {
75+
self.absolute_expiry
76+
}
77+
78+
///
79+
#[cfg(feature = "std")]
80+
pub fn is_expired(&self) -> bool {
81+
match self.absolute_expiry() {
82+
Some(seconds_from_epoch) => match SystemTime::UNIX_EPOCH.elapsed() {
83+
Ok(elapsed) => elapsed > seconds_from_epoch,
84+
Err(_) => false,
85+
},
86+
None => false,
87+
}
88+
}
89+
90+
///
91+
pub fn issuer(&self) -> Option<&String> {
92+
self.issuer.as_ref()
93+
}
94+
95+
///
96+
pub fn destination(&self) -> &Destination {
97+
&self.destination
98+
}
99+
100+
///
101+
pub fn quantity_min(&self) -> u64 {
102+
self.quantity_min.unwrap_or(1)
103+
}
104+
105+
///
106+
pub fn quantity_max(&self) -> u64 {
107+
self.quantity_max.unwrap_or_else(||
108+
self.quantity_min.map_or(1, |_| u64::max_value()))
109+
}
110+
111+
///
112+
pub fn signature(&self) -> Option<&Signature> {
113+
self.signature.as_ref()
114+
}
115+
}
116+
117+
/// The amount required for an item in an [`Offer`] denominated in either bitcoin or another
118+
/// currency.
119+
#[derive(Debug)]
120+
pub enum Amount {
121+
/// An amount of bitcoin.
122+
Bitcoin {
123+
/// The amount in millisatoshi.
124+
amount_msats: u64,
125+
},
126+
/// An amount of currency specified using ISO 4712.
127+
Currency {
128+
/// An ISO 4712 three-letter currency code (e.g., USD).
129+
iso4217_code: String,
130+
/// The amount in the currency unit adjusted by the ISO 4712 exponent (e.g., USD cents).
131+
amount: u64,
132+
},
133+
}
134+
135+
///
136+
#[derive(Debug)]
137+
pub enum Destination {
138+
///
139+
NodeId(XOnlyPublicKey),
140+
///
141+
Paths(Vec<BlindedPath>),
142+
}
143+
144+
///
145+
#[derive(Debug)]
146+
pub struct SendInvoice {
147+
refund_for: Option<PaymentHash>,
148+
}
149+
26150
/// An `offer` TLV stream without any semantic checks, apart from any checks performed when parsing
27151
/// the underlying types.
28152
#[derive(Debug)]
@@ -63,7 +187,8 @@ impl_writeable_tlv_stream!(OfferTlvStream, {
63187
});
64188

65189
#[derive(Debug)]
66-
struct BlindedPath {
190+
///
191+
pub struct BlindedPath {
67192
blinding: PublicKey,
68193
path: WithLength<Vec<OnionMessagePath>, u8>,
69194
}

0 commit comments

Comments
 (0)