@@ -794,6 +794,13 @@ pub trait ChannelSigner {
794794 fn channel_keys_id ( & self ) -> [ u8 ; 32 ] ;
795795}
796796
797+ /// Represents Secret Key used for encrypting Peer Storage.
798+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
799+ pub struct PeerStorageKey {
800+ /// Represents the key used to encrypt and decrypt Peer Storage.
801+ pub inner : [ u8 ; 32 ] ,
802+ }
803+
797804/// Specifies the recipient of an invoice.
798805///
799806/// This indicates to [`NodeSigner::sign_invoice`] what node secret key should be used to sign
@@ -832,6 +839,15 @@ pub trait NodeSigner {
832839 /// [phantom node payments]: PhantomKeysManager
833840 fn get_inbound_payment_key ( & self ) -> ExpandedKey ;
834841
842+ /// Defines a method to derive a 32-byte encryption key for peer storage.
843+ ///
844+ /// Implementations of this method must derive a secure encryption key.
845+ /// The key is used to encrypt or decrypt backups of our state stored with our peers.
846+ ///
847+ /// Thus, if you wish to rely on recovery using this method, you should use a key which
848+ /// can be re-derived from data which would be available after state loss (eg the wallet seed).
849+ fn get_peer_storage_key ( & self ) -> PeerStorageKey ;
850+
835851 /// Get node id based on the provided [`Recipient`].
836852 ///
837853 /// This method must return the same value each time it is called with a given [`Recipient`]
@@ -1771,6 +1787,7 @@ pub struct KeysManager {
17711787 shutdown_pubkey : PublicKey ,
17721788 channel_master_key : Xpriv ,
17731789 channel_child_index : AtomicUsize ,
1790+ peer_storage_key : PeerStorageKey ,
17741791
17751792 #[ cfg( test) ]
17761793 pub ( crate ) entropy_source : RandomBytes ,
@@ -1839,6 +1856,10 @@ impl KeysManager {
18391856 . private_key ;
18401857 let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
18411858 inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
1859+ let peer_storage_key: SecretKey = master_key
1860+ . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 6 ) . unwrap ( ) )
1861+ . expect ( "Your RNG is busted" )
1862+ . private_key ;
18421863
18431864 let mut rand_bytes_engine = Sha256 :: engine ( ) ;
18441865 rand_bytes_engine. input ( & starting_time_secs. to_be_bytes ( ) ) ;
@@ -1854,6 +1875,8 @@ impl KeysManager {
18541875 node_id,
18551876 inbound_payment_key : ExpandedKey :: new ( inbound_pmt_key_bytes) ,
18561877
1878+ peer_storage_key : PeerStorageKey { inner : peer_storage_key. secret_bytes ( ) } ,
1879+
18571880 destination_script,
18581881 shutdown_pubkey,
18591882
@@ -2079,6 +2102,10 @@ impl NodeSigner for KeysManager {
20792102 self . inbound_payment_key . clone ( )
20802103 }
20812104
2105+ fn get_peer_storage_key ( & self ) -> PeerStorageKey {
2106+ self . peer_storage_key . clone ( )
2107+ }
2108+
20822109 fn sign_invoice (
20832110 & self , invoice : & RawBolt11Invoice , recipient : Recipient ,
20842111 ) -> Result < RecoverableSignature , ( ) > {
@@ -2240,6 +2267,10 @@ impl NodeSigner for PhantomKeysManager {
22402267 self . inbound_payment_key . clone ( )
22412268 }
22422269
2270+ fn get_peer_storage_key ( & self ) -> PeerStorageKey {
2271+ self . inner . peer_storage_key . clone ( )
2272+ }
2273+
22432274 fn sign_invoice (
22442275 & self , invoice : & RawBolt11Invoice , recipient : Recipient ,
22452276 ) -> Result < RecoverableSignature , ( ) > {
0 commit comments