@@ -140,10 +140,10 @@ pub fn build_closing_transaction(to_holder_value_sat: u64, to_counterparty_value
140140/// Implements the per-commitment secret storage scheme from
141141/// [BOLT 3](https://github.com/lightningnetwork/lightning-rfc/blob/dcbf8583976df087c79c3ce0b535311212e6812d/03-transactions.md#efficient-per-commitment-secret-storage).
142142///
143- /// Allows us to keep track of all of the revocation secrets of counterarties in just 50*32 bytes
143+ /// Allows us to keep track of all of the revocation secrets of our counterparty in just 50*32 bytes
144144/// or so.
145145#[ derive( Clone ) ]
146- pub ( crate ) struct CounterpartyCommitmentSecrets {
146+ pub struct CounterpartyCommitmentSecrets {
147147 old_secrets : [ ( [ u8 ; 32 ] , u64 ) ; 49 ] ,
148148}
149149
@@ -159,7 +159,8 @@ impl PartialEq for CounterpartyCommitmentSecrets {
159159}
160160
161161impl CounterpartyCommitmentSecrets {
162- pub ( crate ) fn new ( ) -> Self {
162+ /// Creates a new empty `CounterpartyCommitmentSecrets` structure.
163+ pub fn new ( ) -> Self {
163164 Self { old_secrets : [ ( [ 0 ; 32 ] , 1 << 48 ) ; 49 ] , }
164165 }
165166
@@ -173,7 +174,9 @@ impl CounterpartyCommitmentSecrets {
173174 48
174175 }
175176
176- pub ( crate ) fn get_min_seen_secret ( & self ) -> u64 {
177+ /// Returns the minimum index of all stored secrets. Note that indexes start
178+ /// at 1 << 48 and get decremented by one for each new secret.
179+ pub fn get_min_seen_secret ( & self ) -> u64 {
177180 //TODO This can be optimized?
178181 let mut min = 1 << 48 ;
179182 for & ( _, idx) in self . old_secrets . iter ( ) {
@@ -197,7 +200,9 @@ impl CounterpartyCommitmentSecrets {
197200 res
198201 }
199202
200- pub ( crate ) fn provide_secret ( & mut self , idx : u64 , secret : [ u8 ; 32 ] ) -> Result < ( ) , ( ) > {
203+ /// Inserts the `secret` at `idx`. Returns `Ok(())` if the secret
204+ /// was generated in accordance with BOLT 3 and is consistent with previous secrets.
205+ pub fn provide_secret ( & mut self , idx : u64 , secret : [ u8 ; 32 ] ) -> Result < ( ) , ( ) > {
201206 let pos = Self :: place_secret ( idx) ;
202207 for i in 0 ..pos {
203208 let ( old_secret, old_idx) = self . old_secrets [ i as usize ] ;
@@ -212,8 +217,9 @@ impl CounterpartyCommitmentSecrets {
212217 Ok ( ( ) )
213218 }
214219
215- /// Can only fail if idx is < get_min_seen_secret
216- pub ( crate ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
220+ /// Returns the secret at `idx`.
221+ /// Returns `None` if `idx` is < [CounterpartyCommitmentSecrets::get_min_seen_secret].
222+ pub fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
217223 for i in 0 ..self . old_secrets . len ( ) {
218224 if ( idx & ( !( ( 1 << i) - 1 ) ) ) == self . old_secrets [ i] . 1 {
219225 return Some ( Self :: derive_secret ( self . old_secrets [ i] . 0 , i as u8 , idx) )
0 commit comments