kage-baileys 25.3.12 → 25.3.13

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 (259) hide show
  1. package/lib/Defaults/connection.js +49 -108
  2. package/lib/Defaults/constants.js +71 -153
  3. package/lib/Defaults/index.js +0 -199
  4. package/lib/Defaults/media.js +0 -60
  5. package/lib/Defaults/prefix.js +17 -43
  6. package/lib/Modded/message_builder.js +0 -2355
  7. package/lib/Signal/Group/ciphertext-message.js +0 -54
  8. package/lib/Signal/Group/group-session-builder.js +0 -124
  9. package/lib/Signal/Group/group_cipher.js +0 -230
  10. package/lib/Signal/Group/index.js +0 -124
  11. package/lib/Signal/Group/keyhelper.js +0 -108
  12. package/lib/Signal/Group/sender-chain-key.js +0 -88
  13. package/lib/Signal/Group/sender-key-distribution-message.js +0 -157
  14. package/lib/Signal/Group/sender-key-message.js +0 -166
  15. package/lib/Signal/Group/sender-key-name.js +0 -130
  16. package/lib/Signal/Group/sender-key-record.js +0 -122
  17. package/lib/Signal/Group/sender-key-state.js +0 -220
  18. package/lib/Signal/Group/sender-message-key.js +0 -82
  19. package/lib/Signal/libsignal.js +0 -646
  20. package/lib/Signal/lid-mapping.js +0 -537
  21. package/lib/Socket/Client/index.js +0 -60
  22. package/lib/Socket/Client/types.js +0 -51
  23. package/lib/Socket/Client/websocket.js +0 -164
  24. package/lib/Socket/business.js +0 -708
  25. package/lib/Socket/chats.js +0 -2253
  26. package/lib/Socket/communities.js +0 -441
  27. package/lib/Socket/community.js +504 -1019
  28. package/lib/Socket/groups.js +0 -727
  29. package/lib/Socket/messages-recv.js +0 -2780
  30. package/lib/Socket/messages-send.js +0 -2640
  31. package/lib/Socket/mex.js +0 -128
  32. package/lib/Socket/newsletter.js +0 -486
  33. package/lib/Socket/socket.js +0 -1899
  34. package/lib/Store/index.js +0 -68
  35. package/lib/Store/make-cache-manager-store.js +0 -97
  36. package/lib/Store/make-in-memory-store.js +0 -908
  37. package/lib/Store/make-ordered-dictionary.js +0 -187
  38. package/lib/Store/object-repository.js +0 -78
  39. package/lib/Types/Bussines.js +2 -2
  40. package/lib/Types/Chat.js +0 -33
  41. package/lib/Types/Label.js +0 -78
  42. package/lib/Types/LabelAssociation.js +0 -42
  43. package/lib/Types/Message.js +0 -46
  44. package/lib/Types/Mex.js +0 -53
  45. package/lib/Types/MexUpdates.js +16 -43
  46. package/lib/Types/Newsletter.js +0 -27
  47. package/lib/Types/State.js +0 -42
  48. package/lib/Types/index.js +0 -134
  49. package/lib/Utils/auth-utils.js +0 -579
  50. package/lib/Utils/baileys-event-stream.js +65 -142
  51. package/lib/Utils/browser-utils.js +0 -104
  52. package/lib/Utils/business.js +0 -519
  53. package/lib/Utils/chat-utils.js +0 -1702
  54. package/lib/Utils/companion-reg-client-utils.js +0 -45
  55. package/lib/Utils/crypto.js +0 -382
  56. package/lib/Utils/decode-wa-message.js +0 -605
  57. package/lib/Utils/event-buffer.js +0 -1207
  58. package/lib/Utils/generics.js +0 -1020
  59. package/lib/Utils/history.js +0 -231
  60. package/lib/Utils/identity-change-handler.js +0 -59
  61. package/lib/Utils/index.js +0 -133
  62. package/lib/Utils/link-preview.js +0 -228
  63. package/lib/Utils/logger.js +0 -35
  64. package/lib/Utils/lt-hash.js +0 -60
  65. package/lib/Utils/make-mutex.js +0 -110
  66. package/lib/Utils/message-retry-manager.js +0 -429
  67. package/lib/Utils/messages-media.js +0 -1899
  68. package/lib/Utils/messages.js +0 -2950
  69. package/lib/Utils/noise-handler.js +0 -369
  70. package/lib/Utils/offline-node-processor.js +0 -50
  71. package/lib/Utils/pre-key-manager.js +0 -251
  72. package/lib/Utils/process-message.js +0 -1239
  73. package/lib/Utils/reporting-utils.js +0 -268
  74. package/lib/Utils/signal.js +0 -377
  75. package/lib/Utils/stanza-ack.js +0 -48
  76. package/lib/Utils/sync-action-utils.js +0 -58
  77. package/lib/Utils/tc-token-utils.js +0 -28
  78. package/lib/Utils/validate-connection.js +0 -508
  79. package/lib/VoIP/audio-feeder.js +0 -146
  80. package/lib/VoIP/index.js +0 -292
  81. package/lib/VoIP/relay-transport.js +0 -569
  82. package/lib/VoIP/signaling.js +0 -613
  83. package/lib/VoIP/types.js +0 -27
  84. package/lib/VoIP/wasm-engine.js +0 -1222
  85. package/lib/VoIP/worker-bootstrap.js +0 -1785
  86. package/lib/WABinary/constants.js +0 -2639
  87. package/lib/WABinary/decode.js +0 -612
  88. package/lib/WABinary/encode.js +0 -556
  89. package/lib/WABinary/generic-utils.js +0 -272
  90. package/lib/WABinary/index.js +0 -82
  91. package/lib/WABinary/jid-utils.js +0 -262
  92. package/lib/WAM/BinaryInfo.js +0 -50
  93. package/lib/WAM/constants.js +0 -38242
  94. package/lib/WAM/encode.js +0 -396
  95. package/lib/WAM/index.js +0 -78
  96. package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +47 -109
  97. package/lib/WAUSync/Protocols/USyncContactProtocol.js +0 -88
  98. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +0 -142
  99. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +0 -85
  100. package/lib/WAUSync/Protocols/USyncLIDProtocol.js +30 -63
  101. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +0 -114
  102. package/lib/WAUSync/Protocols/index.js +0 -82
  103. package/lib/WAUSync/index.js +0 -78
  104. package/lib/assets/wasm/loader.js +0 -13
  105. package/lib/assets/wasm/worker-modules.js +0 -283
  106. package/lib/index.js +0 -93
  107. package/package.json +2 -3
  108. package/WAProto/index.d.ts +0 -46
  109. package/lib/Defaults/index.d.ts.map +0 -1
  110. package/lib/Defaults/index.js.map +0 -1
  111. package/lib/Signal/Group/ciphertext-message.d.ts.map +0 -1
  112. package/lib/Signal/Group/ciphertext-message.js.map +0 -1
  113. package/lib/Signal/Group/group-session-builder.d.ts.map +0 -1
  114. package/lib/Signal/Group/group-session-builder.js.map +0 -1
  115. package/lib/Signal/Group/group_cipher.d.ts.map +0 -1
  116. package/lib/Signal/Group/group_cipher.js.map +0 -1
  117. package/lib/Signal/Group/index.d.ts.map +0 -1
  118. package/lib/Signal/Group/index.js.map +0 -1
  119. package/lib/Signal/Group/keyhelper.d.ts.map +0 -1
  120. package/lib/Signal/Group/keyhelper.js.map +0 -1
  121. package/lib/Signal/Group/sender-chain-key.d.ts.map +0 -1
  122. package/lib/Signal/Group/sender-chain-key.js.map +0 -1
  123. package/lib/Signal/Group/sender-key-distribution-message.d.ts.map +0 -1
  124. package/lib/Signal/Group/sender-key-distribution-message.js.map +0 -1
  125. package/lib/Signal/Group/sender-key-message.d.ts.map +0 -1
  126. package/lib/Signal/Group/sender-key-message.js.map +0 -1
  127. package/lib/Signal/Group/sender-key-name.d.ts.map +0 -1
  128. package/lib/Signal/Group/sender-key-name.js.map +0 -1
  129. package/lib/Signal/Group/sender-key-record.d.ts.map +0 -1
  130. package/lib/Signal/Group/sender-key-record.js.map +0 -1
  131. package/lib/Signal/Group/sender-key-state.d.ts.map +0 -1
  132. package/lib/Signal/Group/sender-key-state.js.map +0 -1
  133. package/lib/Signal/Group/sender-message-key.d.ts.map +0 -1
  134. package/lib/Signal/Group/sender-message-key.js.map +0 -1
  135. package/lib/Signal/libsignal.d.ts.map +0 -1
  136. package/lib/Signal/libsignal.js.map +0 -1
  137. package/lib/Signal/lid-mapping.d.ts.map +0 -1
  138. package/lib/Signal/lid-mapping.js.map +0 -1
  139. package/lib/Socket/Client/index.d.ts.map +0 -1
  140. package/lib/Socket/Client/index.js.map +0 -1
  141. package/lib/Socket/Client/types.d.ts.map +0 -1
  142. package/lib/Socket/Client/types.js.map +0 -1
  143. package/lib/Socket/Client/websocket.d.ts.map +0 -1
  144. package/lib/Socket/Client/websocket.js.map +0 -1
  145. package/lib/Socket/business.d.ts.map +0 -1
  146. package/lib/Socket/business.js.map +0 -1
  147. package/lib/Socket/chats.d.ts.map +0 -1
  148. package/lib/Socket/chats.js.map +0 -1
  149. package/lib/Socket/communities.d.ts.map +0 -1
  150. package/lib/Socket/communities.js.map +0 -1
  151. package/lib/Socket/groups.d.ts.map +0 -1
  152. package/lib/Socket/groups.js.map +0 -1
  153. package/lib/Socket/index.d.ts.map +0 -1
  154. package/lib/Socket/index.js.map +0 -1
  155. package/lib/Socket/messages-recv.d.ts.map +0 -1
  156. package/lib/Socket/messages-recv.js.map +0 -1
  157. package/lib/Socket/messages-send.d.ts.map +0 -1
  158. package/lib/Socket/messages-send.js.map +0 -1
  159. package/lib/Socket/mex.d.ts.map +0 -1
  160. package/lib/Socket/mex.js.map +0 -1
  161. package/lib/Socket/newsletter.d.ts.map +0 -1
  162. package/lib/Socket/newsletter.js.map +0 -1
  163. package/lib/Socket/socket.d.ts.map +0 -1
  164. package/lib/Socket/socket.js.map +0 -1
  165. package/lib/Store/index.d.ts.map +0 -1
  166. package/lib/Store/index.js.map +0 -1
  167. package/lib/Store/make-in-memory-store.d.ts.map +0 -1
  168. package/lib/Store/make-in-memory-store.js.map +0 -1
  169. package/lib/Store/make-ordered-dictionary.d.ts.map +0 -1
  170. package/lib/Store/make-ordered-dictionary.js.map +0 -1
  171. package/lib/Store/object-repository.d.ts.map +0 -1
  172. package/lib/Store/object-repository.js.map +0 -1
  173. package/lib/Types/Auth.d.ts.map +0 -1
  174. package/lib/Types/Auth.js.map +0 -1
  175. package/lib/Types/Bussines.d.ts.map +0 -1
  176. package/lib/Types/Call.d.ts.map +0 -1
  177. package/lib/Types/Chat.d.ts.map +0 -1
  178. package/lib/Types/Contact.d.ts.map +0 -1
  179. package/lib/Types/Events.d.ts.map +0 -1
  180. package/lib/Types/GroupMetadata.d.ts.map +0 -1
  181. package/lib/Types/Label.d.ts.map +0 -1
  182. package/lib/Types/LabelAssociation.d.ts.map +0 -1
  183. package/lib/Types/Message.d.ts.map +0 -1
  184. package/lib/Types/Mex.d.ts.map +0 -1
  185. package/lib/Types/Product.d.ts.map +0 -1
  186. package/lib/Types/RichType.d.ts.map +0 -1
  187. package/lib/Types/Signal.d.ts.map +0 -1
  188. package/lib/Types/Socket.d.ts.map +0 -1
  189. package/lib/Types/State.d.ts.map +0 -1
  190. package/lib/Types/USync.d.ts.map +0 -1
  191. package/lib/Types/index.d.ts.map +0 -1
  192. package/lib/Utils/auth-utils.d.ts.map +0 -1
  193. package/lib/Utils/auth-utils.js.map +0 -1
  194. package/lib/Utils/browser-utils.d.ts.map +0 -1
  195. package/lib/Utils/browser-utils.js.map +0 -1
  196. package/lib/Utils/business.d.ts.map +0 -1
  197. package/lib/Utils/business.js.map +0 -1
  198. package/lib/Utils/chat-utils.d.ts.map +0 -1
  199. package/lib/Utils/chat-utils.js.map +0 -1
  200. package/lib/Utils/companion-reg-client-utils.d.ts.map +0 -1
  201. package/lib/Utils/companion-reg-client-utils.js.map +0 -1
  202. package/lib/Utils/crypto.d.ts.map +0 -1
  203. package/lib/Utils/crypto.js.map +0 -1
  204. package/lib/Utils/decode-wa-message.d.ts.map +0 -1
  205. package/lib/Utils/decode-wa-message.js.map +0 -1
  206. package/lib/Utils/event-buffer.d.ts.map +0 -1
  207. package/lib/Utils/event-buffer.js.map +0 -1
  208. package/lib/Utils/generics.d.ts.map +0 -1
  209. package/lib/Utils/generics.js.map +0 -1
  210. package/lib/Utils/history.d.ts.map +0 -1
  211. package/lib/Utils/history.js.map +0 -1
  212. package/lib/Utils/identity-change-handler.d.ts.map +0 -1
  213. package/lib/Utils/identity-change-handler.js.map +0 -1
  214. package/lib/Utils/index.d.ts.map +0 -1
  215. package/lib/Utils/index.js.map +0 -1
  216. package/lib/Utils/link-preview.d.ts.map +0 -1
  217. package/lib/Utils/link-preview.js.map +0 -1
  218. package/lib/Utils/logger.d.ts.map +0 -1
  219. package/lib/Utils/logger.js.map +0 -1
  220. package/lib/Utils/lt-hash.d.ts.map +0 -1
  221. package/lib/Utils/lt-hash.js.map +0 -1
  222. package/lib/Utils/make-mutex.d.ts.map +0 -1
  223. package/lib/Utils/make-mutex.js.map +0 -1
  224. package/lib/Utils/message-retry-manager.d.ts.map +0 -1
  225. package/lib/Utils/message-retry-manager.js.map +0 -1
  226. package/lib/Utils/messages-media.d.ts.map +0 -1
  227. package/lib/Utils/messages-media.js.map +0 -1
  228. package/lib/Utils/messages.d.ts.map +0 -1
  229. package/lib/Utils/messages.js.map +0 -1
  230. package/lib/Utils/noise-handler.d.ts.map +0 -1
  231. package/lib/Utils/noise-handler.js.map +0 -1
  232. package/lib/Utils/offline-node-processor.d.ts.map +0 -1
  233. package/lib/Utils/offline-node-processor.js.map +0 -1
  234. package/lib/Utils/pre-key-manager.d.ts.map +0 -1
  235. package/lib/Utils/pre-key-manager.js.map +0 -1
  236. package/lib/Utils/process-message.d.ts.map +0 -1
  237. package/lib/Utils/process-message.js.map +0 -1
  238. package/lib/Utils/reporting-utils.d.ts.map +0 -1
  239. package/lib/Utils/reporting-utils.js.map +0 -1
  240. package/lib/Utils/rich-message-utils.d.ts.map +0 -1
  241. package/lib/Utils/rich-message-utils.js.map +0 -1
  242. package/lib/Utils/signal.d.ts.map +0 -1
  243. package/lib/Utils/signal.js.map +0 -1
  244. package/lib/Utils/stanza-ack.d.ts.map +0 -1
  245. package/lib/Utils/stanza-ack.js.map +0 -1
  246. package/lib/Utils/sync-action-utils.d.ts.map +0 -1
  247. package/lib/Utils/sync-action-utils.js.map +0 -1
  248. package/lib/Utils/tc-token-utils.d.ts.map +0 -1
  249. package/lib/Utils/tc-token-utils.js.map +0 -1
  250. package/lib/Utils/use-multi-file-auth-state.d.ts.map +0 -1
  251. package/lib/Utils/use-multi-file-auth-state.js.map +0 -1
  252. package/lib/Utils/use-single-file-auth-state.d.ts.map +0 -1
  253. package/lib/Utils/use-single-file-auth-state.js.map +0 -1
  254. package/lib/Utils/use-sqlite-auth-state.d.ts.map +0 -1
  255. package/lib/Utils/use-sqlite-auth-state.js.map +0 -1
  256. package/lib/Utils/validate-connection.d.ts.map +0 -1
  257. package/lib/Utils/validate-connection.js.map +0 -1
  258. package/lib/index.d.ts.map +0 -1
  259. package/lib/index.js.map +0 -1
@@ -34,645 +34,6 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.makeLibSignalRepository = makeLibSignalRepository;
37
- const libsignal = __importStar(require("libsignal"));
38
-
39
- const Utils_1 = require("../Utils");
40
- const WABinary_1 = require("../WABinary");
41
- const sender_key_name_1 = require("./Group/sender-key-name");
42
- const sender_key_record_1 = require("./Group/sender-key-record");
43
- const Group_1 = require("./Group");
44
- function makeLibSignalRepository(auth) {
45
- const storage = signalStorage(auth);
46
- return {
47
- decryptGroupMessage({ group, authorJid, msg }) {
48
- const senderName = jidToSignalSenderKeyName(group, authorJid);
49
- const cipher = new Group_1.GroupCipher(storage, senderName);
50
- return cipher.decrypt(msg);
51
- },
52
- async processSenderKeyDistributionMessage({ item, authorJid }) {
53
- const builder = new Group_1.GroupSessionBuilder(storage);
54
- if (!item.groupId) {
55
- throw new Error('Group ID is required for sender key distribution message');
56
- }
57
- const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
58
- const senderMsg = new Group_1.SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
59
- const senderNameStr = senderName.toString();
60
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
61
- if (!senderKey) {
62
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
63
- }
64
- await builder.process(senderName, senderMsg);
65
- },
66
- async decryptMessage({ jid, type, ciphertext }) {
67
- const addr = jidToSignalProtocolAddress(jid);
68
- const session = new (require('./session-cipher').ScopedSessionCipher)(storage, addr);
69
- let result;
70
- switch (type) {
71
- case 'pkmsg':
72
- result = await session.decryptPreKeyWhisperMessage(ciphertext);
73
- break;
74
- case 'msg':
75
- result = await session.decryptWhisperMessage(ciphertext);
76
- break;
77
- default:
78
- throw new Error(`Unknown message type: ${type}`);
79
- }
80
- return result;
81
- },
82
- async encryptMessage({ jid, data }) {
83
- const addr = jidToSignalProtocolAddress(jid);
84
- const cipher = new (require('./session-cipher').ScopedSessionCipher)(storage, addr);
85
- const { type: sigType, body } = await cipher.encrypt(data);
86
- const type = sigType === 3 ? 'pkmsg' : 'msg';
87
- return { type, ciphertext: Buffer.from(body, 'binary') };
88
- },
89
- async encryptGroupMessage({ group, meId, data }) {
90
- const senderName = jidToSignalSenderKeyName(group, meId);
91
- const builder = new Group_1.GroupSessionBuilder(storage);
92
- const senderNameStr = senderName.toString();
93
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
94
- if (!senderKey) {
95
- await storage.storeSenderKey(senderName, new sender_key_record_1.SenderKeyRecord());
96
- }
97
- const senderKeyDistributionMessage = await builder.create(senderName);
98
- const session = new Group_1.GroupCipher(storage, senderName);
99
- const ciphertext = await session.encrypt(data);
100
- return {
101
- ciphertext,
102
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
103
- };
104
- },
105
- async injectE2ESession({ jid, session }) {
106
- const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
107
- await cipher.initOutgoing(session);
108
- },
109
- jidToSignalProtocolAddress(jid) {
110
- return jidToSignalProtocolAddress(jid).toString();
111
- }
112
- };
113
- }
114
- const jidToSignalProtocolAddress = (jid) => {
115
- const { user, device } = (0, WABinary_1.jidDecode)(jid);
116
- return new libsignal.ProtocolAddress(user, device || 0);
117
- };
118
- const jidToSignalSenderKeyName = (group, user) => {
119
- return new sender_key_name_1.SenderKeyName(group, jidToSignalProtocolAddress(user));
120
- };
121
- function signalStorage({ creds, keys }) {
122
- return {
123
- loadSession: async (id) => {
124
- const { [id]: sess } = await keys.get('session', [id]);
125
- if (sess) {
126
- return libsignal.SessionRecord.deserialize(sess);
127
- }
128
- },
129
- storeSession: async (id, session) => {
130
- await keys.set({ session: { [id]: session.serialize() } });
131
- },
132
- isTrustedIdentity: () => {
133
- return true;
134
- },
135
- loadPreKey: async (id) => {
136
- const keyId = id.toString();
137
- const { [keyId]: key } = await keys.get('pre-key', [keyId]);
138
- if (key) {
139
- return {
140
- privKey: Buffer.from(key.private),
141
- pubKey: Buffer.from(key.public)
142
- };
143
- }
144
- },
145
- removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
146
- loadSignedPreKey: () => {
147
- const key = creds.signedPreKey;
148
- return {
149
- privKey: Buffer.from(key.keyPair.private),
150
- pubKey: Buffer.from(key.keyPair.public)
151
- };
152
- },
153
- loadSenderKey: async (senderKeyName) => {
154
- const keyId = senderKeyName.toString();
155
- const { [keyId]: key } = await keys.get('sender-key', [keyId]);
156
- if (key) {
157
- return sender_key_record_1.SenderKeyRecord.deserialize(key);
158
- }
159
- return new sender_key_record_1.SenderKeyRecord();
160
- },
161
- storeSenderKey: async (senderKeyName, key) => {
162
- const keyId = senderKeyName.toString();
163
- const serialized = JSON.stringify(key.serialize());
164
- await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
165
- },
166
- getOurRegistrationId: () => creds.registrationId,
167
- getOurIdentity: () => {
168
- const { signedIdentityKey } = creds;
169
- return {
170
- privKey: Buffer.from(signedIdentityKey.private),
171
- pubKey: (0, Utils_1.generateSignalPubKey)(signedIdentityKey.public)
172
- };
173
- }
174
- };
175
- }
176
-
177
-
178
- const __kageModule19 = (() => {
179
- const module = { exports: {} }; const exports = module.exports;
180
- "use strict"
181
-
182
- Object.defineProperty(exports, "__esModule", { value: true })
183
-
184
- const {
185
- SessionCipher,
186
- SessionBuilder,
187
- SessionRecord,
188
- ProtocolAddress
189
- } = require("libsignal")
190
- const { generateSignalPubKey } = require("../Utils")
191
- const {
192
- isHostedLidUser,
193
- isHostedPnUser,
194
- isLidUser,
195
- isPnUser,
196
- jidDecode,
197
- transferDevice,
198
- WAJIDDomains
199
- } = require("../WABinary")
200
- const { SenderKeyName } = require("./Group/sender-key-name")
201
- const { SenderKeyRecord } = require("./Group/sender-key-record")
202
- const {
203
- GroupCipher,
204
- GroupSessionBuilder,
205
- SenderKeyDistributionMessage
206
- } = require("./Group")
207
- const { LRUCache } = require("lru-cache")
208
- const { LIDMappingStore } = require("./lid-mapping")
209
-
210
- function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
211
- const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc)
212
- const storage = signalStorage(auth, lidMapping)
213
- const parsedKeys = auth.keys
214
- const migratedSessionCache = new LRUCache({
215
- ttl: 3 * 24 * 60 * 60 * 1000,
216
- ttlAutopurge: true,
217
- updateAgeOnGet: true
218
- })
219
-
220
- const repository = {
221
- decryptGroupMessage({ group, authorJid, msg }) {
222
- const senderName = jidToSignalSenderKeyName(group, authorJid)
223
- const cipher = new GroupCipher(storage, senderName)
224
-
225
-
226
- return parsedKeys.transaction(async () => {
227
- return cipher.decrypt(msg)
228
- }, group)
229
- },
230
-
231
- async processSenderKeyDistributionMessage({ item, authorJid }) {
232
- const builder = new GroupSessionBuilder(storage)
233
-
234
- if (!item.groupId) {
235
- throw new Error('Group ID is required for sender key distribution message')
236
- }
237
-
238
- const senderName = jidToSignalSenderKeyName(item.groupId, authorJid)
239
- const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage)
240
- const senderNameStr = senderName.toString()
241
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr])
242
-
243
- if (!senderKey) {
244
- await storage.storeSenderKey(senderName, new SenderKeyRecord())
245
- }
246
-
247
- return parsedKeys.transaction(async () => {
248
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr])
249
-
250
- if (!senderKey) {
251
- await storage.storeSenderKey(senderName, new SenderKeyRecord())
252
- }
253
-
254
- await builder.process(senderName, senderMsg)
255
- }, item.groupId)
256
- },
257
-
258
- async decryptMessage({ jid, type, ciphertext }) {
259
- const addr = jidToSignalProtocolAddress(jid)
260
- const session = new SessionCipher(storage, addr)
261
-
262
- async function doDecrypt() {
263
- let result
264
-
265
- switch (type) {
266
- case 'pkmsg':
267
- result = await session.decryptPreKeyWhisperMessage(ciphertext)
268
- break
269
- case 'msg':
270
- result = await session.decryptWhisperMessage(ciphertext)
271
- break
272
- }
273
-
274
- return result
275
- }
276
-
277
-
278
-
279
- return parsedKeys.transaction(async () => {
280
- return await doDecrypt()
281
- }, jid)
282
- },
283
-
284
- async encryptMessage({ jid, data }) {
285
- const addr = jidToSignalProtocolAddress(jid)
286
- const cipher = new SessionCipher(storage, addr)
287
-
288
-
289
- return parsedKeys.transaction(async () => {
290
- const { type: sigType, body } = await cipher.encrypt(data)
291
- const type = sigType === 3 ? 'pkmsg' : 'msg'
292
-
293
- return { type, ciphertext: Buffer.from(body, 'binary') }
294
- }, jid)
295
- },
296
-
297
- async encryptGroupMessage({ group, meId, data }) {
298
- const senderName = jidToSignalSenderKeyName(group, meId)
299
- const builder = new GroupSessionBuilder(storage)
300
- const senderNameStr = senderName.toString()
301
-
302
- return parsedKeys.transaction(async () => {
303
- const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr])
304
-
305
- if (!senderKey) {
306
- await storage.storeSenderKey(senderName, new SenderKeyRecord())
307
- }
308
-
309
- const senderKeyDistributionMessage = await builder.create(senderName)
310
- const session = new GroupCipher(storage, senderName)
311
- const ciphertext = await session.encrypt(data)
312
-
313
- return {
314
- ciphertext,
315
- senderKeyDistributionMessage: senderKeyDistributionMessage.serialize()
316
- }
317
- }, group)
318
- },
319
-
320
- async injectE2ESession({ jid, session }) {
321
- logger.trace({ jid }, 'injecting E2EE session')
322
-
323
- const cipher = new SessionBuilder(storage, jidToSignalProtocolAddress(jid))
324
-
325
- return parsedKeys.transaction(async () => {
326
- await cipher.initOutgoing(session)
327
- }, jid)
328
- },
329
-
330
- jidToSignalProtocolAddress(jid) {
331
- return jidToSignalProtocolAddress(jid).toString()
332
- },
333
-
334
-
335
- lidMapping,
336
-
337
- async validateSession(jid) {
338
- try {
339
- const addr = jidToSignalProtocolAddress(jid)
340
- const session = await storage.loadSession(addr.toString())
341
-
342
- if (!session) {
343
- return { exists: false, reason: 'no session' }
344
- }
345
-
346
- if (!session.haveOpenSession()) {
347
- return { exists: false, reason: 'no open session' }
348
- }
349
-
350
- return { exists: true }
351
- }
352
-
353
- catch (error) {
354
- return { exists: false, reason: 'validation error' }
355
- }
356
- },
357
-
358
- async deleteSession(jids) {
359
- if (!jids.length) return
360
-
361
-
362
- const sessionUpdates = {}
363
-
364
- jids.forEach(jid => {
365
- const addr = jidToSignalProtocolAddress(jid)
366
- sessionUpdates[addr.toString()] = null
367
- })
368
-
369
-
370
- return parsedKeys.transaction(async () => {
371
- await auth.keys.set({ session: sessionUpdates })
372
- }, `delete-${jids.length}-sessions`)
373
- },
374
-
375
- async migrateSession(fromJid, toJid) {
376
-
377
- if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid))) {
378
- return { migrated: 0, skipped: 0, total: 0 }
379
- }
380
-
381
-
382
- if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
383
- return { migrated: 0, skipped: 0, total: 1 }
384
- }
385
-
386
- const { user } = jidDecode(fromJid)
387
-
388
- logger.debug({ fromJid }, 'bulk device migration - loading all user devices')
389
-
390
-
391
- const { [user]: userDevices } = await parsedKeys.get('device-list', [user])
392
-
393
- if (!userDevices) {
394
- return { migrated: 0, skipped: 0, total: 0 }
395
- }
396
-
397
- const { device: fromDevice } = jidDecode(fromJid)
398
- const fromDeviceStr = fromDevice?.toString() || '0'
399
-
400
- if (!userDevices.includes(fromDeviceStr)) {
401
- userDevices.push(fromDeviceStr)
402
- }
403
-
404
-
405
- const uncachedDevices = userDevices.filter(device => {
406
- const deviceKey = `${user}.${device}`
407
- return !migratedSessionCache.has(deviceKey)
408
- })
409
-
410
-
411
- const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`)
412
- const existingSessions = await parsedKeys.get('session', deviceSessionKeys)
413
-
414
-
415
- const deviceJids = []
416
-
417
- for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
418
- if (sessionData) {
419
-
420
- const deviceStr = sessionKey.split('.')[1]
421
-
422
- if (!deviceStr) continue
423
-
424
- const deviceNum = parseInt(deviceStr)
425
-
426
- let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`
427
-
428
- if (deviceNum === 99) {
429
- jid = `${user}:99@hosted`
430
- }
431
-
432
- deviceJids.push(jid)
433
- }
434
- }
435
-
436
- logger.debug({
437
- fromJid,
438
- totalDevices: userDevices.length,
439
- devicesWithSessions: deviceJids.length,
440
- devices: deviceJids
441
- }, 'bulk device migration complete - all user devices processed')
442
-
443
-
444
- return parsedKeys.transaction(async () => {
445
- const migrationOps = deviceJids.map(jid => {
446
- const lidWithDevice = transferDevice(jid, toJid)
447
- const fromDecoded = jidDecode(jid)
448
- const toDecoded = jidDecode(lidWithDevice)
449
-
450
- return {
451
- fromJid: jid,
452
- toJid: lidWithDevice,
453
- pnUser: fromDecoded.user,
454
- lidUser: toDecoded.user,
455
- deviceId: fromDecoded.device || 0,
456
- fromAddr: jidToSignalProtocolAddress(jid),
457
- toAddr: jidToSignalProtocolAddress(lidWithDevice)
458
- }
459
- })
460
-
461
- const totalOps = migrationOps.length
462
-
463
- let migratedCount = 0
464
-
465
-
466
- const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())))
467
- const pnSessions = await parsedKeys.get('session', pnAddrStrings)
468
-
469
-
470
- const sessionUpdates = {}
471
-
472
- for (const op of migrationOps) {
473
- const pnAddrStr = op.fromAddr.toString()
474
- const lidAddrStr = op.toAddr.toString()
475
- const pnSession = pnSessions[pnAddrStr]
476
-
477
- if (pnSession) {
478
-
479
- const fromSession = SessionRecord.deserialize(pnSession)
480
-
481
- if (fromSession.haveOpenSession()) {
482
-
483
- sessionUpdates[lidAddrStr] = fromSession.serialize()
484
- sessionUpdates[pnAddrStr] = null
485
- migratedCount++
486
- }
487
- }
488
- }
489
-
490
-
491
- if (Object.keys(sessionUpdates).length > 0) {
492
- await parsedKeys.set({ session: sessionUpdates })
493
-
494
- logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete')
495
-
496
-
497
- for (const op of migrationOps) {
498
- if (sessionUpdates[op.toAddr.toString()]) {
499
- const deviceKey = `${op.pnUser}.${op.deviceId}`
500
- migratedSessionCache.set(deviceKey, true)
501
- }
502
- }
503
- }
504
-
505
- const skippedCount = totalOps - migratedCount
506
-
507
- return { migrated: migratedCount, skipped: skippedCount, total: totalOps }
508
- }, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`)
509
- }
510
- }
511
-
512
- return repository
513
- }
514
-
515
- const jidToSignalProtocolAddress = (jid) => {
516
- const decoded = jidDecode(jid)
517
- const { user, device, server, domainType } = decoded
518
-
519
- if (!user) {
520
- throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`)
521
- }
522
-
523
- const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user
524
- const finalDevice = device || 0
525
-
526
- if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
527
- throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid)
528
- }
529
-
530
- return new ProtocolAddress(signalUser, finalDevice)
531
- }
532
-
533
- const jidToSignalSenderKeyName = (group, user) => {
534
- return new SenderKeyName(group, jidToSignalProtocolAddress(user))
535
- }
536
-
537
- function signalStorage({ creds, keys }, lidMapping) {
538
-
539
- const resolveLIDSignalAddress = async (id) => {
540
- if (id.includes('.')) {
541
- const [deviceId, device] = id.split('.')
542
- const [user, domainType_] = deviceId.split('_')
543
- const domainType = parseInt(domainType_ || '0')
544
-
545
- if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID) return id
546
-
547
- const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`
548
- const lidForPN = await lidMapping.getLIDForPN(pnJid)
549
-
550
- if (lidForPN) {
551
- const lidAddr = jidToSignalProtocolAddress(lidForPN)
552
- return lidAddr.toString()
553
- }
554
- }
555
-
556
- return id
557
- }
558
-
559
- return {
560
- loadSession: async (id) => {
561
- try {
562
- const wireJid = await resolveLIDSignalAddress(id)
563
- const { [wireJid]: sess } = await keys.get('session', [wireJid])
564
-
565
- if (sess) {
566
- return SessionRecord.deserialize(sess)
567
- }
568
- }
569
-
570
- catch (e) {
571
- return null
572
- }
573
-
574
- return null
575
- },
576
- storeSession: async (id, session) => {
577
- const wireJid = await resolveLIDSignalAddress(id)
578
- await keys.set({ session: { [wireJid]: session.serialize() } })
579
- },
580
- isTrustedIdentity: () => {
581
- return true
582
- },
583
- loadPreKey: async (id) => {
584
- const keyId = id.toString()
585
- const { [keyId]: key } = await keys.get('pre-key', [keyId])
586
-
587
- if (key) {
588
- return {
589
- privKey: Buffer.from(key.private),
590
- pubKey: Buffer.from(key.public)
591
- }
592
- }
593
- },
594
- removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
595
- loadSignedPreKey: () => {
596
- const key = creds.signedPreKey
597
- return {
598
- privKey: Buffer.from(key.keyPair.private),
599
- pubKey: Buffer.from(key.keyPair.public)
600
- }
601
- },
602
- loadSenderKey: async (senderKeyName) => {
603
- const keyId = senderKeyName.toString()
604
- const { [keyId]: key } = await keys.get('sender-key', [keyId])
605
-
606
- if (key) {
607
- return SenderKeyRecord.deserialize(key)
608
- }
609
-
610
- return new SenderKeyRecord()
611
- },
612
- storeSenderKey: async (senderKeyName, key) => {
613
- const keyId = senderKeyName.toString()
614
- const serialized = JSON.stringify(key.serialize())
615
- await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } })
616
- },
617
- getOurRegistrationId: () => creds.registrationId,
618
- getOurIdentity: () => {
619
- const { signedIdentityKey } = creds
620
- return {
621
- privKey: Buffer.from(signedIdentityKey.private),
622
- pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
623
- }
624
- }
625
- }
626
- }
627
-
628
- module.exports = {
629
- makeLibSignalRepository
630
- }
631
- return module.exports;
632
- })();
633
- for (const key of Object.keys(__kageModule19)) {
634
- if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule19[key]});
635
- }
636
-
637
-
638
- const __kageModule125 = (() => {
639
- const module = { exports: {} }; const exports = module.exports;
640
- "use strict";
641
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
642
- if (k2 === undefined) k2 = k;
643
- var desc = Object.getOwnPropertyDescriptor(m, k);
644
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
645
- desc = { enumerable: true, get: function() { return m[k]; } };
646
- }
647
- Object.defineProperty(o, k2, desc);
648
- }) : (function(o, m, k, k2) {
649
- if (k2 === undefined) k2 = k;
650
- o[k2] = m[k];
651
- }));
652
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
653
- Object.defineProperty(o, "default", { enumerable: true, value: v });
654
- }) : function(o, v) {
655
- o["default"] = v;
656
- });
657
- var __importStar = (this && this.__importStar) || (function () {
658
- var ownKeys = function(o) {
659
- ownKeys = Object.getOwnPropertyNames || function (o) {
660
- var ar = [];
661
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
662
- return ar;
663
- };
664
- return ownKeys(o);
665
- };
666
- return function (mod) {
667
- if (mod && mod.__esModule) return mod;
668
- var result = {};
669
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
670
- __setModuleDefault(result, mod);
671
- return result;
672
- };
673
- })();
674
- Object.defineProperty(exports, "__esModule", { value: true });
675
- exports.makeLibSignalRepository = makeLibSignalRepository;
676
37
 
677
38
  const libsignal = __importStar(require("libsignal"));
678
39
 
@@ -1101,10 +462,3 @@ function signalStorage({ creds, keys }, lidMapping) {
1101
462
  }
1102
463
  };
1103
464
  }
1104
-
1105
- return module.exports;
1106
- })();
1107
- for (const key of Object.keys(__kageModule125)) {
1108
- if (key !== '__esModule' && !Object.prototype.hasOwnProperty.call(exports,key)) Object.defineProperty(exports,key,{enumerable:true,configurable:true,get:()=>__kageModule125[key]});
1109
- }
1110
- Object.defineProperty(exports, Symbol.for('kage.advanced'), {value:__kageModule125, configurable:true});