Skip to content

Commit

Permalink
feat: DecryptionParams may be passed when using queryHistory
Browse files Browse the repository at this point in the history
  • Loading branch information
fryorcraken committed Sep 6, 2022
1 parent f44e138 commit 8f71f18
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Simple connection management that selects the most recent connection for store, light push and filter requests.

### Changed

- **breaking**: `DecryptionParams` may be passed when using `queryHistory` instead of just keys.

## [0.25.0] - 2022-09-5

### Changed
Expand Down
4 changes: 2 additions & 2 deletions src/lib/waku_filter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class WakuFilter {
return;
}

const decryptionKeys = Array.from(this.decryptionKeys).map(
const decryptionParams = Array.from(this.decryptionKeys).map(
([key, { method, contentTopics }]) => {
return {
key,
Expand All @@ -178,7 +178,7 @@ export class WakuFilter {
);

for (const message of messages) {
const decoded = await WakuMessage.decodeProto(message, decryptionKeys);
const decoded = await WakuMessage.decodeProto(message, decryptionParams);
if (!decoded) {
log("Not able to decode message");
continue;
Expand Down
1 change: 0 additions & 1 deletion src/lib/waku_message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface Options {
sigPrivKey?: Uint8Array;
}

// TODO: Use this in Options
export interface DecryptionParams {
key: Uint8Array;
method?: DecryptionMethod;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/waku_relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class WakuRelay extends GossipSub {
"gossipsub:message",
(event: CustomEvent<GossipsubMessage>) => {
if (event.detail.msg.topic === pubSubTopic) {
const decryptionKeys = Array.from(this.decryptionKeys).map(
const decryptionParams = Array.from(this.decryptionKeys).map(
([key, { method, contentTopics }]) => {
return {
key,
Expand All @@ -195,7 +195,7 @@ export class WakuRelay extends GossipSub {
);

dbg(`Message received on ${pubSubTopic}`);
WakuMessage.decode(event.detail.msg.data, decryptionKeys)
WakuMessage.decode(event.detail.msg.data, decryptionParams)
.then((wakuMsg) => {
if (!wakuMsg) {
dbg("Failed to decode Waku Message");
Expand Down
4 changes: 2 additions & 2 deletions src/lib/waku_store/index.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ describe("Waku Store", () => {

dbg("Retrieve messages from store");
const messages = await waku2.store.queryHistory([], {
decryptionKeys: [privateKey],
decryptionParams: [{ key: privateKey }],
});

expect(messages?.length).eq(3);
Expand Down Expand Up @@ -411,7 +411,7 @@ describe("Waku Store", () => {

dbg("Retrieve messages from store");
const messages = await waku2.store.queryHistory([], {
decryptionKeys: [privateKey],
decryptionKeys: [{ key: privateKey }],
});

expect(messages?.length).eq(3);
Expand Down
38 changes: 18 additions & 20 deletions src/lib/waku_store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { DefaultPubSubTopic, StoreCodecs } from "../constants";
import { selectConnection } from "../select_connection";
import { getPeersForProtocol, selectRandomPeer } from "../select_peer";
import { hexToBytes } from "../utils";
import { DecryptionMethod, WakuMessage } from "../waku_message";
import {
DecryptionMethod,
DecryptionParams,
WakuMessage,
} from "../waku_message";

import { HistoryRPC, PageDirection } from "./history_rpc";

Expand Down Expand Up @@ -91,7 +95,7 @@ export interface QueryOptions {
* It can be Asymmetric Private Keys and Symmetric Keys in the same array,
* all keys will be tried with both methods.
*/
decryptionKeys?: Array<Uint8Array | string>;
decryptionParams?: DecryptionParams[];
}

/**
Expand Down Expand Up @@ -176,26 +180,20 @@ export class WakuStore {

if (!connection) throw "Failed to get a connection to the peer";

const decryptionKeys = Array.from(this.decryptionKeys).map(
([key, { method, contentTopics }]) => {
return {
key,
method,
contentTopics,
};
}
);
const decryptionParams: DecryptionParams[] = Array.from(
this.decryptionKeys
).map(([key, { method, contentTopics }]) => {
return {
key,
method,
contentTopics,
};
});

// Add the decryption keys passed to this function against the
// content topics also passed to this function.
if (opts.decryptionKeys) {
opts.decryptionKeys.forEach((key) => {
decryptionKeys.push({
key: hexToBytes(key),
contentTopics: contentTopics.length ? contentTopics : undefined,
method: undefined,
});
});
if (opts.decryptionParams) {
decryptionParams.concat(opts.decryptionParams);
}

const messages: WakuMessage[] = [];
Expand Down Expand Up @@ -245,7 +243,7 @@ export class WakuStore {
const pageMessages: WakuMessage[] = [];
await Promise.all(
response.messages.map(async (protoMsg) => {
const msg = await WakuMessage.decodeProto(protoMsg, decryptionKeys);
const msg = await WakuMessage.decodeProto(protoMsg, decryptionParams);

if (msg) {
messages.push(msg);
Expand Down

0 comments on commit 8f71f18

Please sign in to comment.