@@ -796,6 +796,17 @@ pub trait ChannelSigner {
796796 fn channel_keys_id ( & self ) -> [ u8 ; 32 ] ;
797797}
798798
799+ /// Represents Secret Key used for encrypting Peer Storage.
800+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
801+ pub struct PeerStorageKey {
802+ /// In chanmon_consistency we derive this key.
803+ #[ cfg( fuzzing) ]
804+ pub inner : [ u8 ; 32 ] ,
805+ /// Represents the key used to encrypt and decrypt Peer Storage.
806+ #[ cfg( not( fuzzing) ) ]
807+ pub inner : [ u8 ; 32 ] ,
808+ }
809+
799810/// Specifies the recipient of an invoice.
800811///
801812/// This indicates to [`NodeSigner::sign_invoice`] what node secret key should be used to sign
@@ -834,6 +845,15 @@ pub trait NodeSigner {
834845 /// [phantom node payments]: PhantomKeysManager
835846 fn get_inbound_payment_key ( & self ) -> ExpandedKey ;
836847
848+ /// Defines a method to derive a 32-byte encryption key for peer storage.
849+ ///
850+ /// Implementations of this method must derive a secure encryption key.
851+ /// The key is used to encrypt or decrypt backups of our state stored with our peers.
852+ ///
853+ /// Thus, if you wish to rely on recovery using this method, you should use a key which
854+ /// can be re-derived from data which would be available after state loss (eg the wallet seed).
855+ fn get_peer_storage_key ( & self ) -> PeerStorageKey ;
856+
837857 /// Get node id based on the provided [`Recipient`].
838858 ///
839859 /// This method must return the same value each time it is called with a given [`Recipient`]
@@ -1809,6 +1829,7 @@ pub struct KeysManager {
18091829 shutdown_pubkey : PublicKey ,
18101830 channel_master_key : Xpriv ,
18111831 channel_child_index : AtomicUsize ,
1832+ peer_storage_key : PeerStorageKey ,
18121833
18131834 #[ cfg( test) ]
18141835 pub ( crate ) entropy_source : RandomBytes ,
@@ -1877,6 +1898,10 @@ impl KeysManager {
18771898 . private_key ;
18781899 let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
18791900 inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
1901+ let peer_storage_key: SecretKey = master_key
1902+ . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 6 ) . unwrap ( ) )
1903+ . expect ( "Your RNG is busted" )
1904+ . private_key ;
18801905
18811906 let mut rand_bytes_engine = Sha256 :: engine ( ) ;
18821907 rand_bytes_engine. input ( & starting_time_secs. to_be_bytes ( ) ) ;
@@ -1892,6 +1917,8 @@ impl KeysManager {
18921917 node_id,
18931918 inbound_payment_key : ExpandedKey :: new ( inbound_pmt_key_bytes) ,
18941919
1920+ peer_storage_key : PeerStorageKey { inner : peer_storage_key. secret_bytes ( ) } ,
1921+
18951922 destination_script,
18961923 shutdown_pubkey,
18971924
@@ -2117,6 +2144,10 @@ impl NodeSigner for KeysManager {
21172144 self . inbound_payment_key . clone ( )
21182145 }
21192146
2147+ fn get_peer_storage_key ( & self ) -> PeerStorageKey {
2148+ self . peer_storage_key . clone ( )
2149+ }
2150+
21202151 fn sign_invoice (
21212152 & self , invoice : & RawBolt11Invoice , recipient : Recipient ,
21222153 ) -> Result < RecoverableSignature , ( ) > {
@@ -2278,6 +2309,10 @@ impl NodeSigner for PhantomKeysManager {
22782309 self . inbound_payment_key . clone ( )
22792310 }
22802311
2312+ fn get_peer_storage_key ( & self ) -> PeerStorageKey {
2313+ self . inner . peer_storage_key . clone ( )
2314+ }
2315+
22812316 fn sign_invoice (
22822317 & self , invoice : & RawBolt11Invoice , recipient : Recipient ,
22832318 ) -> Result < RecoverableSignature , ( ) > {
0 commit comments