From 280058de4ab97f6ee3f45fddc28db0524ffb64b3 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 12:46:51 +1000 Subject: [PATCH 01/11] test: deep.equal crashes when comparing Buffer and UInt8Array --- src/lib/enr/enr.spec.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/enr/enr.spec.ts b/src/lib/enr/enr.spec.ts index 4fb08b2cdb..5d5c096443 100644 --- a/src/lib/enr/enr.spec.ts +++ b/src/lib/enr/enr.spec.ts @@ -1,6 +1,7 @@ import { createSecp256k1PeerId } from "@libp2p/peer-id-factory"; import { Multiaddr } from "@multiformats/multiaddr"; import { assert, expect } from "chai"; +import { equals } from "uint8arrays/equals"; import { getPublicKey } from "../crypto"; import { bytesToHex, hexToBytes, utf8ToBytes } from "../utils"; @@ -211,8 +212,12 @@ describe("ENR", function () { }); it("should encode/decode to RLP encoding", async function () { - const decoded = await ENR.decode(await record.encode(privateKey)); - expect(decoded).to.deep.equal(record); + const encoded = await record.encode(privateKey); + const decoded = await ENR.decode(encoded); + + record.forEach((value, key) => { + expect(equals(decoded.get(key)!, value)).to.be.true; + }); }); it("should encode/decode to text encoding", async function () { From 931028e0ac5c0ee477efead75ef5d67b06796a05 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 15:25:37 +1000 Subject: [PATCH 02/11] build: bump ts-node to 10.9.1 and ts-loader to 9.3.1 --- .mocharc.json | 1 + package-lock.json | 100 +++++++++++++-------- package.json | 6 +- src/lib/peer_discovery_dns/dns.spec.ts | 2 +- src/lib/peer_discovery_dns/enrtree.spec.ts | 2 +- tsconfig.dev.json | 1 + 6 files changed, 68 insertions(+), 44 deletions(-) diff --git a/.mocharc.json b/.mocharc.json index ab845eb766..db9dd73531 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -2,6 +2,7 @@ "extension": ["ts"], "spec": "src/**/*.spec.ts", "require": ["ts-node/register", "isomorphic-fetch", "jsdom-global/register"], + "loader": "ts-node/esm", "node-option": [ "experimental-specifier-resolution=node", "loader=ts-node/esm" diff --git a/package-lock.json b/package-lock.json index fd9cf274d0..321a691b06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -84,8 +84,8 @@ "rollup": "^2.75.0", "size-limit": "^8.0.0", "tail": "^2.2.0", - "ts-loader": "^9.2.6", - "ts-node": "^10.4.0", + "ts-loader": "^9.3.1", + "ts-node": "^10.9.1", "typedoc": "^0.22.10", "typedoc-plugin-no-inherit": "^1.3.1", "typescript": "^4.5.5" @@ -685,27 +685,28 @@ "integrity": "sha512-n9So2C2Zw+uSDRzb2h9wq3PjZBqoHx+vBvu6a34H2qpumNjZ6HaEronrzX5tXJJXzOtocIQYrLxdd128TAU3+g==", "dev": true }, - "node_modules/@cspotcode/source-map-consumer": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz", - "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==", - "dev": true, - "engines": { - "node": ">= 12" - } - }, "node_modules/@cspotcode/source-map-support": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz", - "integrity": "sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==", + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", "dev": true, "dependencies": { - "@cspotcode/source-map-consumer": "0.8.0" + "@jridgewell/trace-mapping": "0.3.9" }, "engines": { "node": ">=12" } }, + "node_modules/@cspotcode/source-map-support/node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, "node_modules/@eslint/eslintrc": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.0.5.tgz", @@ -11097,9 +11098,9 @@ } }, "node_modules/ts-loader": { - "version": "9.2.8", - "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.2.8.tgz", - "integrity": "sha512-gxSak7IHUuRtwKf3FIPSW1VpZcqF9+MBrHOvBp9cjHh+525SjtCIJKVGjRKIAfxBwDGDGCFF00rTfzB1quxdSw==", + "version": "9.3.1", + "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.3.1.tgz", + "integrity": "sha512-OkyShkcZTsTwyS3Kt7a4rsT/t2qvEVQuKCTg4LJmpj9fhFR7ukGdZwV6Qq3tRUkqcXtfGpPR7+hFKHCG/0d3Lw==", "dev": true, "dependencies": { "chalk": "^4.1.0", @@ -11116,12 +11117,12 @@ } }, "node_modules/ts-node": { - "version": "10.4.0", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.4.0.tgz", - "integrity": "sha512-g0FlPvvCXSIO1JDF6S232P5jPYqBkRL9qly81ZgAOSU7rwI0stphCgd2kLiCrU9DjQCrJMWEqcNSjQL02s6d8A==", + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", "dev": true, "dependencies": { - "@cspotcode/source-map-support": "0.7.0", + "@cspotcode/source-map-support": "^0.8.0", "@tsconfig/node10": "^1.0.7", "@tsconfig/node12": "^1.0.7", "@tsconfig/node14": "^1.0.0", @@ -11132,11 +11133,13 @@ "create-require": "^1.1.0", "diff": "^4.0.1", "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", "yn": "3.1.1" }, "bin": { "ts-node": "dist/bin.js", "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", "ts-node-script": "dist/bin-script.js", "ts-node-transpile-only": "dist/bin-transpile.js", "ts-script": "dist/bin-script-deprecated.js" @@ -11528,6 +11531,12 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, "node_modules/validate-npm-package-license": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", @@ -12595,19 +12604,25 @@ "integrity": "sha512-n9So2C2Zw+uSDRzb2h9wq3PjZBqoHx+vBvu6a34H2qpumNjZ6HaEronrzX5tXJJXzOtocIQYrLxdd128TAU3+g==", "dev": true }, - "@cspotcode/source-map-consumer": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz", - "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==", - "dev": true - }, "@cspotcode/source-map-support": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz", - "integrity": "sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==", + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", "dev": true, "requires": { - "@cspotcode/source-map-consumer": "0.8.0" + "@jridgewell/trace-mapping": "0.3.9" + }, + "dependencies": { + "@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "requires": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + } } }, "@eslint/eslintrc": { @@ -20463,9 +20478,9 @@ } }, "ts-loader": { - "version": "9.2.8", - "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.2.8.tgz", - "integrity": "sha512-gxSak7IHUuRtwKf3FIPSW1VpZcqF9+MBrHOvBp9cjHh+525SjtCIJKVGjRKIAfxBwDGDGCFF00rTfzB1quxdSw==", + "version": "9.3.1", + "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.3.1.tgz", + "integrity": "sha512-OkyShkcZTsTwyS3Kt7a4rsT/t2qvEVQuKCTg4LJmpj9fhFR7ukGdZwV6Qq3tRUkqcXtfGpPR7+hFKHCG/0d3Lw==", "dev": true, "requires": { "chalk": "^4.1.0", @@ -20475,12 +20490,12 @@ } }, "ts-node": { - "version": "10.4.0", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.4.0.tgz", - "integrity": "sha512-g0FlPvvCXSIO1JDF6S232P5jPYqBkRL9qly81ZgAOSU7rwI0stphCgd2kLiCrU9DjQCrJMWEqcNSjQL02s6d8A==", + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", "dev": true, "requires": { - "@cspotcode/source-map-support": "0.7.0", + "@cspotcode/source-map-support": "^0.8.0", "@tsconfig/node10": "^1.0.7", "@tsconfig/node12": "^1.0.7", "@tsconfig/node14": "^1.0.0", @@ -20491,6 +20506,7 @@ "create-require": "^1.1.0", "diff": "^4.0.1", "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", "yn": "3.1.1" }, "dependencies": { @@ -20770,6 +20786,12 @@ "integrity": "sha512-l8lCEmLcLYZh4nbunNZvQCJc5pv7+RCwa8q/LdUx8u7lsWvPDKmpodJAJNwkAhJC//dFY48KuIEmjtd4RViDrA==", "dev": true }, + "v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, "validate-npm-package-license": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz", diff --git a/package.json b/package.json index ba8cbbeb45..5526607a70 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "test:prettier": "prettier \"src/**/*.ts\" \"./*.json\" \"*.*js\" \".github/**/*.yml\" --list-different", "test:spelling": "cspell \"{README.md,.github/*.md,guides/*.md,src/**/*.ts}\"", "test:tsc": "tsc -p tsconfig.dev.json", - "test:node": "mocha", + "test:node": "TS_NODE_PROJECT=./tsconfig.dev.json mocha", "test:browser": "karma start karma.conf.cjs", "examples:test": "run-s examples:pretest; for d in examples/*/; do (cd $d && npm test;); done", "proto": "rimraf src/proto/*.ts; protons src/proto/*.proto", @@ -160,8 +160,8 @@ "rollup": "^2.75.0", "size-limit": "^8.0.0", "tail": "^2.2.0", - "ts-loader": "^9.2.6", - "ts-node": "^10.4.0", + "ts-loader": "^9.3.1", + "ts-node": "^10.9.1", "typedoc": "^0.22.10", "typedoc-plugin-no-inherit": "^1.3.1", "typescript": "^4.5.5" diff --git a/src/lib/peer_discovery_dns/dns.spec.ts b/src/lib/peer_discovery_dns/dns.spec.ts index 93c28cf10b..2c22b50194 100644 --- a/src/lib/peer_discovery_dns/dns.spec.ts +++ b/src/lib/peer_discovery_dns/dns.spec.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import { DnsClient, DnsNodeDiscovery } from "./dns"; -import testData from "./testdata.json"; +import testData from "./testdata.json" assert { type: "json" }; const mockData = testData.dns; diff --git a/src/lib/peer_discovery_dns/enrtree.spec.ts b/src/lib/peer_discovery_dns/enrtree.spec.ts index 9ca58f32d2..55b8062d2d 100644 --- a/src/lib/peer_discovery_dns/enrtree.spec.ts +++ b/src/lib/peer_discovery_dns/enrtree.spec.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import { ENRTree } from "./enrtree"; -import testData from "./testdata.json"; +import testData from "./testdata.json" assert { type: "json" }; const dns = testData.dns; diff --git a/tsconfig.dev.json b/tsconfig.dev.json index 44c5c662c1..ffc27ef6ba 100644 --- a/tsconfig.dev.json +++ b/tsconfig.dev.json @@ -1,6 +1,7 @@ { "extends": "./tsconfig", "compilerOptions": { + "module": "esnext", "noEmit": true }, "exclude": [] From b98488899392c117da9e824ad8952839bbe5ef07 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 15:56:12 +1000 Subject: [PATCH 03/11] build: add missing exports map entries --- rollup.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rollup.config.js b/rollup.config.js index 0aee5966b2..fd6b347c40 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -7,6 +7,8 @@ export default { index: "dist/index.js", "lib/create_waku": "dist/lib/create_waku.js", "lib/peer_discovery_dns": "dist/lib/peer_discovery_dns/index.js", + "lib/peer_discovery_static_list": "dist/lib/peer_discovery_static_list.js", + "lib/predefined_bootstrap_nodes": "dist/lib/predefined_bootstrap_nodes.js" }, output: { dir: "bundle", From f4da5a8266527d43483adae73727c1492f038cb2 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 16:10:13 +1000 Subject: [PATCH 04/11] test: tsconfig file for karma with esnext enabled --- karma.conf.cjs | 6 +++++- tsconfig.karma.json | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tsconfig.karma.json diff --git a/karma.conf.cjs b/karma.conf.cjs index 192cea1200..d72f88378a 100644 --- a/karma.conf.cjs +++ b/karma.conf.cjs @@ -23,7 +23,10 @@ module.exports = function (config) { rules: [ { test: /\.(js|tsx?)$/, - use: "ts-loader", + use: [{ + loader: 'ts-loader', + options: { configFile: 'tsconfig.karma.json' } + }], exclude: /(node_modules)|(node\.spec\.ts)/, }, { @@ -44,6 +47,7 @@ module.exports = function (config) { extensions: [".ts", ".js"], }, stats: { warnings: false }, + devtool: 'inline-source-map' }, }); }; diff --git a/tsconfig.karma.json b/tsconfig.karma.json new file mode 100644 index 0000000000..c540c783ff --- /dev/null +++ b/tsconfig.karma.json @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.dev", + "compilerOptions": { + "noEmit": false + } +} From cfece62e2af935cca00d71a339bb2f28e931a10b Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 16:14:43 +1000 Subject: [PATCH 05/11] misc: enable lint-staged for config cjs files --- karma.conf.cjs | 12 +++++++----- package.json | 2 +- rollup.config.js | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/karma.conf.cjs b/karma.conf.cjs index d72f88378a..febf1611b4 100644 --- a/karma.conf.cjs +++ b/karma.conf.cjs @@ -23,10 +23,12 @@ module.exports = function (config) { rules: [ { test: /\.(js|tsx?)$/, - use: [{ - loader: 'ts-loader', - options: { configFile: 'tsconfig.karma.json' } - }], + use: [ + { + loader: "ts-loader", + options: { configFile: "tsconfig.karma.json" }, + }, + ], exclude: /(node_modules)|(node\.spec\.ts)/, }, { @@ -47,7 +49,7 @@ module.exports = function (config) { extensions: [".ts", ".js"], }, stats: { warnings: false }, - devtool: 'inline-source-map' + devtool: "inline-source-map", }, }); }; diff --git a/package.json b/package.json index 5526607a70..a3df636c0f 100644 --- a/package.json +++ b/package.json @@ -186,7 +186,7 @@ "*.ts": [ "eslint --fix" ], - "*.{ts,json,conf.js}": [ + "*.{ts,json,conf*.*js}": [ "prettier --write" ] } diff --git a/rollup.config.js b/rollup.config.js index fd6b347c40..2f62f9ed77 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -8,7 +8,7 @@ export default { "lib/create_waku": "dist/lib/create_waku.js", "lib/peer_discovery_dns": "dist/lib/peer_discovery_dns/index.js", "lib/peer_discovery_static_list": "dist/lib/peer_discovery_static_list.js", - "lib/predefined_bootstrap_nodes": "dist/lib/predefined_bootstrap_nodes.js" + "lib/predefined_bootstrap_nodes": "dist/lib/predefined_bootstrap_nodes.js", }, output: { dir: "bundle", From 5ddca918ca467f0b6fd3426a8c35f3d6b76617b6 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 17:34:37 +1000 Subject: [PATCH 06/11] fix: stop store pagination if `response` field is missing --- src/lib/waku_store/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index 4a67dc70ee..317097aa55 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -217,6 +217,7 @@ export class WakuStore { if (!reply.response) { dbg("No message returned from store: `response` field missing"); + return messages; } const response = reply.response as protoV2Beta4.HistoryResponse; From 79146711c53ba99bfcdd7888bada4c67f952e467 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 17:51:06 +1000 Subject: [PATCH 07/11] fix: ensure the content topics that needs to be decrypted are passed --- src/lib/waku_message/index.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/lib/waku_message/index.ts b/src/lib/waku_message/index.ts index 804e395353..a33b5cd155 100644 --- a/src/lib/waku_message/index.ts +++ b/src/lib/waku_message/index.ts @@ -38,6 +38,13 @@ export interface Options { sigPrivKey?: Uint8Array; } +// TODO: Use this in Options +export interface DecryptionParams { + key: Uint8Array; + method?: DecryptionMethod; + contentTopics?: string[]; +} + export class WakuMessage { private constructor( public proto: proto.WakuMessage, @@ -124,33 +131,25 @@ export class WakuMessage { */ static async decode( bytes: Uint8Array, - decryptionKeys?: Array<{ - key: Uint8Array; - method?: DecryptionMethod; - contentTopic?: string[]; - }> + decryptionParams?: DecryptionParams[] ): Promise { const protoBuf = proto.WakuMessage.decode(bytes); - return WakuMessage.decodeProto(protoBuf, decryptionKeys); + return WakuMessage.decodeProto(protoBuf, decryptionParams); } /** * Decode and decrypt Waku Message Protobuf Object into Waku Message. * * @params protoBuf The message to decode and decrypt. - * @params decryptionKeys If the payload is encrypted (version = 1), then the + * @params decryptionParams If the payload is encrypted (version = 1), then the * keys are used to attempt decryption of the message. The passed key can either * be asymmetric private keys or symmetric keys, both method are tried for each * key until the message is decrypted or combinations are run out. */ static async decodeProto( protoBuf: proto.WakuMessage, - decryptionKeys?: Array<{ - key: Uint8Array; - method?: DecryptionMethod; - contentTopics?: string[]; - }> + decryptionParams?: DecryptionParams[] ): Promise { if (protoBuf.payload === undefined) { dbg("Payload is undefined"); @@ -161,14 +160,14 @@ export class WakuMessage { let signaturePublicKey; let signature; if (protoBuf.version === 1 && protoBuf.payload) { - if (decryptionKeys === undefined) { + if (decryptionParams === undefined) { dbg("Payload is encrypted but no private keys have been provided."); return; } // Returns a bunch of `undefined` and hopefully one decrypted result const allResults = await Promise.all( - decryptionKeys.map(async ({ key, method, contentTopics }) => { + decryptionParams.map(async ({ key, method, contentTopics }) => { if ( !contentTopics || (protoBuf.contentTopic && From 863f8ee7a488fb0fe0cd1356ac674aefd1d90384 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 18:42:48 +1000 Subject: [PATCH 08/11] fix: do not use uint8arrays/concat as it returns `Buffer` in NodeJS `Buffer` does not have the same behaviour as `Uint8Array` when creating a `DataView`. This is a problem when manipulating byte for encoding and decoding purposes. --- src/lib/crypto.ts | 9 ++++++--- src/lib/utils.ts | 19 +++++++++++++++++++ src/lib/waku_light_push/index.ts | 2 +- src/lib/waku_message/ecies.ts | 3 +-- src/lib/waku_message/version_1.spec.ts | 12 ++++++++++++ src/lib/waku_message/version_1.ts | 4 +--- src/lib/waku_store/index.ts | 3 +-- 7 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index af21699d7f..321574606d 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -2,8 +2,8 @@ import nodeCrypto from "crypto"; import * as secp from "@noble/secp256k1"; import sha3 from "js-sha3"; -import { concat } from "uint8arrays/concat"; +import { concat } from "./utils"; import { Asymmetric, Symmetric } from "./waku_message/constants"; declare const self: Record | undefined; @@ -65,7 +65,10 @@ export async function sign( recovered: true, der: false, }); - return concat([signature, [recoveryId]], signature.length + 1); + return concat( + [signature, new Uint8Array([recoveryId])], + signature.length + 1 + ); } export function keccak256(input: Uint8Array): Uint8Array { @@ -74,7 +77,7 @@ export function keccak256(input: Uint8Array): Uint8Array { export function compressPublicKey(publicKey: Uint8Array): Uint8Array { if (publicKey.length === 64) { - publicKey = concat([[4], publicKey], 65); + publicKey = concat([new Uint8Array([4]), publicKey], 65); } const point = secp.Point.fromHex(publicKey); return point.toRawBytes(true); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 2d0ca74b11..6b9f0f343a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -29,3 +29,22 @@ export const bytesToUtf8 = (b: Uint8Array): string => toString(b, "utf8"); * Encode utf-8 string to byte array. */ export const utf8ToBytes = (s: string): Uint8Array => fromString(s, "utf8"); + +/** + * Concatenate using Uint8Arrays as `Buffer` has a different behavior with `DataView` + */ +export function concat( + byteArrays: Uint8Array[], + totalLength?: number +): Uint8Array { + const len = + totalLength ?? byteArrays.reduce((acc, curr) => acc + curr.length, 0); + const res = new Uint8Array(len); + + let offset = 0; + for (const bytes of byteArrays) { + res.set(bytes, offset); + offset += bytes.length; + } + return res; +} diff --git a/src/lib/waku_light_push/index.ts b/src/lib/waku_light_push/index.ts index b4fdf4fe35..2db473d525 100644 --- a/src/lib/waku_light_push/index.ts +++ b/src/lib/waku_light_push/index.ts @@ -4,11 +4,11 @@ import all from "it-all"; import * as lp from "it-length-prefixed"; import { pipe } from "it-pipe"; import { Libp2p } from "libp2p"; -import { concat } from "uint8arrays/concat"; import { PushResponse } from "../../proto/light_push"; import { DefaultPubSubTopic } from "../constants"; import { getPeersForProtocol, selectRandomPeer } from "../select_peer"; +import { concat } from "../utils"; import { WakuMessage } from "../waku_message"; import { PushRPC } from "./push_rpc"; diff --git a/src/lib/waku_message/ecies.ts b/src/lib/waku_message/ecies.ts index e05ac121df..eed121cc31 100644 --- a/src/lib/waku_message/ecies.ts +++ b/src/lib/waku_message/ecies.ts @@ -1,8 +1,7 @@ import * as secp from "@noble/secp256k1"; -import { concat } from "uint8arrays/concat"; import { getSubtle, randomBytes, sha256 } from "../crypto"; -import { hexToBytes } from "../utils"; +import { concat, hexToBytes } from "../utils"; /** * HKDF as implemented in go-ethereum. */ diff --git a/src/lib/waku_message/version_1.spec.ts b/src/lib/waku_message/version_1.spec.ts index 7351895a0b..89e937dbf6 100644 --- a/src/lib/waku_message/version_1.spec.ts +++ b/src/lib/waku_message/version_1.spec.ts @@ -2,6 +2,7 @@ import { expect } from "chai"; import fc from "fast-check"; import { getPublicKey } from "../crypto"; +import { bytesToHex } from "../utils"; import { clearDecode, @@ -72,4 +73,15 @@ describe("Waku Message Version 1", function () { ) ); }); + + it("Clear encode and decode", async function () { + await fc.assert( + fc.asyncProperty(fc.uint8Array(), async (payload) => { + const enc = await clearEncode(payload); + const dec = clearDecode(enc.payload); + if (!dec?.payload) throw "payload missing"; + expect(bytesToHex(dec?.payload)).to.eq(bytesToHex(payload)); + }) + ); + }); }); diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 886d011c4f..56b39ad795 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -1,8 +1,7 @@ import * as secp from "@noble/secp256k1"; -import { concat } from "uint8arrays/concat"; import { keccak256, randomBytes, sign } from "../crypto"; -import { hexToBytes } from "../utils"; +import { concat, hexToBytes } from "../utils"; import { Symmetric } from "./constants"; import * as ecies from "./ecies"; @@ -55,7 +54,6 @@ export async function clearEncode( } envelope = concat([envelope, pad]); - let sig; if (sigPrivKey) { envelope[0] |= IsSignedMask; diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index 317097aa55..3b88e4f97b 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -5,13 +5,12 @@ import all from "it-all"; import * as lp from "it-length-prefixed"; import { pipe } from "it-pipe"; import { Libp2p } from "libp2p"; -import { concat } from "uint8arrays/concat"; import * as protoV2Beta4 from "../../proto/store_v2beta4"; import { HistoryResponse } from "../../proto/store_v2beta4"; import { DefaultPubSubTopic, StoreCodecs } from "../constants"; import { getPeersForProtocol, selectRandomPeer } from "../select_peer"; -import { hexToBytes } from "../utils"; +import { concat, hexToBytes } from "../utils"; import { DecryptionMethod, WakuMessage } from "../waku_message"; import { HistoryRPC, PageDirection } from "./history_rpc"; From 5eec4564feb33bfbbdbbd824efffeb6b3caf16c3 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 18:43:00 +1000 Subject: [PATCH 09/11] style: typo --- src/lib/waku_message/version_1.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/waku_message/version_1.spec.ts b/src/lib/waku_message/version_1.spec.ts index 89e937dbf6..ddf756a19c 100644 --- a/src/lib/waku_message/version_1.spec.ts +++ b/src/lib/waku_message/version_1.spec.ts @@ -42,7 +42,7 @@ describe("Waku Message Version 1", function () { ); }); - it("Asymmetric encrypt & Decrypt", async function () { + it("Asymmetric encrypt & decrypt", async function () { await fc.assert( fc.asyncProperty( fc.uint8Array({ minLength: 1 }), From a58691aabd678ee4c4c192bd80af87d65281da58 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 19:04:31 +1000 Subject: [PATCH 10/11] refact: move default value to function signature --- src/test_utils/nwaku.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test_utils/nwaku.ts b/src/test_utils/nwaku.ts index e98044b4f8..2f70cce648 100644 --- a/src/test_utils/nwaku.ts +++ b/src/test_utils/nwaku.ts @@ -215,12 +215,12 @@ export class Nwaku { async sendMessage( message: WakuRelayMessage, - pubSubTopic?: string + pubSubTopic: string = DefaultPubSubTopic ): Promise { this.checkProcess(); return this.rpcCall("post_waku_v2_relay_v1_message", [ - pubSubTopic ? pubSubTopic : DefaultPubSubTopic, + pubSubTopic, message, ]); } From db22c2e86bcc9b72b28c95e93846eb776b61d2cd Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 24 Aug 2022 19:06:53 +1000 Subject: [PATCH 11/11] fix: set default value for version if not on the wire --- src/lib/waku_message/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib/waku_message/index.ts b/src/lib/waku_message/index.ts index a33b5cd155..29493e6000 100644 --- a/src/lib/waku_message/index.ts +++ b/src/lib/waku_message/index.ts @@ -273,8 +273,10 @@ export class WakuMessage { return this.proto.contentTopic; } - get version(): number | undefined { - return this.proto.version; + get version(): number { + // TODO: absent value should be replaced by default + // value of the type by the protobuf decoder + return this.proto.version ?? 0; } get timestamp(): Date | undefined {