@sgintokic/baileys 0.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.

Potentially problematic release.


This version of @sgintokic/baileys might be problematic. Click here for more details.

Files changed (106) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +1097 -0
  3. package/WAProto/index.js +2 -0
  4. package/engine-requirements.js +1 -0
  5. package/lib/Defaults/index.js +155 -0
  6. package/lib/Signal/Group/ciphertext-message.js +11 -0
  7. package/lib/Signal/Group/group-session-builder.js +41 -0
  8. package/lib/Signal/Group/group_cipher.js +108 -0
  9. package/lib/Signal/Group/index.js +11 -0
  10. package/lib/Signal/Group/keyhelper.js +14 -0
  11. package/lib/Signal/Group/sender-chain-key.js +31 -0
  12. package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
  13. package/lib/Signal/Group/sender-key-message.js +79 -0
  14. package/lib/Signal/Group/sender-key-name.js +49 -0
  15. package/lib/Signal/Group/sender-key-record.js +46 -0
  16. package/lib/Signal/Group/sender-key-state.js +104 -0
  17. package/lib/Signal/Group/sender-message-key.js +29 -0
  18. package/lib/Signal/libsignal.js +485 -0
  19. package/lib/Signal/lid-mapping.js +291 -0
  20. package/lib/Socket/Client/index.js +2 -0
  21. package/lib/Socket/Client/types.js +10 -0
  22. package/lib/Socket/Client/websocket.js +64 -0
  23. package/lib/Socket/business.js +293 -0
  24. package/lib/Socket/chats.js +1068 -0
  25. package/lib/Socket/communities.js +476 -0
  26. package/lib/Socket/groups.js +383 -0
  27. package/lib/Socket/index.js +8 -0
  28. package/lib/Socket/messages-recv.js +1830 -0
  29. package/lib/Socket/messages-send.js +1462 -0
  30. package/lib/Socket/mex.js +55 -0
  31. package/lib/Socket/newsletter.js +277 -0
  32. package/lib/Socket/socket.js +1087 -0
  33. package/lib/Store/index.js +3 -0
  34. package/lib/Store/make-in-memory-store.js +517 -0
  35. package/lib/Store/make-ordered-dictionary.js +75 -0
  36. package/lib/Store/object-repository.js +23 -0
  37. package/lib/Types/Auth.js +1 -0
  38. package/lib/Types/Bussines.js +1 -0
  39. package/lib/Types/Call.js +1 -0
  40. package/lib/Types/Chat.js +7 -0
  41. package/lib/Types/Contact.js +1 -0
  42. package/lib/Types/Events.js +1 -0
  43. package/lib/Types/GroupMetadata.js +1 -0
  44. package/lib/Types/Label.js +24 -0
  45. package/lib/Types/LabelAssociation.js +6 -0
  46. package/lib/Types/Message.js +18 -0
  47. package/lib/Types/Newsletter.js +33 -0
  48. package/lib/Types/Product.js +1 -0
  49. package/lib/Types/Signal.js +1 -0
  50. package/lib/Types/Socket.js +2 -0
  51. package/lib/Types/State.js +15 -0
  52. package/lib/Types/USync.js +1 -0
  53. package/lib/Types/index.js +31 -0
  54. package/lib/Utils/auth-utils.js +293 -0
  55. package/lib/Utils/browser-utils.js +32 -0
  56. package/lib/Utils/business.js +245 -0
  57. package/lib/Utils/chat-utils.js +959 -0
  58. package/lib/Utils/crypto.js +133 -0
  59. package/lib/Utils/decode-wa-message.js +376 -0
  60. package/lib/Utils/event-buffer.js +620 -0
  61. package/lib/Utils/generics.js +417 -0
  62. package/lib/Utils/history.js +150 -0
  63. package/lib/Utils/identity-change-handler.js +63 -0
  64. package/lib/Utils/index.js +21 -0
  65. package/lib/Utils/link-preview.js +91 -0
  66. package/lib/Utils/logger.js +2 -0
  67. package/lib/Utils/lt-hash.js +6 -0
  68. package/lib/Utils/make-mutex.js +31 -0
  69. package/lib/Utils/message-retry-manager.js +240 -0
  70. package/lib/Utils/messages-media.js +901 -0
  71. package/lib/Utils/messages.js +2052 -0
  72. package/lib/Utils/noise-handler.js +229 -0
  73. package/lib/Utils/offline-node-processor.js +50 -0
  74. package/lib/Utils/pre-key-manager.js +119 -0
  75. package/lib/Utils/process-message.js +641 -0
  76. package/lib/Utils/reporting-utils.js +346 -0
  77. package/lib/Utils/signal.js +188 -0
  78. package/lib/Utils/stanza-ack.js +33 -0
  79. package/lib/Utils/sync-action-utils.js +53 -0
  80. package/lib/Utils/tc-token-utils.js +15 -0
  81. package/lib/Utils/use-multi-file-auth-state.js +116 -0
  82. package/lib/Utils/use-single-file-auth-state.js +94 -0
  83. package/lib/Utils/validate-connection.js +235 -0
  84. package/lib/WABinary/constants.js +1300 -0
  85. package/lib/WABinary/decode.js +258 -0
  86. package/lib/WABinary/encode.js +219 -0
  87. package/lib/WABinary/generic-utils.js +203 -0
  88. package/lib/WABinary/index.js +5 -0
  89. package/lib/WABinary/jid-utils.js +93 -0
  90. package/lib/WABinary/types.js +1 -0
  91. package/lib/WAM/BinaryInfo.js +9 -0
  92. package/lib/WAM/constants.js +20669 -0
  93. package/lib/WAM/encode.js +151 -0
  94. package/lib/WAM/index.js +3 -0
  95. package/lib/WAUSync/Protocols/USyncContactProtocol.js +21 -0
  96. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
  97. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
  98. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
  99. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
  100. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
  101. package/lib/WAUSync/Protocols/index.js +4 -0
  102. package/lib/WAUSync/USyncQuery.js +103 -0
  103. package/lib/WAUSync/USyncUser.js +22 -0
  104. package/lib/WAUSync/index.js +3 -0
  105. package/lib/index.js +11 -0
  106. package/package.json +58 -0
