mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-17 06:47:29 +00:00
Remove Buffer
from clearEncode
This commit is contained in:
parent
564fee29eb
commit
6929805425
@ -36,9 +36,9 @@ export async function clearEncode(
|
|||||||
messagePayload: Uint8Array,
|
messagePayload: Uint8Array,
|
||||||
sigPrivKey?: Uint8Array
|
sigPrivKey?: Uint8Array
|
||||||
): Promise<{ payload: Uint8Array; sig?: Signature }> {
|
): Promise<{ payload: Uint8Array; sig?: Signature }> {
|
||||||
let envelope = Buffer.from([0]); // No flags
|
let envelope = new Uint8Array([0]); // No flags
|
||||||
envelope = Buffer.from(addPayloadSizeField(envelope, messagePayload));
|
envelope = addPayloadSizeField(envelope, messagePayload);
|
||||||
envelope = Buffer.concat([envelope, Buffer.from(messagePayload)]);
|
envelope = concat([envelope, messagePayload]);
|
||||||
|
|
||||||
// Calculate padding:
|
// Calculate padding:
|
||||||
let rawSize =
|
let rawSize =
|
||||||
@ -52,29 +52,26 @@ export async function clearEncode(
|
|||||||
|
|
||||||
const remainder = rawSize % PaddingTarget;
|
const remainder = rawSize % PaddingTarget;
|
||||||
const paddingSize = PaddingTarget - remainder;
|
const paddingSize = PaddingTarget - remainder;
|
||||||
const pad = Buffer.from(randomBytes(paddingSize));
|
const pad = randomBytes(paddingSize);
|
||||||
|
|
||||||
if (!validateDataIntegrity(pad, paddingSize)) {
|
if (!validateDataIntegrity(pad, paddingSize)) {
|
||||||
throw new Error("failed to generate random padding of size " + paddingSize);
|
throw new Error("failed to generate random padding of size " + paddingSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
envelope = Buffer.concat([envelope, pad]);
|
envelope = concat([envelope, pad]);
|
||||||
|
|
||||||
let sig;
|
let sig;
|
||||||
if (sigPrivKey) {
|
if (sigPrivKey) {
|
||||||
envelope[0] |= IsSignedMask;
|
envelope[0] |= IsSignedMask;
|
||||||
const hash = keccak256(envelope);
|
const hash = keccak256(envelope);
|
||||||
const [signature, recid] = await secp.sign(hash, sigPrivKey, {
|
const [hexSignature, recid] = await secp.sign(hash, sigPrivKey, {
|
||||||
recovered: true,
|
recovered: true,
|
||||||
der: false,
|
der: false,
|
||||||
});
|
});
|
||||||
envelope = Buffer.concat([
|
const bytesSignature = hexToBytes(hexSignature);
|
||||||
envelope,
|
envelope = concat([envelope, bytesSignature, [recid]]);
|
||||||
hexToBytes(signature),
|
|
||||||
Buffer.from([recid]),
|
|
||||||
]);
|
|
||||||
sig = {
|
sig = {
|
||||||
signature: Buffer.from(signature),
|
signature: bytesSignature,
|
||||||
publicKey: getPublicKey(sigPrivKey),
|
publicKey: getPublicKey(sigPrivKey),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user