12
12
//! [`BlindedPath`]: crate::blinded_path::BlindedPath
13
13
14
14
use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 , SecretKey } ;
15
-
16
15
#[ allow( unused_imports) ]
17
16
use crate :: prelude:: * ;
18
17
19
18
use crate :: blinded_path:: { BlindedHop , BlindedPath , IntroductionNode , NextMessageHop , NodeIdLookUp } ;
20
19
use crate :: blinded_path:: utils;
21
20
use crate :: io;
22
21
use crate :: io:: Cursor ;
23
- use crate :: ln:: { PaymentHash , onion_utils} ;
24
22
use crate :: ln:: channelmanager:: PaymentId ;
23
+ use crate :: ln:: msgs:: DecodeError ;
24
+ use crate :: ln:: { PaymentHash , onion_utils} ;
25
25
use crate :: offers:: nonce:: Nonce ;
26
26
use crate :: onion_message:: packet:: ControlTlvs ;
27
27
use crate :: sign:: { NodeSigner , Recipient } ;
28
28
use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
29
- use crate :: util:: ser:: { FixedLengthReader , LengthReadableArgs , Writeable , Writer } ;
29
+ use crate :: util:: ser:: { FixedLengthReader , LengthReadableArgs , Readable , Writeable , Writer } ;
30
30
31
31
use core:: mem;
32
32
use core:: ops:: Deref ;
@@ -57,7 +57,7 @@ pub(crate) struct ReceiveTlvs {
57
57
/// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
58
58
/// sending to. This is useful for receivers to check that said blinded path is being used in
59
59
/// the right context.
60
- pub ( crate ) path_id : Option < [ u8 ; 32 ] > ,
60
+ pub context : Option < MessageContext >
61
61
}
62
62
63
63
impl Writeable for ForwardTlvs {
@@ -80,15 +80,42 @@ impl Writeable for ReceiveTlvs {
80
80
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
81
81
// TODO: write padding
82
82
encode_tlv_stream ! ( writer, {
83
- ( 6 , self . path_id , option) ,
83
+ ( 65537 , self . context , option) ,
84
84
} ) ;
85
85
Ok ( ( ) )
86
86
}
87
87
}
88
88
89
+ impl Readable for ReceiveTlvs {
90
+ fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
91
+ _init_and_read_tlv_stream ! ( r, {
92
+ ( 65537 , context, option) ,
93
+ } ) ;
94
+ Ok ( ReceiveTlvs { context } )
95
+ }
96
+ }
97
+
98
+ /// Represents additional data included by the recipient in a [`BlindedPath`].
89
99
///
90
- pub enum OffersContext {
100
+ /// This data is encrypted and will be included when sending through
101
+ /// [`BlindedPath`]. The recipient can authenticate the message and
102
+ /// utilize it for further processing if needed.
103
+ #[ derive( Clone , Debug ) ]
104
+ pub enum MessageContext {
105
+ /// Represents the data specific to [`OffersMessage`]
91
106
///
107
+ /// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
108
+ Offers ( OffersContext ) ,
109
+ /// Represents the data appended with Custom Onion Message.
110
+ Custom ( Vec < u8 > ) ,
111
+ }
112
+
113
+ /// Contains the data specific to [`OffersMessage`]
114
+ ///
115
+ /// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
116
+ #[ derive( Clone , Debug ) ]
117
+ pub enum OffersContext {
118
+ /// Represents an unknown context.
92
119
Unknown { } ,
93
120
///
94
121
InvoiceRequest {
@@ -100,13 +127,31 @@ pub enum OffersContext {
100
127
///
101
128
payment_hash : PaymentHash ,
102
129
} ,
103
- ///
130
+ /// Represents an outbound BOLT12 payment context.
104
131
OutboundPayment {
105
- ///
132
+ /// Payment ID of the outbound BOLT12 payment.
106
133
payment_id : PaymentId
107
134
} ,
108
135
}
109
136
137
+ impl_writeable_tlv_based_enum ! ( MessageContext , ;
138
+ ( 0 , Offers ) ,
139
+ ( 1 , Custom ) ,
140
+ ) ;
141
+
142
+ impl_writeable_tlv_based_enum ! ( OffersContext ,
143
+ ( 0 , Unknown ) => { } ,
144
+ ( 1 , InvoiceRequest ) => {
145
+ ( 0 , nonce, required) ,
146
+ } ,
147
+ ( 2 , InboundPayment ) => {
148
+ ( 0 , payment_hash, required) ,
149
+ } ,
150
+ ( 3 , OutboundPayment ) => {
151
+ ( 0 , payment_id, required) ,
152
+ } ,
153
+ ; ) ;
154
+
110
155
/// Construct blinded onion message hops for the given `intermediate_nodes` and `recipient_node_id`.
111
156
pub ( super ) fn blinded_hops < T : secp256k1:: Signing + secp256k1:: Verification > (
112
157
secp_ctx : & Secp256k1 < T > , intermediate_nodes : & [ ForwardNode ] , recipient_node_id : PublicKey ,
@@ -122,7 +167,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
122
167
None => NextMessageHop :: NodeId ( * pubkey) ,
123
168
} )
124
169
. map ( |next_hop| ControlTlvs :: Forward ( ForwardTlvs { next_hop, next_blinding_override : None } ) )
125
- . chain ( core:: iter:: once ( ControlTlvs :: Receive ( ReceiveTlvs { path_id : None } ) ) ) ;
170
+ . chain ( core:: iter:: once ( ControlTlvs :: Receive ( ReceiveTlvs { context : None } ) ) ) ;
126
171
127
172
utils:: construct_blinded_hops ( secp_ctx, pks, tlvs, session_priv)
128
173
}
0 commit comments