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

feat!: refactor codebase #407

Merged
merged 9 commits into from
Jan 16, 2024
Merged

feat!: refactor codebase #407

merged 9 commits into from
Jan 16, 2024

Conversation

wemeetagain
Copy link
Member

@wemeetagain wemeetagain commented Dec 16, 2023

Motivation:

  • While debugging lodestar with js-libp2p v1.0 I was constantly visually tracing through this library.
  • There were several organizational deficiencies that distracted from clearly understanding the code flow.
  • This is likely because the usage of noise in libp2p has changed over time.
  • This refactoring should result in better auditability of this library.

Review notes:

  • Each commit except the last one is small and digestible, can be reviewed individually
  • Most changes come from the last commit
    • start with src/protocol.ts which contains the core noise protocol (agnostic to libp2p's usage)
    • then visit src/performHandshake.ts which uses the noise protocol according to the libp2p spec. The initiator and responder flows are broken into separate functions.
    • then everything else

Description

  • Fixes to node crypto (in encryption and dh)
  • Improved uint16 encode/decode perf
  • Reduced allocations of known values (eg: the empty Uint8Array)
  • Most types have been aligned with the noise spec where possible, better commented
  • Replace code in src/handshakes with src/protocol.ts, this is noise protocol code, following the spec verbatim, agnostic to libp2p usage
  • Replace code in src/xx-handshake.ts with src/performHandshake.ts` which performs a noise handshake, libp2p-style. This adds logging, reading/writing to a connection, and sending/receiving/verifying a libp2p noise payload.
  • Utility functions surrounding the libp2p noise payload have been simplified

BREAKING CHANGE:
The return type of ICryptoInterface#chaCha20Poly1305Decrypt has changed, now no longer returns null but rather throws on invalid decryption.

@wemeetagain wemeetagain requested a review from a team as a code owner December 16, 2023 05:49
@wemeetagain wemeetagain requested review from achingbrain, mpetrunic and a team and removed request for a team December 16, 2023 05:49

constructor (crypto: ICrypto, k: Uint8Array | undefined = undefined, n = 0) {
this.crypto = crypto
this.k = k
Copy link
Contributor

Choose a reason for hiding this comment

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

Single letter variable names should at least have comments for clarity

Copy link
Member Author

Choose a reason for hiding this comment

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

If you use an IDE, all protocol-related types have comments.

@@ -181,8 +185,7 @@ export const defaultCrypto: ICryptoInterface = {
publicKey
], X25519_PREFIX.byteLength + publicKey.byteLength)
} else {
publicKey.prepend(X25519_PREFIX)
publicKey = publicKey.subarray()
publicKey = new Uint8ArrayList(X25519_PREFIX, publicKey).subarray()
Copy link
Contributor

Choose a reason for hiding this comment

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

you should write a test to ensure that this state is consistent

Copy link
Member Author

Choose a reason for hiding this comment

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

a regression test?
This fixes a bug where the input Uint8ArrayList was mutated. When that input Uint8ArrayList is part of the noise state, then that mutation breaks things.

@wemeetagain wemeetagain changed the title feat: refactor codebase feat!: refactor codebase Jan 8, 2024
@wemeetagain wemeetagain merged commit 9ebbfc5 into master Jan 16, 2024
16 checks passed
@wemeetagain wemeetagain deleted the fix/cleanup branch January 16, 2024 17:55
@wemeetagain
Copy link
Member Author

argh release-please never picked this up!

@mpetrunic do you have any ideas?

@wemeetagain
Copy link
Member Author

nevermind, it just appeared!

@mpetrunic
Copy link
Member

nevermind, it just appeared!

Souch impatience and doubt in release-please 😋

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.

3 participants