@@ -140,10 +140,10 @@ pub fn build_closing_transaction(to_holder_value_sat: u64, to_counterparty_value
140
140
/// Implements the per-commitment secret storage scheme from
141
141
/// [BOLT 3](https://github.com/lightningnetwork/lightning-rfc/blob/dcbf8583976df087c79c3ce0b535311212e6812d/03-transactions.md#efficient-per-commitment-secret-storage).
142
142
///
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
144
144
/// or so.
145
145
#[ derive( Clone ) ]
146
- pub ( crate ) struct CounterpartyCommitmentSecrets {
146
+ pub struct CounterpartyCommitmentSecrets {
147
147
old_secrets : [ ( [ u8 ; 32 ] , u64 ) ; 49 ] ,
148
148
}
149
149
@@ -159,7 +159,8 @@ impl PartialEq for CounterpartyCommitmentSecrets {
159
159
}
160
160
161
161
impl CounterpartyCommitmentSecrets {
162
- pub ( crate ) fn new ( ) -> Self {
162
+ /// Creates a new empty `CounterpartyCommitmentSecrets` structure.
163
+ pub fn new ( ) -> Self {
163
164
Self { old_secrets : [ ( [ 0 ; 32 ] , 1 << 48 ) ; 49 ] , }
164
165
}
165
166
@@ -173,7 +174,9 @@ impl CounterpartyCommitmentSecrets {
173
174
48
174
175
}
175
176
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 {
177
180
//TODO This can be optimized?
178
181
let mut min = 1 << 48 ;
179
182
for & ( _, idx) in self . old_secrets . iter ( ) {
@@ -197,7 +200,9 @@ impl CounterpartyCommitmentSecrets {
197
200
res
198
201
}
199
202
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 < ( ) , ( ) > {
201
206
let pos = Self :: place_secret ( idx) ;
202
207
for i in 0 ..pos {
203
208
let ( old_secret, old_idx) = self . old_secrets [ i as usize ] ;
@@ -212,8 +217,9 @@ impl CounterpartyCommitmentSecrets {
212
217
Ok ( ( ) )
213
218
}
214
219
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 ] > {
217
223
for i in 0 ..self . old_secrets . len ( ) {
218
224
if ( idx & ( !( ( 1 << i) - 1 ) ) ) == self . old_secrets [ i] . 1 {
219
225
return Some ( Self :: derive_secret ( self . old_secrets [ i] . 0 , i as u8 , idx) )
0 commit comments