From 9ffedd1fbf7eac6a90986c4fed180f7cbb91d2cb Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 30 May 2022 15:01:57 +1000 Subject: [PATCH] Remove circular dependencies --- src/index.ts | 6 ++++-- src/lib/constants.ts | 9 +++++++++ src/lib/crypto.ts | 7 +++---- src/lib/discovery/bootstrap.ts | 4 ++-- src/lib/discovery/index.spec.ts | 3 +-- src/lib/discovery/index.ts | 13 ------------- src/lib/discovery/predefined.ts | 2 +- src/lib/discovery/random_subset.ts | 12 ++++++++++++ src/lib/waku.ts | 9 ++------- src/lib/waku_filter/index.ts | 2 +- src/lib/waku_light_push/index.ts | 2 +- src/lib/waku_message/constants.ts | 10 ++++++++++ src/lib/waku_message/symmetric.ts | 16 ++++++---------- src/lib/waku_message/version_1.ts | 5 ++--- src/lib/waku_relay/get_relay_peers.ts | 2 +- src/lib/waku_relay/index.node.spec.ts | 3 ++- src/lib/waku_relay/index.ts | 6 ++---- src/lib/waku_store/constants.ts | 4 ++++ src/lib/waku_store/history_rpc.ts | 2 +- src/lib/waku_store/index.ts | 9 ++------- src/test_utils/nwaku.ts | 2 +- 21 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 src/lib/constants.ts create mode 100644 src/lib/discovery/random_subset.ts create mode 100644 src/lib/waku_message/constants.ts create mode 100644 src/lib/waku_store/constants.ts diff --git a/src/index.ts b/src/index.ts index 443b9f40b4..5788df3c26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +export { DefaultPubSubTopic } from "./lib/constants"; + export { generatePrivateKey, generateSymmetricKey, @@ -12,7 +14,7 @@ export * as enr from "./lib/enr"; export * as utils from "./lib/utils"; export * as waku from "./lib/waku"; -export { Waku, DefaultPubSubTopic, Protocols } from "./lib/waku"; +export { Waku, Protocols } from "./lib/waku"; export * as waku_message from "./lib/waku_message"; export { WakuMessage } from "./lib/waku_message"; @@ -25,7 +27,7 @@ export { } from "./lib/waku_light_push"; export * as waku_relay from "./lib/waku_relay"; -export { WakuRelay, RelayCodecs } from "./lib/waku_relay"; +export { WakuRelay } from "./lib/waku_relay"; export * as waku_store from "./lib/waku_store"; export { PageDirection, WakuStore, StoreCodecs } from "./lib/waku_store"; diff --git a/src/lib/constants.ts b/src/lib/constants.ts new file mode 100644 index 0000000000..95a695a65c --- /dev/null +++ b/src/lib/constants.ts @@ -0,0 +1,9 @@ +/** + * DefaultPubSubTopic is the default gossipsub topic to use for Waku. + */ +export const DefaultPubSubTopic = "/waku/2/default-waku/proto"; + +export enum StoreCodecs { + V2Beta3 = "/vac/waku/store/2.0.0-beta3", + V2Beta4 = "/vac/waku/store/2.0.0-beta4", +} diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index 0eedbbe6e2..084dbc0318 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -4,8 +4,7 @@ import * as secp from "@noble/secp256k1"; import * as sha3 from "js-sha3"; import { concat } from "uint8arrays/concat"; -import * as symmetric from "./waku_message/symmetric"; -import { PrivateKeySize } from "./waku_message/version_1"; +import { Asymmetric, Symmetric } from "./waku_message/constants"; declare const self: Record | undefined; const crypto: { node?: any; web?: any } = { @@ -34,14 +33,14 @@ export const sha256 = secp.utils.sha256; * Use {@link getPublicKey} to get the corresponding Public Key. */ export function generatePrivateKey(): Uint8Array { - return randomBytes(PrivateKeySize); + return randomBytes(Asymmetric.keySize); } /** * Generate a new symmetric key to be used for symmetric encryption. */ export function generateSymmetricKey(): Uint8Array { - return randomBytes(symmetric.KeySize); + return randomBytes(Symmetric.keySize); } /** diff --git a/src/lib/discovery/bootstrap.ts b/src/lib/discovery/bootstrap.ts index 0e1b96657c..15eae9aeb9 100644 --- a/src/lib/discovery/bootstrap.ts +++ b/src/lib/discovery/bootstrap.ts @@ -2,8 +2,8 @@ import debug from "debug"; import { Multiaddr } from "multiaddr"; import { DnsNodeDiscovery, NodeCapabilityCount } from "./dns"; - -import { getPredefinedBootstrapNodes, getPseudoRandomSubset } from "./index"; +import { getPredefinedBootstrapNodes } from "./predefined"; +import { getPseudoRandomSubset } from "./random_subset"; const dbg = debug("waku:discovery:bootstrap"); diff --git a/src/lib/discovery/index.spec.ts b/src/lib/discovery/index.spec.ts index b9108ca99b..a0ee745f2b 100644 --- a/src/lib/discovery/index.spec.ts +++ b/src/lib/discovery/index.spec.ts @@ -1,8 +1,7 @@ import { expect } from "chai"; import { fleets } from "./predefined"; - -import { getPseudoRandomSubset } from "./index"; +import { getPseudoRandomSubset } from "./random_subset"; declare global { interface Window { diff --git a/src/lib/discovery/index.ts b/src/lib/discovery/index.ts index 84aaa2097c..e01421c2a2 100644 --- a/src/lib/discovery/index.ts +++ b/src/lib/discovery/index.ts @@ -1,19 +1,6 @@ -import { shuffle } from "libp2p-gossipsub/src/utils"; - export { getPredefinedBootstrapNodes } from "./predefined"; export * as predefined from "./predefined"; export { Bootstrap, BootstrapOptions } from "./bootstrap"; export * as dns from "./dns"; export { Endpoints, DnsOverHttps } from "./dns_over_https"; export { ENRTree, ENRTreeValues, ENRRootValues } from "./enrtree"; - -export function getPseudoRandomSubset( - values: T[], - wantedNumber: number -): T[] { - if (values.length <= wantedNumber) { - return values; - } - - return shuffle(values).slice(0, wantedNumber); -} diff --git a/src/lib/discovery/predefined.ts b/src/lib/discovery/predefined.ts index e3c5e2bdc6..1e90db8f96 100644 --- a/src/lib/discovery/predefined.ts +++ b/src/lib/discovery/predefined.ts @@ -1,6 +1,6 @@ import { Multiaddr } from "multiaddr"; -import { getPseudoRandomSubset } from "./index"; +import { getPseudoRandomSubset } from "./random_subset"; export const DefaultWantedNumber = 1; diff --git a/src/lib/discovery/random_subset.ts b/src/lib/discovery/random_subset.ts new file mode 100644 index 0000000000..be038946a4 --- /dev/null +++ b/src/lib/discovery/random_subset.ts @@ -0,0 +1,12 @@ +import { shuffle } from "libp2p-gossipsub/src/utils/index"; + +export function getPseudoRandomSubset( + values: T[], + wantedNumber: number +): T[] { + if (values.length <= wantedNumber) { + return values; + } + + return shuffle(values).slice(0, wantedNumber); +} diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 91577e484c..fd1e09493e 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -20,8 +20,8 @@ import { Bootstrap, BootstrapOptions } from "./discovery"; import { FilterCodec, WakuFilter } from "./waku_filter"; import { LightPushCodec, WakuLightPush } from "./waku_light_push"; import { DecryptionMethod, WakuMessage } from "./waku_message"; -import { RelayCodecs, WakuRelay } from "./waku_relay"; -import { RelayPingContentTopic } from "./waku_relay/constants"; +import { WakuRelay } from "./waku_relay"; +import { RelayCodecs, RelayPingContentTopic } from "./waku_relay/constants"; import { StoreCodecs, WakuStore } from "./waku_store"; const websocketsTransportKey = Websockets.prototype[Symbol.toStringTag]; @@ -29,11 +29,6 @@ const websocketsTransportKey = Websockets.prototype[Symbol.toStringTag]; export const DefaultPingKeepAliveValueSecs = 0; export const DefaultRelayKeepAliveValueSecs = 5 * 60; -/** - * DefaultPubSubTopic is the default gossipsub topic to use for Waku. - */ -export const DefaultPubSubTopic = "/waku/2/default-waku/proto"; - const dbg = debug("waku:waku"); export enum Protocols { diff --git a/src/lib/waku_filter/index.ts b/src/lib/waku_filter/index.ts index dbc09cc37e..6e7a2af37b 100644 --- a/src/lib/waku_filter/index.ts +++ b/src/lib/waku_filter/index.ts @@ -5,9 +5,9 @@ import Libp2p, { MuxedStream } from "libp2p"; import { Peer, PeerId } from "libp2p/src/peer-store"; import { WakuMessage as WakuMessageProto } from "../../proto/waku/v2/message"; +import { DefaultPubSubTopic } from "../constants"; import { getPeersForProtocol, selectRandomPeer } from "../select_peer"; import { hexToBytes } from "../utils"; -import { DefaultPubSubTopic } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; import { ContentFilter, FilterRPC } from "./filter_rpc"; diff --git a/src/lib/waku_light_push/index.ts b/src/lib/waku_light_push/index.ts index 5b8cca2c42..12ee971c0a 100644 --- a/src/lib/waku_light_push/index.ts +++ b/src/lib/waku_light_push/index.ts @@ -6,8 +6,8 @@ import { Peer } from "libp2p/src/peer-store"; import PeerId from "peer-id"; import { PushResponse } from "../../proto/waku/v2/light_push"; +import { DefaultPubSubTopic } from "../constants"; import { getPeersForProtocol, selectRandomPeer } from "../select_peer"; -import { DefaultPubSubTopic } from "../waku"; import { WakuMessage } from "../waku_message"; import { PushRPC } from "./push_rpc"; diff --git a/src/lib/waku_message/constants.ts b/src/lib/waku_message/constants.ts new file mode 100644 index 0000000000..9d2036458b --- /dev/null +++ b/src/lib/waku_message/constants.ts @@ -0,0 +1,10 @@ +export const Symmetric = { + keySize: 32, + ivSize: 12, + tagSize: 16, + algorithm: { name: "AES-GCM", length: 128 }, +}; + +export const Asymmetric = { + keySize: 32, +}; diff --git a/src/lib/waku_message/symmetric.ts b/src/lib/waku_message/symmetric.ts index f6337fe56b..c44192e406 100644 --- a/src/lib/waku_message/symmetric.ts +++ b/src/lib/waku_message/symmetric.ts @@ -1,10 +1,6 @@ import { getSubtle, randomBytes } from "../crypto"; -export const KeySize = 32; -export const IvSize = 12; -export const TagSize = 16; - -const Algorithm = { name: "AES-GCM", length: 128 }; +import { Symmetric } from "./constants"; export async function encrypt( iv: Uint8Array, @@ -12,9 +8,9 @@ export async function encrypt( clearText: Uint8Array ): Promise { return getSubtle() - .importKey("raw", key, Algorithm, false, ["encrypt"]) + .importKey("raw", key, Symmetric.algorithm, false, ["encrypt"]) .then((cryptoKey) => - getSubtle().encrypt({ iv, ...Algorithm }, cryptoKey, clearText) + getSubtle().encrypt({ iv, ...Symmetric.algorithm }, cryptoKey, clearText) ) .then((cipher) => new Uint8Array(cipher)); } @@ -25,13 +21,13 @@ export async function decrypt( cipherText: Uint8Array ): Promise { return getSubtle() - .importKey("raw", key, Algorithm, false, ["decrypt"]) + .importKey("raw", key, Symmetric.algorithm, false, ["decrypt"]) .then((cryptoKey) => - getSubtle().decrypt({ iv, ...Algorithm }, cryptoKey, cipherText) + getSubtle().decrypt({ iv, ...Symmetric.algorithm }, cryptoKey, cipherText) ) .then((clear) => new Uint8Array(clear)); } export function generateIv(): Uint8Array { - return randomBytes(IvSize); + return randomBytes(Symmetric.ivSize); } diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index 89e2c7e1c2..886d011c4f 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -4,6 +4,7 @@ import { concat } from "uint8arrays/concat"; import { keccak256, randomBytes, sign } from "../crypto"; import { hexToBytes } from "../utils"; +import { Symmetric } from "./constants"; import * as ecies from "./ecies"; import * as symmetric from "./symmetric"; @@ -13,8 +14,6 @@ const IsSignedMask = 4; // 0100 const PaddingTarget = 256; const SignatureLength = 65; -export const PrivateKeySize = 32; - export type Signature = { signature: Uint8Array; publicKey: Uint8Array | undefined; @@ -187,7 +186,7 @@ export async function decryptSymmetric( payload: Uint8Array, key: Uint8Array | string ): Promise { - const ivStart = payload.length - symmetric.IvSize; + const ivStart = payload.length - Symmetric.ivSize; const cipher = payload.slice(0, ivStart); const iv = payload.slice(ivStart); diff --git a/src/lib/waku_relay/get_relay_peers.ts b/src/lib/waku_relay/get_relay_peers.ts index 253ae92d5e..df03553dab 100644 --- a/src/lib/waku_relay/get_relay_peers.ts +++ b/src/lib/waku_relay/get_relay_peers.ts @@ -1,7 +1,7 @@ import Gossipsub from "libp2p-gossipsub"; import { shuffle } from "libp2p-gossipsub/src/utils"; -import { RelayCodecs } from "./index"; +import { RelayCodecs } from "./constants"; /** * Given a topic, returns up to count peers subscribed to that topic diff --git a/src/lib/waku_relay/index.node.spec.ts b/src/lib/waku_relay/index.node.spec.ts index a78bfb4a65..764de39cb2 100644 --- a/src/lib/waku_relay/index.node.spec.ts +++ b/src/lib/waku_relay/index.node.spec.ts @@ -8,12 +8,13 @@ import { Nwaku, } from "../../test_utils"; import { delay } from "../../test_utils/delay"; +import { DefaultPubSubTopic } from "../constants"; import { generatePrivateKey, generateSymmetricKey, getPublicKey, } from "../crypto"; -import { DefaultPubSubTopic, Protocols, Waku } from "../waku"; +import { Protocols, Waku } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; const log = debug("waku:test"); diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 13cea4fbb5..1c52277cf5 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -13,19 +13,17 @@ import { InMessage } from "libp2p-interfaces/src/pubsub"; import { SignaturePolicy } from "libp2p-interfaces/src/pubsub/signature-policy"; import PeerId from "peer-id"; +import { DefaultPubSubTopic } from "../constants"; import { hexToBytes } from "../utils"; -import { CreateOptions, DefaultPubSubTopic } from "../waku"; +import { CreateOptions } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; import * as constants from "./constants"; -import { RelayCodecs } from "./constants"; import { getRelayPeers } from "./get_relay_peers"; import { RelayHeartbeat } from "./relay_heartbeat"; const dbg = debug("waku:relay"); -export { RelayCodecs }; - /** * See constructor libp2p-gossipsub [API](https://github.com/ChainSafe/js-libp2p-gossipsub#api). */ diff --git a/src/lib/waku_store/constants.ts b/src/lib/waku_store/constants.ts new file mode 100644 index 0000000000..62d261583c --- /dev/null +++ b/src/lib/waku_store/constants.ts @@ -0,0 +1,4 @@ +export enum StoreCodecs { + V2Beta3 = "/vac/waku/store/2.0.0-beta3", + V2Beta4 = "/vac/waku/store/2.0.0-beta4", +} diff --git a/src/lib/waku_store/history_rpc.ts b/src/lib/waku_store/history_rpc.ts index 33633ba760..25b8bbab60 100644 --- a/src/lib/waku_store/history_rpc.ts +++ b/src/lib/waku_store/history_rpc.ts @@ -5,7 +5,7 @@ import { v4 as uuid } from "uuid"; import * as protoV2Beta3 from "../../proto/waku/v2/store/v2beta3/store"; import * as protoV2Beta4 from "../../proto/waku/v2/store/v2beta4/store"; -import { StoreCodecs } from "./index"; +import { StoreCodecs } from "./constants"; export enum PageDirection { BACKWARD = "backward", diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index 6d7110b49f..9b8b6afb49 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -6,23 +6,18 @@ import Libp2p from "libp2p"; import { Peer } from "libp2p/src/peer-store"; import PeerId from "peer-id"; +import { DefaultPubSubTopic, StoreCodecs } from "../constants"; import { getPeersForProtocol, selectRandomPeer } from "../select_peer"; import { hexToBytes } from "../utils"; -import { DefaultPubSubTopic } from "../waku"; import { DecryptionMethod, WakuMessage } from "../waku_message"; import { HistoryRPC, PageDirection } from "./history_rpc"; const dbg = debug("waku:store"); -export enum StoreCodecs { - V2Beta3 = "/vac/waku/store/2.0.0-beta3", - V2Beta4 = "/vac/waku/store/2.0.0-beta4", -} - export const DefaultPageSize = 10; -export { PageDirection }; +export { PageDirection, StoreCodecs }; export interface CreateOptions { /** diff --git a/src/test_utils/nwaku.ts b/src/test_utils/nwaku.ts index 21d91285ba..bca896fb63 100644 --- a/src/test_utils/nwaku.ts +++ b/src/test_utils/nwaku.ts @@ -11,8 +11,8 @@ import { Multiaddr, multiaddr } from "multiaddr"; import PeerId from "peer-id"; import portfinder from "portfinder"; +import { DefaultPubSubTopic } from "../lib/constants"; import { hexToBytes } from "../lib/utils"; -import { DefaultPubSubTopic } from "../lib/waku"; import { WakuMessage } from "../lib/waku_message"; import * as proto from "../proto/waku/v2/message";