@waku/message-encryption 0.0.21 → 0.0.23-a42b7be.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/CHANGELOG.md +21 -0
- package/bundle/ecies.js +1 -1
- package/bundle/index-c8f623cb.js +25348 -0
- package/bundle/index.js +1 -1
- package/bundle/symmetric.js +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/decoded_message.d.ts +1 -1
- package/dist/decoded_message.js +2 -2
- package/dist/ecies.d.ts +10 -10
- package/dist/ecies.js +22 -20
- package/dist/ecies.js.map +1 -1
- package/dist/symmetric.d.ts +9 -10
- package/dist/symmetric.js +21 -20
- package/dist/symmetric.js.map +1 -1
- package/package.json +1 -117
- package/src/decoded_message.ts +2 -2
- package/src/ecies.ts +26 -17
- package/src/symmetric.ts +33 -18
- package/bundle/index-f743130b.js +0 -8755
package/dist/symmetric.js
CHANGED
@@ -1,18 +1,21 @@
|
|
1
|
+
import { DefaultPubSubTopic } from "@waku/core";
|
1
2
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
2
3
|
import { WakuMessage } from "@waku/proto";
|
3
|
-
import
|
4
|
+
import { Logger } from "@waku/utils";
|
4
5
|
import { DecodedMessage } from "./decoded_message.js";
|
5
6
|
import { decryptSymmetric, encryptSymmetric, postCipher, preCipher } from "./waku_payload.js";
|
6
7
|
import { generateSymmetricKey, OneMillion, Version } from "./index.js";
|
7
8
|
export { generateSymmetricKey };
|
8
|
-
const log =
|
9
|
+
const log = new Logger("message-encryption:symmetric");
|
9
10
|
class Encoder {
|
11
|
+
pubsubTopic;
|
10
12
|
contentTopic;
|
11
13
|
symKey;
|
12
14
|
sigPrivKey;
|
13
15
|
ephemeral;
|
14
16
|
metaSetter;
|
15
|
-
constructor(contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter) {
|
17
|
+
constructor(pubsubTopic, contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter) {
|
18
|
+
this.pubsubTopic = pubsubTopic;
|
16
19
|
this.contentTopic = contentTopic;
|
17
20
|
this.symKey = symKey;
|
18
21
|
this.sigPrivKey = sigPrivKey;
|
@@ -54,26 +57,25 @@ class Encoder {
|
|
54
57
|
*
|
55
58
|
* An encoder is used to encode messages in the [`14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
56
59
|
* format to be sent over the Waku network. The resulting encoder can then be
|
57
|
-
* pass to { @link @waku/interfaces.
|
58
|
-
* { @link @waku/interfaces.Relay.send } to automatically encrypt
|
60
|
+
* pass to { @link @waku/interfaces!ISender.send } to automatically encrypt
|
59
61
|
* and encode outgoing messages.
|
60
62
|
*
|
61
63
|
* The payload can optionally be signed with the given private key as defined
|
62
64
|
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
63
65
|
*/
|
64
|
-
export function createEncoder({ contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter }) {
|
65
|
-
return new Encoder(contentTopic, symKey, sigPrivKey, ephemeral, metaSetter);
|
66
|
+
export function createEncoder({ pubsubTopic = DefaultPubSubTopic, contentTopic, symKey, sigPrivKey, ephemeral = false, metaSetter }) {
|
67
|
+
return new Encoder(pubsubTopic, contentTopic, symKey, sigPrivKey, ephemeral, metaSetter);
|
66
68
|
}
|
67
69
|
class Decoder extends DecoderV0 {
|
68
70
|
symKey;
|
69
|
-
constructor(contentTopic, symKey) {
|
70
|
-
super(contentTopic);
|
71
|
+
constructor(pubsubTopic, contentTopic, symKey) {
|
72
|
+
super(pubsubTopic, contentTopic);
|
71
73
|
this.symKey = symKey;
|
72
74
|
}
|
73
|
-
async fromProtoObj(
|
75
|
+
async fromProtoObj(pubsubTopic, protoMessage) {
|
74
76
|
const cipherPayload = protoMessage.payload;
|
75
77
|
if (protoMessage.version !== Version) {
|
76
|
-
log("Failed to decrypt due to incorrect version, expected:", Version, ", actual:", protoMessage.version);
|
78
|
+
log.error("Failed to decrypt due to incorrect version, expected:", Version, ", actual:", protoMessage.version);
|
77
79
|
return;
|
78
80
|
}
|
79
81
|
let payload;
|
@@ -81,20 +83,20 @@ class Decoder extends DecoderV0 {
|
|
81
83
|
payload = await decryptSymmetric(cipherPayload, this.symKey);
|
82
84
|
}
|
83
85
|
catch (e) {
|
84
|
-
log(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`, e);
|
86
|
+
log.error(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`, e);
|
85
87
|
return;
|
86
88
|
}
|
87
89
|
if (!payload) {
|
88
|
-
log(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);
|
90
|
+
log.error(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);
|
89
91
|
return;
|
90
92
|
}
|
91
93
|
const res = postCipher(payload);
|
92
94
|
if (!res) {
|
93
|
-
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);
|
95
|
+
log.error(`Failed to decode payload for contentTopic ${this.contentTopic}`);
|
94
96
|
return;
|
95
97
|
}
|
96
|
-
log("Message decrypted", protoMessage);
|
97
|
-
return new DecodedMessage(
|
98
|
+
log.info("Message decrypted", protoMessage);
|
99
|
+
return new DecodedMessage(pubsubTopic, protoMessage, res.payload, res.sig?.signature, res.sig?.publicKey);
|
98
100
|
}
|
99
101
|
}
|
100
102
|
/**
|
@@ -103,14 +105,13 @@ class Decoder extends DecoderV0 {
|
|
103
105
|
*
|
104
106
|
* A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
105
107
|
* format when received from the Waku network. The resulting decoder can then be
|
106
|
-
* pass to { @link @waku/interfaces.
|
107
|
-
* { @link @waku/interfaces.Relay.subscribe } to automatically decrypt and
|
108
|
+
* pass to { @link @waku/interfaces!IReceiver.subscribe } to automatically decrypt and
|
108
109
|
* decode incoming messages.
|
109
110
|
*
|
110
111
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
111
112
|
* @param symKey The symmetric key used to decrypt the message.
|
112
113
|
*/
|
113
|
-
export function createDecoder(contentTopic, symKey) {
|
114
|
-
return new Decoder(contentTopic, symKey);
|
114
|
+
export function createDecoder(contentTopic, symKey, pubsubTopic = DefaultPubSubTopic) {
|
115
|
+
return new Decoder(pubsubTopic, contentTopic, symKey);
|
115
116
|
}
|
116
117
|
//# 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,OAAO,IAAI,SAAS,EAAE,MAAM,kCAAkC,CAAC;
|
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,mBAAmB,CAAC;AAE3B,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEvE,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAGhC,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,8BAA8B,CAAC,CAAC;AAEvD,MAAM,OAAO;IAEF;IACA;IACC;IACA;IACD;IACA;IANT,YACS,WAAwB,EACxB,YAAoB,EACnB,MAAkB,EAClB,UAAuB,EACxB,YAAqB,KAAK,EAC1B,UAAwB;QALxB,gBAAW,GAAX,WAAW,CAAa;QACxB,iBAAY,GAAZ,YAAY,CAAQ;QACnB,WAAM,GAAN,MAAM,CAAY;QAClB,eAAU,GA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AAE,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,117 +1 @@
|
|
1
|
-
{
|
2
|
-
"name": "@waku/message-encryption",
|
3
|
-
"version": "0.0.21",
|
4
|
-
"description": "Waku Message Payload Encryption",
|
5
|
-
"types": "./dist/index.d.ts",
|
6
|
-
"module": "./dist/index.js",
|
7
|
-
"exports": {
|
8
|
-
".": {
|
9
|
-
"types": "./dist/index.d.ts",
|
10
|
-
"import": "./dist/index.js"
|
11
|
-
},
|
12
|
-
"./ecies": {
|
13
|
-
"types": "./dist/ecies.d.ts",
|
14
|
-
"import": "./dist/ecies.js"
|
15
|
-
},
|
16
|
-
"./symmetric": {
|
17
|
-
"types": "./dist/symmetric.d.ts",
|
18
|
-
"import": "./dist/symmetric.js"
|
19
|
-
}
|
20
|
-
},
|
21
|
-
"typesVersions": {
|
22
|
-
"*": {
|
23
|
-
"*": [
|
24
|
-
"*",
|
25
|
-
"dist/*",
|
26
|
-
"dist/*/index"
|
27
|
-
]
|
28
|
-
}
|
29
|
-
},
|
30
|
-
"type": "module",
|
31
|
-
"author": "Waku Team",
|
32
|
-
"homepage": "https://github.com/waku-org/js-waku/tree/master/packages/message-encryption#readme",
|
33
|
-
"repository": {
|
34
|
-
"type": "git",
|
35
|
-
"url": "https://github.com/waku-org/js-waku.git"
|
36
|
-
},
|
37
|
-
"bugs": {
|
38
|
-
"url": "https://github.com/waku-org/js-waku/issues"
|
39
|
-
},
|
40
|
-
"license": "MIT OR Apache-2.0",
|
41
|
-
"keywords": [
|
42
|
-
"waku",
|
43
|
-
"decentralized",
|
44
|
-
"secure",
|
45
|
-
"communication",
|
46
|
-
"web3",
|
47
|
-
"ethereum",
|
48
|
-
"dapps",
|
49
|
-
"privacy"
|
50
|
-
],
|
51
|
-
"scripts": {
|
52
|
-
"build": "run-s build:**",
|
53
|
-
"build:esm": "tsc",
|
54
|
-
"build:bundle": "rollup --config rollup.config.js",
|
55
|
-
"fix": "run-s fix:*",
|
56
|
-
"fix:lint": "eslint src *.js --fix",
|
57
|
-
"check": "run-s check:*",
|
58
|
-
"check:lint": "eslint src *.js",
|
59
|
-
"check:spelling": "cspell \"{README.md,src/**/*.ts}\"",
|
60
|
-
"check:tsc": "tsc -p tsconfig.dev.json",
|
61
|
-
"test": "run-s test:*",
|
62
|
-
"test:node": "TS_NODE_PROJECT=./tsconfig.dev.json mocha",
|
63
|
-
"test:browser": "karma start karma.conf.cjs",
|
64
|
-
"prepublish": "npm run build",
|
65
|
-
"reset-hard": "git clean -dfx -e .idea && git reset --hard && npm i && npm run build"
|
66
|
-
},
|
67
|
-
"engines": {
|
68
|
-
"node": ">=16"
|
69
|
-
},
|
70
|
-
"browser": {
|
71
|
-
"crypto": false
|
72
|
-
},
|
73
|
-
"dependencies": {
|
74
|
-
"@noble/secp256k1": "^1.7.1",
|
75
|
-
"@waku/core": "0.0.23",
|
76
|
-
"@waku/interfaces": "0.0.18",
|
77
|
-
"@waku/proto": "0.0.5",
|
78
|
-
"@waku/utils": "0.0.11",
|
79
|
-
"debug": "^4.3.4",
|
80
|
-
"js-sha3": "^0.8.0"
|
81
|
-
},
|
82
|
-
"devDependencies": {
|
83
|
-
"@rollup/plugin-commonjs": "^25.0.4",
|
84
|
-
"@rollup/plugin-json": "^6.0.0",
|
85
|
-
"@rollup/plugin-node-resolve": "^15.1.0",
|
86
|
-
"@types/chai": "^4.3.5",
|
87
|
-
"@types/mocha": "^10.0.1",
|
88
|
-
"@waku/build-utils": "*",
|
89
|
-
"chai": "^4.3.7",
|
90
|
-
"cspell": "^7.3.2",
|
91
|
-
"fast-check": "^3.12.0",
|
92
|
-
"karma": "^6.4.1",
|
93
|
-
"karma-chrome-launcher": "^3.2.0",
|
94
|
-
"karma-mocha": "^2.0.1",
|
95
|
-
"karma-webpack": "^5.0.0",
|
96
|
-
"mocha": "^10.2.0",
|
97
|
-
"npm-run-all": "^4.1.5",
|
98
|
-
"process": "^0.11.10",
|
99
|
-
"puppeteer": "^21.1.1",
|
100
|
-
"rollup": "^3.29.0",
|
101
|
-
"ts-loader": "^9.4.2",
|
102
|
-
"typescript": "^5.0.4"
|
103
|
-
},
|
104
|
-
"typedoc": {
|
105
|
-
"entryPoint": "./src/index.ts"
|
106
|
-
},
|
107
|
-
"files": [
|
108
|
-
"dist",
|
109
|
-
"bundle",
|
110
|
-
"src/**/*.ts",
|
111
|
-
"!**/*.spec.*",
|
112
|
-
"!**/*.json",
|
113
|
-
"CHANGELOG.md",
|
114
|
-
"LICENSE",
|
115
|
-
"README.md"
|
116
|
-
]
|
117
|
-
}
|
1
|
+
{"name":"@waku/message-encryption","version":"0.0.23-a42b7be.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"}},"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.25-a42b7be.0","@waku/interfaces":"0.0.20-a42b7be.0","@waku/proto":"0.0.6-a42b7be.0","@waku/utils":"0.0.13-a42b7be.0","debug":"^4.3.4","js-sha3":"^0.9.2"},"devDependencies":{"@rollup/plugin-commonjs":"^25.0.4","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^4.3.5","@types/mocha":"^10.0.1","@waku/build-utils":"*","chai":"^4.3.7","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/decoded_message.ts
CHANGED
@@ -11,13 +11,13 @@ export class DecodedMessage
|
|
11
11
|
private readonly _decodedPayload: Uint8Array;
|
12
12
|
|
13
13
|
constructor(
|
14
|
-
|
14
|
+
pubsubTopic: string,
|
15
15
|
proto: proto.WakuMessage,
|
16
16
|
decodedPayload: Uint8Array,
|
17
17
|
public signature?: Uint8Array,
|
18
18
|
public signaturePublicKey?: Uint8Array
|
19
19
|
) {
|
20
|
-
super(
|
20
|
+
super(pubsubTopic, proto);
|
21
21
|
this._decodedPayload = decodedPayload;
|
22
22
|
}
|
23
23
|
|
package/src/ecies.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
+
import { DefaultPubSubTopic } from "@waku/core";
|
1
2
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
2
|
-
import { IMetaSetter } from "@waku/interfaces";
|
3
|
+
import { IMetaSetter, PubSubTopic } from "@waku/interfaces";
|
3
4
|
import type {
|
4
5
|
EncoderOptions as BaseEncoderOptions,
|
5
6
|
IDecoder,
|
@@ -8,7 +9,7 @@ import type {
|
|
8
9
|
IProtoMessage
|
9
10
|
} from "@waku/interfaces";
|
10
11
|
import { WakuMessage } from "@waku/proto";
|
11
|
-
import
|
12
|
+
import { Logger } from "@waku/utils";
|
12
13
|
|
13
14
|
import { DecodedMessage } from "./decoded_message.js";
|
14
15
|
import {
|
@@ -28,10 +29,11 @@ import {
|
|
28
29
|
export { generatePrivateKey, getPublicKey };
|
29
30
|
export type { Encoder, Decoder, DecodedMessage };
|
30
31
|
|
31
|
-
const log =
|
32
|
+
const log = new Logger("message-encryption:ecies");
|
32
33
|
|
33
34
|
class Encoder implements IEncoder {
|
34
35
|
constructor(
|
36
|
+
public pubsubTopic: PubSubTopic,
|
35
37
|
public contentTopic: string,
|
36
38
|
private publicKey: Uint8Array,
|
37
39
|
private sigPrivKey?: Uint8Array,
|
@@ -88,13 +90,14 @@ export interface EncoderOptions extends BaseEncoderOptions {
|
|
88
90
|
*
|
89
91
|
* An encoder is used to encode messages in the [`14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
90
92
|
* format to be sent over the Waku network. The resulting encoder can then be
|
91
|
-
* pass to { @link @waku/interfaces.
|
92
|
-
* { @link @waku/interfaces.
|
93
|
+
* pass to { @link @waku/interfaces!ISender.send } or
|
94
|
+
* { @link @waku/interfaces!ISender.send } to automatically encrypt
|
93
95
|
* and encode outgoing messages.
|
94
96
|
* The payload can optionally be signed with the given private key as defined
|
95
97
|
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
96
98
|
*/
|
97
99
|
export function createEncoder({
|
100
|
+
pubsubTopic = DefaultPubSubTopic,
|
98
101
|
contentTopic,
|
99
102
|
publicKey,
|
100
103
|
sigPrivKey,
|
@@ -102,6 +105,7 @@ export function createEncoder({
|
|
102
105
|
metaSetter
|
103
106
|
}: EncoderOptions): Encoder {
|
104
107
|
return new Encoder(
|
108
|
+
pubsubTopic,
|
105
109
|
contentTopic,
|
106
110
|
publicKey,
|
107
111
|
sigPrivKey,
|
@@ -112,20 +116,21 @@ export function createEncoder({
|
|
112
116
|
|
113
117
|
class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
114
118
|
constructor(
|
119
|
+
pubsubTopic: PubSubTopic,
|
115
120
|
contentTopic: string,
|
116
121
|
private privateKey: Uint8Array
|
117
122
|
) {
|
118
|
-
super(contentTopic);
|
123
|
+
super(pubsubTopic, contentTopic);
|
119
124
|
}
|
120
125
|
|
121
126
|
async fromProtoObj(
|
122
|
-
|
127
|
+
pubsubTopic: string,
|
123
128
|
protoMessage: IProtoMessage
|
124
129
|
): Promise<DecodedMessage | undefined> {
|
125
130
|
const cipherPayload = protoMessage.payload;
|
126
131
|
|
127
132
|
if (protoMessage.version !== Version) {
|
128
|
-
log(
|
133
|
+
log.error(
|
129
134
|
"Failed to decrypt due to incorrect version, expected:",
|
130
135
|
Version,
|
131
136
|
", actual:",
|
@@ -139,7 +144,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
139
144
|
try {
|
140
145
|
payload = await decryptAsymmetric(cipherPayload, this.privateKey);
|
141
146
|
} catch (e) {
|
142
|
-
log(
|
147
|
+
log.error(
|
143
148
|
`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`,
|
144
149
|
e
|
145
150
|
);
|
@@ -147,20 +152,24 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
147
152
|
}
|
148
153
|
|
149
154
|
if (!payload) {
|
150
|
-
log(
|
155
|
+
log.error(
|
156
|
+
`Failed to decrypt payload for contentTopic ${this.contentTopic}`
|
157
|
+
);
|
151
158
|
return;
|
152
159
|
}
|
153
160
|
|
154
161
|
const res = postCipher(payload);
|
155
162
|
|
156
163
|
if (!res) {
|
157
|
-
log(
|
164
|
+
log.error(
|
165
|
+
`Failed to decode payload for contentTopic ${this.contentTopic}`
|
166
|
+
);
|
158
167
|
return;
|
159
168
|
}
|
160
169
|
|
161
|
-
log("Message decrypted", protoMessage);
|
170
|
+
log.info("Message decrypted", protoMessage);
|
162
171
|
return new DecodedMessage(
|
163
|
-
|
172
|
+
pubsubTopic,
|
164
173
|
protoMessage,
|
165
174
|
res.payload,
|
166
175
|
res.sig?.signature,
|
@@ -175,8 +184,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
175
184
|
*
|
176
185
|
* A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
177
186
|
* format when received from the Waku network. The resulting decoder can then be
|
178
|
-
* pass to { @link @waku/interfaces.
|
179
|
-
* { @link @waku/interfaces.Relay.subscribe } to automatically decrypt and
|
187
|
+
* pass to { @link @waku/interfaces!IReceiver.subscribe } to automatically decrypt and
|
180
188
|
* decode incoming messages.
|
181
189
|
*
|
182
190
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
@@ -184,7 +192,8 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
184
192
|
*/
|
185
193
|
export function createDecoder(
|
186
194
|
contentTopic: string,
|
187
|
-
privateKey: Uint8Array
|
195
|
+
privateKey: Uint8Array,
|
196
|
+
pubsubTopic: PubSubTopic = DefaultPubSubTopic
|
188
197
|
): Decoder {
|
189
|
-
return new Decoder(contentTopic, privateKey);
|
198
|
+
return new Decoder(pubsubTopic, contentTopic, privateKey);
|
190
199
|
}
|
package/src/symmetric.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
import { DefaultPubSubTopic } from "@waku/core";
|
1
2
|
import { Decoder as DecoderV0 } from "@waku/core/lib/message/version_0";
|
2
3
|
import type {
|
3
4
|
EncoderOptions as BaseEncoderOptions,
|
@@ -5,10 +6,11 @@ import type {
|
|
5
6
|
IEncoder,
|
6
7
|
IMessage,
|
7
8
|
IMetaSetter,
|
8
|
-
IProtoMessage
|
9
|
+
IProtoMessage,
|
10
|
+
PubSubTopic
|
9
11
|
} from "@waku/interfaces";
|
10
12
|
import { WakuMessage } from "@waku/proto";
|
11
|
-
import
|
13
|
+
import { Logger } from "@waku/utils";
|
12
14
|
|
13
15
|
import { DecodedMessage } from "./decoded_message.js";
|
14
16
|
import {
|
@@ -23,10 +25,11 @@ import { generateSymmetricKey, OneMillion, Version } from "./index.js";
|
|
23
25
|
export { generateSymmetricKey };
|
24
26
|
export type { DecodedMessage, Encoder, Decoder };
|
25
27
|
|
26
|
-
const log =
|
28
|
+
const log = new Logger("message-encryption:symmetric");
|
27
29
|
|
28
30
|
class Encoder implements IEncoder {
|
29
31
|
constructor(
|
32
|
+
public pubsubTopic: PubSubTopic,
|
30
33
|
public contentTopic: string,
|
31
34
|
private symKey: Uint8Array,
|
32
35
|
private sigPrivKey?: Uint8Array,
|
@@ -83,39 +86,47 @@ export interface EncoderOptions extends BaseEncoderOptions {
|
|
83
86
|
*
|
84
87
|
* An encoder is used to encode messages in the [`14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
85
88
|
* format to be sent over the Waku network. The resulting encoder can then be
|
86
|
-
* pass to { @link @waku/interfaces.
|
87
|
-
* { @link @waku/interfaces.Relay.send } to automatically encrypt
|
89
|
+
* pass to { @link @waku/interfaces!ISender.send } to automatically encrypt
|
88
90
|
* and encode outgoing messages.
|
89
91
|
*
|
90
92
|
* The payload can optionally be signed with the given private key as defined
|
91
93
|
* in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
92
94
|
*/
|
93
95
|
export function createEncoder({
|
96
|
+
pubsubTopic = DefaultPubSubTopic,
|
94
97
|
contentTopic,
|
95
98
|
symKey,
|
96
99
|
sigPrivKey,
|
97
100
|
ephemeral = false,
|
98
101
|
metaSetter
|
99
102
|
}: EncoderOptions): Encoder {
|
100
|
-
return new Encoder(
|
103
|
+
return new Encoder(
|
104
|
+
pubsubTopic,
|
105
|
+
contentTopic,
|
106
|
+
symKey,
|
107
|
+
sigPrivKey,
|
108
|
+
ephemeral,
|
109
|
+
metaSetter
|
110
|
+
);
|
101
111
|
}
|
102
112
|
|
103
113
|
class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
104
114
|
constructor(
|
115
|
+
pubsubTopic: PubSubTopic,
|
105
116
|
contentTopic: string,
|
106
117
|
private symKey: Uint8Array
|
107
118
|
) {
|
108
|
-
super(contentTopic);
|
119
|
+
super(pubsubTopic, contentTopic);
|
109
120
|
}
|
110
121
|
|
111
122
|
async fromProtoObj(
|
112
|
-
|
123
|
+
pubsubTopic: string,
|
113
124
|
protoMessage: IProtoMessage
|
114
125
|
): Promise<DecodedMessage | undefined> {
|
115
126
|
const cipherPayload = protoMessage.payload;
|
116
127
|
|
117
128
|
if (protoMessage.version !== Version) {
|
118
|
-
log(
|
129
|
+
log.error(
|
119
130
|
"Failed to decrypt due to incorrect version, expected:",
|
120
131
|
Version,
|
121
132
|
", actual:",
|
@@ -129,7 +140,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
129
140
|
try {
|
130
141
|
payload = await decryptSymmetric(cipherPayload, this.symKey);
|
131
142
|
} catch (e) {
|
132
|
-
log(
|
143
|
+
log.error(
|
133
144
|
`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`,
|
134
145
|
e
|
135
146
|
);
|
@@ -137,20 +148,24 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
137
148
|
}
|
138
149
|
|
139
150
|
if (!payload) {
|
140
|
-
log(
|
151
|
+
log.error(
|
152
|
+
`Failed to decrypt payload for contentTopic ${this.contentTopic}`
|
153
|
+
);
|
141
154
|
return;
|
142
155
|
}
|
143
156
|
|
144
157
|
const res = postCipher(payload);
|
145
158
|
|
146
159
|
if (!res) {
|
147
|
-
log(
|
160
|
+
log.error(
|
161
|
+
`Failed to decode payload for contentTopic ${this.contentTopic}`
|
162
|
+
);
|
148
163
|
return;
|
149
164
|
}
|
150
165
|
|
151
|
-
log("Message decrypted", protoMessage);
|
166
|
+
log.info("Message decrypted", protoMessage);
|
152
167
|
return new DecodedMessage(
|
153
|
-
|
168
|
+
pubsubTopic,
|
154
169
|
protoMessage,
|
155
170
|
res.payload,
|
156
171
|
res.sig?.signature,
|
@@ -165,8 +180,7 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
165
180
|
*
|
166
181
|
* A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
|
167
182
|
* format when received from the Waku network. The resulting decoder can then be
|
168
|
-
* pass to { @link @waku/interfaces.
|
169
|
-
* { @link @waku/interfaces.Relay.subscribe } to automatically decrypt and
|
183
|
+
* pass to { @link @waku/interfaces!IReceiver.subscribe } to automatically decrypt and
|
170
184
|
* decode incoming messages.
|
171
185
|
*
|
172
186
|
* @param contentTopic The resulting decoder will only decode messages with this content topic.
|
@@ -174,7 +188,8 @@ class Decoder extends DecoderV0 implements IDecoder<DecodedMessage> {
|
|
174
188
|
*/
|
175
189
|
export function createDecoder(
|
176
190
|
contentTopic: string,
|
177
|
-
symKey: Uint8Array
|
191
|
+
symKey: Uint8Array,
|
192
|
+
pubsubTopic: PubSubTopic = DefaultPubSubTopic
|
178
193
|
): Decoder {
|
179
|
-
return new Decoder(contentTopic, symKey);
|
194
|
+
return new Decoder(pubsubTopic, contentTopic, symKey);
|
180
195
|
}
|