@waku/message-encryption 0.0.24 → 0.0.26-434be7b.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 CHANGED
@@ -68,6 +68,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
68
68
  * @waku/interfaces bumped from 0.0.17 to 0.0.18
69
69
  * @waku/utils bumped from 0.0.10 to 0.0.11
70
70
 
71
+ ### Dependencies
72
+
73
+ * The following workspace dependencies were updated
74
+ * dependencies
75
+ * @waku/core bumped from 0.0.26 to 0.0.27
76
+ * @waku/interfaces bumped from 0.0.21 to 0.0.22
77
+ * @waku/utils bumped from 0.0.14 to 0.0.15
78
+
71
79
  ## [0.0.24](https://github.com/waku-org/js-waku/compare/message-encryption-v0.0.23...message-encryption-v0.0.24) (2024-01-10)
72
80
 
73
81
 
package/bundle/crypto.js CHANGED
@@ -1 +1 @@
1
- export { e as ecies, g as generatePrivateKey, a as generateSymmetricKey, b as getPublicKey, c as getSubtle, k as keccak256, r as randomBytes, s as sha256, d as sign, f as symmetric } from './symmetric-CXVjdTdV.js';
1
+ export { q as ecies, g as generatePrivateKey, a as generateSymmetricKey, b as getPublicKey, t as getSubtle, n as keccak256, r as randomBytes, v as sha256, s as sign, w as symmetric } from './symmetric-TylJB-2X.js';
package/bundle/ecies.js CHANGED
@@ -1,3 +1,2 @@
1
- export { b as decryptAsymmetric, e as encryptAsymmetric, c as postCipher, p as preCipher } from './encryption-zFGfcjHZ.js';
2
- export { g as generatePrivateKey } from './symmetric-CXVjdTdV.js';
3
- export { a as createDecoder, c as createEncoder } from './ecies-R65pUoo3.js';
1
+ export { j as createDecoder, i as createEncoder, g as decryptSymmetric, h as encryptSymmetric, c as postCipher, p as preCipher } from './symmetric-BtVudDdW.js';
2
+ export { a as generateSymmetricKey } from './symmetric-TylJB-2X.js';
@@ -1,4 +1,4 @@
1
- import { e as ecies, g as generatePrivateKey, a as generateSymmetricKey, b as getPublicKey, c as getSubtle, k as keccak256, r as randomBytes, s as sha256, d as sign, f as symmetric } from './symmetric-CXVjdTdV.js';
1
+ import { q as ecies, g as generatePrivateKey, a as generateSymmetricKey, b as getPublicKey, t as getSubtle, n as keccak256, r as randomBytes, v as sha256, s as sign, w as symmetric } from './symmetric-TylJB-2X.js';
2
2
 
