Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit dae8dc1

Browse files
committed
Encode RawLSPSMessage's payload without length prefix
Previously, we'd decode/encode `payload` with `String`'s default encoding, which prepends the lenght. However, according to BOLT8/LSPS0 describe the payload as: "A variable-length message payload (the remainder of the message).", i.e., without any prepended lenght. Here, we fix this de/ser bug by encoding and decoding the payload using LDK's `WithoutLength` helper.
1 parent 6c5d4a2 commit dae8dc1

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/lsps0/ser.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use crate::lsps1::msgs::{
1515
use crate::lsps2::msgs::{
1616
LSPS2Message, LSPS2Request, LSPS2Response, LSPS2_BUY_METHOD_NAME, LSPS2_GET_INFO_METHOD_NAME,
1717
};
18-
use crate::prelude::{HashMap, String, ToString, Vec};
18+
use crate::prelude::{HashMap, String, ToString};
1919

20-
use lightning::impl_writeable_msg;
2120
use lightning::ln::msgs::LightningError;
2221
use lightning::ln::wire;
22+
use lightning::util::ser::WithoutLength;
2323

2424
use bitcoin::secp256k1::PublicKey;
2525

@@ -160,7 +160,23 @@ pub struct RawLSPSMessage {
160160
pub payload: String,
161161
}
162162

163-
impl_writeable_msg!(RawLSPSMessage, { payload }, {});
163+
// We encode `RawLSPSMessage`'s payload without a length prefix as LSPS0 expects it to be the
164+
// remainder of the object.
165+
impl lightning::util::ser::Writeable for RawLSPSMessage {
166+
fn write<W: lightning::util::ser::Writer>(
167+
&self, w: &mut W,
168+
) -> Result<(), lightning::io::Error> {
169+
WithoutLength(&self.payload).write(w)?;
170+
Ok(())
171+
}
172+
}
173+
174+
impl lightning::util::ser::Readable for RawLSPSMessage {
175+
fn read<R: lightning::io::Read>(r: &mut R) -> Result<Self, lightning::ln::msgs::DecodeError> {
176+
let payload_without_length = WithoutLength::read(r)?;
177+
Ok(Self { payload: payload_without_length.0 })
178+
}
179+
}
164180

165181
impl wire::Type for RawLSPSMessage {
166182
fn type_id(&self) -> u16 {

0 commit comments

Comments
 (0)