|
12 | 12 |
|
13 | 13 | use bitcoin::bech32;
|
14 | 14 | use bitcoin::bech32::FromBase32;
|
| 15 | +use bitcoin::blockdata::constants::genesis_block; |
15 | 16 | use bitcoin::hash_types::BlockHash;
|
| 17 | +use bitcoin::network::constants::Network; |
16 | 18 | use bitcoin::secp256k1::{PublicKey, XOnlyPublicKey};
|
17 | 19 | use bitcoin::secp256k1::schnorr::Signature;
|
18 | 20 | use core::str::FromStr;
|
| 21 | +use core::time::Duration; |
19 | 22 | use ln::PaymentHash;
|
20 | 23 | use ln::features::OfferFeatures;
|
21 | 24 | use ln::msgs::DecodeError;
|
22 | 25 | use util::ser::{HighZeroBytesDroppedVarInt, Readable, WithLength, WithoutLength};
|
23 | 26 |
|
24 | 27 | use prelude::*;
|
25 | 28 |
|
| 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 | + |
26 | 150 | /// An `offer` TLV stream without any semantic checks, apart from any checks performed when parsing
|
27 | 151 | /// the underlying types.
|
28 | 152 | #[derive(Debug)]
|
@@ -63,7 +187,8 @@ impl_writeable_tlv_stream!(OfferTlvStream, {
|
63 | 187 | });
|
64 | 188 |
|
65 | 189 | #[derive(Debug)]
|
66 |
| -struct BlindedPath { |
| 190 | +/// |
| 191 | +pub struct BlindedPath { |
67 | 192 | blinding: PublicKey,
|
68 | 193 | path: WithLength<Vec<OnionMessagePath>, u8>,
|
69 | 194 | }
|
|
0 commit comments