Skip to content

Commit cb88897

Browse files
committed
Update Padding struct to contain padding length.
1. This allows setting the length of padding at the time of writing. 2. This will be used in the following commit to allow setting the padding for blinded message paths, and blinded payment paths.
1 parent 70ae5d4 commit cb88897

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1818
use super::message::BlindedMessagePath;
1919
use super::{BlindedHop, BlindedPath};
2020
use crate::crypto::streams::ChaChaPolyWriteAdapter;
21+
use crate::io;
2122
use crate::ln::onion_utils;
2223
use crate::onion_message::messenger::Destination;
23-
use crate::util::ser::Writeable;
24+
use crate::util::ser::{Writeable, Writer};
2425

2526
use core::borrow::Borrow;
2627

@@ -197,4 +198,33 @@ fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_rho: [u8; 32]) -> Ve
197198
/// equal length. Padding is written at Type 1 for compatibility with the lightning specification.
198199
///
199200
/// For more details, see the [BOLTs Specification - Encrypted Recipient Data](https://github.com/lightning/bolts/blob/8707471dbc23245fb4d84c5f5babac1197f1583e/04-onion-routing.md#inside-encrypted_recipient_data-encrypted_data_tlv).
200-
pub(crate) struct BlindedPathPadding {}
201+
pub(crate) struct BlindedPathPadding {
202+
length: usize,
203+
}
204+
205+
impl BlindedPathPadding {
206+
/// Creates a new [`BlindedPathPadding`] instance with a specified size.
207+
/// Use this method when defining the padding size before writing
208+
/// an encrypted payload.
209+
pub fn new(length: usize) -> Self {
210+
Self { length }
211+
}
212+
}
213+
214+
impl Writeable for BlindedPathPadding {
215+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
216+
const BUFFER_SIZE: usize = 1024;
217+
let buffer = [0u8; BUFFER_SIZE];
218+
219+
let mut remaining = self.length;
220+
loop {
221+
let to_write = core::cmp::min(remaining, BUFFER_SIZE);
222+
writer.write_all(&buffer[..to_write])?;
223+
remaining -= to_write;
224+
if remaining == 0 {
225+
break;
226+
}
227+
}
228+
Ok(())
229+
}
230+
}

0 commit comments

Comments
 (0)