@@ -31,6 +31,8 @@ use bitcoin::blockdata::script::Script;
3131use bitcoin:: hash_types:: { Txid , BlockHash } ;
3232
3333use ln:: features:: { ChannelFeatures , ChannelTypeFeatures , InitFeatures , NodeFeatures } ;
34+ use ln:: onion_utils;
35+ use onion_message;
3436
3537use prelude:: * ;
3638use core:: fmt;
@@ -40,7 +42,7 @@ use io_extras::read_to_end;
4042
4143use util:: events:: MessageSendEventsProvider ;
4244use util:: logger;
43- use util:: ser:: { Readable , Writeable , Writer , FixedLengthReader , HighZeroBytesDroppedVarInt , Hostname } ;
45+ use util:: ser:: { LengthReadable , Readable , ReadableArgs , Writeable , Writer , FixedLengthReader , HighZeroBytesDroppedVarInt , Hostname } ;
4446
4547use ln:: { PaymentPreimage , PaymentHash , PaymentSecret } ;
4648
@@ -304,6 +306,14 @@ pub struct UpdateAddHTLC {
304306 pub ( crate ) onion_routing_packet : OnionPacket ,
305307}
306308
309+ /// An onion message to be sent or received from a peer
310+ #[ derive( Clone , Debug , PartialEq ) ]
311+ pub struct OnionMessage {
312+ /// Used in decrypting the onion packet's payload.
313+ pub blinding_point : PublicKey ,
314+ pub ( crate ) onion_routing_packet : onion_message:: Packet ,
315+ }
316+
307317/// An update_fulfill_htlc message to be sent or received from a peer
308318#[ derive( Clone , Debug , PartialEq ) ]
309319pub struct UpdateFulfillHTLC {
@@ -993,6 +1003,18 @@ pub(crate) struct OnionPacket {
9931003 pub ( crate ) hmac : [ u8 ; 32 ] ,
9941004}
9951005
1006+ impl onion_utils:: Packet for OnionPacket {
1007+ type Data = onion_utils:: FixedSizeOnionPacket ;
1008+ fn new ( pubkey : PublicKey , hop_data : onion_utils:: FixedSizeOnionPacket , hmac : [ u8 ; 32 ] ) -> Self {
1009+ Self {
1010+ version : 0 ,
1011+ public_key : Ok ( pubkey) ,
1012+ hop_data : hop_data. 0 ,
1013+ hmac,
1014+ }
1015+ }
1016+ }
1017+
9961018impl PartialEq for OnionPacket {
9971019 fn eq ( & self , other : & OnionPacket ) -> bool {
9981020 for ( i, j) in self . hop_data . iter ( ) . zip ( other. hop_data . iter ( ) ) {
@@ -1327,6 +1349,29 @@ impl_writeable_msg!(UpdateAddHTLC, {
13271349 onion_routing_packet
13281350} , { } ) ;
13291351
1352+ impl Readable for OnionMessage {
1353+ fn read < R : Read > ( r : & mut R ) -> Result < Self , DecodeError > {
1354+ let blinding_point: PublicKey = Readable :: read ( r) ?;
1355+ let len: u16 = Readable :: read ( r) ?;
1356+ let mut packet_reader = FixedLengthReader :: new ( r, len as u64 ) ;
1357+ let onion_routing_packet: onion_message:: Packet = <onion_message:: Packet as LengthReadable >:: read ( & mut packet_reader) ?;
1358+ Ok ( Self {
1359+ blinding_point,
1360+ onion_routing_packet,
1361+ } )
1362+ }
1363+ }
1364+
1365+ impl Writeable for OnionMessage {
1366+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
1367+ self . blinding_point . write ( w) ?;
1368+ let onion_packet_len = self . onion_routing_packet . serialized_length ( ) ;
1369+ ( onion_packet_len as u16 ) . write ( w) ?;
1370+ self . onion_routing_packet . write ( w) ?;
1371+ Ok ( ( ) )
1372+ }
1373+ }
1374+
13301375impl Writeable for FinalOnionHopData {
13311376 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
13321377 self . payment_secret . 0 . write ( w) ?;
@@ -1372,6 +1417,14 @@ impl Writeable for OnionHopData {
13721417 }
13731418}
13741419
1420+ // ReadableArgs because we need onion_utils::decode_next_hop to accommodate payment packets and
1421+ // onion message packets.
1422+ impl ReadableArgs < ( ) > for OnionHopData {
1423+ fn read < R : Read > ( r : & mut R , _arg : ( ) ) -> Result < Self , DecodeError > {
1424+ <Self as Readable >:: read ( r)
1425+ }
1426+ }
1427+
13751428impl Readable for OnionHopData {
13761429 fn read < R : Read > ( mut r : & mut R ) -> Result < Self , DecodeError > {
13771430 use bitcoin:: consensus:: encode:: { Decodable , Error , VarInt } ;
0 commit comments