|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! Data structures and encoding for `offer` messages. |
| 11 | +
|
| 12 | +use bitcoin::blockdata::constants::genesis_block; |
| 13 | +use bitcoin::hash_types::BlockHash; |
| 14 | +use bitcoin::hashes::sha256; |
| 15 | +use bitcoin::network::constants::Network; |
| 16 | +use bitcoin::secp256k1::PublicKey; |
| 17 | +use bitcoin::secp256k1::schnorr::Signature; |
| 18 | +use core::time::Duration; |
| 19 | +use ln::PaymentHash; |
| 20 | +use ln::features::OfferFeatures; |
| 21 | +use util::ser::WithLength; |
| 22 | + |
| 23 | +use prelude::*; |
| 24 | + |
| 25 | +#[cfg(feature = "std")] |
| 26 | +use std::time::SystemTime; |
| 27 | + |
| 28 | +/// |
| 29 | +#[derive(Clone, Debug)] |
| 30 | +pub struct Offer { |
| 31 | + id: sha256::Hash, |
| 32 | + chains: Option<Vec<BlockHash>>, |
| 33 | + amount: Option<Amount>, |
| 34 | + description: String, |
| 35 | + features: Option<OfferFeatures>, |
| 36 | + absolute_expiry: Option<Duration>, |
| 37 | + issuer: Option<String>, |
| 38 | + paths: Option<Vec<BlindedPath>>, |
| 39 | + quantity_min: Option<u64>, |
| 40 | + quantity_max: Option<u64>, |
| 41 | + node_id: Option<PublicKey>, |
| 42 | + send_invoice: Option<SendInvoice>, |
| 43 | + signature: Option<Signature>, |
| 44 | +} |
| 45 | + |
| 46 | +impl Offer { |
| 47 | + /// |
| 48 | + pub fn id(&self) -> sha256::Hash { |
| 49 | + self.id |
| 50 | + } |
| 51 | + |
| 52 | + /// |
| 53 | + pub fn chain(&self) -> BlockHash { |
| 54 | + // TODO: Update once spec is finalized |
| 55 | + self.chains |
| 56 | + .as_ref() |
| 57 | + .and_then(|chains| chains.first().copied()) |
| 58 | + .unwrap_or_else(|| genesis_block(Network::Bitcoin).block_hash()) |
| 59 | + } |
| 60 | + |
| 61 | + /// |
| 62 | + pub fn amount(&self) -> Option<&Amount> { |
| 63 | + self.amount.as_ref() |
| 64 | + } |
| 65 | + |
| 66 | + /// |
| 67 | + pub fn description(&self) -> &String { |
| 68 | + &self.description |
| 69 | + } |
| 70 | + |
| 71 | + /// |
| 72 | + pub fn features(&self) -> Option<&OfferFeatures> { |
| 73 | + self.features.as_ref() |
| 74 | + } |
| 75 | + |
| 76 | + /// |
| 77 | + pub fn absolute_expiry(&self) -> Option<Duration> { |
| 78 | + self.absolute_expiry |
| 79 | + } |
| 80 | + |
| 81 | + /// |
| 82 | + #[cfg(feature = "std")] |
| 83 | + pub fn is_expired(&self) -> bool { |
| 84 | + match self.absolute_expiry() { |
| 85 | + Some(seconds_from_epoch) => match SystemTime::UNIX_EPOCH.elapsed() { |
| 86 | + Ok(elapsed) => elapsed > seconds_from_epoch, |
| 87 | + Err(_) => false, |
| 88 | + }, |
| 89 | + None => false, |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// |
| 94 | + pub fn issuer(&self) -> Option<&String> { |
| 95 | + self.issuer.as_ref() |
| 96 | + } |
| 97 | + |
| 98 | + /// |
| 99 | + pub fn paths(&self) -> Option<&Vec<BlindedPath>> { |
| 100 | + self.paths.as_ref() |
| 101 | + } |
| 102 | + |
| 103 | + /// |
| 104 | + pub fn quantity_min(&self) -> u64 { |
| 105 | + self.quantity_min.unwrap_or(1) |
| 106 | + } |
| 107 | + |
| 108 | + /// |
| 109 | + pub fn quantity_max(&self) -> u64 { |
| 110 | + self.quantity_max.unwrap_or_else(|| |
| 111 | + self.quantity_min.map_or(1, |_| u64::max_value())) |
| 112 | + } |
| 113 | + |
| 114 | + /// |
| 115 | + pub fn node_id(&self) -> PublicKey { |
| 116 | + self.node_id.unwrap_or_else(|| |
| 117 | + self.paths.as_ref().unwrap().first().unwrap().path.0.last().unwrap().node_id) |
| 118 | + } |
| 119 | + |
| 120 | + /// |
| 121 | + pub fn send_invoice(&self) -> Option<&SendInvoice> { |
| 122 | + self.send_invoice.as_ref() |
| 123 | + } |
| 124 | + |
| 125 | + /// |
| 126 | + pub fn signature(&self) -> Option<&Signature> { |
| 127 | + self.signature.as_ref() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/// The amount required for an item in an [`Offer`] denominated in either bitcoin or another |
| 132 | +/// currency. |
| 133 | +#[derive(Clone, Debug)] |
| 134 | +pub enum Amount { |
| 135 | + /// An amount of bitcoin. |
| 136 | + Bitcoin { |
| 137 | + /// The amount in millisatoshi. |
| 138 | + amount_msats: u64, |
| 139 | + }, |
| 140 | + /// An amount of currency specified using ISO 4712. |
| 141 | + Currency { |
| 142 | + /// The currency that the amount is denominated in. |
| 143 | + iso4217_code: CurrencyCode, |
| 144 | + /// The amount in the currency unit adjusted by the ISO 4712 exponent (e.g., USD cents). |
| 145 | + amount: u64, |
| 146 | + }, |
| 147 | +} |
| 148 | + |
| 149 | +/// An ISO 4712 three-letter currency code (e.g., USD). |
| 150 | +pub type CurrencyCode = [u8; 3]; |
| 151 | + |
| 152 | +/// |
| 153 | +#[derive(Clone, Debug)] |
| 154 | +pub struct SendInvoice { |
| 155 | + refund_for: Option<PaymentHash>, |
| 156 | +} |
| 157 | + |
| 158 | +#[derive(Clone, Debug)] |
| 159 | +/// |
| 160 | +pub struct BlindedPath { |
| 161 | + blinding: PublicKey, |
| 162 | + path: WithLength<Vec<OnionMessagePath>, u8>, |
| 163 | +} |
| 164 | + |
| 165 | +#[derive(Clone, Debug)] |
| 166 | +struct OnionMessagePath { |
| 167 | + node_id: PublicKey, |
| 168 | + encrypted_recipient_data: Vec<u8>, |
| 169 | +} |
0 commit comments