feat: easy import by having an export map for each type of encryption

This commit is contained in:
fryorcraken.eth 2022-12-05 15:14:17 +11:00
parent 563b66eab5
commit f7fabec8b3
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
11 changed files with 102 additions and 80 deletions

View File

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

View File

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

View File

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

View File

@ -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<DecodedMessage> {
class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
constructor(contentTopic: string, private privateKey: Uint8Array) {
super(contentTopic);
}
@ -126,9 +134,9 @@ class AsymDecoder extends DecoderV0 implements IDecoder<DecodedMessage> {
}
}
export function createAsymDecoder(
export function createDecoder(
contentTopic: string,
privateKey: Uint8Array
): AsymDecoder {
return new AsymDecoder(contentTopic, privateKey);
): Decoder {
return new Decoder(contentTopic, privateKey);
}

View File

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

View File

@ -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<DecodedMessage> {
class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
constructor(contentTopic: string, private symKey: Uint8Array) {
super(contentTopic);
}
@ -125,9 +132,9 @@ class SymDecoder extends DecoderV0 implements IDecoder<DecodedMessage> {
}
}
export function createSymDecoder(
export function createDecoder(
contentTopic: string,
symKey: Uint8Array
): SymDecoder {
return new SymDecoder(contentTopic, symKey);
): Decoder {
return new Decoder(contentTopic, symKey);
}

View File

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

View File

@ -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 => {

View File

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

View File

@ -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,
])) {

View File

@ -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 = {