Skip to content

Commit a889642

Browse files
Disallow (de)serializing BlindedPaths.
Users should (de)ser BlindedMessagePaths or BlindedPaymentPaths but not the generic blinded path.
1 parent bb3cade commit a889642

File tree

1 file changed

+46
-50
lines changed
  • lightning/src/blinded_path

1 file changed

+46
-50
lines changed

lightning/src/blinded_path/mod.rs

+46-50
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,52 @@ pub struct BlindedHop {
119119
}
120120

121121
impl BlindedPath {
122+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
123+
match &self.introduction_node {
124+
IntroductionNode::NodeId(pubkey) => pubkey.write(w)?,
125+
IntroductionNode::DirectedShortChannelId(direction, scid) => {
126+
match direction {
127+
Direction::NodeOne => 0u8.write(w)?,
128+
Direction::NodeTwo => 1u8.write(w)?,
129+
}
130+
scid.write(w)?;
131+
},
132+
}
133+
134+
self.blinding_point.write(w)?;
135+
(self.blinded_hops.len() as u8).write(w)?;
136+
for hop in &self.blinded_hops {
137+
hop.write(w)?;
138+
}
139+
Ok(())
140+
}
141+
142+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
143+
let mut first_byte: u8 = Readable::read(r)?;
144+
let introduction_node = match first_byte {
145+
0 => IntroductionNode::DirectedShortChannelId(Direction::NodeOne, Readable::read(r)?),
146+
1 => IntroductionNode::DirectedShortChannelId(Direction::NodeTwo, Readable::read(r)?),
147+
2|3 => {
148+
use io::Read;
149+
let mut pubkey_read = core::slice::from_mut(&mut first_byte).chain(r.by_ref());
150+
IntroductionNode::NodeId(Readable::read(&mut pubkey_read)?)
151+
},
152+
_ => return Err(DecodeError::InvalidValue),
153+
};
154+
let blinding_point = Readable::read(r)?;
155+
let num_hops: u8 = Readable::read(r)?;
156+
if num_hops == 0 { return Err(DecodeError::InvalidValue) }
157+
let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
158+
for _ in 0..num_hops {
159+
blinded_hops.push(Readable::read(r)?);
160+
}
161+
Ok(BlindedPath {
162+
introduction_node,
163+
blinding_point,
164+
blinded_hops,
165+
})
166+
}
167+
122168
/// Returns the introduction [`NodeId`] of the blinded path, if it is publicly reachable (i.e.,
123169
/// it is found in the network graph).
124170
pub fn public_introduction_node_id<'a>(
@@ -170,56 +216,6 @@ impl BlindedPath {
170216
}
171217
}
172218

173-
impl Writeable for BlindedPath {
174-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
175-
match &self.introduction_node {
176-
IntroductionNode::NodeId(pubkey) => pubkey.write(w)?,
177-
IntroductionNode::DirectedShortChannelId(direction, scid) => {
178-
match direction {
179-
Direction::NodeOne => 0u8.write(w)?,
180-
Direction::NodeTwo => 1u8.write(w)?,
181-
}
182-
scid.write(w)?;
183-
},
184-
}
185-
186-
self.blinding_point.write(w)?;
187-
(self.blinded_hops.len() as u8).write(w)?;
188-
for hop in &self.blinded_hops {
189-
hop.write(w)?;
190-
}
191-
Ok(())
192-
}
193-
}
194-
195-
impl Readable for BlindedPath {
196-
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
197-
let mut first_byte: u8 = Readable::read(r)?;
198-
let introduction_node = match first_byte {
199-
0 => IntroductionNode::DirectedShortChannelId(Direction::NodeOne, Readable::read(r)?),
200-
1 => IntroductionNode::DirectedShortChannelId(Direction::NodeTwo, Readable::read(r)?),
201-
2|3 => {
202-
use io::Read;
203-
let mut pubkey_read = core::slice::from_mut(&mut first_byte).chain(r.by_ref());
204-
IntroductionNode::NodeId(Readable::read(&mut pubkey_read)?)
205-
},
206-
_ => return Err(DecodeError::InvalidValue),
207-
};
208-
let blinding_point = Readable::read(r)?;
209-
let num_hops: u8 = Readable::read(r)?;
210-
if num_hops == 0 { return Err(DecodeError::InvalidValue) }
211-
let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
212-
for _ in 0..num_hops {
213-
blinded_hops.push(Readable::read(r)?);
214-
}
215-
Ok(BlindedPath {
216-
introduction_node,
217-
blinding_point,
218-
blinded_hops,
219-
})
220-
}
221-
}
222-
223219
impl_writeable!(BlindedHop, {
224220
blinded_node_id,
225221
encrypted_payload

0 commit comments

Comments
 (0)