@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.
Files changed (212) hide show
  1. package/README.upstream.md +1312 -0
  2. package/WAProto/WAProto.json +1 -0
  3. package/WAProto/WAProto.proto +6275 -0
  4. package/WAProto/compiler.js +550 -0
  5. package/WAProto/index.js +2 -97691
  6. package/lib/Defaults/index.js +89 -92
  7. package/lib/Signal/Group/ciphertext-message.js +9 -10
  8. package/lib/Signal/Group/group-session-builder.js +21 -22
  9. package/lib/Signal/Group/group_cipher.js +75 -69
  10. package/lib/Signal/Group/index.js +0 -1
  11. package/lib/Signal/Group/keyhelper.js +9 -10
  12. package/lib/Signal/Group/sender-chain-key.js +21 -22
  13. package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
  14. package/lib/Signal/Group/sender-key-message.js +56 -62
  15. package/lib/Signal/Group/sender-key-name.js +38 -40
  16. package/lib/Signal/Group/sender-key-record.js +33 -34
  17. package/lib/Signal/Group/sender-key-state.js +72 -76
  18. package/lib/Signal/Group/sender-message-key.js +22 -23
  19. package/lib/Signal/libsignal.js +506 -405
  20. package/lib/Signal/lid-mapping.js +276 -258
  21. package/lib/Socket/Client/index.js +0 -1
  22. package/lib/Socket/Client/types.js +6 -7
  23. package/lib/Socket/Client/websocket.js +48 -49
  24. package/lib/Socket/business.js +349 -369
  25. package/lib/Socket/chats.js +1172 -1148
  26. package/lib/Socket/communities.js +473 -413
  27. package/lib/Socket/groups.js +387 -331
  28. package/lib/Socket/index.js +6 -8
  29. package/lib/Socket/messages-recv.js +1782 -1719
  30. package/lib/Socket/messages-send.js +1554 -2450
  31. package/lib/Socket/mex.js +40 -35
  32. package/lib/Socket/newsletter.js +239 -191
  33. package/lib/Socket/socket.js +992 -923
  34. package/lib/Store/index.js +8 -0
  35. package/lib/Store/keyed-db.js +108 -0
  36. package/lib/Store/make-cache-manager-store.js +70 -0
  37. package/lib/Store/make-in-memory-store.js +417 -0
  38. package/lib/Store/make-ordered-dictionary.js +69 -0
  39. package/lib/Store/object-repository.js +27 -0
  40. package/lib/Types/Auth.js +0 -1
  41. package/lib/Types/Bussines.js +0 -1
  42. package/lib/Types/Call.js +0 -1
  43. package/lib/Types/Chat.js +1 -8
  44. package/lib/Types/Contact.js +0 -1
  45. package/lib/Types/Events.js +1 -2
  46. package/lib/Types/GroupMetadata.js +0 -1
  47. package/lib/Types/Label.js +20 -22
  48. package/lib/Types/LabelAssociation.js +2 -4
  49. package/lib/Types/Message.js +3 -5
  50. package/lib/Types/Mex.js +26 -31
  51. package/lib/Types/Product.js +0 -1
  52. package/lib/Types/Signal.js +1 -2
  53. package/lib/Types/Socket.js +2 -3
  54. package/lib/Types/State.js +4 -52
  55. package/lib/Types/USync.js +0 -1
  56. package/lib/Types/index.js +10 -11
  57. package/lib/Utils/auth-utils.js +240 -277
  58. package/lib/Utils/browser-utils.js +20 -23
  59. package/lib/Utils/business.js +200 -204
  60. package/lib/Utils/chat-utils.js +836 -810
  61. package/lib/Utils/companion-reg-client-utils.js +23 -25
  62. package/lib/Utils/crypto.js +70 -86
  63. package/lib/Utils/decode-wa-message.js +277 -284
  64. package/lib/Utils/event-buffer.js +592 -595
  65. package/lib/Utils/generics.js +312 -335
  66. package/lib/Utils/history.js +123 -115
  67. package/lib/Utils/index.js +1 -2
  68. package/lib/Utils/link-preview.js +74 -73
  69. package/lib/Utils/logger.js +3 -2
  70. package/lib/Utils/lt-hash.js +0 -6
  71. package/lib/Utils/make-mutex.js +27 -27
  72. package/lib/Utils/message-retry-manager.js +213 -254
  73. package/lib/Utils/messages-media.js +741 -715
  74. package/lib/Utils/messages.js +791 -827
  75. package/lib/Utils/noise-handler.js +215 -187
  76. package/lib/Utils/pre-key-manager.js +69 -98
  77. package/lib/Utils/process-message.js +617 -586
  78. package/lib/Utils/reporting-utils.js +553 -234
  79. package/lib/Utils/signal.js +217 -170
  80. package/lib/Utils/stanza-ack.js +27 -35
  81. package/lib/Utils/sync-action-utils.js +37 -41
  82. package/lib/Utils/tc-token-utils.js +120 -141
  83. package/lib/Utils/use-multi-file-auth-state.js +87 -105
  84. package/lib/Utils/use-sqlite-auth-state.js +162 -0
  85. package/lib/Utils/validate-connection.js +190 -188
  86. package/lib/WABinary/constants.js +28 -1294
  87. package/lib/WABinary/decode.js +245 -249
  88. package/lib/WABinary/encode.js +201 -206
  89. package/lib/WABinary/generic-utils.js +86 -89
  90. package/lib/WABinary/index.js +0 -1
  91. package/lib/WABinary/jid-utils.js +68 -77
  92. package/lib/WABinary/types.js +0 -1
  93. package/lib/WAM/BinaryInfo.js +7 -8
  94. package/lib/WAM/constants.js +19892 -22853
  95. package/lib/WAM/encode.js +121 -132
  96. package/lib/WAM/index.js +0 -1
  97. package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
  98. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
  99. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
  100. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
  101. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
  102. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
  103. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
  104. package/lib/WAUSync/Protocols/index.js +0 -1
  105. package/lib/WAUSync/USyncQuery.js +79 -88
  106. package/lib/WAUSync/USyncUser.js +28 -29
  107. package/lib/WAUSync/index.js +0 -1
  108. package/lib/index.js +2 -2
  109. package/package.json +22 -86
  110. package/WAProto/GenerateStatics.sh +0 -3
  111. package/WAProto/fix-imports.js +0 -85
  112. package/WAProto/index.d.ts +0 -14017
  113. package/lib/Defaults/index.d.ts +0 -75
  114. package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
  115. package/lib/Signal/Group/group-session-builder.d.ts +0 -15
  116. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  117. package/lib/Signal/Group/index.d.ts +0 -12
  118. package/lib/Signal/Group/keyhelper.d.ts +0 -11
  119. package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
  120. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
  121. package/lib/Signal/Group/sender-key-message.d.ts +0 -19
  122. package/lib/Signal/Group/sender-key-name.d.ts +0 -18
  123. package/lib/Signal/Group/sender-key-record.d.ts +0 -31
  124. package/lib/Signal/Group/sender-key-state.d.ts +0 -39
  125. package/lib/Signal/Group/sender-message-key.d.ts +0 -12
  126. package/lib/Signal/libsignal.d.ts +0 -5
  127. package/lib/Signal/lid-mapping.d.ts +0 -23
  128. package/lib/Socket/Client/index.d.ts +0 -3
  129. package/lib/Socket/Client/types.d.ts +0 -16
  130. package/lib/Socket/Client/websocket.d.ts +0 -13
  131. package/lib/Socket/business.d.ts +0 -217
  132. package/lib/Socket/chats.d.ts +0 -125
  133. package/lib/Socket/communities.d.ts +0 -273
  134. package/lib/Socket/groups.d.ts +0 -161
  135. package/lib/Socket/index.d.ts +0 -260
  136. package/lib/Socket/messages-recv.d.ts +0 -203
  137. package/lib/Socket/messages-send.d.ts +0 -199
  138. package/lib/Socket/mex.d.ts +0 -3
  139. package/lib/Socket/newsletter.d.ts +0 -170
  140. package/lib/Socket/socket.d.ts +0 -59
  141. package/lib/Types/Auth.d.ts +0 -117
  142. package/lib/Types/Bussines.d.ts +0 -25
  143. package/lib/Types/Call.d.ts +0 -15
  144. package/lib/Types/Chat.d.ts +0 -124
  145. package/lib/Types/Contact.d.ts +0 -26
  146. package/lib/Types/Events.d.ts +0 -256
  147. package/lib/Types/GroupMetadata.d.ts +0 -71
  148. package/lib/Types/Label.d.ts +0 -47
  149. package/lib/Types/LabelAssociation.d.ts +0 -30
  150. package/lib/Types/Message.d.ts +0 -320
  151. package/lib/Types/Mex.d.ts +0 -141
  152. package/lib/Types/Product.d.ts +0 -79
  153. package/lib/Types/Signal.d.ts +0 -87
  154. package/lib/Types/Socket.d.ts +0 -136
  155. package/lib/Types/State.d.ts +0 -97
  156. package/lib/Types/USync.d.ts +0 -26
  157. package/lib/Types/index.d.ts +0 -66
  158. package/lib/Utils/auth-utils.d.ts +0 -24
  159. package/lib/Utils/browser-utils.d.ts +0 -4
  160. package/lib/Utils/business.d.ts +0 -23
  161. package/lib/Utils/chat-utils.d.ts +0 -100
  162. package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
  163. package/lib/Utils/crypto.d.ts +0 -37
  164. package/lib/Utils/decode-wa-message.d.ts +0 -66
  165. package/lib/Utils/event-buffer.d.ts +0 -36
  166. package/lib/Utils/generics.d.ts +0 -92
  167. package/lib/Utils/history.d.ts +0 -24
  168. package/lib/Utils/identity-change-handler.d.ts +0 -44
  169. package/lib/Utils/identity-change-handler.js +0 -50
  170. package/lib/Utils/index.d.ts +0 -22
  171. package/lib/Utils/link-preview.d.ts +0 -21
  172. package/lib/Utils/logger.d.ts +0 -12
  173. package/lib/Utils/lt-hash.d.ts +0 -8
  174. package/lib/Utils/make-mutex.d.ts +0 -9
  175. package/lib/Utils/message-retry-manager.d.ts +0 -115
  176. package/lib/Utils/messages-media.d.ts +0 -133
  177. package/lib/Utils/messages.d.ts +0 -91
  178. package/lib/Utils/noise-handler.d.ts +0 -20
  179. package/lib/Utils/offline-node-processor.d.ts +0 -17
  180. package/lib/Utils/offline-node-processor.js +0 -40
  181. package/lib/Utils/pre-key-manager.d.ts +0 -28
  182. package/lib/Utils/process-message.d.ts +0 -60
  183. package/lib/Utils/reporting-utils.d.ts +0 -11
  184. package/lib/Utils/signal.d.ts +0 -47
  185. package/lib/Utils/stanza-ack.d.ts +0 -11
  186. package/lib/Utils/sync-action-utils.d.ts +0 -19
  187. package/lib/Utils/tc-token-utils.d.ts +0 -37
  188. package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
  189. package/lib/Utils/validate-connection.d.ts +0 -11
  190. package/lib/WABinary/constants.d.ts +0 -28
  191. package/lib/WABinary/decode.d.ts +0 -7
  192. package/lib/WABinary/encode.d.ts +0 -3
  193. package/lib/WABinary/generic-utils.d.ts +0 -15
  194. package/lib/WABinary/index.d.ts +0 -6
  195. package/lib/WABinary/jid-utils.d.ts +0 -48
  196. package/lib/WABinary/types.d.ts +0 -19
  197. package/lib/WAM/BinaryInfo.d.ts +0 -9
  198. package/lib/WAM/constants.d.ts +0 -40
  199. package/lib/WAM/encode.d.ts +0 -3
  200. package/lib/WAM/index.d.ts +0 -4
  201. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
  202. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
  203. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
  204. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
  205. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
  208. package/lib/WAUSync/Protocols/index.d.ts +0 -6
  209. package/lib/WAUSync/USyncQuery.d.ts +0 -30
  210. package/lib/WAUSync/USyncUser.d.ts +0 -17
  211. package/lib/WAUSync/index.d.ts +0 -4
  212. package/lib/index.d.ts +0 -12
