Skip to content

WIP: Bolt 11 invoices #9

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

Closed
wants to merge 3 commits into from
Closed

Conversation

sgeisler
Copy link
Collaborator

@sgeisler sgeisler commented Mar 8, 2018

This branch implements lightning invoices and their bech32 en- and decoding. I've already defines the data structures, but I will probably change them to use more semantic types. The parser is written with nom and currently under construction. I haven't worked on serialization yet.

  • Define data structures
  • Parse bech32 invoices
  • Serialize to bech32
  • Documentation

I'd be happy if someone more familiar with the lightning protocol could have a look at the data structures.

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.

Thanks for tackling this! If possible, can you cut down on the dependancy introduction so that things are more readable/pure Rust instead of a long mess of macro invocations that you have to look up the meaning of?

regex = "0.2"
bit-vec = "0.4.4"
nom = "3.2.1"
error-chain = "0.11.0"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, do you really need all these deps? Seems like a lot just to save a few lines on parsing code. See-also: dep guidelines in README.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

dep guidelines in README.

Thank you for pointing that out, I probably skipped right ahead to the todo's. Getting rid of nom and bit-vec will be easy (nom looked nice at the beginning, but isn't that well documented etc. and not really of much help, I just stuck with it because it was challenging and fun). I can probably live without error-chain/failure too. Loosing regex could be a bit annoying.

}
}

// TODO: better types instead onf byte arrays
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just use rust-bitcoin's Script object directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Wouldn't that have quite bad ergonomics? It forces the user to build a complete script and our library checks if it is one of the three supported types, strips away all the boilerplate/OP_CODEs and extracts the hash that the user probably had to begin with. The other way around seems to be a bit cumbersome too: how do I generate the corresponding address from the script (even worse: a to_address() function will have to return a Result because afaik not every instance of Script is encodable as some type of address)?

That's just how I understood Script from the docs, maybe I missed something.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, oops, sorry, I was just thinking the deserialize -> internal struct direction, not struct -> serialized form direction. Probably time to clean up the rust-bitcoin address stuff so that it supports more types of addresses... @apoelstra ?

amount,
timestamp: Utc::now(),
tagged: vec![],
signature: Signature::from_der(&Secp256k1::new(), &[0; 65])?,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be from_compact not from_der (I believe this is busted elsewhere in the codebase right now...)

@TheBlueMatt
Copy link
Collaborator

TheBlueMatt commented Mar 8, 2018 via email

@sgeisler
Copy link
Collaborator Author

sgeisler commented Mar 8, 2018

An extra repo would be nice, the name would probably be rust-lightning-invoice and the crate's name lightning-invoice, or do you have other suggestions? I will work on that and on refactoring out the unnecessary dependencies.

Btw: thank you for taking the time to review my code :)

@TheBlueMatt
Copy link
Collaborator

That sounds great. Happy to open up a new repo on rust-bitcoin for it!

@sgeisler
Copy link
Collaborator Author

sgeisler commented Mar 8, 2018

Oh, relatedly, some folks had asked that the BOLT 11 implementation be a separate crate

When I read that I wondered if there are any other communication channels than this repo and the lightning-dev list? What's the best way to stay in touch?

@TheBlueMatt
Copy link
Collaborator

The comment I was referencing was made at https://www.reddit.com/r/Bitcoin/comments/81shlp/lightning_network_still_needs_a_lot_of_work/dv5sti8/ I'm not aware of any other communication methods for rust-bitcoin stuff, but maybe we should make an IRC channel or something.

@sgeisler
Copy link
Collaborator Author

sgeisler commented Mar 8, 2018

I've created an own repo for now. I will copy the license and the relevant parts of the README.md of this repo. We can transfer it to rust-bitcoin whenever you want.

@sgeisler sgeisler closed this Mar 8, 2018
@TheBlueMatt
Copy link
Collaborator

I believe you have to initiate the transfer.

@sgeisler
Copy link
Collaborator Author

sgeisler commented Mar 9, 2018

Did rust-bitcoin receive a transfer request?

The transfer dialog told me that the transfer would happen when the new owner confirms it. But after pressing the confirm button I got the following error message

You don’t have the permission to create repositories on rust-bitcoin

@sgeisler sgeisler deleted the bolt-11 branch March 9, 2018 12:54
@TheBlueMatt
Copy link
Collaborator

Hmm, well I added you to the org so it should work now.

@SWvheerden SWvheerden mentioned this pull request Nov 1, 2018
4 tasks
vss96 referenced this pull request in vss96/rust-lightning Sep 21, 2021
# This is the 1st commit message:

test utils: refactor fail_payment_along_route for mpp

# This is the commit message #2:

Add MppId field to HTLCSource as a way to correlate mpp payment paths

# This is the commit message #3:

Implement Readable/Writeable for HashSet

To be used in upcoming commits for MPP ID storage

# This is the commit message #4:

Add MPP ID to pending_outbound_htlcs

We'll use this to correlate MPP shards in upcoming commits

# This is the commit message #5:

Prevent duplicate PaymentSent events

by removing all pending outbound payments associated with the same
MPP payment after the preimage is received

# This is the commit message #6:

Add all_paths_failed field to PaymentFailed

see field docs for details

# This is the commit message #7:

Drop writer size hinting/message vec preallocation

In order to avoid significant malloc traffic, messages previously
explicitly stated their serialized length allowing for Vec
preallocation during the message serialization pipeline. This added
some amount of complexity in the serialization code, but did avoid
some realloc() calls.

