From b5941c750c49b624a34130d8433a1617dd96ad17 Mon Sep 17 00:00:00 2001 From: morrigan Date: Wed, 27 Nov 2019 08:39:06 +0100 Subject: [PATCH] Add debug --- package.json | 3 ++- src/crypto.ts | 4 ---- src/handshake.ts | 17 ++++++++++++----- src/utils.ts | 3 +++ test/noise.test.ts | 15 ++++----------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 391ece7..6a89844 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "check-types": "tsc --incremental --noEmit", "lint": "eslint --ext .ts src/", "pretest": "yarn check-types", - "test": "mocha -r ./babel-register.js \"test/**/*.test.ts\"" + "test": "DEBUG=libp2p:noise mocha -r ./babel-register.js \"test/**/*.test.ts\"" }, "devDependencies": { "@babel/cli": "^7.6.4", @@ -28,6 +28,7 @@ "@typescript-eslint/parser": "^2.6.0", "bn.js-typings": "^1.0.1", "chai": "^4.2.0", + "debug": "^4.1.1", "eslint": "^6.6.0", "libp2p-crypto": "^0.17.1", "mocha": "^6.2.2", diff --git a/src/crypto.ts b/src/crypto.ts index d1c0ea6..d3a8a06 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -10,9 +10,7 @@ interface IReturnEncryptionWrapper { export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper { return async function * (source) { for await (const chunk of source) { - console.log("chunk: ", chunk); const data = await handshake.encrypt(chunk, session); - console.log("encrypted: ", data); yield data; } } @@ -23,9 +21,7 @@ export function encryptStream(handshake: Handshake, session: NoiseSession) : IRe export function decryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper { return async function * (source) { for await (const chunk of source) { - console.log("Going to decrypt chunk: ", chunk) const decrypted = await handshake.decrypt(chunk, session); - console.log("Decrypted: ", decrypted) yield decrypted } } diff --git a/src/handshake.ts b/src/handshake.ts index cf378f5..4fc9a0e 100644 --- a/src/handshake.ts +++ b/src/handshake.ts @@ -1,15 +1,17 @@ +import { Buffer } from "buffer"; + import { bytes, bytes32 } from "./@types/basic"; import { NoiseSession, XXHandshake } from "./xx"; import { KeyPair } from "./@types/libp2p"; -import { Buffer } from "buffer"; import { createHandshakePayload, decodeMessageBuffer, encodeMessageBuffer, getHandshakePayload, - signPayload + logger, + signPayload, } from "./utils"; -import {Noise, WrappedConnection} from "./noise"; +import { WrappedConnection } from "./noise"; type handshakeType = "XX"; @@ -56,9 +58,12 @@ export class Handshake { const message = Buffer.concat([Buffer.alloc(0), handshakePayload]); const messageBuffer = await this.xx.sendMessage(ns, message); this.connection.writeLP(encodeMessageBuffer(messageBuffer)); + + logger("Stage 0 - Initiator finished proposing"); } else { const receivedMessageBuffer = (await this.connection.readLP()).slice(); const plaintext = await this.xx.recvMessage(ns, decodeMessageBuffer(receivedMessageBuffer)); + logger("Stage 0 - Responder received proposed message."); } return ns; @@ -69,6 +74,7 @@ export class Handshake { if (this.isInitiator) { const receivedMessageBuffer = (await this.connection.readLP()).slice(); const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer)); + logger('Stage 1 - Initiator received the message.'); } else { // create payload as responder const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey)); @@ -77,6 +83,7 @@ export class Handshake { const message = Buffer.concat([Buffer.alloc(0), handshakePayload]); const messageBuffer = await this.xx.sendMessage(session, message); this.connection.writeLP(encodeMessageBuffer(messageBuffer)); + logger('Stage 1 - Responder sent the message.') } } @@ -85,12 +92,12 @@ export class Handshake { if (this.isInitiator) { const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0)); this.connection.writeLP(encodeMessageBuffer(messageBuffer)); + logger('Stage 2 - Initiator sent message.'); } else { const receivedMessageBuffer = (await this.connection.readLP()).slice(); const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer)); + logger('Stage 2 - Responder received the message, finished handshake.') } - - console.log("FINISHED HANDSHAKE, is initiator: ", this.isInitiator); } encrypt(plaintext: bytes, session: NoiseSession): bytes { diff --git a/src/utils.ts b/src/utils.ts index 3676a59..31c03ce 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,11 +1,14 @@ import { x25519, ed25519 } from 'bcrypto'; import protobuf from "protobufjs"; import { Buffer } from "buffer"; +import debug from "debug"; import { KeyPair } from "./@types/libp2p"; import { bytes } from "./@types/basic"; import { MessageBuffer } from "./xx"; +export const logger = debug('libp2p:noise'); + export async function loadPayloadProto () { const payloadProtoBuf = await protobuf.load("protos/payload.proto"); return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload"); diff --git a/test/noise.test.ts b/test/noise.test.ts index 8e34fe0..ce4208f 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -56,21 +56,15 @@ describe("Noise", () => { wrappedOutbound.writeLP(Buffer.from("test")); const response = await wrappedInbound.readLP(); expect(response.toString()).equal("test"); - }) + }); - /* - it("should test that secureOutbound is spec compliant", async(done) => { + it("should test that secureOutbound is spec compliant", async() => { const libp2pKeys = await generateEd25519Keys(); - const libp2pKeys2 = await generateEd25519Keys(); - const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes); - const noiseResp = new Noise(libp2pKeys2._key, remotePeer.privKey.bytes); - const [inboundConnection, outboundConnection] = DuplexPair(); - const [outbound, inbound] = await Promise.all([ - noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer), - noiseResp.secureInbound(remotePeer, inboundConnection, localPeer), + const [outbound] = await Promise.all([ + noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer), (async () => { const wrapped = Wrap(inboundConnection); const prologue = Buffer.from('/noise'); @@ -90,5 +84,4 @@ describe("Noise", () => { })(), ]); }) - */ });