@baileys-md/baileys 10.1.0 → 11.0.0
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/LICENSE +1 -1
- package/README.md +1303 -2
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +4633 -0
- package/WAProto/fix-imports.js +29 -0
- package/WAProto/index.js +13516 -4182
- package/lib/Defaults/baileys-version.js +1 -0
- package/lib/Defaults/index.js +51 -72
- package/lib/Signal/Group/ciphertext-message.js +12 -0
- package/lib/Signal/Group/group-session-builder.js +30 -0
- package/lib/Signal/Group/group_cipher.js +94 -0
- package/lib/Signal/Group/index.js +12 -0
- package/lib/Signal/Group/keyhelper.js +19 -0
- package/lib/Signal/Group/queue-job.js +54 -0
- package/lib/Signal/Group/sender-chain-key.js +32 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +63 -0
- package/lib/Signal/Group/sender-key-message.js +67 -0
- package/lib/Signal/Group/sender-key-name.js +48 -0
- package/lib/Signal/Group/sender-key-record.js +50 -0
- package/lib/Signal/Group/sender-key-state.js +96 -0
- package/{WASignalGroup/sender_message_key.js → lib/Signal/Group/sender-message-key.js} +4 -16
- package/lib/Signal/libsignal.js +41 -61
- package/lib/Socket/Client/index.js +3 -19
- package/lib/Socket/Client/types.js +11 -0
- package/lib/Socket/Client/websocket.js +50 -0
- package/lib/Socket/business.js +37 -42
- package/lib/Socket/chats.js +194 -187
- package/lib/Socket/communities.js +351 -0
- package/lib/Socket/groups.js +87 -90
- package/lib/Socket/index.js +7 -8
- package/lib/Socket/messages-recv.js +360 -335
- package/lib/Socket/messages-send.js +156 -279
- package/lib/Socket/mex.js +42 -0
- package/lib/Socket/newsletter.js +144 -213
- package/lib/Socket/socket.js +128 -161
- package/lib/Socket/usync.js +19 -26
- package/lib/Types/Auth.js +2 -2
- package/lib/Types/Call.js +2 -2
- package/lib/Types/Chat.js +8 -4
- package/lib/Types/Contact.js +2 -2
- package/lib/Types/Events.js +2 -2
- package/lib/Types/GroupMetadata.js +2 -2
- package/lib/Types/Label.js +3 -5
- package/lib/Types/LabelAssociation.js +3 -5
- package/lib/Types/Message.js +7 -7
- package/lib/Types/Newsletter.js +30 -17
- package/lib/Types/Product.js +2 -2
- package/lib/Types/Signal.js +2 -2
- package/lib/Types/Socket.js +3 -2
- package/lib/Types/State.js +2 -2
- package/lib/Types/USync.js +2 -2
- package/lib/Types/index.js +15 -31
- package/lib/Utils/auth-utils.js +31 -47
- package/lib/Utils/baileys-event-stream.js +15 -22
- package/lib/Utils/business.js +66 -69
- package/lib/Utils/chat-utils.js +200 -195
- package/lib/Utils/crypto.js +70 -85
- package/lib/Utils/decode-wa-message.js +47 -51
- package/lib/Utils/event-buffer.js +36 -46
- package/lib/Utils/generics.js +116 -188
- package/lib/Utils/history.js +37 -46
- package/lib/Utils/index.js +19 -33
- package/lib/Utils/link-preview.js +14 -55
- package/lib/Utils/logger.js +3 -7
- package/lib/Utils/lt-hash.js +23 -26
- package/lib/{Store → Utils}/make-in-memory-store.js +19 -27
- package/lib/Utils/make-mutex.js +7 -10
- package/lib/{Store → Utils}/make-ordered-dictionary.js +1 -3
- package/lib/Utils/messages-media.js +236 -368
- package/lib/Utils/messages.js +278 -510
- package/lib/Utils/noise-handler.js +22 -31
- package/lib/{Store → Utils}/object-repository.js +1 -4
- package/lib/Utils/process-message.js +144 -148
- package/lib/Utils/signal.js +71 -64
- package/lib/Utils/use-multi-file-auth-state.js +112 -84
- package/lib/Utils/validate-connection.js +72 -115
- package/lib/WABinary/constants.js +1281 -20
- package/lib/WABinary/decode.js +15 -52
- package/lib/WABinary/encode.js +14 -48
- package/lib/WABinary/generic-utils.js +31 -39
- package/lib/WABinary/index.js +6 -21
- package/lib/WABinary/jid-utils.js +23 -40
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.js +2 -5
- package/lib/WAM/constants.js +2257 -2366
- package/lib/WAM/encode.js +17 -21
- package/lib/WAM/index.js +4 -19
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +8 -11
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +11 -14
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +9 -12
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +9 -13
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +20 -22
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +3 -6
- package/lib/WAUSync/Protocols/index.js +5 -20
- package/lib/WAUSync/USyncQuery.js +34 -32
- package/lib/WAUSync/USyncUser.js +2 -5
- package/lib/WAUSync/index.js +4 -19
- package/lib/index.js +11 -33
- package/package.json +25 -54
- package/WASignalGroup/GroupProtocol.js +0 -1697
- package/WASignalGroup/ciphertext_message.js +0 -16
- package/WASignalGroup/group_cipher.js +0 -120
- package/WASignalGroup/group_session_builder.js +0 -46
- package/WASignalGroup/index.js +0 -5
- package/WASignalGroup/keyhelper.js +0 -21
- package/WASignalGroup/protobufs.js +0 -3
- package/WASignalGroup/queue_job.js +0 -69
- package/WASignalGroup/sender_chain_key.js +0 -50
- package/WASignalGroup/sender_key_distribution_message.js +0 -78
- package/WASignalGroup/sender_key_message.js +0 -92
- package/WASignalGroup/sender_key_name.js +0 -70
- package/WASignalGroup/sender_key_record.js +0 -56
- package/WASignalGroup/sender_key_state.js +0 -129
- package/lib/Defaults/baileys-version.json +0 -3
- package/lib/Defaults/phonenumber-mcc.json +0 -223
- package/lib/Socket/Client/abstract-socket-client.js +0 -13
- package/lib/Socket/Client/mobile-socket-client.js +0 -65
- package/lib/Socket/Client/web-socket-client.js +0 -62
- package/lib/Socket/registration.js +0 -166
- package/lib/Store/index.js +0 -8
- package/lib/Store/make-cache-manager-store.js +0 -83
- package/lib/Store/make-mongo-store.js +0 -567
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const version = [2, 3000, 1023223821]
|
package/lib/Defaults/index.js
CHANGED
|
@@ -1,60 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
exports.MOBILE_ENDPOINT = 'g.whatsapp.net';
|
|
18
|
-
exports.MOBILE_PORT = 443;
|
|
19
|
-
exports.DEF_CALLBACK_PREFIX = 'CB:';
|
|
20
|
-
exports.DEF_TAG_PREFIX = 'TAG:';
|
|
21
|
-
exports.PHONE_CONNECTION_CB = 'CB:Pong';
|
|
22
|
-
exports.WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
|
|
23
|
-
const WA_VERSION = '2.24.6.77';
|
|
24
|
-
const WA_VERSION_HASH = (0, crypto_1.createHash)('md5').update(WA_VERSION).digest('hex');
|
|
25
|
-
exports.MOBILE_TOKEN = Buffer.from('0a1mLfGUIBVrMKF1RdvLI5lkRBvof6vn0fD2QRSM' + WA_VERSION_HASH);
|
|
26
|
-
exports.MOBILE_REGISTRATION_ENDPOINT = 'https://v.whatsapp.net/v2';
|
|
27
|
-
exports.MOBILE_USERAGENT = `Telegram Android 11.7.0 Samsung Galaxy-A7-2017`;
|
|
28
|
-
exports.REGISTRATION_PUBLIC_KEY = Buffer.from([
|
|
29
|
-
5, 142, 140, 15, 116, 195, 235, 197, 215, 166, 134, 92, 108, 60, 132, 56, 86, 176, 97, 33, 204, 232, 234, 119, 77,
|
|
30
|
-
34, 251, 111, 18, 37, 18, 48, 45,
|
|
31
|
-
]);
|
|
32
|
-
exports.NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
|
|
33
|
-
exports.DICT_VERSION = 2;
|
|
34
|
-
exports.KEY_BUNDLE_TYPE = Buffer.from([5]);
|
|
35
|
-
exports.NOISE_WA_HEADER = Buffer.from([87, 65, 6, exports.DICT_VERSION]); // last is "DICT_VERSION"
|
|
36
|
-
exports.PROTOCOL_VERSION = [5, 2];
|
|
37
|
-
exports.MOBILE_NOISE_HEADER = Buffer.concat([Buffer.from('WA'), Buffer.from(exports.PROTOCOL_VERSION)]);
|
|
1
|
+
import { proto } from '../../WAProto/index.js';
|
|
2
|
+
import { makeLibSignalRepository } from '../Signal/libsignal.js';
|
|
3
|
+
import { Browsers } from '../Utils/index.js';
|
|
4
|
+
import logger from '../Utils/logger.js';
|
|
5
|
+
import * as defaultVersion from '../Defaults/baileys-version.js'
|
|
6
|
+
const { version } = defaultVersion;
|
|
7
|
+
export const UNAUTHORIZED_CODES = [401, 403, 419];
|
|
8
|
+
export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
|
|
9
|
+
export const DEF_CALLBACK_PREFIX = 'CB:';
|
|
10
|
+
export const DEF_TAG_PREFIX = 'TAG:';
|
|
11
|
+
export const PHONE_CONNECTION_CB = 'CB:Pong';
|
|
12
|
+
export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
|
|
13
|
+
export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
|
|
14
|
+
export const DICT_VERSION = 3;
|
|
15
|
+
export const KEY_BUNDLE_TYPE = Buffer.from([5]);
|
|
16
|
+
export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]); // last is "DICT_VERSION"
|
|
38
17
|
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
SERIAL: 0
|
|
18
|
+
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
|
|
19
|
+
export const WA_CERT_DETAILS = {
|
|
20
|
+
SERIAL: 0
|
|
42
21
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
22
|
+
export const PROCESSABLE_HISTORY_TYPES = [
|
|
23
|
+
proto.Message.HistorySyncNotification.HistorySyncType.INITIAL_BOOTSTRAP,
|
|
24
|
+
proto.Message.HistorySyncNotification.HistorySyncType.PUSH_NAME,
|
|
25
|
+
proto.Message.HistorySyncNotification.HistorySyncType.RECENT,
|
|
26
|
+
proto.Message.HistorySyncNotification.HistorySyncType.FULL,
|
|
27
|
+
proto.Message.HistorySyncNotification.HistorySyncType.ON_DEMAND
|
|
49
28
|
];
|
|
50
|
-
|
|
51
|
-
version:
|
|
52
|
-
browser:
|
|
29
|
+
export const DEFAULT_CONNECTION_CONFIG = {
|
|
30
|
+
version: version,
|
|
31
|
+
browser: Browsers.ubuntu('Chrome'),
|
|
53
32
|
waWebSocketUrl: 'https://web.whatsapp.com/ws/chat',
|
|
54
33
|
connectTimeoutMs: 20000,
|
|
55
|
-
keepAliveIntervalMs:
|
|
56
|
-
logger:
|
|
57
|
-
printQRInTerminal: false,
|
|
34
|
+
keepAliveIntervalMs: 5000,
|
|
35
|
+
logger: logger.child({ class: 'baileys' }),
|
|
58
36
|
emitOwnEvents: true,
|
|
59
37
|
defaultQueryTimeoutMs: 60000,
|
|
60
38
|
customUploadHosts: [],
|
|
@@ -73,14 +51,14 @@ exports.DEFAULT_CONNECTION_CONFIG = {
|
|
|
73
51
|
options: {},
|
|
74
52
|
appStateMacVerification: {
|
|
75
53
|
patch: false,
|
|
76
|
-
snapshot: false
|
|
54
|
+
snapshot: false
|
|
77
55
|
},
|
|
78
56
|
countryCode: 'US',
|
|
79
57
|
getMessage: async () => undefined,
|
|
80
58
|
cachedGroupMetadata: async () => undefined,
|
|
81
|
-
makeSignalRepository:
|
|
59
|
+
makeSignalRepository: makeLibSignalRepository
|
|
82
60
|
};
|
|
83
|
-
|
|
61
|
+
export const MEDIA_PATH_MAP = {
|
|
84
62
|
image: '/mms/image',
|
|
85
63
|
video: '/mms/video',
|
|
86
64
|
document: '/mms/document',
|
|
@@ -89,18 +67,18 @@ exports.MEDIA_PATH_MAP = {
|
|
|
89
67
|
'thumbnail-link': '/mms/image',
|
|
90
68
|
'product-catalog-image': '/product/image',
|
|
91
69
|
'md-app-state': '',
|
|
92
|
-
'md-msg-hist': '/mms/md-app-state'
|
|
70
|
+
'md-msg-hist': '/mms/md-app-state'
|
|
93
71
|
};
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
72
|
+
export const MEDIA_HKDF_KEY_MAPPING = {
|
|
73
|
+
audio: 'Audio',
|
|
74
|
+
document: 'Document',
|
|
75
|
+
gif: 'Video',
|
|
76
|
+
image: 'Image',
|
|
77
|
+
ppic: '',
|
|
78
|
+
product: 'Image',
|
|
79
|
+
ptt: 'Audio',
|
|
80
|
+
sticker: 'Image',
|
|
81
|
+
video: 'Video',
|
|
104
82
|
'thumbnail-document': 'Document Thumbnail',
|
|
105
83
|
'thumbnail-image': 'Image Thumbnail',
|
|
106
84
|
'thumbnail-video': 'Video Thumbnail',
|
|
@@ -109,14 +87,15 @@ exports.MEDIA_HKDF_KEY_MAPPING = {
|
|
|
109
87
|
'md-app-state': 'App State',
|
|
110
88
|
'product-catalog-image': '',
|
|
111
89
|
'payment-bg-image': 'Payment Background',
|
|
112
|
-
|
|
90
|
+
ptv: 'Video'
|
|
113
91
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
92
|
+
export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
|
|
93
|
+
export const MIN_PREKEY_COUNT = 5;
|
|
94
|
+
export const INITIAL_PREKEY_COUNT = 30;
|
|
95
|
+
export const DEFAULT_CACHE_TTLS = {
|
|
118
96
|
SIGNAL_STORE: 5 * 60, // 5 minutes
|
|
119
97
|
MSG_RETRY: 60 * 60, // 1 hour
|
|
120
98
|
CALL_OFFER: 5 * 60, // 5 minutes
|
|
121
|
-
USER_DEVICES: 5 * 60
|
|
99
|
+
USER_DEVICES: 5 * 60 // 5 minutes
|
|
122
100
|
};
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=ciphertext-message.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as keyhelper from './keyhelper.js';
|
|
2
|
+
import { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
3
|
+
import { SenderKeyName } from './sender-key-name.js';
|
|
4
|
+
import { SenderKeyRecord } from './sender-key-record.js';
|
|
5
|
+
export class GroupSessionBuilder {
|
|
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);
|
|
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());
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=group-session-builder.js.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/* @ts-ignore */
|
|
2
|
+
import { decrypt, encrypt } from 'libsignal/src/crypto.js';
|
|
3
|
+
import queueJob from './queue-job.js';
|
|
4
|
+
import { SenderKeyMessage } from './sender-key-message.js';
|
|
5
|
+
import { SenderKeyName } from './sender-key-name.js';
|
|
6
|
+
import { SenderKeyRecord } from './sender-key-record.js';
|
|
7
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
8
|
+
export class GroupCipher {
|
|
9
|
+
constructor(senderKeyStore, senderKeyName) {
|
|
10
|
+
this.senderKeyStore = senderKeyStore;
|
|
11
|
+
this.senderKeyName = senderKeyName;
|
|
12
|
+
}
|
|
13
|
+
queueJob(awaitable) {
|
|
14
|
+
return queueJob(this.senderKeyName.toString(), awaitable);
|
|
15
|
+
}
|
|
16
|
+
async encrypt(paddedPlaintext) {
|
|
17
|
+
return await this.queueJob(async () => {
|
|
18
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
19
|
+
if (!record) {
|
|
20
|
+
throw new Error('No SenderKeyRecord found for encryption');
|
|
21
|
+
}
|
|
22
|
+
const senderKeyState = record.getSenderKeyState();
|
|
23
|
+
if (!senderKeyState) {
|
|
24
|
+
throw new Error('No session to encrypt message');
|
|
25
|
+
}
|
|
26
|
+
const iteration = senderKeyState.getSenderChainKey().getIteration();
|
|
27
|
+
const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
|
|
28
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
|
|
29
|
+
const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
|
|
30
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
31
|
+
return senderKeyMessage.serialize();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async decrypt(senderKeyMessageBytes) {
|
|
35
|
+
return await this.queueJob(async () => {
|
|
36
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
37
|
+
if (!record) {
|
|
38
|
+
throw new Error('No SenderKeyRecord found for decryption');
|
|
39
|
+
}
|
|
40
|
+
const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
|
|
41
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
|
|
42
|
+
if (!senderKeyState) {
|
|
43
|
+
throw new Error('No session found to decrypt message');
|
|
44
|
+
}
|
|
45
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
|
|
46
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
|
|
47
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
|
48
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
49
|
+
return plaintext;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
getSenderKey(senderKeyState, iteration) {
|
|
53
|
+
let senderChainKey = senderKeyState.getSenderChainKey();
|
|
54
|
+
if (senderChainKey.getIteration() > iteration) {
|
|
55
|
+
if (senderKeyState.hasSenderMessageKey(iteration)) {
|
|
56
|
+
const messageKey = senderKeyState.removeSenderMessageKey(iteration);
|
|
57
|
+
if (!messageKey) {
|
|
58
|
+
throw new Error('No sender message key found for iteration');
|
|
59
|
+
}
|
|
60
|
+
return messageKey;
|
|
61
|
+
}
|
|
62
|
+
throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
|
|
63
|
+
}
|
|
64
|
+
if (iteration - senderChainKey.getIteration() > 2000) {
|
|
65
|
+
throw new Error('Over 2000 messages into the future!');
|
|
66
|
+
}
|
|
67
|
+
while (senderChainKey.getIteration() < iteration) {
|
|
68
|
+
senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
|
|
69
|
+
senderChainKey = senderChainKey.getNext();
|
|
70
|
+
}
|
|
71
|
+
senderKeyState.setSenderChainKey(senderChainKey.getNext());
|
|
72
|
+
return senderChainKey.getSenderMessageKey();
|
|
73
|
+
}
|
|
74
|
+
async getPlainText(iv, key, ciphertext) {
|
|
75
|
+
try {
|
|
76
|
+
return decrypt(key, ciphertext, iv);
|
|
77
|
+
}
|
|
78
|
+
catch (e) {
|
|
79
|
+
throw new Error('InvalidMessageException');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async getCipherText(iv, key, plaintext) {
|
|
83
|
+
try {
|
|
84
|
+
const ivBuffer = typeof iv === 'string' ? Buffer.from(iv, 'base64') : iv;
|
|
85
|
+
const keyBuffer = typeof key === 'string' ? Buffer.from(key, 'base64') : key;
|
|
86
|
+
const plaintextBuffer = typeof plaintext === 'string' ? Buffer.from(plaintext) : plaintext;
|
|
87
|
+
return encrypt(keyBuffer, plaintextBuffer, ivBuffer);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
throw new Error('InvalidMessageException');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=group_cipher.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { GroupSessionBuilder } from './group-session-builder.js';
|
|
2
|
+
export { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
3
|
+
export { SenderKeyRecord } from './sender-key-record.js';
|
|
4
|
+
export { SenderKeyName } from './sender-key-name.js';
|
|
5
|
+
export { GroupCipher } from './group_cipher.js';
|
|
6
|
+
export { SenderKeyState } from './sender-key-state.js';
|
|
7
|
+
export { SenderKeyMessage } from './sender-key-message.js';
|
|
8
|
+
export { SenderMessageKey } from './sender-message-key.js';
|
|
9
|
+
export { SenderChainKey } from './sender-chain-key.js';
|
|
10
|
+
export { CiphertextMessage } from './ciphertext-message.js';
|
|
11
|
+
export * as keyhelper from './keyhelper.js';
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as nodeCrypto from 'crypto';
|
|
2
|
+
/* @ts-ignore */
|
|
3
|
+
import { generateKeyPair } from 'libsignal/src/curve.js';
|
|
4
|
+
export function generateSenderKey() {
|
|
5
|
+
return nodeCrypto.randomBytes(32);
|
|
6
|
+
}
|
|
7
|
+
export function generateSenderKeyId() {
|
|
8
|
+
return nodeCrypto.randomInt(2147483647);
|
|
9
|
+
}
|
|
10
|
+
export function generateSenderSigningKey(key) {
|
|
11
|
+
if (!key) {
|
|
12
|
+
key = generateKeyPair();
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
public: Buffer.from(key.pubKey),
|
|
16
|
+
private: Buffer.from(key.privKey)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=keyhelper.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const _queueAsyncBuckets = new Map();
|
|
2
|
+
const _gcLimit = 10000;
|
|
3
|
+
async function _asyncQueueExecutor(queue, cleanup) {
|
|
4
|
+
let offt = 0;
|
|
5
|
+
while (true) {
|
|
6
|
+
const limit = Math.min(queue.length, _gcLimit);
|
|
7
|
+
for (let i = offt; i < limit; i++) {
|
|
8
|
+
const job = queue[i];
|
|
9
|
+
try {
|
|
10
|
+
job.resolve(await job.awaitable());
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
job.reject(e);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
if (limit < queue.length) {
|
|
17
|
+
if (limit >= _gcLimit) {
|
|
18
|
+
queue.splice(0, limit);
|
|
19
|
+
offt = 0;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
offt = limit;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
cleanup();
|
|
30
|
+
}
|
|
31
|
+
export default function queueJob(bucket, awaitable) {
|
|
32
|
+
// Skip name assignment since it's readonly in strict mode
|
|
33
|
+
if (typeof bucket !== 'string') {
|
|
34
|
+
console.warn('Unhandled bucket type (for naming):', typeof bucket, bucket);
|
|
35
|
+
}
|
|
36
|
+
let inactive = false;
|
|
37
|
+
if (!_queueAsyncBuckets.has(bucket)) {
|
|
38
|
+
_queueAsyncBuckets.set(bucket, []);
|
|
39
|
+
inactive = true;
|
|
40
|
+
}
|
|
41
|
+
const queue = _queueAsyncBuckets.get(bucket);
|
|
42
|
+
const job = new Promise((resolve, reject) => {
|
|
43
|
+
queue.push({
|
|
44
|
+
awaitable,
|
|
45
|
+
resolve: resolve,
|
|
46
|
+
reject
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
if (inactive) {
|
|
50
|
+
_asyncQueueExecutor(queue, () => _queueAsyncBuckets.delete(bucket));
|
|
51
|
+
}
|
|
52
|
+
return job;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=queue-job.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* @ts-ignore */
|
|
2
|
+
import { calculateMAC } from 'libsignal/src/crypto.js';
|
|
3
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
4
|
+
export class SenderChainKey {
|
|
5
|
+
constructor(iteration, chainKey) {
|
|
6
|
+
this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
|
|
7
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02]);
|
|
8
|
+
this.iteration = iteration;
|
|
9
|
+
if (chainKey instanceof Buffer) {
|
|
10
|
+
this.chainKey = chainKey;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
this.chainKey = Buffer.from(chainKey || []);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
getIteration() {
|
|
17
|
+
return this.iteration;
|
|
18
|
+
}
|
|
19
|
+
getSenderMessageKey() {
|
|
20
|
+
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
|
|
21
|
+
}
|
|
22
|
+
getNext() {
|
|
23
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
|
|
24
|
+
}
|
|
25
|
+
getSeed() {
|
|
26
|
+
return this.chainKey;
|
|
27
|
+
}
|
|
28
|
+
getDerivative(seed, key) {
|
|
29
|
+
return calculateMAC(key, seed);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=sender-chain-key.js.map
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { proto } from '../../../WAProto/index.js';
|
|
2
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
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 typeof this.chainKey === 'string' ? Buffer.from(this.chainKey, 'base64') : this.chainKey;
|
|
55
|
+
}
|
|
56
|
+
getSignatureKey() {
|
|
57
|
+
return typeof this.signatureKey === 'string' ? Buffer.from(this.signatureKey, 'base64') : this.signatureKey;
|
|
58
|
+
}
|
|
59
|
+
getId() {
|
|
60
|
+
return this.id;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=sender-key-distribution-message.js.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* @ts-ignore */
|
|
2
|
+
import { calculateSignature, verifySignature } from 'libsignal/src/curve.js';
|
|
3
|
+
import { proto } from '../../../WAProto/index.js';
|
|
4
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
5
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
6
|
+
constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
|
|
7
|
+
super();
|
|
8
|
+
this.SIGNATURE_LENGTH = 64;
|
|
9
|
+
if (serialized) {
|
|
10
|
+
const version = serialized[0];
|
|
11
|
+
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
|
|
12
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
13
|
+
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
|
|
14
|
+
this.serialized = serialized;
|
|
15
|
+
this.messageVersion = (version & 0xff) >> 4;
|
|
16
|
+
this.keyId = senderKeyMessage.id;
|
|
17
|
+
this.iteration = senderKeyMessage.iteration;
|
|
18
|
+
this.ciphertext =
|
|
19
|
+
typeof senderKeyMessage.ciphertext === 'string'
|
|
20
|
+
? Buffer.from(senderKeyMessage.ciphertext, 'base64')
|
|
21
|
+
: senderKeyMessage.ciphertext;
|
|
22
|
+
this.signature = signature;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
|
|
26
|
+
const ciphertextBuffer = Buffer.from(ciphertext);
|
|
27
|
+
const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
|
|
28
|
+
id: keyId,
|
|
29
|
+
iteration: iteration,
|
|
30
|
+
ciphertext: ciphertextBuffer
|
|
31
|
+
})).finish();
|
|
32
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
|
|
33
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
|
|
34
|
+
this.messageVersion = this.CURRENT_VERSION;
|
|
35
|
+
this.keyId = keyId;
|
|
36
|
+
this.iteration = iteration;
|
|
37
|
+
this.ciphertext = ciphertextBuffer;
|
|
38
|
+
this.signature = signature;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
getKeyId() {
|
|
42
|
+
return this.keyId;
|
|
43
|
+
}
|
|
44
|
+
getIteration() {
|
|
45
|
+
return this.iteration;
|
|
46
|
+
}
|
|
47
|
+
getCipherText() {
|
|
48
|
+
return this.ciphertext;
|
|
49
|
+
}
|
|
50
|
+
verifySignature(signatureKey) {
|
|
51
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
|
|
52
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
53
|
+
const res = verifySignature(signatureKey, part1, part2);
|
|
54
|
+
if (!res)
|
|
55
|
+
throw new Error('Invalid signature!');
|
|
56
|
+
}
|
|
57
|
+
getSignature(signatureKey, serialized) {
|
|
58
|
+
return Buffer.from(calculateSignature(signatureKey, serialized));
|
|
59
|
+
}
|
|
60
|
+
serialize() {
|
|
61
|
+
return this.serialized;
|
|
62
|
+
}
|
|
63
|
+
getType() {
|
|
64
|
+
return 4;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=sender-key-message.js.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
function isNull(str) {
|
|
2
|
+
return str === null || str === '';
|
|
3
|
+
}
|
|
4
|
+
function intValue(num) {
|
|
5
|
+
const MAX_VALUE = 0x7fffffff;
|
|
6
|
+
const MIN_VALUE = -0x80000000;
|
|
7
|
+
if (num > MAX_VALUE || num < MIN_VALUE) {
|
|
8
|
+
return num & 0xffffffff;
|
|
9
|
+
}
|
|
10
|
+
return num;
|
|
11
|
+
}
|
|
12
|
+
function hashCode(strKey) {
|
|
13
|
+
let hash = 0;
|
|
14
|
+
if (!isNull(strKey)) {
|
|
15
|
+
for (let i = 0; i < strKey.length; i++) {
|
|
16
|
+
hash = hash * 31 + strKey.charCodeAt(i);
|
|
17
|
+
hash = intValue(hash);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return hash;
|
|
21
|
+
}
|
|
22
|
+
export class SenderKeyName {
|
|
23
|
+
constructor(groupId, sender) {
|
|
24
|
+
this.groupId = groupId;
|
|
25
|
+
this.sender = sender;
|
|
26
|
+
}
|
|
27
|
+
getGroupId() {
|
|
28
|
+
return this.groupId;
|
|
29
|
+
}
|
|
30
|
+
getSender() {
|
|
31
|
+
return this.sender;
|
|
32
|
+
}
|
|
33
|
+
serialize() {
|
|
34
|
+
return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
|
|
35
|
+
}
|
|
36
|
+
toString() {
|
|
37
|
+
return this.serialize();
|
|
38
|
+
}
|
|
39
|
+
equals(other) {
|
|
40
|
+
if (other === null)
|
|
41
|
+
return false;
|
|
42
|
+
return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
|
|
43
|
+
}
|
|
44
|
+
hashCode() {
|
|
45
|
+
return hashCode(this.groupId) ^ hashCode(this.sender.toString());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=sender-key-name.js.map
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BufferJSON } from '../../Utils/generics.js';
|
|
2
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
3
|
+
export class SenderKeyRecord {
|
|
4
|
+
constructor(serialized) {
|
|
5
|
+
this.MAX_STATES = 5;
|
|
6
|
+
this.senderKeyStates = [];
|
|
7
|
+
if (serialized) {
|
|
8
|
+
for (const structure of serialized) {
|
|
9
|
+
this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
isEmpty() {
|
|
14
|
+
return this.senderKeyStates.length === 0;
|
|
15
|
+
}
|
|
16
|
+
getSenderKeyState(keyId) {
|
|
17
|
+
if (keyId === undefined && this.senderKeyStates.length) {
|
|
18
|
+
return this.senderKeyStates[this.senderKeyStates.length - 1];
|
|
19
|
+
}
|
|
20
|
+
return this.senderKeyStates.find(state => state.getKeyId() === keyId);
|
|
21
|
+
}
|
|
22
|
+
addSenderKeyState(id, iteration, chainKey, signatureKey) {
|
|
23
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
|
|
24
|
+
if (this.senderKeyStates.length > this.MAX_STATES) {
|
|
25
|
+
this.senderKeyStates.shift();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
setSenderKeyState(id, iteration, chainKey, keyPair) {
|
|
29
|
+
this.senderKeyStates.length = 0;
|
|
30
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
|
|
31
|
+
}
|
|
32
|
+
serialize() {
|
|
33
|
+
return this.senderKeyStates.map(state => state.getStructure());
|
|
34
|
+
}
|
|
35
|
+
static deserialize(data) {
|
|
36
|
+
let parsed;
|
|
37
|
+
if (typeof data === 'string') {
|
|
38
|
+
parsed = JSON.parse(data, BufferJSON.reviver);
|
|
39
|
+
}
|
|
40
|
+
else if (data instanceof Uint8Array) {
|
|
41
|
+
const str = Buffer.from(data).toString('utf-8');
|
|
42
|
+
parsed = JSON.parse(str, BufferJSON.reviver);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
parsed = data;
|
|
46
|
+
}
|
|
47
|
+
return new SenderKeyRecord(parsed);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=sender-key-record.js.map
|