Compile-time safety for handshake#121
Merged
nyonson merged 5 commits intorust-bitcoin:mainfrom Jul 3, 2025
Merged
Conversation
7412b98 to
56e5892
Compare
Introducing the "TypeState" pattern to the handshake. This moves the runtime error variant, HandshakeOutOfOrder, to compile time. The type system now doesn't allow for out of order calls.
The BIP-324 protocol is packet in nature, not stream based, and the encrypted packets are not the same size as plaintext. They get length bits added to the front and authentication bits tacked on the back. So encryption in place would be a huge pain. And generally decryption in place is weird too, because there are these bytes on either end which no longer make sense to the plaintext. However, there is a solid use case for *decryption* in place in the handshake where packet content is not really cared about.
Update the receive_version to take a mutable input buffer instead of an immutable input and a mutable output buffer. During the handshake, callers don't care about decoy packets or version packet. And they really don't want to deal with the variable sized output buffer.
The real tricky part of the handshake is reading the unknown number of garbage bytes from the remote peer. That was hard to tease apart when combined with the authenticating the garbage with the following decoy or version packet, so split into two steps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.