3
3
  var index = /*#__PURE__*/Object.freeze({
4
4
  __proto__: null,
package/bundle/index.js CHANGED
@@ -1,5 +1,127 @@
1
- export { i as crypto } from './index-alzPvot7.js';
2
- export { e as ecies } from './ecies-R65pUoo3.js';
3
- export { s as symmetric } from './symmetric-7aAyizy_.js';
4
- export { g as generatePrivateKey, a as generateSymmetricKey, b as getPublicKey } from './symmetric-CXVjdTdV.js';
5
- import './encryption-zFGfcjHZ.js';
1
+ export { i as crypto } from './index-Caa7SScj.js';
2
+ import { L as Logger, d as determinePubsubTopic, D as DefaultPubsubTopic, W as WakuMessage, p as preCipher, e as encryptAsymmetric, a as Decoder$1, b as decryptAsymmetric, c as postCipher, f as DecodedMessage } from './symmetric-BtVudDdW.js';
3
+ export { s as symmetric } from './symmetric-BtVudDdW.js';
4
+ import { V as Version, O as OneMillion, g as generatePrivateKey } from './symmetric-TylJB-2X.js';
5
+ export { a as generateSymmetricKey, b as getPublicKey } from './symmetric-TylJB-2X.js';
6
+
7
+ const log = new Logger("message-encryption:ecies");
8
+ class Encoder {
9
+ pubsubTopic;
10
+ contentTopic;
11
+ publicKey;
12
+ sigPrivKey;
13
+ ephemeral;
14
+ metaSetter;
15
+ constructor(pubsubTopic, contentTopic, publicKey, sigPrivKey, ephemeral = false, metaSetter) {
16
+ this.pubsubTopic = pubsubTopic;
17
+ this.contentTopic = contentTopic;
18
+ this.publicKey = publicKey;
19
+ this.sigPrivKey = sigPrivKey;
20
+ this.ephemeral = ephemeral;
21
+ this.metaSetter = metaSetter;
22
+ if (!contentTopic || contentTopic === "") {
23
+ throw new Error("Content topic must be specified");
24
+ }
25
+ }
26
+ async toWire(message) {
27
+ const protoMessage = await this.toProtoObj(message);
28
+ if (!protoMessage)
29
+ return;
30
+ return WakuMessage.encode(protoMessage);
31
+ }
32
+ async toProtoObj(message) {
33
+ const timestamp = message.timestamp ?? new Date();
34
+ const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
35
+ const payload = await encryptAsymmetric(preparedPayload, this.publicKey);
36
+ const protoMessage = {
37
+ payload,
38
+ version: Version,
39
+ contentTopic: this.contentTopic,
40
+ timestamp: BigInt(timestamp.valueOf()) * OneMillion,
41
+ meta: undefined,
42
+ rateLimitProof: message.rateLimitProof,
43
+ ephemeral: this.ephemeral
44
+ };
45
+ if (this.metaSetter) {
46
+ const meta = this.metaSetter(protoMessage);
47
+ return { ...protoMessage, meta };
48
+ }
49
+ return protoMessage;
50
+ }
51
+ }
52
+ /**
53
+ * Creates an encoder that encrypts messages using ECIES for the given public,
54
+ * as defined in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
55
+ *
56
+ * An encoder is used to encode messages in the [`14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
57
+ * format to be sent over the Waku network. The resulting encoder can then be
58
+ * pass to { @link @waku/interfaces!ISender.send } or
59
+ * { @link @waku/interfaces!ISender.send } to automatically encrypt
60
+ * and encode outgoing messages.
61
+ * The payload can optionally be signed with the given private key as defined
62
+ * in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
63
+ */
64
+ function createEncoder({ pubsubTopic, pubsubTopicShardInfo, contentTopic, publicKey, sigPrivKey, ephemeral = false, metaSetter }) {
65
+ return new Encoder(determinePubsubTopic(contentTopic, pubsubTopic ?? pubsubTopicShardInfo), contentTopic, publicKey, sigPrivKey, ephemeral, metaSetter);
66
+ }
67
+ class Decoder extends Decoder$1 {
68
+ privateKey;
69
+ constructor(pubsubTopic, contentTopic, privateKey) {
70
+ super(pubsubTopic, contentTopic);
71
+ this.privateKey = privateKey;
72
+ }
73
+ async fromProtoObj(pubsubTopic, protoMessage) {
74
+ const cipherPayload = protoMessage.payload;
75
+ if (protoMessage.version !== Version) {
76
+ log.error("Failed to decrypt due to incorrect version, expected:", Version, ", actual:", protoMessage.version);
77
+ return;
78
+ }
79
+ let payload;
80
+ try {
81
+ payload = await decryptAsymmetric(cipherPayload, this.privateKey);
82
+ }
83
+ catch (e) {
84
+ log.error(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`, e);
85
+ return;
86
+ }
87
+ if (!payload) {
88
+ log.error(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);
89
+ return;
90
+ }
91
+ const res = postCipher(payload);
92
+ if (!res) {
93
+ log.error(`Failed to decode payload for contentTopic ${this.contentTopic}`);
94
+ return;
95
+ }
96
+ log.info("Message decrypted", protoMessage);
97
+ return new DecodedMessage(pubsubTopic, protoMessage, res.payload, res.sig?.signature, res.sig?.publicKey);
98
+ }
99
+ }
100
+ /**
101
+ * Creates a decoder that decrypts messages using ECIES, using the given private
102
+ * key as defined in [26/WAKU2-PAYLOAD](https://rfc.vac.dev/spec/26/).
103
+ *
104
+ * A decoder is used to decode messages from the [14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14/)
105
+ * format when received from the Waku network. The resulting decoder can then be
106
+ * pass to { @link @waku/interfaces!IReceiver.subscribe } to automatically decrypt and
107
+ * decode incoming messages.
108
+ *
109
+ * @param contentTopic The resulting decoder will only decode messages with this content topic.
110
+ * @param privateKey The private key used to decrypt the message.
111
+ */
112
+ function createDecoder(contentTopic, privateKey, pubsubTopicShardInfo = DefaultPubsubTopic) {
113
+ return new Decoder(determinePubsubTopic(contentTopic, pubsubTopicShardInfo), contentTopic, privateKey);
114
+ }
115
+
116
+ var ecies = /*#__PURE__*/Object.freeze({
117
+ __proto__: null,
118
+ createDecoder: createDecoder,
119
+ createEncoder: createEncoder,
120
+ decryptAsymmetric: decryptAsymmetric,
121
+ encryptAsymmetric: encryptAsymmetric,
122
+ generatePrivateKey: generatePrivateKey,
123
+ postCipher: postCipher,
124
+ preCipher: preCipher
125
+ });
126
+
127
+ export { ecies, generatePrivateKey };