|
| 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 | +//! Implementation of Lightning Offers |
| 11 | +//! ([BOLT 12](https://github.com/lightning/bolts/blob/master/12-offer-encoding.md)). |
| 12 | +
|
| 13 | +use bitcoin::bech32; |
| 14 | +use bitcoin::bech32::FromBase32; |
| 15 | +use bitcoin::hash_types::BlockHash; |
| 16 | +use bitcoin::secp256k1::{PublicKey, XOnlyPublicKey}; |
| 17 | +use bitcoin::secp256k1::schnorr::Signature; |
| 18 | +use core::str::FromStr; |
| 19 | +use ln::PaymentHash; |
| 20 | +use ln::features::OfferFeatures; |
| 21 | +use ln::msgs::DecodeError; |
| 22 | +use util::ser::{HighZeroBytesDroppedVarInt, Readable, WithLength, WithoutLength}; |
| 23 | + |
| 24 | +use prelude::*; |
| 25 | + |
| 26 | +/// An `offer` TLV stream without any semantic checks, apart from any checks performed when parsing |
| 27 | +/// the underlying types. |
| 28 | +struct OfferTlvStream { |
| 29 | + _empty: (), |
| 30 | + chains: Option<WithoutLength<Vec<BlockHash>>>, |
| 31 | + currency: Option<WithoutLength<String>>, |
| 32 | + amount: Option<HighZeroBytesDroppedVarInt<u64>>, |
| 33 | + description: Option<WithoutLength<String>>, |
| 34 | + features: Option<OfferFeatures>, |
| 35 | + absolute_expiry: Option<HighZeroBytesDroppedVarInt<u64>>, |
| 36 | + paths: Option<WithoutLength<Vec<BlindedPath>>>, |
| 37 | + issuer: Option<WithoutLength<String>>, |
| 38 | + quantity_min: Option<HighZeroBytesDroppedVarInt<u64>>, |
| 39 | + quantity_max: Option<HighZeroBytesDroppedVarInt<u64>>, |
| 40 | + recurrence: Option<Recurrence>, |
| 41 | + node_id: Option<XOnlyPublicKey>, |
| 42 | + send_invoice: Option<()>, |
| 43 | + refund_for: Option<PaymentHash>, |
| 44 | + signature: Option<Signature>, |
| 45 | +} |
| 46 | + |
| 47 | +impl_writeable_msg!(OfferTlvStream, { _empty }, { |
| 48 | + (2, chains, option), |
| 49 | + (6, currency, option), |
| 50 | + (8, amount, option), |
| 51 | + (10, description, option), |
| 52 | + (12, features, option), |
| 53 | + (14, absolute_expiry, option), |
| 54 | + (16, paths, option), |
| 55 | + (20, issuer, option), |
| 56 | + (22, quantity_min, option), |
| 57 | + (24, quantity_max, option), |
| 58 | + (26, recurrence, option), |
| 59 | + (30, node_id, option), |
| 60 | + (34, refund_for, option), |
| 61 | + (54, send_invoice, option), |
| 62 | + (240, signature, option), |
| 63 | +}); |
| 64 | + |
| 65 | +struct BlindedPath { |
| 66 | + blinding: PublicKey, |
| 67 | + path: WithLength<Vec<OnionMessagePath>, u8>, |
| 68 | +} |
| 69 | + |
| 70 | +impl_writeable!(BlindedPath, { blinding, path }); |
| 71 | + |
| 72 | +struct OnionMessagePath { |
| 73 | + node_id: PublicKey, |
| 74 | + encrypted_recipient_data: Vec<u8>, |
| 75 | +} |
| 76 | + |
| 77 | +impl_writeable!(OnionMessagePath, { node_id, encrypted_recipient_data }); |
| 78 | + |
| 79 | +// TODO: Remove once BOLT 12 test vectors are updated. |
| 80 | +struct Recurrence { |
| 81 | + time_unit: u8, |
| 82 | + period: HighZeroBytesDroppedVarInt<u32>, |
| 83 | +} |
| 84 | + |
| 85 | +impl_writeable!(Recurrence, { time_unit, period }); |
0 commit comments