fca-nx 6.8.8 → 7.0.5
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/README.md +24 -10
- package/func/checkUpdate.js +1 -1
- package/func/logAdapter.js +2 -0
- package/index.js +3 -0
- package/module/config.js +5 -2
- package/module/login.js +38 -13
- package/module/loginHelper.js +164 -127
- package/module/options.js +7 -1
- package/package.json +2 -2
- package/src/api/action/addfriend.js +9 -3
- package/src/api/action/changeAvatar.js +1 -0
- package/src/api/action/changeCover.js +3 -1
- package/src/api/action/enableAutoSaveAppState.js +18 -5
- package/src/api/action/refreshFb_dtsg.js +18 -76
- package/src/api/http/httpGet.js +6 -2
- package/src/api/http/httpPost.js +7 -2
- package/src/api/messaging/createNewGroup.js +1 -1
- package/src/api/messaging/deleteMessage.js +1 -1
- package/src/api/messaging/forwardMessage.js +9 -1
- package/src/api/messaging/getEmojiUrl.js +5 -1
- package/src/api/messaging/getMessage.js +4 -2
- package/src/api/messaging/getThemePictures.js +21 -28
- package/src/api/messaging/markAsRead.js +1 -0
- package/src/api/messaging/muteThread.js +1 -1
- package/src/api/messaging/pinMessage.js +10 -1
- package/src/api/messaging/scheduler.js +71 -4
- package/src/api/messaging/sendBroadcast.js +15 -0
- package/src/api/messaging/sendMessage.js +23 -1
- package/src/api/messaging/sendTypingIndicator.js +0 -15
- package/src/api/messaging/sessionGuard.js +45 -9
- package/src/api/messaging/setMessageReaction.js +2 -1
- package/src/api/messaging/threadColors.js +45 -12
- package/src/api/messaging/uploadAttachment.js +37 -3
- package/src/api/socket/OldMessage.js +7 -2
- package/src/api/socket/core/connectMqtt.js +68 -81
- package/src/api/socket/core/emitAuth.js +11 -2
- package/src/api/socket/core/getSeqID.js +28 -36
- package/src/api/socket/core/getTaskResponseData.js +3 -1
- package/src/api/socket/core/parseDelta.js +1 -1
- package/src/api/socket/detail/buildStream.js +4 -1
- package/src/api/socket/detail/constants.js +4 -1
- package/src/api/socket/e2ee/crypto.js +40 -2
- package/src/api/socket/e2ee/index.js +124 -73
- package/src/api/socket/e2ee/protocol.js +40 -32
- package/src/api/socket/e2ee/ratchet.js +43 -37
- package/src/api/socket/e2ee/store.js +47 -133
- package/src/api/socket/e2ee/vendor/fb-e2ee.cjs +18 -140
- package/src/api/socket/listenE2EE.js +23 -12
- package/src/api/socket/listenMqtt.js +94 -110
- package/src/api/socket/middleware/index.js +61 -7
- package/src/api/socket/sendMessage.js +78 -170
- package/src/api/socket/sendMessageMqtt.js +6 -0
- package/src/api/threads/getThreadHistory.js +68 -38
- package/src/api/threads/getThreadInfo.js +4 -3
- package/src/api/threads/getThreadList.js +34 -31
- package/src/api/threads/getThreadPictures.js +1 -1
- package/src/api/users/getUserInfo.js +5 -2
- package/src/api/users/getUserInfoV2.js +65 -59
- package/src/app/createFcaClient.js +2 -2
- package/src/core/sendReqMqtt.js +2 -1
- package/src/database/helpers.js +12 -0
- package/src/database/models/index.js +8 -9
- package/src/utils/broadcast.js +1 -1
- package/src/utils/clientIdentity.js +50 -3
- package/src/utils/cookies.js +4 -2
- package/src/utils/format/utils.js +10 -0
- package/src/utils/headers.js +26 -12
- package/src/utils/loginParser/autoLogin.js +12 -0
- package/src/utils/loginParser/parseAndCheckLogin.js +7 -0
- package/src/utils/loginParser/textUtils.js +6 -0
- package/src/utils/request/config.js +4 -1
- package/src/utils/request/methods.js +0 -13
- package/src/utils/request/proxy.js +13 -0
- package/src/utils/request/retry.js +0 -10
- package/src/utils/request/sanitize.js +1 -1
- package/src/utils/textEffects.js +85 -0
- package/src/utils/utils.js +17 -3
- package/src/utils/instanceLock.js +0 -35
- package/src/utils/ipGuard.js +0 -58
- package/src/utils/sendThrottle.js +0 -51
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Double Ratchet Algorithm (Signal Protocol)
|
|
5
|
+
* Based on the Signal Protocol specification:
|
|
6
|
+
* https://signal.org/docs/specifications/doubleratchet/
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
const { generateKeyPair, dh, hkdf, hkdfExtract, hkdfExpand, encrypt, decrypt, hmacSha256 } = require("./crypto");
|
|
4
10
|
|
|
5
11
|
const MAX_SKIP = 1000;
|
|
@@ -29,17 +35,20 @@ function deriveMessageKeys(mk) {
|
|
|
29
35
|
|
|
30
36
|
class DoubleRatchetSession {
|
|
31
37
|
constructor() {
|
|
32
|
-
this.DHs = null;
|
|
33
|
-
this.DHr = null;
|
|
34
|
-
this.RK = null;
|
|
35
|
-
this.CKs = null;
|
|
36
|
-
this.CKr = null;
|
|
37
|
-
this.Ns = 0;
|
|
38
|
-
this.Nr = 0;
|
|
39
|
-
this.PN = 0;
|
|
40
|
-
this.MKSKIPPED = new Map();
|
|
38
|
+
this.DHs = null; // Our DH ratchet key pair
|
|
39
|
+
this.DHr = null; // Their DH ratchet public key (raw 32 bytes)
|
|
40
|
+
this.RK = null; // Root key (32 bytes)
|
|
41
|
+
this.CKs = null; // Sending chain key
|
|
42
|
+
this.CKr = null; // Receiving chain key
|
|
43
|
+
this.Ns = 0; // Send message number
|
|
44
|
+
this.Nr = 0; // Receive message number
|
|
45
|
+
this.PN = 0; // Previous sending chain length
|
|
46
|
+
this.MKSKIPPED = new Map(); // Skipped message keys
|
|
41
47
|
}
|
|
42
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Initialize as sender (Alice) after X3DH
|
|
51
|
+
*/
|
|
43
52
|
initAsSender(sk, recipientRatchetPublicKey) {
|
|
44
53
|
this.DHs = generateKeyPair();
|
|
45
54
|
this.DHr = Buffer.from(recipientRatchetPublicKey);
|
|
@@ -51,6 +60,9 @@ class DoubleRatchetSession {
|
|
|
51
60
|
this.Ns = 0; this.Nr = 0; this.PN = 0;
|
|
52
61
|
}
|
|
53
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Initialize as receiver (Bob) after X3DH
|
|
65
|
+
*/
|
|
54
66
|
initAsReceiver(sk, ourRatchetKeyPair) {
|
|
55
67
|
this.DHs = ourRatchetKeyPair;
|
|
56
68
|
this.DHr = null;
|
|
@@ -60,6 +72,10 @@ class DoubleRatchetSession {
|
|
|
60
72
|
this.Ns = 0; this.Nr = 0; this.PN = 0;
|
|
61
73
|
}
|
|
62
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Encrypt a message
|
|
77
|
+
* Returns { header, ciphertext }
|
|
78
|
+
*/
|
|
63
79
|
encrypt(plaintext, aad) {
|
|
64
80
|
const { ck, mk } = kdfCK(this.CKs);
|
|
65
81
|
this.CKs = ck;
|
|
@@ -71,32 +87,18 @@ class DoubleRatchetSession {
|
|
|
71
87
|
return { header, ciphertext };
|
|
72
88
|
}
|
|
73
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Decrypt a message
|
|
92
|
+
* @param {Buffer} header - message header
|
|
93
|
+
* @param {Buffer} ciphertext - encrypted payload
|
|
94
|
+
* @param {Buffer} [aad] - additional auth data
|
|
95
|
+
* @returns {Buffer} decrypted plaintext
|
|
96
|
+
*/
|
|
74
97
|
decrypt(header, ciphertext, aad) {
|
|
75
|
-
const snapshot = {
|
|
76
|
-
DHs: this.DHs,
|
|
77
|
-
DHr: this.DHr,
|
|
78
|
-
RK: this.RK,
|
|
79
|
-
CKs: this.CKs,
|
|
80
|
-
CKr: this.CKr,
|
|
81
|
-
Ns: this.Ns,
|
|
82
|
-
Nr: this.Nr,
|
|
83
|
-
PN: this.PN,
|
|
84
|
-
MKSKIPPED: new Map(this.MKSKIPPED)
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
try {
|
|
88
|
-
return this._decryptAndAdvance(header, ciphertext, aad);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
|
|
91
|
-
Object.assign(this, snapshot);
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
_decryptAndAdvance(header, ciphertext, aad) {
|
|
97
98
|
const { dh_pub, pn, n } = this._parseHeader(header);
|
|
98
99
|
const aadFull = aad ? Buffer.concat([aad, header]) : header;
|
|
99
100
|
|
|
101
|
+
// Check skipped keys first
|
|
100
102
|
const skipKey = `${dh_pub.toString("hex")}:${n}`;
|
|
101
103
|
if (this.MKSKIPPED.has(skipKey)) {
|
|
102
104
|
const mk = this.MKSKIPPED.get(skipKey);
|
|
@@ -105,6 +107,7 @@ class DoubleRatchetSession {
|
|
|
105
107
|
return decrypt(keys.encKey, ciphertext, aadFull);
|
|
106
108
|
}
|
|
107
109
|
|
|
110
|
+
// Check if we need to ratchet
|
|
108
111
|
const needsRatchet = !this.DHr || !dh_pub.equals(this.DHr);
|
|
109
112
|
if (needsRatchet) {
|
|
110
113
|
this._skipMessageKeys(pn);
|
|
@@ -124,9 +127,6 @@ class DoubleRatchetSession {
|
|
|
124
127
|
if (this.Nr + MAX_SKIP < until) throw new Error("Too many skipped messages: " + until);
|
|
125
128
|
if (this.CKr) {
|
|
126
129
|
while (this.Nr < until) {
|
|
127
|
-
if (this.MKSKIPPED.size >= MAX_SKIP) {
|
|
128
|
-
throw new Error("Skipped message key limit exceeded");
|
|
129
|
-
}
|
|
130
130
|
const { ck, mk } = kdfCK(this.CKr);
|
|
131
131
|
this.CKr = ck;
|
|
132
132
|
const key = `${this.DHr.toString("hex")}:${this.Nr}`;
|
|
@@ -142,11 +142,13 @@ class DoubleRatchetSession {
|
|
|
142
142
|
this.Nr = 0;
|
|
143
143
|
this.DHr = remoteDhPub;
|
|
144
144
|
|
|
145
|
+
// Receive ratchet
|
|
145
146
|
const dhOut1 = dh(this.DHs.privateKey, this.DHr);
|
|
146
147
|
const { rk: rk1, ck: ckr } = kdfRK(this.RK, dhOut1);
|
|
147
148
|
this.RK = rk1;
|
|
148
149
|
this.CKr = ckr;
|
|
149
150
|
|
|
151
|
+
// Send ratchet
|
|
150
152
|
this.DHs = generateKeyPair();
|
|
151
153
|
const dhOut2 = dh(this.DHs.privateKey, this.DHr);
|
|
152
154
|
const { rk: rk2, ck: cks } = kdfRK(this.RK, dhOut2);
|
|
@@ -163,9 +165,7 @@ class DoubleRatchetSession {
|
|
|
163
165
|
}
|
|
164
166
|
|
|
165
167
|
_parseHeader(header) {
|
|
166
|
-
if (
|
|
167
|
-
throw new Error("Invalid ratchet header length");
|
|
168
|
-
}
|
|
168
|
+
if (header.length < 40) throw new Error("Invalid ratchet header length");
|
|
169
169
|
return {
|
|
170
170
|
dh_pub: header.slice(0, 32),
|
|
171
171
|
pn: header.readUInt32BE(32),
|
|
@@ -173,6 +173,9 @@ class DoubleRatchetSession {
|
|
|
173
173
|
};
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Serialize session state for persistence
|
|
178
|
+
*/
|
|
176
179
|
serialize() {
|
|
177
180
|
return JSON.stringify({
|
|
178
181
|
DHs_priv: this.DHs ? this.DHs.privateKey.toString("base64") : null,
|
|
@@ -188,6 +191,9 @@ class DoubleRatchetSession {
|
|
|
188
191
|
});
|
|
189
192
|
}
|
|
190
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Restore session state from serialized string
|
|
196
|
+
*/
|
|
191
197
|
static deserialize(json) {
|
|
192
198
|
const d = JSON.parse(json);
|
|
193
199
|
const session = new DoubleRatchetSession();
|
|
@@ -1,30 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* E2EE Device/Session Store
|
|
5
|
+
* Persists key material to a JSON file (encrypted with a device secret).
|
|
6
|
+
* Each E2EE session (thread) is stored separately.
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
const fs = require("fs");
|
|
4
10
|
const path = require("path");
|
|
5
11
|
const crypto = require("crypto");
|
|
6
12
|
|
|
7
13
|
class E2EEStore {
|
|
8
|
-
constructor(storePath, userID
|
|
9
|
-
if (typeof storePath !== "string" || !storePath.trim()) {
|
|
10
|
-
throw new TypeError("E2EE store path must be a non-empty string.");
|
|
11
|
-
}
|
|
12
|
-
if ((typeof userID !== "string" && typeof userID !== "number") ||
|
|
13
|
-
!/^[A-Za-z0-9._-]{1,128}$/.test(String(userID))) {
|
|
14
|
-
throw new TypeError("E2EE user ID contains unsupported path characters.");
|
|
15
|
-
}
|
|
16
|
-
if (!options || typeof options !== "object" || Array.isArray(options)) options = {};
|
|
17
|
-
|
|
14
|
+
constructor(storePath, userID) {
|
|
18
15
|
this.storePath = storePath;
|
|
19
|
-
this.userID =
|
|
20
|
-
this.encryptionKey = Object.prototype.hasOwnProperty.call(options, "encryptionKey")
|
|
21
|
-
? options.encryptionKey
|
|
22
|
-
: (process.env.FCA_NX_E2EE_STORE_KEY || "");
|
|
16
|
+
this.userID = userID;
|
|
23
17
|
this.data = {
|
|
24
18
|
identityKey: null,
|
|
25
19
|
signedPreKey: null,
|
|
26
20
|
oneTimePreKeys: [],
|
|
27
|
-
sessions: {}
|
|
21
|
+
sessions: {}, // threadID -> serialized DoubleRatchetSession
|
|
22
|
+
deviceSecret: null // 32-byte random secret for local encryption
|
|
28
23
|
};
|
|
29
24
|
this._loaded = false;
|
|
30
25
|
}
|
|
@@ -33,96 +28,36 @@ class E2EEStore {
|
|
|
33
28
|
return path.join(this.storePath, `fcanx_e2ee_${this.userID}.json`);
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
throw new Error(
|
|
39
|
-
"E2EE store encryption key is missing or too short. Set FCA_NX_E2EE_STORE_KEY to a secret of at least 32 characters."
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
_deriveStorageKey(salt) {
|
|
45
|
-
this._requireEncryptionKey();
|
|
46
|
-
return crypto.scryptSync(this.encryptionKey, salt, 32, {
|
|
47
|
-
N: 16384,
|
|
48
|
-
r: 8,
|
|
49
|
-
p: 1,
|
|
50
|
-
maxmem: 32 * 1024 * 1024
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
_associatedData() {
|
|
55
|
-
return Buffer.from(`fca-nx-e2ee-store:v2:${this.userID}`, "utf8");
|
|
31
|
+
_salt() {
|
|
32
|
+
return Buffer.from(this.userID.padEnd(16, "0").slice(0, 16), "utf8");
|
|
56
33
|
}
|
|
57
34
|
|
|
58
35
|
_localEncrypt(data) {
|
|
59
|
-
|
|
60
|
-
const key = this.
|
|
36
|
+
if (!this.data.deviceSecret) return data;
|
|
37
|
+
const key = Buffer.from(this.data.deviceSecret, "base64");
|
|
38
|
+
if (key.length !== 32) return data;
|
|
61
39
|
const iv = crypto.randomBytes(12);
|
|
62
40
|
const cipher = crypto.createCipheriv("aes-256-gcm", key, iv);
|
|
63
|
-
cipher.setAAD(this._associatedData());
|
|
64
41
|
const enc = Buffer.concat([cipher.update(Buffer.from(data, "utf8")), cipher.final()]);
|
|
65
42
|
const tag = cipher.getAuthTag();
|
|
66
|
-
return
|
|
67
|
-
version: 2,
|
|
68
|
-
salt: salt.toString("base64"),
|
|
69
|
-
iv: iv.toString("base64"),
|
|
70
|
-
tag: tag.toString("base64"),
|
|
71
|
-
payload: enc.toString("base64")
|
|
72
|
-
};
|
|
43
|
+
return Buffer.concat([iv, tag, enc]).toString("base64");
|
|
73
44
|
}
|
|
74
45
|
|
|
75
|
-
_localDecrypt(
|
|
76
|
-
if (!
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
decipher.setAAD(this._associatedData());
|
|
90
|
-
decipher.setAuthTag(tag);
|
|
91
|
-
return Buffer.concat([decipher.update(encrypted), decipher.final()]).toString("utf8");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
_decryptLegacy(parsed) {
|
|
95
|
-
this._requireEncryptionKey();
|
|
96
|
-
const legacyKey = decodeBase64(parsed.deviceSecret, "legacy device secret");
|
|
97
|
-
const packed = decodeBase64(parsed.payload, "legacy payload");
|
|
98
|
-
if (legacyKey.length !== 32 || packed.length < 28) {
|
|
99
|
-
throw new Error("Invalid legacy E2EE store.");
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const decipher = crypto.createDecipheriv("aes-256-gcm", legacyKey, packed.subarray(0, 12));
|
|
103
|
-
decipher.setAuthTag(packed.subarray(12, 28));
|
|
104
|
-
return Buffer.concat([
|
|
105
|
-
decipher.update(packed.subarray(28)),
|
|
106
|
-
decipher.final()
|
|
107
|
-
]).toString("utf8");
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
_applyPayload(payload) {
|
|
111
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
112
|
-
throw new Error("Invalid E2EE store payload.");
|
|
113
|
-
}
|
|
114
|
-
if (payload.oneTimePreKeys !== undefined && !Array.isArray(payload.oneTimePreKeys)) {
|
|
115
|
-
throw new Error("Invalid E2EE one-time pre-key list.");
|
|
116
|
-
}
|
|
117
|
-
if (payload.sessions !== undefined &&
|
|
118
|
-
(!payload.sessions || typeof payload.sessions !== "object" || Array.isArray(payload.sessions))) {
|
|
119
|
-
throw new Error("Invalid E2EE session map.");
|
|
46
|
+
_localDecrypt(data) {
|
|
47
|
+
if (!this.data.deviceSecret) return data;
|
|
48
|
+
try {
|
|
49
|
+
const key = Buffer.from(this.data.deviceSecret, "base64");
|
|
50
|
+
if (key.length !== 32) return data;
|
|
51
|
+
const buf = Buffer.from(data, "base64");
|
|
52
|
+
const iv = buf.slice(0, 12);
|
|
53
|
+
const tag = buf.slice(12, 28);
|
|
54
|
+
const enc = buf.slice(28);
|
|
55
|
+
const decipher = crypto.createDecipheriv("aes-256-gcm", key, iv);
|
|
56
|
+
decipher.setAuthTag(tag);
|
|
57
|
+
return Buffer.concat([decipher.update(enc), decipher.final()]).toString("utf8");
|
|
58
|
+
} catch (_) {
|
|
59
|
+
return data;
|
|
120
60
|
}
|
|
121
|
-
|
|
122
|
-
this.data.identityKey = payload.identityKey || null;
|
|
123
|
-
this.data.signedPreKey = payload.signedPreKey || null;
|
|
124
|
-
this.data.oneTimePreKeys = payload.oneTimePreKeys || [];
|
|
125
|
-
this.data.sessions = payload.sessions || {};
|
|
126
61
|
}
|
|
127
62
|
|
|
128
63
|
load() {
|
|
@@ -131,48 +66,35 @@ class E2EEStore {
|
|
|
131
66
|
try {
|
|
132
67
|
const raw = fs.readFileSync(file, "utf8");
|
|
133
68
|
const parsed = JSON.parse(raw);
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
this.
|
|
69
|
+
if (parsed.deviceSecret) this.data.deviceSecret = parsed.deviceSecret;
|
|
70
|
+
const decrypted = this._localDecrypt(parsed.payload || "{}");
|
|
71
|
+
const payload = JSON.parse(decrypted);
|
|
72
|
+
this.data.identityKey = payload.identityKey || null;
|
|
73
|
+
this.data.signedPreKey = payload.signedPreKey || null;
|
|
74
|
+
this.data.oneTimePreKeys = payload.oneTimePreKeys || [];
|
|
75
|
+
this.data.sessions = payload.sessions || {};
|
|
140
76
|
this._loaded = true;
|
|
141
|
-
|
|
142
|
-
if (isLegacy) this.save();
|
|
143
77
|
return true;
|
|
144
|
-
} catch (
|
|
145
|
-
|
|
78
|
+
} catch (_) {
|
|
79
|
+
return false;
|
|
146
80
|
}
|
|
147
81
|
}
|
|
148
82
|
|
|
149
83
|
save() {
|
|
150
|
-
this.
|
|
84
|
+
if (!fs.existsSync(this.storePath)) {
|
|
85
|
+
fs.mkdirSync(this.storePath, { recursive: true });
|
|
86
|
+
}
|
|
87
|
+
if (!this.data.deviceSecret) {
|
|
88
|
+
this.data.deviceSecret = crypto.randomBytes(32).toString("base64");
|
|
89
|
+
}
|
|
151
90
|
const payload = JSON.stringify({
|
|
152
91
|
identityKey: this.data.identityKey,
|
|
153
92
|
signedPreKey: this.data.signedPreKey,
|
|
154
93
|
oneTimePreKeys: this.data.oneTimePreKeys,
|
|
155
94
|
sessions: this.data.sessions
|
|
156
95
|
});
|
|
157
|
-
const encrypted =
|
|
158
|
-
fs.
|
|
159
|
-
|
|
160
|
-
const file = this._storeFile();
|
|
161
|
-
const tempFile = `${file}.${process.pid}.${crypto.randomBytes(8).toString("hex")}.tmp`;
|
|
162
|
-
let fd;
|
|
163
|
-
try {
|
|
164
|
-
fd = fs.openSync(tempFile, "wx", 0o600);
|
|
165
|
-
fs.writeFileSync(fd, encrypted, "utf8");
|
|
166
|
-
fs.fsyncSync(fd);
|
|
167
|
-
fs.closeSync(fd);
|
|
168
|
-
fd = undefined;
|
|
169
|
-
fs.renameSync(tempFile, file);
|
|
170
|
-
} finally {
|
|
171
|
-
if (fd !== undefined) fs.closeSync(fd);
|
|
172
|
-
try {
|
|
173
|
-
if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile);
|
|
174
|
-
} catch (_) {}
|
|
175
|
-
}
|
|
96
|
+
const encrypted = this._localEncrypt(payload);
|
|
97
|
+
fs.writeFileSync(this._storeFile(), JSON.stringify({ deviceSecret: this.data.deviceSecret, payload: encrypted }, null, 2), "utf8");
|
|
176
98
|
}
|
|
177
99
|
|
|
178
100
|
hasIdentityKey() { return !!this.data.identityKey; }
|
|
@@ -257,12 +179,4 @@ class E2EEStore {
|
|
|
257
179
|
deleteSession(threadID) { delete this.data.sessions[threadID]; }
|
|
258
180
|
}
|
|
259
181
|
|
|
260
|
-
function decodeBase64(value, label) {
|
|
261
|
-
if (typeof value !== "string" ||
|
|
262
|
-
!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)) {
|
|
263
|
-
throw new Error(`Invalid E2EE store ${label}.`);
|
|
264
|
-
}
|
|
265
|
-
return Buffer.from(value, "base64");
|
|
266
|
-
}
|
|
267
|
-
|
|
268
182
|
module.exports = { E2EEStore };
|