@yannajah/baileys 0.0.0-stage → 1.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 +23 -0
- package/LICENSE.jexkcode +21 -0
- package/README.md +41 -2
- package/THIRD_PARTY_NOTICES.md +15 -0
- package/WAProto/index.d.ts +16922 -0
- package/WAProto/index.js +122451 -0
- package/engine-requirements.js +10 -0
- package/lib/Defaults/baileys-version.json +3 -0
- package/lib/Defaults/index.js +156 -0
- package/lib/Defaults/phonenumber-mcc.json +223 -0
- package/lib/Signal/Group/ciphertext-message.js +15 -0
- package/lib/Signal/Group/group-session-builder.js +64 -0
- package/lib/Signal/Group/group_cipher.js +96 -0
- package/lib/Signal/Group/index.js +57 -0
- package/lib/Signal/Group/keyhelper.js +55 -0
- package/lib/Signal/Group/queue-job.js +57 -0
- package/lib/Signal/Group/sender-chain-key.js +34 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +69 -0
- package/lib/Signal/Group/sender-key-name.js +51 -0
- package/lib/Signal/Group/sender-key-record.js +53 -0
- package/lib/Signal/Group/sender-key-state.js +99 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +174 -0
- package/lib/Socket/Client/abstract-socket-client.js +13 -0
- package/lib/Socket/Client/index.js +19 -0
- package/lib/Socket/Client/mobile-socket-client.js +65 -0
- package/lib/Socket/Client/web-socket-client.js +87 -0
- package/lib/Socket/business.js +260 -0
- package/lib/Socket/chats.js +1002 -0
- package/lib/Socket/clutch-handler.d.ts +27 -0
- package/lib/Socket/clutch-handler.js +880 -0
- package/lib/Socket/groups.js +317 -0
- package/lib/Socket/index.js +97 -0
- package/lib/Socket/messages-recv.js +1154 -0
- package/lib/Socket/messages-send.js +946 -0
- package/lib/Socket/newsletter.js +411 -0
- package/lib/Socket/registration.js +166 -0
- package/lib/Socket/socket.js +688 -0
- package/lib/Socket/usync.js +70 -0
- package/lib/Store/index.js +10 -0
- package/lib/Store/make-cache-manager-store.js +83 -0
- package/lib/Store/make-in-memory-store.js +457 -0
- package/lib/Store/make-ordered-dictionary.js +81 -0
- package/lib/Store/object-repository.js +27 -0
- package/lib/Types/Auth.js +2 -0
- package/lib/Types/Call.js +2 -0
- package/lib/Types/Chat.js +4 -0
- package/lib/Types/Contact.js +2 -0
- package/lib/Types/Events.js +2 -0
- package/lib/Types/GroupMetadata.js +2 -0
- package/lib/Types/Label.js +27 -0
- package/lib/Types/LabelAssociation.js +9 -0
- package/lib/Types/Message.js +10 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +98 -0
- package/lib/Types/Newsletter.js +41 -0
- package/lib/Types/Product.js +2 -0
- package/lib/Types/Signal.js +2 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +2 -0
- package/lib/Types/USync.js +2 -0
- package/lib/Types/index.js +43 -0
- package/lib/Utils/MessageBuilder.d.ts +215 -0
- package/lib/Utils/MessageBuilder.js +4001 -0
- package/lib/Utils/a2ui.d.ts +202 -0
- package/lib/Utils/a2ui.js +770 -0
- package/lib/Utils/auth-utils.js +206 -0
- package/lib/Utils/baileys-event-stream.js +63 -0
- package/lib/Utils/bot-signature.d.ts +22 -0
- package/lib/Utils/bot-signature.js +125 -0
- package/lib/Utils/business.js +234 -0
- package/lib/Utils/channel-manager.d.ts +39 -0
- package/lib/Utils/channel-manager.js +312 -0
- package/lib/Utils/chat-utils.js +729 -0
- package/lib/Utils/crypto.js +151 -0
- package/lib/Utils/decode-wa-message.js +198 -0
- package/lib/Utils/event-buffer.js +514 -0
- package/lib/Utils/extras.d.ts +449 -0
- package/lib/Utils/extras.js +1377 -0
- package/lib/Utils/generics.js +574 -0
- package/lib/Utils/history.js +96 -0
- package/lib/Utils/html-app.d.ts +39 -0
- package/lib/Utils/html-app.js +307 -0
- package/lib/Utils/index.d.ts +23 -0
- package/lib/Utils/index.js +51 -0
- package/lib/Utils/link-preview.js +93 -0
- package/lib/Utils/logger.js +7 -0
- package/lib/Utils/lt-hash.js +51 -0
- package/lib/Utils/make-mutex.js +43 -0
- package/lib/Utils/message-retry-manager.d.ts +42 -0
- package/lib/Utils/message-retry-manager.js +309 -0
- package/lib/Utils/messages-media.js +861 -0
- package/lib/Utils/messages.js +1085 -0
- package/lib/Utils/metaai.d.ts +438 -0
- package/lib/Utils/metaai.js +911 -0
- package/lib/Utils/mini-app.d.ts +93 -0
- package/lib/Utils/mini-app.js +453 -0
- package/lib/Utils/modern-messages.d.ts +150 -0
- package/lib/Utils/modern-messages.js +498 -0
- package/lib/Utils/native-flow.d.ts +14 -0
- package/lib/Utils/native-flow.js +122 -0
- package/lib/Utils/newsletter-status.d.ts +163 -0
- package/lib/Utils/newsletter-status.js +695 -0
- package/lib/Utils/noise-handler.js +155 -0
- package/lib/Utils/payment-messages.d.ts +59 -0
- package/lib/Utils/payment-messages.js +164 -0
- package/lib/Utils/process-message.js +334 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +271 -0
- package/lib/Utils/scheduled-message.d.ts +82 -0
- package/lib/Utils/scheduled-message.js +126 -0
- package/lib/Utils/signal.js +153 -0
- package/lib/Utils/spr.d.ts +26 -0
- package/lib/Utils/spr.js +525 -0
- package/lib/Utils/status-stickers.d.ts +78 -0
- package/lib/Utils/status-stickers.js +275 -0
- package/lib/Utils/telegram-adapter.d.ts +28 -0
- package/lib/Utils/telegram-adapter.js +285 -0
- package/lib/Utils/use-multi-file-auth-state.js +153 -0
- package/lib/Utils/validate-connection.js +235 -0
- package/lib/WABinary/constants.js +40 -0
- package/lib/WABinary/decode.js +252 -0
- package/lib/WABinary/encode.js +265 -0
- package/lib/WABinary/generic-utils.js +178 -0
- package/lib/WABinary/index.js +21 -0
- package/lib/WABinary/jid-utils.js +81 -0
- package/lib/WABinary/types.js +2 -0
- package/lib/WAM/BinaryInfo.js +13 -0
- package/lib/WAM/constants.js +15350 -0
- package/lib/WAM/encode.js +155 -0
- package/lib/WAM/index.js +19 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
- package/lib/WAUSync/Protocols/index.js +20 -0
- package/lib/WAUSync/USyncQuery.js +89 -0
- package/lib/WAUSync/USyncUser.js +26 -0
- package/lib/WAUSync/index.js +19 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.js +99 -0
- package/package.json +110 -4
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { proto } from "../../WAProto";
|
|
2
|
+
export const SCHEDULED_MSG_META_TYPE: "scheduled_message";
|
|
3
|
+
export const SCHEDULED_MSG_MAX_PER_CHAT: 30;
|
|
4
|
+
export const SCHEDULED_MSG_MAX_MEDIA: 1;
|
|
5
|
+
export const SCHEDULED_MSG_RESOURCE_LIMIT_NACK_CODE: 419;
|
|
6
|
+
export const SCHEDULED_MSG_REVEAL_KEY_BYTES: 32;
|
|
7
|
+
export const SCHEDULED_MSG_REVEAL_KEY_IV_BYTES: 12;
|
|
8
|
+
export const SCHEDULED_MSG_REVEAL_KEY_RETENTION_DAYS: 30;
|
|
9
|
+
export const SCHEDULED_MSG_TAG_BYTES: 16;
|
|
10
|
+
export const SCHEDULED_MSG_WINDOW: {
|
|
11
|
+
readonly chat: {
|
|
12
|
+
readonly minSeconds: 600;
|
|
13
|
+
readonly maxSeconds: 1209600;
|
|
14
|
+
};
|
|
15
|
+
readonly newsletter: {
|
|
16
|
+
readonly minSeconds: 600;
|
|
17
|
+
readonly maxSeconds: 2592000;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export const generateRevealKey: () => Buffer;
|
|
21
|
+
export const generateRevealKeyId: () => string;
|
|
22
|
+
export const encryptWithRevealKey: (plaintext: any, revealKey: any) => {
|
|
23
|
+
encIv: Buffer;
|
|
24
|
+
encPayload: Buffer;
|
|
25
|
+
};
|
|
26
|
+
export const decryptWithRevealKey: (encPayload: any, encIv: any, revealKey: any) => Buffer;
|
|
27
|
+
export const buildConditionalRevealMessage: ({ encPayload, encIv, revealKeyId }: {
|
|
28
|
+
encPayload: any;
|
|
29
|
+
encIv: any;
|
|
30
|
+
revealKeyId: any;
|
|
31
|
+
}) => {
|
|
32
|
+
conditionalRevealMessage: {
|
|
33
|
+
conditionalRevealMessageType: proto.Message.ConditionalRevealMessage.ConditionalRevealMessageType;
|
|
34
|
+
encPayload: Buffer;
|
|
35
|
+
encIv: Buffer;
|
|
36
|
+
revealKeyId: any;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export const encodeScheduledMessage: (message: any, revealKey?: Buffer) => {
|
|
40
|
+
revealKey: Buffer;
|
|
41
|
+
revealKeyId: string;
|
|
42
|
+
encIv: Buffer;
|
|
43
|
+
encPayload: Buffer;
|
|
44
|
+
message: {
|
|
45
|
+
conditionalRevealMessage: {
|
|
46
|
+
conditionalRevealMessageType: proto.Message.ConditionalRevealMessage.ConditionalRevealMessageType;
|
|
47
|
+
encPayload: Buffer;
|
|
48
|
+
encIv: Buffer;
|
|
49
|
+
revealKeyId: any;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export const decodeScheduledMessage: (conditionalRevealMessage: any, revealKey: any) => proto.Message;
|
|
54
|
+
export const buildScheduledMsgMetaNode: ({ kind, scheduledTimestampS, revealKeyId, revealKey }: {
|
|
55
|
+
kind?: string | undefined;
|
|
56
|
+
scheduledTimestampS?: any;
|
|
57
|
+
revealKeyId: any;
|
|
58
|
+
revealKey: any;
|
|
59
|
+
}) => {
|
|
60
|
+
tag: string;
|
|
61
|
+
attrs: {
|
|
62
|
+
type: string;
|
|
63
|
+
st?: string;
|
|
64
|
+
};
|
|
65
|
+
content: {
|
|
66
|
+
tag: string;
|
|
67
|
+
attrs: {
|
|
68
|
+
rkid: any;
|
|
69
|
+
};
|
|
70
|
+
content: Buffer | undefined;
|
|
71
|
+
}[];
|
|
72
|
+
};
|
|
73
|
+
export const buildUnscheduleProtocolMessage: (key: any) => {
|
|
74
|
+
protocolMessage: {
|
|
75
|
+
key: any;
|
|
76
|
+
type: proto.Message.ProtocolMessage.Type;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
export const isScheduledTimeValid: (scheduledTimestampS: any, nowSeconds?: number, window?: {
|
|
80
|
+
readonly minSeconds: 600;
|
|
81
|
+
readonly maxSeconds: 1209600;
|
|
82
|
+
}) => boolean;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isScheduledTimeValid = exports.buildUnscheduleProtocolMessage = exports.buildScheduledMsgMetaNode = exports.decodeScheduledMessage = exports.encodeScheduledMessage = exports.buildConditionalRevealMessage = exports.decryptWithRevealKey = exports.encryptWithRevealKey = exports.generateRevealKeyId = exports.generateRevealKey = exports.SCHEDULED_MSG_WINDOW = exports.SCHEDULED_MSG_TAG_BYTES = exports.SCHEDULED_MSG_REVEAL_KEY_RETENTION_DAYS = exports.SCHEDULED_MSG_REVEAL_KEY_IV_BYTES = exports.SCHEDULED_MSG_REVEAL_KEY_BYTES = exports.SCHEDULED_MSG_RESOURCE_LIMIT_NACK_CODE = exports.SCHEDULED_MSG_MAX_MEDIA = exports.SCHEDULED_MSG_MAX_PER_CHAT = exports.SCHEDULED_MSG_META_TYPE = void 0;
|
|
4
|
+
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const WAProto_1 = require("../../WAProto");
|
|
7
|
+
|
|
8
|
+
exports.SCHEDULED_MSG_META_TYPE = 'scheduled_message';
|
|
9
|
+
exports.SCHEDULED_MSG_MAX_PER_CHAT = 30;
|
|
10
|
+
exports.SCHEDULED_MSG_MAX_MEDIA = 1;
|
|
11
|
+
exports.SCHEDULED_MSG_RESOURCE_LIMIT_NACK_CODE = 419;
|
|
12
|
+
exports.SCHEDULED_MSG_REVEAL_KEY_BYTES = 32;
|
|
13
|
+
exports.SCHEDULED_MSG_REVEAL_KEY_IV_BYTES = 12;
|
|
14
|
+
exports.SCHEDULED_MSG_REVEAL_KEY_RETENTION_DAYS = 30;
|
|
15
|
+
exports.SCHEDULED_MSG_TAG_BYTES = 16;
|
|
16
|
+
|
|
17
|
+
exports.SCHEDULED_MSG_WINDOW = Object.freeze({
|
|
18
|
+
chat: { minSeconds: 600, maxSeconds: 1209600 },
|
|
19
|
+
newsletter: { minSeconds: 600, maxSeconds: 2592000 }
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const generateRevealKey = () => (0, crypto_1.randomBytes)(exports.SCHEDULED_MSG_REVEAL_KEY_BYTES);
|
|
23
|
+
exports.generateRevealKey = generateRevealKey;
|
|
24
|
+
|
|
25
|
+
const generateRevealKeyId = () => (0, crypto_1.randomUUID)();
|
|
26
|
+
exports.generateRevealKeyId = generateRevealKeyId;
|
|
27
|
+
|
|
28
|
+
const encryptWithRevealKey = (plaintext, revealKey) => {
|
|
29
|
+
const key = Buffer.from(revealKey);
|
|
30
|
+
if (key.length !== exports.SCHEDULED_MSG_REVEAL_KEY_BYTES) {
|
|
31
|
+
throw new TypeError(`reveal key must be ${exports.SCHEDULED_MSG_REVEAL_KEY_BYTES} bytes`);
|
|
32
|
+
}
|
|
33
|
+
const encIv = (0, crypto_1.randomBytes)(exports.SCHEDULED_MSG_REVEAL_KEY_IV_BYTES);
|
|
34
|
+
const cipher = (0, crypto_1.createCipheriv)('aes-256-gcm', key, encIv);
|
|
35
|
+
const body = Buffer.concat([cipher.update(Buffer.from(plaintext)), cipher.final()]);
|
|
36
|
+
return { encIv, encPayload: Buffer.concat([body, cipher.getAuthTag()]) };
|
|
37
|
+
};
|
|
38
|
+
exports.encryptWithRevealKey = encryptWithRevealKey;
|
|
39
|
+
|
|
40
|
+
const decryptWithRevealKey = (encPayload, encIv, revealKey) => {
|
|
41
|
+
const key = Buffer.from(revealKey);
|
|
42
|
+
const payload = Buffer.from(encPayload);
|
|
43
|
+
if (payload.length < exports.SCHEDULED_MSG_TAG_BYTES) {
|
|
44
|
+
throw new TypeError('encrypted payload is too short to carry an auth tag');
|
|
45
|
+
}
|
|
46
|
+
const decipher = (0, crypto_1.createDecipheriv)('aes-256-gcm', key, Buffer.from(encIv));
|
|
47
|
+
decipher.setAuthTag(payload.subarray(payload.length - exports.SCHEDULED_MSG_TAG_BYTES));
|
|
48
|
+
return Buffer.concat([
|
|
49
|
+
decipher.update(payload.subarray(0, payload.length - exports.SCHEDULED_MSG_TAG_BYTES)),
|
|
50
|
+
decipher.final()
|
|
51
|
+
]);
|
|
52
|
+
};
|
|
53
|
+
exports.decryptWithRevealKey = decryptWithRevealKey;
|
|
54
|
+
|
|
55
|
+
const buildConditionalRevealMessage = ({ encPayload, encIv, revealKeyId }) => ({
|
|
56
|
+
conditionalRevealMessage: {
|
|
57
|
+
conditionalRevealMessageType: WAProto_1.proto.Message.ConditionalRevealMessage.ConditionalRevealMessageType.SCHEDULED_MESSAGE,
|
|
58
|
+
encPayload: Buffer.from(encPayload),
|
|
59
|
+
encIv: Buffer.from(encIv),
|
|
60
|
+
revealKeyId
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
exports.buildConditionalRevealMessage = buildConditionalRevealMessage;
|
|
64
|
+
|
|
65
|
+
const encodeScheduledMessage = (message, revealKey = (0, exports.generateRevealKey)()) => {
|
|
66
|
+
const plaintext = WAProto_1.proto.Message.encode(WAProto_1.proto.Message.fromObject(message)).finish();
|
|
67
|
+
const { encIv, encPayload } = (0, exports.encryptWithRevealKey)(plaintext, revealKey);
|
|
68
|
+
const revealKeyId = (0, exports.generateRevealKeyId)();
|
|
69
|
+
return {
|
|
70
|
+
revealKey,
|
|
71
|
+
revealKeyId,
|
|
72
|
+
encIv,
|
|
73
|
+
encPayload,
|
|
74
|
+
message: (0, exports.buildConditionalRevealMessage)({ encPayload, encIv, revealKeyId })
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
exports.encodeScheduledMessage = encodeScheduledMessage;
|
|
78
|
+
|
|
79
|
+
const decodeScheduledMessage = (conditionalRevealMessage, revealKey) => {
|
|
80
|
+
const inner = conditionalRevealMessage?.conditionalRevealMessage ?? conditionalRevealMessage;
|
|
81
|
+
if (!inner?.encPayload || !inner?.encIv) {
|
|
82
|
+
throw new TypeError('conditionalRevealMessage is missing encPayload or encIv');
|
|
83
|
+
}
|
|
84
|
+
return WAProto_1.proto.Message.decode((0, exports.decryptWithRevealKey)(inner.encPayload, inner.encIv, revealKey));
|
|
85
|
+
};
|
|
86
|
+
exports.decodeScheduledMessage = decodeScheduledMessage;
|
|
87
|
+
|
|
88
|
+
const buildScheduledMsgMetaNode = ({ kind = 'schedule', scheduledTimestampS, revealKeyId, revealKey }) => {
|
|
89
|
+
if (!revealKeyId) {
|
|
90
|
+
throw new TypeError('revealKeyId is required');
|
|
91
|
+
}
|
|
92
|
+
if (kind === 'schedule' && !revealKey) {
|
|
93
|
+
throw new TypeError('scheduling requires the reveal key');
|
|
94
|
+
}
|
|
95
|
+
const attrs = { type: exports.SCHEDULED_MSG_META_TYPE };
|
|
96
|
+
if (kind === 'schedule') {
|
|
97
|
+
if (!Number.isFinite(Number(scheduledTimestampS))) {
|
|
98
|
+
throw new TypeError('scheduledTimestampS must be a unix timestamp in seconds');
|
|
99
|
+
}
|
|
100
|
+
attrs.st = String(Math.floor(Number(scheduledTimestampS)));
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
tag: 'meta',
|
|
104
|
+
attrs,
|
|
105
|
+
content: [{
|
|
106
|
+
tag: 'key',
|
|
107
|
+
attrs: { rkid: revealKeyId },
|
|
108
|
+
content: kind === 'schedule' ? Buffer.from(revealKey) : undefined
|
|
109
|
+
}]
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
exports.buildScheduledMsgMetaNode = buildScheduledMsgMetaNode;
|
|
113
|
+
|
|
114
|
+
const buildUnscheduleProtocolMessage = (key) => ({
|
|
115
|
+
protocolMessage: {
|
|
116
|
+
key,
|
|
117
|
+
type: WAProto_1.proto.Message.ProtocolMessage.Type.MESSAGE_UNSCHEDULE
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
exports.buildUnscheduleProtocolMessage = buildUnscheduleProtocolMessage;
|
|
121
|
+
|
|
122
|
+
const isScheduledTimeValid = (scheduledTimestampS, nowSeconds = Math.floor(Date.now() / 1000), window = exports.SCHEDULED_MSG_WINDOW.chat) => {
|
|
123
|
+
const rounded = Math.floor(nowSeconds / 60) * 60;
|
|
124
|
+
return scheduledTimestampS - rounded >= window.minSeconds && scheduledTimestampS - nowSeconds <= window.maxSeconds;
|
|
125
|
+
};
|
|
126
|
+
exports.isScheduledTimeValid = isScheduledTimeValid;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getNextPreKeysNode = exports.getNextPreKeys = exports.extractDeviceJids = exports.parseAndInjectE2ESessions = exports.xmppPreKey = exports.xmppSignedPreKey = exports.generateOrGetPreKeys = exports.getPreKeys = exports.createSignalIdentity = void 0;
|
|
4
|
+
const Defaults_1 = require("../Defaults");
|
|
5
|
+
const WABinary_1 = require("../WABinary");
|
|
6
|
+
const crypto_1 = require("./crypto");
|
|
7
|
+
const lodash_1 = require("lodash")
|
|
8
|
+
const generics_1 = require("./generics");
|
|
9
|
+
const createSignalIdentity = (wid, accountSignatureKey) => {
|
|
10
|
+
return {
|
|
11
|
+
identifier: { name: wid, deviceId: 0 },
|
|
12
|
+
identifierKey: (0, crypto_1.generateSignalPubKey)(accountSignatureKey)
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
exports.createSignalIdentity = createSignalIdentity;
|
|
16
|
+
const getPreKeys = async ({ get }, min, limit) => {
|
|
17
|
+
const idList = [];
|
|
18
|
+
for (let id = min; id < limit; id++) {
|
|
19
|
+
idList.push(id.toString());
|
|
20
|
+
}
|
|
21
|
+
return get('pre-key', idList);
|
|
22
|
+
};
|
|
23
|
+
exports.getPreKeys = getPreKeys;
|
|
24
|
+
const generateOrGetPreKeys = (creds, range) => {
|
|
25
|
+
const avaliable = creds.nextPreKeyId - creds.firstUnuploadedPreKeyId;
|
|
26
|
+
const remaining = range - avaliable;
|
|
27
|
+
const lastPreKeyId = creds.nextPreKeyId + remaining - 1;
|
|
28
|
+
const newPreKeys = {};
|
|
29
|
+
if (remaining > 0) {
|
|
30
|
+
for (let i = creds.nextPreKeyId; i <= lastPreKeyId; i++) {
|
|
31
|
+
newPreKeys[i] = crypto_1.Curve.generateKeyPair();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
newPreKeys,
|
|
36
|
+
lastPreKeyId,
|
|
37
|
+
preKeysRange: [creds.firstUnuploadedPreKeyId, range],
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
exports.generateOrGetPreKeys = generateOrGetPreKeys;
|
|
41
|
+
const xmppSignedPreKey = (key) => ({
|
|
42
|
+
tag: 'skey',
|
|
43
|
+
attrs: {},
|
|
44
|
+
content: [
|
|
45
|
+
{ tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(key.keyId, 3) },
|
|
46
|
+
{ tag: 'value', attrs: {}, content: key.keyPair.public },
|
|
47
|
+
{ tag: 'signature', attrs: {}, content: key.signature }
|
|
48
|
+
]
|
|
49
|
+
});
|
|
50
|
+
exports.xmppSignedPreKey = xmppSignedPreKey;
|
|
51
|
+
const xmppPreKey = (pair, id) => ({
|
|
52
|
+
tag: 'key',
|
|
53
|
+
attrs: {},
|
|
54
|
+
content: [
|
|
55
|
+
{ tag: 'id', attrs: {}, content: (0, generics_1.encodeBigEndian)(id, 3) },
|
|
56
|
+
{ tag: 'value', attrs: {}, content: pair.public }
|
|
57
|
+
]
|
|
58
|
+
});
|
|
59
|
+
exports.xmppPreKey = xmppPreKey;
|
|
60
|
+
const parseAndInjectE2ESessions = async (node, repository) => {
|
|
61
|
+
const extractKey = (key) => (key ? ({
|
|
62
|
+
keyId: WABinary_1.getBinaryNodeChildUInt(key, 'id', 3),
|
|
63
|
+
publicKey: crypto_1.generateSignalPubKey(WABinary_1.getBinaryNodeChildBuffer(key, 'value')),
|
|
64
|
+
signature: WABinary_1.getBinaryNodeChildBuffer(key, 'signature')
|
|
65
|
+
}) : undefined)
|
|
66
|
+
const nodes = WABinary_1.getBinaryNodeChildren(WABinary_1.getBinaryNodeChild(node, 'list'), 'user')
|
|
67
|
+
for (const node of nodes) {
|
|
68
|
+
WABinary_1.assertNodeErrorFree(node)
|
|
69
|
+
}
|
|
70
|
+
// Most of the work in repository.injectE2ESession is CPU intensive, not IO
|
|
71
|
+
// So Promise.all doesn't really help here,
|
|
72
|
+
// but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
|
|
73
|
+
// This way we chunk it in smaller parts and between those parts we can yield to the event loop
|
|
74
|
+
// It's rare case when you need to E2E sessions for so many users, but it's possible
|
|
75
|
+
const chunkSize = 100
|
|
76
|
+
const chunks = lodash_1.chunk(nodes, chunkSize)
|
|
77
|
+
for (const nodesChunk of chunks) {
|
|
78
|
+
await Promise.all(nodesChunk.map(async (node) => {
|
|
79
|
+
const signedKey = WABinary_1.getBinaryNodeChild(node, 'skey')
|
|
80
|
+
const key = WABinary_1.getBinaryNodeChild(node, 'key')
|
|
81
|
+
const identity = WABinary_1.getBinaryNodeChildBuffer(node, 'identity')
|
|
82
|
+
const jid = node.attrs.jid
|
|
83
|
+
const registrationId = WABinary_1.getBinaryNodeChildUInt(node, 'registration', 4)
|
|
84
|
+
await repository.injectE2ESession({
|
|
85
|
+
jid,
|
|
86
|
+
session: {
|
|
87
|
+
registrationId: registrationId,
|
|
88
|
+
identityKey: crypto_1.generateSignalPubKey(identity),
|
|
89
|
+
signedPreKey: extractKey(signedKey),
|
|
90
|
+
preKey: extractKey(key)
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
}))
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
|
|
97
|
+
const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
|
|
98
|
+
const { user: myUser, device: myDevice } = WABinary_1.jidDecode(myJid)
|
|
99
|
+
const extracted = []
|
|
100
|
+
for (const userResult of result) {
|
|
101
|
+
const { devices, id } = userResult
|
|
102
|
+
const { user } = WABinary_1.jidDecode(id)
|
|
103
|
+
const deviceList = devices?.deviceList
|
|
104
|
+
if (Array.isArray(deviceList)) {
|
|
105
|
+
for (const { id: device, keyIndex } of deviceList) {
|
|
106
|
+
if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
|
|
107
|
+
(myUser !== user || myDevice !== device) && // either different user or if me user, not this device
|
|
108
|
+
(device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
|
|
109
|
+
) {
|
|
110
|
+
extracted.push({ user, device })
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return extracted
|
|
116
|
+
}
|
|
117
|
+
exports.extractDeviceJids = extractDeviceJids;
|
|
118
|
+
/**
|
|
119
|
+
* get the next N keys for upload or processing
|
|
120
|
+
* @param count number of pre-keys to get or generate
|
|
121
|
+
*/
|
|
122
|
+
const getNextPreKeys = async ({ creds, keys }, count) => {
|
|
123
|
+
const { newPreKeys, lastPreKeyId, preKeysRange } = (0, exports.generateOrGetPreKeys)(creds, count);
|
|
124
|
+
const update = {
|
|
125
|
+
nextPreKeyId: Math.max(lastPreKeyId + 1, creds.nextPreKeyId),
|
|
126
|
+
firstUnuploadedPreKeyId: Math.max(creds.firstUnuploadedPreKeyId, lastPreKeyId + 1)
|
|
127
|
+
};
|
|
128
|
+
await keys.set({ 'pre-key': newPreKeys });
|
|
129
|
+
const preKeys = await (0, exports.getPreKeys)(keys, preKeysRange[0], preKeysRange[0] + preKeysRange[1]);
|
|
130
|
+
return { update, preKeys };
|
|
131
|
+
};
|
|
132
|
+
exports.getNextPreKeys = getNextPreKeys;
|
|
133
|
+
const getNextPreKeysNode = async (state, count) => {
|
|
134
|
+
const { creds } = state;
|
|
135
|
+
const { update, preKeys } = await (0, exports.getNextPreKeys)(state, count);
|
|
136
|
+
const node = {
|
|
137
|
+
tag: 'iq',
|
|
138
|
+
attrs: {
|
|
139
|
+
xmlns: 'encrypt',
|
|
140
|
+
type: 'set',
|
|
141
|
+
to: WABinary_1.S_WHATSAPP_NET,
|
|
142
|
+
},
|
|
143
|
+
content: [
|
|
144
|
+
{ tag: 'registration', attrs: {}, content: (0, generics_1.encodeBigEndian)(creds.registrationId) },
|
|
145
|
+
{ tag: 'type', attrs: {}, content: Defaults_1.KEY_BUNDLE_TYPE },
|
|
146
|
+
{ tag: 'identity', attrs: {}, content: creds.signedIdentityKey.public },
|
|
147
|
+
{ tag: 'list', attrs: {}, content: Object.keys(preKeys).map(k => (0, exports.xmppPreKey)(preKeys[+k], +k)) },
|
|
148
|
+
(0, exports.xmppSignedPreKey)(creds.signedPreKey)
|
|
149
|
+
]
|
|
150
|
+
};
|
|
151
|
+
return { update, node };
|
|
152
|
+
};
|
|
153
|
+
exports.getNextPreKeysNode = getNextPreKeysNode;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function generateSigningKeyPair(): {
|
|
2
|
+
privateKey: Buffer;
|
|
3
|
+
publicKey: Buffer;
|
|
4
|
+
};
|
|
5
|
+
export function getMessageKey(seed: Buffer): {
|
|
6
|
+
iv: Buffer;
|
|
7
|
+
cipherKey: Buffer;
|
|
8
|
+
};
|
|
9
|
+
export function deriveReportingTag(message: any, msgId: string, remoteJid: string): Buffer;
|
|
10
|
+
export function encryptGroupMessage(plaintext: Buffer | Uint8Array, chainSeed: Buffer, privateKeyObject: any, keyId: number): Buffer;
|
|
11
|
+
export function createSKDM(keyId: number, iteration: number, chainSeed: Buffer, signingPubKey: Buffer): Buffer;
|
|
12
|
+
export function sPR(conn: any, jid: string, message: any, options?: {
|
|
13
|
+
messageId?: string;
|
|
14
|
+
additionalAttributes?: any;
|
|
15
|
+
useUserDevicesCache?: boolean;
|
|
16
|
+
useCachedGroupMetadata?: boolean;
|
|
17
|
+
statusJidList?: string[];
|
|
18
|
+
additionalNodes?: any[];
|
|
19
|
+
targetMe?: boolean;
|
|
20
|
+
mode?: "include" | "exclude";
|
|
21
|
+
target?: string[];
|
|
22
|
+
hideDecryptFail?: boolean;
|
|
23
|
+
deviceMode?: "all" | "primary" | "secondary";
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
export const selectiveRelay: typeof sPR;
|
|
26
|
+
export default sPR;
|