@periskope/baileys 6.7.18-17-4 → 6.7.18-17-7
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/lib/Signal/Group/group_cipher.d.ts +1 -0
- package/lib/Signal/Group/group_cipher.js +39 -29
- package/lib/Signal/Group/queue-job.d.ts +0 -1
- package/lib/Signal/Group/queue-job.js +5 -2
- package/lib/Signal/libsignal.js +29 -50
- package/lib/Signal/lid-mapping.d.ts +3 -1
- package/lib/Signal/lid-mapping.js +29 -3
- package/lib/Socket/messages-recv.js +2 -2
- package/lib/Types/Auth.d.ts +2 -0
- package/lib/Utils/auth-utils.d.ts +1 -1
- package/lib/Utils/auth-utils.js +41 -348
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ export declare class GroupCipher {
|
|
|
8
8
|
private readonly senderKeyStore;
|
|
9
9
|
private readonly senderKeyName;
|
|
10
10
|
constructor(senderKeyStore: SenderKeyStore, senderKeyName: SenderKeyName);
|
|
11
|
+
private queueJob;
|
|
11
12
|
encrypt(paddedPlaintext: Uint8Array | string): Promise<Uint8Array>;
|
|
12
13
|
decrypt(senderKeyMessageBytes: Uint8Array): Promise<Uint8Array>;
|
|
13
14
|
private getSenderKey;
|
|
@@ -1,45 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.GroupCipher = void 0;
|
|
4
|
-
/* @ts-ignore */
|
|
5
7
|
const crypto_1 = require("libsignal/src/crypto");
|
|
8
|
+
const queue_job_1 = __importDefault(require("./queue-job"));
|
|
6
9
|
const sender_key_message_1 = require("./sender-key-message");
|
|
7
10
|
class GroupCipher {
|
|
8
11
|
constructor(senderKeyStore, senderKeyName) {
|
|
9
12
|
this.senderKeyStore = senderKeyStore;
|
|
10
13
|
this.senderKeyName = senderKeyName;
|
|
11
14
|
}
|
|
15
|
+
queueJob(awaitable) {
|
|
16
|
+
return (0, queue_job_1.default)(this.senderKeyName.toString(), awaitable);
|
|
17
|
+
}
|
|
12
18
|
async encrypt(paddedPlaintext) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
return await this.queueJob(async () => {
|
|
20
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
21
|
+
if (!record) {
|
|
22
|
+
throw new Error('No SenderKeyRecord found for encryption');
|
|
23
|
+
}
|
|
24
|
+
const senderKeyState = record.getSenderKeyState();
|
|
25
|
+
if (!senderKeyState) {
|
|
26
|
+
throw new Error('No session to encrypt message');
|
|
27
|
+
}
|
|
28
|
+
const iteration = senderKeyState.getSenderChainKey().getIteration();
|
|
29
|
+
const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
|
|
30
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
|
|
31
|
+
const senderKeyMessage = new sender_key_message_1.SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
|
|
32
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
33
|
+
return senderKeyMessage.serialize();
|
|
34
|
+
});
|
|
27
35
|
}
|
|
28
36
|
async decrypt(senderKeyMessageBytes) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
return await this.queueJob(async () => {
|
|
38
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
39
|
+
if (!record) {
|
|
40
|
+
throw new Error('No SenderKeyRecord found for decryption');
|
|
41
|
+
}
|
|
42
|
+
const senderKeyMessage = new sender_key_message_1.SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
|
|
43
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
|
|
44
|
+
if (!senderKeyState) {
|
|
45
|
+
throw new Error('No session found to decrypt message');
|
|
46
|
+
}
|
|
47
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
|
|
48
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
|
|
49
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
|
50
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
51
|
+
return plaintext;
|
|
52
|
+
});
|
|
43
53
|
}
|
|
44
54
|
getSenderKey(senderKeyState, iteration) {
|
|
45
55
|
let senderChainKey = senderKeyState.getSenderChainKey();
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = queueJob;
|
|
1
4
|
const _queueAsyncBuckets = new Map();
|
|
2
5
|
const _gcLimit = 10000;
|
|
3
6
|
async function _asyncQueueExecutor(queue, cleanup) {
|
|
4
7
|
let offt = 0;
|
|
8
|
+
// eslint-disable-next-line no-constant-condition
|
|
5
9
|
while (true) {
|
|
6
10
|
const limit = Math.min(queue.length, _gcLimit);
|
|
7
11
|
for (let i = offt; i < limit; i++) {
|
|
@@ -28,7 +32,7 @@ async function _asyncQueueExecutor(queue, cleanup) {
|
|
|
28
32
|
}
|
|
29
33
|
cleanup();
|
|
30
34
|
}
|
|
31
|
-
|
|
35
|
+
function queueJob(bucket, awaitable) {
|
|
32
36
|
// Skip name assignment since it's readonly in strict mode
|
|
33
37
|
if (typeof bucket !== 'string') {
|
|
34
38
|
console.warn('Unhandled bucket type (for naming):', typeof bucket, bucket);
|
|
@@ -51,4 +55,3 @@ export default function queueJob(bucket, awaitable) {
|
|
|
51
55
|
}
|
|
52
56
|
return job;
|
|
53
57
|
}
|
|
54
|
-
//# sourceMappingURL=queue-job.js.map
|
package/lib/Signal/libsignal.js
CHANGED
|
@@ -51,15 +51,11 @@ function makeLibSignalRepository(auth) {
|
|
|
51
51
|
max: 500,
|
|
52
52
|
ttl: 5 * 60 * 1000
|
|
53
53
|
});
|
|
54
|
-
const parsedKeys = auth.keys;
|
|
55
54
|
const repository = {
|
|
56
55
|
decryptGroupMessage({ group, authorJid, msg }) {
|
|
57
56
|
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
58
57
|
const cipher = new Group_1.GroupCipher(storage, senderName);
|
|
59
|
-
|
|
60
|
-
return parsedKeys.transaction(async () => {
|
|
61
|
-
return cipher.decrypt(msg);
|
|
62
|
-
});
|
|
58
|
+
return cipher.decrypt(msg);
|
|
63
59
|
},
|
|
64
60
|
async processSenderKeyDistributionMessage({ item, authorJid }) {
|
|
65
61
|
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
@@ -73,32 +69,23 @@ function makeLibSignalRepository(auth) {
|
|
|
73
69
|
if (!senderKey) {
|
|
74
70
|
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
75
71
|
}
|
|
76
|
-
|
|
77
|
-
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
78
|
-
if (!senderKey) {
|
|
79
|
-
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
80
|
-
}
|
|
81
|
-
await builder.process(senderName, senderMsg);
|
|
82
|
-
});
|
|
72
|
+
await builder.process(senderName, senderMsg);
|
|
83
73
|
},
|
|
84
74
|
async decryptMessage({ jid, type, ciphertext }) {
|
|
85
75
|
const addr = jidToSignalProtocolAddress(jid);
|
|
86
76
|
const session = new libsignal.SessionCipher(storage, addr);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
return result;
|
|
101
|
-
});
|
|
77
|
+
let result;
|
|
78
|
+
switch (type) {
|
|
79
|
+
case 'pkmsg':
|
|
80
|
+
result = await session.decryptPreKeyWhisperMessage(ciphertext);
|
|
81
|
+
break;
|
|
82
|
+
case 'msg':
|
|
83
|
+
result = await session.decryptWhisperMessage(ciphertext);
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
throw new Error(`Unknown message type: ${type}`);
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
102
89
|
},
|
|
103
90
|
async encryptMessage({ jid, data }) {
|
|
104
91
|
// LID SINGLE SOURCE OF TRUTH: Always prefer LID when available
|
|
@@ -127,36 +114,29 @@ function makeLibSignalRepository(auth) {
|
|
|
127
114
|
}
|
|
128
115
|
const addr = jidToSignalProtocolAddress(encryptionJid);
|
|
129
116
|
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
134
|
-
return { type, ciphertext: Buffer.from(body, 'binary') };
|
|
135
|
-
});
|
|
117
|
+
const { type: sigType, body } = await cipher.encrypt(data);
|
|
118
|
+
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
119
|
+
return { type, ciphertext: Buffer.from(body, 'binary') };
|
|
136
120
|
},
|
|
137
121
|
async encryptGroupMessage({ group, meId, data }) {
|
|
138
122
|
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
139
123
|
const builder = new Group_1.GroupSessionBuilder(storage);
|
|
140
124
|
const senderNameStr = senderName.toString();
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
};
|
|
153
|
-
});
|
|
125
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
126
|
+
if (!senderKey) {
|
|
127
|
+
await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
|
|
128
|
+
}
|
|
129
|
+
const senderKeyDistributionMessage = await builder.create(senderName);
|
|
130
|
+
const session = new Group_1.GroupCipher(storage, senderName);
|
|
131
|
+
const ciphertext = await session.encrypt(data);
|
|
132
|
+
return {
|
|
133
|
+
ciphertext,
|
|
134
|
+
senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
|
|
135
|
+
};
|
|
154
136
|
},
|
|
155
137
|
async injectE2ESession({ jid, session }) {
|
|
156
138
|
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
157
|
-
|
|
158
|
-
await cipher.initOutgoing(session);
|
|
159
|
-
});
|
|
139
|
+
await cipher.initOutgoing(session);
|
|
160
140
|
},
|
|
161
141
|
jidToSignalProtocolAddress(jid) {
|
|
162
142
|
return jidToSignalProtocolAddress(jid).toString();
|
|
@@ -283,7 +263,6 @@ function signalStorage({ creds, keys }, lidMapping) {
|
|
|
283
263
|
}
|
|
284
264
|
return null;
|
|
285
265
|
},
|
|
286
|
-
// TODO: Replace with libsignal.SessionRecord when type exports are added to libsignal
|
|
287
266
|
storeSession: async (id, session) => {
|
|
288
267
|
await keys.set({ session: { [id]: session.serialize() } });
|
|
289
268
|
},
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { SignalKeyStoreWithTransaction } from '../Types';
|
|
2
2
|
export declare class LIDMappingStore {
|
|
3
3
|
private readonly keys;
|
|
4
|
-
|
|
4
|
+
private getLIDFromPN?;
|
|
5
|
+
private getPNFromLID?;
|
|
6
|
+
constructor(keys: SignalKeyStoreWithTransaction, getLIDFromPN?: (pn: string) => Promise<string | null>, getPNFromLID?: (lid: string) => Promise<string | null>);
|
|
5
7
|
/**
|
|
6
8
|
* Store LID-PN mapping - USER LEVEL
|
|
7
9
|
*/
|
|
@@ -7,8 +7,10 @@ exports.LIDMappingStore = void 0;
|
|
|
7
7
|
const logger_1 = __importDefault(require("../Utils/logger"));
|
|
8
8
|
const WABinary_1 = require("../WABinary");
|
|
9
9
|
class LIDMappingStore {
|
|
10
|
-
constructor(keys) {
|
|
10
|
+
constructor(keys, getLIDFromPN, getPNFromLID) {
|
|
11
11
|
this.keys = keys;
|
|
12
|
+
this.getLIDFromPN = getLIDFromPN;
|
|
13
|
+
this.getPNFromLID = getPNFromLID;
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
16
|
* Store LID-PN mapping - USER LEVEL
|
|
@@ -49,7 +51,19 @@ class LIDMappingStore {
|
|
|
49
51
|
// Look up user-level mapping (whatsmeow approach)
|
|
50
52
|
const pnUser = decoded.user;
|
|
51
53
|
const stored = await this.keys.get('lid-mapping', [pnUser]);
|
|
52
|
-
|
|
54
|
+
let lidUser = stored[pnUser];
|
|
55
|
+
if (!lidUser) {
|
|
56
|
+
if (this.getLIDFromPN) {
|
|
57
|
+
const lid = await this.getLIDFromPN(`${pnUser}@s.whatsapp.net`);
|
|
58
|
+
if (lid) {
|
|
59
|
+
const decodedLid = (0, WABinary_1.jidDecode)(lid);
|
|
60
|
+
if (decodedLid) {
|
|
61
|
+
lidUser = decodedLid.user;
|
|
62
|
+
await this.storeLIDPNMapping(`${lidUser}@lid`, `${pnUser}@s.whatsapp.net`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
53
67
|
if (!lidUser) {
|
|
54
68
|
logger_1.default.trace(`No LID mapping found for PN user ${pnUser}`);
|
|
55
69
|
return null;
|
|
@@ -74,7 +88,19 @@ class LIDMappingStore {
|
|
|
74
88
|
// Look up reverse user mapping
|
|
75
89
|
const lidUser = decoded.user;
|
|
76
90
|
const stored = await this.keys.get('lid-mapping', [`${lidUser}_reverse`]);
|
|
77
|
-
|
|
91
|
+
let pnUser = stored[`${lidUser}_reverse`];
|
|
92
|
+
if (!pnUser) {
|
|
93
|
+
if (this.getPNFromLID) {
|
|
94
|
+
const pn = await this.getPNFromLID(`${lidUser}@lid`);
|
|
95
|
+
if (pn) {
|
|
96
|
+
const decodedJid = (0, WABinary_1.jidDecode)(pn);
|
|
97
|
+
if (decodedJid) {
|
|
98
|
+
pnUser = decodedJid.user;
|
|
99
|
+
await this.storeLIDPNMapping(`${lidUser}@lid`, `${pnUser}@s.whatsapp.net`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
78
104
|
if (!pnUser || typeof pnUser !== 'string') {
|
|
79
105
|
logger_1.default.trace(`No reverse mapping found for LID user: ${lidUser}`);
|
|
80
106
|
return null;
|
|
@@ -642,11 +642,11 @@ const makeMessagesRecvSocket = (config) => {
|
|
|
642
642
|
if (msg) {
|
|
643
643
|
const fromMe = (0, WABinary_1.areJidsSameUser)(node.attrs.participant || remoteJid, authState.creds.me.id);
|
|
644
644
|
msg.key = {
|
|
645
|
+
...(msg.key || {}),
|
|
645
646
|
remoteJid,
|
|
646
647
|
fromMe,
|
|
647
648
|
participant: node.attrs.participant,
|
|
648
|
-
id: node.attrs.id
|
|
649
|
-
...(msg.key || {})
|
|
649
|
+
id: node.attrs.id
|
|
650
650
|
};
|
|
651
651
|
(_a = msg.participant) !== null && _a !== void 0 ? _a : (msg.participant = node.attrs.participant);
|
|
652
652
|
msg.messageTimestamp = +node.attrs.t;
|
package/lib/Types/Auth.d.ts
CHANGED
|
@@ -100,5 +100,7 @@ export type SignalAuthState = {
|
|
|
100
100
|
export type AuthenticationState = {
|
|
101
101
|
creds: AuthenticationCreds;
|
|
102
102
|
keys: SignalKeyStore;
|
|
103
|
+
getLIDFromPN?: (pn: string) => Promise<string | null>;
|
|
104
|
+
getPNFromLID?: (lid: string) => Promise<string | null>;
|
|
103
105
|
};
|
|
104
106
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AuthenticationCreds, CacheStore, SignalKeyStore, SignalKeyStoreWithTransaction, TransactionCapabilityOptions } from '../Types';
|
|
2
|
-
import
|
|
2
|
+
import { ILogger } from './logger';
|
|
3
3
|
/**
|
|
4
4
|
* Adds caching capability to a SignalKeyStore
|
|
5
5
|
* @param store the store to add caching to
|
package/lib/Utils/auth-utils.js
CHANGED
|
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.initAuthCreds = exports.addTransactionCapability = void 0;
|
|
7
7
|
exports.makeCacheableSignalKeyStore = makeCacheableSignalKeyStore;
|
|
8
8
|
const node_cache_1 = __importDefault(require("@cacheable/node-cache"));
|
|
9
|
-
const async_mutex_1 = require("async-mutex");
|
|
10
9
|
const crypto_1 = require("crypto");
|
|
11
10
|
const Defaults_1 = require("../Defaults");
|
|
12
11
|
const crypto_2 = require("./crypto");
|
|
@@ -24,51 +23,45 @@ function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
|
24
23
|
useClones: false,
|
|
25
24
|
deleteOnExpire: true
|
|
26
25
|
});
|
|
27
|
-
// Mutex for protecting cache operations
|
|
28
|
-
const cacheMutex = new async_mutex_1.Mutex();
|
|
29
26
|
function getUniqueId(type, id) {
|
|
30
27
|
return `${type}.${id}`;
|
|
31
28
|
}
|
|
32
29
|
return {
|
|
33
30
|
async get(type, ids) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
data[id] = item;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
idsToFetch.push(id);
|
|
44
|
-
}
|
|
31
|
+
const data = {};
|
|
32
|
+
const idsToFetch = [];
|
|
33
|
+
for (const id of ids) {
|
|
34
|
+
const item = cache.get(getUniqueId(type, id));
|
|
35
|
+
if (typeof item !== 'undefined') {
|
|
36
|
+
data[id] = item;
|
|
45
37
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
38
|
+
else {
|
|
39
|
+
idsToFetch.push(id);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (idsToFetch.length) {
|
|
43
|
+
logger === null || logger === void 0 ? void 0 : logger.trace({ items: idsToFetch.length }, 'loading from store');
|
|
44
|
+
const fetched = await store.get(type, idsToFetch);
|
|
45
|
+
for (const id of idsToFetch) {
|
|
46
|
+
const item = fetched[id];
|
|
47
|
+
if (item) {
|
|
48
|
+
data[id] = item;
|
|
49
|
+
cache.set(getUniqueId(type, id), item);
|
|
55
50
|
}
|
|
56
51
|
}
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
}
|
|
53
|
+
return data;
|
|
59
54
|
},
|
|
60
55
|
async set(data) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
for (const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
keys += 1;
|
|
67
|
-
}
|
|
56
|
+
let keys = 0;
|
|
57
|
+
for (const type in data) {
|
|
58
|
+
for (const id in data[type]) {
|
|
59
|
+
cache.set(getUniqueId(type, id), data[type][id]);
|
|
60
|
+
keys += 1;
|
|
68
61
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
62
|
+
}
|
|
63
|
+
logger === null || logger === void 0 ? void 0 : logger.trace({ keys }, 'updated cache');
|
|
64
|
+
await store.set(data);
|
|
72
65
|
},
|
|
73
66
|
async clear() {
|
|
74
67
|
var _a;
|
|
@@ -77,145 +70,6 @@ function makeCacheableSignalKeyStore(store, logger, _cache) {
|
|
|
77
70
|
}
|
|
78
71
|
};
|
|
79
72
|
}
|
|
80
|
-
// Module-level specialized mutexes for pre-key operations
|
|
81
|
-
const preKeyMutex = new async_mutex_1.Mutex();
|
|
82
|
-
const signedPreKeyMutex = new async_mutex_1.Mutex();
|
|
83
|
-
/**
|
|
84
|
-
* Get the appropriate mutex for the key type
|
|
85
|
-
*/
|
|
86
|
-
const getPreKeyMutex = (keyType) => {
|
|
87
|
-
return keyType === 'signed-pre-key' ? signedPreKeyMutex : preKeyMutex;
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* Handles pre-key operations with mutex protection
|
|
91
|
-
*/
|
|
92
|
-
async function handlePreKeyOperations(data, keyType, transactionCache, mutations, logger, isInTransaction, state) {
|
|
93
|
-
const mutex = getPreKeyMutex(keyType);
|
|
94
|
-
await mutex.runExclusive(async () => {
|
|
95
|
-
const keyData = data[keyType];
|
|
96
|
-
if (!keyData)
|
|
97
|
-
return;
|
|
98
|
-
// Ensure structures exist
|
|
99
|
-
transactionCache[keyType] = transactionCache[keyType] || {};
|
|
100
|
-
mutations[keyType] = mutations[keyType] || {};
|
|
101
|
-
// Separate deletions from updates for batch processing
|
|
102
|
-
const deletionKeys = [];
|
|
103
|
-
const updateKeys = [];
|
|
104
|
-
for (const keyId in keyData) {
|
|
105
|
-
if (keyData[keyId] === null) {
|
|
106
|
-
deletionKeys.push(keyId);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
updateKeys.push(keyId);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
// Process updates first (no validation needed)
|
|
113
|
-
for (const keyId of updateKeys) {
|
|
114
|
-
if (transactionCache[keyType]) {
|
|
115
|
-
transactionCache[keyType][keyId] = keyData[keyId];
|
|
116
|
-
}
|
|
117
|
-
if (mutations[keyType]) {
|
|
118
|
-
mutations[keyType][keyId] = keyData[keyId];
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
// Process deletions with validation
|
|
122
|
-
if (deletionKeys.length === 0)
|
|
123
|
-
return;
|
|
124
|
-
if (isInTransaction) {
|
|
125
|
-
// In transaction, only allow deletion if key exists in cache
|
|
126
|
-
for (const keyId of deletionKeys) {
|
|
127
|
-
if (transactionCache[keyType]) {
|
|
128
|
-
transactionCache[keyType][keyId] = null;
|
|
129
|
-
if (mutations[keyType]) {
|
|
130
|
-
// Mark for deletion in mutations
|
|
131
|
-
mutations[keyType][keyId] = null;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
logger.warn(`Skipping deletion of non-existent ${keyType} in transaction: ${keyId}`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
// Outside transaction, batch validate all deletions
|
|
141
|
-
if (!state)
|
|
142
|
-
return;
|
|
143
|
-
const existingKeys = await state.get(keyType, deletionKeys);
|
|
144
|
-
for (const keyId of deletionKeys) {
|
|
145
|
-
if (existingKeys[keyId]) {
|
|
146
|
-
if (transactionCache[keyType])
|
|
147
|
-
transactionCache[keyType][keyId] = null;
|
|
148
|
-
if (mutations[keyType])
|
|
149
|
-
mutations[keyType][keyId] = null;
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Handles normal key operations for transactions
|
|
159
|
-
*/
|
|
160
|
-
function handleNormalKeyOperations(data, key, transactionCache, mutations) {
|
|
161
|
-
Object.assign(transactionCache[key], data[key]);
|
|
162
|
-
mutations[key] = mutations[key] || {};
|
|
163
|
-
Object.assign(mutations[key], data[key]);
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Process pre-key deletions with validation
|
|
167
|
-
*/
|
|
168
|
-
async function processPreKeyDeletions(data, keyType, state, logger) {
|
|
169
|
-
const mutex = getPreKeyMutex(keyType);
|
|
170
|
-
await mutex.runExclusive(async () => {
|
|
171
|
-
const keyData = data[keyType];
|
|
172
|
-
if (!keyData)
|
|
173
|
-
return;
|
|
174
|
-
// Validate deletions
|
|
175
|
-
for (const keyId in keyData) {
|
|
176
|
-
if (keyData[keyId] === null) {
|
|
177
|
-
const existingKeys = await state.get(keyType, [keyId]);
|
|
178
|
-
if (!existingKeys[keyId]) {
|
|
179
|
-
logger.warn(`Skipping deletion of non-existent ${keyType}: ${keyId}`);
|
|
180
|
-
if (data[keyType])
|
|
181
|
-
delete data[keyType][keyId];
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Executes a function with mutexes acquired for given key types
|
|
189
|
-
* Uses async-mutex's runExclusive with efficient batching
|
|
190
|
-
*/
|
|
191
|
-
async function withMutexes(keyTypes, getKeyTypeMutex, fn) {
|
|
192
|
-
if (keyTypes.length === 0) {
|
|
193
|
-
return fn();
|
|
194
|
-
}
|
|
195
|
-
if (keyTypes.length === 1) {
|
|
196
|
-
return getKeyTypeMutex(keyTypes[0]).runExclusive(fn);
|
|
197
|
-
}
|
|
198
|
-
// For multiple mutexes, sort by key type to prevent deadlocks
|
|
199
|
-
// Then acquire all mutexes in order using Promise.all for better efficiency
|
|
200
|
-
const sortedKeyTypes = [...keyTypes].sort();
|
|
201
|
-
const mutexes = sortedKeyTypes.map(getKeyTypeMutex);
|
|
202
|
-
// Acquire all mutexes in order to prevent deadlocks
|
|
203
|
-
const releases = [];
|
|
204
|
-
try {
|
|
205
|
-
for (const mutex of mutexes) {
|
|
206
|
-
releases.push(await mutex.acquire());
|
|
207
|
-
}
|
|
208
|
-
return await fn();
|
|
209
|
-
}
|
|
210
|
-
finally {
|
|
211
|
-
// Release in reverse order
|
|
212
|
-
while (releases.length > 0) {
|
|
213
|
-
const release = releases.pop();
|
|
214
|
-
if (release)
|
|
215
|
-
release();
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
73
|
/**
|
|
220
74
|
* Adds DB like transaction capability (https://en.wikipedia.org/wiki/Database_transaction) to the SignalKeyStore,
|
|
221
75
|
* this allows batch read & write operations & improves the performance of the lib
|
|
@@ -229,81 +83,7 @@ const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetwee
|
|
|
229
83
|
let dbQueriesInTransaction = 0;
|
|
230
84
|
let transactionCache = {};
|
|
231
85
|
let mutations = {};
|
|
232
|
-
// Mutex for each key type (session, pre-key, etc.)
|
|
233
|
-
const keyTypeMutexes = new Map();
|
|
234
|
-
// Per-sender-key-name mutexes for fine-grained serialization
|
|
235
|
-
const senderKeyMutexes = new Map();
|
|
236
|
-
// Track last usage time for sender key mutexes (for cleanup)
|
|
237
|
-
const senderKeyMutexLastUsed = new Map();
|
|
238
|
-
// Mutex expiration time: 1 hour in milliseconds
|
|
239
|
-
const SENDER_KEY_MUTEX_EXPIRY_MS = 60 * 60 * 1000;
|
|
240
|
-
// Cleanup interval: every 30 minutes
|
|
241
|
-
const CLEANUP_INTERVAL_MS = 30 * 60 * 1000;
|
|
242
|
-
// Cleanup timer
|
|
243
|
-
let cleanupTimer = null;
|
|
244
|
-
// Start cleanup timer if not already running
|
|
245
|
-
function startCleanupTimer() {
|
|
246
|
-
if (!cleanupTimer) {
|
|
247
|
-
cleanupTimer = setInterval(() => {
|
|
248
|
-
cleanupExpiredSenderKeyMutexes();
|
|
249
|
-
}, CLEANUP_INTERVAL_MS);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
// Clean up expired sender key mutexes
|
|
253
|
-
function cleanupExpiredSenderKeyMutexes() {
|
|
254
|
-
const now = Date.now();
|
|
255
|
-
const expiredKeys = [];
|
|
256
|
-
for (const [senderKeyName, lastUsed] of senderKeyMutexLastUsed.entries()) {
|
|
257
|
-
if (now - lastUsed > SENDER_KEY_MUTEX_EXPIRY_MS) {
|
|
258
|
-
const mutex = senderKeyMutexes.get(senderKeyName);
|
|
259
|
-
// Only remove if mutex is not currently being used
|
|
260
|
-
if (mutex && !mutex.isLocked()) {
|
|
261
|
-
expiredKeys.push(senderKeyName);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
if (expiredKeys.length > 0) {
|
|
266
|
-
for (const key of expiredKeys) {
|
|
267
|
-
senderKeyMutexes.delete(key);
|
|
268
|
-
senderKeyMutexLastUsed.delete(key);
|
|
269
|
-
}
|
|
270
|
-
logger.info({ expiredKeys: expiredKeys.length }, 'cleaned up expired sender key mutexes');
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
86
|
let transactionsInProgress = 0;
|
|
274
|
-
// Get or create a mutex for a specific key type
|
|
275
|
-
function getKeyTypeMutex(type) {
|
|
276
|
-
let mutex = keyTypeMutexes.get(type);
|
|
277
|
-
if (!mutex) {
|
|
278
|
-
// Create regular mutex, timeout only for critical operations
|
|
279
|
-
mutex = new async_mutex_1.Mutex();
|
|
280
|
-
keyTypeMutexes.set(type, mutex);
|
|
281
|
-
}
|
|
282
|
-
return mutex;
|
|
283
|
-
}
|
|
284
|
-
// Get or create a mutex for a specific sender key name
|
|
285
|
-
function getSenderKeyMutex(senderKeyName) {
|
|
286
|
-
let mutex = senderKeyMutexes.get(senderKeyName);
|
|
287
|
-
if (!mutex) {
|
|
288
|
-
mutex = new async_mutex_1.Mutex();
|
|
289
|
-
if (senderKeyMutexes.size === 0) {
|
|
290
|
-
startCleanupTimer();
|
|
291
|
-
}
|
|
292
|
-
senderKeyMutexes.set(senderKeyName, mutex);
|
|
293
|
-
logger.info({ senderKeyName }, 'created new sender key mutex');
|
|
294
|
-
}
|
|
295
|
-
// Update last used time
|
|
296
|
-
senderKeyMutexLastUsed.set(senderKeyName, Date.now());
|
|
297
|
-
return mutex;
|
|
298
|
-
}
|
|
299
|
-
// Sender key operations with proper mutex sequencing
|
|
300
|
-
function queueSenderKeyOperation(senderKeyName, operation) {
|
|
301
|
-
return getSenderKeyMutex(senderKeyName).runExclusive(operation);
|
|
302
|
-
}
|
|
303
|
-
// Check if we are currently in a transaction
|
|
304
|
-
function isInTransaction() {
|
|
305
|
-
return transactionsInProgress > 0;
|
|
306
|
-
}
|
|
307
87
|
return {
|
|
308
88
|
get: async (type, ids) => {
|
|
309
89
|
if (isInTransaction()) {
|
|
@@ -312,30 +92,9 @@ const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetwee
|
|
|
312
92
|
// only fetch if there are any items to fetch
|
|
313
93
|
if (idsRequiringFetch.length) {
|
|
314
94
|
dbQueriesInTransaction += 1;
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
// For sender keys, process each one with queued operations to maintain serialization
|
|
319
|
-
for (const senderKeyName of idsRequiringFetch) {
|
|
320
|
-
await queueSenderKeyOperation(senderKeyName, async () => {
|
|
321
|
-
logger.info({ senderKeyName }, 'fetching sender key in transaction');
|
|
322
|
-
const result = await state.get(type, [senderKeyName]);
|
|
323
|
-
// Update transaction cache
|
|
324
|
-
transactionCache[type] || (transactionCache[type] = {});
|
|
325
|
-
Object.assign(transactionCache[type], result);
|
|
326
|
-
logger.info({ senderKeyName, hasResult: !!result[senderKeyName] }, 'sender key fetch complete');
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
// Use runExclusive for cleaner mutex handling
|
|
332
|
-
await getKeyTypeMutex(type).runExclusive(async () => {
|
|
333
|
-
const result = await state.get(type, idsRequiringFetch);
|
|
334
|
-
// Update transaction cache
|
|
335
|
-
transactionCache[type] || (transactionCache[type] = {});
|
|
336
|
-
Object.assign(transactionCache[type], result);
|
|
337
|
-
});
|
|
338
|
-
}
|
|
95
|
+
const result = await state.get(type, idsRequiringFetch);
|
|
96
|
+
transactionCache[type] || (transactionCache[type] = {});
|
|
97
|
+
Object.assign(transactionCache[type], result);
|
|
339
98
|
}
|
|
340
99
|
return ids.reduce((dict, id) => {
|
|
341
100
|
var _a;
|
|
@@ -347,90 +106,21 @@ const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetwee
|
|
|
347
106
|
}, {});
|
|
348
107
|
}
|
|
349
108
|
else {
|
|
350
|
-
|
|
351
|
-
if (type === 'sender-key') {
|
|
352
|
-
// For sender keys, use individual queues to maintain per-key serialization
|
|
353
|
-
const results = {};
|
|
354
|
-
for (const senderKeyName of ids) {
|
|
355
|
-
const result = await queueSenderKeyOperation(senderKeyName, async () => await state.get(type, [senderKeyName]));
|
|
356
|
-
Object.assign(results, result);
|
|
357
|
-
}
|
|
358
|
-
return results;
|
|
359
|
-
}
|
|
360
|
-
else {
|
|
361
|
-
return await getKeyTypeMutex(type).runExclusive(() => state.get(type, ids));
|
|
362
|
-
}
|
|
109
|
+
return state.get(type, ids);
|
|
363
110
|
}
|
|
364
111
|
},
|
|
365
|
-
set:
|
|
112
|
+
set: data => {
|
|
366
113
|
if (isInTransaction()) {
|
|
367
114
|
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
|
|
368
|
-
for (const
|
|
369
|
-
const key = key_;
|
|
115
|
+
for (const key in data) {
|
|
370
116
|
transactionCache[key] = transactionCache[key] || {};
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
}
|
|
375
|
-
else {
|
|
376
|
-
// Normal handling for other key types
|
|
377
|
-
handleNormalKeyOperations(data, key, transactionCache, mutations);
|
|
378
|
-
}
|
|
117
|
+
Object.assign(transactionCache[key], data[key]);
|
|
118
|
+
mutations[key] = mutations[key] || {};
|
|
119
|
+
Object.assign(mutations[key], data[key]);
|
|
379
120
|
}
|
|
380
121
|
}
|
|
381
122
|
else {
|
|
382
|
-
|
|
383
|
-
const hasSenderKeys = 'sender-key' in data;
|
|
384
|
-
const senderKeyNames = hasSenderKeys ? Object.keys(data['sender-key'] || {}) : [];
|
|
385
|
-
if (hasSenderKeys) {
|
|
386
|
-
logger.info({ senderKeyNames }, 'processing sender key set operations');
|
|
387
|
-
// Handle sender key operations with per-key queues
|
|
388
|
-
for (const senderKeyName of senderKeyNames) {
|
|
389
|
-
await queueSenderKeyOperation(senderKeyName, async () => {
|
|
390
|
-
// Create data subset for this specific sender key
|
|
391
|
-
// @ts-ignore
|
|
392
|
-
const senderKeyData = {
|
|
393
|
-
'sender-key': {
|
|
394
|
-
[senderKeyName]: data['sender-key'][senderKeyName]
|
|
395
|
-
}
|
|
396
|
-
};
|
|
397
|
-
logger.trace({ senderKeyName }, 'storing sender key');
|
|
398
|
-
// Apply changes to the store
|
|
399
|
-
await state.set(senderKeyData);
|
|
400
|
-
logger.trace({ senderKeyName }, 'sender key stored');
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
// Handle any non-sender-key data with regular mutexes
|
|
404
|
-
const nonSenderKeyData = { ...data };
|
|
405
|
-
delete nonSenderKeyData['sender-key'];
|
|
406
|
-
if (Object.keys(nonSenderKeyData).length > 0) {
|
|
407
|
-
await withMutexes(Object.keys(nonSenderKeyData), getKeyTypeMutex, async () => {
|
|
408
|
-
// Process pre-keys and signed-pre-keys separately with specialized mutexes
|
|
409
|
-
for (const key_ in nonSenderKeyData) {
|
|
410
|
-
const keyType = key_;
|
|
411
|
-
if (keyType === 'pre-key') {
|
|
412
|
-
await processPreKeyDeletions(nonSenderKeyData, keyType, state, logger);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
// Apply changes to the store
|
|
416
|
-
await state.set(nonSenderKeyData);
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
421
|
-
// No sender keys - use original logic
|
|
422
|
-
await withMutexes(Object.keys(data), getKeyTypeMutex, async () => {
|
|
423
|
-
// Process pre-keys and signed-pre-keys separately with specialized mutexes
|
|
424
|
-
for (const key_ in data) {
|
|
425
|
-
const keyType = key_;
|
|
426
|
-
if (keyType === 'pre-key') {
|
|
427
|
-
await processPreKeyDeletions(data, keyType, state, logger);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
// Apply changes to the store
|
|
431
|
-
await state.set(data);
|
|
432
|
-
});
|
|
433
|
-
}
|
|
123
|
+
return state.set(data);
|
|
434
124
|
}
|
|
435
125
|
},
|
|
436
126
|
isInTransaction,
|
|
@@ -479,6 +169,9 @@ const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetwee
|
|
|
479
169
|
return result;
|
|
480
170
|
}
|
|
481
171
|
};
|
|
172
|
+
function isInTransaction() {
|
|
173
|
+
return transactionsInProgress > 0;
|
|
174
|
+
}
|
|
482
175
|
};
|
|
483
176
|
exports.addTransactionCapability = addTransactionCapability;
|
|
484
177
|
const initAuthCreds = () => {
|