mirror of
https://github.com/waku-org/js-waku.git
synced 2025-01-27 20:55:59 +00:00
Remove Buffer
from addPayloadSizeField
This commit is contained in:
parent
661c6227e7
commit
564fee29eb
@ -2,6 +2,7 @@ import { Buffer } from "buffer";
|
|||||||
|
|
||||||
import * as secp from "@noble/secp256k1";
|
import * as secp from "@noble/secp256k1";
|
||||||
import { keccak256 } from "js-sha3";
|
import { keccak256 } from "js-sha3";
|
||||||
|
import { concat } from "uint8arrays/concat";
|
||||||
|
|
||||||
import { randomBytes } from "../crypto";
|
import { randomBytes } from "../crypto";
|
||||||
import { hexToBytes } from "../utils";
|
import { hexToBytes } from "../utils";
|
||||||
@ -36,7 +37,7 @@ export async function clearEncode(
|
|||||||
sigPrivKey?: Uint8Array
|
sigPrivKey?: Uint8Array
|
||||||
): Promise<{ payload: Uint8Array; sig?: Signature }> {
|
): Promise<{ payload: Uint8Array; sig?: Signature }> {
|
||||||
let envelope = Buffer.from([0]); // No flags
|
let envelope = Buffer.from([0]); // No flags
|
||||||
envelope = addPayloadSizeField(envelope, messagePayload);
|
envelope = Buffer.from(addPayloadSizeField(envelope, messagePayload));
|
||||||
envelope = Buffer.concat([envelope, Buffer.from(messagePayload)]);
|
envelope = Buffer.concat([envelope, Buffer.from(messagePayload)]);
|
||||||
|
|
||||||
// Calculate padding:
|
// Calculate padding:
|
||||||
@ -211,12 +212,13 @@ export function getPublicKey(privateKey: Uint8Array | Buffer): 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: Buffer, payload: Uint8Array): Buffer {
|
function addPayloadSizeField(msg: Uint8Array, payload: Uint8Array): Uint8Array {
|
||||||
const fieldSize = getSizeOfPayloadSizeField(payload);
|
const fieldSize = getSizeOfPayloadSizeField(payload);
|
||||||
let field = Buffer.alloc(4);
|
let field = new Uint8Array(4);
|
||||||
field.writeUInt32LE(payload.length, 0);
|
const fieldDataView = new DataView(field.buffer);
|
||||||
|
fieldDataView.setUint32(0, payload.length, true);
|
||||||
field = field.slice(0, fieldSize);
|
field = field.slice(0, fieldSize);
|
||||||
msg = Buffer.concat([msg, field]);
|
msg = concat([msg, field]);
|
||||||
msg[0] |= fieldSize;
|
msg[0] |= fieldSize;
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user