mirror of https://github.com/waku-org/js-waku.git
Allow passing decryption keys in hex string format
This commit is contained in:
parent
75352abcac
commit
931a414a3c
|
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
### Added
|
### Added
|
||||||
- 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.
|
||||||
|
|
||||||
### 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.
|
||||||
|
|
|
@ -17,6 +17,7 @@ import { InMessage } from 'libp2p-interfaces/src/pubsub';
|
||||||
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';
|
import { SignaturePolicy } from 'libp2p-interfaces/src/pubsub/signature-policy';
|
||||||
import PeerId from 'peer-id';
|
import PeerId from 'peer-id';
|
||||||
|
|
||||||
|
import { hexToBuf } from '../utils';
|
||||||
import { CreateOptions, DefaultPubSubTopic } from '../waku';
|
import { CreateOptions, DefaultPubSubTopic } from '../waku';
|
||||||
import { WakuMessage } from '../waku_message';
|
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
|
* 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
|
* private key for asymmetric encryption or a symmetric key. Waku relay will
|
||||||
* attempt to decrypt messages using both methods.
|
* attempt to decrypt messages using both methods.
|
||||||
|
* Strings must be in hex format.
|
||||||
*/
|
*/
|
||||||
addDecryptionKey(privateKey: Uint8Array): void {
|
addDecryptionKey(key: Uint8Array | string): void {
|
||||||
this.decryptionKeys.add(privateKey);
|
this.decryptionKeys.add(hexToBuf(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a decryption key to attempt decryption of messages received on the
|
* Delete a decryption key to attempt decryption of messages received on the
|
||||||
* given content topic.
|
* given content topic.
|
||||||
|
* Strings must be in hex format.
|
||||||
*/
|
*/
|
||||||
deleteDecryptionKey(privateKey: Uint8Array): void {
|
deleteDecryptionKey(key: Uint8Array | string): void {
|
||||||
this.decryptionKeys.delete(privateKey);
|
this.decryptionKeys.delete(hexToBuf(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -8,6 +8,7 @@ import PeerId from 'peer-id';
|
||||||
|
|
||||||
import { HistoryResponse_Error } from '../../proto/waku/v2/store';
|
import { HistoryResponse_Error } from '../../proto/waku/v2/store';
|
||||||
import { getPeersForProtocol, selectRandomPeer } from '../select_peer';
|
import { getPeersForProtocol, selectRandomPeer } from '../select_peer';
|
||||||
|
import { hexToBuf } from '../utils';
|
||||||
import { DefaultPubSubTopic } from '../waku';
|
import { DefaultPubSubTopic } from '../waku';
|
||||||
import { WakuMessage } from '../waku_message';
|
import { WakuMessage } from '../waku_message';
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ export interface QueryOptions {
|
||||||
pageSize?: number;
|
pageSize?: number;
|
||||||
timeFilter?: TimeFilter;
|
timeFilter?: TimeFilter;
|
||||||
callback?: (messages: WakuMessage[]) => void;
|
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);
|
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[] = [];
|
||||||
|
if (opts.decryptionKeys) {
|
||||||
|
opts.decryptionKeys.forEach((key) => {
|
||||||
|
decryptionKeys.push(hexToBuf(key));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const messages: WakuMessage[] = [];
|
const messages: WakuMessage[] = [];
|
||||||
let cursor = undefined;
|
let cursor = undefined;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
@ -154,10 +162,7 @@ export class WakuStore {
|
||||||
const pageMessages: WakuMessage[] = [];
|
const pageMessages: WakuMessage[] = [];
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
response.messages.map(async (protoMsg) => {
|
response.messages.map(async (protoMsg) => {
|
||||||
const msg = await WakuMessage.decodeProto(
|
const msg = await WakuMessage.decodeProto(protoMsg, decryptionKeys);
|
||||||
protoMsg,
|
|
||||||
opts.decryptionKeys
|
|
||||||
);
|
|
||||||
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
messages.push(msg);
|
messages.push(msg);
|
||||||
|
|
Loading…
Reference in New Issue