diff --git a/packages/message-encryption/package.json b/packages/message-encryption/package.json index 22ff21bd38..c7ebfbad4f 100644 --- a/packages/message-encryption/package.json +++ b/packages/message-encryption/package.json @@ -8,6 +8,14 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" + }, + "./ecies": { + "types": "./dist/ecies.d.ts", + "import": "./dist/ecies.js" + }, + "./symmetric": { + "types": "./dist/symmetric.d.ts", + "import": "./dist/symmetric.js" } }, "type": "module", diff --git a/packages/message-encryption/rollup.config.js b/packages/message-encryption/rollup.config.js index d22d3d231e..f11e6b9c4c 100644 --- a/packages/message-encryption/rollup.config.js +++ b/packages/message-encryption/rollup.config.js @@ -5,6 +5,8 @@ import { nodeResolve } from "@rollup/plugin-node-resolve"; export default { input: { index: "dist/index.js", + ecies: "dist/ecies.js", + symmetric: "dist/symmetric.js", }, output: { dir: "bundle", diff --git a/packages/message-encryption/src/ecies.spec.ts b/packages/message-encryption/src/ecies.spec.ts index b9f75c03c5..0b1a30d25d 100644 --- a/packages/message-encryption/src/ecies.spec.ts +++ b/packages/message-encryption/src/ecies.spec.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import fc from "fast-check"; import { getPublicKey } from "./crypto/index.js"; -import { createAsymDecoder, createAsymEncoder } from "./ecies.js"; +import { createDecoder, createEncoder } from "./ecies.js"; const TestContentTopic = "/test/1/waku-message/utf8"; @@ -15,10 +15,10 @@ describe("Ecies Encryption", function () { async (payload, privateKey) => { const publicKey = getPublicKey(privateKey); - const encoder = createAsymEncoder(TestContentTopic, publicKey); + const encoder = createEncoder(TestContentTopic, publicKey); const bytes = await encoder.toWire({ payload }); - const decoder = createAsymDecoder(TestContentTopic, privateKey); + const decoder = createDecoder(TestContentTopic, privateKey); const protoResult = await decoder.fromWireToProtoObj(bytes!); if (!protoResult) throw "Failed to proto decode"; const result = await decoder.fromProtoObj(protoResult); @@ -46,14 +46,14 @@ describe("Ecies Encryption", function () { const alicePublicKey = getPublicKey(alicePrivateKey); const bobPublicKey = getPublicKey(bobPrivateKey); - const encoder = createAsymEncoder( + const encoder = createEncoder( TestContentTopic, bobPublicKey, alicePrivateKey ); const bytes = await encoder.toWire({ payload }); - const decoder = createAsymDecoder(TestContentTopic, bobPrivateKey); + const decoder = createDecoder(TestContentTopic, bobPrivateKey); const protoResult = await decoder.fromWireToProtoObj(bytes!); if (!protoResult) throw "Failed to proto decode"; const result = await decoder.fromProtoObj(protoResult); diff --git a/packages/message-encryption/src/ecies.ts b/packages/message-encryption/src/ecies.ts index f0075f99ba..65fee0f69d 100644 --- a/packages/message-encryption/src/ecies.ts +++ b/packages/message-encryption/src/ecies.ts @@ -17,11 +17,19 @@ import { preCipher, } from "./waku_payload.js"; -import { DecodedMessage, OneMillion, Version } from "./index.js"; +import { + DecodedMessage, + generatePrivateKey, + getPublicKey, + OneMillion, + Version, +} from "./index.js"; + +export { DecodedMessage, generatePrivateKey, getPublicKey }; const log = debug("waku:message-encryption:ecies"); -class AsymEncoder implements IEncoder { +class Encoder implements IEncoder { constructor( public contentTopic: string, private publicKey: Uint8Array, @@ -59,16 +67,16 @@ class AsymEncoder implements IEncoder { } } -export function createAsymEncoder( +export function createEncoder( contentTopic: string, publicKey: Uint8Array, sigPrivKey?: Uint8Array, ephemeral = false -): AsymEncoder { - return new AsymEncoder(contentTopic, publicKey, sigPrivKey, ephemeral); +): Encoder { + return new Encoder(contentTopic, publicKey, sigPrivKey, ephemeral); } -class AsymDecoder extends DecoderV0 implements IDecoder { +class Decoder extends DecoderV0 implements IDecoder { constructor(contentTopic: string, private privateKey: Uint8Array) { super(contentTopic); } @@ -126,9 +134,9 @@ class AsymDecoder extends DecoderV0 implements IDecoder { } } -export function createAsymDecoder( +export function createDecoder( contentTopic: string, privateKey: Uint8Array -): AsymDecoder { - return new AsymDecoder(contentTopic, privateKey); +): Decoder { + return new Decoder(contentTopic, privateKey); } diff --git a/packages/message-encryption/src/symmetric.spec.ts b/packages/message-encryption/src/symmetric.spec.ts index 94374a606a..47219109fa 100644 --- a/packages/message-encryption/src/symmetric.spec.ts +++ b/packages/message-encryption/src/symmetric.spec.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import fc from "fast-check"; import { getPublicKey } from "./crypto/index.js"; -import { createSymDecoder, createSymEncoder } from "./symmetric.js"; +import { createDecoder, createEncoder } from "./symmetric.js"; const TestContentTopic = "/test/1/waku-message/utf8"; @@ -13,10 +13,10 @@ describe("Symmetric Encryption", function () { fc.uint8Array({ minLength: 1 }), fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }), async (payload, symKey) => { - const encoder = createSymEncoder(TestContentTopic, symKey); + const encoder = createEncoder(TestContentTopic, symKey); const bytes = await encoder.toWire({ payload }); - const decoder = createSymDecoder(TestContentTopic, symKey); + const decoder = createDecoder(TestContentTopic, symKey); const protoResult = await decoder.fromWireToProtoObj(bytes!); if (!protoResult) throw "Failed to proto decode"; const result = await decoder.fromProtoObj(protoResult); @@ -41,14 +41,10 @@ describe("Symmetric Encryption", function () { async (payload, sigPrivKey, symKey) => { const sigPubKey = getPublicKey(sigPrivKey); - const encoder = createSymEncoder( - TestContentTopic, - symKey, - sigPrivKey - ); + const encoder = createEncoder(TestContentTopic, symKey, sigPrivKey); const bytes = await encoder.toWire({ payload }); - const decoder = createSymDecoder(TestContentTopic, symKey); + const decoder = createDecoder(TestContentTopic, symKey); const protoResult = await decoder.fromWireToProtoObj(bytes!); if (!protoResult) throw "Failed to proto decode"; const result = await decoder.fromProtoObj(protoResult); diff --git a/packages/message-encryption/src/symmetric.ts b/packages/message-encryption/src/symmetric.ts index cf990ce669..37cb42372f 100644 --- a/packages/message-encryption/src/symmetric.ts +++ b/packages/message-encryption/src/symmetric.ts @@ -17,11 +17,18 @@ import { preCipher, } from "./waku_payload.js"; -import { DecodedMessage, OneMillion, Version } from "./index.js"; +import { + DecodedMessage, + generateSymmetricKey, + OneMillion, + Version, +} from "./index.js"; + +export { DecodedMessage, generateSymmetricKey }; const log = debug("waku:message-encryption:symmetric"); -class SymEncoder implements IEncoder { +class Encoder implements IEncoder { constructor( public contentTopic: string, private symKey: Uint8Array, @@ -58,16 +65,16 @@ class SymEncoder implements IEncoder { } } -export function createSymEncoder( +export function createEncoder( contentTopic: string, symKey: Uint8Array, sigPrivKey?: Uint8Array, ephemeral = false -): SymEncoder { - return new SymEncoder(contentTopic, symKey, sigPrivKey, ephemeral); +): Encoder { + return new Encoder(contentTopic, symKey, sigPrivKey, ephemeral); } -class SymDecoder extends DecoderV0 implements IDecoder { +class Decoder extends DecoderV0 implements IDecoder { constructor(contentTopic: string, private symKey: Uint8Array) { super(contentTopic); } @@ -125,9 +132,9 @@ class SymDecoder extends DecoderV0 implements IDecoder { } } -export function createSymDecoder( +export function createDecoder( contentTopic: string, symKey: Uint8Array -): SymDecoder { - return new SymDecoder(contentTopic, symKey); +): Decoder { + return new Decoder(contentTopic, symKey); } diff --git a/packages/message-encryption/src/waku_payload.ts b/packages/message-encryption/src/waku_payload.ts index a62b3e835d..08846cc896 100644 --- a/packages/message-encryption/src/waku_payload.ts +++ b/packages/message-encryption/src/waku_payload.ts @@ -6,7 +6,7 @@ import * as ecies from "./crypto/ecies.js"; import { keccak256, randomBytes, sign } from "./crypto/index.js"; import * as symmetric from "./crypto/symmetric.js"; -import { Signature } from "./index"; +import { Signature } from "./index.js"; const FlagsLength = 1; const FlagMask = 3; // 0011 diff --git a/packages/tests/tests/ephemeral.node.spec.ts b/packages/tests/tests/ephemeral.node.spec.ts index a20e9421a5..73f631ba55 100644 --- a/packages/tests/tests/ephemeral.node.spec.ts +++ b/packages/tests/tests/ephemeral.node.spec.ts @@ -4,14 +4,16 @@ import { waitForRemotePeer } from "@waku/core/lib/wait_for_remote_peer"; import { createLightNode } from "@waku/create"; import { Protocols, WakuLight } from "@waku/interfaces"; import { - createAsymDecoder, - createAsymEncoder, - createSymDecoder, - createSymEncoder, + createDecoder as eciesDecoder, + createEncoder as eciesEncoder, generatePrivateKey, - generateSymmetricKey, getPublicKey, -} from "@waku/message-encryption"; +} from "@waku/message-encryption/ecies"; +import { + generateSymmetricKey, + createDecoder as symDecoder, + createEncoder as symEncoder, +} from "@waku/message-encryption/symmetric"; import { expect } from "chai"; import debug from "debug"; @@ -75,22 +77,17 @@ describe("Waku Message Ephemeral field", () => { const AsymContentTopic = "/test/1/ephemeral-asym/utf8"; const SymContentTopic = "/test/1/ephemeral-sym/utf8"; - const asymEncoder = createAsymEncoder( + const asymEncoder = eciesEncoder( AsymContentTopic, publicKey, undefined, true ); - const symEncoder = createSymEncoder( - SymContentTopic, - symKey, - undefined, - true - ); + const symEncoder = eciesEncoder(SymContentTopic, symKey, undefined, true); const clearEncoder = createEncoder(TestContentTopic, true); - const asymDecoder = createAsymDecoder(AsymContentTopic, privateKey); - const symDecoder = createSymDecoder(SymContentTopic, symKey); + const asymDecoder = eciesDecoder(AsymContentTopic, privateKey); + const symDecoder = eciesDecoder(SymContentTopic, symKey); const [waku1, waku2, nimWakuMultiaddr] = await Promise.all([ createLightNode({ @@ -187,14 +184,14 @@ describe("Waku Message Ephemeral field", () => { const symKey = generateSymmetricKey(); - const ephemeralEncoder = createSymEncoder( + const ephemeralEncoder = symEncoder( TestContentTopic, symKey, undefined, true ); - const encoder = createSymEncoder(TestContentTopic, symKey); - const decoder = createSymDecoder(TestContentTopic, symKey); + const encoder = symEncoder(TestContentTopic, symKey); + const decoder = symDecoder(TestContentTopic, symKey); const messages: DecodedMessage[] = []; const callback = (msg: DecodedMessage): void => { @@ -235,14 +232,14 @@ describe("Waku Message Ephemeral field", () => { const privKey = generatePrivateKey(); const pubKey = getPublicKey(privKey); - const ephemeralEncoder = createAsymEncoder( + const ephemeralEncoder = eciesEncoder( TestContentTopic, pubKey, undefined, true ); - const encoder = createAsymEncoder(TestContentTopic, pubKey); - const decoder = createAsymDecoder(TestContentTopic, privKey); + const encoder = eciesEncoder(TestContentTopic, pubKey); + const decoder = eciesDecoder(TestContentTopic, privKey); const messages: DecodedMessage[] = []; const callback = (msg: DecodedMessage): void => { diff --git a/packages/tests/tests/relay.node.spec.ts b/packages/tests/tests/relay.node.spec.ts index d22c4588f8..519c539f35 100644 --- a/packages/tests/tests/relay.node.spec.ts +++ b/packages/tests/tests/relay.node.spec.ts @@ -7,14 +7,16 @@ import { createPrivacyNode } from "@waku/create"; import type { WakuPrivacy } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { - createAsymDecoder, - createAsymEncoder, - createSymDecoder, - createSymEncoder, + createDecoder as createEciesDecoder, + createEncoder as createEciesEncoder, generatePrivateKey, - generateSymmetricKey, getPublicKey, -} from "@waku/message-encryption"; +} from "@waku/message-encryption/ecies"; +import { + createDecoder as createSymDecoder, + createEncoder as createSymEncoder, + generateSymmetricKey, +} from "@waku/message-encryption/symmetric"; import { expect } from "chai"; import debug from "debug"; @@ -187,21 +189,21 @@ describe("Waku Relay [node only]", () => { const symKey = generateSymmetricKey(); const publicKey = getPublicKey(privateKey); - const asymEncoder = createAsymEncoder(asymTopic, publicKey); + const eciesEncoder = createEciesEncoder(asymTopic, publicKey); const symEncoder = createSymEncoder(symTopic, symKey); - const asymDecoder = createAsymDecoder(asymTopic, privateKey); + const eciesDecoder = createEciesDecoder(asymTopic, privateKey); const symDecoder = createSymDecoder(symTopic, symKey); const msgs: DecodedMessage[] = []; - waku2.relay.addObserver(asymDecoder, (wakuMsg) => { + waku2.relay.addObserver(eciesDecoder, (wakuMsg) => { msgs.push(wakuMsg); }); waku2.relay.addObserver(symDecoder, (wakuMsg) => { msgs.push(wakuMsg); }); - await waku1.relay.send(asymEncoder, { payload: utf8ToBytes(asymText) }); + await waku1.relay.send(eciesEncoder, { payload: utf8ToBytes(asymText) }); await delay(200); await waku1.relay.send(symEncoder, { payload: utf8ToBytes(symText) }); diff --git a/packages/tests/tests/store.node.spec.ts b/packages/tests/tests/store.node.spec.ts index f1743169f6..9907d35b4c 100644 --- a/packages/tests/tests/store.node.spec.ts +++ b/packages/tests/tests/store.node.spec.ts @@ -6,14 +6,16 @@ import { createLightNode } from "@waku/create"; import { DecodedMessage, Message, WakuLight } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { - createAsymDecoder, - createAsymEncoder, - createSymDecoder, - createSymEncoder, + createDecoder as createEciesDecoder, + createEncoder as createEciesEncoder, generatePrivateKey, - generateSymmetricKey, getPublicKey, -} from "@waku/message-encryption"; +} from "@waku/message-encryption/ecies"; +import { + createDecoder as createSymDecoder, + createEncoder as createSymEncoder, + generateSymmetricKey, +} from "@waku/message-encryption/symmetric"; import { expect } from "chai"; import debug from "debug"; @@ -365,15 +367,15 @@ describe("Waku Store", () => { const symKey = generateSymmetricKey(); const publicKey = getPublicKey(privateKey); - const asymEncoder = createAsymEncoder(asymTopic, publicKey); + const eciesEncoder = createEciesEncoder(asymTopic, publicKey); const symEncoder = createSymEncoder(symTopic, symKey); - const otherEncoder = createAsymEncoder( + const otherEncoder = createEciesEncoder( TestContentTopic, getPublicKey(generatePrivateKey()) ); - const asymDecoder = createAsymDecoder(asymTopic, privateKey); + const eciesDecoder = createEciesDecoder(asymTopic, privateKey); const symDecoder = createSymDecoder(symTopic, symKey); const [waku1, waku2, nimWakuMultiaddr] = await Promise.all([ @@ -399,7 +401,7 @@ describe("Waku Store", () => { log("Sending messages using light push"); await Promise.all([ - waku1.lightPush.push(asymEncoder, asymMsg), + waku1.lightPush.push(eciesEncoder, asymMsg), waku1.lightPush.push(symEncoder, symMsg), waku1.lightPush.push(otherEncoder, otherMsg), waku1.lightPush.push(TestEncoder, clearMsg), @@ -411,7 +413,7 @@ describe("Waku Store", () => { log("Retrieve messages from store"); for await (const msgPromises of waku2.store.queryGenerator([ - asymDecoder, + eciesDecoder, symDecoder, TestDecoder, ])) { diff --git a/packages/tests/tests/waku.node.spec.ts b/packages/tests/tests/waku.node.spec.ts index fd4049a613..37e4e080c5 100644 --- a/packages/tests/tests/waku.node.spec.ts +++ b/packages/tests/tests/waku.node.spec.ts @@ -12,10 +12,10 @@ import type { } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces"; import { - createSymDecoder, - createSymEncoder, + createDecoder, + createEncoder, generateSymmetricKey, -} from "@waku/message-encryption"; +} from "@waku/message-encryption/symmetric"; import { expect } from "chai"; import { @@ -167,9 +167,9 @@ describe("Decryption Keys", () => { this.timeout(10000); const symKey = generateSymmetricKey(); - const decoder = createSymDecoder(TestContentTopic, symKey); + const decoder = createDecoder(TestContentTopic, symKey); - const encoder = createSymEncoder(TestContentTopic, symKey); + const encoder = createEncoder(TestContentTopic, symKey); const messageText = "Message is encrypted"; const messageTimestamp = new Date("1995-12-17T03:24:00"); const message = {