@@ -31,6 +31,8 @@ use bitcoin::blockdata::script::Script;
31
31
use bitcoin:: hash_types:: { Txid , BlockHash } ;
32
32
33
33
use ln:: features:: { ChannelFeatures , ChannelTypeFeatures , InitFeatures , NodeFeatures } ;
34
+ use ln:: onion_utils;
35
+ use onion_message;
34
36
35
37
use prelude:: * ;
36
38
use core:: fmt;
@@ -40,7 +42,7 @@ use io_extras::read_to_end;
40
42
41
43
use util:: events:: MessageSendEventsProvider ;
42
44
use util:: logger;
43
- use util:: ser:: { Readable , Writeable , Writer , FixedLengthReader , HighZeroBytesDroppedVarInt , Hostname } ;
45
+ use util:: ser:: { LengthReadable , Readable , ReadableArgs , Writeable , Writer , FixedLengthReader , HighZeroBytesDroppedVarInt , Hostname } ;
44
46
45
47
use ln:: { PaymentPreimage , PaymentHash , PaymentSecret } ;
46
48
@@ -304,6 +306,14 @@ pub struct UpdateAddHTLC {
304
306
pub ( crate ) onion_routing_packet : OnionPacket ,
305
307
}
306
308
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
+
307
317
/// An update_fulfill_htlc message to be sent or received from a peer
308
318
#[ derive( Clone , Debug , PartialEq ) ]
309
319
pub struct UpdateFulfillHTLC {
@@ -993,6 +1003,18 @@ pub(crate) struct OnionPacket {
993
1003
pub ( crate ) hmac : [ u8 ; 32 ] ,
994
1004
}
995
1005
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
+
996
1018
impl PartialEq for OnionPacket {
997
1019
fn eq ( & self , other : & OnionPacket ) -> bool {
998
1020
for ( i, j) in self . hop_data . iter ( ) . zip ( other. hop_data . iter ( ) ) {
@@ -1327,6 +1349,29 @@ impl_writeable_msg!(UpdateAddHTLC, {
1327
1349
onion_routing_packet
1328
1350
} , { } ) ;
1329
1351
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
+
1330
1375
impl Writeable for FinalOnionHopData {
1331
1376
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
1332
1377
self . payment_secret . 0 . write ( w) ?;
@@ -1372,6 +1417,14 @@ impl Writeable for OnionHopData {
1372
1417
}
1373
1418
}
1374
1419
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
+
1375
1428
impl Readable for OnionHopData {
1376
1429
fn read < R : Read > ( mut r : & mut R ) -> Result < Self , DecodeError > {
1377
1430
use bitcoin:: consensus:: encode:: { Decodable , Error , VarInt } ;
0 commit comments