Allow passing decryption keys in hex string format

This commit is contained in:
Franck Royer 2021-09-01 12:48:51 +10:00
parent 75352abcac
commit 931a414a3c
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 18 additions and 9 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- 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.
### 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.

View File

@ -17,6 +17,7 @@ import { InMessage } from 'libp2p-interfaces/src/pubsub';
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';
import PeerId from 'peer-id';
import { hexToBuf } from '../utils';
import { CreateOptions, DefaultPubSubTopic } from '../waku';
import { WakuMessage } from '../waku_message';
@ -128,17 +129,19 @@ export class WakuRelay extends Gossipsub {
* of messages received on 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.
* Strings must be in hex format.
*/
addDecryptionKey(privateKey: Uint8Array): void {
this.decryptionKeys.add(privateKey);
addDecryptionKey(key: Uint8Array | string): void {
this.decryptionKeys.add(hexToBuf(key));
}
/**
* Delete a decryption key to attempt decryption of messages received on the
* given content topic.
* Strings must be in hex format.
*/
deleteDecryptionKey(privateKey: Uint8Array): void {
this.decryptionKeys.delete(privateKey);
deleteDecryptionKey(key: Uint8Array | string): void {
this.decryptionKeys.delete(hexToBuf(key));
}
/**

View File

@ -8,6 +8,7 @@ import PeerId from 'peer-id';
import { HistoryResponse_Error } from '../../proto/waku/v2/store';
import { getPeersForProtocol, selectRandomPeer } from '../select_peer';
import { hexToBuf } from '../utils';
import { DefaultPubSubTopic } from '../waku';
import { WakuMessage } from '../waku_message';
@ -43,7 +44,7 @@ export interface QueryOptions {
pageSize?: number;
timeFilter?: TimeFilter;
callback?: (messages: WakuMessage[]) => void;
decryptionKeys?: Uint8Array[];
decryptionKeys?: Array<Uint8Array | string>;
}
/**
@ -114,6 +115,13 @@ 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[] = [];
if (opts.decryptionKeys) {
opts.decryptionKeys.forEach((key) => {
decryptionKeys.push(hexToBuf(key));
});
}
const messages: WakuMessage[] = [];
let cursor = undefined;
while (true) {
@ -154,10 +162,7 @@ export class WakuStore {
const pageMessages: WakuMessage[] = [];
await Promise.all(
response.messages.map(async (protoMsg) => {
const msg = await WakuMessage.decodeProto(
protoMsg,
opts.decryptionKeys
);
const msg = await WakuMessage.decodeProto(protoMsg, decryptionKeys);
if (msg) {
messages.push(msg);