2022-11-23 05:59:15 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import fc from "fast-check";
|
|
|
|
|
|
|
|
import { getPublicKey } from "./crypto/index.js";
|
2022-12-05 04:14:17 +00:00
|
|
|
import { createDecoder, createEncoder } from "./ecies.js";
|
2022-11-23 05:59:15 +00:00
|
|
|
|
|
|
|
const TestContentTopic = "/test/1/waku-message/utf8";
|
2023-03-10 05:43:21 +00:00
|
|
|
const TestPubSubTopic = "/test/pubsub/topic";
|
2022-11-23 05:59:15 +00:00
|
|
|
|
|
|
|
describe("Ecies Encryption", function () {
|
|
|
|
it("Round trip binary encryption [ecies, no signature]", async function () {
|
|
|
|
await fc.assert(
|
|
|
|
fc.asyncProperty(
|
|
|
|
fc.uint8Array({ minLength: 1 }),
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
|
|
|
async (payload, privateKey) => {
|
|
|
|
const publicKey = getPublicKey(privateKey);
|
|
|
|
|
2023-02-02 06:07:28 +00:00
|
|
|
const encoder = createEncoder({
|
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
publicKey,
|
|
|
|
});
|
2022-11-23 05:59:15 +00:00
|
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
|
2022-12-05 04:14:17 +00:00
|
|
|
const decoder = createDecoder(TestContentTopic, privateKey);
|
2022-11-23 05:59:15 +00:00
|
|
|
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
|
|
|
if (!protoResult) throw "Failed to proto decode";
|
2023-03-10 05:43:21 +00:00
|
|
|
const result = await decoder.fromProtoObj(
|
|
|
|
TestPubSubTopic,
|
|
|
|
protoResult
|
|
|
|
);
|
2022-11-23 05:59:15 +00:00
|
|
|
if (!result) throw "Failed to decode";
|
|
|
|
|
|
|
|
expect(result.contentTopic).to.equal(TestContentTopic);
|
2023-03-10 05:43:21 +00:00
|
|
|
expect(result.pubSubTopic).to.equal(TestPubSubTopic);
|
2022-11-23 05:59:15 +00:00
|
|
|
expect(result.version).to.equal(1);
|
|
|
|
expect(result?.payload).to.deep.equal(payload);
|
|
|
|
expect(result.signature).to.be.undefined;
|
|
|
|
expect(result.signaturePublicKey).to.be.undefined;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("R trip binary encryption [ecies, signature]", async function () {
|
|
|
|
this.timeout(4000);
|
|
|
|
|
|
|
|
await fc.assert(
|
|
|
|
fc.asyncProperty(
|
|
|
|
fc.uint8Array({ minLength: 1 }),
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
|
|
|
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
|
|
|
async (payload, alicePrivateKey, bobPrivateKey) => {
|
|
|
|
const alicePublicKey = getPublicKey(alicePrivateKey);
|
|
|
|
const bobPublicKey = getPublicKey(bobPrivateKey);
|
|
|
|
|
2023-02-02 06:07:28 +00:00
|
|
|
const encoder = createEncoder({
|
|
|
|
contentTopic: TestContentTopic,
|
|
|
|
publicKey: bobPublicKey,
|
|
|
|
sigPrivKey: alicePrivateKey,
|
|
|
|
});
|
2022-11-23 05:59:15 +00:00
|
|
|
const bytes = await encoder.toWire({ payload });
|
|
|
|
|
2022-12-05 04:14:17 +00:00
|
|
|
const decoder = createDecoder(TestContentTopic, bobPrivateKey);
|
2022-11-23 05:59:15 +00:00
|
|
|
const protoResult = await decoder.fromWireToProtoObj(bytes!);
|
|
|
|
if (!protoResult) throw "Failed to proto decode";
|
2023-03-10 05:43:21 +00:00
|
|
|
const result = await decoder.fromProtoObj(
|
|
|
|
TestPubSubTopic,
|
|
|
|
protoResult
|
|
|
|
);
|
2022-11-23 05:59:15 +00:00
|
|
|
if (!result) throw "Failed to decode";
|
|
|
|
|
|
|
|
expect(result.contentTopic).to.equal(TestContentTopic);
|
2023-03-10 05:43:21 +00:00
|
|
|
expect(result.pubSubTopic).to.equal(TestPubSubTopic);
|
2022-11-23 05:59:15 +00:00
|
|
|
expect(result.version).to.equal(1);
|
|
|
|
expect(result?.payload).to.deep.equal(payload);
|
|
|
|
expect(result.signature).to.not.be.undefined;
|
|
|
|
expect(result.signaturePublicKey).to.deep.eq(alicePublicKey);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|