@japofc/baileys 2.1.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/NOTICE.md +111 -0
- package/README.md +1254 -0
- package/WAProto/index.d.ts +9 -0
- package/WAProto/index.js +142276 -0
- package/engine-requirements.js +13 -0
- package/lib/Builders/A2UI.d.ts +242 -0
- package/lib/Builders/A2UI.js +308 -0
- package/lib/Builders/AIRich.d.ts +105 -0
- package/lib/Builders/AIRich.js +2286 -0
- package/lib/Builders/Button.d.ts +103 -0
- package/lib/Builders/Button.js +883 -0
- package/lib/Builders/ButtonV2.d.ts +28 -0
- package/lib/Builders/ButtonV2.js +144 -0
- package/lib/Builders/ButtonV3.d.ts +25 -0
- package/lib/Builders/ButtonV3.js +184 -0
- package/lib/Builders/Carousel.d.ts +22 -0
- package/lib/Builders/Carousel.js +111 -0
- package/lib/Builders/JapBaileys.d.ts +34 -0
- package/lib/Builders/JapBaileys.js +108 -0
- package/lib/Builders/Poll.d.ts +25 -0
- package/lib/Builders/Poll.js +128 -0
- package/lib/Builders/index.d.ts +17 -0
- package/lib/Builders/index.js +11 -0
- package/lib/Builders/shared.d.ts +67 -0
- package/lib/Builders/shared.js +652 -0
- package/lib/Defaults/index.d.ts +154 -0
- package/lib/Defaults/index.js +178 -0
- package/lib/Framework/Bot.d.ts +61 -0
- package/lib/Framework/Bot.js +248 -0
- package/lib/Framework/Context.d.ts +21 -0
- package/lib/Framework/Context.js +89 -0
- package/lib/Framework/MediaManager.d.ts +17 -0
- package/lib/Framework/MediaManager.js +151 -0
- package/lib/Framework/SessionManager.d.ts +13 -0
- package/lib/Framework/SessionManager.js +33 -0
- package/lib/Framework/StatsManager.d.ts +26 -0
- package/lib/Framework/StatsManager.js +119 -0
- package/lib/Framework/Store/SQLiteStore.d.ts +11 -0
- package/lib/Framework/Store/SQLiteStore.js +73 -0
- package/lib/Framework/index.d.ts +9 -0
- package/lib/Framework/index.js +10 -0
- package/lib/Signal/Group/ciphertext-message.d.ts +9 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.d.ts +7 -0
- package/lib/Signal/Group/group-session-builder.js +29 -0
- package/lib/Signal/Group/group_cipher.d.ts +10 -0
- package/lib/Signal/Group/group_cipher.js +81 -0
- package/lib/Signal/Group/index.d.ts +11 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.d.ts +6 -0
- package/lib/Signal/Group/keyhelper.js +17 -0
- package/lib/Signal/Group/sender-chain-key.d.ts +13 -0
- package/lib/Signal/Group/sender-chain-key.js +25 -0
- package/lib/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +62 -0
- package/lib/Signal/Group/sender-key-message.d.ts +18 -0
- package/lib/Signal/Group/sender-key-message.js +65 -0
- package/lib/Signal/Group/sender-key-name.d.ts +11 -0
- package/lib/Signal/Group/sender-key-name.js +47 -0
- package/lib/Signal/Group/sender-key-record.d.ts +12 -0
- package/lib/Signal/Group/sender-key-record.js +40 -0
- package/lib/Signal/Group/sender-key-state.d.ts +16 -0
- package/lib/Signal/Group/sender-key-state.js +83 -0
- package/lib/Signal/Group/sender-message-key.d.ts +11 -0
- package/lib/Signal/Group/sender-message-key.js +25 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +430 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +276 -0
- package/lib/Socket/Client/index.d.ts +2 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.d.ts +5 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.d.ts +12 -0
- package/lib/Socket/Client/websocket.js +53 -0
- package/lib/Socket/business.d.ts +395 -0
- package/lib/Socket/business.js +379 -0
- package/lib/Socket/chats.d.ts +136 -0
- package/lib/Socket/chats.js +1327 -0
- package/lib/Socket/communities.d.ts +562 -0
- package/lib/Socket/communities.js +439 -0
- package/lib/Socket/dugong.d.ts +117 -0
- package/lib/Socket/dugong.js +798 -0
- package/lib/Socket/groups.d.ts +285 -0
- package/lib/Socket/groups.js +354 -0
- package/lib/Socket/index.d.ts +585 -0
- package/lib/Socket/index.js +52 -0
- package/lib/Socket/messages-recv.d.ts +333 -0
- package/lib/Socket/messages-recv.js +1781 -0
- package/lib/Socket/messages-send.d.ts +353 -0
- package/lib/Socket/messages-send.js +1524 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.d.ts +286 -0
- package/lib/Socket/newsletter.js +364 -0
- package/lib/Socket/socket.d.ts +60 -0
- package/lib/Socket/socket.js +1012 -0
- package/lib/Socket/username.d.ts +82 -0
- package/lib/Socket/username.js +159 -0
- package/lib/Store/index.d.ts +3 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.d.ts +62 -0
- package/lib/Store/make-in-memory-store.js +428 -0
- package/lib/Store/make-ordered-dictionary.d.ts +12 -0
- package/lib/Store/make-ordered-dictionary.js +78 -0
- package/lib/Store/object-repository.d.ts +10 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.d.ts +117 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.d.ts +25 -0
- package/lib/Types/Call.d.ts +15 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +124 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.d.ts +26 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +256 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.d.ts +71 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +5 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.d.ts +6 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.d.ts +62 -0
- package/lib/Types/Message.js +17 -0
- package/lib/Types/Mex.d.ts +2 -0
- package/lib/Types/Mex.js +38 -0
- package/lib/Types/Product.d.ts +79 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/RichType.d.ts +2 -0
- package/lib/Types/RichType.js +22 -0
- package/lib/Types/Signal.d.ts +87 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.d.ts +138 -0
- package/lib/Types/Socket.js +1 -0
- package/lib/Types/State.d.ts +8 -0
- package/lib/Types/State.js +55 -0
- package/lib/Types/USync.d.ts +26 -0
- package/lib/Types/index.d.ts +68 -0
- package/lib/Types/index.js +27 -0
- package/lib/Utils/MessageBuilder.d.ts +2 -0
- package/lib/Utils/MessageBuilder.js +2 -0
- package/lib/Utils/PersistentStore.d.ts +60 -0
- package/lib/Utils/PersistentStore.js +592 -0
- package/lib/Utils/anti-delete.d.ts +74 -0
- package/lib/Utils/anti-delete.js +221 -0
- package/lib/Utils/auth-secure.d.ts +86 -0
- package/lib/Utils/auth-secure.js +764 -0
- package/lib/Utils/auth-utils.d.ts +57 -0
- package/lib/Utils/auth-utils.js +306 -0
- package/lib/Utils/auto-reply.d.ts +47 -0
- package/lib/Utils/auto-reply.js +157 -0
- package/lib/Utils/baileys-event-stream.d.ts +8 -0
- package/lib/Utils/baileys-event-stream.js +62 -0
- package/lib/Utils/browser-utils.d.ts +8 -0
- package/lib/Utils/browser-utils.js +27 -0
- package/lib/Utils/business.d.ts +49 -0
- package/lib/Utils/business.js +239 -0
- package/lib/Utils/button-helper-utils.d.ts +53 -0
- package/lib/Utils/button-helper-utils.js +312 -0
- package/lib/Utils/button-sender.d.ts +54 -0
- package/lib/Utils/button-sender.js +822 -0
- package/lib/Utils/chat-control.d.ts +159 -0
- package/lib/Utils/chat-control.js +230 -0
- package/lib/Utils/chat-history-helpers.d.ts +21 -0
- package/lib/Utils/chat-history-helpers.js +70 -0
- package/lib/Utils/chat-utils.d.ts +407 -0
- package/lib/Utils/chat-utils.js +885 -0
- package/lib/Utils/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/companion-reg-client-utils.js +39 -0
- package/lib/Utils/crypto.d.ts +41 -0
- package/lib/Utils/crypto.js +147 -0
- package/lib/Utils/decode-wa-message.d.ts +83 -0
- package/lib/Utils/decode-wa-message.js +316 -0
- package/lib/Utils/event-buffer.d.ts +12 -0
- package/lib/Utils/event-buffer.js +635 -0
- package/lib/Utils/generics.d.ts +69 -0
- package/lib/Utils/generics.js +438 -0
- package/lib/Utils/history-share.d.ts +11 -0
- package/lib/Utils/history-share.js +70 -0
- package/lib/Utils/history.d.ts +90 -0
- package/lib/Utils/history.js +146 -0
- package/lib/Utils/humanizer.d.ts +12 -0
- package/lib/Utils/humanizer.js +73 -0
- package/lib/Utils/identity-change-handler.d.ts +13 -0
- package/lib/Utils/identity-change-handler.js +49 -0
- package/lib/Utils/index.d.ts +57 -0
- package/lib/Utils/index.js +56 -0
- package/lib/Utils/jid-tools.d.ts +6 -0
- package/lib/Utils/jid-tools.js +77 -0
- package/lib/Utils/link-preview.d.ts +12 -0
- package/lib/Utils/link-preview.js +84 -0
- package/lib/Utils/logger.d.ts +4 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.d.ts +12 -0
- package/lib/Utils/lt-hash.js +26 -0
- package/lib/Utils/make-mutex.d.ts +6 -0
- package/lib/Utils/make-mutex.js +32 -0
- package/lib/Utils/media-messages.d.ts +18 -0
- package/lib/Utils/media-messages.js +70 -0
- package/lib/Utils/media-set.d.ts +13 -0
- package/lib/Utils/media-set.js +171 -0
- package/lib/Utils/message-kind.d.ts +32 -0
- package/lib/Utils/message-kind.js +136 -0
- package/lib/Utils/message-retry-manager.d.ts +79 -0
- package/lib/Utils/message-retry-manager.js +264 -0
- package/lib/Utils/message-search.d.ts +44 -0
- package/lib/Utils/message-search.js +188 -0
- package/lib/Utils/messages-media.d.ts +135 -0
- package/lib/Utils/messages-media.js +868 -0
- package/lib/Utils/messages.d.ts +44 -0
- package/lib/Utils/messages.js +2326 -0
- package/lib/Utils/meta-ai.d.ts +13 -0
- package/lib/Utils/meta-ai.js +74 -0
- package/lib/Utils/mini-app.d.ts +35 -0
- package/lib/Utils/mini-app.js +213 -0
- package/lib/Utils/noise-handler.d.ts +20 -0
- package/lib/Utils/noise-handler.js +200 -0
- package/lib/Utils/offline-node-processor.d.ts +9 -0
- package/lib/Utils/offline-node-processor.js +39 -0
- package/lib/Utils/past-participants.d.ts +14 -0
- package/lib/Utils/past-participants.js +40 -0
- package/lib/Utils/pre-key-manager.d.ts +25 -0
- package/lib/Utils/pre-key-manager.js +105 -0
- package/lib/Utils/process-message.d.ts +82 -0
- package/lib/Utils/process-message.js +808 -0
- package/lib/Utils/qr-render.d.ts +59 -0
- package/lib/Utils/qr-render.js +179 -0
- package/lib/Utils/qrcodegen.d.ts +79 -0
- package/lib/Utils/qrcodegen.js +848 -0
- package/lib/Utils/reporting-utils.d.ts +12 -0
- package/lib/Utils/reporting-utils.js +257 -0
- package/lib/Utils/rich-message-utils.d.ts +48 -0
- package/lib/Utils/rich-message-utils.js +444 -0
- package/lib/Utils/router.d.ts +37 -0
- package/lib/Utils/router.js +165 -0
- package/lib/Utils/scheduled-posts.d.ts +30 -0
- package/lib/Utils/scheduled-posts.js +110 -0
- package/lib/Utils/scheduling.d.ts +42 -0
- package/lib/Utils/scheduling.js +139 -0
- package/lib/Utils/signal.d.ts +82 -0
- package/lib/Utils/signal.js +200 -0
- package/lib/Utils/stanza-ack.d.ts +16 -0
- package/lib/Utils/stanza-ack.js +37 -0
- package/lib/Utils/status.d.ts +50 -0
- package/lib/Utils/status.js +107 -0
- package/lib/Utils/stickerpack.d.ts +51 -0
- package/lib/Utils/stickerpack.js +274 -0
- package/lib/Utils/sync-action-utils.d.ts +2 -0
- package/lib/Utils/sync-action-utils.js +48 -0
- package/lib/Utils/tag.d.ts +12 -0
- package/lib/Utils/tag.js +44 -0
- package/lib/Utils/tc-token-utils.d.ts +29 -0
- package/lib/Utils/tc-token-utils.js +162 -0
- package/lib/Utils/templates.d.ts +76 -0
- package/lib/Utils/templates.js +150 -0
- package/lib/Utils/transcribe.d.ts +12 -0
- package/lib/Utils/transcribe.js +107 -0
- package/lib/Utils/use-cache-manager-auth-state.d.ts +13 -0
- package/lib/Utils/use-cache-manager-auth-state.js +81 -0
- package/lib/Utils/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-multi-file-auth-state.js +166 -0
- package/lib/Utils/use-single-file-auth-state.d.ts +10 -0
- package/lib/Utils/use-single-file-auth-state.js +113 -0
- package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/use-sqlite-auth-state.js +169 -0
- package/lib/Utils/validate-connection.d.ts +42 -0
- package/lib/Utils/validate-connection.js +198 -0
- package/lib/Utils/vcard.d.ts +58 -0
- package/lib/Utils/vcard.js +104 -0
- package/lib/Utils/voice-note.d.ts +15 -0
- package/lib/Utils/voice-note.js +63 -0
- package/lib/VoIP/audio-feeder.d.ts +18 -0
- package/lib/VoIP/audio-feeder.js +160 -0
- package/lib/VoIP/call-recorder.d.ts +13 -0
- package/lib/VoIP/call-recorder.js +73 -0
- package/lib/VoIP/index.d.ts +86 -0
- package/lib/VoIP/index.js +611 -0
- package/lib/VoIP/relay-transport.d.ts +43 -0
- package/lib/VoIP/relay-transport.js +563 -0
- package/lib/VoIP/signaling.d.ts +26 -0
- package/lib/VoIP/signaling.js +662 -0
- package/lib/VoIP/types.d.ts +69 -0
- package/lib/VoIP/types.js +17 -0
- package/lib/VoIP/wasm-engine.d.ts +120 -0
- package/lib/VoIP/wasm-engine.js +1358 -0
- package/lib/VoIP/worker-bootstrap.d.ts +4 -0
- package/lib/VoIP/worker-bootstrap.js +1046 -0
- package/lib/WABinary/constants.d.ts +61 -0
- package/lib/WABinary/constants.js +1466 -0
- package/lib/WABinary/decode.d.ts +9 -0
- package/lib/WABinary/decode.js +261 -0
- package/lib/WABinary/encode.d.ts +2 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.d.ts +74 -0
- package/lib/WABinary/generic-utils.js +276 -0
- package/lib/WABinary/index.d.ts +5 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.d.ts +28 -0
- package/lib/WABinary/jid-utils.js +95 -0
- package/lib/WABinary/types.d.ts +19 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.d.ts +7 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.d.ts +34926 -0
- package/lib/WAM/constants.js +22852 -0
- package/lib/WAM/encode.d.ts +1 -0
- package/lib/WAM/encode.js +149 -0
- package/lib/WAM/index.d.ts +3 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +51 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +29 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +62 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/lib/WAUSync/Protocols/index.d.ts +7 -0
- package/lib/WAUSync/Protocols/index.js +7 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +121 -0
- package/lib/WAUSync/USyncUser.d.ts +16 -0
- package/lib/WAUSync/USyncUser.js +30 -0
- package/lib/WAUSync/index.d.ts +3 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/assets/wasm/loader.d.ts +5 -0
- package/lib/assets/wasm/loader.js +5 -0
- package/lib/assets/wasm/whatsapp.wasm +0 -0
- package/lib/assets/wasm/worker-modules.d.ts +5 -0
- package/lib/assets/wasm/worker-modules.js +273 -0
- package/lib/index.d.ts +18 -0
- package/lib/index.js +23 -0
- package/package.json +180 -0
- package/postinstall-banner.js +82 -0
|
@@ -0,0 +1,81 @@
|
|
|
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(senderKeyState, iteration === 0 ? 0 : iteration + 1);
|
|
22
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
|
|
23
|
+
const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
|
|
24
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
25
|
+
return senderKeyMessage.serialize();
|
|
26
|
+
}
|
|
27
|
+
async decrypt(senderKeyMessageBytes) {
|
|
28
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
29
|
+
if (!record) {
|
|
30
|
+
throw new Error('No SenderKeyRecord found for decryption');
|
|
31
|
+
}
|
|
32
|
+
const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
|
|
33
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
|
|
34
|
+
if (!senderKeyState) {
|
|
35
|
+
throw new Error('No session found to decrypt message');
|
|
36
|
+
}
|
|
37
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
|
|
38
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
|
|
39
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
|
40
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
41
|
+
return plaintext;
|
|
42
|
+
}
|
|
43
|
+
getSenderKey(senderKeyState, iteration) {
|
|
44
|
+
let senderChainKey = senderKeyState.getSenderChainKey();
|
|
45
|
+
if (senderChainKey.getIteration() > iteration) {
|
|
46
|
+
if (senderKeyState.hasSenderMessageKey(iteration)) {
|
|
47
|
+
const messageKey = senderKeyState.removeSenderMessageKey(iteration);
|
|
48
|
+
if (!messageKey) {
|
|
49
|
+
throw new Error('No sender message key found for iteration');
|
|
50
|
+
}
|
|
51
|
+
return messageKey;
|
|
52
|
+
}
|
|
53
|
+
throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
|
|
54
|
+
}
|
|
55
|
+
if (iteration - senderChainKey.getIteration() > 2000) {
|
|
56
|
+
throw new Error('Over 2000 messages into the future!');
|
|
57
|
+
}
|
|
58
|
+
while (senderChainKey.getIteration() < iteration) {
|
|
59
|
+
senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
|
|
60
|
+
senderChainKey = senderChainKey.getNext();
|
|
61
|
+
}
|
|
62
|
+
senderKeyState.setSenderChainKey(senderChainKey.getNext());
|
|
63
|
+
return senderChainKey.getSenderMessageKey();
|
|
64
|
+
}
|
|
65
|
+
async getPlainText(iv, key, ciphertext) {
|
|
66
|
+
try {
|
|
67
|
+
return decrypt(key, ciphertext, iv);
|
|
68
|
+
}
|
|
69
|
+
catch (e) {
|
|
70
|
+
throw new Error('InvalidMessageException');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async getCipherText(iv, key, plaintext) {
|
|
74
|
+
try {
|
|
75
|
+
return encrypt(key, plaintext, iv);
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
throw new Error('InvalidMessageException');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -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,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,17 @@
|
|
|
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 {
|
|
14
|
+
public: Buffer.from(key.pubKey),
|
|
15
|
+
private: Buffer.from(key.privKey)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export class SenderChainKey {
|
|
2
|
+
constructor(iteration: any, chainKey: any);
|
|
3
|
+
MESSAGE_KEY_SEED: any;
|
|
4
|
+
CHAIN_KEY_SEED: any;
|
|
5
|
+
iteration: any;
|
|
6
|
+
chainKey: any;
|
|
7
|
+
getIteration(): any;
|
|
8
|
+
getSenderMessageKey(): SenderMessageKey;
|
|
9
|
+
getNext(): SenderChainKey;
|
|
10
|
+
getSeed(): any;
|
|
11
|
+
getDerivative(seed: any, key: any): any;
|
|
12
|
+
}
|
|
13
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
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([0x01]);
|
|
6
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02]);
|
|
7
|
+
this.iteration = iteration;
|
|
8
|
+
this.chainKey = Buffer.from(chainKey);
|
|
9
|
+
}
|
|
10
|
+
getIteration() {
|
|
11
|
+
return this.iteration;
|
|
12
|
+
}
|
|
13
|
+
getSenderMessageKey() {
|
|
14
|
+
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
|
|
15
|
+
}
|
|
16
|
+
getNext() {
|
|
17
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
|
|
18
|
+
}
|
|
19
|
+
getSeed() {
|
|
20
|
+
return this.chainKey;
|
|
21
|
+
}
|
|
22
|
+
getDerivative(seed, key) {
|
|
23
|
+
return calculateMAC(key, seed);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
2
|
+
constructor(id: any, iteration: any, chainKey: any, signatureKey: any, serialized: any);
|
|
3
|
+
serialized: any;
|
|
4
|
+
id: any;
|
|
5
|
+
iteration: any;
|
|
6
|
+
chainKey: any;
|
|
7
|
+
signatureKey: any;
|
|
8
|
+
intsToByteHighAndLow(highValue: any, lowValue: any): number;
|
|
9
|
+
serialize(): any;
|
|
10
|
+
getType(): number;
|
|
11
|
+
getIteration(): any;
|
|
12
|
+
getChainKey(): any;
|
|
13
|
+
getSignatureKey(): any;
|
|
14
|
+
getId(): any;
|
|
15
|
+
}
|
|
16
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { proto } from '../../../WAProto/index.js';
|
|
2
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
3
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
4
|
+
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
5
|
+
super();
|
|
6
|
+
if (serialized) {
|
|
7
|
+
try {
|
|
8
|
+
const message = serialized.slice(1);
|
|
9
|
+
const distributionMessage = proto.SenderKeyDistributionMessage.decode(message).toJSON();
|
|
10
|
+
this.serialized = serialized;
|
|
11
|
+
this.id = distributionMessage.id;
|
|
12
|
+
this.iteration = distributionMessage.iteration;
|
|
13
|
+
this.chainKey =
|
|
14
|
+
typeof distributionMessage.chainKey === 'string'
|
|
15
|
+
? Buffer.from(distributionMessage.chainKey, 'base64')
|
|
16
|
+
: distributionMessage.chainKey;
|
|
17
|
+
this.signatureKey =
|
|
18
|
+
typeof distributionMessage.signingKey === 'string'
|
|
19
|
+
? Buffer.from(distributionMessage.signingKey, 'base64')
|
|
20
|
+
: distributionMessage.signingKey;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
throw new Error(String(e));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
|
|
28
|
+
this.id = id;
|
|
29
|
+
this.iteration = iteration;
|
|
30
|
+
this.chainKey = chainKey;
|
|
31
|
+
this.signatureKey = signatureKey;
|
|
32
|
+
const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
|
|
33
|
+
id,
|
|
34
|
+
iteration,
|
|
35
|
+
chainKey,
|
|
36
|
+
signingKey: this.signatureKey
|
|
37
|
+
})).finish();
|
|
38
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
intsToByteHighAndLow(highValue, lowValue) {
|
|
42
|
+
return (((highValue << 4) | lowValue) & 0xff) % 256;
|
|
43
|
+
}
|
|
44
|
+
serialize() {
|
|
45
|
+
return this.serialized;
|
|
46
|
+
}
|
|
47
|
+
getType() {
|
|
48
|
+
return this.SENDERKEY_DISTRIBUTION_TYPE;
|
|
49
|
+
}
|
|
50
|
+
getIteration() {
|
|
51
|
+
return this.iteration;
|
|
52
|
+
}
|
|
53
|
+
getChainKey() {
|
|
54
|
+
return this.chainKey;
|
|
55
|
+
}
|
|
56
|
+
getSignatureKey() {
|
|
57
|
+
return this.signatureKey;
|
|
58
|
+
}
|
|
59
|
+
getId() {
|
|
60
|
+
return this.id;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
2
|
+
constructor(keyId: any, iteration: any, ciphertext: any, signatureKey: any, serialized: any);
|
|
3
|
+
SIGNATURE_LENGTH: number;
|
|
4
|
+
serialized: any;
|
|
5
|
+
messageVersion: number;
|
|
6
|
+
keyId: any;
|
|
7
|
+
iteration: any;
|
|
8
|
+
ciphertext: any;
|
|
9
|
+
signature: any;
|
|
10
|
+
getKeyId(): any;
|
|
11
|
+
getIteration(): any;
|
|
12
|
+
getCipherText(): any;
|
|
13
|
+
verifySignature(signatureKey: any): void;
|
|
14
|
+
getSignature(signatureKey: any, serialized: any): any;
|
|
15
|
+
serialize(): any;
|
|
16
|
+
getType(): number;
|
|
17
|
+
}
|
|
18
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
@@ -0,0 +1,65 @@
|
|
|
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(1, serialized.length - this.SIGNATURE_LENGTH);
|
|
11
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
12
|
+
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
|
|
13
|
+
this.serialized = serialized;
|
|
14
|
+
this.messageVersion = (version & 0xff) >> 4;
|
|
15
|
+
this.keyId = senderKeyMessage.id;
|
|
16
|
+
this.iteration = senderKeyMessage.iteration;
|
|
17
|
+
this.ciphertext =
|
|
18
|
+
typeof senderKeyMessage.ciphertext === 'string'
|
|
19
|
+
? Buffer.from(senderKeyMessage.ciphertext, 'base64')
|
|
20
|
+
: senderKeyMessage.ciphertext;
|
|
21
|
+
this.signature = signature;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
|
|
25
|
+
const ciphertextBuffer = Buffer.from(ciphertext);
|
|
26
|
+
const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
|
|
27
|
+
id: keyId,
|
|
28
|
+
iteration: iteration,
|
|
29
|
+
ciphertext: ciphertextBuffer
|
|
30
|
+
})).finish();
|
|
31
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
|
|
32
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
|
|
33
|
+
this.messageVersion = this.CURRENT_VERSION;
|
|
34
|
+
this.keyId = keyId;
|
|
35
|
+
this.iteration = iteration;
|
|
36
|
+
this.ciphertext = ciphertextBuffer;
|
|
37
|
+
this.signature = signature;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
getKeyId() {
|
|
41
|
+
return this.keyId;
|
|
42
|
+
}
|
|
43
|
+
getIteration() {
|
|
44
|
+
return this.iteration;
|
|
45
|
+
}
|
|
46
|
+
getCipherText() {
|
|
47
|
+
return this.ciphertext;
|
|
48
|
+
}
|
|
49
|
+
verifySignature(signatureKey) {
|
|
50
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
|
|
51
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
52
|
+
const res = verifySignature(signatureKey, part1, part2);
|
|
53
|
+
if (!res)
|
|
54
|
+
throw new Error('Invalid signature!');
|
|
55
|
+
}
|
|
56
|
+
getSignature(signatureKey, serialized) {
|
|
57
|
+
return Buffer.from(calculateSignature(signatureKey, serialized));
|
|
58
|
+
}
|
|
59
|
+
serialize() {
|
|
60
|
+
return this.serialized;
|
|
61
|
+
}
|
|
62
|
+
getType() {
|
|
63
|
+
return 4;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
function isNull(str) {
|
|
2
|
+
return str === null || str === '';
|
|
3
|
+
}
|
|
4
|
+
function intValue(num) {
|
|
5
|
+
const MAX_VALUE = 0x7fffffff;
|
|
6
|
+
const MIN_VALUE = -0x80000000;
|
|
7
|
+
if (num > MAX_VALUE || num < MIN_VALUE) {
|
|
8
|
+
return num & 0xffffffff;
|
|
9
|
+
}
|
|
10
|
+
return num;
|
|
11
|
+
}
|
|
12
|
+
function hashCode(strKey) {
|
|
13
|
+
let hash = 0;
|
|
14
|
+
if (!isNull(strKey)) {
|
|
15
|
+
for (let i = 0; i < strKey.length; i++) {
|
|
16
|
+
hash = hash * 31 + strKey.charCodeAt(i);
|
|
17
|
+
hash = intValue(hash);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return hash;
|
|
21
|
+
}
|
|
22
|
+
export class SenderKeyName {
|
|
23
|
+
constructor(groupId, sender) {
|
|
24
|
+
this.groupId = groupId;
|
|
25
|
+
this.sender = sender;
|
|
26
|
+
}
|
|
27
|
+
getGroupId() {
|
|
28
|
+
return this.groupId;
|
|
29
|
+
}
|
|
30
|
+
getSender() {
|
|
31
|
+
return this.sender;
|
|
32
|
+
}
|
|
33
|
+
serialize() {
|
|
34
|
+
return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
|
|
35
|
+
}
|
|
36
|
+
toString() {
|
|
37
|
+
return this.serialize();
|
|
38
|
+
}
|
|
39
|
+
equals(other) {
|
|
40
|
+
if (other === null)
|
|
41
|
+
return false;
|
|
42
|
+
return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
|
|
43
|
+
}
|
|
44
|
+
hashCode() {
|
|
45
|
+
return hashCode(this.groupId) ^ hashCode(this.sender.toString());
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export class SenderKeyRecord {
|
|
2
|
+
static deserialize(data: any): SenderKeyRecord;
|
|
3
|
+
constructor(serialized: any);
|
|
4
|
+
MAX_STATES: number;
|
|
5
|
+
senderKeyStates: SenderKeyState[];
|
|
6
|
+
isEmpty(): boolean;
|
|
7
|
+
getSenderKeyState(keyId: any): SenderKeyState | undefined;
|
|
8
|
+
addSenderKeyState(id: any, iteration: any, chainKey: any, signatureKey: any): void;
|
|
9
|
+
setSenderKeyState(id: any, iteration: any, chainKey: any, keyPair: any): void;
|
|
10
|
+
serialize(): any[];
|
|
11
|
+
}
|
|
12
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BufferJSON } from '../../Utils/generics.js';
|
|
2
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
3
|
+
export class SenderKeyRecord {
|
|
4
|
+
constructor(serialized) {
|
|
5
|
+
this.MAX_STATES = 5;
|
|
6
|
+
this.senderKeyStates = [];
|
|
7
|
+
if (serialized) {
|
|
8
|
+
for (const structure of serialized) {
|
|
9
|
+
this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
isEmpty() {
|
|
14
|
+
return this.senderKeyStates.length === 0;
|
|
15
|
+
}
|
|
16
|
+
getSenderKeyState(keyId) {
|
|
17
|
+
if (keyId === undefined && this.senderKeyStates.length) {
|
|
18
|
+
return this.senderKeyStates[this.senderKeyStates.length - 1];
|
|
19
|
+
}
|
|
20
|
+
return this.senderKeyStates.find(state => state.getKeyId() === keyId);
|
|
21
|
+
}
|
|
22
|
+
addSenderKeyState(id, iteration, chainKey, signatureKey) {
|
|
23
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
|
|
24
|
+
if (this.senderKeyStates.length > this.MAX_STATES) {
|
|
25
|
+
this.senderKeyStates.shift();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
setSenderKeyState(id, iteration, chainKey, keyPair) {
|
|
29
|
+
this.senderKeyStates.length = 0;
|
|
30
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
|
|
31
|
+
}
|
|
32
|
+
serialize() {
|
|
33
|
+
return this.senderKeyStates.map(state => state.getStructure());
|
|
34
|
+
}
|
|
35
|
+
static deserialize(data) {
|
|
36
|
+
const str = Buffer.from(data).toString('utf-8');
|
|
37
|
+
const parsed = JSON.parse(str, BufferJSON.reviver);
|
|
38
|
+
return new SenderKeyRecord(parsed);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class SenderKeyState {
|
|
2
|
+
constructor(id: any, iteration: any, chainKey: any, signatureKeyPair: any, signatureKeyPublic: any, signatureKeyPrivate: any, senderKeyStateStructure: any);
|
|
3
|
+
MAX_MESSAGE_KEYS: number;
|
|
4
|
+
senderKeyStateStructure: any;
|
|
5
|
+
getKeyId(): any;
|
|
6
|
+
getSenderChainKey(): SenderChainKey;
|
|
7
|
+
setSenderChainKey(chainKey: any): void;
|
|
8
|
+
getSigningKeyPublic(): any;
|
|
9
|
+
getSigningKeyPrivate(): any;
|
|
10
|
+
hasSenderMessageKey(iteration: any): any;
|
|
11
|
+
addSenderMessageKey(senderMessageKey: any): void;
|
|
12
|
+
removeSenderMessageKey(iteration: any): SenderMessageKey | null;
|
|
13
|
+
getStructure(): any;
|
|
14
|
+
}
|
|
15
|
+
import { SenderChainKey } from './sender-chain-key.js';
|
|
16
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { SenderChainKey } from './sender-chain-key.js';
|
|
2
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
3
|
+
export class SenderKeyState {
|
|
4
|
+
constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
|
|
5
|
+
this.MAX_MESSAGE_KEYS = 2000;
|
|
6
|
+
if (senderKeyStateStructure) {
|
|
7
|
+
this.senderKeyStateStructure = {
|
|
8
|
+
...senderKeyStateStructure,
|
|
9
|
+
senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
|
|
10
|
+
? senderKeyStateStructure.senderMessageKeys
|
|
11
|
+
: []
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
if (signatureKeyPair) {
|
|
16
|
+
signatureKeyPublic = signatureKeyPair.public;
|
|
17
|
+
signatureKeyPrivate = signatureKeyPair.private;
|
|
18
|
+
}
|
|
19
|
+
this.senderKeyStateStructure = {
|
|
20
|
+
senderKeyId: id || 0,
|
|
21
|
+
senderChainKey: {
|
|
22
|
+
iteration: iteration || 0,
|
|
23
|
+
seed: Buffer.from(chainKey || [])
|
|
24
|
+
},
|
|
25
|
+
senderSigningKey: {
|
|
26
|
+
public: Buffer.from(signatureKeyPublic || []),
|
|
27
|
+
private: Buffer.from(signatureKeyPrivate || [])
|
|
28
|
+
},
|
|
29
|
+
senderMessageKeys: []
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
getKeyId() {
|
|
34
|
+
return this.senderKeyStateStructure.senderKeyId;
|
|
35
|
+
}
|
|
36
|
+
getSenderChainKey() {
|
|
37
|
+
return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
|
|
38
|
+
}
|
|
39
|
+
setSenderChainKey(chainKey) {
|
|
40
|
+
this.senderKeyStateStructure.senderChainKey = {
|
|
41
|
+
iteration: chainKey.getIteration(),
|
|
42
|
+
seed: chainKey.getSeed()
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
getSigningKeyPublic() {
|
|
46
|
+
const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public);
|
|
47
|
+
if (publicKey.length === 32) {
|
|
48
|
+
const fixed = Buffer.alloc(33);
|
|
49
|
+
fixed[0] = 0x05;
|
|
50
|
+
publicKey.copy(fixed, 1);
|
|
51
|
+
return fixed;
|
|
52
|
+
}
|
|
53
|
+
return publicKey;
|
|
54
|
+
}
|
|
55
|
+
getSigningKeyPrivate() {
|
|
56
|
+
const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
|
|
57
|
+
return Buffer.from(privateKey || []);
|
|
58
|
+
}
|
|
59
|
+
hasSenderMessageKey(iteration) {
|
|
60
|
+
return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
|
|
61
|
+
}
|
|
62
|
+
addSenderMessageKey(senderMessageKey) {
|
|
63
|
+
this.senderKeyStateStructure.senderMessageKeys.push({
|
|
64
|
+
iteration: senderMessageKey.getIteration(),
|
|
65
|
+
seed: senderMessageKey.getSeed()
|
|
66
|
+
});
|
|
67
|
+
if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
|
|
68
|
+
this.senderKeyStateStructure.senderMessageKeys.shift();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
removeSenderMessageKey(iteration) {
|
|
72
|
+
const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
|
|
73
|
+
if (index !== -1) {
|
|
74
|
+
const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
|
|
75
|
+
this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
|
|
76
|
+
return new SenderMessageKey(messageKey.iteration, messageKey.seed);
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
getStructure() {
|
|
81
|
+
return this.senderKeyStateStructure;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { deriveSecrets } from 'libsignal/src/crypto.js';
|
|
2
|
+
export class SenderMessageKey {
|
|
3
|
+
constructor(iteration, seed) {
|
|
4
|
+
const derivative = deriveSecrets(seed, Buffer.alloc(32), Buffer.from('WhisperGroup'));
|
|
5
|
+
const keys = new Uint8Array(32);
|
|
6
|
+
keys.set(new Uint8Array(derivative[0].slice(16)));
|
|
7
|
+
keys.set(new Uint8Array(derivative[1].slice(0, 16)), 16);
|
|
8
|
+
this.iv = Buffer.from(derivative[0].slice(0, 16));
|
|
9
|
+
this.cipherKey = Buffer.from(keys.buffer);
|
|
10
|
+
this.iteration = iteration;
|
|
11
|
+
this.seed = seed;
|
|
12
|
+
}
|
|
13
|
+
getIteration() {
|
|
14
|
+
return this.iteration;
|
|
15
|
+
}
|
|
16
|
+
getIv() {
|
|
17
|
+
return this.iv;
|
|
18
|
+
}
|
|
19
|
+
getCipherKey() {
|
|
20
|
+
return this.cipherKey;
|
|
21
|
+
}
|
|
22
|
+
getSeed() {
|
|
23
|
+
return this.seed;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export function makeLibSignalRepository(auth: any, logger: any, pnToLIDFunc: any): {
|
|
2
|
+
decryptGroupMessage({ group, authorJid, msg }: {
|
|
3
|
+
group: any;
|
|
4
|
+
authorJid: any;
|
|
5
|
+
msg: any;
|
|
6
|
+
}): any;
|
|
7
|
+
processSenderKeyDistributionMessage({ item, authorJid }: {
|
|
8
|
+
item: any;
|
|
9
|
+
authorJid: any;
|
|
10
|
+
}): Promise<any>;
|
|
11
|
+
decryptMessage({ jid, type, ciphertext }: {
|
|
12
|
+
jid: any;
|
|
13
|
+
type: any;
|
|
14
|
+
ciphertext: any;
|
|
15
|
+
}): Promise<any>;
|
|
16
|
+
encryptMessage({ jid, data }: {
|
|
17
|
+
jid: any;
|
|
18
|
+
data: any;
|
|
19
|
+
}): Promise<any>;
|
|
20
|
+
encryptGroupMessage({ group, meId, data }: {
|
|
21
|
+
group: any;
|
|
22
|
+
meId: any;
|
|
23
|
+
data: any;
|
|
24
|
+
}): Promise<any>;
|
|
25
|
+
getSenderKeyDistributionMessage({ group, meId }: {
|
|
26
|
+
group: any;
|
|
27
|
+
meId: any;
|
|
28
|
+
}): Promise<any>;
|
|
29
|
+
hasSenderKey({ group, meId }: {
|
|
30
|
+
group: any;
|
|
31
|
+
meId: any;
|
|
32
|
+
}): Promise<boolean>;
|
|
33
|
+
getSessionInfo(jid: any): Promise<{
|
|
34
|
+
baseKey: Uint8Array<any>;
|
|
35
|
+
registrationId: number;
|
|
36
|
+
} | null>;
|
|
37
|
+
injectE2ESession({ jid, session }: {
|
|
38
|
+
jid: any;
|
|
39
|
+
session: any;
|
|
40
|
+
}): Promise<any>;
|
|
41
|
+
jidToSignalProtocolAddress(jid: any): any;
|
|
42
|
+
lidMapping: LIDMappingStore;
|
|
43
|
+
validateSession(jid: any): Promise<{
|
|
44
|
+
exists: boolean;
|
|
45
|
+
reason: string;
|
|
46
|
+
} | {
|
|
47
|
+
exists: boolean;
|
|
48
|
+
reason?: undefined;
|
|
49
|
+
}>;
|
|
50
|
+
deleteSession(jids: any): Promise<any>;
|
|
51
|
+
close(): void;
|
|
52
|
+
migrateSession(fromJid: any, toJid: any): Promise<any>;
|
|
53
|
+
};
|
|
54
|
+
import { LIDMappingStore } from './lid-mapping.js';
|