Skip to content

Commit 3234136

Browse files
committed
Qualify the BOLT 12 semantic error
To avoid a naming conflict in bindings with BOLT 11 semantic error, qualify the BOLT 12 semantic error type.
1 parent 5627d7c commit 3234136

File tree

9 files changed

+180
-182
lines changed

9 files changed

+180
-182
lines changed

fuzz/src/invoice_request_deser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use lightning::ln::PaymentHash;
1616
use lightning::ln::features::BlindedHopFeatures;
1717
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
1818
use lightning::offers::invoice_request::InvoiceRequest;
19-
use lightning::offers::parse::SemanticError;
19+
use lightning::offers::parse::Bolt12SemanticError;
2020
use lightning::util::ser::Writeable;
2121

2222
#[inline]
@@ -71,7 +71,7 @@ fn privkey(byte: u8) -> SecretKey {
7171

7272
fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
7373
invoice_request: &'a InvoiceRequest, secp_ctx: &Secp256k1<T>
74-
) -> Result<UnsignedBolt12Invoice<'a>, SemanticError> {
74+
) -> Result<UnsignedBolt12Invoice<'a>, Bolt12SemanticError> {
7575
let entropy_source = Randomness {};
7676
let paths = vec![
7777
BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),

fuzz/src/offer_deser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::utils::test_logger;
1212
use core::convert::{Infallible, TryFrom};
1313
use lightning::offers::invoice_request::UnsignedInvoiceRequest;
1414
use lightning::offers::offer::{Amount, Offer, Quantity};
15-
use lightning::offers::parse::SemanticError;
15+
use lightning::offers::parse::Bolt12SemanticError;
1616
use lightning::util::ser::Writeable;
1717

1818
#[inline]
@@ -41,13 +41,13 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
4141

4242
fn build_response<'a>(
4343
offer: &'a Offer, pubkey: PublicKey
44-
) -> Result<UnsignedInvoiceRequest<'a>, SemanticError> {
44+
) -> Result<UnsignedInvoiceRequest<'a>, Bolt12SemanticError> {
4545
let mut builder = offer.request_invoice(vec![42; 64], pubkey)?;
4646

4747
builder = match offer.amount() {
4848
None => builder.amount_msats(1000).unwrap(),
4949
Some(Amount::Bitcoin { amount_msats }) => builder.amount_msats(amount_msats + 1)?,
50-
Some(Amount::Currency { .. }) => return Err(SemanticError::UnsupportedCurrency),
50+
Some(Amount::Currency { .. }) => return Err(Bolt12SemanticError::UnsupportedCurrency),
5151
};
5252

5353
builder = match offer.supported_quantity() {

fuzz/src/refund_deser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use lightning::sign::EntropySource;
1515
use lightning::ln::PaymentHash;
1616
use lightning::ln::features::BlindedHopFeatures;
1717
use lightning::offers::invoice::{BlindedPayInfo, UnsignedBolt12Invoice};
18-
use lightning::offers::parse::SemanticError;
18+
use lightning::offers::parse::Bolt12SemanticError;
1919
use lightning::offers::refund::Refund;
2020
use lightning::util::ser::Writeable;
2121

@@ -60,7 +60,7 @@ fn privkey(byte: u8) -> SecretKey {
6060

6161
fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
6262
refund: &'a Refund, signing_pubkey: PublicKey, secp_ctx: &Secp256k1<T>
63-
) -> Result<UnsignedBolt12Invoice<'a>, SemanticError> {
63+
) -> Result<UnsignedBolt12Invoice<'a>, Bolt12SemanticError> {
6464
let entropy_source = Randomness {};
6565
let paths = vec![
6666
BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),

