|
| 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 | +//! Provides data structures and functions for creating and managing Offers messages, |
| 11 | +//! facilitating communication, and handling Bolt12 messages and payments. |
| 12 | +
|
| 13 | +use crate::ln::inbound_payment; |
| 14 | +use crate::onion_message::async_payments::AsyncPaymentsMessage; |
| 15 | +use crate::onion_message::messenger::{MessageRouter, MessageSendInstructions}; |
| 16 | +use crate::onion_message::offers::OffersMessage; |
| 17 | +use crate::sign::EntropySource; |
| 18 | +use crate::sync::Mutex; |
| 19 | +use bitcoin::constants::ChainHash; |
| 20 | +use bitcoin::secp256k1::{PublicKey, Secp256k1}; |
| 21 | +use bitcoin::{secp256k1, Network}; |
| 22 | + |
| 23 | +use core::ops::Deref; |
| 24 | +use core::sync::atomic::AtomicUsize; |
| 25 | + |
| 26 | +#[cfg(feature = "dnssec")] |
| 27 | +use crate::onion_message::dns_resolution::DNSResolverMessage; |
| 28 | + |
| 29 | +pub struct OffersMessageFlow<ES: Deref, MR: Deref> |
| 30 | +where |
| 31 | + ES::Target: EntropySource, |
| 32 | + MR::Target: MessageRouter, |
| 33 | +{ |
| 34 | + chain_hash: ChainHash, |
| 35 | + message_router: MR, |
| 36 | + |
| 37 | + our_network_pubkey: PublicKey, |
| 38 | + highest_seen_timestamp: AtomicUsize, |
| 39 | + inbound_payment_key: inbound_payment::ExpandedKey, |
| 40 | + |
| 41 | + secp_ctx: Secp256k1<secp256k1::All>, |
| 42 | + entropy_source: ES, |
| 43 | + |
| 44 | + #[cfg(not(any(test, feature = "_test_utils")))] |
| 45 | + pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>, |
| 46 | + #[cfg(any(test, feature = "_test_utils"))] |
| 47 | + pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>, |
| 48 | + |
| 49 | + pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>, |
| 50 | + |
| 51 | + #[cfg(feature = "dnssec")] |
| 52 | + pending_dns_onion_messages: Mutex<Vec<(DNSResolverMessage, MessageSendInstructions)>>, |
| 53 | +} |
| 54 | + |
| 55 | +impl<ES: Deref, MR: Deref> OffersMessageFlow<ES, MR> |
| 56 | +where |
| 57 | + ES::Target: EntropySource, |
| 58 | + MR::Target: MessageRouter, |
| 59 | +{ |
| 60 | + /// Creates a new [`OffersMessageFlow`] |
| 61 | + pub fn new( |
| 62 | + network: Network, message_router: MR, our_network_pubkey: PublicKey, |
| 63 | + current_timestamp: u32, inbound_payment_key: inbound_payment::ExpandedKey, |
| 64 | + entropy_source: ES, |
| 65 | + ) -> Self { |
| 66 | + let mut secp_ctx = Secp256k1::new(); |
| 67 | + secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes()); |
| 68 | + |
| 69 | + Self { |
| 70 | + chain_hash: ChainHash::using_genesis_block(network), |
| 71 | + message_router, |
| 72 | + |
| 73 | + our_network_pubkey, |
| 74 | + highest_seen_timestamp: AtomicUsize::new(current_timestamp as usize), |
| 75 | + inbound_payment_key, |
| 76 | + |
| 77 | + secp_ctx, |
| 78 | + entropy_source, |
| 79 | + |
| 80 | + pending_offers_messages: Mutex::new(Vec::new()), |
| 81 | + pending_async_payments_messages: Mutex::new(Vec::new()), |
| 82 | + #[cfg(feature = "dnssec")] |
| 83 | + pending_dns_onion_messages: Mutex::new(Vec::new()), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /// Gets the node_id held by this OffersMessageFlow |
| 88 | + pub fn get_our_node_id(&self) -> PublicKey { |
| 89 | + self.our_network_pubkey |
| 90 | + } |
| 91 | +} |
0 commit comments