ruby-baileys 0.0.0-stage → 1.5.17
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/EXPERIMENTAL.md +350 -0
- package/LICENSE +23 -0
- package/NOTICE.md +14 -0
- package/README.md +6265 -2
- package/WAProto/GenerateStatics.sh +3 -0
- package/WAProto/WAProto.proto +5479 -0
- package/WAProto/fix-imports.js +81 -0
- package/WAProto/index.d.ts +16877 -0
- package/WAProto/index.js +122159 -0
- package/assets/ruby_aspirant.jpg +0 -0
- package/assets/ruby_aspirant_4k.jpg +88 -0
- package/assets/ruby_aspirant_full.jpg +0 -0
- package/assets/ruby_aspirant_render.png +1 -0
- package/assets/ruby_aspirants_hd.jpg +0 -0
- package/assets/thumb.jpg +0 -0
- package/engine-requirements.js +14 -0
- package/index.cjs +29 -0
- package/index.d.ts +2 -0
- package/package.json +178 -4
- package/src/Defaults/index.d.ts +153 -0
- package/src/Defaults/index.js +165 -0
- package/src/MessageBuilder/bot-signature.d.ts +22 -0
- package/src/MessageBuilder/bot-signature.js +123 -0
- package/src/MessageBuilder/extras.d.ts +396 -0
- package/src/MessageBuilder/extras.js +1229 -0
- package/src/MessageBuilder/index.d.ts +210 -0
- package/src/MessageBuilder/index.js +3521 -0
- package/src/MessageBuilder/metaai.d.ts +438 -0
- package/src/MessageBuilder/metaai.js +891 -0
- package/src/Signal/Group/ciphertext-message.d.ts +10 -0
- package/src/Signal/Group/ciphertext-message.js +12 -0
- package/src/Signal/Group/group-session-builder.d.ts +8 -0
- package/src/Signal/Group/group-session-builder.js +28 -0
- package/src/Signal/Group/group_cipher.d.ts +11 -0
- package/src/Signal/Group/group_cipher.js +79 -0
- package/src/Signal/Group/index.d.ts +12 -0
- package/src/Signal/Group/index.js +12 -0
- package/src/Signal/Group/keyhelper.d.ts +7 -0
- package/src/Signal/Group/keyhelper.js +18 -0
- package/src/Signal/Group/sender-chain-key.d.ts +14 -0
- package/src/Signal/Group/sender-chain-key.js +26 -0
- package/src/Signal/Group/sender-key-distribution-message.d.ts +17 -0
- package/src/Signal/Group/sender-key-distribution-message.js +63 -0
- package/src/Signal/Group/sender-key-message.d.ts +19 -0
- package/src/Signal/Group/sender-key-message.js +66 -0
- package/src/Signal/Group/sender-key-name.d.ts +12 -0
- package/src/Signal/Group/sender-key-name.js +48 -0
- package/src/Signal/Group/sender-key-record.d.ts +13 -0
- package/src/Signal/Group/sender-key-record.js +41 -0
- package/src/Signal/Group/sender-key-state.d.ts +17 -0
- package/src/Signal/Group/sender-key-state.js +84 -0
- package/src/Signal/Group/sender-message-key.d.ts +12 -0
- package/src/Signal/Group/sender-message-key.js +26 -0
- package/src/Signal/libsignal.d.ts +55 -0
- package/src/Signal/libsignal.js +429 -0
- package/src/Signal/lid-mapping.d.ts +21 -0
- package/src/Signal/lid-mapping.js +275 -0
- package/src/Socket/Client/index.d.ts +3 -0
- package/src/Socket/Client/index.js +3 -0
- package/src/Socket/Client/types.d.ts +6 -0
- package/src/Socket/Client/types.js +10 -0
- package/src/Socket/Client/websocket.d.ts +13 -0
- package/src/Socket/Client/websocket.js +54 -0
- package/src/Socket/business.d.ts +432 -0
- package/src/Socket/business.js +380 -0
- package/src/Socket/chats.d.ts +248 -0
- package/src/Socket/chats.js +1485 -0
- package/src/Socket/communities.d.ts +599 -0
- package/src/Socket/communities.js +422 -0
- package/src/Socket/groups.d.ts +299 -0
- package/src/Socket/groups.js +340 -0
- package/src/Socket/index.d.ts +582 -0
- package/src/Socket/index.js +48 -0
- package/src/Socket/messages-recv.d.ts +371 -0
- package/src/Socket/messages-recv.js +1803 -0
- package/src/Socket/messages-send.d.ts +391 -0
- package/src/Socket/messages-send.js +1575 -0
- package/src/Socket/mex.d.ts +2 -0
- package/src/Socket/mex.js +45 -0
- package/src/Socket/newsletter.d.ts +469 -0
- package/src/Socket/newsletter.js +643 -0
- package/src/Socket/socket.d.ts +78 -0
- package/src/Socket/socket.js +1088 -0
- package/src/Store/index.d.ts +4 -0
- package/src/Store/index.js +4 -0
- package/src/Store/make-in-memory-store.d.ts +63 -0
- package/src/Store/make-in-memory-store.js +398 -0
- package/src/Store/make-ordered-dictionary.d.ts +13 -0
- package/src/Store/make-ordered-dictionary.js +79 -0
- package/src/Store/object-repository.d.ts +11 -0
- package/src/Store/object-repository.js +24 -0
- package/src/Types/Auth.d.ts +2 -0
- package/src/Types/Auth.js +2 -0
- package/src/Types/Bussines.d.ts +2 -0
- package/src/Types/Bussines.js +2 -0
- package/src/Types/Call.d.ts +2 -0
- package/src/Types/Call.js +2 -0
- package/src/Types/Chat.d.ts +3 -0
- package/src/Types/Chat.js +25 -0
- package/src/Types/Contact.d.ts +2 -0
- package/src/Types/Contact.js +2 -0
- package/src/Types/Events.d.ts +2 -0
- package/src/Types/Events.js +1 -0
- package/src/Types/GroupMetadata.d.ts +6 -0
- package/src/Types/GroupMetadata.js +7 -0
- package/src/Types/Label.d.ts +3 -0
- package/src/Types/Label.js +25 -0
- package/src/Types/LabelAssociation.d.ts +3 -0
- package/src/Types/LabelAssociation.js +7 -0
- package/src/Types/Message.d.ts +14 -0
- package/src/Types/Message.js +24 -0
- package/src/Types/Mex.d.ts +3 -0
- package/src/Types/Mex.js +96 -0
- package/src/Types/Product.d.ts +2 -0
- package/src/Types/Product.js +2 -0
- package/src/Types/Signal.d.ts +2 -0
- package/src/Types/Signal.js +1 -0
- package/src/Types/Socket.d.ts +2 -0
- package/src/Types/Socket.js +1 -0
- package/src/Types/State.d.ts +6 -0
- package/src/Types/State.js +55 -0
- package/src/Types/USync.d.ts +2 -0
- package/src/Types/USync.js +1 -0
- package/src/Types/index.d.ts +14 -0
- package/src/Types/index.js +26 -0
- package/src/Utils/auth-store.d.ts +30 -0
- package/src/Utils/auth-store.js +77 -0
- package/src/Utils/auth-utils.d.ts +59 -0
- package/src/Utils/auth-utils.js +274 -0
- package/src/Utils/browser-utils.d.ts +10 -0
- package/src/Utils/browser-utils.js +29 -0
- package/src/Utils/business.d.ts +50 -0
- package/src/Utils/business.js +227 -0
- package/src/Utils/chat-utils.d.ts +409 -0
- package/src/Utils/chat-utils.js +847 -0
- package/src/Utils/companion-reg-client-utils.d.ts +5 -0
- package/src/Utils/companion-reg-client-utils.js +40 -0
- package/src/Utils/crypto.d.ts +40 -0
- package/src/Utils/crypto.js +111 -0
- package/src/Utils/decode-wa-message.d.ts +100 -0
- package/src/Utils/decode-wa-message.js +374 -0
- package/src/Utils/event-buffer.d.ts +23 -0
- package/src/Utils/event-buffer.js +625 -0
- package/src/Utils/generics.d.ts +64 -0
- package/src/Utils/generics.js +376 -0
- package/src/Utils/group-metadata-cache.d.ts +5 -0
- package/src/Utils/group-metadata-cache.js +26 -0
- package/src/Utils/history.d.ts +76 -0
- package/src/Utils/history.js +132 -0
- package/src/Utils/html-app.d.ts +19 -0
- package/src/Utils/html-app.js +97 -0
- package/src/Utils/html-multiplayer.d.ts +10 -0
- package/src/Utils/html-multiplayer.js +95 -0
- package/src/Utils/identity-change-handler.d.ts +14 -0
- package/src/Utils/identity-change-handler.js +49 -0
- package/src/Utils/index.d.ts +45 -0
- package/src/Utils/index.js +45 -0
- package/src/Utils/link-preview-metadata.d.ts +47 -0
- package/src/Utils/link-preview-metadata.js +89 -0
- package/src/Utils/link-preview.d.ts +13 -0
- package/src/Utils/link-preview.js +80 -0
- package/src/Utils/logger.d.ts +3 -0
- package/src/Utils/logger.js +3 -0
- package/src/Utils/lt-hash.d.ts +7 -0
- package/src/Utils/lt-hash.js +4 -0
- package/src/Utils/make-mutex.d.ts +7 -0
- package/src/Utils/make-mutex.js +33 -0
- package/src/Utils/message-retry-manager.d.ts +80 -0
- package/src/Utils/message-retry-manager.js +256 -0
- package/src/Utils/messages-media.d.ts +137 -0
- package/src/Utils/messages-media.js +815 -0
- package/src/Utils/messages.d.ts +63 -0
- package/src/Utils/messages.js +2149 -0
- package/src/Utils/modern-messages.d.ts +150 -0
- package/src/Utils/modern-messages.js +496 -0
- package/src/Utils/native-flow.d.ts +11 -0
- package/src/Utils/native-flow.js +116 -0
- package/src/Utils/newsletter-status.d.ts +158 -0
- package/src/Utils/newsletter-status.js +651 -0
- package/src/Utils/noise-handler.d.ts +21 -0
- package/src/Utils/noise-handler.js +201 -0
- package/src/Utils/offline-node-processor.d.ts +10 -0
- package/src/Utils/offline-node-processor.js +34 -0
- package/src/Utils/optional-media.d.ts +3 -0
- package/src/Utils/optional-media.js +46 -0
- package/src/Utils/payment-messages.d.ts +66 -0
- package/src/Utils/payment-messages.js +161 -0
- package/src/Utils/pre-key-manager.d.ts +26 -0
- package/src/Utils/pre-key-manager.js +96 -0
- package/src/Utils/process-message.d.ts +61 -0
- package/src/Utils/process-message.js +689 -0
- package/src/Utils/reporting-utils.d.ts +13 -0
- package/src/Utils/reporting-utils.js +257 -0
- package/src/Utils/scheduled-message.d.ts +51 -0
- package/src/Utils/scheduled-message.js +113 -0
- package/src/Utils/sender-key-memory.d.ts +20 -0
- package/src/Utils/sender-key-memory.js +52 -0
- package/src/Utils/signal.d.ts +83 -0
- package/src/Utils/signal.js +194 -0
- package/src/Utils/stanza-ack.d.ts +17 -0
- package/src/Utils/stanza-ack.js +31 -0
- package/src/Utils/status-stickers.d.ts +127 -0
- package/src/Utils/status-stickers.js +261 -0
- package/src/Utils/sync-action-utils.d.ts +3 -0
- package/src/Utils/sync-action-utils.js +45 -0
- package/src/Utils/tc-token-utils.d.ts +30 -0
- package/src/Utils/tc-token-utils.js +156 -0
- package/src/Utils/ttl-cache.d.ts +29 -0
- package/src/Utils/ttl-cache.js +121 -0
- package/src/Utils/use-mongo-auth-state.d.ts +22 -0
- package/src/Utils/use-mongo-auth-state.js +68 -0
- package/src/Utils/use-multi-file-auth-state.d.ts +12 -0
- package/src/Utils/use-multi-file-auth-state.js +107 -0
- package/src/Utils/use-mysql-auth-state.d.ts +20 -0
- package/src/Utils/use-mysql-auth-state.js +80 -0
- package/src/Utils/use-postgres-auth-state.d.ts +20 -0
- package/src/Utils/use-postgres-auth-state.js +80 -0
- package/src/Utils/use-redis-auth-state.d.ts +20 -0
- package/src/Utils/use-redis-auth-state.js +90 -0
- package/src/Utils/use-single-file-auth-state.d.ts +11 -0
- package/src/Utils/use-single-file-auth-state.js +109 -0
- package/src/Utils/use-sqlite-auth-state.d.ts +13 -0
- package/src/Utils/use-sqlite-auth-state.js +121 -0
- package/src/Utils/useNekoDBAuth.d.ts +19 -0
- package/src/Utils/useNekoDBAuth.js +111 -0
- package/src/Utils/username.d.ts +12 -0
- package/src/Utils/username.js +59 -0
- package/src/Utils/validate-connection.d.ts +44 -0
- package/src/Utils/validate-connection.js +208 -0
- package/src/Utils/voice-recognition.d.ts +53 -0
- package/src/Utils/voice-recognition.js +219 -0
- package/src/Voip/audio-feeder.d.ts +25 -0
- package/src/Voip/audio-feeder.js +269 -0
- package/src/Voip/index.d.ts +89 -0
- package/src/Voip/index.js +937 -0
- package/src/Voip/relay-transport.d.ts +6 -0
- package/src/Voip/relay-transport.js +825 -0
- package/src/Voip/signaling.d.ts +12 -0
- package/src/Voip/signaling.js +1100 -0
- package/src/Voip/types.d.ts +3 -0
- package/src/Voip/types.js +13 -0
- package/src/Voip/video-feeder.d.ts +25 -0
- package/src/Voip/video-feeder.js +269 -0
- package/src/Voip/wasm-engine.d.ts +27 -0
- package/src/Voip/wasm-engine.js +1308 -0
- package/src/Voip/worker-bootstrap.js +1024 -0
- package/src/WABinary/constants.d.ts +23 -0
- package/src/WABinary/constants.js +1301 -0
- package/src/WABinary/decode.d.ts +10 -0
- package/src/WABinary/decode.js +262 -0
- package/src/WABinary/encode.d.ts +3 -0
- package/src/WABinary/encode.js +220 -0
- package/src/WABinary/generic-utils.d.ts +77 -0
- package/src/WABinary/generic-utils.js +214 -0
- package/src/WABinary/index.d.ts +6 -0
- package/src/WABinary/index.js +6 -0
- package/src/WABinary/jid-utils.d.ts +31 -0
- package/src/WABinary/jid-utils.js +98 -0
- package/src/WABinary/types.d.ts +2 -0
- package/src/WABinary/types.js +1 -0
- package/src/WAM/BinaryInfo.d.ts +8 -0
- package/src/WAM/BinaryInfo.js +10 -0
- package/src/WAM/constants.d.ts +34927 -0
- package/src/WAM/constants.js +22853 -0
- package/src/WAM/encode.d.ts +2 -0
- package/src/WAM/encode.js +149 -0
- package/src/WAM/index.d.ts +4 -0
- package/src/WAM/index.js +4 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +51 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +52 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +27 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +38 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.js +50 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.js +29 -0
- package/src/WAUSync/Protocols/index.d.ts +6 -0
- package/src/WAUSync/Protocols/index.js +6 -0
- package/src/WAUSync/USyncQuery.d.ts +21 -0
- package/src/WAUSync/USyncQuery.js +94 -0
- package/src/WAUSync/USyncUser.d.ts +17 -0
- package/src/WAUSync/USyncUser.js +31 -0
- package/src/WAUSync/index.d.ts +4 -0
- package/src/WAUSync/index.js +4 -0
- package/src/assets/wasm/README.md +76 -0
- package/src/assets/wasm/loader.js +5 -0
- package/src/assets/wasm/whatsapp.wasm +0 -0
- package/src/assets/wasm/worker-modules.js +273 -0
- package/src/index.d.ts +49 -0
- package/src/index.js +54 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class CiphertextMessage {
|
|
3
|
+
UNSUPPORTED_VERSION: number;
|
|
4
|
+
CURRENT_VERSION: number;
|
|
5
|
+
WHISPER_TYPE: number;
|
|
6
|
+
PREKEY_TYPE: number;
|
|
7
|
+
SENDERKEY_TYPE: number;
|
|
8
|
+
SENDERKEY_DISTRIBUTION_TYPE: number;
|
|
9
|
+
ENCRYPTED_MESSAGE_OVERHEAD: number;
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class CiphertextMessage {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.UNSUPPORTED_VERSION = 1;
|
|
5
|
+
this.CURRENT_VERSION = 3;
|
|
6
|
+
this.WHISPER_TYPE = 2;
|
|
7
|
+
this.PREKEY_TYPE = 3;
|
|
8
|
+
this.SENDERKEY_TYPE = 4;
|
|
9
|
+
this.SENDERKEY_DISTRIBUTION_TYPE = 5;
|
|
10
|
+
this.ENCRYPTED_MESSAGE_OVERHEAD = 53;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class GroupSessionBuilder {
|
|
3
|
+
constructor(senderKeyStore: any);
|
|
4
|
+
senderKeyStore: any;
|
|
5
|
+
process(senderKeyName: any, senderKeyDistributionMessage: any): Promise<void>;
|
|
6
|
+
create(senderKeyName: any): Promise<SenderKeyDistributionMessage>;
|
|
7
|
+
}
|
|
8
|
+
import { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import * as keyhelper from './keyhelper.js';
|
|
3
|
+
import { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
4
|
+
export class GroupSessionBuilder {
|
|
5
|
+
constructor(senderKeyStore) {
|
|
6
|
+
this.senderKeyStore = senderKeyStore;
|
|
7
|
+
}
|
|
8
|
+
async process(senderKeyName, senderKeyDistributionMessage) {
|
|
9
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
|
|
10
|
+
senderKeyRecord.addSenderKeyState(senderKeyDistributionMessage.getId(), senderKeyDistributionMessage.getIteration(), senderKeyDistributionMessage.getChainKey(), senderKeyDistributionMessage.getSignatureKey());
|
|
11
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
|
|
12
|
+
}
|
|
13
|
+
async create(senderKeyName) {
|
|
14
|
+
const senderKeyRecord = await this.senderKeyStore.loadSenderKey(senderKeyName);
|
|
15
|
+
if (senderKeyRecord.isEmpty()) {
|
|
16
|
+
const keyId = keyhelper.generateSenderKeyId();
|
|
17
|
+
const senderKey = keyhelper.generateSenderKey();
|
|
18
|
+
const signingKey = keyhelper.generateSenderSigningKey();
|
|
19
|
+
senderKeyRecord.setSenderKeyState(keyId, 0, senderKey, signingKey);
|
|
20
|
+
await this.senderKeyStore.storeSenderKey(senderKeyName, senderKeyRecord);
|
|
21
|
+
}
|
|
22
|
+
const state = senderKeyRecord.getSenderKeyState();
|
|
23
|
+
if (!state) {
|
|
24
|
+
throw new Error('No session state available');
|
|
25
|
+
}
|
|
26
|
+
return new SenderKeyDistributionMessage(state.getKeyId(), state.getSenderChainKey().getIteration(), state.getSenderChainKey().getSeed(), state.getSigningKeyPublic());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class GroupCipher {
|
|
3
|
+
constructor(senderKeyStore: any, senderKeyName: any);
|
|
4
|
+
senderKeyStore: any;
|
|
5
|
+
senderKeyName: any;
|
|
6
|
+
encrypt(paddedPlaintext: any): Promise<any>;
|
|
7
|
+
decrypt(senderKeyMessageBytes: any): Promise<any>;
|
|
8
|
+
getSenderKey(senderKeyState: any, iteration: any): any;
|
|
9
|
+
getPlainText(iv: any, key: any, ciphertext: any): Promise<any>;
|
|
10
|
+
getCipherText(iv: any, key: any, plaintext: any): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import { decrypt, encrypt } from 'libsignal/src/crypto.js';
|
|
3
|
+
import { SenderKeyMessage } from './sender-key-message.js';
|
|
4
|
+
export class GroupCipher {
|
|
5
|
+
constructor(senderKeyStore, senderKeyName) {
|
|
6
|
+
this.senderKeyStore = senderKeyStore;
|
|
7
|
+
this.senderKeyName = senderKeyName;
|
|
8
|
+
}
|
|
9
|
+
async encrypt(paddedPlaintext) {
|
|
10
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
11
|
+
if (!record) {
|
|
12
|
+
throw new Error('No SenderKeyRecord found for encryption');
|
|
13
|
+
}
|
|
14
|
+
const senderKeyState = record.getSenderKeyState();
|
|
15
|
+
if (!senderKeyState) {
|
|
16
|
+
throw new Error('No session to encrypt message');
|
|
17
|
+
}
|
|
18
|
+
const iteration = senderKeyState.getSenderChainKey().getIteration();
|
|
19
|
+
const senderKey = this.getSenderKey(senderKeyState, iteration === 0 ? 0 : iteration + 1);
|
|
20
|
+
const ciphertext = await this.getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext);
|
|
21
|
+
const senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate());
|
|
22
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
23
|
+
return senderKeyMessage.serialize();
|
|
24
|
+
}
|
|
25
|
+
async decrypt(senderKeyMessageBytes) {
|
|
26
|
+
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName);
|
|
27
|
+
if (!record) {
|
|
28
|
+
throw new Error('No SenderKeyRecord found for decryption');
|
|
29
|
+
}
|
|
30
|
+
const senderKeyMessage = new SenderKeyMessage(null, null, null, null, senderKeyMessageBytes);
|
|
31
|
+
const senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId());
|
|
32
|
+
if (!senderKeyState) {
|
|
33
|
+
throw new Error('No session found to decrypt message');
|
|
34
|
+
}
|
|
35
|
+
senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic());
|
|
36
|
+
const senderKey = this.getSenderKey(senderKeyState, senderKeyMessage.getIteration());
|
|
37
|
+
const plaintext = await this.getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
|
|
38
|
+
await this.senderKeyStore.storeSenderKey(this.senderKeyName, record);
|
|
39
|
+
return plaintext;
|
|
40
|
+
}
|
|
41
|
+
getSenderKey(senderKeyState, iteration) {
|
|
42
|
+
let senderChainKey = senderKeyState.getSenderChainKey();
|
|
43
|
+
if (senderChainKey.getIteration() > iteration) {
|
|
44
|
+
if (senderKeyState.hasSenderMessageKey(iteration)) {
|
|
45
|
+
const messageKey = senderKeyState.removeSenderMessageKey(iteration);
|
|
46
|
+
if (!messageKey) {
|
|
47
|
+
throw new Error('No sender message key found for iteration');
|
|
48
|
+
}
|
|
49
|
+
return messageKey;
|
|
50
|
+
}
|
|
51
|
+
throw new Error(`Received message with old counter: ${senderChainKey.getIteration()}, ${iteration}`);
|
|
52
|
+
}
|
|
53
|
+
if (iteration - senderChainKey.getIteration() > 2000) {
|
|
54
|
+
throw new Error('Over 2000 messages into the future!');
|
|
55
|
+
}
|
|
56
|
+
while (senderChainKey.getIteration() < iteration) {
|
|
57
|
+
senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey());
|
|
58
|
+
senderChainKey = senderChainKey.getNext();
|
|
59
|
+
}
|
|
60
|
+
senderKeyState.setSenderChainKey(senderChainKey.getNext());
|
|
61
|
+
return senderChainKey.getSenderMessageKey();
|
|
62
|
+
}
|
|
63
|
+
async getPlainText(iv, key, ciphertext) {
|
|
64
|
+
try {
|
|
65
|
+
return decrypt(key, ciphertext, iv);
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
throw new Error('InvalidMessageException');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async getCipherText(iv, key, plaintext) {
|
|
72
|
+
try {
|
|
73
|
+
return encrypt(key, plaintext, iv);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
throw new Error('InvalidMessageException');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export { GroupSessionBuilder } from "./group-session-builder.js";
|
|
3
|
+
export { SenderKeyDistributionMessage } from "./sender-key-distribution-message.js";
|
|
4
|
+
export { SenderKeyRecord } from "./sender-key-record.js";
|
|
5
|
+
export { SenderKeyName } from "./sender-key-name.js";
|
|
6
|
+
export { GroupCipher } from "./group_cipher.js";
|
|
7
|
+
export { SenderKeyState } from "./sender-key-state.js";
|
|
8
|
+
export { SenderKeyMessage } from "./sender-key-message.js";
|
|
9
|
+
export { SenderMessageKey } from "./sender-message-key.js";
|
|
10
|
+
export { SenderChainKey } from "./sender-chain-key.js";
|
|
11
|
+
export { CiphertextMessage } from "./ciphertext-message.js";
|
|
12
|
+
export * as keyhelper from "./keyhelper.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export { GroupSessionBuilder } from './group-session-builder.js';
|
|
3
|
+
export { SenderKeyDistributionMessage } from './sender-key-distribution-message.js';
|
|
4
|
+
export { SenderKeyRecord } from './sender-key-record.js';
|
|
5
|
+
export { SenderKeyName } from './sender-key-name.js';
|
|
6
|
+
export { GroupCipher } from './group_cipher.js';
|
|
7
|
+
export { SenderKeyState } from './sender-key-state.js';
|
|
8
|
+
export { SenderKeyMessage } from './sender-key-message.js';
|
|
9
|
+
export { SenderMessageKey } from './sender-message-key.js';
|
|
10
|
+
export { SenderChainKey } from './sender-chain-key.js';
|
|
11
|
+
export { CiphertextMessage } from './ciphertext-message.js';
|
|
12
|
+
export * as keyhelper from './keyhelper.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export function generateSenderKey(): any;
|
|
3
|
+
export function generateSenderKeyId(): any;
|
|
4
|
+
export function generateSenderSigningKey(key: any): {
|
|
5
|
+
public: any;
|
|
6
|
+
private: any;
|
|
7
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import * as nodeCrypto from 'crypto';
|
|
3
|
+
import { generateKeyPair } from 'libsignal/src/curve.js';
|
|
4
|
+
export function generateSenderKey() {
|
|
5
|
+
return nodeCrypto.randomBytes(32);
|
|
6
|
+
}
|
|
7
|
+
export function generateSenderKeyId() {
|
|
8
|
+
return nodeCrypto.randomInt(2147483647);
|
|
9
|
+
}
|
|
10
|
+
export function generateSenderSigningKey(key) {
|
|
11
|
+
if (!key) {
|
|
12
|
+
key = generateKeyPair();
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
public: Buffer.from(key.pubKey),
|
|
16
|
+
private: Buffer.from(key.privKey)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderChainKey {
|
|
3
|
+
constructor(iteration: any, chainKey: any);
|
|
4
|
+
MESSAGE_KEY_SEED: any;
|
|
5
|
+
CHAIN_KEY_SEED: any;
|
|
6
|
+
iteration: any;
|
|
7
|
+
chainKey: any;
|
|
8
|
+
getIteration(): any;
|
|
9
|
+
getSenderMessageKey(): SenderMessageKey;
|
|
10
|
+
getNext(): SenderChainKey;
|
|
11
|
+
getSeed(): any;
|
|
12
|
+
getDerivative(seed: any, key: any): any;
|
|
13
|
+
}
|
|
14
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import { calculateMAC } from 'libsignal/src/crypto.js';
|
|
3
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
4
|
+
export class SenderChainKey {
|
|
5
|
+
constructor(iteration, chainKey) {
|
|
6
|
+
this.MESSAGE_KEY_SEED = Buffer.from([0x01]);
|
|
7
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02]);
|
|
8
|
+
this.iteration = iteration;
|
|
9
|
+
this.chainKey = Buffer.from(chainKey);
|
|
10
|
+
}
|
|
11
|
+
getIteration() {
|
|
12
|
+
return this.iteration;
|
|
13
|
+
}
|
|
14
|
+
getSenderMessageKey() {
|
|
15
|
+
return new SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey));
|
|
16
|
+
}
|
|
17
|
+
getNext() {
|
|
18
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey));
|
|
19
|
+
}
|
|
20
|
+
getSeed() {
|
|
21
|
+
return this.chainKey;
|
|
22
|
+
}
|
|
23
|
+
getDerivative(seed, key) {
|
|
24
|
+
return calculateMAC(key, seed);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
3
|
+
constructor(id: any, iteration: any, chainKey: any, signatureKey: any, serialized: any);
|
|
4
|
+
serialized: any;
|
|
5
|
+
id: any;
|
|
6
|
+
iteration: any;
|
|
7
|
+
chainKey: any;
|
|
8
|
+
signatureKey: any;
|
|
9
|
+
intsToByteHighAndLow(highValue: any, lowValue: any): number;
|
|
10
|
+
serialize(): any;
|
|
11
|
+
getType(): number;
|
|
12
|
+
getIteration(): any;
|
|
13
|
+
getChainKey(): any;
|
|
14
|
+
getSignatureKey(): any;
|
|
15
|
+
getId(): any;
|
|
16
|
+
}
|
|
17
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import { proto } from '../../../WAProto/index.js';
|
|
3
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
4
|
+
export class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
5
|
+
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
6
|
+
super();
|
|
7
|
+
if (serialized) {
|
|
8
|
+
try {
|
|
9
|
+
const message = serialized.slice(1);
|
|
10
|
+
const distributionMessage = 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
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
throw new Error(String(e));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
|
|
29
|
+
this.id = id;
|
|
30
|
+
this.iteration = iteration;
|
|
31
|
+
this.chainKey = chainKey;
|
|
32
|
+
this.signatureKey = signatureKey;
|
|
33
|
+
const message = proto.SenderKeyDistributionMessage.encode(proto.SenderKeyDistributionMessage.create({
|
|
34
|
+
id,
|
|
35
|
+
iteration,
|
|
36
|
+
chainKey,
|
|
37
|
+
signingKey: this.signatureKey
|
|
38
|
+
})).finish();
|
|
39
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
intsToByteHighAndLow(highValue, lowValue) {
|
|
43
|
+
return (((highValue << 4) | lowValue) & 0xff) % 256;
|
|
44
|
+
}
|
|
45
|
+
serialize() {
|
|
46
|
+
return this.serialized;
|
|
47
|
+
}
|
|
48
|
+
getType() {
|
|
49
|
+
return this.SENDERKEY_DISTRIBUTION_TYPE;
|
|
50
|
+
}
|
|
51
|
+
getIteration() {
|
|
52
|
+
return this.iteration;
|
|
53
|
+
}
|
|
54
|
+
getChainKey() {
|
|
55
|
+
return this.chainKey;
|
|
56
|
+
}
|
|
57
|
+
getSignatureKey() {
|
|
58
|
+
return this.signatureKey;
|
|
59
|
+
}
|
|
60
|
+
getId() {
|
|
61
|
+
return this.id;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
3
|
+
constructor(keyId: any, iteration: any, ciphertext: any, signatureKey: any, serialized: any);
|
|
4
|
+
SIGNATURE_LENGTH: number;
|
|
5
|
+
serialized: any;
|
|
6
|
+
messageVersion: number;
|
|
7
|
+
keyId: any;
|
|
8
|
+
iteration: any;
|
|
9
|
+
ciphertext: any;
|
|
10
|
+
signature: any;
|
|
11
|
+
getKeyId(): any;
|
|
12
|
+
getIteration(): any;
|
|
13
|
+
getCipherText(): any;
|
|
14
|
+
verifySignature(signatureKey: any): void;
|
|
15
|
+
getSignature(signatureKey: any, serialized: any): any;
|
|
16
|
+
serialize(): any;
|
|
17
|
+
getType(): number;
|
|
18
|
+
}
|
|
19
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import { calculateSignature, verifySignature } from 'libsignal/src/curve.js';
|
|
3
|
+
import { proto } from '../../../WAProto/index.js';
|
|
4
|
+
import { CiphertextMessage } from './ciphertext-message.js';
|
|
5
|
+
export class SenderKeyMessage extends CiphertextMessage {
|
|
6
|
+
constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
|
|
7
|
+
super();
|
|
8
|
+
this.SIGNATURE_LENGTH = 64;
|
|
9
|
+
if (serialized) {
|
|
10
|
+
const version = serialized[0];
|
|
11
|
+
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
|
|
12
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
13
|
+
const senderKeyMessage = proto.SenderKeyMessage.decode(message).toJSON();
|
|
14
|
+
this.serialized = serialized;
|
|
15
|
+
this.messageVersion = (version & 0xff) >> 4;
|
|
16
|
+
this.keyId = senderKeyMessage.id;
|
|
17
|
+
this.iteration = senderKeyMessage.iteration;
|
|
18
|
+
this.ciphertext =
|
|
19
|
+
typeof senderKeyMessage.ciphertext === 'string'
|
|
20
|
+
? Buffer.from(senderKeyMessage.ciphertext, 'base64')
|
|
21
|
+
: senderKeyMessage.ciphertext;
|
|
22
|
+
this.signature = signature;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
|
|
26
|
+
const ciphertextBuffer = Buffer.from(ciphertext);
|
|
27
|
+
const message = proto.SenderKeyMessage.encode(proto.SenderKeyMessage.create({
|
|
28
|
+
id: keyId,
|
|
29
|
+
iteration: iteration,
|
|
30
|
+
ciphertext: ciphertextBuffer
|
|
31
|
+
})).finish();
|
|
32
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]));
|
|
33
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
|
|
34
|
+
this.messageVersion = this.CURRENT_VERSION;
|
|
35
|
+
this.keyId = keyId;
|
|
36
|
+
this.iteration = iteration;
|
|
37
|
+
this.ciphertext = ciphertextBuffer;
|
|
38
|
+
this.signature = signature;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
getKeyId() {
|
|
42
|
+
return this.keyId;
|
|
43
|
+
}
|
|
44
|
+
getIteration() {
|
|
45
|
+
return this.iteration;
|
|
46
|
+
}
|
|
47
|
+
getCipherText() {
|
|
48
|
+
return this.ciphertext;
|
|
49
|
+
}
|
|
50
|
+
verifySignature(signatureKey) {
|
|
51
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH);
|
|
52
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
|
|
53
|
+
const res = verifySignature(signatureKey, part1, part2);
|
|
54
|
+
if (!res)
|
|
55
|
+
throw new Error('Invalid signature!');
|
|
56
|
+
}
|
|
57
|
+
getSignature(signatureKey, serialized) {
|
|
58
|
+
return Buffer.from(calculateSignature(signatureKey, serialized));
|
|
59
|
+
}
|
|
60
|
+
serialize() {
|
|
61
|
+
return this.serialized;
|
|
62
|
+
}
|
|
63
|
+
getType() {
|
|
64
|
+
return 4;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderKeyName {
|
|
3
|
+
constructor(groupId: any, sender: any);
|
|
4
|
+
groupId: any;
|
|
5
|
+
sender: any;
|
|
6
|
+
getGroupId(): any;
|
|
7
|
+
getSender(): any;
|
|
8
|
+
serialize(): string;
|
|
9
|
+
toString(): string;
|
|
10
|
+
equals(other: any): boolean;
|
|
11
|
+
hashCode(): number;
|
|
12
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
function isNull(str) {
|
|
3
|
+
return str === null || str === '';
|
|
4
|
+
}
|
|
5
|
+
function intValue(num) {
|
|
6
|
+
const MAX_VALUE = 0x7fffffff;
|
|
7
|
+
const MIN_VALUE = -0x80000000;
|
|
8
|
+
if (num > MAX_VALUE || num < MIN_VALUE) {
|
|
9
|
+
return num & 0xffffffff;
|
|
10
|
+
}
|
|
11
|
+
return num;
|
|
12
|
+
}
|
|
13
|
+
function hashCode(strKey) {
|
|
14
|
+
let hash = 0;
|
|
15
|
+
if (!isNull(strKey)) {
|
|
16
|
+
for (let i = 0; i < strKey.length; i++) {
|
|
17
|
+
hash = hash * 31 + strKey.charCodeAt(i);
|
|
18
|
+
hash = intValue(hash);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return hash;
|
|
22
|
+
}
|
|
23
|
+
export class SenderKeyName {
|
|
24
|
+
constructor(groupId, sender) {
|
|
25
|
+
this.groupId = groupId;
|
|
26
|
+
this.sender = sender;
|
|
27
|
+
}
|
|
28
|
+
getGroupId() {
|
|
29
|
+
return this.groupId;
|
|
30
|
+
}
|
|
31
|
+
getSender() {
|
|
32
|
+
return this.sender;
|
|
33
|
+
}
|
|
34
|
+
serialize() {
|
|
35
|
+
return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`;
|
|
36
|
+
}
|
|
37
|
+
toString() {
|
|
38
|
+
return this.serialize();
|
|
39
|
+
}
|
|
40
|
+
equals(other) {
|
|
41
|
+
if (other === null)
|
|
42
|
+
return false;
|
|
43
|
+
return this.groupId === other.groupId && this.sender.toString() === other.sender.toString();
|
|
44
|
+
}
|
|
45
|
+
hashCode() {
|
|
46
|
+
return hashCode(this.groupId) ^ hashCode(this.sender.toString());
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderKeyRecord {
|
|
3
|
+
static deserialize(data: any): SenderKeyRecord;
|
|
4
|
+
constructor(serialized: any);
|
|
5
|
+
MAX_STATES: number;
|
|
6
|
+
senderKeyStates: SenderKeyState[];
|
|
7
|
+
isEmpty(): boolean;
|
|
8
|
+
getSenderKeyState(keyId: any): SenderKeyState | undefined;
|
|
9
|
+
addSenderKeyState(id: any, iteration: any, chainKey: any, signatureKey: any): void;
|
|
10
|
+
setSenderKeyState(id: any, iteration: any, chainKey: any, keyPair: any): void;
|
|
11
|
+
serialize(): any[];
|
|
12
|
+
}
|
|
13
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import { BufferJSON } from '../../Utils/generics.js';
|
|
3
|
+
import { SenderKeyState } from './sender-key-state.js';
|
|
4
|
+
export class SenderKeyRecord {
|
|
5
|
+
constructor(serialized) {
|
|
6
|
+
this.MAX_STATES = 5;
|
|
7
|
+
this.senderKeyStates = [];
|
|
8
|
+
if (serialized) {
|
|
9
|
+
for (const structure of serialized) {
|
|
10
|
+
this.senderKeyStates.push(new SenderKeyState(null, null, null, null, null, null, structure));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
isEmpty() {
|
|
15
|
+
return this.senderKeyStates.length === 0;
|
|
16
|
+
}
|
|
17
|
+
getSenderKeyState(keyId) {
|
|
18
|
+
if (keyId === undefined && this.senderKeyStates.length) {
|
|
19
|
+
return this.senderKeyStates[this.senderKeyStates.length - 1];
|
|
20
|
+
}
|
|
21
|
+
return this.senderKeyStates.find(state => state.getKeyId() === keyId);
|
|
22
|
+
}
|
|
23
|
+
addSenderKeyState(id, iteration, chainKey, signatureKey) {
|
|
24
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, null, signatureKey));
|
|
25
|
+
if (this.senderKeyStates.length > this.MAX_STATES) {
|
|
26
|
+
this.senderKeyStates.shift();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
setSenderKeyState(id, iteration, chainKey, keyPair) {
|
|
30
|
+
this.senderKeyStates.length = 0;
|
|
31
|
+
this.senderKeyStates.push(new SenderKeyState(id, iteration, chainKey, keyPair));
|
|
32
|
+
}
|
|
33
|
+
serialize() {
|
|
34
|
+
return this.senderKeyStates.map(state => state.getStructure());
|
|
35
|
+
}
|
|
36
|
+
static deserialize(data) {
|
|
37
|
+
const str = Buffer.from(data).toString('utf-8');
|
|
38
|
+
const parsed = JSON.parse(str, BufferJSON.reviver);
|
|
39
|
+
return new SenderKeyRecord(parsed);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderKeyState {
|
|
3
|
+
constructor(id: any, iteration: any, chainKey: any, signatureKeyPair: any, signatureKeyPublic: any, signatureKeyPrivate: any, senderKeyStateStructure: any);
|
|
4
|
+
MAX_MESSAGE_KEYS: number;
|
|
5
|
+
senderKeyStateStructure: any;
|
|
6
|
+
getKeyId(): any;
|
|
7
|
+
getSenderChainKey(): SenderChainKey;
|
|
8
|
+
setSenderChainKey(chainKey: any): void;
|
|
9
|
+
getSigningKeyPublic(): any;
|
|
10
|
+
getSigningKeyPrivate(): any;
|
|
11
|
+
hasSenderMessageKey(iteration: any): any;
|
|
12
|
+
addSenderMessageKey(senderMessageKey: any): void;
|
|
13
|
+
removeSenderMessageKey(iteration: any): SenderMessageKey | null;
|
|
14
|
+
getStructure(): any;
|
|
15
|
+
}
|
|
16
|
+
import { SenderChainKey } from './sender-chain-key.js';
|
|
17
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
import { SenderChainKey } from './sender-chain-key.js';
|
|
3
|
+
import { SenderMessageKey } from './sender-message-key.js';
|
|
4
|
+
export class SenderKeyState {
|
|
5
|
+
constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
|
|
6
|
+
this.MAX_MESSAGE_KEYS = 2000;
|
|
7
|
+
if (senderKeyStateStructure) {
|
|
8
|
+
this.senderKeyStateStructure = {
|
|
9
|
+
...senderKeyStateStructure,
|
|
10
|
+
senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
|
|
11
|
+
? senderKeyStateStructure.senderMessageKeys
|
|
12
|
+
: []
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
if (signatureKeyPair) {
|
|
17
|
+
signatureKeyPublic = signatureKeyPair.public;
|
|
18
|
+
signatureKeyPrivate = signatureKeyPair.private;
|
|
19
|
+
}
|
|
20
|
+
this.senderKeyStateStructure = {
|
|
21
|
+
senderKeyId: id || 0,
|
|
22
|
+
senderChainKey: {
|
|
23
|
+
iteration: iteration || 0,
|
|
24
|
+
seed: Buffer.from(chainKey || [])
|
|
25
|
+
},
|
|
26
|
+
senderSigningKey: {
|
|
27
|
+
public: Buffer.from(signatureKeyPublic || []),
|
|
28
|
+
private: Buffer.from(signatureKeyPrivate || [])
|
|
29
|
+
},
|
|
30
|
+
senderMessageKeys: []
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
getKeyId() {
|
|
35
|
+
return this.senderKeyStateStructure.senderKeyId;
|
|
36
|
+
}
|
|
37
|
+
getSenderChainKey() {
|
|
38
|
+
return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
|
|
39
|
+
}
|
|
40
|
+
setSenderChainKey(chainKey) {
|
|
41
|
+
this.senderKeyStateStructure.senderChainKey = {
|
|
42
|
+
iteration: chainKey.getIteration(),
|
|
43
|
+
seed: chainKey.getSeed()
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
getSigningKeyPublic() {
|
|
47
|
+
const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public);
|
|
48
|
+
if (publicKey.length === 32) {
|
|
49
|
+
const fixed = Buffer.alloc(33);
|
|
50
|
+
fixed[0] = 0x05;
|
|
51
|
+
publicKey.copy(fixed, 1);
|
|
52
|
+
return fixed;
|
|
53
|
+
}
|
|
54
|
+
return publicKey;
|
|
55
|
+
}
|
|
56
|
+
getSigningKeyPrivate() {
|
|
57
|
+
const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
|
|
58
|
+
return Buffer.from(privateKey || []);
|
|
59
|
+
}
|
|
60
|
+
hasSenderMessageKey(iteration) {
|
|
61
|
+
return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
|
|
62
|
+
}
|
|
63
|
+
addSenderMessageKey(senderMessageKey) {
|
|
64
|
+
this.senderKeyStateStructure.senderMessageKeys.push({
|
|
65
|
+
iteration: senderMessageKey.getIteration(),
|
|
66
|
+
seed: senderMessageKey.getSeed()
|
|
67
|
+
});
|
|
68
|
+
if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
|
|
69
|
+
this.senderKeyStateStructure.senderMessageKeys.shift();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
removeSenderMessageKey(iteration) {
|
|
73
|
+
const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
|
|
74
|
+
if (index !== -1) {
|
|
75
|
+
const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
|
|
76
|
+
this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
|
|
77
|
+
return new SenderMessageKey(messageKey.iteration, messageKey.seed);
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
getStructure() {
|
|
82
|
+
return this.senderKeyStateStructure;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
|
|
2
|
+
export class SenderMessageKey {
|
|
3
|
+
constructor(iteration: any, seed: any);
|
|
4
|
+
iv: any;
|
|
5
|
+
cipherKey: any;
|
|
6
|
+
iteration: any;
|
|
7
|
+
seed: any;
|
|
8
|
+
getIteration(): any;
|
|
9
|
+
getIv(): any;
|
|
10
|
+
getCipherKey(): any;
|
|
11
|
+
getSeed(): any;
|
|
12
|
+
}
|