@@ -1,8 +1,10 @@
1
- import { proto } from '../../WAProto/index.js';
1
+ import { createHash } from 'crypto';
2
+ import { createRequire } from 'module';
3
+ import { proto } from '../../WAProto/compiler.js';
2
4
  import { makeLibSignalRepository } from '../Signal/libsignal.js';
3
5
  import { Browsers } from '../Utils/browser-utils.js';
4
6
  import logger from '../Utils/logger.js';
5
- const version = [2, 3000, 1043857760];
7
+ const version = [2, 3000, 1035194821];
6
8
  export const UNAUTHORIZED_CODES = [401, 403, 419];
7
9
  export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
8
10
  export const CALL_VIDEO_PREFIX = 'https://call.whatsapp.com/video/';
@@ -15,115 +17,110 @@ export const WA_ADV_DEVICE_SIG_PREFIX = Buffer.from([6, 1]);
15
17
  export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX = Buffer.from([6, 5]);
16
18
  export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX = Buffer.from([6, 6]);
17
19
  export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
18
- /** Status messages older than 24 hours are considered expired */
19
20
  export const STATUS_EXPIRY_SECONDS = 24 * 60 * 60;
20
- /** WA Web enforces a 14-day maximum age for placeholder resend requests */
21
21
  export const PLACEHOLDER_MAX_AGE_SECONDS = 14 * 24 * 60 * 60;
