@x4r1s3/baileys 0.0.0-stage → 1.0.1
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/README.md +1245 -2
- package/SECURITY.md +47 -0
- package/WAProto/V +1 -0
- package/WAProto/WAProto.proto +8892 -0
- package/WAProto/index.d.ts +104128 -0
- package/WAProto/index.js +334920 -0
- package/assets/rolling-circle.svg +133 -0
- package/assets/x4r1s3-banner.svg +32 -0
- package/engine-requirements.js +27 -0
- package/lib/Builders/A2UI.js +245 -0
- package/lib/Builders/AIRich.js +179 -0
- package/lib/Builders/Button.js +108 -0
- package/lib/Builders/CFC.js +284 -0
- package/lib/Builders/Carousel.js +81 -0
- package/lib/Call/THIRD_PARTY_LICENSE.txt +21 -0
- package/lib/Call/astralena-v3.mjs +96 -0
- package/lib/Call/audio-feeder.mjs +201 -0
- package/lib/Call/audio-recorder.mjs +250 -0
- package/lib/Call/fetch-wasm-resources.mjs +104 -0
- package/lib/Call/index.mjs +707 -0
- package/lib/Call/relay-transport.mjs +563 -0
- package/lib/Call/signaling.mjs +660 -0
- package/lib/Call/types.mjs +17 -0
- package/lib/Call/video-feeder.mjs +94 -0
- package/lib/Call/voip-resources.mjs +259 -0
- package/lib/Call/wasm-engine.mjs +1372 -0
- package/lib/Call/worker-bootstrap.mjs +1042 -0
- package/lib/Defaults/ban-checker.js +56 -0
- package/lib/Defaults/index.js +170 -0
- package/lib/Signal/Group/ciphertext-message.js +23 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +93 -0
- package/lib/Signal/Group/index.js +23 -0
- package/lib/Signal/Group/keyhelper.js +29 -0
- package/lib/Signal/Group/sender-chain-key.js +37 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +74 -0
- package/lib/Signal/Group/sender-key-message.js +77 -0
- package/lib/Signal/Group/sender-key-name.js +59 -0
- package/lib/Signal/Group/sender-key-record.js +52 -0
- package/lib/Signal/Group/sender-key-state.js +95 -0
- package/lib/Signal/Group/sender-message-key.js +37 -0
- package/lib/Signal/libsignal.js +440 -0
- package/lib/Signal/lid-mapping.js +292 -0
- package/lib/Socket/Client/index.js +14 -0
- package/lib/Socket/Client/types.js +22 -0
- package/lib/Socket/Client/websocket.js +65 -0
- package/lib/Socket/aigroups.js +327 -0
- package/lib/Socket/ban-checker.js +165 -0
- package/lib/Socket/business.js +419 -0
- package/lib/Socket/chats.js +1921 -0
- package/lib/Socket/communities.js +443 -0
- package/lib/Socket/graphql.js +911 -0
- package/lib/Socket/groups.js +561 -0
- package/lib/Socket/index.js +49 -0
- package/lib/Socket/interactive-handler.js +612 -0
- package/lib/Socket/interop.js +575 -0
- package/lib/Socket/managed-account.js +220 -0
- package/lib/Socket/messages-recv.js +2193 -0
- package/lib/Socket/messages-send.js +4141 -0
- package/lib/Socket/mex.js +53 -0
- package/lib/Socket/newsletter.js +291 -0
- package/lib/Socket/privacy.js +458 -0
- package/lib/Socket/registration.js +440 -0
- package/lib/Socket/socket.js +1042 -0
- package/lib/Socket/status-actions.js +287 -0
- package/lib/Socket/username.js +241 -0
- package/lib/Store/index.js +16 -0
- package/lib/Store/make-cache-manager-store.js +101 -0
- package/lib/Store/make-in-memory-store.js +432 -0
- package/lib/Store/make-ordered-dictionary.js +90 -0
- package/lib/Store/object-repository.js +35 -0
- package/lib/Types/Auth.js +13 -0
- package/lib/Types/Bussines.js +13 -0
- package/lib/Types/Call.js +13 -0
- package/lib/Types/Chat.js +19 -0
- package/lib/Types/Contact.js +13 -0
- package/lib/Types/Events.js +13 -0
- package/lib/Types/GroupMetadata.js +13 -0
- package/lib/Types/Label.js +36 -0
- package/lib/Types/LabelAssociation.js +18 -0
- package/lib/Types/Message.js +30 -0
- package/lib/Types/Mex.js +53 -0
- package/lib/Types/Product.js +13 -0
- package/lib/Types/RichType.js +34 -0
- package/lib/Types/Signal.js +13 -0
- package/lib/Types/Socket.js +14 -0
- package/lib/Types/State.js +60 -0
- package/lib/Types/USync.js +13 -0
- package/lib/Types/index.js +37 -0
- package/lib/Utils/auth-utils.js +313 -0
- package/lib/Utils/ban-checker.js +48 -0
- package/lib/Utils/bot-capabilities.js +96 -0
- package/lib/Utils/bot-planning-replay.js +177 -0
- package/lib/Utils/browser-utils.js +40 -0
- package/lib/Utils/business.js +242 -0
- package/lib/Utils/c2582.js +147 -0
- package/lib/Utils/chat-utils.js +905 -0
- package/lib/Utils/companion-reg-client-utils.js +104 -0
- package/lib/Utils/consumer-application.js +114 -0
- package/lib/Utils/crypto.js +129 -0
- package/lib/Utils/decode-wa-message.js +496 -0
- package/lib/Utils/direct-chat-theme.js +164 -0
- package/lib/Utils/event-buffer.js +601 -0
- package/lib/Utils/experimental-proto.js +206 -0
- package/lib/Utils/games/miniapp.js +17 -0
- package/lib/Utils/games/slot-machine.js +270 -0
- package/lib/Utils/games/website-preview.js +306 -0
- package/lib/Utils/generics.js +402 -0
- package/lib/Utils/group-history.js +68 -0
- package/lib/Utils/group-wallpaper.js +168 -0
- package/lib/Utils/history.js +162 -0
- package/lib/Utils/identity-change-handler.js +60 -0
- package/lib/Utils/index.js +61 -0
- package/lib/Utils/jid-display-normalization.js +221 -0
- package/lib/Utils/link-preview.js +96 -0
- package/lib/Utils/logger.js +14 -0
- package/lib/Utils/lt-hash.js +19 -0
- package/lib/Utils/make-mutex.js +44 -0
- package/lib/Utils/message-composer.js +709 -0
- package/lib/Utils/message-inspect.js +79 -0
- package/lib/Utils/message-retry-manager.js +253 -0
- package/lib/Utils/message-security.js +167 -0
- package/lib/Utils/messages-media.js +1027 -0
- package/lib/Utils/messages.js +2467 -0
- package/lib/Utils/meta-ai-msmsg.js +215 -0
- package/lib/Utils/meta-compositing.js +239 -0
- package/lib/Utils/native-flow.js +158 -0
- package/lib/Utils/newsletter-live-update-utils.js +74 -0
- package/lib/Utils/newsletter-status-utils.js +152 -0
- package/lib/Utils/noise-handler.js +212 -0
- package/lib/Utils/offline-node-processor.js +51 -0
- package/lib/Utils/poll-manager.js +206 -0
- package/lib/Utils/portable-compat.js +12 -0
- package/lib/Utils/pre-key-manager.js +117 -0
- package/lib/Utils/process-message.js +610 -0
- package/lib/Utils/reporting-utils.js +269 -0
- package/lib/Utils/rich-message-utils.js +2841 -0
- package/lib/Utils/scheduled-message.js +107 -0
- package/lib/Utils/signal.js +212 -0
- package/lib/Utils/source-grep.js +57 -0
- package/lib/Utils/stanza-ack.js +49 -0
- package/lib/Utils/sticker-annotation.js +87 -0
- package/lib/Utils/sync-action-utils.js +59 -0
- package/lib/Utils/tc-token-utils.js +174 -0
- package/lib/Utils/use-multi-file-auth-state.js +132 -0
- package/lib/Utils/use-single-file-auth-state.js +108 -0
- package/lib/Utils/use-sqlite-auth-state.js +120 -0
- package/lib/Utils/validate-connection.js +221 -0
- package/lib/Utils/view-once-utils.js +218 -0
- package/lib/Utils/voip-rekey.js +32 -0
- package/lib/Utils/website-preview.js +27 -0
- package/lib/Utils/welcome-flow.js +228 -0
- package/lib/Utils/x4r1s3.js +144 -0
- package/lib/WABinary/constants.js +1475 -0
- package/lib/WABinary/decode.js +273 -0
- package/lib/WABinary/encode.js +231 -0
- package/lib/WABinary/generic-utils.js +242 -0
- package/lib/WABinary/index.js +17 -0
- package/lib/WABinary/jid-utils.js +107 -0
- package/lib/WABinary/types.js +13 -0
- package/lib/WAM/BinaryInfo.js +21 -0
- package/lib/WAM/constants.js +22864 -0
- package/lib/WAM/encode.js +161 -0
- package/lib/WAM/index.js +15 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +108 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +68 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +65 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +82 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +40 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +49 -0
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +46 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +36 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +62 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +40 -0
- package/lib/WAUSync/Protocols/index.js +24 -0
- package/lib/WAUSync/USyncQuery.js +213 -0
- package/lib/WAUSync/USyncUser.js +66 -0
- package/lib/WAUSync/index.js +15 -0
- package/lib/antiban.cjs +4726 -0
- package/lib/index.d.ts +1895 -0
- package/lib/index.js +41 -0
- package/package.json +125 -4
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Defaults/ban-checker.js
|
|
5
|
+
* About: Ban checker module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: MOBILE_ENDPOINT, MOBILE_PORT, WA_VERSION, WA_VERSION_HASH, MOBILE_TOKEN, MOBILE_REGISTRATION_ENDPOINT, MOBILE_USERAGENT, GRAPHQL_ENDPOINT; …
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { createHash } from 'crypto';
|
|
14
|
+
|
|
15
|
+
export const MOBILE_ENDPOINT =
|
|
16
|
+
process.env.X4R1S3_MOBILE_ENDPOINT || 'g.whatsapp.net';
|
|
17
|
+
|
|
18
|
+
export const MOBILE_PORT = Number(
|
|
19
|
+
process.env.X4R1S3_MOBILE_PORT || 443
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
export const WA_VERSION =
|
|
23
|
+
process.env.X4R1S3_WA_VERSION || '2.2413.51';
|
|
24
|
+
|
|
25
|
+
// Must be declared before MOBILE_TOKEN.
|
|
26
|
+
export const WA_VERSION_HASH = createHash('md5')
|
|
27
|
+
.update(WA_VERSION)
|
|
28
|
+
.digest('hex');
|
|
29
|
+
|
|
30
|
+
// Supply only the private prefix through the runtime environment.
|
|
31
|
+
export const MOBILE_TOKEN = Buffer.from(
|
|
32
|
+
`${process.env.X4R1S3_MOBILE_TOKEN_PREFIX || '0a1mLfGUIBVrMKF1RdvLI5lkRBvof6vn0fD2QRSM'}${WA_VERSION_HASH}`
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
export const MOBILE_REGISTRATION_ENDPOINT =
|
|
36
|
+
process.env.X4R1S3_MOBILE_REGISTRATION_ENDPOINT ||
|
|
37
|
+
'https://v.whatsapp.net/v2';
|
|
38
|
+
|
|
39
|
+
export const MOBILE_USERAGENT =
|
|
40
|
+
process.env.X4R1S3_MOBILE_USERAGENT ||
|
|
41
|
+
`WhatsApp/${WA_VERSION} iOS/17.5.1 Device/Apple-iPhone_13`;
|
|
42
|
+
|
|
43
|
+
export const GRAPHQL_ENDPOINT =
|
|
44
|
+
process.env.X4R1S3_GRAPHQL_ENDPOINT ||
|
|
45
|
+
'https://graph.whatsapp.com/graphql';
|
|
46
|
+
|
|
47
|
+
export const GRAPHQL_ACCESS_TOKEN =
|
|
48
|
+
process.env.X4R1S3_GRAPHQL_ACCESS_TOKEN ||
|
|
49
|
+
'WA|1015890928915437|3201f239340c1c8ec6262a6dad04200e';
|
|
50
|
+
|
|
51
|
+
export const GRAPHQL_BAN_STATUS_DOC_ID =
|
|
52
|
+
process.env.X4R1S3_GRAPHQL_BAN_STATUS_DOC_ID ||
|
|
53
|
+
'25573756098908502';
|
|
54
|
+
|
|
55
|
+
export const MOBILE_COUNTRY_CODE =
|
|
56
|
+
process.env.X4R1S3_PHONE_COUNTRY_CODE || '';
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Defaults/index.js
|
|
5
|
+
* About: Defaults module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: UNAUTHORIZED_CODES, BIZ_BOT_SUPPORT_PAYLOAD, DEFAULT_ORIGIN, CALL_VIDEO_PREFIX, CALL_AUDIO_PREFIX, DEF_CALLBACK_PREFIX, DEF_TAG_PREFIX, DONATE_URL; …
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { proto } from '../../WAProto/index.js';
|
|
14
|
+
import { makeLibSignalRepository } from '../Signal/libsignal.js';
|
|
15
|
+
import { Browsers } from '../Utils/browser-utils.js';
|
|
16
|
+
import logger from '../Utils/logger.js';
|
|
17
|
+
const version = [2, 3000, 1049983640];
|
|
18
|
+
export const UNAUTHORIZED_CODES = [401, 403, 419];
|
|
19
|
+
export const BIZ_BOT_SUPPORT_PAYLOAD = '{"version":1,"is_ai_message":true,"should_upload_client_logs":false,"should_show_system_message":false,"ticket_id":"7004947587700716","citation_items":[],"ticket_locale":"us"}';
|
|
20
|
+
export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
|
|
21
|
+
export const CALL_VIDEO_PREFIX = 'https://call.whatsapp.com/video/';
|
|
22
|
+
export const CALL_AUDIO_PREFIX = 'https://call.whatsapp.com/voice/';
|
|
23
|
+
export const DEF_CALLBACK_PREFIX = 'CB:';
|
|
24
|
+
export const DEF_TAG_PREFIX = 'TAG:';
|
|
25
|
+
export const DONATE_URL = 'https://www.npmjs.com/package/@x4r1s3/baileys'; //
|
|
26
|
+
export const LIBRARY_NAME = 'X4R1S3/bailey';
|
|
27
|
+
export const PHONE_CONNECTION_CB = 'CB:Pong';
|
|
28
|
+
export const WA_ADV_ACCOUNT_SIG_PREFIX = Buffer.from([6, 0]);
|
|
29
|
+
export const WA_ADV_DEVICE_SIG_PREFIX = Buffer.from([6, 1]);
|
|
30
|
+
export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX = Buffer.from([6, 5]);
|
|
31
|
+
export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX = Buffer.from([6, 6]);
|
|
32
|
+
export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
|
|
33
|
+
/** Status messages older than 24 hours are considered expired */
|
|
34
|
+
export const STATUS_EXPIRY_SECONDS = 24 * 60 * 60;
|
|
35
|
+
/** WA Web enforces a 14-day maximum age for placeholder resend requests */
|
|
36
|
+
export const PLACEHOLDER_MAX_AGE_SECONDS = 14 * 24 * 60 * 60;
|
|
37
|
+
export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
|
|
38
|
+
export const DICT_VERSION = 3;
|
|
39
|
+
export const KEY_BUNDLE_TYPE = Buffer.from([5]);
|
|
40
|
+
export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]); // last is "DICT_VERSION"
|
|
41
|
+
export const LEXER_REGEX = /(\/\/.*|\/\*[\s\S]*?\*\/|#.*)|("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`[\s\S]*?`)|(\b[a-zA-Z_]\w*\b)(?=\s*\()|(\b[a-zA-Z_]\w*\b)|(\b\d+(?:\.\d+)?\b)|(\s+|[^\w\s]+)/g;
|
|
42
|
+
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
|
|
43
|
+
export const URL_REGEX = /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
|
|
44
|
+
export const WA_CERT_DETAILS = {
|
|
45
|
+
SERIAL: 0,
|
|
46
|
+
ISSUER: 'WhatsAppLongTerm1',
|
|
47
|
+
PUBLIC_KEY: Buffer.from('142375574d0a587166aae71ebe516437c4a28b73e3695c6ce1f7f9545da8ee6b', 'hex')
|
|
48
|
+
};
|
|
49
|
+
export const PROCESSABLE_HISTORY_TYPES = [
|
|
50
|
+
proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP,
|
|
51
|
+
proto.HistorySync.HistorySyncType.PUSH_NAME,
|
|
52
|
+
proto.HistorySync.HistorySyncType.RECENT,
|
|
53
|
+
proto.HistorySync.HistorySyncType.FULL,
|
|
54
|
+
proto.HistorySync.HistorySyncType.ON_DEMAND,
|
|
55
|
+
proto.HistorySync.HistorySyncType.NON_BLOCKING_DATA,
|
|
56
|
+
proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3
|
|
57
|
+
];
|
|
58
|
+
export const DEFAULT_CONNECTION_CONFIG = {
|
|
59
|
+
version: version,
|
|
60
|
+
browser: Browsers.macOS('Chrome'),
|
|
61
|
+
waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
|
|
62
|
+
connectTimeoutMs: 20000,
|
|
63
|
+
keepAliveIntervalMs: 15000,
|
|
64
|
+
logger: logger.child({ class: 'baileys' }),
|
|
65
|
+
emitOwnEvents: true,
|
|
66
|
+
defaultQueryTimeoutMs: 60000,
|
|
67
|
+
customUploadHosts: [],
|
|
68
|
+
retryRequestDelayMs: 250,
|
|
69
|
+
maxMsgRetryCount: 3,
|
|
70
|
+
fireInitQueries: true,
|
|
71
|
+
auth: undefined,
|
|
72
|
+
markOnlineOnConnect: true,
|
|
73
|
+
syncFullHistory: true,
|
|
74
|
+
patchMessageBeforeSending: msg => msg,
|
|
75
|
+
shouldSyncHistoryMessage: ({ syncType }) => {
|
|
76
|
+
return syncType !== proto.HistorySync.HistorySyncType.FULL;
|
|
77
|
+
},
|
|
78
|
+
shouldIgnoreJid: () => false,
|
|
79
|
+
linkPreviewImageThumbnailWidth: 192,
|
|
80
|
+
transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3000 },
|
|
81
|
+
generateHighQualityLinkPreview: true,
|
|
82
|
+
enableAutoSessionRecreation: true,
|
|
83
|
+
enableRecentMessageCache: true,
|
|
84
|
+
options: {},
|
|
85
|
+
appStateMacVerification: {
|
|
86
|
+
patch: false,
|
|
87
|
+
snapshot: false
|
|
88
|
+
},
|
|
89
|
+
countryCode: 'US',
|
|
90
|
+
getMessage: async () => undefined,
|
|
91
|
+
cachedGroupMetadata: async () => undefined,
|
|
92
|
+
makeSignalRepository: makeLibSignalRepository
|
|
93
|
+
};
|
|
94
|
+
export const MEDIA_PATH_MAP = {
|
|
95
|
+
image: '/mms/image',
|
|
96
|
+
video: '/mms/video',
|
|
97
|
+
document: '/mms/document',
|
|
98
|
+
audio: '/mms/audio',
|
|
99
|
+
sticker: '/mms/image',
|
|
100
|
+
'sticker-pack': '/mms/sticker-pack',
|
|
101
|
+
'thumbnail-sticker-pack': '/mms/thumbnail-sticker-pack',
|
|
102
|
+
'thumbnail-link': '/mms/image',
|
|
103
|
+
'thumbnail-image': '/mms/image',
|
|
104
|
+
'thumbnail-video': '/mms/video',
|
|
105
|
+
'thumbnail-document': '/mms/document',
|
|
106
|
+
'product-catalog-image': '/product/image',
|
|
107
|
+
'md-app-state': '',
|
|
108
|
+
'md-msg-hist': '/mms/md-app-state',
|
|
109
|
+
'biz-cover-photo': '/pps/biz-cover-photo'
|
|
110
|
+
};
|
|
111
|
+
// Lia@Changes 06-02-26 --- Add newsletter media path for "/m1/" instead of "/o1/" (≧▽≦)
|
|
112
|
+
export const NEWSLETTER_MEDIA_PATH_MAP = {
|
|
113
|
+
image: '/newsletter/newsletter-image',
|
|
114
|
+
video: '/newsletter/newsletter-video',
|
|
115
|
+
document: '/newsletter/newsletter-document',
|
|
116
|
+
audio: '/newsletter/newsletter-audio',
|
|
117
|
+
sticker: '/newsletter/newsletter-image',
|
|
118
|
+
'thumbnail-link': '/newsletter/newsletter-thumbnail-link'
|
|
119
|
+
};
|
|
120
|
+
export const MEDIA_HKDF_KEY_MAPPING = {
|
|
121
|
+
audio: 'Audio',
|
|
122
|
+
document: 'Document',
|
|
123
|
+
gif: 'Video',
|
|
124
|
+
image: 'Image',
|
|
125
|
+
ppic: '',
|
|
126
|
+
product: 'Image',
|
|
127
|
+
ptt: 'Audio',
|
|
128
|
+
'sticker-pack': 'Sticker Pack',
|
|
129
|
+
'thumbnail-sticker-pack': 'Sticker Pack Thumbnail',
|
|
130
|
+
sticker: 'Image',
|
|
131
|
+
video: 'Video',
|
|
132
|
+
'thumbnail-document': 'Document Thumbnail',
|
|
133
|
+
'thumbnail-image': 'Image Thumbnail',
|
|
134
|
+
'thumbnail-video': 'Video Thumbnail',
|
|
135
|
+
'thumbnail-link': 'Link Thumbnail',
|
|
136
|
+
'md-msg-hist': 'History',
|
|
137
|
+
'md-app-state': 'App State',
|
|
138
|
+
'product-catalog-image': 'Product Catalog Image',
|
|
139
|
+
'payment-bg-image': 'Payment Background',
|
|
140
|
+
ptv: 'Video',
|
|
141
|
+
'biz-cover-photo': 'Image',
|
|
142
|
+
location: 'Location',
|
|
143
|
+
contact: 'Contact',
|
|
144
|
+
'voip-token': 'Voip Token'
|
|
145
|
+
};
|
|
146
|
+
export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
|
|
147
|
+
export const MIN_PREKEY_COUNT = 5;
|
|
148
|
+
export const INITIAL_PREKEY_COUNT = 812;
|
|
149
|
+
export const UPLOAD_TIMEOUT = 30000; // 30 seconds
|
|
150
|
+
export const DEFAULT_CACHE_TTLS = {
|
|
151
|
+
SIGNAL_STORE: 5 * 60, // 5 minutes
|
|
152
|
+
MSG_RETRY: 60 * 60, // 1 hour
|
|
153
|
+
CALL_OFFER: 5 * 60, // 5 minutes
|
|
154
|
+
USER_DEVICES: 5 * 60 // 5 minutes
|
|
155
|
+
};
|
|
156
|
+
/** 120s timeout for history sync stall detection, same as WA Web's handleChunkProgress / restartPausedTimer (g = 120) */
|
|
157
|
+
export const HISTORY_SYNC_PAUSED_TIMEOUT_MS = 120000;
|
|
158
|
+
export const TimeMs = {
|
|
159
|
+
Minute: 60 * 1000,
|
|
160
|
+
Hour: 60 * 60 * 1000,
|
|
161
|
+
Day: 24 * 60 * 60 * 1000,
|
|
162
|
+
Week: 7 * 24 * 60 * 60 * 1000
|
|
163
|
+
};
|
|
164
|
+
export const BOT_RENDERING_CONFIG_METADATA = {
|
|
165
|
+
bloksVersioningId: '0903aa5f7f47de66789d5f4c86d3bd6e05e4bc3ff85e454a9f907d5ed7fef97c',
|
|
166
|
+
pixelDensity: 2.75
|
|
167
|
+
};
|
|
168
|
+
export const WA_ADV_KEY_INDEX_LIST_SIG_PREFIX = Buffer.from([6, 2]);
|
|
169
|
+
export const WA_ADV_HOSTED_KEY_INDEX_LIST_SIG_PREFIX_VERIFY = true;
|
|
170
|
+
export const WA_ADV_HOSTED_KEY_INDEX_LIST_SIG_PREFIX = Buffer.from([6, 7]);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/ciphertext-message.js
|
|
5
|
+
* About: Ciphertext message module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: CiphertextMessage
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
export class CiphertextMessage {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.UNSUPPORTED_VERSION = 1;
|
|
16
|
+
this.CURRENT_VERSION = 3;
|
|
17
|
+
this.WHISPER_TYPE = 2;
|
|
18
|
+
this.PREKEY_TYPE = 3;
|
|
19
|
+
this.SENDERKEY_TYPE = 4;
|
|
20
|
+
this.SENDERKEY_DISTRIBUTION_TYPE = 5;
|
|
21
|
+
this.ENCRYPTED_MESSAGE_OVERHEAD = 53;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/group-session-builder.js
|
|
5
|
+
* About: Group session builder module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: GroupSessionBuilder
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import * as keyhelper from './keyhelper.js';
|
|
14
|
+
import { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
15
|
+
import { SenderKeyName } from './sender-key-name.js';
|
|
16
|
+
import { SenderKeyRecord } from './sender-key-record.js';
|
|
17
|
+
export class GroupSessionBuilder {
|
|
18
|
+
constructor(senderKeyStore) {
|
|
19
|
+
this.senderKeyStore = senderKeyStore;
|
|
20
|
+
}
|
|
21
|
+
async process(senderKeyName, senderKeyDistributionMessage) {
|
|
22
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
|
|
23
|
+
senderKeyRecord.addSenderKeyState(senderKeyDistributionMessage.getId(), senderKeyDistributionMessage.getIteration(), senderKeyDistributionMessage.getChainKey(), senderKeyDistributionMessage.getSignatureKey());
|
|
24
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
|
|
25
|
+
}
|
|
26
|
+
async create(senderKeyName) {
|
|
27
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
|
|
28
|
+
if (senderKeyRecord.isEmpty()) {
|
|
29
|
+
const keyId = keyhelper.generateSenderKeyId();
|
|
30
|
+
const senderKey = keyhelper.generateSenderKey();
|
|
31
|
+
const signingKey = keyhelper.generateSenderSigningKey();
|
|
32
|
+
senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey);
|
|
33
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
|
|
34
|
+
}
|
|
35
|
+
const state = senderKeyRecord.getSenderKeyState();
|
|
36
|
+
if (!state) {
|
|
37
|
+
throw new Error('No session state available');
|
|
38
|
+
}
|
|
39
|
+
return new SenderKeyDistributionMessage(state.getKeyId(), state.getSenderChainKey().getIteration(), state.getSenderChainKey().getSeed(), state.getSigningKeyPublic());
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/group_cipher.js
|
|
5
|
+
* About: Group cipher module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: GroupCipher
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { decrypt, encrypt } from 'libsignal/src/crypto.js';
|
|
14
|
+
import { SenderKeyMessage } from './sender-key-message.js';
|
|
15
|
+
import { SenderKeyName } from './sender-key-name.js';
|
|
16
|
+
import { SenderKeyRecord } from './sender-key-record.js';
|
|
17
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
18
|
+
export class GroupCipher {
|
|
19
|
+
constructor(senderKeyStore, senderKeyName) {
|
|
20
|
+
this.senderKeyStore = senderKeyStore;
|
|
21
|
+
this.senderKeyName = senderKeyName;
|
|
22
|
+
}
|
|
23
|
+
async encrypt(paddedPlaintext) {
|
|
24
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
25
|
+
if (!record) {
|
|
26
|
+
throw new Error('No SenderKeyRecord found for encryption');
|
|
27
|
+
}
|
|
28
|
+
const senderKeyState = record.getSenderKeyState();
|
|
29
|
+
if (!senderKeyState) {
|
|
30
|
+
throw new Error('No session to encrypt message');
|
|
31
|
+
}
|
|
32
|
+
const iteration = senderKeyState.getSenderChainKey().getIteration();
|
|
33
|
+
const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
|
|
34
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
|
|
35
|
+
const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
|
|
36
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
37
|
+
return senderKeyMessage.serialize();
|
|
38
|
+
}
|
|
39
|
+
async decrypt(senderKeyMessageBytes) {
|
|
40
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
41
|
+
if (!record) {
|
|
42
|
+
throw new Error('No SenderKeyRecord found for decryption');
|
|
43
|
+
}
|
|
44
|
+
const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
|
|
45
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
|
|
46
|
+
if (!senderKeyState) {
|
|
47
|
+
throw new Error('No session found to decrypt message');
|
|
48
|
+
}
|
|
49
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
|
|
50
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
|
|
51
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
|
52
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
53
|
+
return plaintext;
|
|
54
|
+
}
|
|
55
|
+
getSenderKey(senderKeyState, iteration) {
|
|
56
|
+
let senderChainKey = senderKeyState.getSenderChainKey();
|
|
57
|
+
if (senderChainKey.getIteration() > iteration) {
|
|
58
|
+
if (senderKeyState.hasSenderMessageKey(iteration)) {
|
|
59
|
+
const messageKey = senderKeyState.removeSenderMessageKey(iteration);
|
|
60
|
+
if (!messageKey) {
|
|
61
|
+
throw new Error('No sender message key found for iteration');
|
|
62
|
+
}
|
|
63
|
+
return messageKey;
|
|
64
|
+
}
|
|
65
|
+
throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
|
|
66
|
+
}
|
|
67
|
+
if (iteration - senderChainKey.getIteration() > 2000) {
|
|
68
|
+
throw new Error('Over 2000 messages into the future!');
|
|
69
|
+
}
|
|
70
|
+
while (senderChainKey.getIteration() < iteration) {
|
|
71
|
+
senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
|
|
72
|
+
senderChainKey = senderChainKey.getNext();
|
|
73
|
+
}
|
|
74
|
+
senderKeyState.setSenderChainKey(senderChainKey.getNext());
|
|
75
|
+
return senderChainKey.getSenderMessageKey();
|
|
76
|
+
}
|
|
77
|
+
async getPlainText(iv, key, ciphertext) {
|
|
78
|
+
try {
|
|
79
|
+
return decrypt(key, ciphertext, iv);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
throw new Error('InvalidMessageException');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async getCipherText(iv, key, plaintext) {
|
|
86
|
+
try {
|
|
87
|
+
return encrypt(key, plaintext, iv);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
throw new Error('InvalidMessageException');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/index.js
|
|
5
|
+
* About: Group module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: GroupSessionBuilder, SenderKeyDistributionMessage, SenderKeyRecord, SenderKeyName, GroupCipher, SenderKeyState, SenderKeyMessage, SenderMessageKey; …
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
export { GroupSessionBuilder } from './group-session-builder.js';
|
|
14
|
+
export { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
15
|
+
export { SenderKeyRecord } from './sender-key-record.js';
|
|
16
|
+
export { SenderKeyName } from './sender-key-name.js';
|
|
17
|
+
export { GroupCipher } from './group_cipher.js';
|
|
18
|
+
export { SenderKeyState } from './sender-key-state.js';
|
|
19
|
+
export { SenderKeyMessage } from './sender-key-message.js';
|
|
20
|
+
export { SenderMessageKey } from './sender-message-key.js';
|
|
21
|
+
export { SenderChainKey } from './sender-chain-key.js';
|
|
22
|
+
export { CiphertextMessage } from './ciphertext-message.js';
|
|
23
|
+
export * as keyhelper from './keyhelper.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/keyhelper.js
|
|
5
|
+
* About: Keyhelper module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: generateSenderKey, generateSenderKeyId, generateSenderSigningKey
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import * as nodeCrypto from 'crypto';
|
|
14
|
+
import { generateKeyPair } from 'libsignal/src/curve.js';
|
|
15
|
+
export function generateSenderKey() {
|
|
16
|
+
return nodeCrypto.randomBytes(32);
|
|
17
|
+
}
|
|
18
|
+
export function generateSenderKeyId() {
|
|
19
|
+
return nodeCrypto.randomInt(2147483647);
|
|
20
|
+
}
|
|
21
|
+
export function generateSenderSigningKey(key) {
|
|
22
|
+
if (!key) {
|
|
23
|
+
key = generateKeyPair();
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
public: Buffer.from(key.pubKey),
|
|
27
|
+
private: Buffer.from(key.privKey)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/sender-chain-key.js
|
|
5
|
+
* About: Sender chain key module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: SenderChainKey
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { calculateMAC } from 'libsignal/src/crypto.js';
|
|
14
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
15
|
+
export class SenderChainKey {
|
|
16
|
+
constructor(iteration, chainKey) {
|
|
17
|
+
this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
|
|
18
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02]);
|
|
19
|
+
this.iteration = iteration;
|
|
20
|
+
this.chainKey = Buffer.from(chainKey);
|
|
21
|
+
}
|
|
22
|
+
getIteration() {
|
|
23
|
+
return this.iteration;
|
|
24
|
+
}
|
|
25
|
+
getSenderMessageKey() {
|
|
26
|
+
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
|
|
27
|
+
}
|
|
28
|
+
getNext() {
|
|
29
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
|
|
30
|
+
}
|
|
31
|
+
getSeed() {
|
|
32
|
+
return this.chainKey;
|
|
33
|
+
}
|
|
34
|
+
getDerivative(seed, key) {
|
|
35
|
+
return calculateMAC(key, seed);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/sender-key-distribution-message.js
|
|
5
|
+
* About: Sender key distribution message module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: SenderKeyDistributionMessage
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { proto } from '../../../WAProto/index.js';
|
|
14
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
15
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
16
|
+
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
17
|
+
super();
|
|
18
|
+
if (serialized) {
|
|
19
|
+
try {
|
|
20
|
+
const message = serialized.slice(1);
|
|
21
|
+
const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
|
|
22
|
+
this.serialized = serialized;
|
|
23
|
+
this.id = distributionMessage.id;
|
|
24
|
+
this.iteration = distributionMessage.iteration;
|
|
25
|
+
this.chainKey =
|
|
26
|
+
typeof distributionMessage.chainKey === 'string'
|
|
27
|
+
? Buffer.from(distributionMessage.chainKey, 'base64')
|
|
28
|
+
: distributionMessage.chainKey;
|
|
29
|
+
this.signatureKey =
|
|
30
|
+
typeof distributionMessage.signingKey === 'string'
|
|
31
|
+
? Buffer.from(distributionMessage.signingKey, 'base64')
|
|
32
|
+
: distributionMessage.signingKey;
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
throw new Error(String(e));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
|
|
40
|
+
this.id = id;
|
|
41
|
+
this.iteration = iteration;
|
|
42
|
+
this.chainKey = chainKey;
|
|
43
|
+
this.signatureKey = signatureKey;
|
|
44
|
+
const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
|
|
45
|
+
id,
|
|
46
|
+
iteration,
|
|
47
|
+
chainKey,
|
|
48
|
+
signingKey: this.signatureKey
|
|
49
|
+
})).finish();
|
|
50
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
intsToByteHighAndLow(highValue, lowValue) {
|
|
54
|
+
return (((highValue << 4) | lowValue) & 0xff) % 256;
|
|
55
|
+
}
|
|
56
|
+
serialize() {
|
|
57
|
+
return this.serialized;
|
|
58
|
+
}
|
|
59
|
+
getType() {
|
|
60
|
+
return this.SENDERKEY_DISTRIBUTION_TYPE;
|
|
61
|
+
}
|
|
62
|
+
getIteration() {
|
|
63
|
+
return this.iteration;
|
|
64
|
+
}
|
|
65
|
+
getChainKey() {
|
|
66
|
+
return this.chainKey;
|
|
67
|
+
}
|
|
68
|
+
getSignatureKey() {
|
|
69
|
+
return this.signatureKey;
|
|
70
|
+
}
|
|
71
|
+
getId() {
|
|
72
|
+
return this.id;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @X4R1S3
|
|
3
|
+
*
|
|
4
|
+
* File: lib/Signal/Group/sender-key-message.js
|
|
5
|
+
* About: Sender key message module for the X4R1S3 WhatsApp protocol client.
|
|
6
|
+
* Important exports: SenderKeyMessage
|
|
7
|
+
* Directions: Import the listed exports and preserve protocol, validation, and compatibility invariants when modifying.
|
|
8
|
+
*
|
|
9
|
+
* Copyright and attribution notice.
|
|
10
|
+
* Do not copy, reproduce, redistribute, modify, or create derivative works
|
|
11
|
+
* from this source without explicit written permission.
|
|
12
|
+
*/
|
|
13
|
+
import { calculateSignature, verifySignature } from 'libsignal/src/curve.js';
|
|
14
|
+
import { proto } from '../../../WAProto/index.js';
|
|
15
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
16
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
17
|
+
constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
|
|
18
|
+
super();
|
|
19
|
+
this.SIGNATURE_LENGTH = 64;
|
|
20
|
+
if (serialized) {
|
|
21
|
+
const version = serialized[0];
|
|
22
|
+
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
|
|
23
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
24
|
+
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
|
|
25
|
+
this.serialized = serialized;
|
|
26
|
+
this.messageVersion = (version & 0xff) >> 4;
|
|
27
|
+
this.keyId = senderKeyMessage.id;
|
|
28
|
+
this.iteration = senderKeyMessage.iteration;
|
|
29
|
+
this.ciphertext =
|
|
30
|
+
typeof senderKeyMessage.ciphertext === 'string'
|
|
31
|
+
? Buffer.from(senderKeyMessage.ciphertext, 'base64')
|
|
32
|
+
: senderKeyMessage.ciphertext;
|
|
33
|
+
this.signature = signature;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
|
|
37
|
+
const ciphertextBuffer = Buffer.from(ciphertext);
|
|
38
|
+
const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
|
|
39
|
+
id: keyId,
|
|
40
|
+
iteration: iteration,
|
|
41
|
+
ciphertext: ciphertextBuffer
|
|
42
|
+
})).finish();
|
|
43
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
|
|
44
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
|
|
45
|
+
this.messageVersion = this.CURRENT_VERSION;
|
|
46
|
+
this.keyId = keyId;
|
|
47
|
+
this.iteration = iteration;
|
|
48
|
+
this.ciphertext = ciphertextBuffer;
|
|
49
|
+
this.signature = signature;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
getKeyId() {
|
|
53
|
+
return this.keyId;
|
|
54
|
+
}
|
|
55
|
+
getIteration() {
|
|
56
|
+
return this.iteration;
|
|
57
|
+
}
|
|
58
|
+
getCipherText() {
|
|
59
|
+
return this.ciphertext;
|
|
60
|
+
}
|
|
61
|
+
verifySignature(signatureKey) {
|
|
62
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
|
|
63
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
64
|
+
const res = verifySignature(signatureKey, part1, part2);
|
|
65
|
+
if (!res)
|
|
66
|
+
throw new Error('Invalid signature!');
|
|
67
|
+
}
|
|
68
|
+
getSignature(signatureKey, serialized) {
|
|
69
|
+
return Buffer.from(calculateSignature(signatureKey, serialized));
|
|
70
|
+
}
|
|
71
|
+
serialize() {
|
|
72
|
+
return this.serialized;
|
|
73
|
+
}
|
|
74
|
+
getType() {
|
|
75
|
+
return 4;
|
|
76
|
+
}
|
|
77
|
+
}
|