1
0
mirror of https://github.com/fluencelabs/js-libp2p-noise synced 2025-05-13 12:41:36 +00:00

29 lines
820 B
TypeScript
Raw Normal View History

2019-11-20 13:23:36 +01:00
import { Duplex } from "it-pair";
2019-11-12 14:02:59 +01:00
import { NoiseSession } from "./xx";
2019-11-25 13:09:40 +01:00
import { Handshake } from "./handshake";
2019-11-12 14:02:59 +01:00
2019-11-28 17:53:27 +01:00
interface ReturnEncryptionWrapper {
2019-11-25 13:09:40 +01:00
(source: any): any;
2019-11-12 14:02:59 +01:00
}
2019-11-25 13:09:40 +01:00
// Returns generator that encrypts payload from the user
2019-11-28 17:53:27 +01:00
export function encryptStream(handshake: Handshake, session: NoiseSession): ReturnEncryptionWrapper {
2019-11-25 13:09:40 +01:00
return async function * (source) {
for await (const chunk of source) {
const data = await handshake.encrypt(chunk, session);
yield data;
}
}
}
2019-11-12 14:02:59 +01:00
2019-11-25 13:09:40 +01:00
// Decrypt received payload to the user
2019-11-28 17:53:27 +01:00
export function decryptStream(handshake: Handshake, session: NoiseSession): ReturnEncryptionWrapper {
2019-11-25 13:09:40 +01:00
return async function * (source) {
for await (const chunk of source) {
const decrypted = await handshake.decrypt(chunk, session);
yield decrypted
}
}
2019-11-12 14:02:59 +01:00
}