Extract decoding of size of payload size field

This commit is contained in:
Franck Royer 2022-05-19 19:00:29 +10:00
parent 0964425a12
commit 9e09de831f
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 9 additions and 5 deletions

View File

@ -43,7 +43,7 @@ export async function clearEncode(
// Calculate padding:
let rawSize =
FlagsLength +
getSizeOfPayloadSizeField(messagePayload) +
computeSizeOfPayloadSizeField(messagePayload) +
messagePayload.length;
if (sigPrivKey) {
@ -91,8 +91,7 @@ export function clearDecode(
let start = 1;
let sig;
const sizeOfPayloadSizeField = buf.readUIntLE(0, 1) & FlagMask;
const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message);
if (sizeOfPayloadSizeField === 0) return;
const payloadSize = buf.readUIntLE(start, sizeOfPayloadSizeField);
@ -110,6 +109,11 @@ export function clearDecode(
return { payload, sig };
}
function getSizeOfPayloadSizeField(message: Uint8Array): number {
const buf = Buffer.from(message);
return buf.readUIntLE(0, 1) & FlagMask;
}
/**
* 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].
@ -205,7 +209,7 @@ export function getPublicKey(privateKey: Uint8Array): Uint8Array {
* Computes the flags & auxiliary-field as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
*/
function addPayloadSizeField(msg: Uint8Array, payload: Uint8Array): Uint8Array {
const fieldSize = getSizeOfPayloadSizeField(payload);
const fieldSize = computeSizeOfPayloadSizeField(payload);
let field = new Uint8Array(4);
const fieldDataView = new DataView(field.buffer);
fieldDataView.setUint32(0, payload.length, true);
@ -218,7 +222,7 @@ function addPayloadSizeField(msg: Uint8Array, payload: Uint8Array): Uint8Array {
/**
* Returns the size of the auxiliary-field which in turns contains the payload size
*/
function getSizeOfPayloadSizeField(payload: Uint8Array): number {
function computeSizeOfPayloadSizeField(payload: Uint8Array): number {
let s = 1;
for (let i = payload.length; i >= 256; i /= 256) {
s++;