lightning/src/offers/invoice.rs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ use crate::ln::msgs::DecodeError;
112112
use crate::offers::invoice_request::{INVOICE_REQUEST_PAYER_ID_TYPE, INVOICE_REQUEST_TYPES, IV_BYTES as INVOICE_REQUEST_IV_BYTES, InvoiceRequest, InvoiceRequestContents, InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
113113
use crate::offers::merkle::{SignError, SignatureTlvStream, SignatureTlvStreamRef, TlvStream, WithoutSignatures, self};
114114
use crate::offers::offer::{Amount, OFFER_TYPES, OfferTlvStream, OfferTlvStreamRef};
115-
use crate::offers::parse::{Bolt12ParseError, ParsedMessage, SemanticError};
115+
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
116116
use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef};
117117
use crate::offers::refund::{IV_BYTES as REFUND_IV_BYTES, Refund, RefundContents};
118118
use crate::offers::signer;
@@ -168,7 +168,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
168168
pub(super) fn for_offer(
169169
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
170170
created_at: Duration, payment_hash: PaymentHash
171-
) -> Result<Self, SemanticError> {
171+
) -> Result<Self, Bolt12SemanticError> {
172172
let amount_msats = Self::check_amount_msats(invoice_request)?;
173173
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
174174
let contents = InvoiceContents::ForOffer {
@@ -184,7 +184,7 @@ impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
184184
pub(super) fn for_refund(
185185
refund: &'a Refund, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
186186
payment_hash: PaymentHash, signing_pubkey: PublicKey
187-
) -> Result<Self, SemanticError> {
187+
) -> Result<Self, Bolt12SemanticError> {
188188
let amount_msats = refund.amount_msats();
189189
let contents = InvoiceContents::ForRefund {
190190
refund: refund.contents.clone(),
@@ -201,7 +201,7 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
201201
pub(super) fn for_offer_using_keys(
202202
invoice_request: &'a InvoiceRequest, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
203203
created_at: Duration, payment_hash: PaymentHash, keys: KeyPair
204-
) -> Result<Self, SemanticError> {
204+
) -> Result<Self, Bolt12SemanticError> {
205205
let amount_msats = Self::check_amount_msats(invoice_request)?;
206206
let signing_pubkey = invoice_request.contents.inner.offer.signing_pubkey();
207207
let contents = InvoiceContents::ForOffer {
@@ -217,7 +217,7 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
217217
pub(super) fn for_refund_using_keys(
218218
refund: &'a Refund, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, created_at: Duration,
219219
payment_hash: PaymentHash, keys: KeyPair,
220-
) -> Result<Self, SemanticError> {
220+
) -> Result<Self, Bolt12SemanticError> {
221221
let amount_msats = refund.amount_msats();
222222
let signing_pubkey = keys.public_key();
223223
let contents = InvoiceContents::ForRefund {
@@ -232,16 +232,16 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
232232
}
233233

234234
impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
235-
fn check_amount_msats(invoice_request: &InvoiceRequest) -> Result<u64, SemanticError> {
235+
fn check_amount_msats(invoice_request: &InvoiceRequest) -> Result<u64, Bolt12SemanticError> {
236236
match invoice_request.amount_msats() {
237237
Some(amount_msats) => Ok(amount_msats),
238238
None => match invoice_request.contents.inner.offer.amount() {
239239
Some(Amount::Bitcoin { amount_msats }) => {
240240
amount_msats.checked_mul(invoice_request.quantity().unwrap_or(1))
241-
.ok_or(SemanticError::InvalidAmount)
241+
.ok_or(Bolt12SemanticError::InvalidAmount)
242242
},
243-
Some(Amount::Currency { .. }) => Err(SemanticError::UnsupportedCurrency),
244-
None => Err(SemanticError::MissingAmount),
243+
Some(Amount::Currency { .. }) => Err(Bolt12SemanticError::UnsupportedCurrency),
244+
None => Err(Bolt12SemanticError::MissingAmount),
245245
},
246246
}
247247
}
@@ -258,9 +258,9 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
258258

259259
fn new(
260260
invreq_bytes: &'a Vec<u8>, contents: InvoiceContents, keys: Option<KeyPair>
261-
) -> Result<Self, SemanticError> {
261+
) -> Result<Self, Bolt12SemanticError> {
262262
if contents.fields().payment_paths.is_empty() {
263-
return Err(SemanticError::MissingPaths);
263+
return Err(Bolt12SemanticError::MissingPaths);
264264
}
265265

266266
Ok(Self {
@@ -331,10 +331,10 @@ impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
331331
impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
332332
/// Builds an unsigned [`Bolt12Invoice`] after checking for valid semantics. It can be signed by
333333
/// [`UnsignedBolt12Invoice::sign`].
334-
pub fn build(self) -> Result<UnsignedBolt12Invoice<'a>, SemanticError> {
334+
pub fn build(self) -> Result<UnsignedBolt12Invoice<'a>, Bolt12SemanticError> {
335335
#[cfg(feature = "std")] {
336336
if self.invoice.is_offer_or_refund_expired() {
337-
return Err(SemanticError::AlreadyExpired);
337+
return Err(Bolt12SemanticError::AlreadyExpired);
338338
}
339339
}
340340

@@ -347,10 +347,10 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
347347
/// Builds a signed [`Bolt12Invoice`] after checking for valid semantics.
348348
pub fn build_and_sign<T: secp256k1::Signing>(
349349
self, secp_ctx: &Secp256k1<T>
350-
) -> Result<Bolt12Invoice, SemanticError> {
350+
) -> Result<Bolt12Invoice, Bolt12SemanticError> {
351351
#[cfg(feature = "std")] {
352352
if self.invoice.is_offer_or_refund_expired() {
353-
return Err(SemanticError::AlreadyExpired);
353+
return Err(Bolt12SemanticError::AlreadyExpired);
354354
}
355355
}
356356

@@ -853,7 +853,7 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
853853
)?;
854854

855855
let signature = match signature {
856-
None => return Err(Bolt12ParseError::InvalidSemantics(SemanticError::MissingSignature)),
856+
None => return Err(Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature)),
857857
Some(signature) => signature,
858858
};
859859
let pubkey = contents.fields().signing_pubkey;
@@ -864,7 +864,7 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
864864
}
865865

866866
impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
867-
type Error = SemanticError;
867+
type Error = Bolt12SemanticError;
868868

869869
fn try_from(tlv_stream: PartialInvoiceTlvStream) -> Result<Self, Self::Error> {
870870
let (
@@ -878,19 +878,19 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
878878
) = tlv_stream;
879879

880880
let payment_paths = match (blindedpay, paths) {
881-
(_, None) => return Err(SemanticError::MissingPaths),
882-
(None, _) => return Err(SemanticError::InvalidPayInfo),
883-
(_, Some(paths)) if paths.is_empty() => return Err(SemanticError::MissingPaths),
881+
(_, None) => return Err(Bolt12SemanticError::MissingPaths),
882+
(None, _) => return Err(Bolt12SemanticError::InvalidPayInfo),
883+
(_, Some(paths)) if paths.is_empty() => return Err(Bolt12SemanticError::MissingPaths),
884884
(Some(blindedpay), Some(paths)) if paths.len() != blindedpay.len() => {
885-
return Err(SemanticError::InvalidPayInfo);
885+
return Err(Bolt12SemanticError::InvalidPayInfo);
886886
},
887887
(Some(blindedpay), Some(paths)) => {
888888
blindedpay.into_iter().zip(paths.into_iter()).collect::<Vec<_>>()
889889
},
890890
};
891891

892892
let created_at = match created_at {
893-
None => return Err(SemanticError::MissingCreationTime),
893+
None => return Err(Bolt12SemanticError::MissingCreationTime),
894894
Some(timestamp) => Duration::from_secs(timestamp),
895895
};
896896

@@ -899,19 +899,19 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
899899
.map(Duration::from_secs);
900900

901901
let payment_hash = match payment_hash {
902-
None => return Err(SemanticError::MissingPaymentHash),
902+
None => return Err(Bolt12SemanticError::MissingPaymentHash),
903903
Some(payment_hash) => payment_hash,
904904
};
905905

906906
let amount_msats = match amount {
907-
None => return Err(SemanticError::MissingAmount),
907+
None => return Err(Bolt12SemanticError::MissingAmount),
908908
Some(amount) => amount,
909909
};
910910

911911
let features = features.unwrap_or_else(Bolt12InvoiceFeatures::empty);
912912

913913
let signing_pubkey = match node_id {
914-
None => return Err(SemanticError::MissingSigningPubkey),
914+
None => return Err(Bolt12SemanticError::MissingSigningPubkey),
915915
Some(node_id) => node_id,
916916
};
917917

@@ -923,7 +923,7 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
923923
match offer_tlv_stream.node_id {
924924
Some(expected_signing_pubkey) => {
925925
if fields.signing_pubkey != expected_signing_pubkey {
926-
return Err(SemanticError::InvalidSigningPubkey);
926+
return Err(Bolt12SemanticError::InvalidSigningPubkey);
927927
}
928928

929929
let invoice_request = InvoiceRequestContents::try_from(
@@ -961,7 +961,7 @@ mod tests {
961961
use crate::offers::invoice_request::InvoiceRequestTlvStreamRef;
962962
use crate::offers::merkle::{SignError, SignatureTlvStreamRef, self};
963963
use crate::offers::offer::{OfferBuilder, OfferTlvStreamRef, Quantity};
964-
use crate::offers::parse::{Bolt12ParseError, SemanticError};
964+
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
965965
use crate::offers::payer::PayerTlvStreamRef;
966966
use crate::offers::refund::RefundBuilder;
967967
use crate::offers::test_utils::*;
@@ -1180,7 +1180,7 @@ mod tests {
11801180
.build()
11811181
{
11821182
Ok(_) => panic!("expected error"),
1183-
Err(e) => assert_eq!(e, SemanticError::AlreadyExpired),
1183+
Err(e) => assert_eq!(e, Bolt12SemanticError::AlreadyExpired),
11841184
}
11851185
}
11861186

@@ -1208,7 +1208,7 @@ mod tests {
12081208
.build()
12091209
{
12101210
Ok(_) => panic!("expected error"),
1211-
Err(e) => assert_eq!(e, SemanticError::AlreadyExpired),
1211+
Err(e) => assert_eq!(e, Bolt12SemanticError::AlreadyExpired),
12121212
}
12131213
}
12141214

@@ -1253,7 +1253,7 @@ mod tests {
12531253
payment_paths(), payment_hash(), now(), &expanded_key, &secp_ctx
12541254
) {
12551255
Ok(_) => panic!("expected error"),
1256-
Err(e) => assert_eq!(e, SemanticError::InvalidMetadata),
1256+
Err(e) => assert_eq!(e, Bolt12SemanticError::InvalidMetadata),
12571257
}
12581258

12591259
let desc = "foo".to_string();
@@ -1269,7 +1269,7 @@ mod tests {
12691269
payment_paths(), payment_hash(), now(), &expanded_key, &secp_ctx
12701270
) {
12711271
Ok(_) => panic!("expected error"),
1272-
Err(e) => assert_eq!(e, SemanticError::InvalidMetadata),
1272+
Err(e) => assert_eq!(e, Bolt12SemanticError::InvalidMetadata),
12731273
}
12741274
}
12751275

@@ -1376,7 +1376,7 @@ mod tests {
13761376
.respond_with_no_std(payment_paths(), payment_hash(), now())
13771377
{
13781378
Ok(_) => panic!("expected error"),
1379-
Err(e) => assert_eq!(e, SemanticError::InvalidAmount),
1379+
Err(e) => assert_eq!(e, Bolt12SemanticError::InvalidAmount),
13801380
}
13811381
}
13821382

@@ -1502,15 +1502,15 @@ mod tests {
15021502

15031503
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
15041504
Ok(_) => panic!("expected error"),
1505-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPaths)),
1505+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingPaths)),
15061506
}
15071507

15081508
let mut tlv_stream = invoice.as_tlv_stream();
15091509
tlv_stream.3.blindedpay = None;
15101510

15111511
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
15121512
Ok(_) => panic!("expected error"),
1513-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)),
1513+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidPayInfo)),
15141514
}
15151515

15161516
let empty_payment_paths = vec![];
@@ -1519,7 +1519,7 @@ mod tests {
15191519

15201520
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
15211521
Ok(_) => panic!("expected error"),
1522-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPaths)),
1522+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingPaths)),
15231523
}
15241524

