mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 12:52:15 +00:00
Add another test + logger
This commit is contained in:
parent
47e295add5
commit
244348c596
@ -44,15 +44,20 @@ export class IKHandshake implements IHandshake {
|
||||
|
||||
public async stage0(): Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
logger("IK Stage 0 - Initiator sending message...");
|
||||
const messageBuffer = this.ik.sendMessage(this.session, this.payload);
|
||||
this.connection.writeLP(encode1(messageBuffer));
|
||||
logger("IK Stage 0 - Initiator sent message.");
|
||||
} else {
|
||||
logger("IK Stage 0 - Responder receiving message...");
|
||||
const receivedMsg = (await this.connection.readLP()).slice();
|
||||
const receivedMessageBuffer = decode1(Buffer.from(receivedMsg));
|
||||
const plaintext = this.ik.recvMessage(this.session, receivedMessageBuffer);
|
||||
|
||||
try {
|
||||
const receivedMessageBuffer = decode1(Buffer.from(receivedMsg));
|
||||
const plaintext = this.ik.recvMessage(this.session, receivedMessageBuffer);
|
||||
|
||||
logger("IK Stage 0 - Responder got message, going to verify payload.");
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
logger("IK Stage 0 - Responder successfully verified payload!");
|
||||
} catch (e) {
|
||||
logger("Responder breaking up with IK handshake in stage 0.");
|
||||
throw new FailedIKError(receivedMsg, `Error occurred while verifying initiator's signed payload: ${e.message}`);
|
||||
@ -62,19 +67,24 @@ export class IKHandshake implements IHandshake {
|
||||
|
||||
public async stage1(): Promise<void> {
|
||||
if (this.isInitiator) {
|
||||
logger("IK Stage 1 - Initiator receiving message...");
|
||||
const receivedMsg = (await this.connection.readLP()).slice();
|
||||
const receivedMessageBuffer = decode0(Buffer.from(receivedMsg));
|
||||
const plaintext = this.ik.recvMessage(this.session, receivedMessageBuffer);
|
||||
logger("IK Stage 1 - Initiator got message, going to verify payload.");
|
||||
|
||||
try {
|
||||
await verifySignedPayload(receivedMessageBuffer.ns, plaintext, this.remotePeer.id);
|
||||
logger("IK Stage 1 - Initiator successfully verified payload!");
|
||||
} catch (e) {
|
||||
logger("Initiator breaking up with IK handshake in stage 1.");
|
||||
throw new FailedIKError(receivedMsg, `Error occurred while verifying responder's signed payload: ${e.message}`);
|
||||
}
|
||||
} else {
|
||||
logger("IK Stage 1 - Responder sending message...");
|
||||
const messageBuffer = this.ik.sendMessage(this.session, this.payload);
|
||||
this.connection.writeLP(encode0(messageBuffer));
|
||||
logger("IK Stage 1 - Responder sent message...");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@ export class XXFallbackHandshake extends XXHandshake {
|
||||
this.xx.sendMessage(this.session, Buffer.alloc(0), this.ephemeralKeys);
|
||||
logger("XX Fallback Stage 0 - Initialized state as the first message was sent by initiator.");
|
||||
} else {
|
||||
logger("XX Fallback Stage 0 - Responder decoding initial msg from IK.")
|
||||
const receivedMessageBuffer = decode0(this.initialMsg);
|
||||
this.xx.recvMessage(this.session, {
|
||||
ne: receivedMessageBuffer.ne,
|
||||
|
@ -199,8 +199,7 @@ describe("Noise", () => {
|
||||
const staticKeysInitiator = generateKeypair();
|
||||
const noiseInit = new Noise(staticKeysInitiator.privateKey);
|
||||
const staticKeysResponder = generateKeypair();
|
||||
console.log("staticKeysInitiator: ", staticKeysInitiator)
|
||||
console.log("staticKeysResponder: ", staticKeysResponder)
|
||||
|
||||
const noiseResp = new Noise(staticKeysResponder.privateKey, undefined, false);
|
||||
const xxSpy = sandbox.spy(noiseInit, "performXXFallbackHandshake");
|
||||
|
||||
@ -228,4 +227,39 @@ describe("Noise", () => {
|
||||
assert(false, e.message);
|
||||
}
|
||||
});
|
||||
|
||||
it("Initiator starts with XX (pipes disabled) responder has noise pipes", async() => {
|
||||
try {
|
||||
const staticKeysInitiator = generateKeypair();
|
||||
const noiseInit = new Noise(staticKeysInitiator.privateKey, undefined, false);
|
||||
const staticKeysResponder = generateKeypair();
|
||||
|
||||
const noiseResp = new Noise(staticKeysResponder.privateKey);
|
||||
const xxInitSpy = sandbox.spy(noiseInit, "performXXHandshake");
|
||||
const xxRespSpy = sandbox.spy(noiseResp, "performXXFallbackHandshake");
|
||||
|
||||
// Prepare key cache for noise pipes
|
||||
await KeyCache.store(localPeer, staticKeysInitiator.publicKey);
|
||||
|
||||
const [inboundConnection, outboundConnection] = DuplexPair();
|
||||
|
||||
const [outbound, inbound] = await Promise.all([
|
||||
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
||||
noiseResp.secureInbound(remotePeer, inboundConnection, localPeer),
|
||||
]);
|
||||
|
||||
const wrappedInbound = Wrap(inbound.conn);
|
||||
const wrappedOutbound = Wrap(outbound.conn);
|
||||
|
||||
wrappedOutbound.writeLP(Buffer.from("test fallback"));
|
||||
const response = await wrappedInbound.readLP();
|
||||
expect(response.toString()).equal("test fallback");
|
||||
|
||||
assert(xxInitSpy.calledOnce, "XX method was never called.");
|
||||
assert(xxRespSpy.calledOnce, "XX Fallback method was never called.");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
assert(false, e.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user