Skip to content

Commit 518dcc0

Browse files
Give us a self when reading a custom onion message
Co-authored-by: Matt Corallo <[email protected]> Co-authored-by: Valentine Wallace <[email protected]>
1 parent 2e343e7 commit 518dcc0

File tree

5 files changed

+36
-31
lines changed

5 files changed

+36
-31
lines changed

lightning/src/ln/onion_utils.rs

-8
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,6 @@ impl DecodeInput for PaymentHash {
606606
fn read_arg(self) -> Self::Arg { () }
607607
}
608608

609-
impl DecodeInput for SharedSecret {
610-
type Arg = SharedSecret;
611-
fn payment_hash(&self) -> Option<&PaymentHash> {
612-
None
613-
}
614-
fn read_arg(self) -> Self::Arg { self }
615-
}
616-
617609
/// Allows `decode_next_hop` to return the next hop packet bytes for either payments or onion
618610
/// message forwards.
619611
pub(crate) trait NextPacketBytes: AsMut<[u8]> {

lightning/src/ln/peer_handler.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::ln::features::{InitFeatures, NodeFeatures};
2121
use crate::ln::msgs;
2222
use crate::ln::msgs::{ChannelMessageHandler, LightningError, NetAddress, OnionMessageHandler, RoutingMessageHandler};
2323
use crate::ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
24-
use crate::util::ser::{MaybeReadableArgs, VecWriter, Writeable, Writer};
24+
use crate::util::ser::{VecWriter, Writeable, Writer};
2525
use crate::ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
2626
use crate::ln::wire;
2727
use crate::ln::wire::Encode;
@@ -97,13 +97,11 @@ impl OnionMessageHandler for IgnoringMessageHandler {
9797
}
9898
impl CustomOnionMessageHandler for IgnoringMessageHandler {
9999
type CustomMessage = Infallible;
100-
fn handle_custom_message(&self, _msg: Self::CustomMessage) {
100+
fn handle_custom_message(&self, _msg: Infallible) {
101101
// Since we always return `None` in the read the handle method should never be called.
102102
unreachable!();
103103
}
104-
}
105-
impl MaybeReadableArgs<u64> for Infallible {
106-
fn read<R: io::Read>(_buffer: &mut R, _msg_type: u64) -> Result<Option<Self>, msgs::DecodeError> where Self: Sized {
104+
fn read<R: io::Read>(&self, _msg_type: u64, _buffer: &mut R) -> Result<Option<Infallible>, msgs::DecodeError> where Self: Sized {
107105
Ok(None)
108106
}
109107
}

lightning/src/onion_message/functional_tests.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ impl Writeable for TestCustomMessage {
5454
}
5555
}
5656

57-
impl MaybeReadableArgs<u64> for TestCustomMessage {
58-
fn read<R: io::Read>(buffer: &mut R, message_type: u64) -> Result<Option<Self>, DecodeError> where Self: Sized {
57+
struct TestCustomMessageHandler {}
58+
59+
impl CustomOnionMessageHandler for TestCustomMessageHandler {
60+
type CustomMessage = TestCustomMessage;
61+
fn handle_custom_message(&self, _msg: Self::CustomMessage) {}
62+
fn read<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<TestCustomMessage>, DecodeError> where Self: Sized {
5963
if message_type == CUSTOM_MESSAGE_TYPE {
6064
let mut buf = Vec::new();
6165
buffer.read_to_end(&mut buf)?;
@@ -66,13 +70,6 @@ impl MaybeReadableArgs<u64> for TestCustomMessage {
6670
}
6771
}
6872

69-
struct TestCustomMessageHandler {}
70-
71-
impl CustomOnionMessageHandler for TestCustomMessageHandler {
72-
type CustomMessage = TestCustomMessage;
73-
fn handle_custom_message(&self, _msg: Self::CustomMessage) {}
74-
}
75-
7673
fn create_nodes(num_messengers: u8) -> Vec<MessengerNode> {
7774
let mut nodes = Vec::new();
7875
for i in 0..num_messengers {

lightning/src/onion_message/messenger.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ use bitcoin::hashes::{Hash, HashEngine};
1414
use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1515
use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
17+
use bitcoin::secp256k1::ecdh::SharedSecret;
1718

1819
use crate::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager, Recipient, Sign};
20+
use crate::ln::PaymentHash;
1921
use crate::ln::features::{InitFeatures, NodeFeatures};
2022
use crate::ln::msgs::{self, OnionMessageHandler};
2123
use crate::ln::onion_utils;
@@ -29,6 +31,7 @@ use crate::util::logger::Logger;
2931
use crate::util::ser::Writeable;
3032

3133
use core::ops::Deref;
34+
use crate::io;
3235
use crate::sync::{Arc, Mutex};
3336
use crate::prelude::*;
3437

@@ -178,6 +181,9 @@ pub trait CustomOnionMessageHandler {
178181
type CustomMessage: CustomOnionMessageContents;
179182
/// Called with the custom message that was received.
180183
fn handle_custom_message(&self, msg: Self::CustomMessage);
184+
/// Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
185+
/// message type is unknown.
186+
fn read<R: io::Read>(&self, message_type: u64, buffer: &mut R) -> Result<Option<Self::CustomMessage>, msgs::DecodeError>;
181187
}
182188

183189
impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessenger<Signer, K, L, CMH>
@@ -276,10 +282,19 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, Ve
276282
false
277283
}
278284

285+
impl<'a, T: CustomOnionMessageHandler> onion_utils::DecodeInput for (SharedSecret, &'a T) {
286+
type Arg = (SharedSecret, &'a T);
287+
fn payment_hash(&self) -> Option<&PaymentHash> {
288+
None
289+
}
290+
fn read_arg(self) -> Self::Arg { self }
291+
}
292+
279293
impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessageHandler for OnionMessenger<Signer, K, L, CMH>
280294
where K::Target: KeysInterface<Signer = Signer>,
281295
L::Target: Logger,
282-
CMH::Target: CustomOnionMessageHandler,
296+
CMH::Target: CustomOnionMessageHandler + Sized, // XXX: No idea why rustc desperately
297+
// wants Sized here, it's kinda dumb
283298
{
284299
/// Handle an incoming onion message. Currently, if a message was destined for us we will log, but
285300
/// soon we'll delegate the onion message to a handler that can generate invoices or send
@@ -309,7 +324,7 @@ impl<Signer: Sign, K: Deref, L: Deref, CMH: Deref> OnionMessageHandler for Onion
309324
}
310325
};
311326
match onion_utils::decode_next_hop(onion_decode_ss, &msg.onion_routing_packet.hop_data[..],
312-
msg.onion_routing_packet.hmac, control_tlvs_ss)
327+
msg.onion_routing_packet.hmac, (control_tlvs_ss, &*self.custom_handler))
313328
{
314329
Ok((Payload::Receive::<<<CMH as Deref>::Target as CustomOnionMessageHandler>::CustomMessage> {
315330
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path,

lightning/src/onion_message/packet.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use bitcoin::secp256k1::ecdh::SharedSecret;
1515
use crate::ln::msgs::DecodeError;
1616
use crate::ln::onion_utils;
1717
use super::blinded_route::{BlindedRoute, ForwardTlvs, ReceiveTlvs};
18+
use super::messenger::CustomOnionMessageHandler;
1819
use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
19-
use crate::util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, MaybeReadableArgs, Readable, ReadableArgs, Writeable, Writer};
20+
use crate::util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer};
2021

2122
use core::cmp;
2223
use crate::io::{self, Read};
@@ -106,7 +107,7 @@ pub(super) enum Payload<T: CustomOnionMessageContents> {
106107
#[derive(Debug)]
107108
/// The contents of an onion message. In the context of offers, this would be the invoice, invoice
108109
/// request, or invoice error.
109-
pub enum OnionMessageContents<T> where T: CustomOnionMessageContents {
110+
pub enum OnionMessageContents<T: CustomOnionMessageContents> {
110111
// Coming soon:
111112
// Invoice,
112113
// InvoiceRequest,
@@ -115,7 +116,7 @@ pub enum OnionMessageContents<T> where T: CustomOnionMessageContents {
115116
Custom(T),
116117
}
117118

118-
impl<T> OnionMessageContents<T> where T: CustomOnionMessageContents {
119+
impl<T: CustomOnionMessageContents> OnionMessageContents<T> {
119120
/// Returns the type that was used to decode the message payload.
120121
pub fn tlv_type(&self) -> u64 {
121122
match self {
@@ -134,7 +135,7 @@ impl<T: CustomOnionMessageContents> Writeable for OnionMessageContents<T> {
134135

135136
/// The contents of a custom onion message. Must implement `MaybeReadableArgs<u64>` where the `u64`
136137
/// is the custom TLV type attempting to be read, and return `Ok(None)` if the TLV type is unknown.
137-
pub trait CustomOnionMessageContents: Writeable + MaybeReadableArgs<u64> {
138+
pub trait CustomOnionMessageContents: Writeable {
138139
/// Returns the TLV type identifying the message contents. MUST be >= 64.
139140
fn tlv_type(&self) -> u64;
140141
}
@@ -198,8 +199,10 @@ impl<T: CustomOnionMessageContents> Writeable for (Payload<T>, [u8; 32]) {
198199
}
199200

200201
// Uses the provided secret to simultaneously decode and decrypt the control TLVs and data TLV.
201-
impl<T: CustomOnionMessageContents> ReadableArgs<SharedSecret> for Payload<T> {
202-
fn read<R: Read>(r: &mut R, encrypted_tlvs_ss: SharedSecret) -> Result<Self, DecodeError> {
202+
impl<H: CustomOnionMessageHandler> ReadableArgs<(SharedSecret, &H)> for Payload<<H as CustomOnionMessageHandler>::CustomMessage> {
203+
fn read<R: Read>(r: &mut R, args: (SharedSecret, &H)) -> Result<Self, DecodeError> {
204+
let (encrypted_tlvs_ss, handler) = args;
205+
203206
let v: BigSize = Readable::read(r)?;
204207
let mut rd = FixedLengthReader::new(r, v.0);
205208
let mut reply_path: Option<BlindedRoute> = None;
@@ -216,7 +219,7 @@ impl<T: CustomOnionMessageContents> ReadableArgs<SharedSecret> for Payload<T> {
216219
if message_type.is_some() { return Err(DecodeError::InvalidValue) }
217220

218221
message_type = Some(msg_type);
219-
match T::read(msg_reader, msg_type) {
222+
match handler.read(msg_type, msg_reader) {
220223
Ok(Some(msg)) => {
221224
message = Some(msg);
222225
Ok(true)

0 commit comments

Comments
 (0)