Test symmetric encryption with nim-waku using relay

This commit is contained in:
Franck Royer 2021-07-15 12:12:43 +10:00
parent d12430e19b
commit 13c8a0527b
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 104 additions and 3 deletions

View File

@ -105,5 +105,59 @@ describe('Waku Message: Node only', function () {
expect(msgs[0].contentTopic).to.equal(message.contentTopic); expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText); expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText);
}); });
it('JS decrypts nim message [symmetric, no signature]', async function () {
this.timeout(10000);
await delay(200);
const messageText = 'Here is a message encrypted in a symmetric manner.';
const message: WakuRelayMessage = {
contentTopic: DefaultContentTopic,
payload: Buffer.from(messageText, 'utf-8').toString('hex'),
};
const symKey = generatePrivateKey();
waku.relay.addDecryptionPrivateKey(symKey);
const receivedMsgPromise: Promise<WakuMessage> = new Promise(
(resolve) => {
waku.relay.addObserver(resolve);
}
);
dbg('Post message');
await nimWaku.postSymmetricMessage(message, symKey);
const receivedMsg = await receivedMsgPromise;
expect(receivedMsg.contentTopic).to.eq(message.contentTopic);
expect(receivedMsg.version).to.eq(1);
expect(receivedMsg.payloadAsUtf8).to.eq(messageText);
});
it('Js encrypts message for nim [symmetric, no signature]', async function () {
this.timeout(5000);
const symKey = await nimWaku.getSymmetricKey();
const messageText =
'This is a message I am going to encrypt with a symmetric key';
const message = await WakuMessage.fromUtf8String(messageText, {
symKey: symKey,
});
await waku.relay.send(message);
let msgs: WakuRelayMessage[] = [];
while (msgs.length === 0) {
await delay(200);
msgs = await nimWaku.getSymmetricMessages(symKey);
}
expect(msgs[0].contentTopic).to.equal(message.contentTopic);
expect(hexToBuf(msgs[0].payload).toString('utf-8')).to.equal(messageText);
});
}); });
}); });

View File

@ -172,14 +172,19 @@ export async function decryptSymmetric(
} }
/** /**
* Generate a new private key * Generate a new key. Can be used as a private key for Asymmetric encryption
* or a key for symmetric encryption.
*
* If using Asymmetric encryption, use {@link getPublicKey} to get the
* corresponding Public Key.
*/ */
export function generatePrivateKey(): Uint8Array { export function generatePrivateKey(): Uint8Array {
return randomBytes(32); return randomBytes(32);
} }
/** /**
* Return the public key for the given private key * Return the public key for the given private key, to be used for asymmetric
* encryption.
*/ */
export function getPublicKey(privateKey: Uint8Array | Buffer): Uint8Array { export function getPublicKey(privateKey: Uint8Array | Buffer): Uint8Array {
return secp256k1.publicKeyCreate(privateKey, false); return secp256k1.publicKeyCreate(privateKey, false);

View File

@ -125,7 +125,9 @@ export class WakuRelay extends Gossipsub {
/** /**
* Register a decryption private key to attempt decryption of messages of * Register a decryption private key to attempt decryption of messages of
* the given content topic. * the given content topic. This can either be a private key for asymmetric
* encryption or a symmetric key. Waku relay will attempt to decrypt messages
* using both methods.
*/ */
addDecryptionPrivateKey(privateKey: Uint8Array): void { addDecryptionPrivateKey(privateKey: Uint8Array): void {
this.decPrivateKeys.add(privateKey); this.decPrivateKeys.add(privateKey);

View File

@ -12,6 +12,7 @@ import debug from 'debug';
import { Multiaddr, multiaddr } from 'multiaddr'; import { Multiaddr, multiaddr } from 'multiaddr';
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import { hexToBuf } from '../lib/utils';
import { WakuMessage } from '../lib/waku_message'; import { WakuMessage } from '../lib/waku_message';
import { DefaultPubsubTopic } from '../lib/waku_relay'; import { DefaultPubsubTopic } from '../lib/waku_relay';
import * as proto from '../proto/waku/v2/message'; import * as proto from '../proto/waku/v2/message';
@ -251,6 +252,45 @@ export class NimWaku {
); );
} }
async getSymmetricKey(): Promise<Buffer> {
this.checkProcess();
return this.rpcCall<string>(
'get_waku_v2_private_v1_symmetric_key',
[]
).then(hexToBuf);
}
async postSymmetricMessage(
message: WakuRelayMessage,
symKey: Uint8Array,
pubsubTopic?: string
): Promise<boolean> {
this.checkProcess();
if (!message.payload) {
throw 'Attempting to send empty message';
}
return this.rpcCall<boolean>('post_waku_v2_private_v1_symmetric_message', [
pubsubTopic ? pubsubTopic : DefaultPubsubTopic,
message,
'0x' + bufToHex(symKey),
]);
}
async getSymmetricMessages(
symKey: Uint8Array,
pubsubTopic?: string
): Promise<WakuRelayMessage[]> {
this.checkProcess();
return await this.rpcCall<WakuRelayMessage[]>(
'get_waku_v2_private_v1_symmetric_messages',
[pubsubTopic ? pubsubTopic : DefaultPubsubTopic, '0x' + bufToHex(symKey)]
);
}
async getPeerId(): Promise<PeerId> { async getPeerId(): Promise<PeerId> {
return await this.setPeerId().then((res) => res.peerId); return await this.setPeerId().then((res) => res.peerId);
} }