test: Symmetric encryption with waku store

This commit is contained in:
Franck Royer 2021-07-15 12:26:05 +10:00
parent 44efd28ac1
commit 0b3f1a33c2
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 29 additions and 17 deletions

View File

@ -146,32 +146,42 @@ describe('Waku Store', () => {
expect(result).to.not.eq(-1); expect(result).to.not.eq(-1);
}); });
it('Retrieves history with asymmetric encrypted messages', async function () { it('Retrieves history with asymmetric & symmetric encrypted messages', async function () {
this.timeout(10_000); this.timeout(10_000);
nimWaku = new NimWaku(makeLogFileName(this)); nimWaku = new NimWaku(makeLogFileName(this));
await nimWaku.start({ persistMessages: true, lightpush: true }); await nimWaku.start({ persistMessages: true, lightpush: true });
const encryptedMessageText = 'This message is encrypted for me'; const encryptedAsymmetricMessageText =
'This message is encrypted for me using asymmetric';
const encryptedSymmetricMessageText =
'This message is encrypted for me using symmetric encryption';
const clearMessageText = const clearMessageText =
'This is a clear text message for everyone to read'; 'This is a clear text message for everyone to read';
const otherEncMessageText = const otherEncMessageText =
'This message is not for and I must not be able to read it'; 'This message is not for and I must not be able to read it';
const privateKey = generatePrivateKey(); const privateKey = generatePrivateKey();
const symKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey); const publicKey = getPublicKey(privateKey);
const [encryptedMessage, clearMessage, otherEncMessage] = await Promise.all( const [
[ encryptedAsymmetricMessage,
WakuMessage.fromUtf8String(encryptedMessageText, { encryptedSymmetricMessage,
clearMessage,
otherEncMessage,
] = await Promise.all([
WakuMessage.fromUtf8String(encryptedAsymmetricMessageText, {
encPublicKey: publicKey, encPublicKey: publicKey,
}), }),
WakuMessage.fromUtf8String(encryptedSymmetricMessageText, {
symKey: symKey,
}),
WakuMessage.fromUtf8String(clearMessageText), WakuMessage.fromUtf8String(clearMessageText),
WakuMessage.fromUtf8String(otherEncMessageText, { WakuMessage.fromUtf8String(otherEncMessageText, {
encPublicKey: getPublicKey(generatePrivateKey()), encPublicKey: getPublicKey(generatePrivateKey()),
}), }),
] ]);
);
dbg('Messages have been encrypted'); dbg('Messages have been encrypted');
@ -204,7 +214,8 @@ describe('Waku Store', () => {
dbg('Sending messages using light push'); dbg('Sending messages using light push');
await Promise.all([ await Promise.all([
await waku1.lightPush.push(encryptedMessage), waku1.lightPush.push(encryptedAsymmetricMessage),
waku1.lightPush.push(encryptedSymmetricMessage),
waku1.lightPush.push(otherEncMessage), waku1.lightPush.push(otherEncMessage),
waku1.lightPush.push(clearMessage), waku1.lightPush.push(clearMessage),
]); ]);
@ -218,13 +229,14 @@ describe('Waku Store', () => {
dbg('Retrieve messages from store'); dbg('Retrieve messages from store');
const messages = await waku2.store.queryHistory({ const messages = await waku2.store.queryHistory({
contentTopics: [], contentTopics: [],
decryptionPrivateKeys: [privateKey], decryptionKeys: [privateKey, symKey],
}); });
expect(messages?.length).eq(2); expect(messages?.length).eq(3);
if (!messages) throw 'Length was tested'; if (!messages) throw 'Length was tested';
expect(messages[0].payloadAsUtf8).to.eq(clearMessageText); expect(messages[0].payloadAsUtf8).to.eq(clearMessageText);
expect(messages[1].payloadAsUtf8).to.eq(encryptedMessageText); expect(messages[1].payloadAsUtf8).to.eq(encryptedSymmetricMessageText);
expect(messages[2].payloadAsUtf8).to.eq(encryptedAsymmetricMessageText);
await Promise.all([waku1.stop(), waku2.stop()]); await Promise.all([waku1.stop(), waku2.stop()]);
}); });