@@ -0,0 +1 @@
1
+ const major=parseInt(process.versions.node.split(".")[0],10);if(major<20){console.error(`\n❌ This package requires Node.js 20+ to run reliably.\n`+` You are using Node.js ${process.versions.node}.\n`+` Please upgrade to Node.js 20+ to proceed.\n`);process.exit(1)}
@@ -0,0 +1,155 @@
1
+ import { createHash, randomBytes } from 'crypto';
2
+ import { proto } from '../../WAProto/index.js';
3
+ import { makeLibSignalRepository } from '../Signal/libsignal.js';
4
+ import { Browsers } from '../Utils/browser-utils.js';
5
+ import logger from '../Utils/logger.js';
6
+ const version = [2, 3e3, 1035194821];
7
+ export const UNAUTHORIZED_CODES = [401, 403, 419];
8
+ export const BIZ_BOT_SUPPORT_PAYLOAD =
9
+ '{"version":1,"is_ai_message":true,"should_upload_client_logs":false,"should_show_system_message":false,"ticket_id":"7004947587700716","citation_items":[],"ticket_locale":"us"}';
10
+ export const DEFAULT_ORIGIN = 'https://web.whatsapp.com';
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 DEF_CALLBACK_PREFIX = 'CB:';
14
+ export const DEF_TAG_PREFIX = 'TAG:';
15
+
16
+ export const PHONE_CONNECTION_CB = 'CB:Pong';
17
+ export const WA_ADV_ACCOUNT_SIG_PREFIX = Buffer.from([6, 0]);
18
+ export const WA_ADV_DEVICE_SIG_PREFIX = Buffer.from([6, 1]);
19
+ export const WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX = Buffer.from([6, 5]);
20
+ export const WA_ADV_HOSTED_DEVICE_SIG_PREFIX = Buffer.from([6, 6]);
21
+ export const WA_DEFAULT_EPHEMERAL = 7 * 24 * 60 * 60;
22
+ /** Status messages older than 24 hours are considered expired */ export const STATUS_EXPIRY_SECONDS = 24 * 60 * 60;
23
+ /** WA Web enforces a 14-day maximum age for placeholder resend requests */ export const PLACEHOLDER_MAX_AGE_SECONDS = 14 * 24 * 60 * 60;
24
+ export const NOISE_MODE = 'Noise_XX_25519_AESGCM_SHA256\0\0\0\0';
25
+ export const DICT_VERSION = 3;
26
+ export const KEY_BUNDLE_TYPE = Buffer.from([5]);
27
+ export const NOISE_WA_HEADER = Buffer.from([87, 65, 6, DICT_VERSION]); // last is "DICT_VERSION"
28
+ /** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */ export const URL_REGEX =
29
+ /https:\/\/(?![^:@\/\s]+:[^:@\/\s]+@)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(:\d+)?(\/[^\s]*)?/g;
30
+ export const WA_CERT_DETAILS = {
31
+ SERIAL: 0,
32
+ ISSUER: 'WhatsAppLongTerm1',
33
+ PUBLIC_KEY: Buffer.from('142375574d0a587166aae71ebe516437c4a28b73e3695c6ce1f7f9545da8ee6b', 'hex')
34
+ };
35
+ export const PROCESSABLE_HISTORY_TYPES = [
36
+ proto.HistorySync.HistorySyncType.INITIAL_BOOTSTRAP,
37
+ proto.HistorySync.HistorySyncType.PUSH_NAME,
38
+ proto.HistorySync.HistorySyncType.RECENT,
39
+ proto.HistorySync.HistorySyncType.FULL,
40
+ proto.HistorySync.HistorySyncType.ON_DEMAND,
41
+ proto.HistorySync.HistorySyncType.NON_BLOCKING_DATA,
42
+ proto.HistorySync.HistorySyncType.INITIAL_STATUS_V3
43
+ ];
44
+ export const DEFAULT_CONNECTION_CONFIG = {
45
+ version: version,
46
+ browser: Browsers.macOS('Chrome'),
47
+ waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat',
48
+ connectTimeoutMs: 2e4,
49
+ keepAliveIntervalMs: 3e4,
50
+ logger: logger.child({ class: 'baileys' }),
51
+ emitOwnEvents: true,
52
+ defaultQueryTimeoutMs: 6e4,
53
+ customUploadHosts: [],
54
+ retryRequestDelayMs: 250,
55
+ maxMsgRetryCount: 3,
56
+ fireInitQueries: true,
57
+ auth: undefined,
58
+ markOnlineOnConnect: true,
59
+ syncFullHistory: true,
60
+ patchMessageBeforeSending: msg => msg,
61
+ shouldSyncHistoryMessage: ({ syncType }) => {
62
+ return syncType !== proto.HistorySync.HistorySyncType.FULL;
63
+ },
64
+ shouldIgnoreJid: () => false,
65
+ linkPreviewImageThumbnailWidth: 192,
66
+ transactionOpts: { maxCommitRetries: 10, delayBetweenTriesMs: 3e3 },
67
+ generateHighQualityLinkPreview: false,
68
+ enableAutoSessionRecreation: true,
69
+ enableRecentMessageCache: true,
70
+ options: {},
71
+ generateMessageID: () => 'GIN4B0T' + randomBytes(11).toString('hex').toUpperCase(),
72
+ generateMessageIDV2: userId => {
73
+ if (!userId) {
74
+ userId = randomBytes(32).toString('hex');
75
+ }
76
+ const hash = createHash('sha256').update(userId).digest('hex').toUpperCase();
77
+ const randomPart = randomBytes(11).toString('hex').toUpperCase();
78
+ const combined = hash + randomPart;
79
+ let result = '';
80
+ for (let i = 0; i < 25; i++) {
81
+ const randomIndex = randomBytes(1)[0] % combined.length;
82
+ result += combined[randomIndex];
83
+ }
84
+ return 'G1N4B0T' + result;
85
+ },
86
+
87
+ appStateMacVerification: { patch: false, snapshot: false },
88
+ countryCode: 'US',
89
+ getMessage: async () => undefined,
90
+ cachedGroupMetadata: async () => undefined,
91
+ makeSignalRepository: makeLibSignalRepository
92
+ };
93
+ export const MEDIA_PATH_MAP = {
94
+ image: '/mms/image',
95
+ video: '/mms/video',
96
+ document: '/mms/document',
97
+ audio: '/mms/audio',
98
+ sticker: '/mms/image',
99
+ 'sticker-pack': '/mms/sticker-pack',
100
+ 'thumbnail-sticker-pack': '/mms/thumbnail-sticker-pack',
101
+ 'thumbnail-link': '/mms/thumbnail-link',
102
+ 'product-catalog-image': '/product/image',
103
+ 'md-app-state': '',
104
+ 'md-msg-hist': '/mms/md-app-state',
105
+ 'biz-cover-photo': '/pps/biz-cover-photo'
106
+ };
107
+ // Lia@Changes 06-02-26 --- Add newsletter media path for "/m1/" instead of "/o1/" (⁠≧⁠▽⁠≦⁠)
108
+ export const NEWSLETTER_MEDIA_PATH_MAP = {
109
+ image: '/newsletter/newsletter-image',
110
+ video: '/newsletter/newsletter-video',
111
+ document: '/newsletter/newsletter-document',
112
+ audio: '/newsletter/newsletter-audio',
113
+ sticker: '/newsletter/newsletter-image',
114
+ 'thumbnail-link': '/newsletter/newsletter-thumbnail-link'
115
+ };
116
+ export const MEDIA_HKDF_KEY_MAPPING = {
117
+ audio: 'Audio',
118
+ document: 'Document',
119
+ gif: 'Video',
120
+ image: 'Image',
121
+ ppic: '',
122
+ product: 'Image',
123
+ ptt: 'Audio',
124
+ 'sticker-pack': 'Sticker Pack',
125
+ 'thumbnail-sticker-pack': 'Sticker Pack Thumbnail',
126
+ sticker: 'Image',
127
+ video: 'Video',
128
+ 'thumbnail-document': 'Document Thumbnail',
129
+ 'thumbnail-image': 'Image Thumbnail',
130
+ 'thumbnail-video': 'Video Thumbnail',
131
+ 'thumbnail-link': 'Link Thumbnail',
132
+ 'md-msg-hist': 'History',
133
+ 'md-app-state': 'App State',
134
+ 'product-catalog-image': '',
135
+ 'payment-bg-image': 'Payment Background',
136
+ ptv: 'Video',
137
+ 'biz-cover-photo': 'Image'
138
+ };
139
+ export const MEDIA_KEYS = Object.keys(MEDIA_PATH_MAP);
140
+ export const MIN_PREKEY_COUNT = 5;
141
+ export const INITIAL_PREKEY_COUNT = 812;
142
+ export const UPLOAD_TIMEOUT = 3e4; // 30 seconds
143
+ export const MIN_UPLOAD_INTERVAL = 5e3; // 5 seconds minimum between uploads
144
+ export const DEFAULT_CACHE_TTLS = {
145
+ SIGNAL_STORE: 5 * 60, // 5 minutes
146
+ MSG_RETRY: 60 * 60, // 1 hour
147
+ CALL_OFFER: 5 * 60, // 5 minutes
148
+ USER_DEVICES: 5 * 60
149
+ };
150
+ export const TimeMs = {
151
+ Minute: 60 * 1e3,
152
+ Hour: 60 * 60 * 1e3,
153
+ Day: 24 * 60 * 60 * 1e3,
154
+ Week: 7 * 24 * 60 * 60 * 1e3
155
+ };
@@ -0,0 +1,11 @@
1
+ export class CiphertextMessage {
2
+ constructor() {
3
+ this.UNSUPPORTED_VERSION = 1;
4
+ this.CURRENT_VERSION = 3;
5
+ this.WHISPER_TYPE = 2;
6
+ this.PREKEY_TYPE = 3;
7
+ this.SENDERKEY_TYPE = 4;
8
+ this.SENDERKEY_DISTRIBUTION_TYPE = 5;
9
+ this.ENCRYPTED_MESSAGE_OVERHEAD = 53;
10
+ }
11
+ }
@@ -0,0 +1,41 @@
1
+ import * as keyhelper from "./keyhelper.js";
2
+ import { SenderKeyDistributionMessage } from "./sender-key-distribution-message.js";
3
+ import { SenderKeyName } from "./sender-key-name.js";
4
+ import { SenderKeyRecord } from "./sender-key-record.js";
5
+ export class GroupSessionBuilder {
6
+ constructor(senderKeyStore) {
7
+ this.senderKeyStore = senderKeyStore;
8
+ }
9
+ async process(senderKeyName, senderKeyDistributionMessage) {
10
+ const senderKeyRecord =
11
+ await this.senderKeyStore.loadSenderKey(senderKeyName);
12
+ senderKeyRecord.addSenderKeyState(
13
+ senderKeyDistributionMessage.getId(),
14
+ senderKeyDistributionMessage.getIteration(),
15
+ senderKeyDistributionMessage.getChainKey(),
16
+ senderKeyDistributionMessage.getSignatureKey(),
17
+ );
18
+ await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
19
+ }
20
+ async create(senderKeyName) {
21
+ const senderKeyRecord =
22
+ await this.senderKeyStore.loadSenderKey(senderKeyName);
23
+ if (senderKeyRecord.isEmpty()) {
24
+ const keyId = keyhelper.generateSenderKeyId();
25
+ const senderKey = keyhelper.generateSenderKey();
26
+ const signingKey = keyhelper.generateSenderSigningKey();
27
+ senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey);
28
+ await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
29
+ }
30
+ const state = senderKeyRecord.getSenderKeyState();
31
+ if (!state) {
32
+ throw new Error("No session state available");
33
+ }
34
+ return new SenderKeyDistributionMessage(
35
+ state.getKeyId(),
36
+ state.getSenderChainKey().getIteration(),
37
+ state.getSenderChainKey().getSeed(),
38
+ state.getSigningKeyPublic(),
39
+ );
40
+ }
41
+ }
@@ -0,0 +1,108 @@
1
+ import { decrypt, encrypt } from "libsignal/src/crypto.js";
2
+ import { SenderKeyMessage } from "./sender-key-message.js";
3
+ import { SenderKeyName } from "./sender-key-name.js";
4
+ import { SenderKeyRecord } from "./sender-key-record.js";
5
+ import { SenderKeyState } from "./sender-key-state.js";
6
+ export class GroupCipher {
7
+ constructor(senderKeyStore, senderKeyName) {
8
+ this.senderKeyStore = senderKeyStore;
9
+ this.senderKeyName = senderKeyName;
10
+ }
11
+ async encrypt(paddedPlaintext) {
12
+ const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
13
+ if (!record) {
14
+ throw new Error("No SenderKeyRecord found for encryption");
15
+ }
16
+ const senderKeyState = record.getSenderKeyState();
17
+ if (!senderKeyState) {
18
+ throw new Error("No session to encrypt message");
19
+ }
20
+ const iteration = senderKeyState.getSenderChainKey().getIteration();
21
+ const senderKey = this.getSenderKey(
22
+ senderKeyState,
23
+ iteration === 0 ? 0 : iteration + 1,
24
+ );
25
+ const ciphertext = await this.getCipherText(
26
+ senderKey.getIv(),
27
+ senderKey.getCipherKey(),
28
+ paddedPlaintext,
29
+ );
30
+ const senderKeyMessage = new SenderKeyMessage(
31
+ senderKeyState.getKeyId(),
32
+ senderKey.getIteration(),
33
+ ciphertext,
34
+ senderKeyState.getSigningKeyPrivate(),
35
+ );
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(
45
+ null,
46
+ null,
47
+ null,
48
+ null,
49
+ senderKeyMessageBytes,
50
+ );
51
+ const senderKeyState = record.getSenderKeyState(
52
+ senderKeyMessage.getKeyId(),
53
+ );
54
+ if (!senderKeyState) {
55
+ throw new Error("No session found to decrypt message");
56
+ }
57
+ senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
58
+ const senderKey = this.getSenderKey(
59
+ senderKeyState,
60
+ senderKeyMessage.getIteration(),
61
+ );
62
+ const plaintext = await this.getPlainText(
63
+ senderKey.getIv(),
64
+ senderKey.getCipherKey(),
65
+ senderKeyMessage.getCipherText(),
66
+ );
67
+ await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
68
+ return plaintext;
69
+ }
70
+ getSenderKey(senderKeyState, iteration) {
71
+ let senderChainKey = senderKeyState.getSenderChainKey();
72
+ if (senderChainKey.getIteration() > iteration) {
73
+ if (senderKeyState.hasSenderMessageKey(iteration)) {
74
+ const messageKey = senderKeyState.removeSenderMessageKey(iteration);
75
+ if (!messageKey) {
76
+ throw new Error("No sender message key found for iteration");
77
+ }
78
+ return messageKey;
79
+ }
80
+ throw new Error(
81
+ `Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`,
82
+ );
83
+ }
84
+ if (iteration - senderChainKey.getIteration() > 2e3) {
85
+ throw new Error("Over 2000 messages into the future!");
86
+ }
87
+ while (senderChainKey.getIteration() < iteration) {
88
+ senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
89
+ senderChainKey = senderChainKey.getNext();
90
+ }
91
+ senderKeyState.setSenderChainKey(senderChainKey.getNext());
92
+ return senderChainKey.getSenderMessageKey();
93
+ }
94
+ async getPlainText(iv, key, ciphertext) {
95
+ try {
96
+ return decrypt(key, ciphertext, iv);
97
+ } catch (e) {
98
+ throw new Error("InvalidMessageException");
99
+ }
100
+ }
101
+ async getCipherText(iv, key, plaintext) {
102
+ try {
103
+ return encrypt(key, plaintext, iv);
104
+ } catch (e) {
105
+ throw new Error("InvalidMessageException");
106
+ }
107
+ }
108
+ }
@@ -0,0 +1,11 @@
1
+ export { GroupSessionBuilder } from "./group-session-builder.js";
2
+ export { SenderKeyDistributionMessage } from "./sender-key-distribution-message.js";
3
+ export { SenderKeyRecord } from "./sender-key-record.js";
4
+ export { SenderKeyName } from "./sender-key-name.js";
5
+ export { GroupCipher } from "./group_cipher.js";
6
+ export { SenderKeyState } from "./sender-key-state.js";
7
+ export { SenderKeyMessage } from "./sender-key-message.js";
8
+ export { SenderMessageKey } from "./sender-message-key.js";
9
+ export { SenderChainKey } from "./sender-chain-key.js";
10
+ export { CiphertextMessage } from "./ciphertext-message.js";
11
+ export * as keyhelper from "./keyhelper.js";
@@ -0,0 +1,14 @@
1
+ import * as nodeCrypto from "crypto";
2
+ import { generateKeyPair } from "libsignal/src/curve.js";
3
+ export function generateSenderKey() {
4
+ return nodeCrypto.randomBytes(32);
5
+ }
6
+ export function generateSenderKeyId() {
7
+ return nodeCrypto.randomInt(2147483647);
8
+ }
9
+ export function generateSenderSigningKey(key) {
10
+ if (!key) {
11
+ key = generateKeyPair();
12
+ }
13
+ return { public: Buffer.from(key.pubKey), private: Buffer.from(key.privKey) };
14
+ }
@@ -0,0 +1,31 @@
1
+ import { calculateMAC } from "libsignal/src/crypto.js";
2
+ import { SenderMessageKey } from "./sender-message-key.js";
3
+ export class SenderChainKey {
4
+ constructor(iteration, chainKey) {
5
+ this.MESSAGE_KEY_SEED = Buffer.from([1]);
6
+ this.CHAIN_KEY_SEED = Buffer.from([2]);
7
+ this.iteration = iteration;
8
+ this.chainKey = Buffer.from(chainKey);
9
+ }
10
+ getIteration() {
11
+ return this.iteration;
12
+ }
13
+ getSenderMessageKey() {
14
+ return new SenderMessageKey(
15
+ this.iteration,
16
+ this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey),
17
+ );
18
+ }
19
+ getNext() {
20
+ return new SenderChainKey(
21
+ this.iteration + 1,
22
+ this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey),
23
+ );
24
+ }
25
+ getSeed() {
26
+ return this.chainKey;
27
+ }
28
+ getDerivative(seed, key) {
29
+ return calculateMAC(key, seed);
30
+ }
31
+ }
@@ -0,0 +1,66 @@
1
+ import { proto } from "../../../WAProto/index.js";
2
+ import { CiphertextMessage } from "./ciphertext-message.js";
3
+ export class SenderKeyDistributionMessage extends CiphertextMessage {
4
+ constructor(id, iteration, chainKey, signatureKey, serialized) {
5
+ super();
6
+ if (serialized) {
7
+ try {
8
+ const message = serialized.slice(1);
9
+ const distributionMessage =
10
+ proto.SenderKeyDistributionMessage.decode(message).toJSON();
11
+ this.serialized = serialized;
12
+ this.id = distributionMessage.id;
13
+ this.iteration = distributionMessage.iteration;
14
+ this.chainKey =
15
+ typeof distributionMessage.chainKey === "string"
16
+ ? Buffer.from(distributionMessage.chainKey, "base64")
17
+ : distributionMessage.chainKey;
18
+ this.signatureKey =
19
+ typeof distributionMessage.signingKey === "string"
20
+ ? Buffer.from(distributionMessage.signingKey, "base64")
21
+ : distributionMessage.signingKey;
22
+ } catch (e) {
23
+ throw new Error(String(e));
24
+ }
25
+ } else {
26
+ const version = this.intsToByteHighAndLow(
27
+ this.CURRENT_VERSION,
28
+ this.CURRENT_VERSION,
29
+ );
30
+ this.id = id;
31
+ this.iteration = iteration;
32
+ this.chainKey = chainKey;
33
+ this.signatureKey = signatureKey;
34
+ const message = proto.SenderKeyDistributionMessage.encode(
35
+ proto.SenderKeyDistributionMessage.create({
36
+ id: id,
37
+ iteration: iteration,
38
+ chainKey: chainKey,
39
+ signingKey: this.signatureKey,
40
+ }),
41
+ ).finish();
42
+ this.serialized = Buffer.concat([Buffer.from([version]), message]);
43
+ }
44
+ }
45
+ intsToByteHighAndLow(highValue, lowValue) {
46
+ return (((highValue << 4) | lowValue) & 255) % 256;
47
+ }
48
+ serialize() {
49
+ return this.serialized;
50
+ }
51
+ getType() {
52
+ return this.SENDERKEY_DISTRIBUTION_TYPE;
53
+ }
54
+ getIteration() {
55
+ return this.iteration;
56
+ }
57
+ getChainKey() {
58
+ return this.chainKey;
59
+ }
60
+ getSignatureKey() {
61
+ return this.signatureKey;
62
+ }
63
+ getId() {
64
+ return this.id;
65
+ }
66
+ }
@@ -0,0 +1,79 @@
1
+ import { calculateSignature, verifySignature } from "libsignal/src/curve.js";
2
+ import { proto } from "../../../WAProto/index.js";
3
+ import { CiphertextMessage } from "./ciphertext-message.js";
4
+ export class SenderKeyMessage extends CiphertextMessage {
5
+ constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
6
+ super();
7
+ this.SIGNATURE_LENGTH = 64;
8
+ if (serialized) {
9
+ const version = serialized[0];
10
+ const message = serialized.slice(
11
+ 1,
12
+ serialized.length - this.SIGNATURE_LENGTH,
13
+ );
14
+ const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
15
+ const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
16
+ this.serialized = serialized;
17
+ this.messageVersion = (version & 255) >> 4;
18
+ this.keyId = senderKeyMessage.id;
19
+ this.iteration = senderKeyMessage.iteration;
20
+ this.ciphertext =
21
+ typeof senderKeyMessage.ciphertext === "string"
22
+ ? Buffer.from(senderKeyMessage.ciphertext, "base64")
23
+ : senderKeyMessage.ciphertext;
24
+ this.signature = signature;
25
+ } else {
26
+ const version =
27
+ (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 255) % 256;
28
+ const ciphertextBuffer = Buffer.from(ciphertext);
29
+ const message = proto.SenderKeyMessage.encode(
30
+ proto.SenderKeyMessage.create({
31
+ id: keyId,
32
+ iteration: iteration,
33
+ ciphertext: ciphertextBuffer,
34
+ }),
35
+ ).finish();
36
+ const signature = this.getSignature(
37
+ signatureKey,
38
+ Buffer.concat([Buffer.from([version]), message]),
39
+ );
40
+ this.serialized = Buffer.concat([
41
+ Buffer.from([version]),
42
+ message,
43
+ Buffer.from(signature),
44
+ ]);
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(
63
+ 0,
64
+ this.serialized.length - this.SIGNATURE_LENGTH,
65
+ );
66
+ const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
67
+ const res = verifySignature(signatureKey, part1, part2);
68
+ if (!res) throw new Error("Invalid signature!");
69
+ }
70
+ getSignature(signatureKey, serialized) {
71
+ return Buffer.from(calculateSignature(signatureKey, serialized));
72
+ }
73
+ serialize() {
74
+ return this.serialized;
75
+ }
76
+ getType() {
77
+ return 4;
78
+ }
79
+ }
@@ -0,0 +1,49 @@
1
+ function isNull(str) {
2
+ return str === null || str === "";
3
+ }
4
+ function intValue(num) {
5
+ const MAX_VALUE = 2147483647;
6
+ const MIN_VALUE = -2147483648;
7
+ if (num > MAX_VALUE || num < MIN_VALUE) {
8
+ return num & 4294967295;
9
+ }
10
+ return num;
11
+ }
12
+ function hashCode(strKey) {
13
+ let hash = 0;
14
+ if (!isNull(strKey)) {
15
+ for (let i = 0; i < strKey.length; i++) {
16
+ hash = hash * 31 + strKey.charCodeAt(i);
17
+ hash = intValue(hash);
18
+ }
19
+ }
20
+ return hash;
21
+ }
22
+ export class SenderKeyName {
23
+ constructor(groupId, sender) {
24
+ this.groupId = groupId;
25
+ this.sender = sender;
26
+ }
27
+ getGroupId() {
28
+ return this.groupId;
29
+ }
30
+ getSender() {
31
+ return this.sender;
32
+ }
33
+ serialize() {
34
+ return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
35
+ }
36
+ toString() {
37
+ return this.serialize();
38
+ }
39
+ equals(other) {
40
+ if (other === null) return false;
41
+ return (
42
+ this.groupId === other.groupId &&
43
+ this.sender.toString() === other.sender.toString()
44
+ );
45
+ }
46
+ hashCode() {
47
+ return hashCode(this.groupId) ^ hashCode(this.sender.toString());
48
+ }
49
+ }
@@ -0,0 +1,46 @@
1
+ import { BufferJSON } from "../../Utils/generics.js";
2
+ import { SenderKeyState } from "./sender-key-state.js";
3
+ export class SenderKeyRecord {
4
+ constructor(serialized) {
5
+ this.MAX_STATES = 5;
6
+ this.senderKeyStates = [];
7
+ if (serialized) {
8
+ for (const structure of serialized) {
9
+ this.senderKeyStates.push(
10
+ new SenderKeyState(null, null, null, null, null, null, structure),
11
+ );
12
+ }
13
+ }
14
+ }
15
+ isEmpty() {
16
+ return this.senderKeyStates.length === 0;
17
+ }
18
+ getSenderKeyState(keyId) {
19
+ if (keyId === undefined && this.senderKeyStates.length) {
20
+ return this.senderKeyStates[this.senderKeyStates.length - 1];
21
+ }
22
+ return this.senderKeyStates.find((state) => state.getKeyId() === keyId);
23
+ }
24
+ addSenderKeyState(id, iteration, chainKey, signatureKey) {
25
+ this.senderKeyStates.push(
26
+ new SenderKeyState(id, iteration, chainKey, null, signatureKey),
27
+ );
28
+ if (this.senderKeyStates.length > this.MAX_STATES) {
29
+ this.senderKeyStates.shift();
30
+ }
31
+ }
32
+ setSenderKeyState(id, iteration, chainKey, keyPair) {
33
+ this.senderKeyStates.length = 0;
34
+ this.senderKeyStates.push(
35
+ new SenderKeyState(id, iteration, chainKey, keyPair),
36
+ );
37
+ }
38
+ serialize() {
39
+ return this.senderKeyStates.map((state) => state.getStructure());
40
+ }
41
+ static deserialize(data) {
42
+ const str = Buffer.from(data).toString("utf-8");
43
+ const parsed = JSON.parse(str, BufferJSON.reviver);
44
+ return new SenderKeyRecord(parsed);
45
+ }
46
+ }