@waku/core 0.0.1 → 0.0.2
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 +9 -0
- package/bundle/{index-691c0be6.js → index-a67d7136.js} +1 -1
- package/bundle/{index-0a4bdddc.js → index-f7e049ad.js} +1 -1
- package/bundle/index.js +24853 -3555
- package/bundle/lib/peer_discovery_static_list.js +18 -4
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/lib/wait_for_remote_peer.js +2 -3
- package/bundle/lib/waku_message/topic_only_message.js +2 -3
- package/bundle/lib/waku_message/version_0.js +317 -4
- package/bundle/{message-e2db79d7.js → message-049c8b67.js} +861 -2
- package/bundle/{topic_only_message-34f36fa6.js → topic_only_message-5ad3a869.js} +1 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/waku_filter/index.d.ts +2 -2
- package/dist/lib/waku_filter/index.js.map +1 -1
- package/dist/lib/waku_message/version_0.d.ts +3 -2
- package/dist/lib/waku_message/version_0.js +2 -1
- package/dist/lib/waku_message/version_0.js.map +1 -1
- package/dist/lib/waku_relay/index.d.ts +4 -3
- package/dist/lib/waku_relay/index.js.map +1 -1
- package/dist/lib/waku_store/index.d.ts +4 -4
- package/package.json +3 -118
- package/src/index.ts +0 -10
- package/src/lib/waku_filter/index.ts +4 -3
- package/src/lib/waku_message/version_0.ts +5 -3
- package/src/lib/waku_relay/index.ts +4 -3
- package/src/lib/waku_store/index.ts +5 -5
- package/bundle/crypto-8551d579.js +0 -2585
- package/bundle/crypto-b00764b7.js +0 -1772
- package/bundle/enr-564d4a51.js +0 -20785
- package/bundle/enr-9fc5eed8.js +0 -20786
- package/bundle/enr-f6e82a53.js +0 -20785
- package/bundle/events-fcbda4dc.js +0 -76
- package/bundle/index-02d21809.js +0 -20
- package/bundle/index-2ae915be.js +0 -1854
- package/bundle/index-a013a259.js +0 -20
- package/bundle/index-ba42b4fc.js +0 -862
- package/bundle/lib/enr.js +0 -8
- package/bundle/lib/peer_discovery_dns.js +0 -5018
- package/bundle/lib/utils.js +0 -1
- package/bundle/lib/waku_message/version_1.js +0 -463
- package/bundle/multiaddr_to_peer_info-c406b1e1.js +0 -19
- package/bundle/multiaddr_to_peer_info-fd1de516.js +0 -19
- package/bundle/utils-9a3221f2.js +0 -815
- package/bundle/version_0-e6fe440c.js +0 -317
- package/dist/lib/crypto.d.ts +0 -34
- package/dist/lib/crypto.js +0 -79
- package/dist/lib/crypto.js.map +0 -1
- package/dist/lib/enr/constants.d.ts +0 -4
- package/dist/lib/enr/constants.js +0 -8
- package/dist/lib/enr/constants.js.map +0 -1
- package/dist/lib/enr/enr.d.ts +0 -90
- package/dist/lib/enr/enr.js +0 -432
- package/dist/lib/enr/enr.js.map +0 -1
- package/dist/lib/enr/index.d.ts +0 -5
- package/dist/lib/enr/index.js +0 -6
- package/dist/lib/enr/index.js.map +0 -1
- package/dist/lib/enr/keypair/index.d.ts +0 -8
- package/dist/lib/enr/keypair/index.js +0 -53
- package/dist/lib/enr/keypair/index.js.map +0 -1
- package/dist/lib/enr/keypair/secp256k1.d.ts +0 -13
- package/dist/lib/enr/keypair/secp256k1.js +0 -57
- package/dist/lib/enr/keypair/secp256k1.js.map +0 -1
- package/dist/lib/enr/keypair/types.d.ts +0 -13
- package/dist/lib/enr/keypair/types.js +0 -7
- package/dist/lib/enr/keypair/types.js.map +0 -1
- package/dist/lib/enr/multiaddr_from_fields.d.ts +0 -2
- package/dist/lib/enr/multiaddr_from_fields.js +0 -8
- package/dist/lib/enr/multiaddr_from_fields.js.map +0 -1
- package/dist/lib/enr/multiaddrs_codec.d.ts +0 -3
- package/dist/lib/enr/multiaddrs_codec.js +0 -32
- package/dist/lib/enr/multiaddrs_codec.js.map +0 -1
- package/dist/lib/enr/types.d.ts +0 -8
- package/dist/lib/enr/types.js +0 -3
- package/dist/lib/enr/types.js.map +0 -1
- package/dist/lib/enr/v4.d.ts +0 -3
- package/dist/lib/enr/v4.js +0 -14
- package/dist/lib/enr/v4.js.map +0 -1
- package/dist/lib/enr/waku2_codec.d.ts +0 -8
- package/dist/lib/enr/waku2_codec.js +0 -36
- package/dist/lib/enr/waku2_codec.js.map +0 -1
- package/dist/lib/peer_discovery_dns/dns.d.ts +0 -48
- package/dist/lib/peer_discovery_dns/dns.js +0 -158
- package/dist/lib/peer_discovery_dns/dns.js.map +0 -1
- package/dist/lib/peer_discovery_dns/dns_over_https.d.ts +0 -32
- package/dist/lib/peer_discovery_dns/dns_over_https.js +0 -87
- package/dist/lib/peer_discovery_dns/dns_over_https.js.map +0 -1
- package/dist/lib/peer_discovery_dns/enrtree.d.ts +0 -33
- package/dist/lib/peer_discovery_dns/enrtree.js +0 -76
- package/dist/lib/peer_discovery_dns/enrtree.js.map +0 -1
- package/dist/lib/peer_discovery_dns/fetch_nodes.d.ts +0 -14
- package/dist/lib/peer_discovery_dns/fetch_nodes.js +0 -133
- package/dist/lib/peer_discovery_dns/fetch_nodes.js.map +0 -1
- package/dist/lib/peer_discovery_dns/index.d.ts +0 -30
- package/dist/lib/peer_discovery_dns/index.js +0 -54
- package/dist/lib/peer_discovery_dns/index.js.map +0 -1
- package/dist/lib/utils.d.ts +0 -22
- package/dist/lib/utils.js +0 -40
- package/dist/lib/utils.js.map +0 -1
- package/dist/lib/waku_message/constants.d.ts +0 -12
- package/dist/lib/waku_message/constants.js +0 -10
- package/dist/lib/waku_message/constants.js.map +0 -1
- package/dist/lib/waku_message/ecies.d.ts +0 -17
- package/dist/lib/waku_message/ecies.js +0 -126
- package/dist/lib/waku_message/ecies.js.map +0 -1
- package/dist/lib/waku_message/symmetric.d.ts +0 -3
- package/dist/lib/waku_message/symmetric.js +0 -18
- package/dist/lib/waku_message/symmetric.js.map +0 -1
- package/dist/lib/waku_message/version_1.d.ts +0 -93
- package/dist/lib/waku_message/version_1.js +0 -325
- package/dist/lib/waku_message/version_1.js.map +0 -1
- package/src/lib/crypto.ts +0 -100
- package/src/lib/enr/constants.ts +0 -10
- package/src/lib/enr/enr.ts +0 -516
- package/src/lib/enr/index.ts +0 -5
- package/src/lib/enr/keypair/index.ts +0 -76
- package/src/lib/enr/keypair/secp256k1.ts +0 -69
- package/src/lib/enr/keypair/types.ts +0 -14
- package/src/lib/enr/multiaddr_from_fields.ts +0 -18
- package/src/lib/enr/multiaddrs_codec.ts +0 -50
- package/src/lib/enr/types.ts +0 -11
- package/src/lib/enr/v4.ts +0 -22
- package/src/lib/enr/waku2_codec.ts +0 -39
- package/src/lib/peer_discovery_dns/dns.ts +0 -223
- package/src/lib/peer_discovery_dns/dns_over_https.ts +0 -98
- package/src/lib/peer_discovery_dns/enrtree.ts +0 -123
- package/src/lib/peer_discovery_dns/fetch_nodes.ts +0 -180
- package/src/lib/peer_discovery_dns/index.ts +0 -84
- package/src/lib/utils.ts +0 -50
- package/src/lib/waku_message/constants.ts +0 -10
- package/src/lib/waku_message/ecies.ts +0 -194
- package/src/lib/waku_message/symmetric.ts +0 -33
- package/src/lib/waku_message/version_1.ts +0 -457
package/bundle/lib/utils.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export { d as bytesToHex, g as bytesToUtf8, c as concat, h as hexToBytes, e as utf8ToBytes } from '../utils-9a3221f2.js';
|
@@ -1,463 +0,0 @@
|
|
1
|
-
import { r as randomBytes, f as sha256, c as getPublicKey, h as getSubtle, i as getSharedSecret, S as Symmetric, k as keccak256, j as sign, l as Signature, m as recoverPublicKey } from '../../crypto-b00764b7.js';
|
2
|
-
import { d as debug } from '../../browser-1e1a2f27.js';
|
3
|
-
import { M as MessageV0, W as WakuMessage, D as DecoderV0 } from '../../version_0-e6fe440c.js';
|
4
|
-
import { c as concat, h as hexToBytes } from '../../utils-9a3221f2.js';
|
5
|
-
import '../../index-ba42b4fc.js';
|
6
|
-
import '../../message-e2db79d7.js';
|
7
|
-
|
8
|
-
/**
|
9
|
-
* HKDF as implemented in go-ethereum.
|
10
|
-
*/
|
11
|
-
function kdf(secret, outputLength) {
|
12
|
-
let ctr = 1;
|
13
|
-
let written = 0;
|
14
|
-
let willBeResult = Promise.resolve(new Uint8Array());
|
15
|
-
while (written < outputLength) {
|
16
|
-
const counters = new Uint8Array([ctr >> 24, ctr >> 16, ctr >> 8, ctr]);
|
17
|
-
const countersSecret = concat([counters, secret], counters.length + secret.length);
|
18
|
-
const willBeHashResult = sha256(countersSecret);
|
19
|
-
willBeResult = willBeResult.then((result) => willBeHashResult.then((hashResult) => {
|
20
|
-
const _hashResult = new Uint8Array(hashResult);
|
21
|
-
return concat([result, _hashResult], result.length + _hashResult.length);
|
22
|
-
}));
|
23
|
-
written += 32;
|
24
|
-
ctr += 1;
|
25
|
-
}
|
26
|
-
return willBeResult;
|
27
|
-
}
|
28
|
-
function aesCtrEncrypt(counter, key, data) {
|
29
|
-
return getSubtle()
|
30
|
-
.importKey("raw", key, "AES-CTR", false, ["encrypt"])
|
31
|
-
.then((cryptoKey) => getSubtle().encrypt({ name: "AES-CTR", counter: counter, length: 128 }, cryptoKey, data))
|
32
|
-
.then((bytes) => new Uint8Array(bytes));
|
33
|
-
}
|
34
|
-
function aesCtrDecrypt(counter, key, data) {
|
35
|
-
return getSubtle()
|
36
|
-
.importKey("raw", key, "AES-CTR", false, ["decrypt"])
|
37
|
-
.then((cryptoKey) => getSubtle().decrypt({ name: "AES-CTR", counter: counter, length: 128 }, cryptoKey, data))
|
38
|
-
.then((bytes) => new Uint8Array(bytes));
|
39
|
-
}
|
40
|
-
function hmacSha256Sign(key, msg) {
|
41
|
-
const algorithm = { name: "HMAC", hash: { name: "SHA-256" } };
|
42
|
-
return getSubtle()
|
43
|
-
.importKey("raw", key, algorithm, false, ["sign"])
|
44
|
-
.then((cryptoKey) => getSubtle().sign(algorithm, cryptoKey, msg))
|
45
|
-
.then((bytes) => new Uint8Array(bytes));
|
46
|
-
}
|
47
|
-
function hmacSha256Verify(key, msg, sig) {
|
48
|
-
const algorithm = { name: "HMAC", hash: { name: "SHA-256" } };
|
49
|
-
const _key = getSubtle().importKey("raw", key, algorithm, false, ["verify"]);
|
50
|
-
return _key.then((cryptoKey) => getSubtle().verify(algorithm, cryptoKey, sig, msg));
|
51
|
-
}
|
52
|
-
/**
|
53
|
-
* Derive shared secret for given private and public keys.
|
54
|
-
*
|
55
|
-
* @param privateKeyA Sender's private key (32 bytes)
|
56
|
-
* @param publicKeyB Recipient's public key (65 bytes)
|
57
|
-
* @returns A promise that resolves with the derived shared secret (Px, 32 bytes)
|
58
|
-
* @throws Error If arguments are invalid
|
59
|
-
*/
|
60
|
-
function derive(privateKeyA, publicKeyB) {
|
61
|
-
if (privateKeyA.length !== 32) {
|
62
|
-
throw new Error(`Bad private key, it should be 32 bytes but it's actually ${privateKeyA.length} bytes long`);
|
63
|
-
}
|
64
|
-
else if (publicKeyB.length !== 65) {
|
65
|
-
throw new Error(`Bad public key, it should be 65 bytes but it's actually ${publicKeyB.length} bytes long`);
|
66
|
-
}
|
67
|
-
else if (publicKeyB[0] !== 4) {
|
68
|
-
throw new Error("Bad public key, a valid public key would begin with 4");
|
69
|
-
}
|
70
|
-
else {
|
71
|
-
const px = getSharedSecret(privateKeyA, publicKeyB, true);
|
72
|
-
// Remove the compression prefix
|
73
|
-
return new Uint8Array(hexToBytes(px).slice(1));
|
74
|
-
}
|
75
|
-
}
|
76
|
-
/**
|
77
|
-
* Encrypt message for given recipient's public key.
|
78
|
-
*
|
79
|
-
* @param publicKeyTo Recipient's public key (65 bytes)
|
80
|
-
* @param msg The message being encrypted
|
81
|
-
* @return A promise that resolves with the ECIES structure serialized
|
82
|
-
*/
|
83
|
-
async function encrypt$1(publicKeyTo, msg) {
|
84
|
-
const ephemPrivateKey = randomBytes(32);
|
85
|
-
const sharedPx = await derive(ephemPrivateKey, publicKeyTo);
|
86
|
-
const hash = await kdf(sharedPx, 32);
|
87
|
-
const iv = randomBytes(16);
|
88
|
-
const encryptionKey = hash.slice(0, 16);
|
89
|
-
const cipherText = await aesCtrEncrypt(iv, encryptionKey, msg);
|
90
|
-
const ivCipherText = concat([iv, cipherText], iv.length + cipherText.length);
|
91
|
-
const macKey = await sha256(hash.slice(16));
|
92
|
-
const hmac = await hmacSha256Sign(macKey, ivCipherText);
|
93
|
-
const ephemPublicKey = getPublicKey(ephemPrivateKey, false);
|
94
|
-
return concat([ephemPublicKey, ivCipherText, hmac], ephemPublicKey.length + ivCipherText.length + hmac.length);
|
95
|
-
}
|
96
|
-
const metaLength = 1 + 64 + 16 + 32;
|
97
|
-
/**
|
98
|
-
* Decrypt message using given private key.
|
99
|
-
*
|
100
|
-
* @param privateKey A 32-byte private key of recipient of the message
|
101
|
-
* @param encrypted ECIES serialized structure (result of ECIES encryption)
|
102
|
-
* @returns The clear text
|
103
|
-
* @throws Error If decryption fails
|
104
|
-
*/
|
105
|
-
async function decrypt$1(privateKey, encrypted) {
|
106
|
-
if (encrypted.length <= metaLength) {
|
107
|
-
throw new Error(`Invalid Ciphertext. Data is too small. It should ba at least ${metaLength} bytes`);
|
108
|
-
}
|
109
|
-
else if (encrypted[0] !== 4) {
|
110
|
-
throw new Error(`Not a valid ciphertext. It should begin with 4 but actually begin with ${encrypted[0]}`);
|
111
|
-
}
|
112
|
-
else {
|
113
|
-
// deserialize
|
114
|
-
const ephemPublicKey = encrypted.slice(0, 65);
|
115
|
-
const cipherTextLength = encrypted.length - metaLength;
|
116
|
-
const iv = encrypted.slice(65, 65 + 16);
|
117
|
-
const cipherAndIv = encrypted.slice(65, 65 + 16 + cipherTextLength);
|
118
|
-
const ciphertext = cipherAndIv.slice(16);
|
119
|
-
const msgMac = encrypted.slice(65 + 16 + cipherTextLength);
|
120
|
-
// check HMAC
|
121
|
-
const px = derive(privateKey, ephemPublicKey);
|
122
|
-
const hash = await kdf(px, 32);
|
123
|
-
const [encryptionKey, macKey] = await sha256(hash.slice(16)).then((macKey) => [hash.slice(0, 16), macKey]);
|
124
|
-
if (!(await hmacSha256Verify(macKey, cipherAndIv, msgMac))) {
|
125
|
-
throw new Error("Incorrect MAC");
|
126
|
-
}
|
127
|
-
return aesCtrDecrypt(iv, encryptionKey, ciphertext);
|
128
|
-
}
|
129
|
-
}
|
130
|
-
|
131
|
-
async function encrypt(iv, key, clearText) {
|
132
|
-
return getSubtle()
|
133
|
-
.importKey("raw", key, Symmetric.algorithm, false, ["encrypt"])
|
134
|
-
.then((cryptoKey) => getSubtle().encrypt({ iv, ...Symmetric.algorithm }, cryptoKey, clearText))
|
135
|
-
.then((cipher) => new Uint8Array(cipher));
|
136
|
-
}
|
137
|
-
async function decrypt(iv, key, cipherText) {
|
138
|
-
return getSubtle()
|
139
|
-
.importKey("raw", key, Symmetric.algorithm, false, ["decrypt"])
|
140
|
-
.then((cryptoKey) => getSubtle().decrypt({ iv, ...Symmetric.algorithm }, cryptoKey, cipherText))
|
141
|
-
.then((clear) => new Uint8Array(clear));
|
142
|
-
}
|
143
|
-
function generateIv() {
|
144
|
-
return randomBytes(Symmetric.ivSize);
|
145
|
-
}
|
146
|
-
|
147
|
-
const log = debug("waku:message:version-1");
|
148
|
-
const FlagsLength = 1;
|
149
|
-
const FlagMask = 3; // 0011
|
150
|
-
const IsSignedMask = 4; // 0100
|
151
|
-
const PaddingTarget = 256;
|
152
|
-
const SignatureLength = 65;
|
153
|
-
const OneMillion = BigInt(1000000);
|
154
|
-
const Version = 1;
|
155
|
-
class MessageV1 extends MessageV0 {
|
156
|
-
constructor(proto, decodedPayload, signature, signaturePublicKey) {
|
157
|
-
super(proto);
|
158
|
-
this.signature = signature;
|
159
|
-
this.signaturePublicKey = signaturePublicKey;
|
160
|
-
this._decodedPayload = decodedPayload;
|
161
|
-
}
|
162
|
-
get payload() {
|
163
|
-
return this._decodedPayload;
|
164
|
-
}
|
165
|
-
}
|
166
|
-
class AsymEncoder {
|
167
|
-
constructor(contentTopic, publicKey, sigPrivKey) {
|
168
|
-
this.contentTopic = contentTopic;
|
169
|
-
this.publicKey = publicKey;
|
170
|
-
this.sigPrivKey = sigPrivKey;
|
171
|
-
}
|
172
|
-
async toWire(message$1) {
|
173
|
-
const protoMessage = await this.toProtoObj(message$1);
|
174
|
-
if (!protoMessage)
|
175
|
-
return;
|
176
|
-
return WakuMessage.encode(protoMessage);
|
177
|
-
}
|
178
|
-
async toProtoObj(message) {
|
179
|
-
const timestamp = message.timestamp ?? new Date();
|
180
|
-
if (!message.payload) {
|
181
|
-
log("No payload to encrypt, skipping: ", message);
|
182
|
-
return;
|
183
|
-
}
|
184
|
-
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
|
185
|
-
const payload = await encryptAsymmetric(preparedPayload, this.publicKey);
|
186
|
-
return {
|
187
|
-
payload,
|
188
|
-
version: Version,
|
189
|
-
contentTopic: this.contentTopic,
|
190
|
-
timestamp: BigInt(timestamp.valueOf()) * OneMillion,
|
191
|
-
rateLimitProof: message.rateLimitProof,
|
192
|
-
};
|
193
|
-
}
|
194
|
-
}
|
195
|
-
class SymEncoder {
|
196
|
-
constructor(contentTopic, symKey, sigPrivKey) {
|
197
|
-
this.contentTopic = contentTopic;
|
198
|
-
this.symKey = symKey;
|
199
|
-
this.sigPrivKey = sigPrivKey;
|
200
|
-
}
|
201
|
-
async toWire(message$1) {
|
202
|
-
const protoMessage = await this.toProtoObj(message$1);
|
203
|
-
if (!protoMessage)
|
204
|
-
return;
|
205
|
-
return WakuMessage.encode(protoMessage);
|
206
|
-
}
|
207
|
-
async toProtoObj(message) {
|
208
|
-
const timestamp = message.timestamp ?? new Date();
|
209
|
-
if (!message.payload) {
|
210
|
-
log("No payload to encrypt, skipping: ", message);
|
211
|
-
return;
|
212
|
-
}
|
213
|
-
const preparedPayload = await preCipher(message.payload, this.sigPrivKey);
|
214
|
-
const payload = await encryptSymmetric(preparedPayload, this.symKey);
|
215
|
-
return {
|
216
|
-
payload,
|
217
|
-
version: Version,
|
218
|
-
contentTopic: this.contentTopic,
|
219
|
-
timestamp: BigInt(timestamp.valueOf()) * OneMillion,
|
220
|
-
rateLimitProof: message.rateLimitProof,
|
221
|
-
};
|
222
|
-
}
|
223
|
-
}
|
224
|
-
class AsymDecoder extends DecoderV0 {
|
225
|
-
constructor(contentTopic, privateKey) {
|
226
|
-
super(contentTopic);
|
227
|
-
this.privateKey = privateKey;
|
228
|
-
}
|
229
|
-
async fromProtoObj(protoMessage) {
|
230
|
-
const cipherPayload = protoMessage.payload;
|
231
|
-
if (protoMessage.version !== Version) {
|
232
|
-
log("Failed to decrypt due to incorrect version, expected:", Version, ", actual:", protoMessage.version);
|
233
|
-
return;
|
234
|
-
}
|
235
|
-
let payload;
|
236
|
-
if (!cipherPayload) {
|
237
|
-
log(`No payload to decrypt for contentTopic ${this.contentTopic}`);
|
238
|
-
return;
|
239
|
-
}
|
240
|
-
try {
|
241
|
-
payload = await decryptAsymmetric(cipherPayload, this.privateKey);
|
242
|
-
}
|
243
|
-
catch (e) {
|
244
|
-
log(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`, e);
|
245
|
-
return;
|
246
|
-
}
|
247
|
-
if (!payload) {
|
248
|
-
log(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);
|
249
|
-
return;
|
250
|
-
}
|
251
|
-
const res = await postCipher(payload);
|
252
|
-
if (!res) {
|
253
|
-
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);
|
254
|
-
return;
|
255
|
-
}
|
256
|
-
log("Message decrypted", protoMessage);
|
257
|
-
return new MessageV1(protoMessage, res.payload, res.sig?.signature, res.sig?.publicKey);
|
258
|
-
}
|
259
|
-
}
|
260
|
-
class SymDecoder extends DecoderV0 {
|
261
|
-
constructor(contentTopic, symKey) {
|
262
|
-
super(contentTopic);
|
263
|
-
this.symKey = symKey;
|
264
|
-
}
|
265
|
-
async fromProtoObj(protoMessage) {
|
266
|
-
const cipherPayload = protoMessage.payload;
|
267
|
-
if (protoMessage.version !== Version) {
|
268
|
-
log("Failed to decrypt due to incorrect version, expected:", Version, ", actual:", protoMessage.version);
|
269
|
-
return;
|
270
|
-
}
|
271
|
-
let payload;
|
272
|
-
if (!cipherPayload) {
|
273
|
-
log(`No payload to decrypt for contentTopic ${this.contentTopic}`);
|
274
|
-
return;
|
275
|
-
}
|
276
|
-
try {
|
277
|
-
payload = await decryptSymmetric(cipherPayload, this.symKey);
|
278
|
-
}
|
279
|
-
catch (e) {
|
280
|
-
log(`Failed to decrypt message using asymmetric decryption for contentTopic: ${this.contentTopic}`, e);
|
281
|
-
return;
|
282
|
-
}
|
283
|
-
if (!payload) {
|
284
|
-
log(`Failed to decrypt payload for contentTopic ${this.contentTopic}`);
|
285
|
-
return;
|
286
|
-
}
|
287
|
-
const res = await postCipher(payload);
|
288
|
-
if (!res) {
|
289
|
-
log(`Failed to decode payload for contentTopic ${this.contentTopic}`);
|
290
|
-
return;
|
291
|
-
}
|
292
|
-
log("Message decrypted", protoMessage);
|
293
|
-
return new MessageV1(protoMessage, res.payload, res.sig?.signature, res.sig?.publicKey);
|
294
|
-
}
|
295
|
-
}
|
296
|
-
function getSizeOfPayloadSizeField(message) {
|
297
|
-
const messageDataView = new DataView(message.buffer);
|
298
|
-
return messageDataView.getUint8(0) & FlagMask;
|
299
|
-
}
|
300
|
-
function getPayloadSize(message, sizeOfPayloadSizeField) {
|
301
|
-
let payloadSizeBytes = message.slice(1, 1 + sizeOfPayloadSizeField);
|
302
|
-
// int 32 == 4 bytes
|
303
|
-
if (sizeOfPayloadSizeField < 4) {
|
304
|
-
// If less than 4 bytes pad right (Little Endian).
|
305
|
-
payloadSizeBytes = concat([payloadSizeBytes, new Uint8Array(4 - sizeOfPayloadSizeField)], 4);
|
306
|
-
}
|
307
|
-
const payloadSizeDataView = new DataView(payloadSizeBytes.buffer);
|
308
|
-
return payloadSizeDataView.getInt32(0, true);
|
309
|
-
}
|
310
|
-
function isMessageSigned(message) {
|
311
|
-
const messageDataView = new DataView(message.buffer);
|
312
|
-
return (messageDataView.getUint8(0) & IsSignedMask) == IsSignedMask;
|
313
|
-
}
|
314
|
-
/**
|
315
|
-
* Proceed with Asymmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
316
|
-
* The data MUST be flags | payload-length | payload | [signature].
|
317
|
-
* The returned result can be set to `WakuMessage.payload`.
|
318
|
-
*
|
319
|
-
* @internal
|
320
|
-
*/
|
321
|
-
async function encryptAsymmetric(data, publicKey) {
|
322
|
-
return encrypt$1(hexToBytes(publicKey), data);
|
323
|
-
}
|
324
|
-
/**
|
325
|
-
* Proceed with Asymmetric decryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
326
|
-
* The returned data is expected to be `flags | payload-length | payload | [signature]`.
|
327
|
-
*
|
328
|
-
* @internal
|
329
|
-
*/
|
330
|
-
async function decryptAsymmetric(payload, privKey) {
|
331
|
-
return decrypt$1(privKey, payload);
|
332
|
-
}
|
333
|
-
/**
|
334
|
-
* Proceed with Symmetric encryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
335
|
-
*
|
336
|
-
* @param data The data to encrypt, expected to be `flags | payload-length | payload | [signature]`.
|
337
|
-
* @param key The key to use for encryption.
|
338
|
-
* @returns The decrypted data, `cipherText | tag | iv` and can be set to `WakuMessage.payload`.
|
339
|
-
*
|
340
|
-
* @internal
|
341
|
-
*/
|
342
|
-
async function encryptSymmetric(data, key) {
|
343
|
-
const iv = generateIv();
|
344
|
-
// Returns `cipher | tag`
|
345
|
-
const cipher = await encrypt(iv, hexToBytes(key), data);
|
346
|
-
return concat([cipher, iv]);
|
347
|
-
}
|
348
|
-
/**
|
349
|
-
* Proceed with Symmetric decryption of the data as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
350
|
-
*
|
351
|
-
* @param payload The cipher data, it is expected to be `cipherText | tag | iv`.
|
352
|
-
* @param key The key to use for decryption.
|
353
|
-
* @returns The decrypted data, expected to be `flags | payload-length | payload | [signature]`.
|
354
|
-
*
|
355
|
-
* @internal
|
356
|
-
*/
|
357
|
-
async function decryptSymmetric(payload, key) {
|
358
|
-
const ivStart = payload.length - Symmetric.ivSize;
|
359
|
-
const cipher = payload.slice(0, ivStart);
|
360
|
-
const iv = payload.slice(ivStart);
|
361
|
-
return decrypt(iv, hexToBytes(key), cipher);
|
362
|
-
}
|
363
|
-
/**
|
364
|
-
* Computes the flags & auxiliary-field as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
365
|
-
*/
|
366
|
-
function addPayloadSizeField(msg, payload) {
|
367
|
-
const fieldSize = computeSizeOfPayloadSizeField(payload);
|
368
|
-
let field = new Uint8Array(4);
|
369
|
-
const fieldDataView = new DataView(field.buffer);
|
370
|
-
fieldDataView.setUint32(0, payload.length, true);
|
371
|
-
field = field.slice(0, fieldSize);
|
372
|
-
msg = concat([msg, field]);
|
373
|
-
msg[0] |= fieldSize;
|
374
|
-
return msg;
|
375
|
-
}
|
376
|
-
/**
|
377
|
-
* Returns the size of the auxiliary-field which in turns contains the payload size
|
378
|
-
*/
|
379
|
-
function computeSizeOfPayloadSizeField(payload) {
|
380
|
-
let s = 1;
|
381
|
-
for (let i = payload.length; i >= 256; i /= 256) {
|
382
|
-
s++;
|
383
|
-
}
|
384
|
-
return s;
|
385
|
-
}
|
386
|
-
function validateDataIntegrity(value, expectedSize) {
|
387
|
-
if (value.length !== expectedSize) {
|
388
|
-
return false;
|
389
|
-
}
|
390
|
-
return expectedSize <= 3 || value.findIndex((i) => i !== 0) !== -1;
|
391
|
-
}
|
392
|
-
function getSignature(message) {
|
393
|
-
return message.slice(message.length - SignatureLength, message.length);
|
394
|
-
}
|
395
|
-
function getHash(message, isSigned) {
|
396
|
-
if (isSigned) {
|
397
|
-
return keccak256(message.slice(0, message.length - SignatureLength));
|
398
|
-
}
|
399
|
-
return keccak256(message);
|
400
|
-
}
|
401
|
-
function ecRecoverPubKey(messageHash, signature) {
|
402
|
-
const recoveryDataView = new DataView(signature.slice(64).buffer);
|
403
|
-
const recovery = recoveryDataView.getUint8(0);
|
404
|
-
const _signature = Signature.fromCompact(signature.slice(0, 64));
|
405
|
-
return recoverPublicKey(messageHash, _signature, recovery, false);
|
406
|
-
}
|
407
|
-
/**
|
408
|
-
* Prepare the payload pre-encryption.
|
409
|
-
*
|
410
|
-
* @internal
|
411
|
-
* @returns The encoded payload, ready for encryption using {@link encryptAsymmetric}
|
412
|
-
* or {@link encryptSymmetric}.
|
413
|
-
*/
|
414
|
-
async function preCipher(messagePayload, sigPrivKey) {
|
415
|
-
let envelope = new Uint8Array([0]); // No flags
|
416
|
-
envelope = addPayloadSizeField(envelope, messagePayload);
|
417
|
-
envelope = concat([envelope, messagePayload]);
|
418
|
-
// Calculate padding:
|
419
|
-
let rawSize = FlagsLength +
|
420
|
-
computeSizeOfPayloadSizeField(messagePayload) +
|
421
|
-
messagePayload.length;
|
422
|
-
if (sigPrivKey) {
|
423
|
-
rawSize += SignatureLength;
|
424
|
-
}
|
425
|
-
const remainder = rawSize % PaddingTarget;
|
426
|
-
const paddingSize = PaddingTarget - remainder;
|
427
|
-
const pad = randomBytes(paddingSize);
|
428
|
-
if (!validateDataIntegrity(pad, paddingSize)) {
|
429
|
-
throw new Error("failed to generate random padding of size " + paddingSize);
|
430
|
-
}
|
431
|
-
envelope = concat([envelope, pad]);
|
432
|
-
if (sigPrivKey) {
|
433
|
-
envelope[0] |= IsSignedMask;
|
434
|
-
const hash = keccak256(envelope);
|
435
|
-
const bytesSignature = await sign(hash, sigPrivKey);
|
436
|
-
envelope = concat([envelope, bytesSignature]);
|
437
|
-
}
|
438
|
-
return envelope;
|
439
|
-
}
|
440
|
-
/**
|
441
|
-
* Decode a decrypted payload.
|
442
|
-
*
|
443
|
-
* @internal
|
444
|
-
*/
|
445
|
-
function postCipher(message) {
|
446
|
-
const sizeOfPayloadSizeField = getSizeOfPayloadSizeField(message);
|
447
|
-
if (sizeOfPayloadSizeField === 0)
|
448
|
-
return;
|
449
|
-
const payloadSize = getPayloadSize(message, sizeOfPayloadSizeField);
|
450
|
-
const payloadStart = 1 + sizeOfPayloadSizeField;
|
451
|
-
const payload = message.slice(payloadStart, payloadStart + payloadSize);
|
452
|
-
const isSigned = isMessageSigned(message);
|
453
|
-
let sig;
|
454
|
-
if (isSigned) {
|
455
|
-
const signature = getSignature(message);
|
456
|
-
const hash = getHash(message, isSigned);
|
457
|
-
const publicKey = ecRecoverPubKey(hash, signature);
|
458
|
-
sig = { signature, publicKey };
|
459
|
-
}
|
460
|
-
return { payload, sig };
|
461
|
-
}
|
462
|
-
|
463
|
-
export { AsymDecoder, AsymEncoder, MessageV1, SymDecoder, SymEncoder, Version, decryptAsymmetric, decryptSymmetric, encryptAsymmetric, encryptSymmetric, postCipher, preCipher };
|
@@ -1,19 +0,0 @@
|
|
1
|
-
import { p as peerIdFromString } from './events-158407bb.js';
|
2
|
-
|
3
|
-
const symbol = Symbol.for('@libp2p/peer-discovery');
|
4
|
-
|
5
|
-
function multiaddrsToPeerInfo(mas) {
|
6
|
-
return mas
|
7
|
-
.map((ma) => {
|
8
|
-
const peerIdStr = ma.getPeerId();
|
9
|
-
const protocols = [];
|
10
|
-
return {
|
11
|
-
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
|
12
|
-
multiaddrs: [ma.decapsulateCode(421)],
|
13
|
-
protocols,
|
14
|
-
};
|
15
|
-
})
|
16
|
-
.filter((peerInfo) => peerInfo.id !== null);
|
17
|
-
}
|
18
|
-
|
19
|
-
export { multiaddrsToPeerInfo as m, symbol as s };
|
@@ -1,19 +0,0 @@
|
|
1
|
-
import { p as peerIdFromString } from './index-2ae915be.js';
|
2
|
-
|
3
|
-
const symbol = Symbol.for('@libp2p/peer-discovery');
|
4
|
-
|
5
|
-
function multiaddrsToPeerInfo(mas) {
|
6
|
-
return mas
|
7
|
-
.map((ma) => {
|
8
|
-
const peerIdStr = ma.getPeerId();
|
9
|
-
const protocols = [];
|
10
|
-
return {
|
11
|
-
id: peerIdStr ? peerIdFromString(peerIdStr) : null,
|
12
|
-
multiaddrs: [ma.decapsulateCode(421)],
|
13
|
-
protocols,
|
14
|
-
};
|
15
|
-
})
|
16
|
-
.filter((peerInfo) => peerInfo.id !== null);
|
17
|
-
}
|
18
|
-
|
19
|
-
export { multiaddrsToPeerInfo as m, symbol as s };
|