@skyzopedia/baileys-mod 3.0.2
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 +21 -0
- package/WAProto/WAProto.proto +5311 -0
- package/WAProto/index.js +94091 -0
- package/lib/Defaults/index.js +123 -0
- package/lib/KeyDB/BinarySearch.js +20 -0
- package/lib/KeyDB/KeyedDB.js +167 -0
- package/lib/KeyDB/index.js +4 -0
- package/lib/Signal/Group/ciphertext-message.js +13 -0
- package/lib/Signal/Group/group-session-builder.js +32 -0
- package/lib/Signal/Group/group_cipher.js +84 -0
- package/lib/Signal/Group/index.js +13 -0
- package/lib/Signal/Group/keyhelper.js +20 -0
- package/lib/Signal/Group/sender-chain-key.js +28 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +65 -0
- package/lib/Signal/Group/sender-key-message.js +68 -0
- package/lib/Signal/Group/sender-key-name.js +52 -0
- package/lib/Signal/Group/sender-key-record.js +43 -0
- package/lib/Signal/Group/sender-key-state.js +86 -0
- package/lib/Signal/Group/sender-message-key.js +28 -0
- package/lib/Signal/libsignal.js +324 -0
- package/lib/Signal/lid-mapping.js +155 -0
- package/lib/Socket/Client/index.js +4 -0
- package/lib/Socket/Client/types.js +13 -0
- package/lib/Socket/Client/websocket.js +52 -0
- package/lib/Socket/business.js +377 -0
- package/lib/Socket/chats.js +881 -0
- package/lib/Socket/communities.js +413 -0
- package/lib/Socket/groups.js +312 -0
- package/lib/Socket/index.js +16 -0
- package/lib/Socket/messages-recv.js +1163 -0
- package/lib/Socket/messages-send.js +1082 -0
- package/lib/Socket/mex.js +45 -0
- package/lib/Socket/newsletter.js +259 -0
- package/lib/Socket/socket.js +781 -0
- package/lib/Store/index.js +6 -0
- package/lib/Store/make-cache-manager-store.js +75 -0
- package/lib/Store/make-in-memory-store.js +290 -0
- package/lib/Store/make-ordered-dictionary.js +79 -0
- package/lib/Store/object-repository.js +25 -0
- package/lib/Types/Auth.js +3 -0
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Call.js +3 -0
- package/lib/Types/Chat.js +9 -0
- package/lib/Types/Contact.js +3 -0
- package/lib/Types/Events.js +3 -0
- package/lib/Types/GroupMetadata.js +3 -0
- package/lib/Types/Label.js +25 -0
- package/lib/Types/LabelAssociation.js +7 -0
- package/lib/Types/Message.js +12 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Newsletter.js.bak +33 -0
- package/lib/Types/Product.js +3 -0
- package/lib/Types/Signal.js +3 -0
- package/lib/Types/Socket.js +4 -0
- package/lib/Types/State.js +11 -0
- package/lib/Types/USync.js +3 -0
- package/lib/Types/index.js +28 -0
- package/lib/Utils/auth-utils.js +219 -0
- package/lib/Utils/baileys-event-stream.js +44 -0
- package/lib/Utils/browser-utils.js +17 -0
- package/lib/Utils/business.js +233 -0
- package/lib/Utils/chat-utils.js +752 -0
- package/lib/Utils/crypto.js +130 -0
- package/lib/Utils/decode-wa-message.js +267 -0
- package/lib/Utils/event-buffer.js +528 -0
- package/lib/Utils/generics.js +355 -0
- package/lib/Utils/history.js +87 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +81 -0
- package/lib/Utils/logger.js +5 -0
- package/lib/Utils/lt-hash.js +45 -0
- package/lib/Utils/make-mutex.js +36 -0
- package/lib/Utils/message-retry-manager.js +113 -0
- package/lib/Utils/messages-media.js +601 -0
- package/lib/Utils/messages.js +776 -0
- package/lib/Utils/noise-handler.js +144 -0
- package/lib/Utils/pre-key-manager.js +85 -0
- package/lib/Utils/process-message.js +341 -0
- package/lib/Utils/signal.js +161 -0
- package/lib/Utils/use-multi-file-auth-state.js +111 -0
- package/lib/Utils/validate-connection.js +200 -0
- package/lib/WABinary/constants.js +1303 -0
- package/lib/WABinary/decode.js +240 -0
- package/lib/WABinary/encode.js +218 -0
- package/lib/WABinary/generic-utils.js +113 -0
- package/lib/WABinary/index.js +7 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +3 -0
- package/lib/WAM/BinaryInfo.js +11 -0
- package/lib/WAM/constants.js +22853 -0
- package/lib/WAM/encode.js +154 -0
- package/lib/WAM/index.js +5 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +39 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +30 -0
- package/lib/WAUSync/Protocols/index.js +6 -0
- package/lib/WAUSync/USyncQuery.js +90 -0
- package/lib/WAUSync/USyncUser.js +24 -0
- package/lib/WAUSync/index.js +5 -0
- package/lib/index.js +15 -0
- package/package.json +102 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { makeLibSignalRepository } from "../Signal/libsignal.js";
|
|
3
|
+
import { Browsers } from "../Utils/browser-utils.js";
|
|
4
|
+
import { proto } from "../../WAProto/index.js";
|
|
5
|
+
import logger from "../Utils/logger.js";
|
|
6
|
+
//=======================================================//
|
|
7
|
+
export const version = [2, 3000, 1028716292];
|
|
8
|
+
export const DICT_VERSION = 3;
|
|
9
|
+
//=======================================================//
|
|
10
|
+
export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]);
|
|
11
|
+
export const CALL_VIDEO_PREFIX = "https://call.whatsapp.com/video/";
|
|
12
|
+
export const CALL_AUDIO_PREFIX = "https://call.whatsapp.com/voice/";
|
|
13
|
+
export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX = Buffer.from([6, 5]);
|
|
14
|
+
export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX = Buffer.from([6, 6]);
|
|
15
|
+
export const NOISE_MODE = "Noise_XX_25519_AESGCM_SHA256\0\0\0\0";
|
|
16
|
+
export const WA_ADV_ACCOUNT_SIG_PREFIX = Buffer.from([6, 0]);
|
|
17
|
+
export const WA_ADV_DEVICE_SIG_PREFIX = Buffer.from([6, 1]);
|
|
18
|
+
export const DEFAULT_ORIGIN = "https://web.whatsapp.com";
|
|
19
|
+
export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
|
|
20
|
+
export const UNAUTHORIZED_CODES = [401, 403, 419];
|
|
21
|
+
export const KEY_BUNDLE_TYPE = Buffer.from([5]);
|
|
22
|
+
export const PHONE_CONNECTION_CB = "CB:Pong";
|
|
23
|
+
export const DEF_CALLBACK_PREFIX = "CB:";
|
|
24
|
+
export const DEF_TAG_PREFIX = "TAG:";
|
|
25
|
+
//=======================================================//
|
|
26
|
+
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
|
|
27
|
+
//=======================================================//
|
|
28
|
+
export const WA_CERT_DETAILS = {
|
|
29
|
+
SERIAL: 0
|
|
30
|
+
};
|
|
31
|
+
//=======================================================//
|
|
32
|
+
export const PROCESSABLE_HISTORY_TYPES = [
|
|
33
|
+
proto.Message.HistorySyncNotification.HistorySyncType.INITIAL_BOOTSTRAP,
|
|
34
|
+
proto.Message.HistorySyncNotification.HistorySyncType.PUSH_NAME,
|
|
35
|
+
proto.Message.HistorySyncNotification.HistorySyncType.RECENT,
|
|
36
|
+
proto.Message.HistorySyncNotification.HistorySyncType.FULL,
|
|
37
|
+
proto.Message.HistorySyncNotification.HistorySyncType.ON_DEMAND,
|
|
38
|
+
proto.Message.HistorySyncNotification.HistorySyncType.NON_BLOCKING_DATA,
|
|
39
|
+
proto.Message.HistorySyncNotification.HistorySyncType.INITIAL_STATUS_V3
|
|
40
|
+
];
|
|
41
|
+
//=======================================================//
|
|
42
|
+
export const DEFAULT_CONNECTION_CONFIG = {
|
|
43
|
+
"version": version,
|
|
44
|
+
"browser": Browsers.iOS("Safari"),
|
|
45
|
+
"waWebSocketUrl": "wss://web.whatsapp.com/ws/chat",
|
|
46
|
+
"connectTimeoutMs": 20000,
|
|
47
|
+
"keepAliveIntervalMs": 30000,
|
|
48
|
+
"logger": logger.child({ "class": "baileys" }),
|
|
49
|
+
"emitOwnEvents": true,
|
|
50
|
+
"defaultQueryTimeoutMs": 60000,
|
|
51
|
+
"customUploadHosts": [],
|
|
52
|
+
"retryRequestDelayMs": 250,
|
|
53
|
+
"maxMsgRetryCount": 5,
|
|
54
|
+
"fireInitQueries": true,
|
|
55
|
+
"auth": undefined,
|
|
56
|
+
"markOnlineOnConnect": true,
|
|
57
|
+
"syncFullHistory": true,
|
|
58
|
+
"patchMessageBeforeSending": msg => msg,
|
|
59
|
+
"shouldSyncHistoryMessage": () => true,
|
|
60
|
+
"shouldIgnoreJid": () => false,
|
|
61
|
+
"linkPreviewImageThumbnailWidth": 192,
|
|
62
|
+
"transactionOpts": { "maxCommitRetries": 10, "delayBetweenTriesMs": 3000 },
|
|
63
|
+
"generateHighQualityLinkPreview": false,
|
|
64
|
+
"enableAutoSessionRecreation": true,
|
|
65
|
+
"enableRecentMessageCache": true,
|
|
66
|
+
"options": {},
|
|
67
|
+
"appStateMacVerification": {
|
|
68
|
+
"patch": false,
|
|
69
|
+
"snapshot": false
|
|
70
|
+
},
|
|
71
|
+
"countryCode": "US",
|
|
72
|
+
"getMessage": async () => undefined,
|
|
73
|
+
"cachedGroupMetadata": async () => undefined,
|
|
74
|
+
"makeSignalRepository": makeLibSignalRepository
|
|
75
|
+
};
|
|
76
|
+
//=======================================================//
|
|
77
|
+
export const MEDIA_PATH_MAP = {
|
|
78
|
+
"image": "/mms/image",
|
|
79
|
+
"video": "/mms/video",
|
|
80
|
+
"document": "/mms/document",
|
|
81
|
+
"audio": "/mms/audio",
|
|
82
|
+
"sticker": "/mms/image",
|
|
83
|
+
"thumbnail-link": "/mms/image",
|
|
84
|
+
"product-catalog-image": "/product/image",
|
|
85
|
+
"md-app-state": "",
|
|
86
|
+
"md-msg-hist": "/mms/md-app-state",
|
|
87
|
+
"biz-cover-photo": "/pps/biz-cover-photo"
|
|
88
|
+
};
|
|
89
|
+
//=======================================================//
|
|
90
|
+
export const MEDIA_HKDF_KEY_MAPPING = {
|
|
91
|
+
"audio": "Audio",
|
|
92
|
+
"document": "Document",
|
|
93
|
+
"gif": "Video",
|
|
94
|
+
"image": "Image",
|
|
95
|
+
"ppic": "",
|
|
96
|
+
"product": "Image",
|
|
97
|
+
"ptt": "Audio",
|
|
98
|
+
"sticker": "Image",
|
|
99
|
+
"video": "Video",
|
|
100
|
+
"thumbnail-document": "Document Thumbnail",
|
|
101
|
+
"thumbnail-image": "Image Thumbnail",
|
|
102
|
+
"thumbnail-video": "Video Thumbnail",
|
|
103
|
+
"thumbnail-link": "Link Thumbnail",
|
|
104
|
+
"md-msg-hist": "History",
|
|
105
|
+
"md-app-state": "App State",
|
|
106
|
+
"product-catalog-image": "",
|
|
107
|
+
"payment-bg-image": "Payment Background",
|
|
108
|
+
"ptv": "Video",
|
|
109
|
+
"biz-cover-photo": "Image"
|
|
110
|
+
};
|
|
111
|
+
//=======================================================//
|
|
112
|
+
export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
|
|
113
|
+
export const MIN_PREKEY_COUNT = 5;
|
|
114
|
+
export const INITIAL_PREKEY_COUNT = 812;
|
|
115
|
+
export const UPLOAD_TIMEOUT = 30000;
|
|
116
|
+
export const MIN_UPLOAD_INTERVAL = 5000;
|
|
117
|
+
export const DEFAULT_CACHE_TTLS = {
|
|
118
|
+
SIGNAL_STORE: 5 * 60,
|
|
119
|
+
MSG_RETRY: 60 * 60,
|
|
120
|
+
CALL_OFFER: 5 * 60,
|
|
121
|
+
USER_DEVICES: 5 * 60
|
|
122
|
+
};
|
|
123
|
+
//=======================================================//
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//===================================//
|
|
2
|
+
export default function binarySearch(array, predicate) {
|
|
3
|
+
let low = 0;
|
|
4
|
+
let high = array.length;
|
|
5
|
+
if (array.length === 0) return low;
|
|
6
|
+
if (predicate(array[low]) < 0) return low - 1;
|
|
7
|
+
else if (predicate(array[low]) === 0) return low;
|
|
8
|
+
const maxPred = predicate(array[high - 1]);
|
|
9
|
+
if (maxPred > 0) return high;
|
|
10
|
+
else if (maxPred === 0) return high - 1;
|
|
11
|
+
while (low !== high) {
|
|
12
|
+
const mid = low + Math.floor((high - low) / 2);
|
|
13
|
+
const pred = predicate(array[mid]);
|
|
14
|
+
if (pred < 0) high = mid;
|
|
15
|
+
else if (pred > 0) low = mid + 1;
|
|
16
|
+
else return mid;
|
|
17
|
+
}
|
|
18
|
+
return low;
|
|
19
|
+
}
|
|
20
|
+
//===================================//
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
//===================================//
|
|
2
|
+
import binarySearch from "./BinarySearch.js";
|
|
3
|
+
//===================================//
|
|
4
|
+
export default class KeyedDB {
|
|
5
|
+
constructor(key, id) {
|
|
6
|
+
this.key = key;
|
|
7
|
+
this.idGetter = id || (v => this.key.key(v).toString());
|
|
8
|
+
this.dict = {};
|
|
9
|
+
this.array = [];
|
|
10
|
+
}
|
|
11
|
+
get length() {
|
|
12
|
+
return this.array.length;
|
|
13
|
+
}
|
|
14
|
+
get first() {
|
|
15
|
+
return this.array[0];
|
|
16
|
+
}
|
|
17
|
+
get last() {
|
|
18
|
+
return this.array[this.array.length - 1];
|
|
19
|
+
}
|
|
20
|
+
toJSON() {
|
|
21
|
+
return this.array;
|
|
22
|
+
}
|
|
23
|
+
insert(...values) {
|
|
24
|
+
values.forEach(v => this._insertSingle(v));
|
|
25
|
+
}
|
|
26
|
+
upsert(...values) {
|
|
27
|
+
const updates = [];
|
|
28
|
+
values.forEach(v => {
|
|
29
|
+
if (!v) return;
|
|
30
|
+
const deleted = this.deleteById(this.idGetter(v), false);
|
|
31
|
+
this._insertSingle(v);
|
|
32
|
+
deleted && updates.push(v);
|
|
33
|
+
});
|
|
34
|
+
return updates;
|
|
35
|
+
}
|
|
36
|
+
insertIfAbsent(...values) {
|
|
37
|
+
const insertions = [];
|
|
38
|
+
values.forEach(v => {
|
|
39
|
+
if (!v) return;
|
|
40
|
+
const presentValue = this.get(this.idGetter(v));
|
|
41
|
+
if (presentValue) return;
|
|
42
|
+
const presentKey = this.firstIndex(v);
|
|
43
|
+
if (this.array[presentKey] && this.key.key(this.array[presentKey]) === this.key.key(v)) return;
|
|
44
|
+
this.insert(v);
|
|
45
|
+
insertions.push(v);
|
|
46
|
+
});
|
|
47
|
+
return insertions;
|
|
48
|
+
}
|
|
49
|
+
deleteById(id, assertPresent = true) {
|
|
50
|
+
const value = this.get(id);
|
|
51
|
+
if (!value) {
|
|
52
|
+
if (assertPresent) throw new Error(`Value not found`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
return this.delete(value);
|
|
56
|
+
}
|
|
57
|
+
delete(value) {
|
|
58
|
+
const index = this.firstIndex(value);
|
|
59
|
+
if (index < 0 || index >= this.array.length || this.key.key(value) !== this.key.key(this.array[index])) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
delete this.dict[this.idGetter(value)];
|
|
63
|
+
return this.array.splice(index, 1)[0];
|
|
64
|
+
}
|
|
65
|
+
slice(start, end) {
|
|
66
|
+
const db = new KeyedDB(this.key, this.idGetter);
|
|
67
|
+
db.array = this.array.slice(start, end);
|
|
68
|
+
db.array.forEach(item => db.dict[this.idGetter(item)] = item);
|
|
69
|
+
return db;
|
|
70
|
+
}
|
|
71
|
+
clear() {
|
|
72
|
+
this.array = [];
|
|
73
|
+
this.dict = {};
|
|
74
|
+
}
|
|
75
|
+
get(id) {
|
|
76
|
+
return this.dict[id];
|
|
77
|
+
}
|
|
78
|
+
all() {
|
|
79
|
+
return this.array;
|
|
80
|
+
}
|
|
81
|
+
update(id, update) {
|
|
82
|
+
const value = this.get(id);
|
|
83
|
+
if (value) {
|
|
84
|
+
const idx = this.firstIndex(value);
|
|
85
|
+
if (idx >= 0 && idx < this.array.length && this.idGetter(this.array[idx]) === id) {
|
|
86
|
+
const oldKey = this.key.key(value);
|
|
87
|
+
update(value);
|
|
88
|
+
const newKey = this.key.key(value);
|
|
89
|
+
if (newKey !== oldKey) {
|
|
90
|
+
delete this.dict[id];
|
|
91
|
+
this.array.splice(idx, 1);
|
|
92
|
+
this._insertSingle(value);
|
|
93
|
+
return 2;
|
|
94
|
+
}
|
|
95
|
+
return 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
updateKey(value, update) {
|
|
100
|
+
return this.update(this.idGetter(value), update);
|
|
101
|
+
}
|
|
102
|
+
filter(predicate) {
|
|
103
|
+
const db = new KeyedDB(this.key, this.idGetter);
|
|
104
|
+
db.array = this.array.filter((value, index) => {
|
|
105
|
+
if (predicate(value, index)) {
|
|
106
|
+
db.dict[this.idGetter(value)] = value;
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return db;
|
|
111
|
+
}
|
|
112
|
+
paginatedByValue(value, limit, predicate, mode = "after") {
|
|
113
|
+
return this.paginated(value && this.key.key(value), limit, predicate, mode);
|
|
114
|
+
}
|
|
115
|
+
paginated(cursor, limit, predicate, mode = "after") {
|
|
116
|
+
let index = mode === "after" ? 0 : this.array.length;
|
|
117
|
+
if (cursor !== null && typeof cursor !== "undefined") {
|
|
118
|
+
index = binarySearch(this.array, v => this.key.compare(cursor, this.key.key(v)));
|
|
119
|
+
if (index < 0) index = 0;
|
|
120
|
+
if (this.key.key(this.array[index]) === cursor) index += (mode === "after" ? 1 : 0);
|
|
121
|
+
}
|
|
122
|
+
return this.filtered(index, limit, mode, predicate);
|
|
123
|
+
}
|
|
124
|
+
_insertSingle(value) {
|
|
125
|
+
if (!value) throw new Error("falsey value");
|
|
126
|
+
const valueID = this.idGetter(value);
|
|
127
|
+
if (this.get(valueID)) throw new Error("duplicate ID being inserted: " + valueID);
|
|
128
|
+
|
|
129
|
+
if (this.array.length > 0) {
|
|
130
|
+
const index = this.firstIndex(value);
|
|
131
|
+
if (index >= this.array.length) this.array.push(value);
|
|
132
|
+
else if (index < 0) this.array.unshift(value);
|
|
133
|
+
else if (this.key.key(value) !== this.key.key(this.array[index])) this.array.splice(index, 0, value);
|
|
134
|
+
else throw new Error(`duplicate key: ${this.key.key(value)}, inserting: ${valueID}, present: ${this.idGetter(this.array[index])}`);
|
|
135
|
+
} else {
|
|
136
|
+
this.array.push(value);
|
|
137
|
+
}
|
|
138
|
+
this.dict[valueID] = value;
|
|
139
|
+
}
|
|
140
|
+
filtered(start, count, mode, predicate) {
|
|
141
|
+
let arr;
|
|
142
|
+
if (mode === "after") {
|
|
143
|
+
if (predicate) {
|
|
144
|
+
arr = [];
|
|
145
|
+
for (let item of this.array.slice(start)) {
|
|
146
|
+
predicate(item, start + arr.length) && arr.push(item);
|
|
147
|
+
if (arr.length >= count) break;
|
|
148
|
+
}
|
|
149
|
+
} else arr = this.array.slice(start, start + count);
|
|
150
|
+
} else if (mode === "before") {
|
|
151
|
+
if (predicate) {
|
|
152
|
+
arr = [];
|
|
153
|
+
for (let i = start - 1; i >= 0; i--) {
|
|
154
|
+
let item = this.array[i];
|
|
155
|
+
predicate(item, start + arr.length) && arr.unshift(item);
|
|
156
|
+
if (arr.length >= count) break;
|
|
157
|
+
}
|
|
158
|
+
} else arr = this.array.slice(Math.max(start - count, 0), start);
|
|
159
|
+
}
|
|
160
|
+
return arr;
|
|
161
|
+
}
|
|
162
|
+
firstIndex(value) {
|
|
163
|
+
const valueKey = this.key.key(value);
|
|
164
|
+
return binarySearch(this.array, v => this.key.compare(valueKey, this.key.key(v)));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
//===================================//
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
export class CiphertextMessage {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.UNSUPPORTED_VERSION = 1;
|
|
5
|
+
this.CURRENT_VERSION = 3;
|
|
6
|
+
this.WHISPER_TYPE = 2;
|
|
7
|
+
this.PREKEY_TYPE = 3;
|
|
8
|
+
this.SENDERKEY_TYPE = 4;
|
|
9
|
+
this.SENDERKEY_DISTRIBUTION_TYPE = 5;
|
|
10
|
+
this.ENCRYPTED_MESSAGE_OVERHEAD = 53;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//=======================================================//
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { SenderKeyDistributionMessage } from "./sender-key-distribution-message.js";
|
|
3
|
+
import { SenderKeyRecord } from "./sender-key-record.js";
|
|
4
|
+
import { SenderKeyName } from "./sender-key-name.js";
|
|
5
|
+
import * as keyhelper from "./keyhelper.js";
|
|
6
|
+
//=======================================================//
|
|
7
|
+
export class GroupSessionBuilder {
|
|
8
|
+
constructor(senderKeyStore) {
|
|
9
|
+
this.senderKeyStore = senderKeyStore;
|
|
10
|
+
}
|
|
11
|
+
async process(senderKeyName, senderKeyDistributionMessage) {
|
|
12
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
|
|
13
|
+
senderKeyRecord.addSenderKeyState(senderKeyDistributionMessage.getId(), senderKeyDistributionMessage.getIteration(), senderKeyDistributionMessage.getChainKey(), senderKeyDistributionMessage.getSignatureKey());
|
|
14
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
|
|
15
|
+
}
|
|
16
|
+
async create(senderKeyName) {
|
|
17
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
|
|
18
|
+
if (senderKeyRecord.isEmpty()) {
|
|
19
|
+
const keyId = keyhelper.generateSenderKeyId();
|
|
20
|
+
const senderKey = keyhelper.generateSenderKey();
|
|
21
|
+
const signingKey = keyhelper.generateSenderSigningKey();
|
|
22
|
+
senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey);
|
|
23
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
|
|
24
|
+
}
|
|
25
|
+
const state = senderKeyRecord.getSenderKeyState();
|
|
26
|
+
if (!state) {
|
|
27
|
+
throw new Error("No session state available");
|
|
28
|
+
}
|
|
29
|
+
return new SenderKeyDistributionMessage(state.getKeyId(), state.getSenderChainKey().getIteration(), state.getSenderChainKey().getSeed(), state.getSigningKeyPublic());
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=group-session-builder.js.map
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { decrypt, encrypt } from "libsignal-xeuka/src/crypto.js";
|
|
3
|
+
import { SenderKeyMessage } from "./sender-key-message.js";
|
|
4
|
+
import { SenderKeyRecord } from "./sender-key-record.js";
|
|
5
|
+
import { SenderKeyState } from "./sender-key-state.js";
|
|
6
|
+
import { SenderKeyName } from "./sender-key-name.js";
|
|
7
|
+
//=======================================================//
|
|
8
|
+
export class GroupCipher {
|
|
9
|
+
constructor(senderKeyStore, senderKeyName) {
|
|
10
|
+
this.senderKeyStore = senderKeyStore;
|
|
11
|
+
this.senderKeyName = senderKeyName;
|
|
12
|
+
}
|
|
13
|
+
async encrypt(paddedPlaintext) {
|
|
14
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
15
|
+
if (!record) {
|
|
16
|
+
throw new Error("No SenderKeyRecord found for encryption");
|
|
17
|
+
}
|
|
18
|
+
const senderKeyState = record.getSenderKeyState();
|
|
19
|
+
if (!senderKeyState) {
|
|
20
|
+
throw new Error("No session to encrypt message");
|
|
21
|
+
}
|
|
22
|
+
const iteration = senderKeyState.getSenderChainKey().getIteration();
|
|
23
|
+
const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
|
|
24
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
|
|
25
|
+
const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
|
|
26
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
27
|
+
return senderKeyMessage.serialize();
|
|
28
|
+
}
|
|
29
|
+
async decrypt(senderKeyMessageBytes) {
|
|
30
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
31
|
+
if (!record) {
|
|
32
|
+
throw new Error("No SenderKeyRecord found for decryption");
|
|
33
|
+
}
|
|
34
|
+
const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
|
|
35
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
|
|
36
|
+
if (!senderKeyState) {
|
|
37
|
+
throw new Error("No session found to decrypt message");
|
|
38
|
+
}
|
|
39
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
|
|
40
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
|
|
41
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
|
42
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
43
|
+
return plaintext;
|
|
44
|
+
}
|
|
45
|
+
getSenderKey(senderKeyState, iteration) {
|
|
46
|
+
let senderChainKey = senderKeyState.getSenderChainKey();
|
|
47
|
+
if (senderChainKey.getIteration() > iteration) {
|
|
48
|
+
if (senderKeyState.hasSenderMessageKey(iteration)) {
|
|
49
|
+
const messageKey = senderKeyState.removeSenderMessageKey(iteration);
|
|
50
|
+
if (!messageKey) {
|
|
51
|
+
throw new Error("No sender message key found for iteration");
|
|
52
|
+
}
|
|
53
|
+
return messageKey;
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
|
|
56
|
+
}
|
|
57
|
+
if (iteration - senderChainKey.getIteration() > 2000) {
|
|
58
|
+
throw new Error("Over 2000 messages into the future!");
|
|
59
|
+
}
|
|
60
|
+
while (senderChainKey.getIteration() < iteration) {
|
|
61
|
+
senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
|
|
62
|
+
senderChainKey = senderChainKey.getNext();
|
|
63
|
+
}
|
|
64
|
+
senderKeyState.setSenderChainKey(senderChainKey.getNext());
|
|
65
|
+
return senderChainKey.getSenderMessageKey();
|
|
66
|
+
}
|
|
67
|
+
async getPlainText(iv, key, ciphertext) {
|
|
68
|
+
try {
|
|
69
|
+
return decrypt(key, ciphertext, iv);
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
throw new Error("InvalidMessageException");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async getCipherText(iv, key, plaintext) {
|
|
76
|
+
try {
|
|
77
|
+
return encrypt(key, plaintext, iv);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
throw new Error("InvalidMessageException");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
//=======================================================//
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
export { SenderKeyDistributionMessage } from "./sender-key-distribution-message.js";
|
|
3
|
+
export { GroupSessionBuilder } from "./group-session-builder.js";
|
|
4
|
+
export { CiphertextMessage } from "./ciphertext-message.js";
|
|
5
|
+
export { SenderKeyMessage } from "./sender-key-message.js";
|
|
6
|
+
export { SenderMessageKey } from "./sender-message-key.js";
|
|
7
|
+
export { SenderKeyRecord } from "./sender-key-record.js";
|
|
8
|
+
export { SenderKeyState } from "./sender-key-state.js";
|
|
9
|
+
export { SenderChainKey } from "./sender-chain-key.js";
|
|
10
|
+
export { SenderKeyName } from "./sender-key-name.js";
|
|
11
|
+
export { GroupCipher } from "./group_cipher.js";
|
|
12
|
+
export * as keyhelper from "./keyhelper.js";
|
|
13
|
+
//=======================================================//
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { generateKeyPair } from "libsignal-xeuka/src/curve.js";
|
|
3
|
+
import * as nodeCrypto from "crypto";
|
|
4
|
+
//=======================================================//
|
|
5
|
+
export function generateSenderKey() {
|
|
6
|
+
return nodeCrypto.randomBytes(32);
|
|
7
|
+
}
|
|
8
|
+
export function generateSenderKeyId() {
|
|
9
|
+
return nodeCrypto.randomInt(2147483647);
|
|
10
|
+
}
|
|
11
|
+
export function generateSenderSigningKey(key) {
|
|
12
|
+
if (!key) {
|
|
13
|
+
key = generateKeyPair();
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
public: Buffer.from(key.pubKey),
|
|
17
|
+
private: Buffer.from(key.privKey)
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//=======================================================//
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { calculateMAC } from "libsignal-xeuka/src/crypto.js";
|
|
3
|
+
import { SenderMessageKey } from "./sender-message-key.js";
|
|
4
|
+
//=======================================================//
|
|
5
|
+
export class SenderChainKey {
|
|
6
|
+
constructor(iteration, chainKey) {
|
|
7
|
+
this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
|
|
8
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02]);
|
|
9
|
+
this.iteration = iteration;
|
|
10
|
+
this.chainKey = Buffer.from(chainKey);
|
|
11
|
+
}
|
|
12
|
+
getIteration() {
|
|
13
|
+
return this.iteration;
|
|
14
|
+
}
|
|
15
|
+
getSenderMessageKey() {
|
|
16
|
+
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
|
|
17
|
+
}
|
|
18
|
+
getNext() {
|
|
19
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
|
|
20
|
+
}
|
|
21
|
+
getSeed() {
|
|
22
|
+
return this.chainKey;
|
|
23
|
+
}
|
|
24
|
+
getDerivative(seed, key) {
|
|
25
|
+
return calculateMAC(key, seed);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//=======================================================//
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { CiphertextMessage } from "./ciphertext-message.js";
|
|
3
|
+
import { proto } from "../../../WAProto/index.js";
|
|
4
|
+
//=======================================================//
|
|
5
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
6
|
+
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
7
|
+
super();
|
|
8
|
+
if (serialized) {
|
|
9
|
+
try {
|
|
10
|
+
const message = serialized.slice(1);
|
|
11
|
+
const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
|
|
12
|
+
this.serialized = serialized;
|
|
13
|
+
this.id = distributionMessage.id;
|
|
14
|
+
this.iteration = distributionMessage.iteration;
|
|
15
|
+
this.chainKey =
|
|
16
|
+
typeof distributionMessage.chainKey === "string"
|
|
17
|
+
? Buffer.from(distributionMessage.chainKey, "base64")
|
|
18
|
+
: distributionMessage.chainKey;
|
|
19
|
+
this.signatureKey =
|
|
20
|
+
typeof distributionMessage.signingKey === "string"
|
|
21
|
+
? Buffer.from(distributionMessage.signingKey, "base64")
|
|
22
|
+
: distributionMessage.signingKey;
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
throw new Error(String(e));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
|
|
30
|
+
this.id = id;
|
|
31
|
+
this.iteration = iteration;
|
|
32
|
+
this.chainKey = chainKey;
|
|
33
|
+
this.signatureKey = signatureKey;
|
|
34
|
+
const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
|
|
35
|
+
id,
|
|
36
|
+
iteration,
|
|
37
|
+
chainKey,
|
|
38
|
+
signingKey: this.signatureKey
|
|
39
|
+
})).finish();
|
|
40
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
intsToByteHighAndLow(highValue, lowValue) {
|
|
44
|
+
return (((highValue << 4) | lowValue) & 0xff) % 256;
|
|
45
|
+
}
|
|
46
|
+
serialize() {
|
|
47
|
+
return this.serialized;
|
|
48
|
+
}
|
|
49
|
+
getType() {
|
|
50
|
+
return this.SENDERKEY_DISTRIBUTION_TYPE;
|
|
51
|
+
}
|
|
52
|
+
getIteration() {
|
|
53
|
+
return this.iteration;
|
|
54
|
+
}
|
|
55
|
+
getChainKey() {
|
|
56
|
+
return this.chainKey;
|
|
57
|
+
}
|
|
58
|
+
getSignatureKey() {
|
|
59
|
+
return this.signatureKey;
|
|
60
|
+
}
|
|
61
|
+
getId() {
|
|
62
|
+
return this.id;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//=======================================================//
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
//=======================================================//
|
|
2
|
+
import { calculateSignature, verifySignature } from "libsignal-xeuka/src/curve.js";
|
|
3
|
+
import { CiphertextMessage } from "./ciphertext-message.js";
|
|
4
|
+
import { proto } from "../../../WAProto/index.js";
|
|
5
|
+
//=======================================================//
|
|
6
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
7
|
+
constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
|
|
8
|
+
super();
|
|
9
|
+
this.SIGNATURE_LENGTH = 64;
|
|
10
|
+
if (serialized) {
|
|
11
|
+
const version = serialized[0];
|
|
12
|
+
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
|
|
13
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
14
|
+
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
|
|
15
|
+
this.serialized = serialized;
|
|
16
|
+
this.messageVersion = (version & 0xff) >> 4;
|
|
17
|
+
this.keyId = senderKeyMessage.id;
|
|
18
|
+
this.iteration = senderKeyMessage.iteration;
|
|
19
|
+
this.ciphertext =
|
|
20
|
+
typeof senderKeyMessage.ciphertext === "string"
|
|
21
|
+
? Buffer.from(senderKeyMessage.ciphertext, "base64")
|
|
22
|
+
: senderKeyMessage.ciphertext;
|
|
23
|
+
this.signature = signature;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
|
|
27
|
+
const ciphertextBuffer = Buffer.from(ciphertext);
|
|
28
|
+
const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
|
|
29
|
+
id: keyId,
|
|
30
|
+
iteration: iteration,
|
|
31
|
+
ciphertext: ciphertextBuffer
|
|
32
|
+
})).finish();
|
|
33
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
|
|
34
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
|
|
35
|
+
this.messageVersion = this.CURRENT_VERSION;
|
|
36
|
+
this.keyId = keyId;
|
|
37
|
+
this.iteration = iteration;
|
|
38
|
+
this.ciphertext = ciphertextBuffer;
|
|
39
|
+
this.signature = signature;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
getKeyId() {
|
|
43
|
+
return this.keyId;
|
|
44
|
+
}
|
|
45
|
+
getIteration() {
|
|
46
|
+
return this.iteration;
|
|
47
|
+
}
|
|
48
|
+
getCipherText() {
|
|
49
|
+
return this.ciphertext;
|
|
50
|
+
}
|
|
51
|
+
verifySignature(signatureKey) {
|
|
52
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
|
|
53
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
54
|
+
const res = verifySignature(signatureKey, part1, part2);
|
|
55
|
+
if (!res)
|
|
56
|
+
throw new Error("Invalid signature!");
|
|
57
|
+
}
|
|
58
|
+
getSignature(signatureKey, serialized) {
|
|
59
|
+
return Buffer.from(calculateSignature(signatureKey, serialized));
|
|
60
|
+
}
|
|
61
|
+
serialize() {
|
|
62
|
+
return this.serialized;
|
|
63
|
+
}
|
|
64
|
+
getType() {
|
|
65
|
+
return 4;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
//=======================================================//
|