Add/delete decryption keys

This commit is contained in:
Nicholas Molnar 2022-05-16 08:57:26 -07:00 committed by Franck Royer
parent 5faa186b34
commit c77b74c10c
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 27 additions and 0 deletions

View File

@ -310,6 +310,7 @@ export class Waku {
): void {
this.relay.addDecryptionKey(key, options);
this.store.addDecryptionKey(key, options);
this.filter.addDecryptionKey(key, options);
}
/**
@ -321,6 +322,7 @@ export class Waku {
deleteDecryptionKey(key: Uint8Array | string): void {
this.relay.deleteDecryptionKey(key);
this.store.deleteDecryptionKey(key);
this.filter.deleteDecryptionKey(key);
}
/**

View File

@ -6,6 +6,7 @@ import { Peer, PeerId } from "libp2p/src/peer-store";
import { WakuMessage as WakuMessageProto } from "../../proto/waku/v2/message";
import { getPeersForProtocol, selectRandomPeer } from "../select_peer";
import { hexToBytes } from "../utils";
import { DefaultPubSubTopic } from "../waku";
import { DecryptionMethod, WakuMessage } from "../waku_message/index";
@ -170,6 +171,30 @@ export class WakuFilter {
return peer;
}
/**
* Register a decryption key to attempt decryption of messages received in any
* subsequent [[subscribe]] 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,
options?: { method?: DecryptionMethod; contentTopics?: string[] }
): void {
this.decryptionKeys.set(hexToBytes(key), options ?? {});
}
/**cursorV2Beta4
* Delete a decryption key so that it cannot be used in future [[subscribe]] calls
*
* Strings must be in hex format.
*/
deleteDecryptionKey(key: Uint8Array | string): void {
this.decryptionKeys.delete(hexToBytes(key));
}
get peers(): AsyncIterable<Peer> {
return getPeersForProtocol(this.libp2p, [FilterCodec]);
}