js-waku/src/lib/waku_message/version_1.browser.spec.ts

75 lines
2.0 KiB
TypeScript
Raw Normal View History

import { expect } from 'chai';
import fc from 'fast-check';
2021-07-06 15:12:54 +10:00
import {
clearDecode,
clearEncode,
decryptAsymmetric,
2021-07-13 12:32:57 +10:00
decryptSymmetric,
2021-07-06 15:12:54 +10:00
encryptAsymmetric,
2021-07-13 12:32:57 +10:00
encryptSymmetric,
2021-07-06 15:12:54 +10:00
getPublicKey,
} from './version_1';
describe('Waku Message Version 1', function () {
it('Sign & Recover', function () {
fc.assert(
fc.property(
fc.uint8Array(),
fc.uint8Array({ minLength: 32, maxLength: 32 }),
(message, privKey) => {
2021-07-06 15:12:54 +10:00
const enc = clearEncode(message, privKey);
2021-07-07 11:23:56 +10:00
const res = clearDecode(enc.payload);
2021-07-06 15:12:54 +10:00
const pubKey = getPublicKey(privKey);
2021-07-07 11:23:56 +10:00
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'
);
}
)
);
});
2021-07-06 15:12:54 +10:00
it('Asymmetric encrypt & Decrypt', async function () {
await fc.assert(
fc.asyncProperty(
2021-07-07 11:23:56 +10:00
fc.uint8Array({ minLength: 1 }),
2021-07-06 15:12:54 +10:00
fc.uint8Array({ 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);
}
)
);
});
2021-07-13 12:32:57 +10:00
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);
}
)
);
});
});