22
22
  export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
23
23
  export const DICT_VERSION = 3;
24
24
  export const KEY_BUNDLE_TYPE = Buffer.from([5]);
25
- export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]); // last is "DICT_VERSION"
26
- /** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
25
+ export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]);
27
26
  export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
28
27
  export const WA_CERT_DETAILS = {
29
- SERIAL: 0,
30
- ISSUER: 'WhatsAppLongTerm1',
31
- PUBLIC_KEY: Buffer.from('142375574d0a587166aae71ebe516437c4a28b73e3695c6ce1f7f9545da8ee6b', 'hex')
28
+ SERIAL: 0,
29
+ ISSUER: 'WhatsAppLongTerm1',
30
+ PUBLIC_KEY: Buffer.from('142375574d0a587166aae71ebe516437c4a28b73e3695c6ce1f7f9545da8ee6b', 'hex')
32
31
  };
33
- export const PROCESSABLE_HISTORY_TYPES = [
34
- proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP,
35
- proto.HistorySync.HistorySyncType.PUSH_NAME,
36
- proto.HistorySync.HistorySyncType.RECENT,
37
- proto.HistorySync.HistorySyncType.FULL,
38
- proto.HistorySync.HistorySyncType.ON_DEMAND,
39
- proto.HistorySync.HistorySyncType.NON_BLOCKING_DATA,
40
- proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3
41
- ];
32
+ export const PROCESSABLE_HISTORY_TYPES = [proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP, proto.HistorySync.HistorySyncType.PUSH_NAME, proto.HistorySync.HistorySyncType.RECENT, proto.HistorySync.HistorySyncType.FULL, proto.HistorySync.HistorySyncType.ON_DEMAND, proto.HistorySync.HistorySyncType.NON_BLOCKING_DATA, proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3];
42
33
  export const DEFAULT_CACHE_TTLS = {
43
- SIGNAL_STORE: 5 * 60, // 5 minutes
44
- MSG_RETRY: 60 * 60, // 1 hour
45
- CALL_OFFER: 5 * 60, // 5 minutes
46
- USER_DEVICES: 5 * 60 // 5 minutes
34
+ SIGNAL_STORE: 5 * 60,
35
+ MSG_RETRY: 60 * 60,
36
+ CALL_OFFER: 5 * 60,
37
+ USER_DEVICES: 5 * 60
47
38
  };
48
39
  export const DEFAULT_CONNECTION_CONFIG = {
49
- version: version,
50
- browser: Browsers.macOS('Chrome'),
51
- waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
52
- connectTimeoutMs: 20000,
53
- keepAliveIntervalMs: 30000,
54
- logger: logger.child({ class: 'baileys' }),
55
- emitOwnEvents: true,
56
- defaultQueryTimeoutMs: 60000,
57
- customUploadHosts: [],
58
- retryRequestDelayMs: 250,
59
- maxMsgRetryCount: 5,
60
- fireInitQueries: true,
61
- auth: undefined,
62
- markOnlineOnConnect: true,
63
- syncFullHistory: true,
64
- patchMessageBeforeSending: msg => msg,
65
- shouldSyncHistoryMessage: ({ syncType }) => {
66
- return syncType !== proto.HistorySync.HistorySyncType.FULL;
67
- },
68
- shouldIgnoreJid: () => false,
69
- linkPreviewImageThumbnailWidth: 192,
70
- transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
71
- generateHighQualityLinkPreview: false,
72
- enableAutoSessionRecreation: true,
73
- enableRecentMessageCache: true,
74
- options: {},
75
- appStateMacVerification: {
76
- patch: false,
77
- snapshot: false
78
- },
79
- countryCode: 'US',
80
- getMessage: async () => undefined,
81
- cachedGroupMetadata: async () => undefined,
82
- makeSignalRepository: makeLibSignalRepository
40
+ version: version,
41
+ browser: Browsers.macOS('Chrome'),
42
+ waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
43
+ connectTimeoutMs: 20000,
44
+ keepAliveIntervalMs: 30000,
45
+ logger: logger.child({
46
+ class: 'baileys'
47
+ }),
48
+ emitOwnEvents: true,
49
+ defaultQueryTimeoutMs: 60000,
50
+ customUploadHosts: [],
51
+ retryRequestDelayMs: 250,
52
+ maxMsgRetryCount: 5,
53
+ fireInitQueries: true,
54
+ auth: undefined,
55
+ markOnlineOnConnect: true,
56
+ syncFullHistory: true,
57
+ patchMessageBeforeSending: msg => msg,
58
+ shouldSyncHistoryMessage: ({
59
+ syncType
60
+ }) => {
61
+ return syncType !== proto.HistorySync.HistorySyncType.FULL;
62
+ },
63
+ shouldIgnoreJid: () => false,
64
+ ignoreOfflineMessages: false,
65
+ linkPreviewImageThumbnailWidth: 192,
66
+ transactionOpts: {
67
+ maxCommitRetries: 10,
68
+ delayBetweenTriesMs: 3000
69
+ },
70
+ generateHighQualityLinkPreview: false,
71
+ enableAutoSessionRecreation: true,
72
+ enableRecentMessageCache: true,
73
+ options: {},
74
+ appStateMacVerification: {
75
+ patch: false,
76
+ snapshot: false
77
+ },
78
+ countryCode: 'US',
79
+ getMessage: async () => undefined,
80
+ cachedGroupMetadata: async () => undefined,
81
+ makeSignalRepository: makeLibSignalRepository
83
82
  };
84
83
  export const MEDIA_PATH_MAP = {
85
- image: '/mms/image',
86
- video: '/mms/video',
87
- document: '/mms/document',
88
- audio: '/mms/audio',
89
- sticker: '/mms/image',
90
- 'thumbnail-link': '/mms/image',
91
- 'product-catalog-image': '/product/image',
92
- 'md-app-state': '',
93
- 'md-msg-hist': '/mms/md-app-state',
94
- 'biz-cover-photo': '/pps/biz-cover-photo'
84
+ image: '/mms/image',
85
+ video: '/mms/video',
86
+ document: '/mms/document',
87
+ audio: '/mms/audio',
88
+ sticker: '/mms/image',
89
+ 'thumbnail-link': '/mms/image',
90
+ 'product-catalog-image': '/product/image',
91
+ 'md-app-state': '',
92
+ 'md-msg-hist': '/mms/md-app-state',
93
+ 'biz-cover-photo': '/pps/biz-cover-photo'
95
94
  };
96
95
  export const MEDIA_HKDF_KEY_MAPPING = {
97
- audio: 'Audio',
98
- document: 'Document',
99
- gif: 'Video',
100
- image: 'Image',
101
- ppic: '',
102
- product: 'Image',
103
- ptt: 'Audio',
104
- sticker: 'Image',
105
- video: 'Video',
106
- 'thumbnail-document': 'Document Thumbnail',
107
- 'thumbnail-image': 'Image Thumbnail',
108
- 'thumbnail-video': 'Video Thumbnail',
109
- 'thumbnail-link': 'Link Thumbnail',
110
- 'md-msg-hist': 'History',
111
- 'md-app-state': 'App State',
112
- 'product-catalog-image': '',
113
- 'payment-bg-image': 'Payment Background',
114
- ptv: 'Video',
115
- 'biz-cover-photo': 'Image'
96
+ audio: 'Audio',
97
+ document: 'Document',
98
+ gif: 'Video',
99
+ image: 'Image',
100
+ ppic: '',
101
+ product: 'Image',
102
+ ptt: 'Audio',
103
+ sticker: 'Image',
104
+ video: 'Video',
105
+ 'thumbnail-document': 'Document Thumbnail',
106
+ 'thumbnail-image': 'Image Thumbnail',
107
+ 'thumbnail-video': 'Video Thumbnail',
108
+ 'thumbnail-link': 'Link Thumbnail',
109
+ 'md-msg-hist': 'History',
110
+ 'md-app-state': 'App State',
111
+ 'product-catalog-image': '',
112
+ 'payment-bg-image': 'Payment Background',
113
+ ptv: 'Video',
114
+ 'biz-cover-photo': 'Image'
116
115
  };
117
116
  export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
118
- /** 120s timeout for history sync stall detection, same as WA Web's handleChunkProgress / restartPausedTimer (g = 120) */
119
117
  export const HISTORY_SYNC_PAUSED_TIMEOUT_MS = 120000;
