Add baseline OnionMessenger and msgs::OnionMessage and its serialization
[rust-lightning] / lightning / src / onion_message / messenger.rs
diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs
new file mode 100644 (file)
index 0000000..e2d7b51
--- /dev/null
@@ -0,0 +1,71 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! LDK sends, receives, and forwards onion messages via the [`OnionMessenger`]. See its docs for
+//! more information.
+
+use bitcoin::secp256k1::{self, PublicKey, Secp256k1};
+
+use chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Sign};
+use ln::msgs;
+use util::logger::Logger;
+
+use core::ops::Deref;
+use sync::{Arc, Mutex};
+use prelude::*;
+
+/// A sender, receiver and forwarder of onion messages. In upcoming releases, this object will be
+/// used to retrieve invoices and fulfill invoice requests from [offers].
+///
+/// [offers]: <https://github.com/lightning/bolts/pull/798>
+pub struct OnionMessenger<Signer: Sign, K: Deref, L: Deref>
+       where K::Target: KeysInterface<Signer = Signer>,
+             L::Target: Logger,
+{
+       keys_manager: K,
+       logger: L,
+       pending_messages: Mutex<HashMap<PublicKey, Vec<msgs::OnionMessage>>>,
+       secp_ctx: Secp256k1<secp256k1::All>,
+       // Coming soon:
+       // invoice_handler: InvoiceHandler,
+       // custom_handler: CustomHandler, // handles custom onion messages
+}
+
+impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
+       where K::Target: KeysInterface<Signer = Signer>,
+             L::Target: Logger,
+{
+       /// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
+       /// their respective handlers.
+       pub fn new(keys_manager: K, logger: L) -> Self {
+               let mut secp_ctx = Secp256k1::new();
+               secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
+               OnionMessenger {
+                       keys_manager,
+                       pending_messages: Mutex::new(HashMap::new()),
+                       secp_ctx,
+                       logger,
+               }
+       }
+}
+
+// TODO: parameterize the below Simple* types with OnionMessenger and handle the messages it
+// produces
+/// Useful for simplifying the parameters of [`SimpleArcChannelManager`] and
+/// [`SimpleArcPeerManager`]. See their docs for more details.
+///
+///[`SimpleArcChannelManager`]: crate::ln::channelmanager::SimpleArcChannelManager
+///[`SimpleArcPeerManager`]: crate::ln::peer_handler::SimpleArcPeerManager
+pub type SimpleArcOnionMessenger<L> = OnionMessenger<InMemorySigner, Arc<KeysManager>, Arc<L>>;
+/// Useful for simplifying the parameters of [`SimpleRefChannelManager`] and
+/// [`SimpleRefPeerManager`]. See their docs for more details.
+///
+///[`SimpleRefChannelManager`]: crate::ln::channelmanager::SimpleRefChannelManager
+///[`SimpleRefPeerManager`]: crate::ln::peer_handler::SimpleRefPeerManager
+pub type SimpleRefOnionMessenger<'a, 'b, L> = OnionMessenger<InMemorySigner, &'a KeysManager, &'b L>;