mirror of https://github.com/status-im/js-waku.git
Extract decoding of size of payload size field
This commit is contained in:
parent
0964425a12
commit
9e09de831f
|
@ -43,7 +43,7 @@ export async function clearEncode(
|
||||||
// Calculate padding:
|
// Calculate padding:
|
||||||
let rawSize =
|
let rawSize =
|
||||||
FlagsLength +
|
FlagsLength +
|
||||||
getSizeOfPayloadSizeField(messagePayload) +
|
computeSizeOfPayloadSizeField(messagePayload) +
|
||||||
messagePayload.length;
|
messagePayload.length;
|
||||||
|
|
||||||
if (sigPrivKey) {
|
if (sigPrivKey) {
|
||||||
|
@ -91,8 +91,7 @@ export function clearDecode(
|
||||||
let start = 1;
|
let start = 1;
|
||||||
let sig;
|
let sig;
|
||||||
|
|
||||||
const sizeOfPayloadSizeField = buf.readUIntLE(0, 1) & FlagMask;
|
const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message);
|
||||||
|
|
||||||
if (sizeOfPayloadSizeField === 0) return;
|
if (sizeOfPayloadSizeField === 0) return;
|
||||||
|
|
||||||
const payloadSize = buf.readUIntLE(start, sizeOfPayloadSizeField);
|
const payloadSize = buf.readUIntLE(start, sizeOfPayloadSizeField);
|
||||||
|
@ -110,6 +109,11 @@ export function clearDecode(
|
||||||
return { payload, sig };
|
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/).
|
* 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 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/).
|
* Computes the flags & auxiliary-field as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
||||||
*/
|
*/
|
||||||
function addPayloadSizeField(msg: Uint8Array, payload: Uint8Array): Uint8Array {
|
function addPayloadSizeField(msg: Uint8Array, payload: Uint8Array): Uint8Array {
|
||||||
const fieldSize = getSizeOfPayloadSizeField(payload);
|
const fieldSize = computeSizeOfPayloadSizeField(payload);
|
||||||
let field = new Uint8Array(4);
|
let field = new Uint8Array(4);
|
||||||
const fieldDataView = new DataView(field.buffer);
|
const fieldDataView = new DataView(field.buffer);
|
||||||
fieldDataView.setUint32(0, payload.length, true);
|
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
|
* 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;
|
let s = 1;
|
||||||
for (let i = payload.length; i >= 256; i /= 256) {
|
for (let i = payload.length; i >= 256; i /= 256) {
|
||||||
s++;
|
s++;
|
||||||
|
|
Loading…
Reference in New Issue