diff --git a/src/crypto.ts b/src/crypto.ts index 8385595..d1c0ea6 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -23,7 +23,9 @@ 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 df0c922..cf378f5 100644 --- a/src/handshake.ts +++ b/src/handshake.ts @@ -89,6 +89,8 @@ export class Handshake { const receivedMessageBuffer = (await this.connection.readLP()).slice(); const plaintext = await this.xx.recvMessage(session, decodeMessageBuffer(receivedMessageBuffer)); } + + console.log("FINISHED HANDSHAKE, is initiator: ", this.isInitiator); } encrypt(plaintext: bytes, session: NoiseSession): bytes { diff --git a/src/noise.ts b/src/noise.ts index ebc89c1..7cb06f4 100644 --- a/src/noise.ts +++ b/src/noise.ts @@ -91,8 +91,6 @@ export class Noise implements NoiseConnection { const [secure, user] = DuplexPair(); const network = connection.unwrap(); - console.log("Unwrapped network: ", network) - pipe( secure, // write to wrapper ensureBuffer, // ensure any type of data is converted to buffer diff --git a/test/noise.test.ts b/test/noise.test.ts index c54b8e6..8e34fe0 100644 --- a/test/noise.test.ts +++ b/test/noise.test.ts @@ -38,47 +38,57 @@ describe("Noise", () => { } }); - it("should test that secureOutbound is spec compliant", async(done) => { + it("should communicate through encrypted streams", async() => { const libp2pKeys = await generateEd25519Keys(); + const libp2pKeys2 = await generateEd25519Keys(); - const noise = new Noise(libp2pKeys._key); + 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([ - noise.secureOutbound(localPeer, outboundConnection, remotePeer), - noise.secureInbound(remotePeer, inboundConnection, localPeer), - - // (async () => { - // const wrapped = Wrap(inboundConnection); - // const prologue = Buffer.from('/noise'); - // const staticKeys = { - // privateKey: remotePeer.privKey.bytes, - // publicKey: remotePeer.pubKey.bytes, - // }; - // const handshake = new Handshake('XX', false, localPeer.pubKey.bytes, prologue, staticKeys, wrapped); - // - // // Finish handshake - // const sessionResponder = await handshake.propose(Buffer.alloc(0)); - // await handshake.exchange(sessionResponder); - // await handshake.finish(sessionResponder); - // - // // Create the encrypted streams - // console.log(sessionResponder); - // })(), + noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer), + noiseResp.secureInbound(remotePeer, inboundConnection, localPeer), ]); const wrappedInbound = Wrap(inbound.conn); const wrappedOutbound = Wrap(outbound.conn); - wrappedInbound.readLP().then((log) => { - console.log("Read this thing: ", log); - done(); - }); - wrappedOutbound.writeLP(Buffer.from("test")); - - console.log("Payload is: ", Buffer.from("test")) - - // const response = (await inbound.conn.source.next()).value; - // console.log(response); + const response = await wrappedInbound.readLP(); + expect(response.toString()).equal("test"); }) + + /* + it("should test that secureOutbound is spec compliant", async(done) => { + 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), + + (async () => { + const wrapped = Wrap(inboundConnection); + const prologue = Buffer.from('/noise'); + const staticKeys = { + privateKey: remotePeer.privKey.bytes, + publicKey: remotePeer.pubKey.bytes, + }; + const handshake = new Handshake('XX', false, localPeer.pubKey.bytes, prologue, staticKeys, wrapped); + + // Finish handshake + const sessionResponder = await handshake.propose(Buffer.alloc(0)); + await handshake.exchange(sessionResponder); + await handshake.finish(sessionResponder); + + // Create the encrypted streams + console.log(sessionResponder); + })(), + ]); + }) + */ });