Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BOLT 12 offer parsing #1726

Merged
merged 5 commits into from
Nov 18, 2022
Merged

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Sep 16, 2022

Add common bech32 parsing for BOLT 12 messages. The encoding is similar to bech32 only without a checksum and with support for continuing messages across multiple parts.

Messages implementing Bech32Encode are parsed into a TLV stream, which is converted to the desired message content while performing semantic checks. Checking after conversion allows for more elaborate checks of data composed of multiple TLV records and for more meaningful error messages.

The parsed bytes are also saved to allow creating messages with mirrored data, even if TLV records are unknown.

Also, implement Writeable for Offer and have a corresponding TryFrom implementation for decoding the raw bytes.

@codecov-commenter
Copy link

codecov-commenter commented Sep 16, 2022

Codecov Report

Base: 90.80% // Head: 91.95% // Increases project coverage by +1.14% 🎉

Coverage data is based on head (e916bf8) compared to base (f1428fd).
Patch coverage: 86.83% of modified lines in pull request are covered.

❗ Current head e916bf8 differs from pull request most recent head 1e26a2b. Consider uploading reports for the commit 1e26a2b to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1726      +/-   ##
==========================================
+ Coverage   90.80%   91.95%   +1.14%     
==========================================
  Files          89       91       +2     
  Lines       47963    58814   +10851     
  Branches    47963    58814   +10851     
==========================================
+ Hits        43554    54081   +10527     
- Misses       4409     4733     +324     
Impacted Files Coverage Δ
lightning/src/util/ser.rs 91.70% <ø> (-0.10%) ⬇️
lightning/src/util/ser_macros.rs 89.15% <53.84%> (+0.74%) ⬆️
lightning/src/offers/offer.rs 91.77% <87.38%> (-2.79%) ⬇️
lightning/src/offers/parse.rs 93.47% <93.47%> (ø)
lightning/src/util/events.rs 34.91% <0.00%> (-2.72%) ⬇️
lightning/src/chain/mod.rs 66.66% <0.00%> (-1.52%) ⬇️
lightning-net-tokio/src/lib.rs 76.73% <0.00%> (-0.31%) ⬇️
lightning/src/ln/reorg_tests.rs 100.00% <0.00%> (ø)
lightning/src/ln/reload_tests.rs 95.24% <0.00%> (ø)
... and 24 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

impl Writeable for OfferContents {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
self.as_tlv_stream().write(writer)
}
}

impl TryFrom<Vec<u8>> for Offer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readable seems more ergonomic as an API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readable only supports DecodeError. Could map ParseError to DecodeError::InvalidValue, but I don't think we'd want to swallow the more specific error. Plus, it would be inconsistent with parsing InvoiceRequest and Invoice, where it's helpful to know the error reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention but also note that Offer needs to take ownership of the bytes.

lightning/src/offers/offer.rs Show resolved Hide resolved
MissingNodeId,
/// An empty set of blinded paths was provided.
MissingPaths,
/// A quantity representing an empty range or that was outside of a valid range was provided.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit confusing wording to parse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote the InvalidQuantity docs. PTAL.

@jkczyz jkczyz force-pushed the 2022-09-offer-parsing branch 2 times, most recently from b35db47 to c9fc32f Compare September 29, 2022 01:44
@jkczyz
Copy link
Contributor Author

jkczyz commented Oct 7, 2022

Updated to parse bech32 strings directly into the higher-level structs instead of an intermediary TLV stream format by adding a TryFrom<Vec<u8>> trait bound to Bech32Encode and dropping the associated type. This is more straightforward and also makes it so that checking that all bytes are read in #1738 only needs to be defined in one place.

@jkczyz jkczyz force-pushed the 2022-09-offer-parsing branch 2 times, most recently from 11b712e to 9eeccb2 Compare November 1, 2022 19:10
@jkczyz jkczyz force-pushed the 2022-09-offer-parsing branch 3 times, most recently from 7fe129f to 0af1aff Compare November 9, 2022 00:22
@jkczyz jkczyz marked this pull request as ready for review November 9, 2022 00:24
@jkczyz
Copy link
Contributor Author

jkczyz commented Nov 10, 2022

FYI, I refactored commit out of the next PR that limits TLV stream decoding to a type range and moved it here.

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically LGTM, really.

lightning/src/offers/offer.rs Outdated Show resolved Hide resolved
lightning/src/util/ser_macros.rs Show resolved Hide resolved
Copy link
Contributor Author

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good feedback

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing major :)

