Merge pull request #903 from status-im/run-full-suite

test: fix test suite
This commit is contained in:
fryorcraken.eth 2022-08-24 19:27:15 +10:00 committed by GitHub
commit b6612e9835
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 153 additions and 78 deletions

View File

@ -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"

View File

@ -23,7 +23,12 @@ 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 +49,7 @@ module.exports = function (config) {
extensions: [".ts", ".js"],
},
stats: { warnings: false },
devtool: "inline-source-map",
},
});
};

100
package-lock.json generated
View File

@ -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",

View File

@ -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"
@ -186,7 +186,7 @@
"*.ts": [
"eslint --fix"
],
"*.{ts,json,conf.js}": [
"*.{ts,json,conf*.*js}": [
"prettier --write"
]
}

View File

@ -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",

View File

@ -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<string, any> | 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);

View File

@ -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 () {

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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";

View File

@ -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.
*/

View File

@ -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<WakuMessage | undefined> {
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<WakuMessage | undefined> {
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 &&
@ -274,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 {

View File

@ -2,6 +2,7 @@ import { expect } from "chai";
import fc from "fast-check";
import { getPublicKey } from "../crypto";
import { bytesToHex } from "../utils";
import {
clearDecode,
@ -41,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 }),
@ -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));
})
);
});
});

View File

@ -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;

View File

@ -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";
@ -217,6 +216,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;

View File

@ -215,12 +215,12 @@ export class Nwaku {
async sendMessage(
message: WakuRelayMessage,
pubSubTopic?: string
pubSubTopic: string = DefaultPubSubTopic
): Promise<boolean> {
this.checkProcess();
return this.rpcCall<boolean>("post_waku_v2_relay_v1_message", [
pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
pubSubTopic,
message,
]);
}

View File

@ -1,6 +1,7 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"module": "esnext",
"noEmit": true
},
"exclude": []

6
tsconfig.karma.json Normal file
View File

@ -0,0 +1,6 @@
{
"extends": "./tsconfig.dev",
"compilerOptions": {
"noEmit": false
}
}