Instead, here, we drop all the complexity in favor of a fixed 2KiB
buffer for all message serialization. This should not only be
simpler with a similar reduction in realloc() traffic, but also
may reduce heap fragmentation by allocating identically-sized
buffers more often.

# This is the commit message #8:

[fuzz] Swap mode on most messages to account for TLV suffix

# This is the commit message #9:

Add forward-compat due serialization variants of HTLCFailureMsg

Going forward, all lightning messages have a TLV stream suffix,
allowing new fields to be added as needed. In the P2P protocol,
messages have an explicit length, so there is no implied length in
the TLV stream itself. HTLCFailureMsg enum variants have messages
in them, but without a size prefix or any explicit end. Thus, if a
HTLCFailureMsg is read as a part of a ChannelManager, with a TLV
stream at the end, there is no way to differentiate between the end
of the message and the next field(s) in the ChannelManager.

Here we add two new variant values for HTLCFailureMsg variants in
the read path, allowing us to switch to the new values if/when we
add new TLV fields in UpdateFailHTLC or UpdateFailMalformedHTLC so
that older versions can still read the new TLV fields.

# This is the commit message #10:

Convert most P2P msg serialization to a new macro with TLV suffixes

The network serialization format for all messages was changed some
time ago to include a TLV suffix for all messages, however we never
bothered to implement it as there isn't a lot of use validating a
TLV stream with nothing to do with it. However, messages are
increasingly utilizing the TLV suffix feature, and there are some
compatibility concerns with messages written as a part of other
structs having their format changed (see previous commit).

Thus, here we go ahead and convert most message serialization to a
new macro which includes a TLV suffix after a series of fields,
simplifying several serialization implementations in the process.

# This is the commit message #11:

Update docs to specify where process events is called
vss96 referenced this pull request in vss96/rust-lightning Sep 21, 2021
# This is the 1st commit message:

Update fuzz README with latest instructions

# This is the commit message #2:

Allow multiple monitor_update_failed calls

without requiring calls to channel_monitor_updated in between.

Found by the fuzzer

# This is the commit message #3:

Move CounterpartyForwardingInfo from channel to channelmanager

CounterpartyForwardingInfo is public (previously exposed with a
`pub use`), and used inside of ChannelCounterparty in
channelmanager.rs. However, it is defined in channel.rs, away from
where it is used.

This would be fine, except that the bindings generator is somewhat
confused by this - it doesn't currently support interpreting
`pub use` as a struct to expose, instead ignoring it.

Fixes lightningdevkit/ldk-garbagecollected#44

# This is the commit message #4:

test utils: refactor fail_payment_along_route for mpp

# This is the commit message #5:

Add MppId field to HTLCSource as a way to correlate mpp payment paths

# This is the commit message #6:

Implement Readable/Writeable for HashSet

To be used in upcoming commits for MPP ID storage

# This is the commit message #7:

Add MPP ID to pending_outbound_htlcs

We'll use this to correlate MPP shards in upcoming commits

# This is the commit message #8:

Prevent duplicate PaymentSent events

by removing all pending outbound payments associated with the same
MPP payment after the preimage is received

# This is the commit message #9:

Add all_paths_failed field to PaymentFailed

see field docs for details

# This is the commit message #10:

Drop writer size hinting/message vec preallocation

In order to avoid significant malloc traffic, messages previously
explicitly stated their serialized length allowing for Vec
preallocation during the message serialization pipeline. This added
some amount of complexity in the serialization code, but did avoid
some realloc() calls.

Instead, here, we drop all the complexity in favor of a fixed 2KiB
buffer for all message serialization. This should not only be
simpler with a similar reduction in realloc() traffic, but also
may reduce heap fragmentation by allocating identically-sized
buffers more often.

# This is the commit message #11:

[fuzz] Swap mode on most messages to account for TLV suffix

# This is the commit message #12:

Add forward-compat due serialization variants of HTLCFailureMsg

Going forward, all lightning messages have a TLV stream suffix,
allowing new fields to be added as needed. In the P2P protocol,
messages have an explicit length, so there is no implied length in
the TLV stream itself. HTLCFailureMsg enum variants have messages
in them, but without a size prefix or any explicit end. Thus, if a
HTLCFailureMsg is read as a part of a ChannelManager, with a TLV
stream at the end, there is no way to differentiate between the end
of the message and the next field(s) in the ChannelManager.

Here we add two new variant values for HTLCFailureMsg variants in
the read path, allowing us to switch to the new values if/when we
add new TLV fields in UpdateFailHTLC or UpdateFailMalformedHTLC so
that older versions can still read the new TLV fields.

# This is the commit message #13:

Convert most P2P msg serialization to a new macro with TLV suffixes

The network serialization format for all messages was changed some
time ago to include a TLV suffix for all messages, however we never
bothered to implement it as there isn't a lot of use validating a
TLV stream with nothing to do with it. However, messages are
increasingly utilizing the TLV suffix feature, and there are some
compatibility concerns with messages written as a part of other
structs having their format changed (see previous commit).

Thus, here we go ahead and convert most message serialization to a
new macro which includes a TLV suffix after a series of fields,
simplifying several serialization implementations in the process.

# This is the commit message #14:

Update docs to specify where process events is called
alecchendev pushed a commit to alecchendev/rust-lightning that referenced this pull request Aug 29, 2023
…-panic-gossip

Don't panic if we can't sign a gossip message.
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.

2 participants