@waku/message-encryption 0.0.26-b5e8b17.0 → 0.0.26-efe9b8d.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,4 +1,4 @@
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';
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-qyNI8P98.js';
2
2
 
3
3
  var index = /*#__PURE__*/Object.freeze({
4
4
  __proto__: null,
package/bundle/index.js CHANGED
@@ -1,127 +1,5 @@
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 };
1
+ export { i as crypto } from './index-DxtaUQnt.js';
2
+ export { e as ecies } from './ecies-pobP8Dyx.js';
3
+ export { s as symmetric } from './symmetric-HwbDJphw.js';
4
+ export { g as generatePrivateKey, a as generateSymmetricKey, b as getPublicKey } from './symmetric-qyNI8P98.js';
5
+ import './encryption-D5jL4iz1.js';
@@ -0,0 +1,124 @@
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-D5jL4iz1.js';
2
+ import { V as Version, O as OneMillion, a as generateSymmetricKey } from './symmetric-qyNI8P98.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 };
@@ -1036,17 +1036,6 @@ Object.defineProperties(utils, {
1036
1036
  },
1037
1037
  });
1038
1038
 
1039
- /**
1040
- * To guarantee Uint8Array semantics, convert nodejs Buffers
1041
- * into vanilla Uint8Arrays
1042
- */
1043
- function asUint8Array(buf) {
1044
- if (globalThis.Buffer != null) {
1045
- return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
1046
- }
1047
- return buf;
1048
- }
1049
-
1050
1039
  function coerce(o) {
1051
1040
  if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array')
1052
1041
  return o;
@@ -1671,15 +1660,19 @@ new TextDecoder();
1671
1660
 
1672
1661
  const bases = { ...identityBase, ...base2$1, ...base8$1, ...base10$1, ...base16$1, ...base32$1, ...base36$1, ...base58, ...base64$1, ...base256emoji$1 };
1673
1662
 
1663
+ /**
1664
+ * Returns a `Uint8Array` of the requested size. Referenced memory will
1665
+ * be initialized to 0.
1666
+ */
1667
+ function alloc(size = 0) {
1668
+ return new Uint8Array(size);
1669
+ }
1674
1670
  /**
1675
1671
  * Where possible returns a Uint8Array of the requested size that references
1676
1672
  * uninitialized memory. Only use if you are certain you will immediately
1677
1673
  * overwrite every value in the returned `Uint8Array`.
1678
1674
  */
1679
1675
  function allocUnsafe(size = 0) {
1680
- if (globalThis.Buffer?.allocUnsafe != null) {
1681
- return asUint8Array(globalThis.Buffer.allocUnsafe(size));
1682
- }
1683
1676
  return new Uint8Array(size);
1684
1677
  }
1685
1678
 
@@ -1740,9 +1733,6 @@ function fromString(string, encoding = 'utf8') {
1740
1733
  if (base == null) {
1741
1734
  throw new Error(`Unsupported encoding "${encoding}"`);
1742
1735
  }
1743
- if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) {
1744
- return asUint8Array(globalThis.Buffer.from(string, 'utf-8'));
1745
- }
1746
1736
  // add multibase prefix
1747
1737
  return base.decoder.decode(`${base.prefix}${string}`); // eslint-disable-line @typescript-eslint/restrict-template-expressions
1748
1738
  }
