fix: ensure the content topics that needs to be decrypted are passed

This commit is contained in:
fryorcraken.eth 2022-08-24 17:51:06 +10:00
parent 5ddca918ca
commit 79146711c5
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 13 additions and 14 deletions

View File

@ -38,6 +38,13 @@ export interface Options {
sigPrivKey?: Uint8Array; sigPrivKey?: Uint8Array;
} }
// TODO: Use this in Options
export interface DecryptionParams {
key: Uint8Array;
method?: DecryptionMethod;
contentTopics?: string[];
}
export class WakuMessage { export class WakuMessage {
private constructor( private constructor(
public proto: proto.WakuMessage, public proto: proto.WakuMessage,
@ -124,33 +131,25 @@ export class WakuMessage {
*/ */
static async decode( static async decode(
bytes: Uint8Array, bytes: Uint8Array,
decryptionKeys?: Array<{ decryptionParams?: DecryptionParams[]
key: Uint8Array;
method?: DecryptionMethod;
contentTopic?: string[];
}>
): Promise<WakuMessage | undefined> { ): Promise<WakuMessage | undefined> {
const protoBuf = proto.WakuMessage.decode(bytes); const protoBuf = proto.WakuMessage.decode(bytes);
return WakuMessage.decodeProto(protoBuf, decryptionKeys); return WakuMessage.decodeProto(protoBuf, decryptionParams);
} }
/** /**
* Decode and decrypt Waku Message Protobuf Object into Waku Message. * Decode and decrypt Waku Message Protobuf Object into Waku Message.
* *
* @params protoBuf The message to decode and decrypt. * @params protoBuf The message to decode and decrypt.
* @params decryptionKeys If the payload is encrypted (version = 1), then the * @params decryptionParams If the payload is encrypted (version = 1), then the
* keys are used to attempt decryption of the message. The passed key can either * 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 * be asymmetric private keys or symmetric keys, both method are tried for each
* key until the message is decrypted or combinations are run out. * key until the message is decrypted or combinations are run out.
*/ */
static async decodeProto( static async decodeProto(
protoBuf: proto.WakuMessage, protoBuf: proto.WakuMessage,
decryptionKeys?: Array<{ decryptionParams?: DecryptionParams[]
key: Uint8Array;
method?: DecryptionMethod;
contentTopics?: string[];
}>
): Promise<WakuMessage | undefined> { ): Promise<WakuMessage | undefined> {
if (protoBuf.payload === undefined) { if (protoBuf.payload === undefined) {
dbg("Payload is undefined"); dbg("Payload is undefined");
@ -161,14 +160,14 @@ export class WakuMessage {
let signaturePublicKey; let signaturePublicKey;
let signature; let signature;
if (protoBuf.version === 1 && protoBuf.payload) { if (protoBuf.version === 1 && protoBuf.payload) {
if (decryptionKeys === undefined) { if (decryptionParams === undefined) {
dbg("Payload is encrypted but no private keys have been provided."); dbg("Payload is encrypted but no private keys have been provided.");
return; return;
} }
// Returns a bunch of `undefined` and hopefully one decrypted result // Returns a bunch of `undefined` and hopefully one decrypted result
const allResults = await Promise.all( const allResults = await Promise.all(
decryptionKeys.map(async ({ key, method, contentTopics }) => { decryptionParams.map(async ({ key, method, contentTopics }) => {
if ( if (
!contentTopics || !contentTopics ||
(protoBuf.contentTopic && (protoBuf.contentTopic &&