mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 12:02:32 +00:00
Add debug
This commit is contained in:
parent
fa33cdcd44
commit
b5941c750c
@ -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",
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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");
|
||||
|
@ -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", () => {
|
||||
})(),
|
||||
]);
|
||||
})
|
||||
*/
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user