diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d24360ca7..ed2f4974ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Examples (eth-pm): Encrypt Public Key Messages using symmetric encryption. - Guides: Encrypt messages using Waku Message Version 1. - Allow passing decryption keys in hex string format. +- Allow passing decryption keys to `WakuStore` instance to avoid having to pass them at every `queryHistory` call. ### Changed - **Breaking**: Moved `startTime` and `endTime` for history queries to a `timeFilter` property as both or neither must be passed; passing only one parameter is not supported. diff --git a/src/lib/waku_store/index.spec.ts b/src/lib/waku_store/index.spec.ts index 0424de60ce..b11daf570f 100644 --- a/src/lib/waku_store/index.spec.ts +++ b/src/lib/waku_store/index.spec.ts @@ -236,9 +236,11 @@ describe('Waku Store', () => { storePeers = waku2.store.peers; } + waku2.store.addDecryptionKey(symKey); + dbg('Retrieve messages from store'); const messages = await waku2.store.queryHistory([], { - decryptionKeys: [privateKey, symKey], + decryptionKeys: [privateKey], }); expect(messages?.length).eq(3); diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index 40fadb83e5..ed3d7ca513 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -52,6 +52,7 @@ export interface QueryOptions { */ export class WakuStore { pubSubTopic: string; + public decryptionKeys: Set; constructor(public libp2p: Libp2p, options?: CreateOptions) { if (options?.pubSubTopic) { @@ -59,6 +60,8 @@ export class WakuStore { } else { this.pubSubTopic = DefaultPubSubTopic; } + + this.decryptionKeys = new Set(); } /** @@ -115,7 +118,7 @@ export class WakuStore { const connection = this.libp2p.connectionManager.get(peer.id); if (!connection) throw 'Failed to get a connection to the peer'; - const decryptionKeys: Uint8Array[] = []; + const decryptionKeys = Array.from(this.decryptionKeys.values()); if (opts.decryptionKeys) { opts.decryptionKeys.forEach((key) => { decryptionKeys.push(hexToBuf(key)); @@ -198,6 +201,28 @@ export class WakuStore { } } + /** + * Register a decryption key to attempt decryption of messages received in any + * subsequent [[queryHistory]] call. This can either be a private key for + * asymmetric encryption or a symmetric key. [[WakuStore]] will attempt to + * decrypt messages using both methods. + * + * Strings must be in hex format. + */ + addDecryptionKey(key: Uint8Array | string): void { + this.decryptionKeys.add(hexToBuf(key)); + } + + /** + * Delete a decryption key that was used to attempt decryption of messages + * received in subsequent [[queryHistory]] calls. + * + * Strings must be in hex format. + */ + deleteDecryptionKey(key: Uint8Array | string): void { + this.decryptionKeys.delete(hexToBuf(key)); + } + /** * Returns known peers from the address book (`libp2p.peerStore`) that support * store protocol. Waku may or may not be currently connected to these peers.