15251525
let mut payment_paths = payment_paths();
@@ -1529,7 +1529,7 @@ mod tests {
15291529

15301530
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
15311531
Ok(_) => panic!("expected error"),
1532-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidPayInfo)),
1532+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidPayInfo)),
15331533
}
15341534
}
15351535

@@ -1558,7 +1558,7 @@ mod tests {
15581558
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
15591559
Ok(_) => panic!("expected error"),
15601560
Err(e) => {
1561-
assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingCreationTime));
1561+
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingCreationTime));
15621562
},
15631563
}
15641564
}
@@ -1610,7 +1610,7 @@ mod tests {
16101610
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
16111611
Ok(_) => panic!("expected error"),
16121612
Err(e) => {
1613-
assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingPaymentHash));
1613+
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingPaymentHash));
16141614
},
16151615
}
16161616
}
@@ -1639,7 +1639,7 @@ mod tests {
16391639

16401640
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
16411641
Ok(_) => panic!("expected error"),
1642-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingAmount)),
1642+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingAmount)),
16431643
}
16441644
}
16451645

@@ -1758,7 +1758,7 @@ mod tests {
17581758
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
17591759
Ok(_) => panic!("expected error"),
17601760
Err(e) => {
1761-
assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSigningPubkey));
1761+
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSigningPubkey));
17621762
},
17631763
}
17641764

@@ -1769,7 +1769,7 @@ mod tests {
17691769
match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
17701770
Ok(_) => panic!("expected error"),
17711771
Err(e) => {
1772-
assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::InvalidSigningPubkey));
1772+
assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidSigningPubkey));
17731773
},
17741774
}
17751775
}
@@ -1790,7 +1790,7 @@ mod tests {
17901790

17911791
match Bolt12Invoice::try_from(buffer) {
17921792
Ok(_) => panic!("expected error"),
1793-
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(SemanticError::MissingSignature)),
1793+
Err(e) => assert_eq!(e, Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::MissingSignature)),
17941794
}
17951795
}
17961796

0 commit comments

Comments
 (0)