@mrlegendbot/baileys 1.2.3 → 1.2.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.upstream.md +1312 -0
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +6275 -0
- package/WAProto/compiler.js +550 -0
- package/WAProto/index.js +2 -97691
- package/lib/Defaults/index.js +89 -92
- package/lib/Signal/Group/ciphertext-message.js +9 -10
- package/lib/Signal/Group/group-session-builder.js +21 -22
- package/lib/Signal/Group/group_cipher.js +75 -69
- package/lib/Signal/Group/index.js +0 -1
- package/lib/Signal/Group/keyhelper.js +9 -10
- package/lib/Signal/Group/sender-chain-key.js +21 -22
- package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
- package/lib/Signal/Group/sender-key-message.js +56 -62
- package/lib/Signal/Group/sender-key-name.js +38 -40
- package/lib/Signal/Group/sender-key-record.js +33 -34
- package/lib/Signal/Group/sender-key-state.js +72 -76
- package/lib/Signal/Group/sender-message-key.js +22 -23
- package/lib/Signal/libsignal.js +506 -405
- package/lib/Signal/lid-mapping.js +276 -258
- package/lib/Socket/Client/index.js +0 -1
- package/lib/Socket/Client/types.js +6 -7
- package/lib/Socket/Client/websocket.js +48 -49
- package/lib/Socket/business.js +349 -369
- package/lib/Socket/chats.js +1172 -1148
- package/lib/Socket/communities.js +473 -413
- package/lib/Socket/groups.js +387 -331
- package/lib/Socket/index.js +6 -8
- package/lib/Socket/messages-recv.js +1782 -1719
- package/lib/Socket/messages-send.js +1554 -2450
- package/lib/Socket/mex.js +40 -35
- package/lib/Socket/newsletter.js +239 -191
- package/lib/Socket/socket.js +992 -923
- package/lib/Store/index.js +8 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +70 -0
- package/lib/Store/make-in-memory-store.js +417 -0
- package/lib/Store/make-ordered-dictionary.js +69 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +0 -1
- package/lib/Types/Bussines.js +0 -1
- package/lib/Types/Call.js +0 -1
- package/lib/Types/Chat.js +1 -8
- package/lib/Types/Contact.js +0 -1
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +0 -1
- package/lib/Types/Label.js +20 -22
- package/lib/Types/LabelAssociation.js +2 -4
- package/lib/Types/Message.js +3 -5
- package/lib/Types/Mex.js +26 -31
- package/lib/Types/Product.js +0 -1
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +4 -52
- package/lib/Types/USync.js +0 -1
- package/lib/Types/index.js +10 -11
- package/lib/Utils/auth-utils.js +240 -277
- package/lib/Utils/browser-utils.js +20 -23
- package/lib/Utils/business.js +200 -204
- package/lib/Utils/chat-utils.js +836 -810
- package/lib/Utils/companion-reg-client-utils.js +23 -25
- package/lib/Utils/crypto.js +70 -86
- package/lib/Utils/decode-wa-message.js +277 -284
- package/lib/Utils/event-buffer.js +592 -595
- package/lib/Utils/generics.js +312 -335
- package/lib/Utils/history.js +123 -115
- package/lib/Utils/index.js +1 -2
- package/lib/Utils/link-preview.js +74 -73
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +27 -27
- package/lib/Utils/message-retry-manager.js +213 -254
- package/lib/Utils/messages-media.js +741 -715
- package/lib/Utils/messages.js +791 -827
- package/lib/Utils/noise-handler.js +215 -187
- package/lib/Utils/pre-key-manager.js +69 -98
- package/lib/Utils/process-message.js +617 -586
- package/lib/Utils/reporting-utils.js +553 -234
- package/lib/Utils/signal.js +217 -170
- package/lib/Utils/stanza-ack.js +27 -35
- package/lib/Utils/sync-action-utils.js +37 -41
- package/lib/Utils/tc-token-utils.js +120 -141
- package/lib/Utils/use-multi-file-auth-state.js +87 -105
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/Utils/validate-connection.js +190 -188
- package/lib/WABinary/constants.js +28 -1294
- package/lib/WABinary/decode.js +245 -249
- package/lib/WABinary/encode.js +201 -206
- package/lib/WABinary/generic-utils.js +86 -89
- package/lib/WABinary/index.js +0 -1
- package/lib/WABinary/jid-utils.js +68 -77
- package/lib/WABinary/types.js +0 -1
- package/lib/WAM/BinaryInfo.js +7 -8
- package/lib/WAM/constants.js +19892 -22853
- package/lib/WAM/encode.js +121 -132
- package/lib/WAM/index.js +0 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
- package/lib/WAUSync/Protocols/index.js +0 -1
- package/lib/WAUSync/USyncQuery.js +79 -88
- package/lib/WAUSync/USyncUser.js +28 -29
- package/lib/WAUSync/index.js +0 -1
- package/lib/index.js +2 -2
- package/package.json +22 -86
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/fix-imports.js +0 -85
- package/WAProto/index.d.ts +0 -14017
- package/lib/Defaults/index.d.ts +0 -75
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
- package/lib/Signal/Group/group-session-builder.d.ts +0 -15
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -12
- package/lib/Signal/Group/keyhelper.d.ts +0 -11
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -18
- package/lib/Signal/Group/sender-key-record.d.ts +0 -31
- package/lib/Signal/Group/sender-key-state.d.ts +0 -39
- package/lib/Signal/Group/sender-message-key.d.ts +0 -12
- package/lib/Signal/libsignal.d.ts +0 -5
- package/lib/Signal/lid-mapping.d.ts +0 -23
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -217
- package/lib/Socket/chats.d.ts +0 -125
- package/lib/Socket/communities.d.ts +0 -273
- package/lib/Socket/groups.d.ts +0 -161
- package/lib/Socket/index.d.ts +0 -260
- package/lib/Socket/messages-recv.d.ts +0 -203
- package/lib/Socket/messages-send.d.ts +0 -199
- package/lib/Socket/mex.d.ts +0 -3
- package/lib/Socket/newsletter.d.ts +0 -170
- package/lib/Socket/socket.d.ts +0 -59
- package/lib/Types/Auth.d.ts +0 -117
- package/lib/Types/Bussines.d.ts +0 -25
- package/lib/Types/Call.d.ts +0 -15
- package/lib/Types/Chat.d.ts +0 -124
- package/lib/Types/Contact.d.ts +0 -26
- package/lib/Types/Events.d.ts +0 -256
- package/lib/Types/GroupMetadata.d.ts +0 -71
- package/lib/Types/Label.d.ts +0 -47
- package/lib/Types/LabelAssociation.d.ts +0 -30
- package/lib/Types/Message.d.ts +0 -320
- package/lib/Types/Mex.d.ts +0 -141
- package/lib/Types/Product.d.ts +0 -79
- package/lib/Types/Signal.d.ts +0 -87
- package/lib/Types/Socket.d.ts +0 -136
- package/lib/Types/State.d.ts +0 -97
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -66
- package/lib/Utils/auth-utils.d.ts +0 -24
- package/lib/Utils/browser-utils.d.ts +0 -4
- package/lib/Utils/business.d.ts +0 -23
- package/lib/Utils/chat-utils.d.ts +0 -100
- package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
- package/lib/Utils/crypto.d.ts +0 -37
- package/lib/Utils/decode-wa-message.d.ts +0 -66
- package/lib/Utils/event-buffer.d.ts +0 -36
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -24
- package/lib/Utils/identity-change-handler.d.ts +0 -44
- package/lib/Utils/identity-change-handler.js +0 -50
- package/lib/Utils/index.d.ts +0 -22
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -12
- package/lib/Utils/lt-hash.d.ts +0 -8
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -115
- package/lib/Utils/messages-media.d.ts +0 -133
- package/lib/Utils/messages.d.ts +0 -91
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/offline-node-processor.d.ts +0 -17
- package/lib/Utils/offline-node-processor.js +0 -40
- package/lib/Utils/pre-key-manager.d.ts +0 -28
- package/lib/Utils/process-message.d.ts +0 -60
- package/lib/Utils/reporting-utils.d.ts +0 -11
- package/lib/Utils/signal.d.ts +0 -47
- package/lib/Utils/stanza-ack.d.ts +0 -11
- package/lib/Utils/sync-action-utils.d.ts +0 -19
- package/lib/Utils/tc-token-utils.d.ts +0 -37
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -28
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -6
- package/lib/WABinary/jid-utils.d.ts +0 -48
- package/lib/WABinary/types.d.ts +0 -19
- package/lib/WAM/BinaryInfo.d.ts +0 -9
- package/lib/WAM/constants.d.ts +0 -40
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -30
- package/lib/WAUSync/USyncUser.d.ts +0 -17
- package/lib/WAUSync/index.d.ts +0 -4
- package/lib/index.d.ts +0 -12
package/lib/Signal/libsignal.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
1
|
import * as libsignal from 'libsignal';
|
|
3
|
-
// @ts-ignore
|
|
4
2
|
import { PreKeyWhisperMessage } from 'libsignal/src/protobufs.js';
|
|
5
3
|
import { LRUCache } from 'lru-cache';
|
|
6
4
|
import { generateSignalPubKey } from '../Utils/index.js';
|
|
@@ -9,423 +7,526 @@ import { SenderKeyName } from './Group/sender-key-name.js';
|
|
|
9
7
|
import { SenderKeyRecord } from './Group/sender-key-record.js';
|
|
10
8
|
import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
|
|
11
9
|
import { LIDMappingStore } from './lid-mapping.js';
|
|
12
|
-
/** Extract identity key from PreKeyWhisperMessage for identity change detection */
|
|
13
10
|
function extractIdentityFromPkmsg(ciphertext) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
// Version byte check (version 3)
|
|
19
|
-
const version = ciphertext[0];
|
|
20
|
-
if ((version & 0xf) !== 3) {
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
// Parse protobuf (skip version byte)
|
|
24
|
-
const preKeyProto = PreKeyWhisperMessage.decode(ciphertext.slice(1));
|
|
25
|
-
if (preKeyProto.identityKey?.length === 33) {
|
|
26
|
-
return new Uint8Array(preKeyProto.identityKey);
|
|
27
|
-
}
|
|
28
|
-
return undefined;
|
|
11
|
+
try {
|
|
12
|
+
if (!ciphertext || ciphertext.length < 2) {
|
|
13
|
+
return undefined;
|
|
29
14
|
}
|
|
30
|
-
|
|
31
|
-
|
|
15
|
+
const version = ciphertext[0];
|
|
16
|
+
if ((version & 0xf) !== 3) {
|
|
17
|
+
return undefined;
|
|
32
18
|
}
|
|
19
|
+
const preKeyProto = PreKeyWhisperMessage.decode(ciphertext.slice(1));
|
|
20
|
+
if (preKeyProto.identityKey?.length === 33) {
|
|
21
|
+
return new Uint8Array(preKeyProto.identityKey);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
} catch {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
33
27
|
}
|
|
34
28
|
export function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
29
|
+
const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc);
|
|
30
|
+
const storage = signalStorage(auth, lidMapping);
|
|
31
|
+
const parsedKeys = auth.keys;
|
|
32
|
+
const migratedSessionCache = new LRUCache({
|
|
33
|
+
ttl: 3 * 24 * 60 * 60 * 1000,
|
|
34
|
+
ttlAutopurge: true,
|
|
35
|
+
updateAgeOnGet: true
|
|
36
|
+
});
|
|
37
|
+
const ensureSenderKeyAndCreateSkdm = async (group, meId) => {
|
|
38
|
+
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
39
|
+
const senderNameStr = senderName.toString();
|
|
40
|
+
const {
|
|
41
|
+
[senderNameStr]: senderKey
|
|
42
|
+
} = await auth.keys.get('sender-key', [senderNameStr]);
|
|
43
|
+
if (!senderKey) {
|
|
44
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
45
|
+
}
|
|
46
|
+
const skdm = await new GroupSessionBuilder(storage).create(senderName);
|
|
47
|
+
return {
|
|
48
|
+
senderName,
|
|
49
|
+
skdm
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const repository = {
|
|
53
|
+
decryptGroupMessage({
|
|
54
|
+
group,
|
|
55
|
+
authorJid,
|
|
56
|
+
msg
|
|
57
|
+
}) {
|
|
58
|
+
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
59
|
+
const cipher = new GroupCipher(storage, senderName);
|
|
60
|
+
return parsedKeys.transaction(async () => {
|
|
61
|
+
return cipher.decrypt(msg);
|
|
62
|
+
}, group);
|
|
63
|
+
},
|
|
64
|
+
async processSenderKeyDistributionMessage({
|
|
65
|
+
item,
|
|
66
|
+
authorJid
|
|
67
|
+
}) {
|
|
68
|
+
const builder = new GroupSessionBuilder(storage);
|
|
69
|
+
if (!item.groupId) {
|
|
70
|
+
throw new Error('Group ID is required for sender key distribution message');
|
|
71
|
+
}
|
|
72
|
+
const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
|
|
73
|
+
const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
|
|
74
|
+
const senderNameStr = senderName.toString();
|
|
75
|
+
const {
|
|
76
|
+
[senderNameStr]: senderKey
|
|
77
|
+
} = await auth.keys.get('sender-key', [senderNameStr]);
|
|
78
|
+
if (!senderKey) {
|
|
79
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
80
|
+
}
|
|
81
|
+
return parsedKeys.transaction(async () => {
|
|
82
|
+
const {
|
|
83
|
+
[senderNameStr]: senderKey
|
|
84
|
+
} = await auth.keys.get('sender-key', [senderNameStr]);
|
|
47
85
|
if (!senderKey) {
|
|
48
|
-
|
|
86
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
49
87
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
88
|
+
await builder.process(senderName, senderMsg);
|
|
89
|
+
}, item.groupId);
|
|
90
|
+
},
|
|
91
|
+
async decryptMessage({
|
|
92
|
+
jid,
|
|
93
|
+
type,
|
|
94
|
+
ciphertext
|
|
95
|
+
}) {
|
|
96
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
97
|
+
const session = new libsignal.SessionCipher(storage, addr);
|
|
98
|
+
if (type === 'pkmsg') {
|
|
99
|
+
const identityKey = extractIdentityFromPkmsg(ciphertext);
|
|
100
|
+
if (identityKey) {
|
|
101
|
+
const addrStr = addr.toString();
|
|
102
|
+
const identityChanged = await storage.saveIdentity(addrStr, identityKey);
|
|
103
|
+
if (identityChanged) {
|
|
104
|
+
logger.info({
|
|
105
|
+
jid,
|
|
106
|
+
addr: addrStr
|
|
107
|
+
}, 'identity key changed or new contact, session will be re-established');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function doDecrypt() {
|
|
112
|
+
let result;
|
|
113
|
+
switch (type) {
|
|
114
|
+
case 'pkmsg':
|
|
115
|
+
result = await session.decryptPreKeyWhisperMessage(ciphertext);
|
|
116
|
+
break;
|
|
117
|
+
case 'msg':
|
|
118
|
+
result = await session.decryptWhisperMessage(ciphertext);
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
return parsedKeys.transaction(async () => {
|
|
124
|
+
return await doDecrypt();
|
|
125
|
+
}, jid);
|
|
126
|
+
},
|
|
127
|
+
async encryptMessage({
|
|
128
|
+
jid,
|
|
129
|
+
data
|
|
130
|
+
}) {
|
|
131
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
132
|
+
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
133
|
+
return parsedKeys.transaction(async () => {
|
|
134
|
+
const {
|
|
135
|
+
type: sigType,
|
|
136
|
+
body
|
|
137
|
+
} = await cipher.encrypt(data);
|
|
138
|
+
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
139
|
+
return {
|
|
140
|
+
type,
|
|
141
|
+
ciphertext: Buffer.from(body, 'binary')
|
|
142
|
+
};
|
|
143
|
+
}, jid);
|
|
144
|
+
},
|
|
145
|
+
async encryptGroupMessage({
|
|
146
|
+
group,
|
|
147
|
+
meId,
|
|
148
|
+
data
|
|
149
|
+
}) {
|
|
150
|
+
return parsedKeys.transaction(async () => {
|
|
151
|
+
const {
|
|
152
|
+
senderName,
|
|
153
|
+
skdm
|
|
154
|
+
} = await ensureSenderKeyAndCreateSkdm(group, meId);
|
|
155
|
+
const ciphertext = await new GroupCipher(storage, senderName).encrypt(data);
|
|
156
|
+
return {
|
|
157
|
+
ciphertext,
|
|
158
|
+
senderKeyDistributionMessage: skdm.serialize()
|
|
159
|
+
};
|
|
160
|
+
}, group);
|
|
161
|
+
},
|
|
162
|
+
async getSenderKeyDistributionMessage({
|
|
163
|
+
group,
|
|
164
|
+
meId
|
|
165
|
+
}) {
|
|
166
|
+
return parsedKeys.transaction(async () => {
|
|
167
|
+
const {
|
|
168
|
+
skdm
|
|
169
|
+
} = await ensureSenderKeyAndCreateSkdm(group, meId);
|
|
170
|
+
return skdm.serialize();
|
|
171
|
+
}, group);
|
|
172
|
+
},
|
|
173
|
+
async hasSenderKey({
|
|
174
|
+
group,
|
|
175
|
+
meId
|
|
176
|
+
}) {
|
|
177
|
+
const senderName = jidToSignalSenderKeyName(group, meId).toString();
|
|
178
|
+
const {
|
|
179
|
+
[senderName]: key
|
|
180
|
+
} = await auth.keys.get('sender-key', [senderName]);
|
|
181
|
+
return !!key;
|
|
182
|
+
},
|
|
183
|
+
async getSessionInfo(jid) {
|
|
184
|
+
const addr = jidToSignalProtocolAddress(jid).toString();
|
|
185
|
+
const session = await storage.loadSession(addr);
|
|
186
|
+
if (!session) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const open = session.getOpenSession?.();
|
|
190
|
+
const baseKey = open?.indexInfo?.baseKey;
|
|
191
|
+
const registrationId = open?.registrationId;
|
|
192
|
+
if (!baseKey || typeof registrationId !== 'number') {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
baseKey: new Uint8Array(baseKey),
|
|
197
|
+
registrationId
|
|
198
|
+
};
|
|
199
|
+
},
|
|
200
|
+
async injectE2ESession({
|
|
201
|
+
jid,
|
|
202
|
+
session
|
|
203
|
+
}) {
|
|
204
|
+
logger.trace({
|
|
205
|
+
jid
|
|
206
|
+
}, 'injecting E2EE session');
|
|
207
|
+
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
208
|
+
return parsedKeys.transaction(async () => {
|
|
209
|
+
await cipher.initOutgoing(session);
|
|
210
|
+
}, jid);
|
|
211
|
+
},
|
|
212
|
+
jidToSignalProtocolAddress(jid) {
|
|
213
|
+
return jidToSignalProtocolAddress(jid).toString();
|
|
214
|
+
},
|
|
215
|
+
lidMapping,
|
|
216
|
+
async validateSession(jid) {
|
|
217
|
+
try {
|
|
218
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
219
|
+
const session = await storage.loadSession(addr.toString());
|
|
220
|
+
if (!session) {
|
|
221
|
+
return {
|
|
222
|
+
exists: false,
|
|
223
|
+
reason: 'no session'
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (!session.haveOpenSession()) {
|
|
227
|
+
return {
|
|
228
|
+
exists: false,
|
|
229
|
+
reason: 'no open session'
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
exists: true
|
|
234
|
+
};
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return {
|
|
237
|
+
exists: false,
|
|
238
|
+
reason: 'validation error'
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
async deleteSession(jids) {
|
|
243
|
+
if (!jids.length) return;
|
|
244
|
+
const sessionUpdates = {};
|
|
245
|
+
jids.forEach(jid => {
|
|
246
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
247
|
+
sessionUpdates[addr.toString()] = null;
|
|
248
|
+
});
|
|
249
|
+
return parsedKeys.transaction(async () => {
|
|
250
|
+
await auth.keys.set({
|
|
251
|
+
session: sessionUpdates
|
|
252
|
+
});
|
|
253
|
+
}, `delete-${jids.length}-sessions`);
|
|
254
|
+
},
|
|
255
|
+
close() {
|
|
256
|
+
migratedSessionCache.clear();
|
|
257
|
+
lidMapping.close();
|
|
258
|
+
},
|
|
259
|
+
async migrateSession(fromJid, toJid) {
|
|
260
|
+
if (!fromJid || !isLidUser(toJid) && !isHostedLidUser(toJid)) return {
|
|
261
|
+
migrated: 0,
|
|
262
|
+
skipped: 0,
|
|
263
|
+
total: 0
|
|
264
|
+
};
|
|
265
|
+
if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
|
|
266
|
+
return {
|
|
267
|
+
migrated: 0,
|
|
268
|
+
skipped: 0,
|
|
269
|
+
total: 1
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
const {
|
|
273
|
+
user
|
|
274
|
+
} = jidDecode(fromJid);
|
|
275
|
+
logger.debug({
|
|
276
|
+
fromJid
|
|
277
|
+
}, 'bulk device migration - loading all user devices');
|
|
278
|
+
const {
|
|
279
|
+
[user]: userDevices
|
|
280
|
+
} = await parsedKeys.get('device-list', [user]);
|
|
281
|
+
if (!userDevices) {
|
|
282
|
+
return {
|
|
283
|
+
migrated: 0,
|
|
284
|
+
skipped: 0,
|
|
285
|
+
total: 0
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const {
|
|
289
|
+
device: fromDevice
|
|
290
|
+
} = jidDecode(fromJid);
|
|
291
|
+
const fromDeviceStr = fromDevice?.toString() || '0';
|
|
292
|
+
if (!userDevices.includes(fromDeviceStr)) {
|
|
293
|
+
userDevices.push(fromDeviceStr);
|
|
294
|
+
}
|
|
295
|
+
const uncachedDevices = userDevices.filter(device => {
|
|
296
|
+
const deviceKey = `${user}.${device}`;
|
|
297
|
+
return !migratedSessionCache.has(deviceKey);
|
|
298
|
+
});
|
|
299
|
+
const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
|
|
300
|
+
const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
|
|
301
|
+
const deviceJids = [];
|
|
302
|
+
for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
|
|
303
|
+
if (sessionData) {
|
|
304
|
+
const deviceStr = sessionKey.split('.')[1];
|
|
305
|
+
if (!deviceStr) continue;
|
|
306
|
+
const deviceNum = parseInt(deviceStr);
|
|
307
|
+
let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
|
|
308
|
+
if (deviceNum === 99) {
|
|
309
|
+
jid = `${user}:99@hosted`;
|
|
310
|
+
}
|
|
311
|
+
deviceJids.push(jid);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
logger.debug({
|
|
315
|
+
fromJid,
|
|
316
|
+
totalDevices: userDevices.length,
|
|
317
|
+
devicesWithSessions: deviceJids.length,
|
|
318
|
+
devices: deviceJids
|
|
319
|
+
}, 'bulk device migration complete - all user devices processed');
|
|
320
|
+
return parsedKeys.transaction(async () => {
|
|
321
|
+
const migrationOps = deviceJids.map(jid => {
|
|
322
|
+
const lidWithDevice = transferDevice(jid, toJid);
|
|
323
|
+
const fromDecoded = jidDecode(jid);
|
|
324
|
+
const toDecoded = jidDecode(lidWithDevice);
|
|
325
|
+
return {
|
|
326
|
+
fromJid: jid,
|
|
327
|
+
toJid: lidWithDevice,
|
|
328
|
+
pnUser: fromDecoded.user,
|
|
329
|
+
lidUser: toDecoded.user,
|
|
330
|
+
deviceId: fromDecoded.device || 0,
|
|
331
|
+
fromAddr: jidToSignalProtocolAddress(jid),
|
|
332
|
+
toAddr: jidToSignalProtocolAddress(lidWithDevice)
|
|
333
|
+
};
|
|
334
|
+
});
|
|
335
|
+
const totalOps = migrationOps.length;
|
|
336
|
+
let migratedCount = 0;
|
|
337
|
+
const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
|
|
338
|
+
const pnSessions = await parsedKeys.get('session', pnAddrStrings);
|
|
339
|
+
const sessionUpdates = {};
|
|
340
|
+
for (const op of migrationOps) {
|
|
341
|
+
const pnAddrStr = op.fromAddr.toString();
|
|
342
|
+
const lidAddrStr = op.toAddr.toString();
|
|
343
|
+
const pnSession = pnSessions[pnAddrStr];
|
|
344
|
+
if (pnSession) {
|
|
345
|
+
const fromSession = libsignal.SessionRecord.deserialize(pnSession);
|
|
346
|
+
if (fromSession.haveOpenSession()) {
|
|
347
|
+
sessionUpdates[lidAddrStr] = fromSession.serialize();
|
|
348
|
+
sessionUpdates[pnAddrStr] = null;
|
|
349
|
+
migratedCount++;
|
|
223
350
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const deviceStr = sessionKey.split('.')[1];
|
|
238
|
-
if (!deviceStr)
|
|
239
|
-
continue;
|
|
240
|
-
const deviceNum = parseInt(deviceStr);
|
|
241
|
-
let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
|
|
242
|
-
if (deviceNum === 99) {
|
|
243
|
-
jid = `${user}:99@hosted`;
|
|
244
|
-
}
|
|
245
|
-
deviceJids.push(jid);
|
|
246
|
-
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
if (Object.keys(sessionUpdates).length > 0) {
|
|
354
|
+
await parsedKeys.set({
|
|
355
|
+
session: sessionUpdates
|
|
356
|
+
});
|
|
357
|
+
logger.debug({
|
|
358
|
+
migratedSessions: migratedCount
|
|
359
|
+
}, 'bulk session migration complete');
|
|
360
|
+
for (const op of migrationOps) {
|
|
361
|
+
if (sessionUpdates[op.toAddr.toString()]) {
|
|
362
|
+
const deviceKey = `${op.pnUser}.${op.deviceId}`;
|
|
363
|
+
migratedSessionCache.set(deviceKey, true);
|
|
247
364
|
}
|
|
248
|
-
|
|
249
|
-
fromJid,
|
|
250
|
-
totalDevices: userDevices.length,
|
|
251
|
-
devicesWithSessions: deviceJids.length,
|
|
252
|
-
devices: deviceJids
|
|
253
|
-
}, 'bulk device migration complete - all user devices processed');
|
|
254
|
-
// Single transaction for all migrations
|
|
255
|
-
return parsedKeys.transaction(async () => {
|
|
256
|
-
const migrationOps = deviceJids.map(jid => {
|
|
257
|
-
const lidWithDevice = transferDevice(jid, toJid);
|
|
258
|
-
const fromDecoded = jidDecode(jid);
|
|
259
|
-
const toDecoded = jidDecode(lidWithDevice);
|
|
260
|
-
return {
|
|
261
|
-
fromJid: jid,
|
|
262
|
-
toJid: lidWithDevice,
|
|
263
|
-
pnUser: fromDecoded.user,
|
|
264
|
-
lidUser: toDecoded.user,
|
|
265
|
-
deviceId: fromDecoded.device || 0,
|
|
266
|
-
fromAddr: jidToSignalProtocolAddress(jid),
|
|
267
|
-
toAddr: jidToSignalProtocolAddress(lidWithDevice)
|
|
268
|
-
};
|
|
269
|
-
});
|
|
270
|
-
const totalOps = migrationOps.length;
|
|
271
|
-
let migratedCount = 0;
|
|
272
|
-
// Bulk fetch PN sessions - already exist (verified during device discovery)
|
|
273
|
-
const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
|
|
274
|
-
const pnSessions = await parsedKeys.get('session', pnAddrStrings);
|
|
275
|
-
// Prepare bulk session updates (PN → LID migration + deletion)
|
|
276
|
-
const sessionUpdates = {};
|
|
277
|
-
for (const op of migrationOps) {
|
|
278
|
-
const pnAddrStr = op.fromAddr.toString();
|
|
279
|
-
const lidAddrStr = op.toAddr.toString();
|
|
280
|
-
const pnSession = pnSessions[pnAddrStr];
|
|
281
|
-
if (pnSession) {
|
|
282
|
-
// Session exists (guaranteed from device discovery)
|
|
283
|
-
const fromSession = libsignal.SessionRecord.deserialize(pnSession);
|
|
284
|
-
if (fromSession.haveOpenSession()) {
|
|
285
|
-
// Queue for bulk update: copy to LID, delete from PN
|
|
286
|
-
sessionUpdates[lidAddrStr] = fromSession.serialize();
|
|
287
|
-
sessionUpdates[pnAddrStr] = null;
|
|
288
|
-
migratedCount++;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
// Single bulk session update for all migrations
|
|
293
|
-
if (Object.keys(sessionUpdates).length > 0) {
|
|
294
|
-
await parsedKeys.set({ session: sessionUpdates });
|
|
295
|
-
logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
|
|
296
|
-
// Cache device-level migrations
|
|
297
|
-
for (const op of migrationOps) {
|
|
298
|
-
if (sessionUpdates[op.toAddr.toString()]) {
|
|
299
|
-
const deviceKey = `${op.pnUser}.${op.deviceId}`;
|
|
300
|
-
migratedSessionCache.set(deviceKey, true);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
const skippedCount = totalOps - migratedCount;
|
|
305
|
-
return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
|
|
306
|
-
}, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
|
|
365
|
+
}
|
|
307
366
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
|
|
367
|
+
const skippedCount = totalOps - migratedCount;
|
|
368
|
+
return {
|
|
369
|
+
migrated: migratedCount,
|
|
370
|
+
skipped: skippedCount,
|
|
371
|
+
total: totalOps
|
|
372
|
+
};
|
|
373
|
+
}, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
|
|
316
374
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
375
|
+
};
|
|
376
|
+
return repository;
|
|
377
|
+
}
|
|
378
|
+
const jidToSignalProtocolAddress = jid => {
|
|
379
|
+
const decoded = jidDecode(jid);
|
|
380
|
+
const {
|
|
381
|
+
user,
|
|
382
|
+
device,
|
|
383
|
+
server,
|
|
384
|
+
domainType
|
|
385
|
+
} = decoded;
|
|
386
|
+
if (!user) {
|
|
387
|
+
throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
|
|
388
|
+
}
|
|
389
|
+
const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
|
|
390
|
+
const finalDevice = device || 0;
|
|
391
|
+
if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
|
|
392
|
+
throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
|
|
393
|
+
}
|
|
394
|
+
return new libsignal.ProtocolAddress(signalUser, finalDevice);
|
|
323
395
|
};
|
|
324
396
|
const jidToSignalSenderKeyName = (group, user) => {
|
|
325
|
-
|
|
397
|
+
return new SenderKeyName(group, jidToSignalProtocolAddress(user));
|
|
326
398
|
};
|
|
327
|
-
function signalStorage({
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
399
|
+
function signalStorage({
|
|
400
|
+
creds,
|
|
401
|
+
keys
|
|
402
|
+
}, lidMapping) {
|
|
403
|
+
const resolveLIDSignalAddress = async id => {
|
|
404
|
+
if (id.includes('.')) {
|
|
405
|
+
const [deviceId, device] = id.split('.');
|
|
406
|
+
const [user, domainType_] = deviceId.split('_');
|
|
407
|
+
const domainType = parseInt(domainType_ || '0');
|
|
408
|
+
if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID) return id;
|
|
409
|
+
const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
|
|
410
|
+
const lidForPN = await lidMapping.getLIDForPN(pnJid);
|
|
411
|
+
if (lidForPN) {
|
|
412
|
+
const lidAddr = jidToSignalProtocolAddress(lidForPN);
|
|
413
|
+
return lidAddr.toString();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return id;
|
|
417
|
+
};
|
|
418
|
+
return {
|
|
419
|
+
loadSession: async id => {
|
|
420
|
+
try {
|
|
421
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
422
|
+
const {
|
|
423
|
+
[wireJid]: sess
|
|
424
|
+
} = await keys.get('session', [wireJid]);
|
|
425
|
+
if (sess) {
|
|
426
|
+
return libsignal.SessionRecord.deserialize(sess);
|
|
342
427
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
354
|
-
catch (e) {
|
|
355
|
-
return null;
|
|
356
|
-
}
|
|
357
|
-
return null;
|
|
358
|
-
},
|
|
359
|
-
storeSession: async (id, session) => {
|
|
360
|
-
const wireJid = await resolveLIDSignalAddress(id);
|
|
361
|
-
await keys.set({ session: { [wireJid]: session.serialize() } });
|
|
362
|
-
},
|
|
363
|
-
isTrustedIdentity: () => {
|
|
364
|
-
return true; // TOFU - Trust on First Use (same as WhatsApp Web)
|
|
365
|
-
},
|
|
366
|
-
loadIdentityKey: async (id) => {
|
|
367
|
-
const wireJid = await resolveLIDSignalAddress(id);
|
|
368
|
-
const { [wireJid]: key } = await keys.get('identity-key', [wireJid]);
|
|
369
|
-
return key || undefined;
|
|
370
|
-
},
|
|
371
|
-
saveIdentity: async (id, identityKey) => {
|
|
372
|
-
const wireJid = await resolveLIDSignalAddress(id);
|
|
373
|
-
const { [wireJid]: existingKey } = await keys.get('identity-key', [wireJid]);
|
|
374
|
-
const keysMatch = existingKey?.length === identityKey.length && existingKey.every((byte, i) => byte === identityKey[i]);
|
|
375
|
-
if (existingKey && !keysMatch) {
|
|
376
|
-
// Identity changed - clear session and update key
|
|
377
|
-
await keys.set({
|
|
378
|
-
session: { [wireJid]: null },
|
|
379
|
-
'identity-key': { [wireJid]: identityKey }
|
|
380
|
-
});
|
|
381
|
-
return true;
|
|
382
|
-
}
|
|
383
|
-
if (!existingKey) {
|
|
384
|
-
// New contact - Trust on First Use (TOFU)
|
|
385
|
-
await keys.set({ 'identity-key': { [wireJid]: identityKey } });
|
|
386
|
-
return true;
|
|
387
|
-
}
|
|
388
|
-
return false;
|
|
389
|
-
},
|
|
390
|
-
loadPreKey: async (id) => {
|
|
391
|
-
const keyId = id.toString();
|
|
392
|
-
const { [keyId]: key } = await keys.get('pre-key', [keyId]);
|
|
393
|
-
if (key) {
|
|
394
|
-
return {
|
|
395
|
-
privKey: Buffer.from(key.private),
|
|
396
|
-
pubKey: Buffer.from(key.public)
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
},
|
|
400
|
-
removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
|
|
401
|
-
loadSignedPreKey: () => {
|
|
402
|
-
const key = creds.signedPreKey;
|
|
403
|
-
return {
|
|
404
|
-
privKey: Buffer.from(key.keyPair.private),
|
|
405
|
-
pubKey: Buffer.from(key.keyPair.public)
|
|
406
|
-
};
|
|
407
|
-
},
|
|
408
|
-
loadSenderKey: async (senderKeyName) => {
|
|
409
|
-
const keyId = senderKeyName.toString();
|
|
410
|
-
const { [keyId]: key } = await keys.get('sender-key', [keyId]);
|
|
411
|
-
if (key) {
|
|
412
|
-
return SenderKeyRecord.deserialize(key);
|
|
413
|
-
}
|
|
414
|
-
return new SenderKeyRecord();
|
|
415
|
-
},
|
|
416
|
-
storeSenderKey: async (senderKeyName, key) => {
|
|
417
|
-
const keyId = senderKeyName.toString();
|
|
418
|
-
const serialized = JSON.stringify(key.serialize());
|
|
419
|
-
await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
|
|
420
|
-
},
|
|
421
|
-
getOurRegistrationId: () => creds.registrationId,
|
|
422
|
-
getOurIdentity: () => {
|
|
423
|
-
const { signedIdentityKey } = creds;
|
|
424
|
-
return {
|
|
425
|
-
privKey: Buffer.from(signedIdentityKey.private),
|
|
426
|
-
pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
|
|
427
|
-
};
|
|
428
|
+
} catch (e) {
|
|
429
|
+
return null;
|
|
430
|
+
}
|
|
431
|
+
return null;
|
|
432
|
+
},
|
|
433
|
+
storeSession: async (id, session) => {
|
|
434
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
435
|
+
await keys.set({
|
|
436
|
+
session: {
|
|
437
|
+
[wireJid]: session.serialize()
|
|
428
438
|
}
|
|
429
|
-
|
|
439
|
+
});
|
|
440
|
+
},
|
|
441
|
+
isTrustedIdentity: () => {
|
|
442
|
+
return true;
|
|
443
|
+
},
|
|
444
|
+
loadIdentityKey: async id => {
|
|
445
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
446
|
+
const {
|
|
447
|
+
[wireJid]: key
|
|
448
|
+
} = await keys.get('identity-key', [wireJid]);
|
|
449
|
+
return key || undefined;
|
|
450
|
+
},
|
|
451
|
+
saveIdentity: async (id, identityKey) => {
|
|
452
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
453
|
+
const {
|
|
454
|
+
[wireJid]: existingKey
|
|
455
|
+
} = await keys.get('identity-key', [wireJid]);
|
|
456
|
+
const keysMatch = existingKey?.length === identityKey.length && existingKey.every((byte, i) => byte === identityKey[i]);
|
|
457
|
+
if (existingKey && !keysMatch) {
|
|
458
|
+
await keys.set({
|
|
459
|
+
session: {
|
|
460
|
+
[wireJid]: null
|
|
461
|
+
},
|
|
462
|
+
'identity-key': {
|
|
463
|
+
[wireJid]: identityKey
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
return true;
|
|
467
|
+
}
|
|
468
|
+
if (!existingKey) {
|
|
469
|
+
await keys.set({
|
|
470
|
+
'identity-key': {
|
|
471
|
+
[wireJid]: identityKey
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
return false;
|
|
477
|
+
},
|
|
478
|
+
loadPreKey: async id => {
|
|
479
|
+
const keyId = id.toString();
|
|
480
|
+
const {
|
|
481
|
+
[keyId]: key
|
|
482
|
+
} = await keys.get('pre-key', [keyId]);
|
|
483
|
+
if (key) {
|
|
484
|
+
return {
|
|
485
|
+
privKey: Buffer.from(key.private),
|
|
486
|
+
pubKey: Buffer.from(key.public)
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
},
|
|
490
|
+
removePreKey: id => keys.set({
|
|
491
|
+
'pre-key': {
|
|
492
|
+
[id]: null
|
|
493
|
+
}
|
|
494
|
+
}),
|
|
495
|
+
loadSignedPreKey: () => {
|
|
496
|
+
const key = creds.signedPreKey;
|
|
497
|
+
return {
|
|
498
|
+
privKey: Buffer.from(key.keyPair.private),
|
|
499
|
+
pubKey: Buffer.from(key.keyPair.public)
|
|
500
|
+
};
|
|
501
|
+
},
|
|
502
|
+
loadSenderKey: async senderKeyName => {
|
|
503
|
+
const keyId = senderKeyName.toString();
|
|
504
|
+
const {
|
|
505
|
+
[keyId]: key
|
|
506
|
+
} = await keys.get('sender-key', [keyId]);
|
|
507
|
+
if (key) {
|
|
508
|
+
return SenderKeyRecord.deserialize(key);
|
|
509
|
+
}
|
|
510
|
+
return new SenderKeyRecord();
|
|
511
|
+
},
|
|
512
|
+
storeSenderKey: async (senderKeyName, key) => {
|
|
513
|
+
const keyId = senderKeyName.toString();
|
|
514
|
+
const serialized = JSON.stringify(key.serialize());
|
|
515
|
+
await keys.set({
|
|
516
|
+
'sender-key': {
|
|
517
|
+
[keyId]: Buffer.from(serialized, 'utf-8')
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
},
|
|
521
|
+
getOurRegistrationId: () => creds.registrationId,
|
|
522
|
+
getOurIdentity: () => {
|
|
523
|
+
const {
|
|
524
|
+
signedIdentityKey
|
|
525
|
+
} = creds;
|
|
526
|
+
return {
|
|
527
|
+
privKey: Buffer.from(signedIdentityKey.private),
|
|
528
|
+
pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
};
|
|
430
532
|
}
|
|
431
|
-
//# sourceMappingURL=libsignal.js.map
|