From 44efd28ac1258d464fcb1fb317dff25827f46302 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 15 Jul 2021 12:25:47 +1000 Subject: [PATCH] Update terminology and docs to cater for both sym and asym encryption --- src/lib/waku_message/index.ts | 24 ++++++++++++++++-------- src/lib/waku_store/index.ts | 7 +++++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lib/waku_message/index.ts b/src/lib/waku_message/index.ts index ea335c171e..5de1e73a7c 100644 --- a/src/lib/waku_message/index.ts +++ b/src/lib/waku_message/index.ts @@ -117,25 +117,33 @@ export class WakuMessage { /** * Decode a byte array into Waku Message. * - * If the payload is encrypted, then `decPrivateKey` is used for decryption. + * @params bytes The message encoded using protobuf as defined in [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/). + * @params decryptionKeys If the payload is encrypted (version = 1), then the + * keys are used to attempt decryption of the message. The passed key can either + * be asymmetric private keys or symmetric keys, both method are tried for each + * key until the message is decrypted or combinations are ran out. */ static async decode( bytes: Uint8Array, - decPrivateKeys?: Uint8Array[] + decryptionKeys?: Uint8Array[] ): Promise { const protoBuf = proto.WakuMessage.decode(Reader.create(bytes)); - return WakuMessage.decodeProto(protoBuf, decPrivateKeys); + return WakuMessage.decodeProto(protoBuf, decryptionKeys); } /** - * Decode a Waku Message Protobuf Object into Waku Message. + * Decode and decrypt Waku Message Protobuf Object into Waku Message. * - * If the payload is encrypted, then `decPrivateKey` is used for decryption. + * @params protoBuf The message to decode and decrypt. + * @params decryptionKeys If the payload is encrypted (version = 1), then the + * keys are used to attempt decryption of the message. The passed key can either + * be asymmetric private keys or symmetric keys, both method are tried for each + * key until the message is decrypted or combinations are ran out. */ static async decodeProto( protoBuf: proto.WakuMessage, - decPrivateKeys?: Uint8Array[] + decryptionKeys?: Uint8Array[] ): Promise { if (protoBuf.payload === undefined) { dbg('Payload is undefined'); @@ -146,14 +154,14 @@ export class WakuMessage { let signaturePublicKey; let signature; if (protoBuf.version === 1 && protoBuf.payload) { - if (decPrivateKeys === undefined) { + if (decryptionKeys === undefined) { dbg('Payload is encrypted but no private keys have been provided.'); return; } // Returns a bunch of `undefined` and hopefully one decrypted result const allResults = await Promise.all( - decPrivateKeys.map(async (privateKey) => { + decryptionKeys.map(async (privateKey) => { try { return await version_1.decryptSymmetric(payload, privateKey); } catch (e) { diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index c1b4762538..0d757daab9 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -37,7 +37,7 @@ export interface QueryOptions { direction?: Direction; pageSize?: number; callback?: (messages: WakuMessage[]) => void; - decryptionPrivateKeys?: Uint8Array[]; + decryptionKeys?: Uint8Array[]; } /** @@ -64,6 +64,9 @@ export class WakuStore { * @param options.pubsubTopic The pubsub topic to pass to the query. Defaults * to the value set at creation. See [Waku v2 Topic Usage Recommendations](https://rfc.vac.dev/spec/23/). * @param options.callback Callback called on page of stored messages as they are retrieved + * @param options.decryptionKeys Keys that will be used to decrypt messages. + * It can be Asymmetric Private Keys and Symmetric Keys in the same array, all keys will be tried with both + * methods. * @throws If not able to reach the peer to query. */ async queryHistory(options: QueryOptions): Promise { @@ -129,7 +132,7 @@ export class WakuStore { response.messages.map(async (protoMsg) => { const msg = await WakuMessage.decodeProto( protoMsg, - opts.decryptionPrivateKeys + opts.decryptionKeys ); if (msg) {