120
118
  export const MIN_PREKEY_COUNT = 5;
121
119
  export const INITIAL_PREKEY_COUNT = 812;
122
- export const UPLOAD_TIMEOUT = 30000; // 30 seconds
120
+ export const UPLOAD_TIMEOUT = 30000;
123
121
  export const TimeMs = {
124
- Minute: 60 * 1000,
125
- Hour: 60 * 60 * 1000,
126
- Day: 24 * 60 * 60 * 1000,
127
- Week: 7 * 24 * 60 * 60 * 1000
122
+ Minute: 60 * 1000,
123
+ Hour: 60 * 60 * 1000,
124
+ Day: 24 * 60 * 60 * 1000,
125
+ Week: 7 * 24 * 60 * 60 * 1000
128
126
  };
129
- //# sourceMappingURL=index.js.map
@@ -1,12 +1,11 @@
1
1
  export class CiphertextMessage {
2
- constructor() {
3
- this.UNSUPPORTED_VERSION = 1;
4
- this.CURRENT_VERSION = 3;
5
- this.WHISPER_TYPE = 2;
6
- this.PREKEY_TYPE = 3;
7
- this.SENDERKEY_TYPE = 4;
8
- this.SENDERKEY_DISTRIBUTION_TYPE = 5;
9
- this.ENCRYPTED_MESSAGE_OVERHEAD = 53;
10
- }
2
+ constructor() {
3
+ this.UNSUPPORTED_VERSION = 1;
4
+ this.CURRENT_VERSION = 3;
5
+ this.WHISPER_TYPE = 2;
6
+ this.PREKEY_TYPE = 3;
7
+ this.SENDERKEY_TYPE = 4;
8
+ this.SENDERKEY_DISTRIBUTION_TYPE = 5;
9
+ this.ENCRYPTED_MESSAGE_OVERHEAD = 53;
10
+ }
11
11
  }
