@@ -29,18 +29,11 @@ use crate::io;
29
29
use crate :: prelude:: * ;
30
30
31
31
// TODO: DRY with onion_utils::construct_onion_keys_callback
32
- #[ inline]
33
- pub ( crate ) fn construct_keys_callback < ' a , T , I , F > (
34
- secp_ctx : & Secp256k1 < T > , unblinded_path : I , destination : Option < Destination > ,
35
- session_priv : & SecretKey , mut callback : F
36
- ) -> Result < ( ) , secp256k1:: Error >
37
- where
38
- T : secp256k1:: Signing + secp256k1:: Verification ,
39
- I : Iterator < Item =& ' a PublicKey > ,
40
- F : FnMut ( PublicKey , SharedSecret , PublicKey , [ u8 ; 32 ] , Option < PublicKey > , Option < Vec < u8 > > ) ,
32
+ macro_rules! build_keys_helper {
33
+ ( $session_priv: ident, $secp_ctx: ident, $callback: ident) =>
41
34
{
42
- let mut msg_blinding_point_priv = session_priv. clone ( ) ;
43
- let mut msg_blinding_point = PublicKey :: from_secret_key ( secp_ctx, & msg_blinding_point_priv) ;
35
+ let mut msg_blinding_point_priv = $ session_priv. clone( ) ;
36
+ let mut msg_blinding_point = PublicKey :: from_secret_key( $ secp_ctx, & msg_blinding_point_priv) ;
44
37
let mut onion_packet_pubkey_priv = msg_blinding_point_priv. clone( ) ;
45
38
let mut onion_packet_pubkey = msg_blinding_point. clone( ) ;
46
39
@@ -54,13 +47,13 @@ where
54
47
hmac. input( encrypted_data_ss. as_ref( ) ) ;
55
48
Hmac :: from_engine( hmac) . to_byte_array( )
56
49
} ;
57
- $pk. mul_tweak( secp_ctx, & Scalar :: from_be_bytes( hop_pk_blinding_factor) . unwrap( ) ) ?
50
+ $pk. mul_tweak( $ secp_ctx, & Scalar :: from_be_bytes( hop_pk_blinding_factor) . unwrap( ) ) ?
58
51
} ;
59
52
let onion_packet_ss = SharedSecret :: new( & blinded_hop_pk, & onion_packet_pubkey_priv) ;
60
53
61
54
let rho = onion_utils:: gen_rho_from_shared_secret( encrypted_data_ss. as_ref( ) ) ;
62
55
let unblinded_pk_opt = if $blinded { None } else { Some ( $pk) } ;
63
- callback( blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload) ;
56
+ $ callback( blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload) ;
64
57
( encrypted_data_ss, onion_packet_ss)
65
58
} }
66
59
}
77
70
} ;
78
71
79
72
msg_blinding_point_priv = msg_blinding_point_priv. mul_tweak( & Scalar :: from_be_bytes( msg_blinding_point_blinding_factor) . unwrap( ) ) ?;
80
- msg_blinding_point = PublicKey :: from_secret_key( secp_ctx, & msg_blinding_point_priv) ;
73
+ msg_blinding_point = PublicKey :: from_secret_key( $ secp_ctx, & msg_blinding_point_priv) ;
81
74
82
75
let onion_packet_pubkey_blinding_factor = {
83
76
let mut sha = Sha256 :: engine( ) ;
86
79
Sha256 :: from_engine( sha) . to_byte_array( )
87
80
} ;
88
81
onion_packet_pubkey_priv = onion_packet_pubkey_priv. mul_tweak( & Scalar :: from_be_bytes( onion_packet_pubkey_blinding_factor) . unwrap( ) ) ?;
89
- onion_packet_pubkey = PublicKey :: from_secret_key( secp_ctx, & onion_packet_pubkey_priv) ;
82
+ onion_packet_pubkey = PublicKey :: from_secret_key( $ secp_ctx, & onion_packet_pubkey_priv) ;
90
83
} ;
91
84
}
85
+ } }
86
+
87
+ #[ inline]
88
+ pub ( crate ) fn construct_keys_callback < ' a , T , I , F > (
89
+ secp_ctx : & Secp256k1 < T > , unblinded_path : I , destination : Option < Destination > ,
90
+ session_priv : & SecretKey , mut callback : F
91
+ ) -> Result < ( ) , secp256k1:: Error >
92
+ where
93
+ T : secp256k1:: Signing + secp256k1:: Verification ,
94
+ I : Iterator < Item =& ' a PublicKey > ,
95
+ F : FnMut ( PublicKey , SharedSecret , PublicKey , [ u8 ; 32 ] , Option < PublicKey > , Option < Vec < u8 > > ) ,
96
+ {
97
+ build_keys_helper ! ( session_priv, secp_ctx, callback) ;
92
98
93
99
for pk in unblinded_path {
94
100
build_keys_in_loop ! ( * pk, false , None ) ;
0 commit comments