@@ -73,14 +73,31 @@ pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
73
73
Hmac :: from_engine ( hmac) . into_inner ( )
74
74
}
75
75
76
+ /// Used in the construction of keys to build the onion routing packet for payments and onion
77
+ /// messages, in `construct_onion_keys_callback`.
78
+ ///
79
+ /// `construct_onion_keys_callback` needs to be able to take a path of `RouteHop`s, as we use the
80
+ /// information in the `RouteHop` in processing onion faiilures. However, for onion messages, we
81
+ /// don't have a `RouteHop` and instead only a list of node ids for the path. Thus this enum allows
82
+ /// `construct_onion_keys_callback` to accommodate both use cases.
83
+ pub ( super ) enum Hop {
84
+ PublicKey ( & PublicKey ) ,
85
+ Routing ( & RouteHop ) ,
86
+ }
87
+
88
+ impl Hop {
89
+ /// Retrieve the `Hop`'s node id.
90
+ fn pubkey ( & self ) -> & PublicKey { }
91
+ }
92
+
76
93
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
77
94
#[ inline]
78
- pub ( super ) fn construct_onion_keys_callback < T : secp256k1:: Signing , FType : FnMut ( SharedSecret , [ u8 ; 32 ] , PublicKey , & RouteHop , usize ) > ( secp_ctx : & Secp256k1 < T > , path : & Vec < RouteHop > , session_priv : & SecretKey , mut callback : FType ) -> Result < ( ) , secp256k1:: Error > {
95
+ fn construct_onion_keys_callback < T : secp256k1:: Signing , FType : FnMut ( SharedSecret , [ u8 ; 32 ] , PublicKey , & Hop , usize ) > ( secp_ctx : & Secp256k1 < T > , path : & Vec < Hop > , session_priv : & SecretKey , mut callback : FType ) -> Result < ( ) , secp256k1:: Error > {
79
96
let mut blinded_priv = session_priv. clone ( ) ;
80
97
let mut blinded_pub = PublicKey :: from_secret_key ( secp_ctx, & blinded_priv) ;
81
98
82
99
for ( idx, hop) in path. iter ( ) . enumerate ( ) {
83
- let shared_secret = SharedSecret :: new ( & hop. pubkey , & blinded_priv) ;
100
+ let shared_secret = SharedSecret :: new ( hop. pubkey ( ) , & blinded_priv) ;
84
101
85
102
let mut sha = Sha256 :: engine ( ) ;
86
103
sha. input ( & blinded_pub. serialize ( ) [ ..] ) ;
@@ -98,11 +115,20 @@ pub(super) fn construct_onion_keys_callback<T: secp256k1::Signing, FType: FnMut(
98
115
Ok ( ( ) )
99
116
}
100
117
118
+ /// Construct keys for sending an onion message along the given `path`.
119
+ ///
120
+ /// Returns keys for encrypting the `encrypted_data` field of the onion message and keys for
121
+ /// encrypting the onion message's onion routing packet.
122
+ pub ( super ) fn construct_onion_message_keys < T : secp256k1:: Signing + secp256k1:: Verification > ( secp_ctx : & Secp256k1 < T > , path : Vec < & PublicKey > , session_priv : & SecretKey ) -> Result < ( Vec < [ u8 ; 32 ] > , Vec < OnionKeys > ) , secp256k1:: Error > {
123
+ // calls `construct_onion_keys_callback`
124
+ }
125
+
101
126
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
102
127
pub ( super ) fn construct_onion_keys < T : secp256k1:: Signing > ( secp_ctx : & Secp256k1 < T > , path : & Vec < RouteHop > , session_priv : & SecretKey ) -> Result < Vec < OnionKeys > , secp256k1:: Error > {
103
128
let mut res = Vec :: with_capacity ( path. len ( ) ) ;
104
129
105
- construct_onion_keys_callback ( secp_ctx, path, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _, _| {
130
+ let hops = path. iter ( ) . map ( |hop| Hop :: Routing ( & hop) ) ;
131
+ construct_onion_keys_callback ( secp_ctx, hops, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _, _| {
106
132
let ( rho, mu) = gen_rho_mu_from_shared_secret ( & shared_secret[ ..] ) ;
107
133
108
134
res. push ( OnionKeys {
@@ -119,6 +145,10 @@ pub(super) fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T
119
145
Ok ( res)
120
146
}
121
147
148
+ /// Builds an onion message payload for each hop in the `path`, ready to be encoded in the onion
149
+ /// routing packet.
150
+ pub ( super ) fn build_onion_message_payloads ( mut path : Vec < PublicKey > ) -> Result < Vec < msgs:: OnionMsgPayload > , APIError > { }
151
+
122
152
/// returns the hop data, as well as the first-hop value_msat and CLTV value we should send.
123
153
pub ( super ) fn build_onion_payloads ( path : & Vec < RouteHop > , total_msat : u64 , payment_secret_option : & Option < PaymentSecret > , starting_htlc_offset : u32 , keysend_preimage : & Option < PaymentPreimage > ) -> Result < ( Vec < msgs:: OnionHopData > , u64 , u32 ) , APIError > {
124
154
let mut cur_value_msat = 0u64 ;
@@ -199,6 +229,8 @@ pub(super) fn route_size_insane(payloads: &Vec<msgs::OnionHopData>) -> bool {
199
229
}
200
230
201
231
/// panics if route_size_insane(paylods)
232
+ // NOTE: I believe since onion messages can be variable size, we'll have to remove the panic
233
+ // mentioned above^
202
234
pub ( super ) fn construct_onion_packet ( payloads : Vec < msgs:: OnionHopData > , onion_keys : Vec < OnionKeys > , prng_seed : [ u8 ; 32 ] , associated_data : & PaymentHash ) -> msgs:: OnionPacket {
203
235
let mut packet_data = [ 0 ; ONION_DATA_LEN ] ;
204
236
@@ -208,6 +240,14 @@ pub(super) fn construct_onion_packet(payloads: Vec<msgs::OnionHopData>, onion_ke
208
240
construct_onion_packet_with_init_noise ( payloads, onion_keys, packet_data, associated_data)
209
241
}
210
242
243
+ /// Constructs the onion routing packet for onion messages.
244
+ // NOTE: this could prob be DRY'd with `construct_onion_packet`, but it'd lead to a large-ish diff,
245
+ // and it's a small method.
246
+ pub ( super ) fn construct_onion_message_packet ( payloads : Vec < msgs:: OnionMsgPayload > , encrypted_data_keys : Vec < [ u8 ; 32 ] > , onion_packet_keys : Vec < OnionKeys > , prng_seed : [ u8 ; 32 ] ) -> msgs:: OnionPacket {
247
+ // calls:
248
+ // * construct_onion_packet_with_init_noise
249
+ }
250
+
211
251
#[ cfg( test) ]
212
252
// Used in testing to write bogus OnionHopDatas, which is otherwise not representable in
213
253
// msgs::OnionHopData.
@@ -221,7 +261,8 @@ pub(super) fn construct_onion_packet_bogus_hopdata<HD: Writeable>(payloads: Vec<
221
261
}
222
262
223
263
/// panics if route_size_insane(paylods)
224
- fn construct_onion_packet_with_init_noise < HD : Writeable > ( mut payloads : Vec < HD > , onion_keys : Vec < OnionKeys > , mut packet_data : [ u8 ; ONION_DATA_LEN ] , associated_data : & PaymentHash ) -> msgs:: OnionPacket {
264
+ fn construct_onion_packet_with_init_noise < HD : Writeable > ( mut payloads : Vec < HD > , onion_keys : Vec < OnionKeys > , mut packet_data : [ u8 ; ONION_DATA_LEN ] , associated_data : Option < & PaymentHash > ) -> msgs:: OnionPacket {
265
+ // calls `msgs::OnionMsgPayload::write`
225
266
let filler = {
226
267
const ONION_HOP_DATA_LEN : usize = 65 ; // We may decrease this eventually after TLV is common
227
268
let mut res = Vec :: with_capacity ( ONION_HOP_DATA_LEN * ( payloads. len ( ) - 1 ) ) ;
@@ -507,7 +548,7 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
507
548
}
508
549
509
550
/// Data decrypted from the onion payload.
510
- pub ( crate ) enum Hop {
551
+ pub ( crate ) enum HopPayload {
511
552
/// This onion payload was for us, not for forwarding to a next-hop. Contains information for
512
553
/// verifying the incoming payment.
513
554
Receive ( msgs:: OnionHopData ) ,
0 commit comments