12
- //# sourceMappingURL=ciphertext-message.js.map
@@ -3,28 +3,27 @@ import { SenderKeyDistributionMessage } from './sender-key-distribution-message.
3
3
  import { SenderKeyName } from './sender-key-name.js';
4
4
  import { SenderKeyRecord } from './sender-key-record.js';
5
5
  export class GroupSessionBuilder {
6
- constructor(senderKeyStore) {
7
- this.senderKeyStore = senderKeyStore;
6
+ constructor(senderKeyStore) {
7
+ this.senderKeyStore = senderKeyStore;
8
+ }
9
+ async process(senderKeyName, senderKeyDistributionMessage) {
10
+ const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
11
+ senderKeyRecord.addSenderKeyState(senderKeyDistributionMessage.getId(), senderKeyDistributionMessage.getIteration(), senderKeyDistributionMessage.getChainKey(), senderKeyDistributionMessage.getSignatureKey());
12
+ await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
13
+ }
14
+ async create(senderKeyName) {
15
+ const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
16
+ if (senderKeyRecord.isEmpty()) {
17
+ const keyId = keyhelper.generateSenderKeyId();
18
+ const senderKey = keyhelper.generateSenderKey();
19
+ const signingKey = keyhelper.generateSenderSigningKey();
20
+ senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey);
21
+ await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
8
22
  }
9
- async process(senderKeyName, senderKeyDistributionMessage) {
10
- const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
11
- senderKeyRecord.addSenderKeyState(senderKeyDistributionMessage.getId(), senderKeyDistributionMessage.getIteration(), senderKeyDistributionMessage.getChainKey(), senderKeyDistributionMessage.getSignatureKey());
12
- await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
13
- }
14
- async create(senderKeyName) {
15
- const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
16
- if (senderKeyRecord.isEmpty()) {
17
- const keyId = keyhelper.generateSenderKeyId();
18
- const senderKey = keyhelper.generateSenderKey();
19
- const signingKey = keyhelper.generateSenderSigningKey();
20
- senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey);
21
- await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
22
- }
23
- const state = senderKeyRecord.getSenderKeyState();
24
- if (!state) {
25
- throw new Error('No session state available');
26
- }
27
- return new SenderKeyDistributionMessage(state.getKeyId(), state.getSenderChainKey().getIteration(), state.getSenderChainKey().getSeed(), state.getSigningKeyPublic());
23
+ const state = senderKeyRecord.getSenderKeyState();
24
+ if (!state) {
25
+ throw new Error('No session state available');
28
26
  }
27
+ return new SenderKeyDistributionMessage(state.getKeyId(), state.getSenderChainKey().getIteration(), state.getSenderChainKey().getSeed(), state.getSigningKeyPublic());
28
+ }
29
29
  }