lightning/src/offers/offer.rs Show resolved Hide resolved
lightning/src/offers/parse.rs Outdated Show resolved Hide resolved
lightning/src/offers/offer.rs Outdated Show resolved Hide resolved
}

let mut builder = OfferBuilder::new("foo".into(), pubkey(42));
builder.offer.paths = Some(vec![]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite get why this should cause us to fail to parse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I likely inferred it from this part of the spec:

  - if it is connected only by private channels:
    - MUST include `offer_paths` containing one or more paths to the node from
      publicly reachable nodes.
  - otherwise:
    - MAY include `offer_paths`.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But doesn't that imply offer_paths is optional? Only for public nodes, but that's not something we know at decode-time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True... I guess it is more that we fail if given a paths TLV record with length 0. We are fine parsing it if the paths TLV record doesn't exist. Could remove this check if you prefer.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, no strong opinion, I worry we'd spuriously fail there encountering those in the wild, though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the check.

lightning/src/offers/offer.rs Show resolved Hide resolved
lightning/src/offers/parse.rs Outdated Show resolved Hide resolved
@jkczyz jkczyz force-pushed the 2022-09-offer-parsing branch 2 times, most recently from 391fa0b to 179e961 Compare November 11, 2022 20:49
@TheBlueMatt
Copy link
Collaborator

Feel free to squash IMO.

TheBlueMatt
TheBlueMatt previously approved these changes Nov 16, 2022
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits, feel free to ignore and land, though.


/// Parses a bech32-encoded message into a TLV stream.
fn from_bech32_str(s: &str) -> Result<Self, ParseError> {
// Offer encoding may be split by '+' followed by optional whitespace.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused - are offers allowed to have an arbitrary number of +( )*s? Is there some restriction as to where in the offer they can be that we can avoid copying the whole string next just to call bech32::decode? What is the reason they're allowed to have +s anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to BOLT 12 requirements:

Readers of a bolt12 string:

  • if it encounters a + followed by zero or more whitespace characters between two bech32 characters:
    • MUST remove the + and whitespace.

Rationale is for use in text fields with limited size, like Twitter.

BOLT 12 defines some test vectors currently exercised in fails_parsing_bech32_encoded_offers_with_invalid_continuations.

Could probably avoid the copy if bech32 crate worked on an iterator of characters. But currently bech32::decode also returns slices into the string for HRP and data.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh....I wonder if it makes sense to at least have an optimization for cases with no +? Probably more work than it's worth but nice to avoid copying just because.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done with an enum.


/// Formats the message using bech32-encoding.
fn fmt_bech32_str(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
bech32::encode_without_checksum_to_fmt(f, Self::BECH32_HRP, self.as_ref().to_base32())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grr, its really kinda dumb we're forced to put the intermediate u5s in a vec in between here, but that's upstream's fault - rust-bitcoin/rust-bech32#81

lightning/src/offers/offer.rs Show resolved Hide resolved
lightning/src/offers/parse.rs Show resolved Hide resolved
lightning/src/util/ser_macros.rs Show resolved Hide resolved
lightning/src/offers/offer.rs Outdated Show resolved Hide resolved
@TheBlueMatt
Copy link
Collaborator

LGTM, feel free to squash IMO.

Add common bech32 parsing for BOLT 12 messages. The encoding is similar
to bech32 only without a checksum and with support for continuing
messages across multiple parts.

Messages implementing Bech32Encode are parsed into a TLV stream, which
is converted to the desired message content while performing semantic
checks. Checking after conversion allows for more elaborate checks of
data composed of multiple TLV records and for more meaningful error
messages.

The parsed bytes are also saved to allow creating messages with mirrored
data, even if TLV records are unknown.
Test semantic errors when parsing offer bytes.
BOLT 12 messages are limited to a range of TLV record types. Refactor
decode_tlv_stream into a decode_tlv_stream_range macro for limiting
which types are parsed. Requires a SeekReadable trait for rewinding when
a type outside of the range is seen. This allows for composing TLV
streams of different ranges.

Updates offer parsing accordingly and adds a test demonstrating failure
if a type outside of the range is included.
@TheBlueMatt TheBlueMatt merged commit 8d93dba into lightningdevkit:main Nov 18, 2022
@jkczyz jkczyz mentioned this pull request May 10, 2023
60 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants