jagproject 26.3.23 → 26.3.26

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.
Files changed (209) hide show
  1. package/WAProto/GenerateStatics.sh +3 -4
  2. package/WAProto/WAProto.proto +1215 -511
  3. package/WAProto/fix-imports.js +73 -0
  4. package/WAProto/index.d.ts +14017 -0
  5. package/WAProto/index.js +64857 -145167
  6. package/engine-requirements.js +4 -7
  7. package/lib/Defaults/index.d.ts +74 -0
  8. package/lib/Defaults/index.js +49 -35
  9. package/lib/Defaults/phonenumber-mcc.json +223 -0
  10. package/lib/Defaults/wileys-version.json +2 -2
  11. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  12. package/lib/Signal/Group/group-session-builder.d.ts +15 -0
  13. package/lib/Signal/Group/group-session-builder.js +5 -3
  14. package/lib/Signal/Group/group_cipher.d.ts +17 -0
  15. package/lib/Signal/Group/group_cipher.js +35 -46
  16. package/lib/Signal/Group/index.d.ts +12 -0
  17. package/lib/Signal/Group/index.js +21 -21
  18. package/lib/Signal/Group/keyhelper.d.ts +11 -0
  19. package/lib/Signal/Group/keyhelper.js +2 -2
  20. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  21. package/lib/Signal/Group/sender-chain-key.js +5 -10
  22. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  23. package/lib/Signal/Group/sender-key-distribution-message.js +7 -7
  24. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  25. package/lib/Signal/Group/sender-key-message.js +8 -8
  26. package/lib/Signal/Group/sender-key-name.d.ts +18 -0
  27. package/lib/Signal/Group/sender-key-record.d.ts +31 -0
  28. package/lib/Signal/Group/sender-key-record.js +7 -16
  29. package/lib/Signal/Group/sender-key-state.d.ts +39 -0
  30. package/lib/Signal/Group/sender-key-state.js +25 -37
  31. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  32. package/lib/Signal/Group/sender-message-key.js +2 -2
  33. package/lib/Signal/libsignal.d.ts +5 -0
  34. package/lib/Signal/libsignal.js +358 -54
  35. package/lib/Signal/lid-mapping.d.ts +19 -0
  36. package/lib/Signal/lid-mapping.js +274 -0
  37. package/lib/Socket/Client/index.d.ts +3 -0
  38. package/lib/Socket/Client/index.js +2 -2
  39. package/lib/Socket/Client/types.d.ts +16 -0
  40. package/lib/Socket/Client/types.js +1 -0
  41. package/lib/Socket/Client/websocket.d.ts +13 -0
  42. package/lib/Socket/Client/websocket.js +18 -30
  43. package/lib/Socket/business.d.ts +202 -0
  44. package/lib/Socket/business.js +160 -38
  45. package/lib/Socket/chats.d.ts +111 -0
  46. package/lib/Socket/chats.js +497 -314
  47. package/lib/Socket/communities.d.ts +258 -0
  48. package/lib/Socket/communities.js +438 -0
  49. package/lib/Socket/community.js +333 -0
  50. package/lib/Socket/groups.d.ts +150 -0
  51. package/lib/Socket/groups.js +229 -91
  52. package/lib/Socket/index.d.ts +245 -0
  53. package/lib/Socket/index.js +9 -6
  54. package/lib/Socket/messages-recv.d.ts +187 -0
  55. package/lib/Socket/messages-recv.js +1105 -501
  56. package/lib/Socket/messages-send.d.ts +183 -0
  57. package/lib/Socket/messages-send.js +1181 -501
  58. package/lib/Socket/mex.d.ts +3 -0
  59. package/lib/Socket/mex.js +45 -0
  60. package/lib/Socket/newsletter.d.ts +160 -0
  61. package/lib/Socket/newsletter.js +227 -200
  62. package/lib/Socket/socket.d.ts +55 -0
  63. package/lib/Socket/socket.js +507 -206
  64. package/lib/Socket/usync.js +6 -6
  65. package/lib/Store/index.js +17 -5
  66. package/lib/Store/make-cache-manager-store.js +83 -0
  67. package/lib/Store/make-in-memory-store.js +48 -89
  68. package/lib/Store/make-ordered-dictionary.js +1 -1
  69. package/lib/Types/Auth.d.ts +116 -0
  70. package/lib/Types/Bussines.d.ts +25 -0
  71. package/lib/Types/Bussines.js +2 -0
  72. package/lib/Types/Call.d.ts +15 -0
  73. package/lib/Types/Chat.d.ts +123 -0
  74. package/lib/Types/Chat.js +7 -1
  75. package/lib/Types/Contact.d.ts +24 -0
  76. package/lib/Types/Events.d.ts +237 -0
  77. package/lib/Types/Events.js +1 -0
  78. package/lib/Types/GroupMetadata.d.ts +67 -0
  79. package/lib/Types/Label.d.ts +47 -0
  80. package/lib/Types/Label.js +1 -3
  81. package/lib/Types/LabelAssociation.d.ts +30 -0
  82. package/lib/Types/LabelAssociation.js +1 -3
  83. package/lib/Types/Message.d.ts +305 -0
  84. package/lib/Types/Message.js +9 -5
  85. package/lib/Types/MexUpdates.js +11 -0
  86. package/lib/Types/Newsletter.d.ts +135 -0
  87. package/lib/Types/Newsletter.js +36 -11
  88. package/lib/Types/Product.d.ts +79 -0
  89. package/lib/Types/Signal.d.ts +76 -0
  90. package/lib/Types/Signal.js +1 -0
  91. package/lib/Types/Socket.d.ts +133 -0
  92. package/lib/Types/Socket.js +1 -0
  93. package/lib/Types/State.d.ts +39 -0
  94. package/lib/Types/State.js +12 -0
  95. package/lib/Types/USync.d.ts +26 -0
  96. package/lib/Types/USync.js +1 -0
  97. package/lib/Types/index.d.ts +65 -0
  98. package/lib/Types/index.js +14 -14
  99. package/lib/Utils/audioToBuffer.js +31 -0
  100. package/lib/Utils/auth-utils.d.ts +19 -0
  101. package/lib/Utils/auth-utils.js +222 -123
  102. package/lib/Utils/baileys-event-stream.js +60 -0
  103. package/lib/Utils/bridge-runtime.d.ts +1 -0
  104. package/lib/Utils/bridge-runtime.js +14 -0
  105. package/lib/Utils/browser-utils.d.ts +4 -0
  106. package/lib/Utils/browser-utils.js +38 -29
  107. package/lib/Utils/business.d.ts +23 -0
  108. package/lib/Utils/business.js +54 -48
  109. package/lib/Utils/chat-utils.d.ts +70 -0
  110. package/lib/Utils/chat-utils.js +284 -189
  111. package/lib/Utils/crypto.d.ts +37 -0
  112. package/lib/Utils/crypto.js +16 -41
  113. package/lib/Utils/decode-wa-message.d.ts +48 -0
  114. package/lib/Utils/decode-wa-message.js +128 -48
  115. package/lib/Utils/event-buffer.d.ts +34 -0
  116. package/lib/Utils/event-buffer.js +124 -62
  117. package/lib/Utils/generics.d.ts +91 -0
  118. package/lib/Utils/generics.js +154 -138
  119. package/lib/Utils/history.d.ts +22 -0
  120. package/lib/Utils/history.js +77 -34
  121. package/lib/Utils/identity-change-handler.d.ts +37 -0
  122. package/lib/Utils/identity-change-handler.js +54 -0
  123. package/lib/Utils/index.d.ts +22 -0
  124. package/lib/Utils/index.js +32 -19
  125. package/lib/Utils/link-preview.d.ts +21 -0
  126. package/lib/Utils/link-preview.js +12 -17
  127. package/lib/Utils/logger.d.ts +13 -0
  128. package/lib/Utils/lt-hash.d.ts +8 -0
  129. package/lib/Utils/lt-hash.js +2 -43
  130. package/lib/Utils/make-mutex.d.ts +9 -0
  131. package/lib/Utils/make-mutex.js +21 -27
  132. package/lib/Utils/message-retry-manager.d.ts +110 -0
  133. package/lib/Utils/message-retry-manager.js +143 -45
  134. package/lib/Utils/messages-media.d.ts +130 -0
  135. package/lib/Utils/messages-media.js +429 -502
  136. package/lib/Utils/messages-newsletter.d.ts +84 -0
  137. package/lib/Utils/messages-newsletter.js +295 -0
  138. package/lib/Utils/messages.d.ts +92 -0
  139. package/lib/Utils/messages.js +1025 -674
  140. package/lib/Utils/noise-handler.d.ts +20 -0
  141. package/lib/Utils/noise-handler.js +145 -91
  142. package/lib/Utils/pre-key-manager.d.ts +28 -0
  143. package/lib/Utils/pre-key-manager.js +112 -0
  144. package/lib/Utils/process-message.d.ts +60 -0
  145. package/lib/Utils/process-message.js +316 -184
  146. package/lib/Utils/reporting-utils.d.ts +11 -0
  147. package/lib/Utils/reporting-utils.js +262 -0
  148. package/lib/Utils/resolve-jid.d.ts +43 -0
  149. package/lib/Utils/resolve-jid.js +95 -0
  150. package/lib/Utils/rust-bridge-shim.d.ts +22 -0
  151. package/lib/Utils/rust-bridge-shim.js +70 -0
  152. package/lib/Utils/serial-task-queue.js +29 -0
  153. package/lib/Utils/signal.d.ts +34 -0
  154. package/lib/Utils/signal.js +56 -39
  155. package/lib/Utils/streamToBuffer.js +17 -0
  156. package/lib/Utils/sync-action-utils.d.ts +19 -0
  157. package/lib/Utils/sync-action-utils.js +52 -0
  158. package/lib/Utils/tc-token-utils.d.ts +12 -0
  159. package/lib/Utils/tc-token-utils.js +20 -0
  160. package/lib/Utils/use-mongo-file-auth-state.js +71 -0
  161. package/lib/Utils/use-multi-file-auth-state.d.ts +13 -0
  162. package/lib/Utils/use-multi-file-auth-state.js +11 -12
  163. package/lib/Utils/use-single-file-auth-state.js +73 -0
  164. package/lib/Utils/validate-connection.d.ts +11 -0
  165. package/lib/Utils/validate-connection.js +59 -82
  166. package/lib/Utils/wileys-event-stream.js +1 -61
  167. package/lib/WABinary/constants.d.ts +28 -0
  168. package/lib/WABinary/decode.d.ts +7 -0
  169. package/lib/WABinary/decode.js +39 -4
  170. package/lib/WABinary/encode.d.ts +3 -0
  171. package/lib/WABinary/encode.js +17 -11
  172. package/lib/WABinary/generic-utils.d.ts +15 -0
  173. package/lib/WABinary/generic-utils.js +46 -18
  174. package/lib/WABinary/index.d.ts +6 -0
  175. package/lib/WABinary/index.js +9 -5
  176. package/lib/WABinary/jid-utils.d.ts +48 -0
  177. package/lib/WABinary/jid-utils.js +67 -37
  178. package/lib/WABinary/types.d.ts +19 -0
  179. package/lib/WABinary/types.js +34 -0
  180. package/lib/WAM/BinaryInfo.d.ts +9 -0
  181. package/lib/WAM/constants.d.ts +40 -0
  182. package/lib/WAM/constants.js +19183 -11678
  183. package/lib/WAM/encode.d.ts +3 -0
  184. package/lib/WAM/encode.js +15 -17
  185. package/lib/WAM/index.d.ts +4 -0
  186. package/lib/WAM/index.js +3 -3
  187. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +10 -0
  188. package/lib/WAUSync/Protocols/USyncContactProtocol.js +6 -6
  189. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  190. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +9 -9
  191. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  192. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +6 -6
  193. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  194. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +7 -8
  195. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +26 -0
  196. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +18 -17
  197. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +10 -0
  198. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +11 -3
  199. package/lib/WAUSync/Protocols/index.d.ts +5 -0
  200. package/lib/WAUSync/Protocols/index.js +6 -4
  201. package/lib/WAUSync/USyncQuery.d.ts +29 -0
  202. package/lib/WAUSync/USyncQuery.js +38 -30
  203. package/lib/WAUSync/USyncUser.d.ts +13 -0
  204. package/lib/WAUSync/index.d.ts +4 -0
  205. package/lib/WAUSync/index.js +3 -3
  206. package/lib/index.d.ts +12 -0
  207. package/lib/index.js +3 -5
  208. package/package.json +7 -4
  209. package/LICENSE +0 -21
@@ -34,102 +34,406 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.makeLibSignalRepository = makeLibSignalRepository;
37
+ // @ts-ignore
37
38
  const libsignal = __importStar(require("libsignal"));
38
- const Utils_1 = require("../Utils");
39
- const WABinary_1 = require("../WABinary");
40
- const sender_key_name_1 = require("./Group/sender-key-name");
41
- const sender_key_record_1 = require("./Group/sender-key-record");
42
- const Group_1 = require("./Group");
43
- function makeLibSignalRepository(auth) {
44
- const storage = signalStorage(auth);
45
- return {
39
+ // @ts-ignore
40
+ const protobufs_js_1 = require("libsignal/src/protobufs.js");
41
+ const lru_cache_1 = require("lru-cache");
42
+ // ── Suppress libsignal "Closing session: SessionEntry {...}" noise ────────────
43
+ // ES module imports di-hoist, jadi suppressor harus dipasang setelah semua import.
44
+ // Cara paling efektif: patch console + process.stdout setelah module loaded.
45
+ ;
46
+ (function _suppressSessionNoise() {
47
+ const _isNoise = (...args) => {
48
+ if (typeof args[0] === 'string') {
49
+ const s = args[0];
50
+ if (s.startsWith('Closing session') || s.includes('SessionEntry') ||
51
+ s.includes('pendingPreKey') || s.includes('currentRatchet') ||
52
+ s.includes('registrationId:') || s.includes('indexInfo:'))
53
+ return true;
54
+ }
55
+ if (args[1] && typeof args[1] === 'object' &&
56
+ args[1]?.constructor?.name === 'SessionEntry')
57
+ return true;
58
+ if (typeof args[0] === 'object' &&
59
+ args[0]?.constructor?.name === 'SessionEntry')
60
+ return true;
61
+ return false;
62
+ };
63
+ const _wrapFn = (orig) => (...args) => { if (_isNoise(...args))
64
+ return; orig(...args); };
65
+ console.log = _wrapFn(console.log.bind(console));
66
+ console.warn = _wrapFn(console.warn.bind(console));
67
+ console.info = _wrapFn(console.info.bind(console));
68
+ // Intercept process.stdout.write untuk kasus libsignal bypass console
69
+ const _origWrite = process.stdout.write.bind(process.stdout);
70
+ process.stdout.write = function (chunk, ...rest) {
71
+ if (typeof chunk === 'string' && (chunk.includes('Closing session') || chunk.includes('SessionEntry') ||
72
+ chunk.includes('pendingPreKey') || chunk.includes('currentRatchet')))
73
+ return true;
74
+ return _origWrite(chunk, ...rest);
75
+ };
76
+ })();
77
+ // ─────────────────────────────────────────────────────────────────────────────
78
+ const index_js_1 = require("../Utils/index.js");
79
+ const index_js_2 = require("../WABinary/index.js");
80
+ const sender_key_name_js_1 = require("./Group/sender-key-name.js");
81
+ const sender_key_record_js_1 = require("./Group/sender-key-record.js");
82
+ const index_js_3 = require("./Group/index.js");
83
+ const lid_mapping_js_1 = require("./lid-mapping.js");
84
+ /** Extract identity key from PreKeyWhisperMessage for identity change detection */
85
+ function extractIdentityFromPkmsg(ciphertext) {
86
+ try {
87
+ if (!ciphertext || ciphertext.length < 2) {
88
+ return undefined;
89
+ }
90
+ // Version byte check (version 3)
91
+ const version = ciphertext[0];
92
+ if ((version & 0xf) !== 3) {
93
+ return undefined;
94
+ }
95
+ // Parse protobuf (skip version byte)
96
+ const preKeyProto = protobufs_js_1.PreKeyWhisperMessage.decode(ciphertext.slice(1));
97
+ if (preKeyProto.identityKey?.length === 33) {
98
+ return new Uint8Array(preKeyProto.identityKey);
99
+ }
100
+ return undefined;
101
+ }
102
+ catch {
103
+ return undefined;
104
+ }
105
+ }
106
+ function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
107
+ const lidMapping = new lid_mapping_js_1.LIDMappingStore(auth.keys, logger, pnToLIDFunc);
108
+ const storage = signalStorage(auth, lidMapping);
109
+ const parsedKeys = auth.keys;
110
+ const migratedSessionCache = new lru_cache_1.LRUCache({
111
+ ttl: 3 * 24 * 60 * 60 * 1000, // 7 days
112
+ ttlAutopurge: true,
113
+ updateAgeOnGet: true
114
+ });
115
+ const repository = {
46
116
  decryptGroupMessage({ group, authorJid, msg }) {
47
117
  const senderName = jidToSignalSenderKeyName(group, authorJid);
48
- const cipher = new Group_1.GroupCipher(storage, senderName);
49
- return cipher.decrypt(msg);
118
+ const cipher = new index_js_3.GroupCipher(storage, senderName);
119
+ // Use transaction to ensure atomicity
120
+ return parsedKeys.transaction(async () => {
121
+ return cipher.decrypt(msg);
122
+ }, group);
50
123
  },
51
124
  async processSenderKeyDistributionMessage({ item, authorJid }) {
52
- const builder = new Group_1.GroupSessionBuilder(storage);
125
+ const builder = new index_js_3.GroupSessionBuilder(storage);
53
126
  if (!item.groupId) {
54
127
  throw new Error('Group ID is required for sender key distribution message');
55
128
  }
56
129
  const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
57
- const senderMsg = new Group_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
130
+ const senderMsg = new index_js_3.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
58
131
  const senderNameStr = senderName.toString();
59
132
  const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
60
133
  if (!senderKey) {
61
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
134
+ await storage.storeSenderKey(senderName, new sender_key_record_js_1.SenderKeyRecord());
62
135
  }
63
- await builder.process(senderName, senderMsg);
136
+ return parsedKeys.transaction(async () => {
137
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
138
+ if (!senderKey) {
139
+ await storage.storeSenderKey(senderName, new sender_key_record_js_1.SenderKeyRecord());
140
+ }
141
+ await builder.process(senderName, senderMsg);
142
+ }, item.groupId);
64
143
  },
65
144
  async decryptMessage({ jid, type, ciphertext }) {
66
145
  const addr = jidToSignalProtocolAddress(jid);
67
146
  const session = new libsignal.SessionCipher(storage, addr);
68
- let result;
69
- switch (type) {
70
- case 'pkmsg':
71
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
72
- break;
73
- case 'msg':
74
- result = await session.decryptWhisperMessage(ciphertext);
75
- break;
76
- default:
77
- throw new Error(`Unknown message type: ${type}`);
78
- }
79
- return result;
147
+ // Extract and save sender's identity key before decryption for identity change detection
148
+ if (type === 'pkmsg') {
149
+ const identityKey = extractIdentityFromPkmsg(ciphertext);
150
+ if (identityKey) {
151
+ const addrStr = addr.toString();
152
+ const identityChanged = await storage.saveIdentity(addrStr, identityKey);
153
+ if (identityChanged) {
154
+ logger.info({ jid, addr: addrStr }, 'identity key changed or new contact, session will be re-established');
155
+ }
156
+ }
157
+ }
158
+ async function doDecrypt() {
159
+ let result;
160
+ try {
161
+ switch (type) {
162
+ case 'pkmsg':
163
+ result = await session.decryptPreKeyWhisperMessage(ciphertext);
164
+ break;
165
+ case 'msg':
166
+ result = await session.decryptWhisperMessage(ciphertext);
167
+ break;
168
+ }
169
+ }
170
+ finally {
171
+ }
172
+ return result;
173
+ }
174
+ // If it's not a sync message, we need to ensure atomicity
175
+ // For regular messages, we use a transaction to ensure atomicity
176
+ return parsedKeys.transaction(async () => {
177
+ return await doDecrypt();
178
+ }, jid);
80
179
  },
81
180
  async encryptMessage({ jid, data }) {
82
181
  const addr = jidToSignalProtocolAddress(jid);
83
182
  const cipher = new libsignal.SessionCipher(storage, addr);
84
- const { type: sigType, body } = await cipher.encrypt(data);
85
- const type = sigType === 3 ? 'pkmsg' : 'msg';
86
- return { type, ciphertext: Buffer.from(body, 'binary') };
183
+ // Use transaction to ensure atomicity
184
+ return parsedKeys.transaction(async () => {
185
+ const { type: sigType, body } = await cipher.encrypt(data);
186
+ const type = sigType === 3 ? 'pkmsg' : 'msg';
187
+ return { type, ciphertext: Buffer.from(body, 'binary') };
188
+ }, jid);
87
189
  },
88
190
  async encryptGroupMessage({ group, meId, data }) {
89
191
  const senderName = jidToSignalSenderKeyName(group, meId);
90
- const builder = new Group_1.GroupSessionBuilder(storage);
192
+ const builder = new index_js_3.GroupSessionBuilder(storage);
91
193
  const senderNameStr = senderName.toString();
92
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
93
- if (!senderKey) {
94
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
95
- }
96
- const senderKeyDistributionMessage = await builder.create(senderName);
97
- const session = new Group_1.GroupCipher(storage, senderName);
98
- const ciphertext = await session.encrypt(data);
99
- return {
100
- ciphertext,
101
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
102
- };
194
+ return parsedKeys.transaction(async () => {
195
+ const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
196
+ if (!senderKey) {
197
+ await storage.storeSenderKey(senderName, new sender_key_record_js_1.SenderKeyRecord());
198
+ }
199
+ const senderKeyDistributionMessage = await builder.create(senderName);
200
+ const session = new index_js_3.GroupCipher(storage, senderName);
201
+ const ciphertext = await session.encrypt(data);
202
+ return {
203
+ ciphertext,
204
+ senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
205
+ };
206
+ }, group);
103
207
  },
104
208
  async injectE2ESession({ jid, session }) {
209
+ logger.trace({ jid }, 'injecting E2EE session');
105
210
  const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
106
- await cipher.initOutgoing(session);
211
+ return parsedKeys.transaction(async () => {
212
+ await cipher.initOutgoing(session);
213
+ }, jid);
107
214
  },
108
215
  jidToSignalProtocolAddress(jid) {
109
216
  return jidToSignalProtocolAddress(jid).toString();
217
+ },
218
+ // Optimized direct access to LID mapping store
219
+ lidMapping,
220
+ async validateSession(jid) {
221
+ try {
222
+ const addr = jidToSignalProtocolAddress(jid);
223
+ const session = await storage.loadSession(addr.toString());
224
+ if (!session) {
225
+ return { exists: false, reason: 'no session' };
226
+ }
227
+ if (!session.haveOpenSession()) {
228
+ return { exists: false, reason: 'no open session' };
229
+ }
230
+ return { exists: true };
231
+ }
232
+ catch (error) {
233
+ return { exists: false, reason: 'validation error' };
234
+ }
235
+ },
236
+ async deleteSession(jids) {
237
+ if (!jids.length)
238
+ return;
239
+ // Convert JIDs to signal addresses and prepare for bulk deletion
240
+ const sessionUpdates = {};
241
+ jids.forEach(jid => {
242
+ const addr = jidToSignalProtocolAddress(jid);
243
+ sessionUpdates[addr.toString()] = null;
244
+ });
245
+ // Single transaction for all deletions
246
+ return parsedKeys.transaction(async () => {
247
+ await auth.keys.set({ session: sessionUpdates });
248
+ }, `delete-${jids.length}-sessions`);
249
+ },
250
+ async migrateSession(fromJid, toJid) {
251
+ // TODO: use usync to handle this entire mess
252
+ if (!fromJid || (!(0, index_js_2.isLidUser)(toJid) && !(0, index_js_2.isHostedLidUser)(toJid)))
253
+ return { migrated: 0, skipped: 0, total: 0 };
254
+ // Only support PN to LID migration
255
+ if (!(0, index_js_2.isPnUser)(fromJid) && !(0, index_js_2.isHostedPnUser)(fromJid)) {
256
+ return { migrated: 0, skipped: 0, total: 1 };
257
+ }
258
+ const { user } = (0, index_js_2.jidDecode)(fromJid);
259
+ logger.debug({ fromJid }, 'bulk device migration - loading all user devices');
260
+ // Get user's device list from storage
261
+ const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
262
+ if (!userDevices) {
263
+ return { migrated: 0, skipped: 0, total: 0 };
264
+ }
265
+ const { device: fromDevice } = (0, index_js_2.jidDecode)(fromJid);
266
+ const fromDeviceStr = fromDevice?.toString() || '0';
267
+ if (!userDevices.includes(fromDeviceStr)) {
268
+ userDevices.push(fromDeviceStr);
269
+ }
270
+ // Filter out cached devices before database fetch
271
+ const uncachedDevices = userDevices.filter(device => {
272
+ const deviceKey = `${user}.${device}`;
273
+ return !migratedSessionCache.has(deviceKey);
274
+ });
275
+ // Bulk check session existence only for uncached devices
276
+ const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
277
+ const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
278
+ // Step 3: Convert existing sessions to JIDs (only migrate sessions that exist)
279
+ const deviceJids = [];
280
+ for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
281
+ if (sessionData) {
282
+ // Session exists in storage
283
+ const deviceStr = sessionKey.split('.')[1];
284
+ if (!deviceStr)
285
+ continue;
286
+ const deviceNum = parseInt(deviceStr);
287
+ let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
288
+ if (deviceNum === 99) {
289
+ jid = `${user}:99@hosted`;
290
+ }
291
+ deviceJids.push(jid);
292
+ }
293
+ }
294
+ logger.debug({
295
+ fromJid,
296
+ totalDevices: userDevices.length,
297
+ devicesWithSessions: deviceJids.length,
298
+ devices: deviceJids
299
+ }, 'bulk device migration complete - all user devices processed');
300
+ // Single transaction for all migrations
301
+ return parsedKeys.transaction(async () => {
302
+ const migrationOps = deviceJids.map(jid => {
303
+ const lidWithDevice = (0, index_js_2.transferDevice)(jid, toJid);
304
+ const fromDecoded = (0, index_js_2.jidDecode)(jid);
305
+ const toDecoded = (0, index_js_2.jidDecode)(lidWithDevice);
306
+ return {
307
+ fromJid: jid,
308
+ toJid: lidWithDevice,
309
+ pnUser: fromDecoded.user,
310
+ lidUser: toDecoded.user,
311
+ deviceId: fromDecoded.device || 0,
312
+ fromAddr: jidToSignalProtocolAddress(jid),
313
+ toAddr: jidToSignalProtocolAddress(lidWithDevice)
314
+ };
315
+ });
316
+ const totalOps = migrationOps.length;
317
+ let migratedCount = 0;
318
+ // Bulk fetch PN sessions - already exist (verified during device discovery)
319
+ const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
320
+ const pnSessions = await parsedKeys.get('session', pnAddrStrings);
321
+ // Prepare bulk session updates (PN → LID migration + deletion)
322
+ const sessionUpdates = {};
323
+ for (const op of migrationOps) {
324
+ const pnAddrStr = op.fromAddr.toString();
325
+ const lidAddrStr = op.toAddr.toString();
326
+ const pnSession = pnSessions[pnAddrStr];
327
+ if (pnSession) {
328
+ // Session exists (guaranteed from device discovery)
329
+ const fromSession = libsignal.SessionRecord.deserialize(pnSession);
330
+ if (fromSession.haveOpenSession()) {
331
+ // Queue for bulk update: copy to LID, delete from PN
332
+ sessionUpdates[lidAddrStr] = fromSession.serialize();
333
+ sessionUpdates[pnAddrStr] = null;
334
+ migratedCount++;
335
+ }
336
+ }
337
+ }
338
+ // Single bulk session update for all migrations
339
+ if (Object.keys(sessionUpdates).length > 0) {
340
+ await parsedKeys.set({ session: sessionUpdates });
341
+ logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
342
+ // Cache device-level migrations
343
+ for (const op of migrationOps) {
344
+ if (sessionUpdates[op.toAddr.toString()]) {
345
+ const deviceKey = `${op.pnUser}.${op.deviceId}`;
346
+ migratedSessionCache.set(deviceKey, true);
347
+ }
348
+ }
349
+ }
350
+ const skippedCount = totalOps - migratedCount;
351
+ return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
352
+ }, `migrate-${deviceJids.length}-sessions-${(0, index_js_2.jidDecode)(toJid)?.user}`);
110
353
  }
111
354
  };
355
+ return repository;
112
356
  }
