Allow passing decryption keys to `WakuStore` instance

To avoid having to pass them at every `queryHistory` call.
This commit is contained in:
Franck Royer 2021-09-01 13:55:46 +10:00
parent f4cae60ef8
commit 911ce5bab7
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 30 additions and 2 deletions

View File

@ -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. - Examples (eth-pm): Encrypt Public Key Messages using symmetric encryption.
- Guides: Encrypt messages using Waku Message Version 1. - Guides: Encrypt messages using Waku Message Version 1.
- Allow passing decryption keys in hex string format. - 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 ### 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. - **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.

View File

@ -236,9 +236,11 @@ describe('Waku Store', () => {
storePeers = waku2.store.peers; storePeers = waku2.store.peers;
} }
waku2.store.addDecryptionKey(symKey);
dbg('Retrieve messages from store'); dbg('Retrieve messages from store');
const messages = await waku2.store.queryHistory([], { const messages = await waku2.store.queryHistory([], {
decryptionKeys: [privateKey, symKey], decryptionKeys: [privateKey],
}); });
expect(messages?.length).eq(3); expect(messages?.length).eq(3);

View File

@ -52,6 +52,7 @@ export interface QueryOptions {
*/ */
export class WakuStore { export class WakuStore {
pubSubTopic: string; pubSubTopic: string;
public decryptionKeys: Set<Uint8Array>;
constructor(public libp2p: Libp2p, options?: CreateOptions) { constructor(public libp2p: Libp2p, options?: CreateOptions) {
if (options?.pubSubTopic) { if (options?.pubSubTopic) {
@ -59,6 +60,8 @@ export class WakuStore {
} else { } else {
this.pubSubTopic = DefaultPubSubTopic; this.pubSubTopic = DefaultPubSubTopic;
} }
this.decryptionKeys = new Set();
} }
/** /**
@ -115,7 +118,7 @@ export class WakuStore {
const connection = this.libp2p.connectionManager.get(peer.id); const connection = this.libp2p.connectionManager.get(peer.id);
if (!connection) throw 'Failed to get a connection to the peer'; if (!connection) throw 'Failed to get a connection to the peer';
const decryptionKeys: Uint8Array[] = []; const decryptionKeys = Array.from(this.decryptionKeys.values());
if (opts.decryptionKeys) { if (opts.decryptionKeys) {
opts.decryptionKeys.forEach((key) => { opts.decryptionKeys.forEach((key) => {
decryptionKeys.push(hexToBuf(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 * Returns known peers from the address book (`libp2p.peerStore`) that support
* store protocol. Waku may or may not be currently connected to these peers. * store protocol. Waku may or may not be currently connected to these peers.