@@ -392,16 +392,33 @@ pub enum Recipient {
392
392
PhantomNode ,
393
393
}
394
394
395
+ /// A transport and onion encryptor / decryptor that uses the node key
396
+ pub trait ProtocolEncrypt {
397
+ /// Get node secret key (aka node_id or network_key) based on the provided [`Recipient`].
398
+ ///
399
+ /// This method must return the same value each time it is called with a given `Recipient`
400
+ /// parameter.
401
+ fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > ;
402
+ }
403
+
395
404
/// A trait to describe an object which can get user secrets and key material.
396
405
pub trait KeysInterface {
397
406
/// A type which implements Sign which will be returned by get_channel_signer.
398
407
type Signer : Sign ;
408
+ /// A type which implements ProtoconCnrypt which will be returned by get_protocol_keys.
409
+ type ProtocolEncryptor : ProtocolEncrypt ;
410
+
411
+ /// A protocol encryptor / decryptor that uses the node secret key
412
+ fn get_protocol_keys ( & self ) -> Self :: ProtocolEncryptor ;
399
413
400
414
/// Get node secret key (aka node_id or network_key) based on the provided [`Recipient`].
401
415
///
402
416
/// This method must return the same value each time it is called with a given `Recipient`
403
417
/// parameter.
404
- fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > ;
418
+ fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > {
419
+ self . get_protocol_keys ( ) . get_node_secret ( recipient)
420
+ }
421
+
405
422
/// Get a script pubkey which we send funds to when claiming on-chain contestable outputs.
406
423
///
407
424
/// This method should return a different value each time it is called, to avoid linking
@@ -828,6 +845,21 @@ impl ReadableArgs<SecretKey> for InMemorySigner {
828
845
}
829
846
}
830
847
848
+ /// A protocol encryptor that keeps the key in memory
849
+ pub struct InMemoryProtocolEncryptor {
850
+ /// Private key of our node secret, used for signing channel announcements
851
+ pub node_secret : SecretKey ,
852
+ }
853
+
854
+ impl ProtocolEncrypt for InMemoryProtocolEncryptor {
855
+ fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > {
856
+ match recipient {
857
+ Recipient :: Node => Ok ( self . node_secret . clone ( ) ) ,
858
+ Recipient :: PhantomNode => Err ( ( ) )
859
+ }
860
+ }
861
+ }
862
+
831
863
/// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
832
864
/// and derives keys from that.
833
865
///
@@ -1122,12 +1154,10 @@ impl KeysManager {
1122
1154
1123
1155
impl KeysInterface for KeysManager {
1124
1156
type Signer = InMemorySigner ;
1157
+ type ProtocolEncryptor = InMemoryProtocolEncryptor ;
1125
1158
1126
- fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > {
1127
- match recipient {
1128
- Recipient :: Node => Ok ( self . node_secret . clone ( ) ) ,
1129
- Recipient :: PhantomNode => Err ( ( ) )
1130
- }
1159
+ fn get_protocol_keys ( & self ) -> Self :: ProtocolEncryptor {
1160
+ InMemoryProtocolEncryptor { node_secret : self . node_secret }
1131
1161
}
1132
1162
1133
1163
fn get_inbound_payment_key_material ( & self ) -> KeyMaterial {
@@ -1170,7 +1200,7 @@ impl KeysInterface for KeysManager {
1170
1200
fn sign_invoice ( & self , hrp_bytes : & [ u8 ] , invoice_data : & [ u5 ] , recipient : Recipient ) -> Result < RecoverableSignature , ( ) > {
1171
1201
let preimage = construct_invoice_preimage ( & hrp_bytes, & invoice_data) ;
1172
1202
let secret = match recipient {
1173
- Recipient :: Node => self . get_node_secret ( Recipient :: Node ) ?,
1203
+ Recipient :: Node => self . get_protocol_keys ( ) . get_node_secret ( Recipient :: Node ) ?,
1174
1204
Recipient :: PhantomNode => return Err ( ( ) ) ,
1175
1205
} ;
1176
1206
Ok ( self . secp_ctx . sign_recoverable ( & hash_to_message ! ( & Sha256 :: hash( & preimage) ) , & secret) )
@@ -1204,13 +1234,32 @@ pub struct PhantomKeysManager {
1204
1234
phantom_secret : SecretKey ,
1205
1235
}
1206
1236
1207
- impl KeysInterface for PhantomKeysManager {
1208
- type Signer = InMemorySigner ;
1237
+ /// A protocol encryptor that keeps the key in memory, with phantom node support
1238
+ pub struct PhantomProtocolEncryptor {
1239
+ /// Private key of our node secret
1240
+ pub node_secret : SecretKey ,
1241
+ /// Private key of our phantom node secret
1242
+ pub phantom_secret : SecretKey ,
1243
+ }
1209
1244
1245
+ impl ProtocolEncrypt for PhantomProtocolEncryptor {
1210
1246
fn get_node_secret ( & self , recipient : Recipient ) -> Result < SecretKey , ( ) > {
1211
1247
match recipient {
1212
- Recipient :: Node => self . inner . get_node_secret ( Recipient :: Node ) ,
1213
- Recipient :: PhantomNode => Ok ( self . phantom_secret . clone ( ) ) ,
1248
+ Recipient :: Node => Ok ( self . node_secret . clone ( ) ) ,
1249
+ Recipient :: PhantomNode => Ok ( self . phantom_secret . clone ( ) )
1250
+ }
1251
+ }
1252
+ }
1253
+
1254
+
1255
+ impl KeysInterface for PhantomKeysManager {
1256
+ type Signer = InMemorySigner ;
1257
+ type ProtocolEncryptor = PhantomProtocolEncryptor ;
1258
+
1259
+ fn get_protocol_keys ( & self ) -> Self :: ProtocolEncryptor {
1260
+ PhantomProtocolEncryptor {
1261
+ node_secret : self . inner . node_secret ,
1262
+ phantom_secret : self . phantom_secret ,
1214
1263
}
1215
1264
}
1216
1265
0 commit comments