30
- //# sourceMappingURL=group-session-builder.js.map
@@ -1,82 +1,88 @@
1
- import { decrypt, encrypt } from 'libsignal/src/crypto.js';
1
+ import { createCipheriv, createDecipheriv } from 'crypto';
2
2
  import { SenderKeyMessage } from './sender-key-message.js';
3
3
  import { SenderKeyName } from './sender-key-name.js';
4
4
  import { SenderKeyRecord } from './sender-key-record.js';
5
5
  import { SenderKeyState } from './sender-key-state.js';
6
6
  export class GroupCipher {
7
- constructor(senderKeyStore, senderKeyName) {
8
- this.senderKeyStore = senderKeyStore;
9
- this.senderKeyName = senderKeyName;
7
+ constructor(senderKeyStore, senderKeyName) {
8
+ this.senderKeyStore = senderKeyStore;
9
+ this.senderKeyName = senderKeyName;
10
+ }
11
+ async encrypt(paddedPlaintext) {
12
+ const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
13
+ if (!record) {
14
+ throw new Error('No SenderKeyRecord found for encryption');
10
15
  }
11
- async encrypt(paddedPlaintext) {
12
- const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
13
- if (!record) {
14
- throw new Error('No SenderKeyRecord found for encryption');
15
- }
16
- const senderKeyState = record.getSenderKeyState();
17
- if (!senderKeyState) {
18
- throw new Error('No session to encrypt message');
19
- }
20
- const iteration = senderKeyState.getSenderChainKey().getIteration();
21
- const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
22
- const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
23
- const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
24
- await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
25
- return senderKeyMessage.serialize();
16
+ const senderKeyState = record.getSenderKeyState();
17
+ if (!senderKeyState) {
18
+ throw new Error('No session to encrypt message');
26
19
  }
27
- async decrypt(senderKeyMessageBytes) {
28
- const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
29
- if (!record) {
30
- throw new Error('No SenderKeyRecord found for decryption');
31
- }
32
- const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
33
- const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
34
- if (!senderKeyState) {
35
- throw new Error('No session found to decrypt message');
36
- }
37
- senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
38
- const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
39
- const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
40
- await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
41
- return plaintext;
20
+ const iteration = senderKeyState.getSenderChainKey().getIteration();
21
+ const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
22
+ const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
23
+ const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
24
+ await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
25
+ return senderKeyMessage.serialize();
26
+ }
27
+ async decrypt(senderKeyMessageBytes) {
28
+ const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
29
+ if (!record) {
30
+ throw new Error('No SenderKeyRecord found for decryption');
42
31
  }
43
- getSenderKey(senderKeyState, iteration) {
44
- let senderChainKey = senderKeyState.getSenderChainKey();
45
- if (senderChainKey.getIteration() > iteration) {
46
- if (senderKeyState.hasSenderMessageKey(iteration)) {
47
- const messageKey = senderKeyState.removeSenderMessageKey(iteration);
48
- if (!messageKey) {
49
- throw new Error('No sender message key found for iteration');
50
- }
51
- return messageKey;
52
- }
53
- throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
54
- }
55
- if (iteration - senderChainKey.getIteration() > 2000) {
56
- throw new Error('Over 2000 messages into the future!');
57
- }
58
- while (senderChainKey.getIteration() < iteration) {
59
- senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
60
- senderChainKey = senderChainKey.getNext();
61
- }
62
- senderKeyState.setSenderChainKey(senderChainKey.getNext());
63
- return senderChainKey.getSenderMessageKey();
32
+ const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
33
+ const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
34
+ if (!senderKeyState) {
35
+ throw new Error('No session found to decrypt message');
64
36
  }
65
- async getPlainText(iv, key, ciphertext) {
66
- try {
67
- return decrypt(key, ciphertext, iv);
68
- }
69
- catch (e) {
70
- throw new Error('InvalidMessageException');
37
+ senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
38
+ const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
39
+ const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
40
+ await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
41
+ return plaintext;
42
+ }
43
+ getSenderKey(senderKeyState, iteration) {
44
+ let senderChainKey = senderKeyState.getSenderChainKey();
45
+ if (senderChainKey.getIteration() > iteration) {
46
+ if (senderKeyState.hasSenderMessageKey(iteration)) {
47
+ const messageKey = senderKeyState.removeSenderMessageKey(iteration);
48
+ if (!messageKey) {
49
+ throw new Error('No sender message key found for iteration');
71
50
  }
51
+ return messageKey;
52
+ }
53
+ throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
72
54
  }
73
- async getCipherText(iv, key, plaintext) {
74
- try {
75
- return encrypt(key, plaintext, iv);
76
- }
77
- catch (e) {
78
- throw new Error('InvalidMessageException');
79
- }
55
+ if (iteration - senderChainKey.getIteration() > 2000) {
56
+ throw new Error('Over 2000 messages into the future!');
57
+ }
58
+ while (senderChainKey.getIteration() < iteration) {
59
+ senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
60
+ senderChainKey = senderChainKey.getNext();
61
+ }
62
+ senderKeyState.setSenderChainKey(senderChainKey.getNext());
63
+ return senderChainKey.getSenderMessageKey();
64
+ }
65
+ async getPlainText(iv, key, ciphertext) {
66
+ try {
67
+ return aesDecryptWithIV(ciphertext, key, iv);
68
+ } catch (e) {
69
+ throw new Error('InvalidMessageException');
80
70
  }
71
+ }
72
+ async getCipherText(iv, key, plaintext) {
73
+ try {
74
+ return aesEncryptWithIV(plaintext, key, iv);
75
+ } catch (e) {
76
+ throw new Error('InvalidMessageException');
77
+ }
78
+ }
79
+ }
80
+ const toBuf = v => Buffer.isBuffer(v) ? v : Buffer.from(v.buffer ?? v, v.byteOffset ?? 0, v.byteLength ?? v.length);
81
+ function aesDecryptWithIV(buffer, key, iv) {
82
+ const decipher = createDecipheriv('aes-256-cbc', toBuf(key), toBuf(iv));
83
+ return Buffer.concat([decipher.update(toBuf(buffer)), decipher.final()]);
84
+ }
85
+ function aesEncryptWithIV(buffer, key, iv) {
86
+ const cipher = createCipheriv('aes-256-cbc', toBuf(key), toBuf(iv));
87
+ return Buffer.concat([cipher.update(toBuf(buffer)), cipher.final()]);
81
88
  }
82
- //# sourceMappingURL=group_cipher.js.map
@@ -9,4 +9,3 @@ export { SenderMessageKey } from './sender-message-key.js';
9
9
  export { SenderChainKey } from './sender-chain-key.js';
10
10
  export { CiphertextMessage } from './ciphertext-message.js';
11
11
  export * as keyhelper from './keyhelper.js';
12
- //# sourceMappingURL=index.js.map
@@ -1,18 +1,17 @@
1
1
  import * as nodeCrypto from 'crypto';
2
2
  import { generateKeyPair } from 'libsignal/src/curve.js';
3
3
  export function generateSenderKey() {
4
- return nodeCrypto.randomBytes(32);
4
+ return nodeCrypto.randomBytes(32);
5
5
  }
6
6
  export function generateSenderKeyId() {
7
- return nodeCrypto.randomInt(2147483647);
7
+ return nodeCrypto.randomInt(2147483647);
8
8
  }
9
9
  export function generateSenderSigningKey(key) {
10
- if (!key) {
11
- key = generateKeyPair();
12
- }
13
- return {
14
- public: Buffer.from(key.pubKey),
15
- private: Buffer.from(key.privKey)
16
- };
10
+ if (!key) {
11
+ key = generateKeyPair();
12
+ }
13
+ return {
14
+ public: Buffer.from(key.pubKey),
15
+ private: Buffer.from(key.privKey)
16
+ };
17
17
  }
18
- //# sourceMappingURL=keyhelper.js.map
@@ -1,26 +1,25 @@
1
1
  import { calculateMAC } from 'libsignal/src/crypto.js';
2
2
  import { SenderMessageKey } from './sender-message-key.js';
3
3
  export class SenderChainKey {
4
- constructor(iteration, chainKey) {
5
- this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
6
- this.CHAIN_KEY_SEED = Buffer.from([0x02]);
7
- this.iteration = iteration;
8
- this.chainKey = Buffer.from(chainKey);
9
- }
10
- getIteration() {
11
- return this.iteration;
12
- }
13
- getSenderMessageKey() {
14
- return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
15
- }
16
- getNext() {
17
- return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
18
- }
19
- getSeed() {
20
- return this.chainKey;
21
- }
22
- getDerivative(seed, key) {
23
- return calculateMAC(key, seed);
24
- }
4
+ constructor(iteration, chainKey) {
5
+ this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
6
+ this.CHAIN_KEY_SEED = Buffer.from([0x02]);
7
+ this.iteration = iteration;
8
+ this.chainKey = Buffer.from(chainKey);
9
+ }
10
+ getIteration() {
11
+ return this.iteration;
12
+ }
13
+ getSenderMessageKey() {
14
+ return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
15
+ }
16
+ getNext() {
17
+ return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
18
+ }
19
+ getSeed() {
20
+ return this.chainKey;
21
+ }
22
+ getDerivative(seed, key) {
23
+ return calculateMAC(key, seed);
24
+ }
25
25
  }
26
- //# sourceMappingURL=sender-chain-key.js.map
@@ -1,63 +1,54 @@
1
- import { proto } from '../../../WAProto/index.js';
1
+ import { proto } from '../../../WAProto/compiler.js';
2
2
  import { CiphertextMessage } from './ciphertext-message.js';
3
3
  export class SenderKeyDistributionMessage extends CiphertextMessage {
4
- constructor(id, iteration, chainKey, signatureKey, serialized) {
5
- super();
6
- if (serialized) {
7
- try {
8
- const message = serialized.slice(1);
9
- const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
10
- this.serialized = serialized;
11
- this.id = distributionMessage.id;
12
- this.iteration = distributionMessage.iteration;
13
- this.chainKey =
14
- typeof distributionMessage.chainKey === 'string'
15
- ? Buffer.from(distributionMessage.chainKey, 'base64')
16
- : distributionMessage.chainKey;
17
- this.signatureKey =
18
- typeof distributionMessage.signingKey === 'string'
19
- ? Buffer.from(distributionMessage.signingKey, 'base64')
20
- : distributionMessage.signingKey;
21
- }
22
- catch (e) {
23
- throw new Error(String(e));
24
- }
25
- }
26
- else {
27
- const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
28
- this.id = id;
29
- this.iteration = iteration;
30
- this.chainKey = chainKey;
31
- this.signatureKey = signatureKey;
32
- const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
33
- id,
34
- iteration,
35
- chainKey,
36
- signingKey: this.signatureKey
37
- })).finish();
38
- this.serialized = Buffer.concat([Buffer.from([version]), message]);
39
- }
40
- }
41
- intsToByteHighAndLow(highValue, lowValue) {
42
- return (((highValue << 4) | lowValue) & 0xff) % 256;
43
- }
44
- serialize() {
45
- return this.serialized;
46
- }
47
- getType() {
48
- return this.SENDERKEY_DISTRIBUTION_TYPE;
49
- }
50
- getIteration() {
51
- return this.iteration;
52
- }
53
- getChainKey() {
54
- return this.chainKey;
55
- }
56
- getSignatureKey() {
57
- return this.signatureKey;
58
- }
59
- getId() {
60
- return this.id;
61
- }
4
+ constructor(id, iteration, chainKey, signatureKey, serialized) {
5
+ super();
6
+ if (serialized) {
7
+ try {
8
+ const message = serialized.slice(1);
9
+ const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
10
+ this.serialized = serialized;
11
+ this.id = distributionMessage.id;
12
+ this.iteration = distributionMessage.iteration;
13
+ this.chainKey = typeof distributionMessage.chainKey === 'string' ? Buffer.from(distributionMessage.chainKey, 'base64') : distributionMessage.chainKey;
14
+ this.signatureKey = typeof distributionMessage.signingKey === 'string' ? Buffer.from(distributionMessage.signingKey, 'base64') : distributionMessage.signingKey;
15
+ } catch (e) {
16
+ throw new Error(String(e));
17
+ }
18
+ } else {
19
+ const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
20
+ this.id = id;
21
+ this.iteration = iteration;
22
+ this.chainKey = chainKey;
23
+ this.signatureKey = signatureKey;
24
+ const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
25
+ id,
26
+ iteration,
27
+ chainKey,
28
+ signingKey: this.signatureKey
29
+ })).finish();
30
+ this.serialized = Buffer.concat([Buffer.from([version]), message]);
31
+ }
32
+ }
33
+ intsToByteHighAndLow(highValue, lowValue) {
34
+ return ((highValue << 4 | lowValue) & 0xff) % 256;
35
+ }
36
+ serialize() {
37
+ return this.serialized;
38
+ }
39
+ getType() {
40
+ return this.SENDERKEY_DISTRIBUTION_TYPE;
41
+ }
42
+ getIteration() {
43
+ return this.iteration;
44
+ }
45
+ getChainKey() {
46
+ return this.chainKey;
47
+ }
48
+ getSignatureKey() {
49
+ return this.signatureKey;
50
+ }
51
+ getId() {
52
+ return this.id;
53
+ }
62
54
  }
63
- //# sourceMappingURL=sender-key-distribution-message.js.map