Skip to content

Commit e5174cd

Browse files
committed
f - Semantic checks without wrapped OfferTlvStream
1 parent 8e2e0a2 commit e5174cd

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

lightning/src/offers/mod.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -241,47 +241,59 @@ impl TryFrom<OfferTlvStream> for Offer {
241241
type Error = SemanticError;
242242

243243
fn try_from(tlv_stream: OfferTlvStream) -> Result<Self, Self::Error> {
244-
if tlv_stream.description.is_none() {
245-
return Err(SemanticError::MissingDescription);
246-
}
244+
let OfferTlvStream {
245+
chains, currency, amount, description, features, absolute_expiry, paths, issuer,
246+
quantity_min, quantity_max, recurrence, node_id, send_invoice, refund_for, signature,
247+
} = tlv_stream;
247248

248-
if tlv_stream.node_id.is_none() && tlv_stream.paths.is_none() {
249-
return Err(SemanticError::MissingDestination);
250-
}
251-
252-
if tlv_stream.node_id.is_some() && tlv_stream.paths.is_some() {
253-
return Err(SemanticError::DuplicateDestination);
249+
if description.is_none() {
250+
return Err(SemanticError::MissingDescription);
254251
}
255252

256-
if let Some(WithoutLength(ref paths)) = tlv_stream.paths {
257-
if paths.is_empty() {
253+
let destination = match (node_id, paths) {
254+
(None, None) => return Err(SemanticError::MissingDestination),
255+
(Some(_), Some(_)) => return Err(SemanticError::DuplicateDestination),
256+
(Some(node_id), None) => Destination::NodeId(node_id),
257+
(None, Some(WithoutLength(paths))) if paths.is_empty() => {
258258
return Err(SemanticError::MissingPaths);
259-
}
260-
}
259+
},
260+
(None, Some(WithoutLength(paths))) => Destination::Paths(paths),
261+
};
261262

262-
if let Some(HighZeroBytesDroppedVarInt(quantity_min)) = tlv_stream.quantity_min {
263+
if let Some(HighZeroBytesDroppedVarInt(quantity_min)) = quantity_min {
263264
if quantity_min < 1 {
264265
return Err(SemanticError::InvalidQuantity);
265266
}
266267

267-
if let Some(HighZeroBytesDroppedVarInt(quantity_max)) = tlv_stream.quantity_max {
268+
if let Some(HighZeroBytesDroppedVarInt(quantity_max)) = quantity_max {
268269
if quantity_min > quantity_max {
269270
return Err(SemanticError::InvalidQuantity);
270271
}
271272
}
272273
}
273274

274-
if let Some(HighZeroBytesDroppedVarInt(quantity_max)) = tlv_stream.quantity_max {
275+
if let Some(HighZeroBytesDroppedVarInt(quantity_max)) = quantity_max {
275276
if quantity_max < 1 {
276277
return Err(SemanticError::InvalidQuantity);
277278
}
278279
}
279280

280-
if tlv_stream.refund_for.is_some() && tlv_stream.send_invoice.is_none() {
281+
if refund_for.is_some() && send_invoice.is_none() {
281282
return Err(SemanticError::UnexpectedRefund);
282283
}
283284

284-
Ok(Offer { tlv_stream })
285+
286+
Ok(Offer {
287+
description: description.unwrap().0,
288+
features,
289+
absolute_expiry:
290+
absolute_expiry.map(|seconds_from_epoch| Duration::from_secs(seconds_from_epoch.0)),
291+
issuer: issuer.map(|issuer| issuer.0),
292+
destination,
293+
quantity_min: quantity_min.map(|quantity_min| quantity_min.0),
294+
quantity_max: quantity_max.map(|quantity_max| quantity_max.0),
295+
signature,
296+
})
285297
}
286298
}
287299

0 commit comments

Comments
 (0)