js-waku/src/lib/waku_message/version_1.spec.ts
fryorcraken.eth 5eec4564fe
style: typo
2022-08-24 18:43:00 +10:00

88 lines
2.4 KiB
TypeScript

import { expect } from "chai";
import fc from "fast-check";
import { getPublicKey } from "../crypto";
import { bytesToHex } from "../utils";
import {
clearDecode,
clearEncode,
decryptAsymmetric,
decryptSymmetric,
encryptAsymmetric,
encryptSymmetric,
} from "./version_1";
describe("Waku Message Version 1", function () {
it("Sign & Recover", function () {
fc.assert(
fc.asyncProperty(
fc.uint8Array(),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (message, privKey) => {
const enc = await clearEncode(message, privKey);
const res = clearDecode(enc.payload);
const pubKey = getPublicKey(privKey);
expect(res?.payload).deep.equal(
message,
"Payload was not encrypted then decrypted correctly"
);
expect(res?.sig?.publicKey).deep.equal(
pubKey,
"signature Public key was not recovered from encrypted then decrypted signature"
);
expect(enc?.sig?.publicKey).deep.equal(
pubKey,
"Incorrect signature public key was returned when signing the payload"
);
}
)
);
});
it("Asymmetric encrypt & decrypt", async function () {
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1 }),
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
async (message, privKey) => {
const publicKey = getPublicKey(privKey);
const enc = await encryptAsymmetric(message, publicKey);
const res = await decryptAsymmetric(enc, privKey);
expect(res).deep.equal(message);
}
)
);
});
it("Symmetric encrypt & Decrypt", async function () {
await fc.assert(
fc.asyncProperty(
fc.uint8Array(),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
async (message, key) => {
const enc = await encryptSymmetric(message, key);
const res = await decryptSymmetric(enc, key);
expect(res).deep.equal(message);
}
)
);
});
it("Clear encode and decode", async function () {
await fc.assert(
fc.asyncProperty(fc.uint8Array(), async (payload) => {
const enc = await clearEncode(payload);
const dec = clearDecode(enc.payload);
if (!dec?.payload) throw "payload missing";
expect(bytesToHex(dec?.payload)).to.eq(bytesToHex(payload));
})
);
});
});