Add debug

This commit is contained in:
morrigan 2019-11-27 08:39:06 +01:00
parent fa33cdcd44
commit b5941c750c
5 changed files with 21 additions and 21 deletions

View File

@ -11,7 +11,7 @@
"check-types": "tsc --incremental --noEmit", "check-types": "tsc --incremental --noEmit",
"lint": "eslint --ext .ts src/", "lint": "eslint --ext .ts src/",
"pretest": "yarn check-types", "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": { "devDependencies": {
"@babel/cli": "^7.6.4", "@babel/cli": "^7.6.4",
@ -28,6 +28,7 @@
"@typescript-eslint/parser": "^2.6.0", "@typescript-eslint/parser": "^2.6.0",
"bn.js-typings": "^1.0.1", "bn.js-typings": "^1.0.1",
"chai": "^4.2.0", "chai": "^4.2.0",
"debug": "^4.1.1",
"eslint": "^6.6.0", "eslint": "^6.6.0",
"libp2p-crypto": "^0.17.1", "libp2p-crypto": "^0.17.1",
"mocha": "^6.2.2", "mocha": "^6.2.2",

View File

@ -10,9 +10,7 @@ interface IReturnEncryptionWrapper {
export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper { export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
return async function * (source) { return async function * (source) {
for await (const chunk of source) { for await (const chunk of source) {
console.log("chunk: ", chunk);
const data = await handshake.encrypt(chunk, session); const data = await handshake.encrypt(chunk, session);
console.log("encrypted: ", data);
yield data; yield data;
} }
} }
@ -23,9 +21,7 @@ export function encryptStream(handshake: Handshake, session: NoiseSession) : IRe
export function decryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper { export function decryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
return async function * (source) { return async function * (source) {
for await (const chunk of source) { for await (const chunk of source) {
console.log("Going to decrypt chunk: ", chunk)
const decrypted = await handshake.decrypt(chunk, session); const decrypted = await handshake.decrypt(chunk, session);
console.log("Decrypted: ", decrypted)
yield decrypted yield decrypted
} }
} }

View File

@ -1,15 +1,17 @@
import { Buffer } from "buffer";
import { bytes, bytes32 } from "./@types/basic"; import { bytes, bytes32 } from "./@types/basic";
import { NoiseSession, XXHandshake } from "./xx"; import { NoiseSession, XXHandshake } from "./xx";
import { KeyPair } from "./@types/libp2p"; import { KeyPair } from "./@types/libp2p";
import { Buffer } from "buffer";
import { import {
createHandshakePayload, createHandshakePayload,
decodeMessageBuffer, decodeMessageBuffer,
encodeMessageBuffer, encodeMessageBuffer,
getHandshakePayload, getHandshakePayload,
signPayload logger,
signPayload,
} from "./utils"; } from "./utils";
import {Noise, WrappedConnection} from "./noise"; import { WrappedConnection } from "./noise";
type handshakeType = "XX"; type handshakeType = "XX";
@ -56,9 +58,12 @@ export class Handshake {
const message = Buffer.concat([Buffer.alloc(0), handshakePayload]); const message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(ns, message); const messageBuffer = await this.xx.sendMessage(ns, message);
this.connection.writeLP(encodeMessageBuffer(messageBuffer)); this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger("Stage 0 - Initiator finished proposing");
} else { } else {
const receivedMessageBuffer = (await this.connection.readLP()).slice(); const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(ns, decodeMessageBuffer(receivedMessageBuffer)); const plaintext = await this.xx.recvMessage(ns, decodeMessageBuffer(receivedMessageBuffer));
logger("Stage 0 - Responder received proposed message.");
} }
return ns; return ns;
@ -69,6 +74,7 @@ export class Handshake {
if (this.isInitiator) { if (this.isInitiator) {
const receivedMessageBuffer = (await this.connection.readLP()).slice(); const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer)); const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer));
logger('Stage 1 - Initiator received the message.');
} else { } else {
// create payload as responder // create payload as responder
const signedPayload = signPayload(this.staticKeys.privateKey, getHandshakePayload(this.staticKeys.publicKey)); 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 message = Buffer.concat([Buffer.alloc(0), handshakePayload]);
const messageBuffer = await this.xx.sendMessage(session, message); const messageBuffer = await this.xx.sendMessage(session, message);
this.connection.writeLP(encodeMessageBuffer(messageBuffer)); this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger('Stage 1 - Responder sent the message.')
} }
} }
@ -85,12 +92,12 @@ export class Handshake {
if (this.isInitiator) { if (this.isInitiator) {
const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0)); const messageBuffer = await this.xx.sendMessage(session, Buffer.alloc(0));
this.connection.writeLP(encodeMessageBuffer(messageBuffer)); this.connection.writeLP(encodeMessageBuffer(messageBuffer));
logger('Stage 2 - Initiator sent message.');
} else { } else {
const receivedMessageBuffer = (await this.connection.readLP()).slice(); const receivedMessageBuffer = (await this.connection.readLP()).slice();
const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer)); 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 { encrypt(plaintext: bytes, session: NoiseSession): bytes {

View File

@ -1,11 +1,14 @@
import { x25519, ed25519 } from 'bcrypto'; import { x25519, ed25519 } from 'bcrypto';
import protobuf from "protobufjs"; import protobuf from "protobufjs";
import { Buffer } from "buffer"; import { Buffer } from "buffer";
import debug from "debug";
import { KeyPair } from "./@types/libp2p"; import { KeyPair } from "./@types/libp2p";
import { bytes } from "./@types/basic"; import { bytes } from "./@types/basic";
import { MessageBuffer } from "./xx"; import { MessageBuffer } from "./xx";
export const logger = debug('libp2p:noise');
export async function loadPayloadProto () { export async function loadPayloadProto () {
const payloadProtoBuf = await protobuf.load("protos/payload.proto"); const payloadProtoBuf = await protobuf.load("protos/payload.proto");
return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload"); return payloadProtoBuf.lookupType("pb.NoiseHandshakePayload");

View File

@ -56,21 +56,15 @@ describe("Noise", () => {
wrappedOutbound.writeLP(Buffer.from("test")); wrappedOutbound.writeLP(Buffer.from("test"));
const response = await wrappedInbound.readLP(); const response = await wrappedInbound.readLP();
expect(response.toString()).equal("test"); expect(response.toString()).equal("test");
}) });
/* it("should test that secureOutbound is spec compliant", async() => {
it("should test that secureOutbound is spec compliant", async(done) => {
const libp2pKeys = await generateEd25519Keys(); const libp2pKeys = await generateEd25519Keys();
const libp2pKeys2 = await generateEd25519Keys();
const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes); const noiseInit = new Noise(libp2pKeys._key, localPeer.privKey.bytes);
const noiseResp = new Noise(libp2pKeys2._key, remotePeer.privKey.bytes);
const [inboundConnection, outboundConnection] = DuplexPair(); 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 () => { (async () => {
const wrapped = Wrap(inboundConnection); const wrapped = Wrap(inboundConnection);
const prologue = Buffer.from('/noise'); const prologue = Buffer.from('/noise');
@ -90,5 +84,4 @@ describe("Noise", () => {
})(), })(),
]); ]);
}) })
*/
}); });