@mrlegendbot/baileys 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.upstream.md +1312 -0
- package/WAProto/WAProto.json +1 -0
- package/WAProto/WAProto.proto +6275 -0
- package/WAProto/compiler.js +550 -0
- package/WAProto/index.js +2 -97691
- package/lib/Defaults/index.js +89 -92
- package/lib/Signal/Group/ciphertext-message.js +9 -10
- package/lib/Signal/Group/group-session-builder.js +21 -22
- package/lib/Signal/Group/group_cipher.js +75 -69
- package/lib/Signal/Group/index.js +0 -1
- package/lib/Signal/Group/keyhelper.js +9 -10
- package/lib/Signal/Group/sender-chain-key.js +21 -22
- package/lib/Signal/Group/sender-key-distribution-message.js +51 -60
- package/lib/Signal/Group/sender-key-message.js +56 -62
- package/lib/Signal/Group/sender-key-name.js +38 -40
- package/lib/Signal/Group/sender-key-record.js +33 -34
- package/lib/Signal/Group/sender-key-state.js +72 -76
- package/lib/Signal/Group/sender-message-key.js +22 -23
- package/lib/Signal/libsignal.js +506 -405
- package/lib/Signal/lid-mapping.js +276 -258
- package/lib/Socket/Client/index.js +0 -1
- package/lib/Socket/Client/types.js +6 -7
- package/lib/Socket/Client/websocket.js +48 -49
- package/lib/Socket/business.js +349 -369
- package/lib/Socket/chats.js +1172 -1148
- package/lib/Socket/communities.js +473 -413
- package/lib/Socket/groups.js +387 -331
- package/lib/Socket/index.js +6 -8
- package/lib/Socket/messages-recv.js +1782 -1719
- package/lib/Socket/messages-send.js +1503 -2450
- package/lib/Socket/mex.js +40 -35
- package/lib/Socket/newsletter.js +239 -191
- package/lib/Socket/socket.js +992 -923
- package/lib/Store/index.js +8 -0
- package/lib/Store/keyed-db.js +108 -0
- package/lib/Store/make-cache-manager-store.js +70 -0
- package/lib/Store/make-in-memory-store.js +417 -0
- package/lib/Store/make-ordered-dictionary.js +69 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +0 -1
- package/lib/Types/Bussines.js +0 -1
- package/lib/Types/Call.js +0 -1
- package/lib/Types/Chat.js +1 -8
- package/lib/Types/Contact.js +0 -1
- package/lib/Types/Events.js +1 -2
- package/lib/Types/GroupMetadata.js +0 -1
- package/lib/Types/Label.js +20 -22
- package/lib/Types/LabelAssociation.js +2 -4
- package/lib/Types/Message.js +3 -5
- package/lib/Types/Mex.js +26 -31
- package/lib/Types/Product.js +0 -1
- package/lib/Types/Signal.js +1 -2
- package/lib/Types/Socket.js +2 -3
- package/lib/Types/State.js +4 -52
- package/lib/Types/USync.js +0 -1
- package/lib/Types/index.js +10 -11
- package/lib/Utils/auth-utils.js +240 -277
- package/lib/Utils/browser-utils.js +20 -23
- package/lib/Utils/business.js +200 -204
- package/lib/Utils/chat-utils.js +836 -810
- package/lib/Utils/companion-reg-client-utils.js +23 -25
- package/lib/Utils/crypto.js +70 -86
- package/lib/Utils/decode-wa-message.js +277 -284
- package/lib/Utils/event-buffer.js +592 -595
- package/lib/Utils/generics.js +312 -335
- package/lib/Utils/history.js +123 -115
- package/lib/Utils/index.js +1 -2
- package/lib/Utils/link-preview.js +74 -73
- package/lib/Utils/logger.js +3 -2
- package/lib/Utils/lt-hash.js +0 -6
- package/lib/Utils/make-mutex.js +27 -27
- package/lib/Utils/message-retry-manager.js +213 -254
- package/lib/Utils/messages-media.js +741 -715
- package/lib/Utils/messages.js +791 -827
- package/lib/Utils/noise-handler.js +215 -187
- package/lib/Utils/pre-key-manager.js +69 -98
- package/lib/Utils/process-message.js +617 -586
- package/lib/Utils/reporting-utils.js +553 -234
- package/lib/Utils/signal.js +217 -170
- package/lib/Utils/stanza-ack.js +27 -35
- package/lib/Utils/sync-action-utils.js +37 -41
- package/lib/Utils/tc-token-utils.js +120 -141
- package/lib/Utils/use-multi-file-auth-state.js +87 -105
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/Utils/validate-connection.js +190 -188
- package/lib/WABinary/constants.js +28 -1294
- package/lib/WABinary/decode.js +245 -249
- package/lib/WABinary/encode.js +201 -206
- package/lib/WABinary/generic-utils.js +86 -89
- package/lib/WABinary/index.js +0 -1
- package/lib/WABinary/jid-utils.js +68 -77
- package/lib/WABinary/types.js +0 -1
- package/lib/WAM/BinaryInfo.js +7 -8
- package/lib/WAM/constants.js +19892 -22853
- package/lib/WAM/encode.js +121 -132
- package/lib/WAM/index.js +0 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +46 -43
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +47 -48
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +22 -23
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +30 -33
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +19 -20
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -44
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -24
- package/lib/WAUSync/Protocols/index.js +0 -1
- package/lib/WAUSync/USyncQuery.js +79 -88
- package/lib/WAUSync/USyncUser.js +28 -29
- package/lib/WAUSync/index.js +0 -1
- package/lib/index.js +2 -2
- package/package.json +22 -86
- package/WAProto/GenerateStatics.sh +0 -3
- package/WAProto/fix-imports.js +0 -85
- package/WAProto/index.d.ts +0 -14017
- package/lib/Defaults/index.d.ts +0 -75
- package/lib/Signal/Group/ciphertext-message.d.ts +0 -10
- package/lib/Signal/Group/group-session-builder.d.ts +0 -15
- package/lib/Signal/Group/group_cipher.d.ts +0 -17
- package/lib/Signal/Group/index.d.ts +0 -12
- package/lib/Signal/Group/keyhelper.d.ts +0 -11
- package/lib/Signal/Group/sender-chain-key.d.ts +0 -14
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -17
- package/lib/Signal/Group/sender-key-message.d.ts +0 -19
- package/lib/Signal/Group/sender-key-name.d.ts +0 -18
- package/lib/Signal/Group/sender-key-record.d.ts +0 -31
- package/lib/Signal/Group/sender-key-state.d.ts +0 -39
- package/lib/Signal/Group/sender-message-key.d.ts +0 -12
- package/lib/Signal/libsignal.d.ts +0 -5
- package/lib/Signal/lid-mapping.d.ts +0 -23
- package/lib/Socket/Client/index.d.ts +0 -3
- package/lib/Socket/Client/types.d.ts +0 -16
- package/lib/Socket/Client/websocket.d.ts +0 -13
- package/lib/Socket/business.d.ts +0 -217
- package/lib/Socket/chats.d.ts +0 -125
- package/lib/Socket/communities.d.ts +0 -273
- package/lib/Socket/groups.d.ts +0 -161
- package/lib/Socket/index.d.ts +0 -260
- package/lib/Socket/messages-recv.d.ts +0 -203
- package/lib/Socket/messages-send.d.ts +0 -199
- package/lib/Socket/mex.d.ts +0 -3
- package/lib/Socket/newsletter.d.ts +0 -170
- package/lib/Socket/socket.d.ts +0 -59
- package/lib/Types/Auth.d.ts +0 -117
- package/lib/Types/Bussines.d.ts +0 -25
- package/lib/Types/Call.d.ts +0 -15
- package/lib/Types/Chat.d.ts +0 -124
- package/lib/Types/Contact.d.ts +0 -26
- package/lib/Types/Events.d.ts +0 -256
- package/lib/Types/GroupMetadata.d.ts +0 -71
- package/lib/Types/Label.d.ts +0 -47
- package/lib/Types/LabelAssociation.d.ts +0 -30
- package/lib/Types/Message.d.ts +0 -320
- package/lib/Types/Mex.d.ts +0 -141
- package/lib/Types/Product.d.ts +0 -79
- package/lib/Types/Signal.d.ts +0 -87
- package/lib/Types/Socket.d.ts +0 -136
- package/lib/Types/State.d.ts +0 -97
- package/lib/Types/USync.d.ts +0 -26
- package/lib/Types/index.d.ts +0 -66
- package/lib/Utils/auth-utils.d.ts +0 -24
- package/lib/Utils/browser-utils.d.ts +0 -4
- package/lib/Utils/business.d.ts +0 -23
- package/lib/Utils/chat-utils.d.ts +0 -100
- package/lib/Utils/companion-reg-client-utils.d.ts +0 -17
- package/lib/Utils/crypto.d.ts +0 -37
- package/lib/Utils/decode-wa-message.d.ts +0 -66
- package/lib/Utils/event-buffer.d.ts +0 -36
- package/lib/Utils/generics.d.ts +0 -92
- package/lib/Utils/history.d.ts +0 -24
- package/lib/Utils/identity-change-handler.d.ts +0 -44
- package/lib/Utils/identity-change-handler.js +0 -50
- package/lib/Utils/index.d.ts +0 -22
- package/lib/Utils/link-preview.d.ts +0 -21
- package/lib/Utils/logger.d.ts +0 -12
- package/lib/Utils/lt-hash.d.ts +0 -8
- package/lib/Utils/make-mutex.d.ts +0 -9
- package/lib/Utils/message-retry-manager.d.ts +0 -115
- package/lib/Utils/messages-media.d.ts +0 -133
- package/lib/Utils/messages.d.ts +0 -91
- package/lib/Utils/noise-handler.d.ts +0 -20
- package/lib/Utils/offline-node-processor.d.ts +0 -17
- package/lib/Utils/offline-node-processor.js +0 -40
- package/lib/Utils/pre-key-manager.d.ts +0 -28
- package/lib/Utils/process-message.d.ts +0 -60
- package/lib/Utils/reporting-utils.d.ts +0 -11
- package/lib/Utils/signal.d.ts +0 -47
- package/lib/Utils/stanza-ack.d.ts +0 -11
- package/lib/Utils/sync-action-utils.d.ts +0 -19
- package/lib/Utils/tc-token-utils.d.ts +0 -37
- package/lib/Utils/use-multi-file-auth-state.d.ts +0 -13
- package/lib/Utils/validate-connection.d.ts +0 -11
- package/lib/WABinary/constants.d.ts +0 -28
- package/lib/WABinary/decode.d.ts +0 -7
- package/lib/WABinary/encode.d.ts +0 -3
- package/lib/WABinary/generic-utils.d.ts +0 -15
- package/lib/WABinary/index.d.ts +0 -6
- package/lib/WABinary/jid-utils.d.ts +0 -48
- package/lib/WABinary/types.d.ts +0 -19
- package/lib/WAM/BinaryInfo.d.ts +0 -9
- package/lib/WAM/constants.d.ts +0 -40
- package/lib/WAM/encode.d.ts +0 -3
- package/lib/WAM/index.d.ts +0 -4
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +0 -23
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +0 -13
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +0 -26
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +0 -10
- package/lib/WAUSync/Protocols/index.d.ts +0 -6
- package/lib/WAUSync/USyncQuery.d.ts +0 -30
- package/lib/WAUSync/USyncUser.d.ts +0 -17
- package/lib/WAUSync/index.d.ts +0 -4
- package/lib/index.d.ts +0 -12
package/lib/Defaults/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
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,
|
|
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]);
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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;
|
|
120
|
+
export const UPLOAD_TIMEOUT = 30000;
|
|
123
121
|
export const TimeMs = {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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 {
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
|
@@ -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
|
-
|
|
4
|
+
return nodeCrypto.randomBytes(32);
|
|
5
5
|
}
|
|
6
6
|
export function generateSenderKeyId() {
|
|
7
|
-
|
|
7
|
+
return nodeCrypto.randomInt(2147483647);
|
|
8
8
|
}
|
|
9
9
|
export function generateSenderSigningKey(key) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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/
|
|
1
|
+
import { proto } from '../../../WAProto/compiler.js';
|
|
2
2
|
import { CiphertextMessage } from './ciphertext-message.js';
|
|
3
3
|
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|