Skip to content

Commit db66eeb

Browse files
chore: Add prefix in encryptStream (#236)
* Add prefix in encryptStream * chore: fix lint error Co-authored-by: Cayman <caymannava@gmail.com>
1 parent 4803ffd commit db66eeb

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

Diff for: src/crypto/streaming.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Transform } from 'it-stream-types'
22
import type { Uint8ArrayList } from 'uint8arraylist'
33
import type { IHandshake } from '../@types/handshake-interface.js'
44
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from '../constants.js'
5+
import { uint16BEEncode } from '../encoder.js'
56

67
// Returns generator that encrypts payload from the user
78
export function encryptStream (handshake: IHandshake): Transform<Uint8Array> {
@@ -14,6 +15,8 @@ export function encryptStream (handshake: IHandshake): Transform<Uint8Array> {
1415
}
1516

1617
const data = handshake.encrypt(chunk.subarray(i, end), handshake.session)
18+
19+
yield uint16BEEncode(data.byteLength)
1720
yield data
1821
}
1922
}

Diff for: src/encoder.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
22
import type { Uint8ArrayList } from 'uint8arraylist'
33
import type { bytes } from './@types/basic.js'
44
import type { MessageBuffer } from './@types/handshake.js'
5-
import type { LengthDecoderFunction, LengthEncoderFunction } from 'it-length-prefixed'
5+
import type { LengthDecoderFunction } from 'it-length-prefixed'
66

77
const allocUnsafe = (len: number): Uint8Array => {
88
if (globalThis.Buffer) {
@@ -12,7 +12,7 @@ const allocUnsafe = (len: number): Uint8Array => {
1212
return new Uint8Array(len)
1313
}
1414

15-
export const uint16BEEncode: LengthEncoderFunction = (value: number) => {
15+
export const uint16BEEncode = (value: number): Uint8Array => {
1616
const target = allocUnsafe(2)
1717
new DataView(target.buffer, target.byteOffset, target.byteLength).setUint16(0, value, false)
1818
return target

Diff for: src/noise.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { SecuredConnection } from '@libp2p/interface-connection-encrypter'
33
import { pbStream, ProtobufStream } from 'it-pb-stream'
44
import { duplexPair } from 'it-pair/duplex'
55
import { pipe } from 'it-pipe'
6-
import { encode, decode } from 'it-length-prefixed'
6+
import { decode } from 'it-length-prefixed'
77
import type { Duplex } from 'it-stream-types'
88
import type { bytes } from './@types/basic.js'
99
import type { IHandshake } from './@types/handshake-interface.js'
@@ -173,8 +173,7 @@ export class Noise implements INoiseConnection {
173173

174174
await pipe(
175175
secure, // write to wrapper
176-
encryptStream(handshake), // data is encrypted
177-
encode({ lengthEncoder: uint16BEEncode }), // prefix with message length
176+
encryptStream(handshake), // encrypt data + prefix with message length
178177
network, // send to the remote peer
179178
decode({ lengthDecoder: uint16BEDecode }), // read message length prefix
180179
decryptStream(handshake), // decrypt the incoming data

0 commit comments

Comments
 (0)