Merge pull request #1221 from waku-org/feat/export-decoder-from-root

This commit is contained in:
fryorcraken.eth 2023-03-08 09:59:26 +11:00 committed by GitHub
commit b7bc09d493
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 53 deletions

View File

@ -16,14 +16,12 @@ module.exports = [
{ {
name: "ECIES encryption", name: "ECIES encryption",
path: "packages/message-encryption/bundle/ecies.js", path: "packages/message-encryption/bundle/ecies.js",
import: import: "{ generatePrivateKey, createEncoder, createDecoder }",
"{ generatePrivateKey, createEncoder, createDecoder, DecodedMessage }",
}, },
{ {
name: "Symmetric encryption", name: "Symmetric encryption",
path: "packages/message-encryption/bundle/symmetric.js", path: "packages/message-encryption/bundle/symmetric.js",
import: import: "{ generateSymmetricKey, createEncoder, createDecoder }",
"{ generateSymmetricKey, createEncoder, createDecoder, DecodedMessage }",
}, },
{ {
name: "DNS discovery", name: "DNS discovery",

View File

@ -1,9 +1,10 @@
export { DefaultPubSubTopic } from "./lib/constants.js"; export { DefaultPubSubTopic } from "./lib/constants.js";
export { DefaultUserAgent } from "./lib/waku.js"; export { DefaultUserAgent } from "./lib/waku.js";
export { export { createEncoder, createDecoder } from "./lib/message/version_0.js";
createEncoder, export type {
createDecoder, Encoder,
Decoder,
DecodedMessage, DecodedMessage,
} from "./lib/message/version_0.js"; } from "./lib/message/version_0.js";
export * as message from "./lib/message/index.js"; export * as message from "./lib/message/index.js";

View File

@ -0,0 +1,26 @@
import {
DecodedMessage as DecodedMessageV0,
proto,
} from "@waku/core/lib/message/version_0";
import type { IDecodedMessage } from "@waku/interfaces";
export class DecodedMessage
extends DecodedMessageV0
implements IDecodedMessage
{
private readonly _decodedPayload: Uint8Array;
constructor(
proto: proto.WakuMessage,
decodedPayload: Uint8Array,
public signature?: Uint8Array,
public signaturePublicKey?: Uint8Array
) {
super(proto);
this._decodedPayload = decodedPayload;
}
get payload(): Uint8Array {
return this._decodedPayload;
}
}

View File

@ -9,6 +9,7 @@ import type {
import { WakuMessage } from "@waku/proto"; import { WakuMessage } from "@waku/proto";
import debug from "debug"; import debug from "debug";
import { DecodedMessage } from "./decoded_message.js";
import { import {
decryptAsymmetric, decryptAsymmetric,
encryptAsymmetric, encryptAsymmetric,
@ -17,18 +18,18 @@ import {
} from "./waku_payload.js"; } from "./waku_payload.js";
import { import {
DecodedMessage,
generatePrivateKey, generatePrivateKey,
getPublicKey, getPublicKey,
OneMillion, OneMillion,
Version, Version,
} from "./index.js"; } from "./index.js";
export { DecodedMessage, generatePrivateKey, getPublicKey }; export { generatePrivateKey, getPublicKey };
export type { Encoder, Decoder, DecodedMessage };
const log = debug("waku:message-encryption:ecies"); const log = debug("waku:message-encryption:ecies");
export class Encoder implements IEncoder { class Encoder implements IEncoder {
constructor( constructor(
public contentTopic: string, public contentTopic: string,
private publicKey: Uint8Array, private publicKey: Uint8Array,
@ -88,7 +89,7 @@ export function createEncoder({
return new Encoder(contentTopic, publicKey, sigPrivKey, ephemeral); return new Encoder(contentTopic, publicKey, sigPrivKey, ephemeral);
} }
export class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> { class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
constructor(contentTopic: string, private privateKey: Uint8Array) { constructor(contentTopic: string, private privateKey: Uint8Array) {
super(contentTopic); super(contentTopic);
} }

View File

@ -1,18 +1,14 @@
import {
DecodedMessage as DecodedMessageV0,
proto,
} from "@waku/core/lib/message/version_0";
import type { IDecodedMessage } from "@waku/interfaces";
import { import {
generatePrivateKey, generatePrivateKey,
generateSymmetricKey, generateSymmetricKey,
getPublicKey, getPublicKey,
} from "./crypto/index.js"; } from "./crypto/index.js";
import { DecodedMessage } from "./decoded_message.js";
export const OneMillion = BigInt(1_000_000); export const OneMillion = BigInt(1_000_000);
export { generatePrivateKey, generateSymmetricKey, getPublicKey }; export { generatePrivateKey, generateSymmetricKey, getPublicKey };
export type { DecodedMessage };
export * as ecies from "./ecies.js"; export * as ecies from "./ecies.js";
export * as symmetric from "./symmetric.js"; export * as symmetric from "./symmetric.js";
@ -23,24 +19,3 @@ export type Signature = {
signature: Uint8Array; signature: Uint8Array;
publicKey: Uint8Array | undefined; publicKey: Uint8Array | undefined;
}; };
export class DecodedMessage
extends DecodedMessageV0
implements IDecodedMessage
{
private readonly _decodedPayload: Uint8Array;
constructor(
proto: proto.WakuMessage,
decodedPayload: Uint8Array,
public signature?: Uint8Array,
public signaturePublicKey?: Uint8Array
) {
super(proto);
this._decodedPayload = decodedPayload;
}
get payload(): Uint8Array {
return this._decodedPayload;
}
}

View File

@ -9,6 +9,7 @@ import type {
import { WakuMessage } from "@waku/proto"; import { WakuMessage } from "@waku/proto";
import debug from "debug"; import debug from "debug";
import { DecodedMessage } from "./decoded_message.js";
import { import {
decryptSymmetric, decryptSymmetric,
encryptSymmetric, encryptSymmetric,
@ -16,18 +17,14 @@ import {
preCipher, preCipher,
} from "./waku_payload.js"; } from "./waku_payload.js";
import { import { generateSymmetricKey, OneMillion, Version } from "./index.js";
DecodedMessage,
generateSymmetricKey,
OneMillion,
Version,
} from "./index.js";
export { DecodedMessage, generateSymmetricKey }; export { generateSymmetricKey };
export type { DecodedMessage, Encoder, Decoder };
const log = debug("waku:message-encryption:symmetric"); const log = debug("waku:message-encryption:symmetric");
export class Encoder implements IEncoder { class Encoder implements IEncoder {
constructor( constructor(
public contentTopic: string, public contentTopic: string,
private symKey: Uint8Array, private symKey: Uint8Array,
@ -87,7 +84,7 @@ export function createEncoder({
return new Encoder(contentTopic, symKey, sigPrivKey, ephemeral); return new Encoder(contentTopic, symKey, sigPrivKey, ephemeral);
} }
export class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> { class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
constructor(contentTopic: string, private symKey: Uint8Array) { constructor(contentTopic: string, private symKey: Uint8Array) {
super(contentTopic); super(contentTopic);
} }

View File

@ -2,11 +2,12 @@ import {
createCursor, createCursor,
createDecoder, createDecoder,
createEncoder, createEncoder,
DecodedMessage,
PageDirection, PageDirection,
waitForRemotePeer, waitForRemotePeer,
} from "@waku/core"; } from "@waku/core";
import { createLightNode } from "@waku/create"; import { createLightNode } from "@waku/create";
import type { IDecodedMessage, IMessage, LightNode } from "@waku/interfaces"; import type { IMessage, LightNode } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces";
import { import {
createDecoder as createEciesDecoder, createDecoder as createEciesDecoder,
@ -147,10 +148,10 @@ describe("Waku Store", () => {
const query = waku.store.queryGenerator([TestDecoder]); const query = waku.store.queryGenerator([TestDecoder]);
// messages in reversed order (first message at last index) // messages in reversed order (first message at last index)
const messages: IDecodedMessage[] = []; const messages: DecodedMessage[] = [];
for await (const page of query) { for await (const page of query) {
for await (const msg of page.reverse()) { for await (const msg of page.reverse()) {
messages.push(msg as IDecodedMessage); messages.push(msg as DecodedMessage);
} }
} }
@ -160,12 +161,12 @@ describe("Waku Store", () => {
// create cursor to extract messages after the 3rd index // create cursor to extract messages after the 3rd index
const cursor = await createCursor(messages[cursorIndex]); const cursor = await createCursor(messages[cursorIndex]);
const messagesAfterCursor: IDecodedMessage[] = []; const messagesAfterCursor: DecodedMessage[] = [];
for await (const page of waku.store.queryGenerator([TestDecoder], { for await (const page of waku.store.queryGenerator([TestDecoder], {
cursor, cursor,
})) { })) {
for await (const msg of page.reverse()) { for await (const msg of page.reverse()) {
messagesAfterCursor.push(msg as IDecodedMessage); messagesAfterCursor.push(msg as DecodedMessage);
} }
} }
@ -419,7 +420,7 @@ describe("Waku Store", () => {
await waitForRemotePeer(waku2, [Protocols.Store]); await waitForRemotePeer(waku2, [Protocols.Store]);
const messages: IDecodedMessage[] = []; const messages: DecodedMessage[] = [];
log("Retrieve messages from store"); log("Retrieve messages from store");
for await (const msgPromises of waku2.store.queryGenerator([ for await (const msgPromises of waku2.store.queryGenerator([