From bd9d592fdac246585b059a9624d8c3b441cad2ff Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:13:13 +1000 Subject: [PATCH] Remove Buffer from `getPayloadSize` --- src/lib/waku_message/version_1.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index a43378e826..6ba8f0f388 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -118,14 +118,23 @@ function getPayloadSize( message: Uint8Array, sizeOfPayloadSizeField: number ): number { - const buf = Buffer.from(message); - return buf.readUIntLE(1, sizeOfPayloadSizeField); + let payloadSizeBytes = message.slice(1, 1 + sizeOfPayloadSizeField); + // int 32 == 4 bytes + if (sizeOfPayloadSizeField < 4) { + // If less than 4 bytes pad right (Little Endian). + payloadSizeBytes = concat( + [payloadSizeBytes, new Uint8Array(4 - sizeOfPayloadSizeField)], + 4 + ); + } + const payloadSizeDataView = new DataView(payloadSizeBytes.buffer); + return payloadSizeDataView.getInt32(0, true); } /** * 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]. - * The returned result can be set to `WakuMessage.payload`. + * The returned result can be set to `WakuMessage.payload`. * * @internal */