mirror of
https://github.com/fluencelabs/js-libp2p-noise
synced 2025-04-25 12:22:20 +00:00
29 lines
826 B
TypeScript
29 lines
826 B
TypeScript
import { Duplex } from "it-pair";
|
|
import { NoiseSession } from "./xx";
|
|
import { Handshake } from "./handshake";
|
|
|
|
interface IReturnEncryptionWrapper {
|
|
(source: any): any;
|
|
}
|
|
|
|
// Returns generator that encrypts payload from the user
|
|
export function encryptStream(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
|
|
return async function * (source) {
|
|
for await (const chunk of source) {
|
|
const data = await handshake.encrypt(chunk, session);
|
|
yield data;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Decrypt received payload to the user
|
|
export function decryptStreams(handshake: Handshake, session: NoiseSession) : IReturnEncryptionWrapper {
|
|
return async function * (source) {
|
|
for await (const chunk of source) {
|
|
const decrypted = await handshake.decrypt(chunk, session);
|
|
yield decrypted
|
|
}
|
|
}
|
|
}
|