From 33f52ef56d6c606be32f69b6cf7aa0215611a8fa Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 19:54:16 +1000 Subject: [PATCH] Extract `getPayloadSize` logic --- src/lib/waku_message/version_1.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 5a77c7f930..a43378e826 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -94,7 +94,7 @@ export function clearDecode( const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message); if (sizeOfPayloadSizeField === 0) return; - const payloadSize = buf.readUIntLE(start, sizeOfPayloadSizeField); + const payloadSize = getPayloadSize(message, sizeOfPayloadSizeField); start += sizeOfPayloadSizeField; const payload = buf.slice(start, start + payloadSize); @@ -114,6 +114,14 @@ function getSizeOfPayloadSizeField(message: Uint8Array): number { return messageDataView.getUint8(0) & FlagMask; } +function getPayloadSize( + message: Uint8Array, + sizeOfPayloadSizeField: number +): number { + const buf = Buffer.from(message); + return buf.readUIntLE(1, sizeOfPayloadSizeField); +} + /** * Proceed with Asymmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/). * The data MUST be flags | payload-length | payload | [signature].