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