mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-26 19:32:28 +00:00
Fix test
This commit is contained in:
parent
2af4c744df
commit
fa33cdcd44
@ -23,7 +23,9 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,8 @@ export class Handshake {
|
|||||||
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("FINISHED HANDSHAKE, is initiator: ", this.isInitiator);
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypt(plaintext: bytes, session: NoiseSession): bytes {
|
encrypt(plaintext: bytes, session: NoiseSession): bytes {
|
||||||
|
@ -91,8 +91,6 @@ export class Noise implements NoiseConnection {
|
|||||||
const [secure, user] = DuplexPair();
|
const [secure, user] = DuplexPair();
|
||||||
const network = connection.unwrap();
|
const network = connection.unwrap();
|
||||||
|
|
||||||
console.log("Unwrapped network: ", network)
|
|
||||||
|
|
||||||
pipe(
|
pipe(
|
||||||
secure, // write to wrapper
|
secure, // write to wrapper
|
||||||
ensureBuffer, // ensure any type of data is converted to buffer
|
ensureBuffer, // ensure any type of data is converted to buffer
|
||||||
|
@ -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 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 [inboundConnection, outboundConnection] = DuplexPair();
|
||||||
const [outbound, inbound] = await Promise.all([
|
const [outbound, inbound] = await Promise.all([
|
||||||
noise.secureOutbound(localPeer, outboundConnection, remotePeer),
|
noiseInit.secureOutbound(localPeer, outboundConnection, remotePeer),
|
||||||
noise.secureInbound(remotePeer, inboundConnection, localPeer),
|
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);
|
|
||||||
// })(),
|
|
||||||
]);
|
]);
|
||||||
const wrappedInbound = Wrap(inbound.conn);
|
const wrappedInbound = Wrap(inbound.conn);
|
||||||
const wrappedOutbound = Wrap(outbound.conn);
|
const wrappedOutbound = Wrap(outbound.conn);
|
||||||
|
|
||||||
wrappedInbound.readLP().then((log) => {
|
|
||||||
console.log("Read this thing: ", log);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
|
|
||||||
wrappedOutbound.writeLP(Buffer.from("test"));
|
wrappedOutbound.writeLP(Buffer.from("test"));
|
||||||
|
const response = await wrappedInbound.readLP();
|
||||||
console.log("Payload is: ", Buffer.from("test"))
|
expect(response.toString()).equal("test");
|
||||||
|
|
||||||
// const response = (await inbound.conn.source.next()).value;
|
|
||||||
// console.log(response);
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/*
|
||||||
|
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);
|
||||||
|
})(),
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
*/
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user