113
357
  const jidToSignalProtocolAddress = (jid) => {
114
- const { user, device } = (0, WABinary_1.jidDecode)(jid);
115
- return new libsignal.ProtocolAddress(user, device || 0);
358
+ const decoded = (0, index_js_2.jidDecode)(jid);
359
+ const { user, device, server, domainType } = decoded;
360
+ if (!user) {
361
+ throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
362
+ }
363
+ const signalUser = domainType !== index_js_2.WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
364
+ const finalDevice = device || 0;
365
+ if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
366
+ throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
367
+ }
368
+ return new libsignal.ProtocolAddress(signalUser, finalDevice);
116
369
  };
117
370
  const jidToSignalSenderKeyName = (group, user) => {
118
- return new sender_key_name_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
371
+ return new sender_key_name_js_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
119
372
  };
120
- function signalStorage({ creds, keys }) {
373
+ function signalStorage({ creds, keys }, lidMapping) {
374
+ // Shared function to resolve PN signal address to LID if mapping exists
375
+ const resolveLIDSignalAddress = async (id) => {
376
+ if (id.includes('.')) {
377
+ const [deviceId, device] = id.split('.');
378
+ const [user, domainType_] = deviceId.split('_');
379
+ const domainType = parseInt(domainType_ || '0');
380
+ if (domainType === index_js_2.WAJIDDomains.LID || domainType === index_js_2.WAJIDDomains.HOSTED_LID)
381
+ return id;
382
+ const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === index_js_2.WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
383
+ const lidForPN = await lidMapping.getLIDForPN(pnJid);
384
+ if (lidForPN) {
385
+ const lidAddr = jidToSignalProtocolAddress(lidForPN);
386
+ return lidAddr.toString();
387
+ }
388
+ }
389
+ return id;
390
+ };
121
391
  return {
122
392
  loadSession: async (id) => {
123
- const { [id]: sess } = await keys.get('session', [id]);
124
- if (sess) {
125
- return libsignal.SessionRecord.deserialize(sess);
393
+ try {
394
+ const wireJid = await resolveLIDSignalAddress(id);
395
+ const { [wireJid]: sess } = await keys.get('session', [wireJid]);
396
+ if (sess) {
397
+ return libsignal.SessionRecord.deserialize(sess);
398
+ }
399
+ }
400
+ catch (e) {
401
+ return null;
126
402
  }
403
+ return null;
127
404
  },
128
405
  storeSession: async (id, session) => {
129
- await keys.set({ session: { [id]: session.serialize() } });
406
+ const wireJid = await resolveLIDSignalAddress(id);
407
+ await keys.set({ session: { [wireJid]: session.serialize() } });
130
408
  },
131
409
  isTrustedIdentity: () => {
132
- return true;
410
+ return true; // TOFU - Trust on First Use (same as WhatsApp Web)
411
+ },
412
+ loadIdentityKey: async (id) => {
413
+ const wireJid = await resolveLIDSignalAddress(id);
414
+ const { [wireJid]: key } = await keys.get('identity-key', [wireJid]);
415
+ return key || undefined;
416
+ },
417
+ saveIdentity: async (id, identityKey) => {
418
+ const wireJid = await resolveLIDSignalAddress(id);
419
+ const { [wireJid]: existingKey } = await keys.get('identity-key', [wireJid]);
420
+ const keysMatch = existingKey &&
421
+ existingKey.length === identityKey.length &&
422
+ existingKey.every((byte, i) => byte === identityKey[i]);
423
+ if (existingKey && !keysMatch) {
424
+ // Identity changed - clear session and update key
425
+ await keys.set({
426
+ session: { [wireJid]: null },
427
+ 'identity-key': { [wireJid]: identityKey }
428
+ });
429
+ return true;
430
+ }
431
+ if (!existingKey) {
432
+ // New contact - Trust on First Use (TOFU)
433
+ await keys.set({ 'identity-key': { [wireJid]: identityKey } });
434
+ return true;
435
+ }
436
+ return false;
133
437
  },
134
438
  loadPreKey: async (id) => {
135
439
  const keyId = id.toString();
@@ -153,9 +457,9 @@ function signalStorage({ creds, keys }) {
153
457
  const keyId = senderKeyName.toString();
154
458
  const { [keyId]: key } = await keys.get('sender-key', [keyId]);
155
459
  if (key) {
156
- return sender_key_record_1.SenderKeyRecord.deserialize(key);
460
+ return sender_key_record_js_1.SenderKeyRecord.deserialize(key);
157
461
  }
158
- return new sender_key_record_1.SenderKeyRecord();
462
+ return new sender_key_record_js_1.SenderKeyRecord();
159
463
  },
160
464
  storeSenderKey: async (senderKeyName, key) => {
161
465
  const keyId = senderKeyName.toString();
@@ -167,7 +471,7 @@ function signalStorage({ creds, keys }) {
167
471
  const { signedIdentityKey } = creds;
168
472
  return {
169
473
  privKey: Buffer.from(signedIdentityKey.private),
170
- pubKey: (0, Utils_1.generateSignalPubKey)(signedIdentityKey.public)
474
+ pubKey: Buffer.from((0, index_js_1.generateSignalPubKey)(signedIdentityKey.public))
171
475
  };
172
476
  }
173
477
  };
@@ -0,0 +1,19 @@
1
+ import type { LIDMapping, SignalKeyStoreWithTransaction } from '../Types/index.js';
2
+ import type { ILogger } from '../Utils/logger.js';
3
+ export declare class LIDMappingStore {
4
+ private readonly mappingCache;
5
+ private readonly keys;
6
+ private readonly logger;
7
+ private pnToLIDFunc?;
8
+ private readonly inflightLIDLookups;
9
+ private readonly inflightPNLookups;
10
+ constructor(keys: SignalKeyStoreWithTransaction, logger: ILogger, pnToLIDFunc?: (jids: string[]) => Promise<LIDMapping[] | undefined>);
11
+ storeLIDPNMappings(pairs: LIDMapping[]): Promise<void>;
12
+ getLIDForPN(pn: string): Promise<string | null>;
13
+ getLIDsForPNs(pns: string[]): Promise<LIDMapping[] | null>;
14
+ private _getLIDsForPNsImpl;
15
+ getPNForLID(lid: string): Promise<string | null>;
16
+ getPNsForLIDs(lids: string[]): Promise<LIDMapping[] | null>;
17
+ private _getPNsForLIDsImpl;
18
+ }
19
+ //# sourceMappingURL=lid-mapping.d.ts.map