From c66927668bdbff5b96c43f3715af8216d56a407a Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:00:57 +1000 Subject: [PATCH 01/19] Run tsc as part of test script instead of building --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index fedceed501..ed95b1b485 100644 --- a/package.json +++ b/package.json @@ -42,10 +42,11 @@ "examples:pretest": "for d in examples/*/; do (cd $d && npm install); done", "nim-waku:build": "(cd nim-waku; NIMFLAGS=\"-d:chronicles_colors=off -d:chronicles_sinks=textlines -d:chronicles_log_level=TRACE\" make -j$(nproc --all 2>/dev/null || echo 2) wakunode2)", "nim-waku:force-build": "(cd nim-waku && rm -rf ./build/ ./vendor && make -j$(nproc --all 2>/dev/null || echo 2) update) && run-s nim-waku:build", - "test": "run-s build test:*", + "test": "run-s test:*", "test:lint": "eslint src --ext .ts", "test:prettier": "prettier \"src/**/*.ts\" \"./*.json\" \"*.conf.js\" \".github/**/*.yml\" --list-different", "test:spelling": "cspell \"{README.md,.github/*.md,guides/*.md,src/**/*.ts}\"", + "test:tsc": "tsc -p tsconfig.dev.json", "test:unit": "nyc --silent mocha", "test:karma": "karma start", "examples:test": "run-s examples:pretest; for d in examples/*/; do (cd $d && npm test;); done", From ab3b23f1003bc374d65f512873c9a6165a17c062 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:07:30 +1000 Subject: [PATCH 02/19] Remove Buffer from symmetric.ts --- src/lib/waku_message/symmetric.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lib/waku_message/symmetric.ts b/src/lib/waku_message/symmetric.ts index 9d2890a2a4..f6337fe56b 100644 --- a/src/lib/waku_message/symmetric.ts +++ b/src/lib/waku_message/symmetric.ts @@ -7,29 +7,29 @@ export const TagSize = 16; const Algorithm = { name: "AES-GCM", length: 128 }; export async function encrypt( - iv: Buffer | Uint8Array, - key: Buffer, - clearText: Buffer -): Promise { + iv: Uint8Array, + key: Uint8Array, + clearText: Uint8Array +): Promise { return getSubtle() .importKey("raw", key, Algorithm, false, ["encrypt"]) .then((cryptoKey) => getSubtle().encrypt({ iv, ...Algorithm }, cryptoKey, clearText) ) - .then(Buffer.from); + .then((cipher) => new Uint8Array(cipher)); } export async function decrypt( - iv: Buffer, - key: Buffer, - cipherText: Buffer -): Promise { + iv: Uint8Array, + key: Uint8Array, + cipherText: Uint8Array +): Promise { return getSubtle() .importKey("raw", key, Algorithm, false, ["decrypt"]) .then((cryptoKey) => getSubtle().decrypt({ iv, ...Algorithm }, cryptoKey, cipherText) ) - .then(Buffer.from); + .then((clear) => new Uint8Array(clear)); } export function generateIv(): Uint8Array { From 661c6227e738946ac1e8f80a1fb362a4d672c877 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:10:23 +1000 Subject: [PATCH 03/19] Move type definition to the top --- src/lib/waku_message/version_1.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index e24fb8fe68..3ca8362eab 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -17,6 +17,11 @@ const SignatureLength = 65; export const PrivateKeySize = 32; +export type Signature = { + signature: Uint8Array; + publicKey: Uint8Array | undefined; +}; + /** * Encode the payload pre-encryption. * @@ -76,11 +81,6 @@ export async function clearEncode( return { payload: envelope, sig }; } -export type Signature = { - signature: Uint8Array; - publicKey: Uint8Array | undefined; -}; - /** * Decode a decrypted payload. * From 564fee29ebee1b39a4a68f2758502faecffefd11 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:13:39 +1000 Subject: [PATCH 04/19] Remove `Buffer` from addPayloadSizeField --- src/lib/waku_message/version_1.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 3ca8362eab..9947dbd805 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -2,6 +2,7 @@ import { Buffer } from "buffer"; import * as secp from "@noble/secp256k1"; import { keccak256 } from "js-sha3"; +import { concat } from "uint8arrays/concat"; import { randomBytes } from "../crypto"; import { hexToBytes } from "../utils"; @@ -36,7 +37,7 @@ export async function clearEncode( sigPrivKey?: Uint8Array ): Promise<{ payload: Uint8Array; sig?: Signature }> { let envelope = Buffer.from([0]); // No flags - envelope = addPayloadSizeField(envelope, messagePayload); + envelope = Buffer.from(addPayloadSizeField(envelope, messagePayload)); envelope = Buffer.concat([envelope, Buffer.from(messagePayload)]); // 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/). */ -function addPayloadSizeField(msg: Buffer, payload: Uint8Array): Buffer { +function addPayloadSizeField(msg: Uint8Array, payload: Uint8Array): Uint8Array { const fieldSize = getSizeOfPayloadSizeField(payload); - let field = Buffer.alloc(4); - field.writeUInt32LE(payload.length, 0); + let field = new Uint8Array(4); + const fieldDataView = new DataView(field.buffer); + fieldDataView.setUint32(0, payload.length, true); field = field.slice(0, fieldSize); - msg = Buffer.concat([msg, field]); + msg = concat([msg, field]); msg[0] |= fieldSize; return msg; } From 6929805425ee65092556ddb1e4b713967b1f1153 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:16:56 +1000 Subject: [PATCH 05/19] Remove `Buffer` from `clearEncode` --- src/lib/waku_message/version_1.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 9947dbd805..acf54530de 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -36,9 +36,9 @@ export async function clearEncode( messagePayload: Uint8Array, sigPrivKey?: Uint8Array ): Promise<{ payload: Uint8Array; sig?: Signature }> { - let envelope = Buffer.from([0]); // No flags - envelope = Buffer.from(addPayloadSizeField(envelope, messagePayload)); - envelope = Buffer.concat([envelope, Buffer.from(messagePayload)]); + let envelope = new Uint8Array([0]); // No flags + envelope = addPayloadSizeField(envelope, messagePayload); + envelope = concat([envelope, messagePayload]); // Calculate padding: let rawSize = @@ -52,29 +52,26 @@ export async function clearEncode( const remainder = rawSize % PaddingTarget; const paddingSize = PaddingTarget - remainder; - const pad = Buffer.from(randomBytes(paddingSize)); + const pad = randomBytes(paddingSize); if (!validateDataIntegrity(pad, paddingSize)) { throw new Error("failed to generate random padding of size " + paddingSize); } - envelope = Buffer.concat([envelope, pad]); + envelope = concat([envelope, pad]); let sig; if (sigPrivKey) { envelope[0] |= IsSignedMask; const hash = keccak256(envelope); - const [signature, recid] = await secp.sign(hash, sigPrivKey, { + const [hexSignature, recid] = await secp.sign(hash, sigPrivKey, { recovered: true, der: false, }); - envelope = Buffer.concat([ - envelope, - hexToBytes(signature), - Buffer.from([recid]), - ]); + const bytesSignature = hexToBytes(hexSignature); + envelope = concat([envelope, bytesSignature, [recid]]); sig = { - signature: Buffer.from(signature), + signature: bytesSignature, publicKey: getPublicKey(sigPrivKey), }; } From 5d32877357e1bbe58fbc4fca1427be382554e594 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:20:04 +1000 Subject: [PATCH 06/19] Remove `Buffer` from `getSignature`, `getHash`, `getPublicKey` --- src/lib/waku_message/version_1.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index acf54530de..600316f5b4 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -103,7 +103,7 @@ export function clearDecode( if (isSigned) { const signature = getSignature(buf); const hash = getHash(buf, isSigned); - const publicKey = ecRecoverPubKey(hash, signature); + const publicKey = ecRecoverPubKey(hash, Buffer.from(signature)); sig = { signature, publicKey }; } @@ -202,7 +202,7 @@ export function generateSymmetricKey(): Uint8Array { * Return the public key for the given private key, to be used for asymmetric * encryption. */ -export function getPublicKey(privateKey: Uint8Array | Buffer): Uint8Array { +export function getPublicKey(privateKey: Uint8Array): Uint8Array { return secp.getPublicKey(privateKey, false); } @@ -244,11 +244,11 @@ function validateDataIntegrity( ); } -function getSignature(message: Buffer): Buffer { +function getSignature(message: Uint8Array): Uint8Array { return message.slice(message.length - SignatureLength, message.length); } -function getHash(message: Buffer, isSigned: boolean): string { +function getHash(message: Uint8Array, isSigned: boolean): string { if (isSigned) { return keccak256(message.slice(0, message.length - SignatureLength)); } From 181ba489be91bbe770601acdc144d184b46c2886 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:21:09 +1000 Subject: [PATCH 07/19] Remove `Buffer` from `encrypt*` --- src/lib/waku_message/version_1.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 600316f5b4..6407b2a00d 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -118,10 +118,10 @@ export function clearDecode( * @internal */ export async function encryptAsymmetric( - data: Uint8Array | Buffer, - publicKey: Uint8Array | Buffer | string + data: Uint8Array, + publicKey: Uint8Array | string ): Promise { - return ecies.encrypt(Buffer.from(hexToBytes(publicKey)), Buffer.from(data)); + return ecies.encrypt(hexToBytes(publicKey), data); } /** @@ -147,18 +147,14 @@ export async function decryptAsymmetric( * @internal */ export async function encryptSymmetric( - data: Uint8Array | Buffer, - key: Uint8Array | Buffer | string + data: Uint8Array, + key: Uint8Array | string ): Promise { const iv = symmetric.generateIv(); // Returns `cipher | tag` - const cipher = await symmetric.encrypt( - iv, - Buffer.from(hexToBytes(key)), - Buffer.from(data) - ); - return Buffer.concat([cipher, Buffer.from(iv)]); + const cipher = await symmetric.encrypt(iv, hexToBytes(key), data); + return concat([cipher, iv]); } /** From 76777744f0adbd4ceece9098d0ae6ad31653e139 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:22:04 +1000 Subject: [PATCH 08/19] Remove `Buffer` from `decrypt*` --- src/lib/waku_message/version_1.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 6407b2a00d..05c6a2a929 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -126,15 +126,15 @@ export async function encryptAsymmetric( /** * Proceed with Asymmetric decryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/). - * The return data is expect to be flags | payload-length | payload | [signature]. + * The returned data is expected to be `flags | payload-length | payload | [signature]`. * * @internal */ export async function decryptAsymmetric( - payload: Uint8Array | Buffer, - privKey: Uint8Array | Buffer + payload: Uint8Array, + privKey: Uint8Array ): Promise { - return ecies.decrypt(Buffer.from(privKey), Buffer.from(payload)); + return ecies.decrypt(privKey, payload); } /** @@ -167,15 +167,14 @@ export async function encryptSymmetric( * @internal */ export async function decryptSymmetric( - payload: Uint8Array | Buffer, - key: Uint8Array | Buffer | string + payload: Uint8Array, + key: Uint8Array | string ): Promise { - const data = Buffer.from(payload); - const ivStart = data.length - symmetric.IvSize; - const cipher = data.slice(0, ivStart); - const iv = data.slice(ivStart); + const ivStart = payload.length - symmetric.IvSize; + const cipher = payload.slice(0, ivStart); + const iv = payload.slice(ivStart); - return symmetric.decrypt(iv, Buffer.from(hexToBytes(key)), cipher); + return symmetric.decrypt(iv, hexToBytes(key), cipher); } /** From 64ea36faa34dc05082b9d0176daa5360c0983a3d Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:44:56 +1000 Subject: [PATCH 09/19] Remove `Buffer` from `ecRecoverPubKey` --- src/lib/waku_message/version_1.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 05c6a2a929..0926fb1d89 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -252,9 +252,12 @@ function getHash(message: Uint8Array, isSigned: boolean): string { function ecRecoverPubKey( messageHash: string, - signature: Buffer + signature: Uint8Array ): Uint8Array | undefined { - const recovery = signature.slice(64).readIntBE(0, 1); + // TODO: This is only needed because we are getting a `Buffer` instance. + signature = new Uint8Array(signature); + const recoveryDataView = new DataView(signature.slice(64).buffer); + const recovery = recoveryDataView.getUint8(0); const _signature = secp.Signature.fromCompact(signature.slice(0, 64)); return secp.recoverPublicKey( From 0964425a12609902590ab652ac10293e1e68feca Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 16:47:27 +1000 Subject: [PATCH 10/19] Remove `Buffer` from `validateDataIntegrity` --- src/lib/waku_message/version_1.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 0926fb1d89..47e7d16b07 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -234,9 +234,7 @@ function validateDataIntegrity( return false; } - return !( - expectedSize > 3 && Buffer.from(value).equals(Buffer.alloc(value.length)) - ); + return expectedSize <= 3 || value.findIndex((i) => i !== 0) !== -1; } function getSignature(message: Uint8Array): Uint8Array { From 9e09de831fc1e9b62b9169773d05bec0506cf670 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 19:00:29 +1000 Subject: [PATCH 11/19] Extract decoding of size of payload size field --- src/lib/waku_message/version_1.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 47e7d16b07..8a3a4b7302 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -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++; From 95485f0a6999fccc78d4ca211489a7350ec992bc Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 19:51:51 +1000 Subject: [PATCH 12/19] Remove `Buffer` from `getSizeOfPayloadSizeField` --- src/lib/waku_message/version_1.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 8a3a4b7302..5a77c7f930 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -110,8 +110,8 @@ export function clearDecode( } function getSizeOfPayloadSizeField(message: Uint8Array): number { - const buf = Buffer.from(message); - return buf.readUIntLE(0, 1) & FlagMask; + const messageDataView = new DataView(message.buffer); + return messageDataView.getUint8(0) & FlagMask; } /** From 33f52ef56d6c606be32f69b6cf7aa0215611a8fa Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 19 May 2022 19:54:16 +1000 Subject: [PATCH 13/19] Extract `getPayloadSize` logic --- src/lib/waku_message/version_1.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 5a77c7f930..a43378e826 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -94,7 +94,7 @@ export function clearDecode( const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message); if (sizeOfPayloadSizeField === 0) return; - const payloadSize = buf.readUIntLE(start, sizeOfPayloadSizeField); + const payloadSize = getPayloadSize(message, sizeOfPayloadSizeField); start += sizeOfPayloadSizeField; const payload = buf.slice(start, start + payloadSize); @@ -114,6 +114,14 @@ function getSizeOfPayloadSizeField(message: Uint8Array): number { return messageDataView.getUint8(0) & FlagMask; } +function getPayloadSize( + message: Uint8Array, + sizeOfPayloadSizeField: number +): number { + const buf = Buffer.from(message); + return buf.readUIntLE(1, sizeOfPayloadSizeField); +} + /** * 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]. From bd9d592fdac246585b059a9624d8c3b441cad2ff Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:13:13 +1000 Subject: [PATCH 14/19] 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 */ From 5648f72d3d7cc81554e07fc6c01f091138879892 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:18:09 +1000 Subject: [PATCH 15/19] Extract is message signed logic --- src/lib/waku_message/version_1.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 6ba8f0f388..f497e4bcbe 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -98,7 +98,7 @@ export function clearDecode( start += sizeOfPayloadSizeField; const payload = buf.slice(start, start + payloadSize); - const isSigned = (buf.readUIntLE(0, 1) & IsSignedMask) == IsSignedMask; + const isSigned = isMessageSigned(buf); if (isSigned) { const signature = getSignature(buf); const hash = getHash(buf, isSigned); @@ -131,6 +131,10 @@ function getPayloadSize( return payloadSizeDataView.getInt32(0, true); } +function isMessageSigned(message: Buffer): boolean { + return (message.readUIntLE(0, 1) & IsSignedMask) == IsSignedMask; +} + /** * 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]. From ce0c5cef0743027849fc6d699ab10d69ead83352 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:20:06 +1000 Subject: [PATCH 16/19] Pass Uint8Array when possible --- src/lib/waku_message/version_1.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index f497e4bcbe..c8d589ee58 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -100,9 +100,9 @@ export function clearDecode( const isSigned = isMessageSigned(buf); if (isSigned) { - const signature = getSignature(buf); - const hash = getHash(buf, isSigned); - const publicKey = ecRecoverPubKey(hash, Buffer.from(signature)); + const signature = getSignature(message); + const hash = getHash(message, isSigned); + const publicKey = ecRecoverPubKey(hash, signature); sig = { signature, publicKey }; } @@ -277,8 +277,6 @@ function ecRecoverPubKey( messageHash: string, signature: Uint8Array ): Uint8Array | undefined { - // TODO: This is only needed because we are getting a `Buffer` instance. - signature = new Uint8Array(signature); const recoveryDataView = new DataView(signature.slice(64).buffer); const recovery = recoveryDataView.getUint8(0); const _signature = secp.Signature.fromCompact(signature.slice(0, 64)); From 06b4ab581d58b65b9a49da7a550f81819db404f4 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:26:06 +1000 Subject: [PATCH 17/19] Remove `Buffer` from `isMessageSigned` --- src/lib/waku_message/version_1.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index c8d589ee58..aa533e6e96 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -98,7 +98,7 @@ export function clearDecode( start += sizeOfPayloadSizeField; const payload = buf.slice(start, start + payloadSize); - const isSigned = isMessageSigned(buf); + const isSigned = isMessageSigned(message); if (isSigned) { const signature = getSignature(message); const hash = getHash(message, isSigned); @@ -131,8 +131,9 @@ function getPayloadSize( return payloadSizeDataView.getInt32(0, true); } -function isMessageSigned(message: Buffer): boolean { - return (message.readUIntLE(0, 1) & IsSignedMask) == IsSignedMask; +function isMessageSigned(message: Uint8Array): boolean { + const messageDataView = new DataView(message.buffer); + return (messageDataView.getUint8(0) & IsSignedMask) == IsSignedMask; } /** From 89de2e3c775e00f4448a1d583ecc9941f6df471a Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:28:05 +1000 Subject: [PATCH 18/19] Remove `buf` variable --- src/lib/waku_message/version_1.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index aa533e6e96..3a3bc82e45 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -87,18 +87,16 @@ export async function clearEncode( export function clearDecode( message: Uint8Array | Buffer ): { payload: Uint8Array; sig?: Signature } | undefined { - const buf = Buffer.from(message); - let start = 1; - let sig; - const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message); if (sizeOfPayloadSizeField === 0) return; const payloadSize = getPayloadSize(message, sizeOfPayloadSizeField); - start += sizeOfPayloadSizeField; - const payload = buf.slice(start, start + payloadSize); + const payloadStart = 1 + sizeOfPayloadSizeField; + const payload = message.slice(payloadStart, payloadStart + payloadSize); const isSigned = isMessageSigned(message); + + let sig; if (isSigned) { const signature = getSignature(message); const hash = getHash(message, isSigned); From dbdf37a31dd7ae6d12334a812963a99af8651251 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 10:29:49 +1000 Subject: [PATCH 19/19] Remove usage of `Buffer` from version_1.ts --- src/lib/waku_message/version_1.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 3a3bc82e45..faddc2c4de 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -1,5 +1,3 @@ -import { Buffer } from "buffer"; - import * as secp from "@noble/secp256k1"; import { keccak256 } from "js-sha3"; import { concat } from "uint8arrays/concat"; @@ -85,7 +83,7 @@ export async function clearEncode( * @internal */ export function clearDecode( - message: Uint8Array | Buffer + message: Uint8Array ): { payload: Uint8Array; sig?: Signature } | undefined { const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message); if (sizeOfPayloadSizeField === 0) return;