@@ -1788,7 +1778,7 @@ var sha3$1 = {exports: {}};
1788
1778
  /**
1789
1779
  * [js-sha3]{@link https://github.com/emn178/js-sha3}
1790
1780
  *
1791
- * @version 0.9.2
1781
+ * @version 0.9.3
1792
1782
  * @author Chen, Yi-Cyuan [emn178@gmail.com]
1793
1783
  * @copyright Chen, Yi-Cyuan 2015-2023
1794
1784
  * @license MIT
@@ -1868,6 +1858,14 @@ var sha3$1 = {exports: {}};
1868
1858
  return formatMessage(message)[0].length === 0;
1869
1859
  };
1870
1860
 
1861
+ var cloneArray = function (array) {
1862
+ var newArray = [];
1863
+ for (var i = 0; i < array.length; ++i) {
1864
+ newArray[i] = array[i];
1865
+ }
1866
+ return newArray;
1867
+ };
1868
+
1871
1869
  var createOutputMethod = function (bits, padding, outputType) {
1872
1870
  return function (message) {
1873
1871
  return new Keccak(bits, padding, bits).update(message)[outputType]();
@@ -2147,6 +2145,7 @@ var sha3$1 = {exports: {}};
2147
2145
  HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F];
2148
2146
  }
2149
2147
  if (j % blockCount === 0) {
2148
+ s = cloneArray(s);
2150
2149
  f(s);
2151
2150
  i = 0;
2152
2151
  }
@@ -2182,11 +2181,12 @@ var sha3$1 = {exports: {}};
2182
2181
  array[j] = s[i];
2183
2182
  }
2184
2183
  if (j % blockCount === 0) {
2184
+ s = cloneArray(s);
2185
2185
  f(s);
2186
2186
  }
2187
2187
  }
2188
2188
  if (extraBytes) {
2189
- array[i] = s[i];
2189
+ array[j] = s[i];
2190
2190
  buffer = buffer.slice(0, bytes);
2191
2191
  }
2192
2192
  return buffer;
@@ -2210,6 +2210,7 @@ var sha3$1 = {exports: {}};
2210
2210
  array[offset + 3] = (block >> 24) & 0xFF;
2211
2211
  }
2212
2212
  if (j % blockCount === 0) {
2213
+ s = cloneArray(s);
2213
2214
  f(s);
2214
2215
  }
2215
2216
  }
@@ -2445,7 +2446,7 @@ const Symmetric = {
2445
2446
  const Asymmetric = {
2446
2447
  keySize: 32
2447
2448
  };
2448
- const OneMillion = BigInt(1000000);
2449
+ const OneMillion = BigInt(1_000_000);
2449
2450
  const Version = 1;
2450
2451
 
2451
2452
  const crypto = {
@@ -2655,4 +2656,4 @@ var symmetric = /*#__PURE__*/Object.freeze({
2655
2656
  generateIv: generateIv
2656
2657
  });
2657
2658
 
2658
- export { OneMillion as O, Symmetric as S, Version as V, generateSymmetricKey as a, getPublicKey as b, allocUnsafe as c, concat as d, getDefaultExportFromCjs as e, fromString as f, generatePrivateKey as g, encrypt$1 as h, hexToBytes as i, decrypt$1 as j, generateIv as k, encrypt as l, decrypt as m, keccak256 as n, Signature as o, recoverPublicKey as p, ecies as q, randomBytes as r, sign as s, getSubtle as t, utf8ToBytes as u, sha256 as v, symmetric as w };
2659
+ export { OneMillion as O, Symmetric as S, Version as V, generateSymmetricKey as a, getPublicKey as b, getSubtle as c, sign as d, ecies as e, symmetric as f, generatePrivateKey as g, allocUnsafe as h, fromString as i, alloc as j, keccak256 as k, concat as l, getDefaultExportFromCjs as m, encrypt$1 as n, hexToBytes as o, decrypt$1 as p, generateIv as q, randomBytes as r, sha256 as s, encrypt as t, utf8ToBytes as u, decrypt as v, Signature as w, recoverPublicKey as x };
@@ -1,2 +1,3 @@
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
+ export { h as decryptSymmetric, g as encryptSymmetric, c as postCipher, p as preCipher } from './encryption-D5jL4iz1.js';
2
+ export { a as generateSymmetricKey } from './symmetric-qyNI8P98.js';
3
+ export { a as createDecoder, c as createEncoder } from './symmetric-HwbDJphw.js';