mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-06-24 10:12:01 +00:00
Add debug
This commit is contained in:
@ -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");
|
||||
|
Reference in New Issue
Block a user