Skip to content

Commit 8fdf255

Browse files
committed
Make CounterpartyCommitmentSecrets public
1 parent d29ae18 commit 8fdf255

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn build_closing_transaction(to_holder_value_sat: u64, to_counterparty_value
143143
/// Allows us to keep track of all of the revocation secrets of counterarties 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

161161
impl 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+
/// Return 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 `secret` at index `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 index `idx`.
221+
/// Returns `None` if idx is < [CounterpartyCommitmentSecrets::get_min_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

Comments
 (0)