@waku/message-encryption 0.0.24-4c5a8a9.0 → 0.0.24-7eb3375.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/crypto.js +1 -0
- package/bundle/ecies-137fdfde.js +124 -0
- package/bundle/ecies.js +3 -1
- package/bundle/{index-62691783.js → encryption-d94ff773.js} +12872 -17157
- package/bundle/index-268375db.js +17 -0
- package/bundle/index.js +5 -1
- package/bundle/symmetric-58d457ca.js +2836 -0
- package/bundle/symmetric-d25c7b7a.js +124 -0
- package/bundle/symmetric.js +3 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/crypto/ecies.js +1 -1
- package/dist/crypto/index.d.ts +3 -29
- package/dist/crypto/index.js +3 -59
- package/dist/crypto/index.js.map +1 -1
- package/dist/crypto/symmetric.js +2 -2
- package/dist/crypto/symmetric.js.map +1 -1
- package/dist/crypto/utils.d.ts +29 -0
- package/dist/crypto/utils.js +60 -0
- package/dist/crypto/utils.js.map +1 -0
- package/dist/ecies.d.ts +8 -9
- package/dist/ecies.js +7 -6
- package/dist/ecies.js.map +1 -1
- package/dist/{waku_payload.d.ts → encryption.d.ts} +5 -1
- package/dist/{waku_payload.js → encryption.js} +3 -5
- package/dist/encryption.js.map +1 -0
- package/dist/index.d.ts +1 -6
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/{constants.d.ts → misc.d.ts} +2 -0
- package/dist/{constants.js → misc.js} +3 -1
- package/dist/misc.js.map +1 -0
- package/dist/symmetric.d.ts +8 -8
- package/dist/symmetric.js +7 -6
- package/dist/symmetric.js.map +1 -1
- package/package.json +1 -1
- package/src/crypto/ecies.ts +1 -1
- package/src/crypto/index.ts +3 -76
- package/src/crypto/symmetric.ts +2 -2
- package/src/crypto/utils.ts +76 -0
- package/src/ecies.ts +18 -17
- package/src/{waku_payload.ts → encryption.ts} +13 -6
- package/src/index.ts +1 -9
- package/src/{constants.ts → misc.ts} +4 -0
- package/src/symmetric.ts +16 -11
- package/dist/constants.js.map +0 -1
- package/dist/waku_payload.js.map +0 -1
@@ -0,0 +1,60 @@
|
|
1
|
+
import nodeCrypto from "crypto";
|
2
|
+
import * as secp from "@noble/secp256k1";
|
3
|
+
import { concat } from "@waku/utils/bytes";
|
4
|
+
import sha3 from "js-sha3";
|
5
|
+
import { Asymmetric, Symmetric } from "../misc.js";
|
6
|
+
const crypto = {
|
7
|
+
node: nodeCrypto,
|
8
|
+
web: typeof self === "object" && "crypto" in self ? self.crypto : undefined
|
9
|
+
};
|
10
|
+
export function getSubtle() {
|
11
|
+
if (crypto.web) {
|
12
|
+
return crypto.web.subtle;
|
13
|
+
}
|
14
|
+
else if (crypto.node) {
|
15
|
+
return crypto.node.webcrypto.subtle;
|
16
|
+
}
|
17
|
+
else {
|
18
|
+
throw new Error("The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)");
|
19
|
+
}
|
20
|
+
}
|
21
|
+
export const randomBytes = secp.utils.randomBytes;
|
22
|
+
export const sha256 = secp.utils.sha256;
|
23
|
+
/**
|
24
|
+
* Generate a new private key to be used for asymmetric encryption.
|
25
|
+
*
|
26
|
+
* Use {@link getPublicKey} to get the corresponding Public Key.
|
27
|
+
*/
|
28
|
+
export function generatePrivateKey() {
|
29
|
+
return randomBytes(Asymmetric.keySize);
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Generate a new symmetric key to be used for symmetric encryption.
|
33
|
+
*/
|
34
|
+
export function generateSymmetricKey() {
|
35
|
+
return randomBytes(Symmetric.keySize);
|
36
|
+
}
|
37
|
+
/**
|
38
|
+
* Return the public key for the given private key, to be used for asymmetric
|
39
|
+
* encryption.
|
40
|
+
*/
|
41
|
+
export const getPublicKey = secp.getPublicKey;
|
42
|
+
/**
|
43
|
+
* ECDSA Sign a message with the given private key.
|
44
|
+
*
|
45
|
+
* @param message The message to sign, usually a hash.
|
46
|
+
* @param privateKey The ECDSA private key to use to sign the message.
|
47
|
+
*
|
48
|
+
* @returns The signature and the recovery id concatenated.
|
49
|
+
*/
|
50
|
+
export async function sign(message, privateKey) {
|
51
|
+
const [signature, recoveryId] = await secp.sign(message, privateKey, {
|
52
|
+
recovered: true,
|
53
|
+
der: false
|
54
|
+
});
|
55
|
+
return concat([signature, new Uint8Array([recoveryId])], signature.length + 1);
|
56
|
+
}
|
57
|
+
export function keccak256(input) {
|
58
|
+
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
59
|
+
}
|
60
|
+
//# sourceMappingURL=utils.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/crypto/utils.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,QAAQ,CAAC;AAEhC,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,IAAI,MAAM,SAAS,CAAC;AAE3B,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAGnD,MAAM,MAAM,GAA8B;IACxC,IAAI,EAAE,UAAU;IAChB,GAAG,EAAE,OAAO,IAAI,KAAK,QAAQ,IAAI,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;CAC5E,CAAC;AAEF,MAAM,UAAU,SAAS;IACvB,IAAI,MAAM,CAAC,GAAG,EAAE;QACd,OAAO,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC;KAC1B;SAAM,IAAI,MAAM,CAAC,IAAI,EAAE;QACtB,OAAO,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;KACrC;SAAM;QACL,MAAM,IAAI,KAAK,CACb,yHAAyH,CAC1H,CAAC;KACH;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC;AAClD,MAAM,CAAC,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC;AAExC;;;;GAIG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO,WAAW,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,WAAW,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AACxC,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC;AAE9C;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CACxB,OAAmB,EACnB,UAAsB;IAEtB,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE;QACnE,SAAS,EAAE,IAAI;QACf,GAAG,EAAE,KAAK;KACX,CAAC,CAAC;IACH,OAAO,MAAM,CACX,CAAC,SAAS,EAAE,IAAI,UAAU,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,EACzC,SAAS,CAAC,MAAM,GAAG,CAAC,CACrB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAiB;IACzC,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC;AAC3D,CAAC"}
|
package/dist/ecies.d.ts
CHANGED
@@ -1,18 +1,17 @@
|
|
1
1
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
2
|
-
import { IMetaSetter,
|
3
|
-
import
|
2
|
+
import type { EncoderOptions as BaseEncoderOptions, IDecoder, IEncoder, IMessage, IMetaSetter, IProtoMessage, PubsubTopic } from "@waku/interfaces";
|
3
|
+
import { generatePrivateKey } from "./crypto/utils.js";
|
4
4
|
import { DecodedMessage } from "./decoded_message.js";
|
5
|
-
import {
|
6
|
-
export {
|
7
|
-
export type { Encoder, Decoder, DecodedMessage };
|
5
|
+
import { decryptAsymmetric, encryptAsymmetric, postCipher, preCipher } from "./encryption.js";
|
6
|
+
export { decryptAsymmetric, encryptAsymmetric, postCipher, preCipher, generatePrivateKey };
|
8
7
|
declare class Encoder implements IEncoder {
|
9
|
-
pubsubTopic:
|
8
|
+
pubsubTopic: PubsubTopic;
|
10
9
|
contentTopic: string;
|
11
10
|
private publicKey;
|
12
11
|
private sigPrivKey?;
|
13
12
|
ephemeral: boolean;
|
14
13
|
metaSetter?: IMetaSetter | undefined;
|
15
|
-
constructor(pubsubTopic:
|
14
|
+
constructor(pubsubTopic: PubsubTopic, contentTopic: string, publicKey: Uint8Array, sigPrivKey?: Uint8Array | undefined, ephemeral?: boolean, metaSetter?: IMetaSetter | undefined);
|
16
15
|
toWire(message: IMessage): Promise<Uint8Array | undefined>;
|
17
16
|
toProtoObj(message: IMessage): Promise<IProtoMessage | undefined>;
|
18
17
|
}
|
@@ -37,7 +36,7 @@ export interface EncoderOptions extends BaseEncoderOptions {
|
|
37
36
|
export declare function createEncoder({ pubsubTopic, contentTopic, publicKey, sigPrivKey, ephemeral, metaSetter }: EncoderOptions): Encoder;
|
38
37
|
declare class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
39
38
|
private privateKey;
|
40
|
-
constructor(pubsubTopic:
|
39
|
+
constructor(pubsubTopic: PubsubTopic, contentTopic: string, privateKey: Uint8Array);
|
41
40
|
fromProtoObj(pubsubTopic: string, protoMessage: IProtoMessage): Promise<DecodedMessage | undefined>;
|
42
41
|
}
|
43
42
|
/**
|
@@ -52,4 +51,4 @@ declare class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
52
51
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
53
52
|
* @param privateKey The private key used to decrypt the message.
|
54
53
|
*/
|
55
|
-
export declare function createDecoder(contentTopic: string, privateKey: Uint8Array, pubsubTopic?:
|
54
|
+
export declare function createDecoder(contentTopic: string, privateKey: Uint8Array, pubsubTopic?: PubsubTopic): Decoder;
|
package/dist/ecies.js
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import { DefaultPubsubTopic } from "@waku/core";
|
2
2
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
3
3
|
import { WakuMessage } from "@waku/proto";
|
4
4
|
import { Logger } from "@waku/utils";
|
5
|
+
import { generatePrivateKey } from "./crypto/utils.js";
|
5
6
|
import { DecodedMessage } from "./decoded_message.js";
|
6
|
-
import { decryptAsymmetric, encryptAsymmetric, postCipher, preCipher } from "./
|
7
|
-
import {
|
8
|
-
export {
|
7
|
+
import { decryptAsymmetric, encryptAsymmetric, postCipher, preCipher } from "./encryption.js";
|
8
|
+
import { OneMillion, Version } from "./misc.js";
|
9
|
+
export { decryptAsymmetric, encryptAsymmetric, postCipher, preCipher, generatePrivateKey };
|
9
10
|
const log = new Logger("message-encryption:ecies");
|
10
11
|
class Encoder {
|
11
12
|
pubsubTopic;
|
@@ -63,7 +64,7 @@ class Encoder {
|
|
63
64
|
* The payload can optionally be signed with the given private key as defined
|
64
65
|
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
65
66
|
*/
|
66
|
-
export function createEncoder({ pubsubTopic =
|
67
|
+
export function createEncoder({ pubsubTopic = DefaultPubsubTopic, contentTopic, publicKey, sigPrivKey, ephemeral = false, metaSetter }) {
|
67
68
|
return new Encoder(pubsubTopic, contentTopic, publicKey, sigPrivKey, ephemeral, metaSetter);
|
68
69
|
}
|
69
70
|
class Decoder extends DecoderV0 {
|
@@ -111,7 +112,7 @@ class Decoder extends DecoderV0 {
|
|
111
112
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
112
113
|
* @param privateKey The private key used to decrypt the message.
|
113
114
|
*/
|
114
|
-
export function createDecoder(contentTopic, privateKey, pubsubTopic =
|
115
|
+
export function createDecoder(contentTopic, privateKey, pubsubTopic = DefaultPubsubTopic) {
|
115
116
|
return new Decoder(pubsubTopic, contentTopic, privateKey);
|
116
117
|
}
|
117
118
|
//# sourceMappingURL=ecies.js.map
|
package/dist/ecies.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"ecies.js","sourceRoot":"","sources":["../src/ecies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,kCAAkC,CAAC;
|
1
|
+
{"version":3,"file":"ecies.js","sourceRoot":"","sources":["../src/ecies.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAUxE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EACV,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EACV,SAAS,EACT,kBAAkB,EACnB,CAAC;AAEF,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,0BAA0B,CAAC,CAAC;AAEnD,MAAM,OAAO;IAEF;IACA;IACC;IACA;IACD;IACA;IANT,YACS,WAAwB,EACxB,YAAoB,EACnB,SAAqB,EACrB,UAAuB,EACxB,YAAqB,KAAK,EAC1B,UAAwB;QALxB,gBAAW,GAAX,WAAW,CAAa;QACxB,iBAAY,GAAZ,YAAY,CAAQ;QACnB,cAAS,GAAT,SAAS,CAAY;QACrB,eAAU,GAAV,UAAU,CAAa;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,CAAC;QAC5C,OAAO,IAAI,cAAc,CACvB,WAAW,EACX,YAAY,EACZ,GAAG,CAAC,OAAO,EACX,GAAG,CAAC,GAAG,EAAE,SAAS,EAClB,GAAG,CAAC,GAAG,EAAE,SAAS,CACnB,CAAC;IACJ,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAC3B,YAAoB,EACpB,UAAsB,EACtB,cAA2B,kBAAkB;IAE7C,OAAO,IAAI,OAAO,CAAC,WAAW,EAAE,YAAY,EAAE,UAAU,CAAC,CAAC;AAC5D,CAAC"}
|
@@ -1,4 +1,3 @@
|
|
1
|
-
import { Signature } from "./index.js";
|
2
1
|
/**
|
3
2
|
* Proceed with Asymmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
4
3
|
* The data MUST be flags | payload-length | payload | [signature].
|
@@ -42,6 +41,10 @@ export declare function decryptSymmetric(payload: Uint8Array, key: Uint8Array |
|
|
42
41
|
* or {@link encryptSymmetric}.
|
43
42
|
*/
|
44
43
|
export declare function preCipher(messagePayload: Uint8Array, sigPrivKey?: Uint8Array): Promise<Uint8Array>;
|
44
|
+
type Signature = {
|
45
|
+
signature: Uint8Array;
|
46
|
+
publicKey: Uint8Array | undefined;
|
47
|
+
};
|
45
48
|
/**
|
46
49
|
* Decode a decrypted payload.
|
47
50
|
*
|
@@ -51,3 +54,4 @@ export declare function postCipher(message: Uint8Array): {
|
|
51
54
|
payload: Uint8Array;
|
52
55
|
sig?: Signature;
|
53
56
|
} | undefined;
|
57
|
+
export {};
|
@@ -1,9 +1,7 @@
|
|
1
1
|
import * as secp from "@noble/secp256k1";
|
2
2
|
import { concat, hexToBytes } from "@waku/utils/bytes";
|
3
|
-
import {
|
4
|
-
import
|
5
|
-
import { keccak256, randomBytes, sign } from "./crypto/index.js";
|
6
|
-
import * as symmetric from "./crypto/symmetric.js";
|
3
|
+
import { ecies, keccak256, randomBytes, sign, symmetric } from "./crypto/index.js";
|
4
|
+
import { Symmetric } from "./misc.js";
|
7
5
|
const FlagsLength = 1;
|
8
6
|
const FlagMask = 3; // 0011
|
9
7
|
const IsSignedMask = 4; // 0100
|
@@ -175,4 +173,4 @@ export function postCipher(message) {
|
|
175
173
|
}
|
176
174
|
return { payload, sig };
|
177
175
|
}
|
178
|
-
//# sourceMappingURL=
|
176
|
+
//# sourceMappingURL=encryption.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"encryption.js","sourceRoot":"","sources":["../src/encryption.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EACL,KAAK,EACL,SAAS,EACT,WAAW,EACX,IAAI,EACJ,SAAS,EACV,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,WAAW,GAAG,CAAC,CAAC;AACtB,MAAM,QAAQ,GAAG,CAAC,CAAC,CAAC,OAAO;AAC3B,MAAM,YAAY,GAAG,CAAC,CAAC,CAAC,OAAO;AAC/B,MAAM,aAAa,GAAG,GAAG,CAAC;AAC1B,MAAM,eAAe,GAAG,EAAE,CAAC;AAE3B,SAAS,yBAAyB,CAAC,OAAmB;IACpD,MAAM,eAAe,GAAG,IAAI,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACrD,OAAO,eAAe,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC;AAChD,CAAC;AAED,SAAS,cAAc,CACrB,OAAmB,EACnB,sBAA8B;IAE9B,IAAI,gBAAgB,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,sBAAsB,CAAC,CAAC;IACpE,oBAAoB;IACpB,IAAI,sBAAsB,GAAG,CAAC,EAAE;QAC9B,kDAAkD;QAClD,gBAAgB,GAAG,MAAM,CACvB,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,CAAC,KAAK,CAAC,YAAY,EAAE,YAAY,GAAG,WAAW,CAAC,CAAC;IAExE,MAAM,QAAQ,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;IAE1C,IAAI,GAAG,CAAC;IACR,IAAI,QAAQ,EAAE;QACZ,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;QACxC,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;QACxC,MAAM,SAAS,GAAG,eAAe,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QACnD,GAAG,GAAG,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC;KAChC;IAED,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC;AAC1B,CAAC"}
|
package/dist/index.d.ts
CHANGED
@@ -1,12 +1,7 @@
|
|
1
1
|
import { generatePrivateKey, generateSymmetricKey, getPublicKey } from "./crypto/index.js";
|
2
2
|
import { DecodedMessage } from "./decoded_message.js";
|
3
|
-
export declare const OneMillion: bigint;
|
4
3
|
export { generatePrivateKey, generateSymmetricKey, getPublicKey };
|
5
4
|
export type { DecodedMessage };
|
6
5
|
export * as ecies from "./ecies.js";
|
7
6
|
export * as symmetric from "./symmetric.js";
|
8
|
-
export
|
9
|
-
export type Signature = {
|
10
|
-
signature: Uint8Array;
|
11
|
-
publicKey: Uint8Array | undefined;
|
12
|
-
};
|
7
|
+
export * as crypto from "./crypto";
|
package/dist/index.js
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import { generatePrivateKey, generateSymmetricKey, getPublicKey } from "./crypto/index.js";
|
2
|
-
export const OneMillion = BigInt(1000000);
|
3
2
|
export { generatePrivateKey, generateSymmetricKey, getPublicKey };
|
4
3
|
export * as ecies from "./ecies.js";
|
5
4
|
export * as symmetric from "./symmetric.js";
|
6
|
-
export
|
5
|
+
export * as crypto from "./crypto";
|
7
6
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,YAAY,EACb,MAAM,mBAAmB,CAAC;AAG3B,
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,YAAY,EACb,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,YAAY,EAAE,CAAC;AAGlE,OAAO,KAAK,KAAK,MAAM,YAAY,CAAC;AACpC,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC"}
|
package/dist/misc.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"misc.js","sourceRoot":"","sources":["../src/misc.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG;IACvB,OAAO,EAAE,EAAE;IACX,MAAM,EAAE,EAAE;IACV,OAAO,EAAE,EAAE;IACX,SAAS,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE;CAC5C,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,OAAO,EAAE,EAAE;CACZ,CAAC;AAEF,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,OAAS,CAAC,CAAC;AAE5C,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC"}
|
package/dist/symmetric.d.ts
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
2
|
-
import type { EncoderOptions as BaseEncoderOptions, IDecoder, IEncoder, IMessage, IMetaSetter, IProtoMessage,
|
2
|
+
import type { EncoderOptions as BaseEncoderOptions, IDecoder, IEncoder, IMessage, IMetaSetter, IProtoMessage, PubsubTopic } from "@waku/interfaces";
|
3
|
+
import { generateSymmetricKey } from "./crypto/utils.js";
|
3
4
|
import { DecodedMessage } from "./decoded_message.js";
|
4
|
-
import {
|
5
|
-
export { generateSymmetricKey };
|
6
|
-
export type { DecodedMessage, Encoder, Decoder };
|
5
|
+
import { decryptSymmetric, encryptSymmetric, postCipher, preCipher } from "./encryption.js";
|
6
|
+
export { decryptSymmetric, encryptSymmetric, postCipher, preCipher, generateSymmetricKey };
|
7
7
|
declare class Encoder implements IEncoder {
|
8
|
-
pubsubTopic:
|
8
|
+
pubsubTopic: PubsubTopic;
|
9
9
|
contentTopic: string;
|
10
10
|
private symKey;
|
11
11
|
private sigPrivKey?;
|
12
12
|
ephemeral: boolean;
|
13
13
|
metaSetter?: IMetaSetter | undefined;
|
14
|
-
constructor(pubsubTopic:
|
14
|
+
constructor(pubsubTopic: PubsubTopic, contentTopic: string, symKey: Uint8Array, sigPrivKey?: Uint8Array | undefined, ephemeral?: boolean, metaSetter?: IMetaSetter | undefined);
|
15
15
|
toWire(message: IMessage): Promise<Uint8Array | undefined>;
|
16
16
|
toProtoObj(message: IMessage): Promise<IProtoMessage | undefined>;
|
17
17
|
}
|
@@ -36,7 +36,7 @@ export interface EncoderOptions extends BaseEncoderOptions {
|
|
36
36
|
export declare function createEncoder({ pubsubTopic, contentTopic, symKey, sigPrivKey, ephemeral, metaSetter }: EncoderOptions): Encoder;
|
37
37
|
declare class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
38
38
|
private symKey;
|
39
|
-
constructor(pubsubTopic:
|
39
|
+
constructor(pubsubTopic: PubsubTopic, contentTopic: string, symKey: Uint8Array);
|
40
40
|
fromProtoObj(pubsubTopic: string, protoMessage: IProtoMessage): Promise<DecodedMessage | undefined>;
|
41
41
|
}
|
42
42
|
/**
|
@@ -51,4 +51,4 @@ declare class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
51
51
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
52
52
|
* @param symKey The symmetric key used to decrypt the message.
|
53
53
|
*/
|
54
|
-
export declare function createDecoder(contentTopic: string, symKey: Uint8Array, pubsubTopic?:
|
54
|
+
export declare function createDecoder(contentTopic: string, symKey: Uint8Array, pubsubTopic?: PubsubTopic): Decoder;
|
package/dist/symmetric.js
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
import {
|
1
|
+
import { DefaultPubsubTopic } from "@waku/core";
|
2
2
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
3
3
|
import { WakuMessage } from "@waku/proto";
|
4
4
|
import { Logger } from "@waku/utils";
|
5
|
+
import { generateSymmetricKey } from "./crypto/utils.js";
|
5
6
|
import { DecodedMessage } from "./decoded_message.js";
|
6
|
-
import { decryptSymmetric, encryptSymmetric, postCipher, preCipher } from "./
|
7
|
-
import {
|
8
|
-
export { generateSymmetricKey };
|
7
|
+
import { decryptSymmetric, encryptSymmetric, postCipher, preCipher } from "./encryption.js";
|
8
|
+
import { OneMillion, Version } from "./misc.js";
|
9
|
+
export { decryptSymmetric, encryptSymmetric, postCipher, preCipher, generateSymmetricKey };
|
9
10
|
const log = new Logger("message-encryption:symmetric");
|
10
11
|
class Encoder {
|
11
12
|
pubsubTopic;
|
@@ -63,7 +64,7 @@ class Encoder {
|
|
63
64
|
* The payload can optionally be signed with the given private key as defined
|
64
65
|
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
65
66
|
*/
|
66
|
-
export function createEncoder({ pubsubTopic =
|
67
|
+
export function createEncoder({ pubsubTopic = DefaultPubsubTopic, contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter }) {
|
67
68
|
return new Encoder(pubsubTopic, contentTopic, symKey, sigPrivKey, ephemeral, metaSetter);
|
68
69
|
}
|
69
70
|
class Decoder extends DecoderV0 {
|
@@ -111,7 +112,7 @@ class Decoder extends DecoderV0 {
|
|
111
112
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
112
113
|
* @param symKey The symmetric key used to decrypt the message.
|
113
114
|
*/
|
114
|
-
export function createDecoder(contentTopic, symKey, pubsubTopic =
|
115
|
+
export function createDecoder(contentTopic, symKey, pubsubTopic = DefaultPubsubTopic) {
|
115
116
|
return new Decoder(pubsubTopic, contentTopic, symKey);
|
116
117
|
}
|
117
118
|
//# sourceMappingURL=symmetric.js.map
|
package/dist/symmetric.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"symmetric.js","sourceRoot":"","sources":["../src/symmetric.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAUxE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,SAAS,EACV,MAAM,
|
1
|
+
{"version":3,"file":"symmetric.js","sourceRoot":"","sources":["../src/symmetric.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAUxE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,UAAU,EACV,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEhD,OAAO,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,CAAC,YAAY,EAAE,EAC9F,CAAC,CACF,CAAC;YACF,OAAO;SACR;QAED,IAAI,CAAC,OAAO,EAAE;YACZ,GAAG,CAAC,KAAK,CACP,8CAA8C,IAAI,CAAC,YAAY,EAAE,CAClE,CAAC;YACF,OAAO;SACR;QAED,MAAM,GAAG,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;QAEhC,IAAI,CAAC,GAAG,EAAE;YACR,GAAG,CAAC,KAAK,CACP,6CAA6C,IAAI,CAAC,YAAY,EAAE,CACjE,CAAC;YACF,OAAO;SACR;QAED,GAAG,CAAC,IAAI,CAAC,mBAAmB,EAAE,YAAY,CAAC,CAAC;QAC5C,OAAO,IAAI,cAAc,CACvB,WAAW,EACX,YAAY,EACZ,GAAG,CAAC,OAAO,EACX,GAAG,CAAC,GAAG,EAAE,SAAS,EAClB,GAAG,CAAC,GAAG,EAAE,SAAS,CACnB,CAAC;IACJ,CAAC;CACF;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAC3B,YAAoB,EACpB,MAAkB,EAClB,cAA2B,kBAAkB;IAE7C,OAAO,IAAI,OAAO,CAAC,WAAW,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;AACxD,CAAC"}
|
package/package.json
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"name":"@waku/message-encryption","version":"0.0.24-
|
1
|
+
{"name":"@waku/message-encryption","version":"0.0.24-7eb3375.0","description":"Waku Message Payload Encryption","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"},"./ecies":{"types":"./dist/ecies.d.ts","import":"./dist/ecies.js"},"./symmetric":{"types":"./dist/symmetric.d.ts","import":"./dist/symmetric.js"},"./crypto":{"types":"./dist/crypto/index.d.ts","import":"./dist/crypto/index.js"}},"typesVersions":{"*":{"*":["*","dist/*","dist/*/index"]}},"type":"module","author":"Waku Team","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/message-encryption#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","decentralized","secure","communication","web3","ethereum","dapps","privacy"],"scripts":{"build":"run-s build:**","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:lint":"eslint src *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","check:tsc":"tsc -p tsconfig.dev.json","test":"run-s test:*","test:node":"TS_NODE_PROJECT=./tsconfig.dev.json mocha","test:browser":"karma start karma.conf.cjs","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=18"},"browser":{"crypto":false},"dependencies":{"@noble/secp256k1":"^1.7.1","@waku/core":"0.0.26-7eb3375.0","@waku/interfaces":"0.0.21-7eb3375.0","@waku/proto":"0.0.6-7eb3375.0","@waku/utils":"0.0.14-7eb3375.0","debug":"^4.3.4","js-sha3":"^0.9.2"},"devDependencies":{"@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.11","@types/mocha":"^10.0.1","@waku/build-utils":"*","chai":"^4.3.10","cspell":"^7.3.2","fast-check":"^3.13.1","mocha":"^10.2.0","npm-run-all":"^4.1.5","process":"^0.11.10","rollup":"^3.29.2"},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"]}
|
package/src/crypto/ecies.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import * as secp from "@noble/secp256k1";
|
2
2
|
import { concat, hexToBytes } from "@waku/utils/bytes";
|
3
3
|
|
4
|
-
import { getSubtle, randomBytes, sha256 } from "./
|
4
|
+
import { getSubtle, randomBytes, sha256 } from "./utils.js";
|
5
5
|
/**
|
6
6
|
* HKDF as implemented in go-ethereum.
|
7
7
|
*/
|
package/src/crypto/index.ts
CHANGED
@@ -1,76 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
import { concat } from "@waku/utils/bytes";
|
5
|
-
import sha3 from "js-sha3";
|
6
|
-
|
7
|
-
import { Asymmetric, Symmetric } from "../constants.js";
|
8
|
-
|
9
|
-
declare const self: Record<string, any> | undefined;
|
10
|
-
const crypto: { node?: any; web?: any } = {
|
11
|
-
node: nodeCrypto,
|
12
|
-
web: typeof self === "object" && "crypto" in self ? self.crypto : undefined
|
13
|
-
};
|
14
|
-
|
15
|
-
export function getSubtle(): SubtleCrypto {
|
16
|
-
if (crypto.web) {
|
17
|
-
return crypto.web.subtle;
|
18
|
-
} else if (crypto.node) {
|
19
|
-
return crypto.node.webcrypto.subtle;
|
20
|
-
} else {
|
21
|
-
throw new Error(
|
22
|
-
"The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)"
|
23
|
-
);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
|
27
|
-
export const randomBytes = secp.utils.randomBytes;
|
28
|
-
export const sha256 = secp.utils.sha256;
|
29
|
-
|
30
|
-
/**
|
31
|
-
* Generate a new private key to be used for asymmetric encryption.
|
32
|
-
*
|
33
|
-
* Use {@link getPublicKey} to get the corresponding Public Key.
|
34
|
-
*/
|
35
|
-
export function generatePrivateKey(): Uint8Array {
|
36
|
-
return randomBytes(Asymmetric.keySize);
|
37
|
-
}
|
38
|
-
|
39
|
-
/**
|
40
|
-
* Generate a new symmetric key to be used for symmetric encryption.
|
41
|
-
*/
|
42
|
-
export function generateSymmetricKey(): Uint8Array {
|
43
|
-
return randomBytes(Symmetric.keySize);
|
44
|
-
}
|
45
|
-
|
46
|
-
/**
|
47
|
-
* Return the public key for the given private key, to be used for asymmetric
|
48
|
-
* encryption.
|
49
|
-
*/
|
50
|
-
export const getPublicKey = secp.getPublicKey;
|
51
|
-
|
52
|
-
/**
|
53
|
-
* ECDSA Sign a message with the given private key.
|
54
|
-
*
|
55
|
-
* @param message The message to sign, usually a hash.
|
56
|
-
* @param privateKey The ECDSA private key to use to sign the message.
|
57
|
-
*
|
58
|
-
* @returns The signature and the recovery id concatenated.
|
59
|
-
*/
|
60
|
-
export async function sign(
|
61
|
-
message: Uint8Array,
|
62
|
-
privateKey: Uint8Array
|
63
|
-
): Promise<Uint8Array> {
|
64
|
-
const [signature, recoveryId] = await secp.sign(message, privateKey, {
|
65
|
-
recovered: true,
|
66
|
-
der: false
|
67
|
-
});
|
68
|
-
return concat(
|
69
|
-
[signature, new Uint8Array([recoveryId])],
|
70
|
-
signature.length + 1
|
71
|
-
);
|
72
|
-
}
|
73
|
-
|
74
|
-
export function keccak256(input: Uint8Array): Uint8Array {
|
75
|
-
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
76
|
-
}
|
1
|
+
export * from "./utils.js";
|
2
|
+
export * as ecies from "./ecies.js";
|
3
|
+
export * as symmetric from "./symmetric.js";
|
package/src/crypto/symmetric.ts
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
import nodeCrypto from "crypto";
|
2
|
+
|
3
|
+
import * as secp from "@noble/secp256k1";
|
4
|
+
import { concat } from "@waku/utils/bytes";
|
5
|
+
import sha3 from "js-sha3";
|
6
|
+
|
7
|
+
import { Asymmetric, Symmetric } from "../misc.js";
|
8
|
+
|
9
|
+
declare const self: Record<string, any> | undefined;
|
10
|
+
const crypto: { node?: any; web?: any } = {
|
11
|
+
node: nodeCrypto,
|
12
|
+
web: typeof self === "object" && "crypto" in self ? self.crypto : undefined
|
13
|
+
};
|
14
|
+
|
15
|
+
export function getSubtle(): SubtleCrypto {
|
16
|
+
if (crypto.web) {
|
17
|
+
return crypto.web.subtle;
|
18
|
+
} else if (crypto.node) {
|
19
|
+
return crypto.node.webcrypto.subtle;
|
20
|
+
} else {
|
21
|
+
throw new Error(
|
22
|
+
"The environment doesn't have Crypto Subtle API (if in the browser, be sure to use to be in a secure context, ie, https)"
|
23
|
+
);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
export const randomBytes = secp.utils.randomBytes;
|
28
|
+
export const sha256 = secp.utils.sha256;
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Generate a new private key to be used for asymmetric encryption.
|
32
|
+
*
|
33
|
+
* Use {@link getPublicKey} to get the corresponding Public Key.
|
34
|
+
*/
|
35
|
+
export function generatePrivateKey(): Uint8Array {
|
36
|
+
return randomBytes(Asymmetric.keySize);
|
37
|
+
}
|
38
|
+
|
39
|
+
/**
|
40
|
+
* Generate a new symmetric key to be used for symmetric encryption.
|
41
|
+
*/
|
42
|
+
export function generateSymmetricKey(): Uint8Array {
|
43
|
+
return randomBytes(Symmetric.keySize);
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Return the public key for the given private key, to be used for asymmetric
|
48
|
+
* encryption.
|
49
|
+
*/
|
50
|
+
export const getPublicKey = secp.getPublicKey;
|
51
|
+
|
52
|
+
/**
|
53
|
+
* ECDSA Sign a message with the given private key.
|
54
|
+
*
|
55
|
+
* @param message The message to sign, usually a hash.
|
56
|
+
* @param privateKey The ECDSA private key to use to sign the message.
|
57
|
+
*
|
58
|
+
* @returns The signature and the recovery id concatenated.
|
59
|
+
*/
|
60
|
+
export async function sign(
|
61
|
+
message: Uint8Array,
|
62
|
+
privateKey: Uint8Array
|
63
|
+
): Promise<Uint8Array> {
|
64
|
+
const [signature, recoveryId] = await secp.sign(message, privateKey, {
|
65
|
+
recovered: true,
|
66
|
+
der: false
|
67
|
+
});
|
68
|
+
return concat(
|
69
|
+
[signature, new Uint8Array([recoveryId])],
|
70
|
+
signature.length + 1
|
71
|
+
);
|
72
|
+
}
|
73
|
+
|
74
|
+
export function keccak256(input: Uint8Array): Uint8Array {
|
75
|
+
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
76
|
+
}
|
package/src/ecies.ts
CHANGED
@@ -1,39 +1,40 @@
|
|
1
|
-
import {
|
1
|
+
import { DefaultPubsubTopic } from "@waku/core";
|
2
2
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
3
|
-
import { IMetaSetter, PubSubTopic } from "@waku/interfaces";
|
4
3
|
import type {
|
5
4
|
EncoderOptions as BaseEncoderOptions,
|
6
5
|
IDecoder,
|
7
6
|
IEncoder,
|
8
7
|
IMessage,
|
9
|
-
|
8
|
+
IMetaSetter,
|
9
|
+
IProtoMessage,
|
10
|
+
PubsubTopic
|
10
11
|
} from "@waku/interfaces";
|
11
12
|
import { WakuMessage } from "@waku/proto";
|
12
13
|
import { Logger } from "@waku/utils";
|
13
14
|
|
15
|
+
import { generatePrivateKey } from "./crypto/utils.js";
|
14
16
|
import { DecodedMessage } from "./decoded_message.js";
|
15
17
|
import {
|
16
18
|
decryptAsymmetric,
|
17
19
|
encryptAsymmetric,
|
18
20
|
postCipher,
|
19
21
|
preCipher
|
20
|
-
} from "./
|
22
|
+
} from "./encryption.js";
|
23
|
+
import { OneMillion, Version } from "./misc.js";
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
export { generatePrivateKey, getPublicKey };
|
30
|
-
export type { Encoder, Decoder, DecodedMessage };
|
25
|
+
export {
|
26
|
+
decryptAsymmetric,
|
27
|
+
encryptAsymmetric,
|
28
|
+
postCipher,
|
29
|
+
preCipher,
|
30
|
+
generatePrivateKey
|
31
|
+
};
|
31
32
|
|
32
33
|
const log = new Logger("message-encryption:ecies");
|
33
34
|
|
34
35
|
class Encoder implements IEncoder {
|
35
36
|
constructor(
|
36
|
-
public pubsubTopic:
|
37
|
+
public pubsubTopic: PubsubTopic,
|
37
38
|
public contentTopic: string,
|
38
39
|
private publicKey: Uint8Array,
|
39
40
|
private sigPrivKey?: Uint8Array,
|
@@ -97,7 +98,7 @@ export interface EncoderOptions extends BaseEncoderOptions {
|
|
97
98
|
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
98
99
|
*/
|
99
100
|
export function createEncoder({
|
100
|
-
pubsubTopic =
|
101
|
+
pubsubTopic = DefaultPubsubTopic,
|
101
102
|
contentTopic,
|
102
103
|
publicKey,
|
103
104
|
sigPrivKey,
|
@@ -116,7 +117,7 @@ export function createEncoder({
|
|
116
117
|
|
117
118
|
class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
118
119
|
constructor(
|
119
|
-
pubsubTopic:
|
120
|
+
pubsubTopic: PubsubTopic,
|
120
121
|
contentTopic: string,
|
121
122
|
private privateKey: Uint8Array
|
122
123
|
) {
|
@@ -193,7 +194,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
193
194
|
export function createDecoder(
|
194
195
|
contentTopic: string,
|
195
196
|
privateKey: Uint8Array,
|
196
|
-
pubsubTopic:
|
197
|
+
pubsubTopic: PubsubTopic = DefaultPubsubTopic
|
197
198
|
): Decoder {
|
198
199
|
return new Decoder(pubsubTopic, contentTopic, privateKey);
|
199
200
|
}
|