2022-12-02 16:38:30 +11:00
|
|
|
import {
|
|
|
|
|
createCursor,
|
|
|
|
|
createDecoder,
|
|
|
|
|
createEncoder,
|
2023-03-07 15:29:41 +11:00
|
|
|
DecodedMessage,
|
2022-12-02 16:38:30 +11:00
|
|
|
PageDirection,
|
|
|
|
|
waitForRemotePeer,
|
|
|
|
|
} from "@waku/core";
|
2022-11-21 10:51:44 +11:00
|
|
|
import { createLightNode } from "@waku/create";
|
2023-03-07 15:29:41 +11:00
|
|
|
import type { IMessage, LightNode } from "@waku/interfaces";
|
2022-11-04 11:45:15 +11:00
|
|
|
import { Protocols } from "@waku/interfaces";
|
2022-09-19 13:50:29 +10:00
|
|
|
import {
|
2022-12-05 15:14:17 +11:00
|
|
|
createDecoder as createEciesDecoder,
|
|
|
|
|
createEncoder as createEciesEncoder,
|
2022-11-04 11:45:15 +11:00
|
|
|
generatePrivateKey,
|
|
|
|
|
getPublicKey,
|
2022-12-05 15:14:17 +11:00
|
|
|
} from "@waku/message-encryption/ecies";
|
|
|
|
|
import {
|
|
|
|
|
createDecoder as createSymDecoder,
|
|
|
|
|
createEncoder as createSymEncoder,
|
|
|
|
|
generateSymmetricKey,
|
|
|
|
|
} from "@waku/message-encryption/symmetric";
|
2023-03-14 10:10:38 +05:30
|
|
|
import { bytesToUtf8, utf8ToBytes } from "@waku/utils/bytes";
|
2022-11-01 21:31:53 +11:00
|
|
|
import { expect } from "chai";
|
|
|
|
|
import debug from "debug";
|
2021-04-07 11:04:30 +10:00
|
|
|
|
2022-12-02 15:43:46 +11:00
|
|
|
import {
|
|
|
|
|
delay,
|
|
|
|
|
makeLogFileName,
|
|
|
|
|
NOISE_KEY_1,
|
|
|
|
|
NOISE_KEY_2,
|
|
|
|
|
} from "../src/index.js";
|
2023-05-19 01:38:43 +05:30
|
|
|
import { NimGoNode } from "../src/node/node.js";
|
2021-05-17 16:11:01 +10:00
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
const log = debug("waku:test:store");
|
2021-07-12 13:13:00 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
const TestContentTopic = "/test/1/waku-store/utf8";
|
2023-02-02 11:37:28 +05:30
|
|
|
const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
|
2022-11-23 16:25:50 +11:00
|
|
|
const TestDecoder = createDecoder(TestContentTopic);
|
2021-07-28 11:19:24 +10:00
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
describe("Waku Store", () => {
|
2022-12-06 13:18:32 +11:00
|
|
|
let waku: LightNode;
|
2023-05-19 01:28:49 +05:30
|
|
|
let nwaku: NimGoNode;
|
2021-04-07 11:04:30 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
beforeEach(async function () {
|
2022-11-16 19:06:32 +05:30
|
|
|
this.timeout(15_000);
|
2023-05-19 01:28:49 +05:30
|
|
|
nwaku = new NimGoNode(makeLogFileName(this));
|
2022-12-12 11:08:08 +05:30
|
|
|
await nwaku.start({ store: true, lightpush: true, relay: true });
|
2022-09-14 13:44:00 +10:00
|
|
|
});
|
|
|
|
|
|
2021-04-07 11:04:30 +10:00
|
|
|
afterEach(async function () {
|
2023-04-17 10:29:36 +05:30
|
|
|
!!nwaku &&
|
|
|
|
|
nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e));
|
2022-02-04 14:12:00 +11:00
|
|
|
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
|
2021-04-07 11:04:30 +10:00
|
|
|
});
|
|
|
|
|
|
2022-11-15 17:30:35 +05:30
|
|
|
it("Generator", async function () {
|
2022-11-15 18:07:29 +05:30
|
|
|
this.timeout(15_000);
|
2022-11-15 17:30:35 +05:30
|
|
|
const totalMsgs = 20;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
|
|
|
|
expect(
|
|
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-11-15 17:30:35 +05:30
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
).to.be.true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2022-11-15 17:30:35 +05:30
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
|
|
|
|
await waku.start();
|
|
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
|
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-11-15 17:30:35 +05:30
|
|
|
let promises: Promise<void>[] = [];
|
|
|
|
|
for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
|
|
|
|
|
const _promises = msgPromises.map(async (promise) => {
|
|
|
|
|
const msg = await promise;
|
|
|
|
|
if (msg) {
|
|
|
|
|
messages.push(msg);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promises = promises.concat(_promises);
|
|
|
|
|
}
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
|
|
expect(messages?.length).eq(totalMsgs);
|
|
|
|
|
const result = messages?.findIndex((msg) => {
|
2023-02-24 23:22:04 +11:00
|
|
|
return msg.payload[0]! === 0;
|
2022-11-15 17:30:35 +05:30
|
|
|
});
|
|
|
|
|
expect(result).to.not.eq(-1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Generator, no message returned", async function () {
|
|
|
|
|
this.timeout(15_000);
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2022-11-15 17:30:35 +05:30
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
|
|
|
|
await waku.start();
|
|
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
|
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-11-15 17:30:35 +05:30
|
|
|
let promises: Promise<void>[] = [];
|
|
|
|
|
for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
|
|
|
|
|
const _promises = msgPromises.map(async (promise) => {
|
|
|
|
|
const msg = await promise;
|
|
|
|
|
if (msg) {
|
|
|
|
|
messages.push(msg);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promises = promises.concat(_promises);
|
|
|
|
|
}
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
|
|
expect(messages?.length).eq(0);
|
|
|
|
|
});
|
2022-11-15 05:17:24 +05:30
|
|
|
|
|
|
|
|
it("Passing a cursor", async function () {
|
|
|
|
|
this.timeout(4_000);
|
2022-09-14 13:44:00 +10:00
|
|
|
const totalMsgs = 20;
|
2021-04-13 11:47:15 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
2021-05-06 10:36:36 +10:00
|
|
|
expect(
|
2022-04-01 12:19:51 +11:00
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-09-11 00:57:10 +10:00
|
|
|
payload: utf8ToBytes(`Message ${i}`),
|
|
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
})
|
2021-07-07 11:23:56 +10:00
|
|
|
)
|
2021-05-06 10:36:36 +10:00
|
|
|
).to.be.true;
|
2021-04-13 11:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2021-04-15 11:19:26 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
2022-09-12 11:35:24 +10:00
|
|
|
|
2022-11-15 05:17:24 +05:30
|
|
|
const query = waku.store.queryGenerator([TestDecoder]);
|
2021-10-07 15:33:00 +11:00
|
|
|
|
2022-11-15 17:30:35 +05:30
|
|
|
// messages in reversed order (first message at last index)
|
2023-03-07 15:29:41 +11:00
|
|
|
const messages: DecodedMessage[] = [];
|
2022-11-15 05:17:24 +05:30
|
|
|
for await (const page of query) {
|
2022-11-15 17:30:35 +05:30
|
|
|
for await (const msg of page.reverse()) {
|
2023-03-07 15:29:41 +11:00
|
|
|
messages.push(msg as DecodedMessage);
|
2022-11-15 05:17:24 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-10-07 15:33:00 +11:00
|
|
|
|
2022-11-15 17:30:35 +05:30
|
|
|
// index 2 would mean the third last message sent
|
2022-11-15 05:17:24 +05:30
|
|
|
const cursorIndex = 2;
|
2022-09-14 13:44:00 +10:00
|
|
|
|
2022-11-15 17:30:35 +05:30
|
|
|
// create cursor to extract messages after the 3rd index
|
2022-11-16 18:57:46 +05:30
|
|
|
const cursor = await createCursor(messages[cursorIndex]);
|
2022-09-14 13:44:00 +10:00
|
|
|
|
2023-03-07 15:29:41 +11:00
|
|
|
const messagesAfterCursor: DecodedMessage[] = [];
|
2022-11-15 17:30:35 +05:30
|
|
|
for await (const page of waku.store.queryGenerator([TestDecoder], {
|
|
|
|
|
cursor,
|
|
|
|
|
})) {
|
|
|
|
|
for await (const msg of page.reverse()) {
|
2023-03-07 15:29:41 +11:00
|
|
|
messagesAfterCursor.push(msg as DecodedMessage);
|
2022-11-15 17:30:35 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const testMessage = messagesAfterCursor[0];
|
|
|
|
|
|
|
|
|
|
expect(messages.length).be.eq(totalMsgs);
|
|
|
|
|
|
2023-02-24 23:22:04 +11:00
|
|
|
expect(bytesToUtf8(testMessage.payload)).to.be.eq(
|
|
|
|
|
bytesToUtf8(messages[cursorIndex + 1].payload)
|
2022-11-15 17:30:35 +05:30
|
|
|
);
|
2022-09-14 13:44:00 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Callback on promise", async function () {
|
|
|
|
|
this.timeout(15_000);
|
|
|
|
|
|
|
|
|
|
const totalMsgs = 15;
|
2021-10-07 15:33:00 +11:00
|
|
|
|
|
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
|
|
|
|
expect(
|
2022-04-01 12:19:51 +11:00
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-09-11 00:57:10 +10:00
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
})
|
2021-10-07 15:33:00 +11:00
|
|
|
)
|
|
|
|
|
).to.be.true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2021-10-07 15:33:00 +11:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
2021-10-07 15:33:00 +11:00
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-09-19 16:33:07 +10:00
|
|
|
await waku.store.queryCallbackOnPromise(
|
|
|
|
|
[TestDecoder],
|
|
|
|
|
async (msgPromise) => {
|
|
|
|
|
const msg = await msgPromise;
|
|
|
|
|
if (msg) {
|
|
|
|
|
messages.push(msg);
|
|
|
|
|
}
|
2022-09-14 13:44:00 +10:00
|
|
|
}
|
2022-09-19 16:33:07 +10:00
|
|
|
);
|
2021-10-07 15:33:00 +11:00
|
|
|
|
|
|
|
|
expect(messages?.length).eq(totalMsgs);
|
|
|
|
|
const result = messages?.findIndex((msg) => {
|
2023-02-24 23:22:04 +11:00
|
|
|
return msg.payload[0]! === 0;
|
2021-10-07 15:33:00 +11:00
|
|
|
});
|
|
|
|
|
expect(result).to.not.eq(-1);
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
it("Callback on promise, aborts when callback returns true", async function () {
|
2022-04-01 12:59:59 +11:00
|
|
|
this.timeout(15_000);
|
2021-10-07 15:33:00 +11:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
const totalMsgs = 20;
|
2021-10-07 15:33:00 +11:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
2021-10-07 15:33:00 +11:00
|
|
|
expect(
|
2022-04-01 12:19:51 +11:00
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-09-11 00:57:10 +10:00
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
})
|
2021-10-07 15:33:00 +11:00
|
|
|
)
|
|
|
|
|
).to.be.true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2021-10-07 15:33:00 +11:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
2021-10-07 15:33:00 +11:00
|
|
|
|
|
|
|
|
const desiredMsgs = 14;
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-09-14 13:44:00 +10:00
|
|
|
await waku.store.queryCallbackOnPromise(
|
2022-09-19 16:33:07 +10:00
|
|
|
[TestDecoder],
|
2022-09-14 13:44:00 +10:00
|
|
|
async (msgPromise) => {
|
|
|
|
|
const msg = await msgPromise;
|
|
|
|
|
if (msg) {
|
|
|
|
|
messages.push(msg);
|
|
|
|
|
}
|
2021-10-07 15:33:00 +11:00
|
|
|
return messages.length >= desiredMsgs;
|
|
|
|
|
},
|
2022-09-12 11:35:24 +10:00
|
|
|
{ pageSize: 7 }
|
|
|
|
|
);
|
2021-10-07 15:33:00 +11:00
|
|
|
|
|
|
|
|
expect(messages?.length).eq(desiredMsgs);
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
it("Ordered Callback - Forward", async function () {
|
2022-04-01 12:59:59 +11:00
|
|
|
this.timeout(15_000);
|
2021-04-13 11:47:15 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
const totalMsgs = 18;
|
|
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
2021-05-06 10:36:36 +10:00
|
|
|
expect(
|
2022-04-01 12:19:51 +11:00
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-09-11 00:57:10 +10:00
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
})
|
2021-07-07 11:23:56 +10:00
|
|
|
)
|
2021-05-06 10:36:36 +10:00
|
|
|
).to.be.true;
|
2022-11-17 11:05:55 +11:00
|
|
|
await delay(1); // to ensure each timestamp is unique.
|
2021-04-13 11:47:15 +10:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2021-04-15 11:19:26 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
2021-04-15 14:28:18 +10:00
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-09-14 13:44:00 +10:00
|
|
|
await waku.store.queryOrderedCallback(
|
2022-09-19 16:33:07 +10:00
|
|
|
[TestDecoder],
|
2022-09-14 13:44:00 +10:00
|
|
|
async (msg) => {
|
|
|
|
|
messages.push(msg);
|
2022-09-12 11:35:24 +10:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
pageDirection: PageDirection.FORWARD,
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-04-13 11:47:15 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
expect(messages?.length).eq(totalMsgs);
|
2023-02-24 23:22:04 +11:00
|
|
|
const payloads = messages.map((msg) => msg.payload[0]!);
|
2022-11-15 15:32:34 +11:00
|
|
|
expect(payloads).to.deep.eq(Array.from(Array(totalMsgs).keys()));
|
2021-04-13 11:47:15 +10:00
|
|
|
});
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
it("Ordered Callback - Backward", async function () {
|
2022-04-01 12:59:59 +11:00
|
|
|
this.timeout(15_000);
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
const totalMsgs = 18;
|
|
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
2021-06-09 12:25:56 +10:00
|
|
|
expect(
|
2022-04-01 12:19:51 +11:00
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-09-11 00:57:10 +10:00
|
|
|
contentTopic: TestContentTopic,
|
2022-09-14 13:44:00 +10:00
|
|
|
})
|
2021-06-09 12:25:56 +10:00
|
|
|
)
|
|
|
|
|
).to.be.true;
|
2022-11-17 11:05:55 +11:00
|
|
|
await delay(1); // to ensure each timestamp is unique.
|
2021-06-09 12:25:56 +10:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2021-06-09 12:25:56 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
2021-06-09 12:25:56 +10:00
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
let messages: IMessage[] = [];
|
2022-09-14 13:44:00 +10:00
|
|
|
await waku.store.queryOrderedCallback(
|
2022-09-19 16:33:07 +10:00
|
|
|
[TestDecoder],
|
2022-09-14 13:44:00 +10:00
|
|
|
async (msg) => {
|
|
|
|
|
messages.push(msg);
|
2022-09-12 11:35:24 +10:00
|
|
|
},
|
|
|
|
|
{
|
2022-09-14 13:44:00 +10:00
|
|
|
pageDirection: PageDirection.BACKWARD,
|
2022-09-12 11:35:24 +10:00
|
|
|
}
|
|
|
|
|
);
|
2021-07-12 13:13:00 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
messages = messages.reverse();
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
expect(messages?.length).eq(totalMsgs);
|
2022-11-15 15:32:34 +11:00
|
|
|
const payloads = messages.map((msg) => msg.payload![0]!);
|
|
|
|
|
expect(payloads).to.deep.eq(Array.from(Array(totalMsgs).keys()));
|
2022-01-17 14:11:05 +11:00
|
|
|
});
|
|
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
it("Generator, with asymmetric & symmetric encrypted messages", async function () {
|
2022-04-01 12:59:59 +11:00
|
|
|
this.timeout(15_000);
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
const asymText = "This message is encrypted for me using asymmetric";
|
|
|
|
|
const asymTopic = "/test/1/asymmetric/proto";
|
|
|
|
|
const symText =
|
2022-02-04 14:12:00 +11:00
|
|
|
"This message is encrypted for me using symmetric encryption";
|
2022-09-19 13:50:29 +10:00
|
|
|
const symTopic = "/test/1/symmetric/proto";
|
|
|
|
|
const clearText = "This is a clear text message for everyone to read";
|
|
|
|
|
const otherText =
|
2022-02-04 14:12:00 +11:00
|
|
|
"This message is not for and I must not be able to read it";
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-09-19 13:50:29 +10:00
|
|
|
const timestamp = new Date();
|
|
|
|
|
|
|
|
|
|
const asymMsg = { payload: utf8ToBytes(asymText), timestamp };
|
|
|
|
|
const symMsg = {
|
|
|
|
|
payload: utf8ToBytes(symText),
|
|
|
|
|
timestamp: new Date(timestamp.valueOf() + 1),
|
|
|
|
|
};
|
|
|
|
|
const clearMsg = {
|
|
|
|
|
payload: utf8ToBytes(clearText),
|
|
|
|
|
timestamp: new Date(timestamp.valueOf() + 2),
|
|
|
|
|
};
|
|
|
|
|
const otherMsg = {
|
|
|
|
|
payload: utf8ToBytes(otherText),
|
|
|
|
|
timestamp: new Date(timestamp.valueOf() + 3),
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-17 14:11:05 +11:00
|
|
|
const privateKey = generatePrivateKey();
|
|
|
|
|
const symKey = generateSymmetricKey();
|
|
|
|
|
const publicKey = getPublicKey(privateKey);
|
|
|
|
|
|
2023-02-02 11:37:28 +05:30
|
|
|
const eciesEncoder = createEciesEncoder({
|
|
|
|
|
contentTopic: asymTopic,
|
|
|
|
|
publicKey,
|
|
|
|
|
});
|
|
|
|
|
const symEncoder = createSymEncoder({
|
|
|
|
|
contentTopic: symTopic,
|
|
|
|
|
symKey,
|
|
|
|
|
});
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2023-02-02 11:37:28 +05:30
|
|
|
const otherEncoder = createEciesEncoder({
|
|
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
publicKey: getPublicKey(generatePrivateKey()),
|
|
|
|
|
});
|
2022-09-19 13:50:29 +10:00
|
|
|
|
2022-12-05 15:14:17 +11:00
|
|
|
const eciesDecoder = createEciesDecoder(asymTopic, privateKey);
|
2022-11-23 16:25:50 +11:00
|
|
|
const symDecoder = createSymDecoder(symTopic, symKey);
|
2022-01-17 14:11:05 +11:00
|
|
|
|
|
|
|
|
const [waku1, waku2, nimWakuMultiaddr] = await Promise.all([
|
2022-11-21 10:51:44 +11:00
|
|
|
createLightNode({
|
2022-01-17 14:11:05 +11:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2022-11-21 10:51:44 +11:00
|
|
|
createLightNode({
|
2022-01-17 14:11:05 +11:00
|
|
|
staticNoiseKey: NOISE_KEY_2,
|
2022-06-23 14:03:52 +10:00
|
|
|
}).then((waku) => waku.start().then(() => waku)),
|
2022-04-01 12:19:51 +11:00
|
|
|
nwaku.getMultiaddrWithId(),
|
2022-01-17 14:11:05 +11:00
|
|
|
]);
|
|
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Waku nodes created");
|
2022-01-17 14:11:05 +11:00
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
waku1.dial(nimWakuMultiaddr),
|
|
|
|
|
waku2.dial(nimWakuMultiaddr),
|
|
|
|
|
]);
|
|
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Waku nodes connected to nwaku");
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku1, [Protocols.LightPush]);
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Sending messages using light push");
|
2022-01-17 14:11:05 +11:00
|
|
|
await Promise.all([
|
2023-03-21 02:07:59 +01:00
|
|
|
waku1.lightPush.send(eciesEncoder, asymMsg),
|
|
|
|
|
waku1.lightPush.send(symEncoder, symMsg),
|
|
|
|
|
waku1.lightPush.send(otherEncoder, otherMsg),
|
|
|
|
|
waku1.lightPush.send(TestEncoder, clearMsg),
|
2022-01-17 14:11:05 +11:00
|
|
|
]);
|
|
|
|
|
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku2, [Protocols.Store]);
|
2022-01-17 14:11:05 +11:00
|
|
|
|
2023-03-07 15:29:41 +11:00
|
|
|
const messages: DecodedMessage[] = [];
|
2022-09-05 21:30:29 +10:00
|
|
|
log("Retrieve messages from store");
|
2022-09-14 13:44:00 +10:00
|
|
|
|
2022-09-19 16:33:07 +10:00
|
|
|
for await (const msgPromises of waku2.store.queryGenerator([
|
2022-12-05 15:14:17 +11:00
|
|
|
eciesDecoder,
|
2022-09-19 16:33:07 +10:00
|
|
|
symDecoder,
|
|
|
|
|
TestDecoder,
|
|
|
|
|
])) {
|
2022-09-19 13:50:29 +10:00
|
|
|
for (const promise of msgPromises) {
|
|
|
|
|
const msg = await promise;
|
|
|
|
|
if (msg) {
|
|
|
|
|
messages.push(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Messages are ordered from oldest to latest within a page (1 page query)
|
|
|
|
|
expect(bytesToUtf8(messages[0].payload!)).to.eq(asymText);
|
|
|
|
|
expect(bytesToUtf8(messages[1].payload!)).to.eq(symText);
|
|
|
|
|
expect(bytesToUtf8(messages[2].payload!)).to.eq(clearText);
|
|
|
|
|
expect(messages?.length).eq(3);
|
|
|
|
|
|
2022-02-04 14:12:00 +11:00
|
|
|
!!waku1 && waku1.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
|
!!waku2 && waku2.stop().catch((e) => console.log("Waku failed to stop", e));
|
2021-07-12 13:13:00 +10:00
|
|
|
});
|
2021-08-19 15:49:43 +10:00
|
|
|
|
2022-09-14 13:44:00 +10:00
|
|
|
it("Ordered callback, using start and end time", async function () {
|
|
|
|
|
this.timeout(20000);
|
2021-08-19 15:49:43 +10:00
|
|
|
|
2022-03-09 12:59:05 +11:00
|
|
|
const now = new Date();
|
|
|
|
|
|
2021-08-19 15:49:43 +10:00
|
|
|
const startTime = new Date();
|
2022-11-15 14:15:55 +11:00
|
|
|
// Set start time 15 seconds in the past
|
|
|
|
|
startTime.setTime(now.getTime() - 15 * 1000);
|
2021-08-19 15:49:43 +10:00
|
|
|
|
|
|
|
|
const message1Timestamp = new Date();
|
2022-11-15 14:15:55 +11:00
|
|
|
// Set first message was 10 seconds in the past
|
|
|
|
|
message1Timestamp.setTime(now.getTime() - 10 * 1000);
|
2022-03-09 12:59:05 +11:00
|
|
|
|
2021-08-19 15:49:43 +10:00
|
|
|
const message2Timestamp = new Date();
|
2022-11-15 14:15:55 +11:00
|
|
|
// Set second message 2 seconds in the past
|
|
|
|
|
message2Timestamp.setTime(now.getTime() - 2 * 1000);
|
2021-08-19 15:49:43 +10:00
|
|
|
const messageTimestamps = [message1Timestamp, message2Timestamp];
|
|
|
|
|
|
|
|
|
|
const endTime = new Date();
|
2022-11-15 14:15:55 +11:00
|
|
|
// Set end time 1 second in the past
|
|
|
|
|
endTime.setTime(now.getTime() - 1000);
|
2021-08-19 15:49:43 +10:00
|
|
|
|
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
|
expect(
|
2022-04-01 12:19:51 +11:00
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-09-11 00:57:10 +10:00
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
timestamp: messageTimestamps[i],
|
|
|
|
|
})
|
2021-08-19 15:49:43 +10:00
|
|
|
)
|
|
|
|
|
).to.be.true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2021-08-19 15:49:43 +10:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
2022-06-23 14:03:52 +10:00
|
|
|
await waku.start();
|
2022-04-01 12:19:51 +11:00
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
2022-07-25 12:33:08 +10:00
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
2021-08-19 15:49:43 +10:00
|
|
|
|
2022-04-01 12:19:51 +11:00
|
|
|
const nwakuPeerId = await nwaku.getPeerId();
|
2021-08-19 15:49:43 +10:00
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const firstMessages: IMessage[] = [];
|
2022-09-14 13:44:00 +10:00
|
|
|
await waku.store.queryOrderedCallback(
|
2022-09-19 16:33:07 +10:00
|
|
|
[TestDecoder],
|
2022-09-14 13:44:00 +10:00
|
|
|
(msg) => {
|
|
|
|
|
if (msg) {
|
|
|
|
|
firstMessages.push(msg);
|
|
|
|
|
}
|
2021-08-25 12:14:53 +10:00
|
|
|
},
|
2022-09-12 11:35:24 +10:00
|
|
|
{
|
|
|
|
|
peerId: nwakuPeerId,
|
|
|
|
|
timeFilter: { startTime, endTime: message1Timestamp },
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const bothMessages: IMessage[] = [];
|
2022-09-14 13:44:00 +10:00
|
|
|
await waku.store.queryOrderedCallback(
|
2022-09-19 16:33:07 +10:00
|
|
|
[TestDecoder],
|
2022-09-14 13:44:00 +10:00
|
|
|
async (msg) => {
|
|
|
|
|
bothMessages.push(msg);
|
2022-09-12 11:35:24 +10:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
peerId: nwakuPeerId,
|
|
|
|
|
timeFilter: {
|
|
|
|
|
startTime,
|
|
|
|
|
endTime,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(firstMessages?.length).eq(1);
|
|
|
|
|
|
2022-11-15 14:28:41 +11:00
|
|
|
expect(firstMessages[0].payload![0]!).eq(0);
|
2021-08-19 15:49:43 +10:00
|
|
|
|
|
|
|
|
expect(bothMessages?.length).eq(2);
|
|
|
|
|
});
|
2022-10-28 09:28:16 +11:00
|
|
|
|
|
|
|
|
it("Ordered callback, aborts when callback returns true", async function () {
|
|
|
|
|
this.timeout(15_000);
|
|
|
|
|
|
|
|
|
|
const totalMsgs = 20;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
|
|
|
|
expect(
|
|
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-10-28 09:28:16 +11:00
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
).to.be.true;
|
2022-11-17 11:05:55 +11:00
|
|
|
await delay(1); // to ensure each timestamp is unique.
|
2022-10-28 09:28:16 +11:00
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2022-10-28 09:28:16 +11:00
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
});
|
|
|
|
|
await waku.start();
|
|
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
|
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
|
|
|
|
|
|
|
|
|
const desiredMsgs = 14;
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-10-28 09:28:16 +11:00
|
|
|
await waku.store.queryOrderedCallback(
|
|
|
|
|
[TestDecoder],
|
|
|
|
|
async (msg) => {
|
|
|
|
|
messages.push(msg);
|
|
|
|
|
return messages.length >= desiredMsgs;
|
|
|
|
|
},
|
|
|
|
|
{ pageSize: 7 }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(messages?.length).eq(desiredMsgs);
|
|
|
|
|
});
|
2021-04-07 11:04:30 +10:00
|
|
|
});
|
2022-09-14 13:44:00 +10:00
|
|
|
|
2022-11-15 17:30:35 +05:30
|
|
|
describe("Waku Store, custom pubsub topic", () => {
|
|
|
|
|
const customPubSubTopic = "/waku/2/custom-dapp/proto";
|
2022-12-06 13:18:32 +11:00
|
|
|
let waku: LightNode;
|
2023-05-19 01:28:49 +05:30
|
|
|
let nwaku: NimGoNode;
|
2022-11-15 17:30:35 +05:30
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
|
this.timeout(15_000);
|
2023-05-19 01:28:49 +05:30
|
|
|
nwaku = new NimGoNode(makeLogFileName(this));
|
2022-11-15 17:30:35 +05:30
|
|
|
await nwaku.start({
|
|
|
|
|
store: true,
|
|
|
|
|
topics: customPubSubTopic,
|
2022-12-12 11:08:08 +05:30
|
|
|
relay: true,
|
2022-11-15 17:30:35 +05:30
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(async function () {
|
2023-04-17 10:29:36 +05:30
|
|
|
!!nwaku &&
|
|
|
|
|
nwaku.stop().catch((e) => console.log("Nwaku failed to stop", e));
|
2022-11-15 17:30:35 +05:30
|
|
|
!!waku && waku.stop().catch((e) => console.log("Waku failed to stop", e));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("Generator, custom pubsub topic", async function () {
|
|
|
|
|
this.timeout(15_000);
|
|
|
|
|
|
|
|
|
|
const totalMsgs = 20;
|
|
|
|
|
for (let i = 0; i < totalMsgs; i++) {
|
|
|
|
|
expect(
|
|
|
|
|
await nwaku.sendMessage(
|
2023-05-19 01:28:49 +05:30
|
|
|
NimGoNode.toMessageRpcQuery({
|
2022-11-15 14:28:41 +11:00
|
|
|
payload: new Uint8Array([i]),
|
2022-11-15 17:30:35 +05:30
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
|
}),
|
|
|
|
|
customPubSubTopic
|
|
|
|
|
)
|
|
|
|
|
).to.be.true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 10:51:44 +11:00
|
|
|
waku = await createLightNode({
|
2022-11-15 17:30:35 +05:30
|
|
|
staticNoiseKey: NOISE_KEY_1,
|
|
|
|
|
pubSubTopic: customPubSubTopic,
|
|
|
|
|
});
|
|
|
|
|
await waku.start();
|
|
|
|
|
await waku.dial(await nwaku.getMultiaddrWithId());
|
|
|
|
|
await waitForRemotePeer(waku, [Protocols.Store]);
|
|
|
|
|
|
2022-12-05 17:07:03 +11:00
|
|
|
const messages: IMessage[] = [];
|
2022-11-15 17:30:35 +05:30
|
|
|
let promises: Promise<void>[] = [];
|
|
|
|
|
for await (const msgPromises of waku.store.queryGenerator([TestDecoder])) {
|
|
|
|
|
const _promises = msgPromises.map(async (promise) => {
|
|
|
|
|
const msg = await promise;
|
|
|
|
|
if (msg) {
|
|
|
|
|
messages.push(msg);
|
2023-03-10 16:43:21 +11:00
|
|
|
expect(msg.pubSubTopic).to.eq(customPubSubTopic);
|
2022-11-15 17:30:35 +05:30
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
promises = promises.concat(_promises);
|
|
|
|
|
}
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
|
|
expect(messages?.length).eq(totalMsgs);
|
|
|
|
|
const result = messages?.findIndex((msg) => {
|
2022-11-15 14:28:41 +11:00
|
|
|
return msg.payload![0]! === 0;
|
2022-11-15 17:30:35 +05:30
|
|
|
});
|
|
|
|
|
expect(result).to.not.eq(-1);
|
|
|
|
|
});
|
|
|
|
|
});
|