@@ -796,6 +796,13 @@ 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+ /// Represents the key used to encrypt and decrypt Peer Storage.
803+ pub inner : [ u8 ; 32 ] ,
804+ }
805+
799806/// Specifies the recipient of an invoice.
800807///
801808/// This indicates to [`NodeSigner::sign_invoice`] what node secret key should be used to sign
@@ -834,6 +841,15 @@ pub trait NodeSigner {
834841 /// [phantom node payments]: PhantomKeysManager
835842 fn get_inbound_payment_key ( & self ) -> ExpandedKey ;
836843
844+ /// Defines a method to derive a 32-byte encryption key for peer storage.
845+ ///
846+ /// Implementations of this method must derive a secure encryption key.
847+ /// The key is used to encrypt or decrypt backups of our state stored with our peers.
848+ ///
849+ /// Thus, if you wish to rely on recovery using this method, you should use a key which
850+ /// can be re-derived from data which would be available after state loss (eg the wallet seed).
851+ fn get_peer_storage_key ( & self ) -> PeerStorageKey ;
852+
837853 /// Get node id based on the provided [`Recipient`].
838854 ///
839855 /// This method must return the same value each time it is called with a given [`Recipient`]
@@ -1809,6 +1825,7 @@ pub struct KeysManager {
18091825 shutdown_pubkey : PublicKey ,
18101826 channel_master_key : Xpriv ,
18111827 channel_child_index : AtomicUsize ,
1828+ peer_storage_key : PeerStorageKey ,
18121829
18131830 #[ cfg( test) ]
18141831 pub ( crate ) entropy_source : RandomBytes ,
@@ -1877,6 +1894,10 @@ impl KeysManager {
18771894 . private_key ;
18781895 let mut inbound_pmt_key_bytes = [ 0 ; 32 ] ;
18791896 inbound_pmt_key_bytes. copy_from_slice ( & inbound_payment_key[ ..] ) ;
1897+ let peer_storage_key: SecretKey = master_key
1898+ . derive_priv ( & secp_ctx, & ChildNumber :: from_hardened_idx ( 6 ) . unwrap ( ) )
1899+ . expect ( "Your RNG is busted" )
1900+ . private_key ;
18801901
18811902 let mut rand_bytes_engine = Sha256 :: engine ( ) ;
18821903 rand_bytes_engine. input ( & starting_time_secs. to_be_bytes ( ) ) ;
@@ -1892,6 +1913,8 @@ impl KeysManager {
18921913 node_id,
18931914 inbound_payment_key : ExpandedKey :: new ( inbound_pmt_key_bytes) ,
18941915
1916+ peer_storage_key : PeerStorageKey { inner : peer_storage_key. secret_bytes ( ) } ,
1917+
18951918 destination_script,
18961919 shutdown_pubkey,
18971920
@@ -2117,6 +2140,10 @@ impl NodeSigner for KeysManager {
21172140 self . inbound_payment_key . clone ( )
21182141 }
21192142
2143+ fn get_peer_storage_key ( & self ) -> PeerStorageKey {
2144+ self . peer_storage_key . clone ( )
2145+ }
2146+
21202147 fn sign_invoice (
21212148 & self , invoice : & RawBolt11Invoice , recipient : Recipient ,
21222149 ) -> Result < RecoverableSignature , ( ) > {
@@ -2278,6 +2305,10 @@ impl NodeSigner for PhantomKeysManager {
22782305 self . inbound_payment_key . clone ( )
22792306 }
22802307
2308+ fn get_peer_storage_key ( & self ) -> PeerStorageKey {
2309+ self . inner . peer_storage_key . clone ( )
2310+ }
2311+
22812312 fn sign_invoice (
22822313 & self , invoice : & RawBolt11Invoice , recipient : Recipient ,
22832314 ) -> Result < RecoverableSignature , ( ) > {
0 commit comments