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 withsrc/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.
Motivation:
Review notes:
src/protocol.ts
which contains the core noise protocol (agnostic to libp2p's usage)src/performHandshake.ts
which uses the noise protocol according to the libp2p spec. The initiator and responder flows are broken into separate functions.Description
src/handshakes
withsrc/protocol.ts
, this is noise protocol code, following the spec verbatim, agnostic to libp2p usagesrc/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.BREAKING CHANGE: The return type of
ICryptoInterface#chaCha20Poly1305Decrypt
has changed, now no longer returnsnull
but rather throws on invalid decryption.