@dhilzzkz/baileys 0.0.0-stage → 0.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/LICENSE +23 -0
- package/NOTICE.md +37 -0
- package/README.md +2083 -2
- package/WAProto/index.d.ts +22220 -0
- package/WAProto/index.js +5 -0
- package/lib/Addon/http-error.d.ts +18 -0
- package/lib/Addon/http-error.js +40 -0
- package/lib/Addon/index.d.ts +3 -0
- package/lib/Addon/index.js +4 -0
- package/lib/Addon/keyed-db.d.ts +14 -0
- package/lib/Addon/keyed-db.js +241 -0
- package/lib/Addon/link-preview.d.ts +15 -0
- package/lib/Addon/link-preview.js +79 -0
- package/lib/Defaults/index.d.ts +188 -0
- package/lib/Defaults/index.js +185 -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-cipher.d.ts +10 -0
- package/lib/Signal/Group/group-cipher.js +81 -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/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/Protocol/crypto.d.ts +6 -0
- package/lib/Signal/Protocol/crypto.js +61 -0
- package/lib/Signal/Protocol/curve.d.ts +8 -0
- package/lib/Signal/Protocol/curve.js +61 -0
- package/lib/Signal/Protocol/index.d.ts +16 -0
- package/lib/Signal/Protocol/index.js +9 -0
- package/lib/Signal/Protocol/internals.d.ts +57 -0
- package/lib/Signal/Protocol/internals.js +155 -0
- package/lib/Signal/Protocol/session-builder.d.ts +40 -0
- package/lib/Signal/Protocol/session-builder.js +143 -0
- package/lib/Signal/Protocol/session-cipher.d.ts +26 -0
- package/lib/Signal/Protocol/session-cipher.js +301 -0
- package/lib/Signal/Protocol/session-record.d.ts +55 -0
- package/lib/Signal/Protocol/session-record.js +272 -0
- package/lib/Signal/libsignal.d.ts +54 -0
- package/lib/Signal/libsignal.js +426 -0
- package/lib/Signal/lid-mapping.d.ts +20 -0
- package/lib/Signal/lid-mapping.js +274 -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 +119 -0
- package/lib/Socket/business.d.ts +387 -0
- package/lib/Socket/business.js +380 -0
- package/lib/Socket/chats.d.ts +144 -0
- package/lib/Socket/chats.js +1216 -0
- package/lib/Socket/communities.d.ts +554 -0
- package/lib/Socket/communities.js +431 -0
- package/lib/Socket/groups.d.ts +286 -0
- package/lib/Socket/groups.js +347 -0
- package/lib/Socket/index.d.ts +522 -0
- package/lib/Socket/index.js +11 -0
- package/lib/Socket/messages-recv.d.ts +326 -0
- package/lib/Socket/messages-recv.js +1822 -0
- package/lib/Socket/messages-send.d.ts +321 -0
- package/lib/Socket/messages-send.js +1378 -0
- package/lib/Socket/mex.d.ts +1 -0
- package/lib/Socket/mex.js +41 -0
- package/lib/Socket/newsletter.d.ts +283 -0
- package/lib/Socket/newsletter.js +225 -0
- package/lib/Socket/socket.d.ts +61 -0
- package/lib/Socket/socket.js +992 -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 +63 -0
- package/lib/Store/make-in-memory-store.js +418 -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 +1 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Business.d.ts +1 -0
- package/lib/Types/Business.js +1 -0
- package/lib/Types/Call.d.ts +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.d.ts +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.d.ts +1 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.d.ts +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.d.ts +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.d.ts +2 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.d.ts +2 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.d.ts +11 -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 +1 -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 +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.d.ts +1 -0
- package/lib/Types/Socket.js +1 -0
- package/lib/Types/State.d.ts +5 -0
- package/lib/Types/State.js +54 -0
- package/lib/Types/USync.d.ts +1 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.d.ts +13 -0
- package/lib/Types/index.js +25 -0
- package/lib/Utils/Auth/auth-utils.d.ts +58 -0
- package/lib/Utils/Auth/auth-utils.js +316 -0
- package/lib/Utils/Auth/companion-reg-client-utils.d.ts +4 -0
- package/lib/Utils/Auth/companion-reg-client-utils.js +39 -0
- package/lib/Utils/Auth/pre-key-manager.d.ts +25 -0
- package/lib/Utils/Auth/pre-key-manager.js +105 -0
- package/lib/Utils/Auth/use-multi-file-auth-state.d.ts +10 -0
- package/lib/Utils/Auth/use-multi-file-auth-state.js +116 -0
- package/lib/Utils/Auth/use-single-file-auth-state.d.ts +11 -0
- package/lib/Utils/Auth/use-single-file-auth-state.js +94 -0
- package/lib/Utils/Auth/use-sqlite-auth-state.d.ts +11 -0
- package/lib/Utils/Auth/use-sqlite-auth-state.js +109 -0
- package/lib/Utils/Auth/validate-connection.d.ts +44 -0
- package/lib/Utils/Auth/validate-connection.js +210 -0
- package/lib/Utils/Common/browser-utils.d.ts +9 -0
- package/lib/Utils/Common/browser-utils.js +28 -0
- package/lib/Utils/Common/generics.d.ts +63 -0
- package/lib/Utils/Common/generics.js +392 -0
- package/lib/Utils/Common/logger.d.ts +12 -0
- package/lib/Utils/Common/logger.js +12 -0
- package/lib/Utils/Common/make-mutex.d.ts +6 -0
- package/lib/Utils/Common/make-mutex.js +32 -0
- package/lib/Utils/Crypto/crypto.d.ts +39 -0
- package/lib/Utils/Crypto/crypto.js +117 -0
- package/lib/Utils/Crypto/lt-hash.d.ts +6 -0
- package/lib/Utils/Crypto/lt-hash.js +7 -0
- package/lib/Utils/Crypto/noise-handler.d.ts +20 -0
- package/lib/Utils/Crypto/noise-handler.js +200 -0
- package/lib/Utils/Crypto/signal.d.ts +82 -0
- package/lib/Utils/Crypto/signal.js +200 -0
- package/lib/Utils/Message/business.d.ts +49 -0
- package/lib/Utils/Message/business.js +230 -0
- package/lib/Utils/Message/decode-wa-message.d.ts +83 -0
- package/lib/Utils/Message/decode-wa-message.js +316 -0
- package/lib/Utils/Message/history.d.ts +75 -0
- package/lib/Utils/Message/history.js +133 -0
- package/lib/Utils/Message/link-preview.d.ts +13 -0
- package/lib/Utils/Message/link-preview.js +73 -0
- package/lib/Utils/Message/message-retry-manager.d.ts +79 -0
- package/lib/Utils/Message/message-retry-manager.js +259 -0
- package/lib/Utils/Message/messages-media.d.ts +134 -0
- package/lib/Utils/Message/messages-media.js +802 -0
- package/lib/Utils/Message/messages.d.ts +47 -0
- package/lib/Utils/Message/messages.js +1907 -0
- package/lib/Utils/Message/offline-node-processor.d.ts +10 -0
- package/lib/Utils/Message/offline-node-processor.js +40 -0
- package/lib/Utils/Message/process-message.d.ts +83 -0
- package/lib/Utils/Message/process-message.js +748 -0
- package/lib/Utils/Message/reporting-utils.d.ts +12 -0
- package/lib/Utils/Message/reporting-utils.js +257 -0
- package/lib/Utils/Message/rich-message-utils.d.ts +48 -0
- package/lib/Utils/Message/rich-message-utils.js +366 -0
- package/lib/Utils/Message/stanza-ack.d.ts +16 -0
- package/lib/Utils/Message/stanza-ack.js +37 -0
- package/lib/Utils/State/chat-utils.d.ts +408 -0
- package/lib/Utils/State/chat-utils.js +871 -0
- package/lib/Utils/State/event-buffer.d.ts +12 -0
- package/lib/Utils/State/event-buffer.js +621 -0
- package/lib/Utils/State/identity-change-handler.d.ts +13 -0
- package/lib/Utils/State/identity-change-handler.js +48 -0
- package/lib/Utils/State/sync-action-utils.d.ts +2 -0
- package/lib/Utils/State/sync-action-utils.js +48 -0
- package/lib/Utils/State/tc-token-utils.d.ts +29 -0
- package/lib/Utils/State/tc-token-utils.js +163 -0
- package/lib/Utils/index.d.ts +24 -0
- package/lib/Utils/index.js +24 -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 +79 -0
- package/lib/WABinary/generic-utils.js +226 -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 +1 -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/USyncBotProfileProtocol.d.ts +38 -0
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +50 -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 +22 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +53 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +14 -0
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +28 -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/index.d.ts +5 -0
- package/lib/WAUSync/Protocols/index.js +5 -0
- package/lib/WAUSync/USyncQuery.d.ts +20 -0
- package/lib/WAUSync/USyncQuery.js +97 -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/index.d.ts +11 -0
- package/lib/index.js +11 -0
- package/package.json +120 -5
- package/scripts/engine-requirements.js +16 -0
|
@@ -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/Common/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 { crypto } from '../Protocol/index.js';
|
|
2
|
+
export class SenderMessageKey {
|
|
3
|
+
constructor(iteration, seed) {
|
|
4
|
+
const derivative = crypto.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,6 @@
|
|
|
1
|
+
export function encrypt(key: any, data: any, iv: any): any;
|
|
2
|
+
export function decrypt(key: any, data: any, iv: any): any;
|
|
3
|
+
export function calculateMAC(key: any, data: any): any;
|
|
4
|
+
export function hash(data: any): any;
|
|
5
|
+
export function deriveSecrets(input: any, salt: any, info: any, chunks: any): any[];
|
|
6
|
+
export function verifyMAC(data: any, key: any, mac: any, length: any): void;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, createHmac, createHash } from 'node:crypto';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { hkdf } from 'whatsapp-rust-bridge';
|
|
4
|
+
function assertBuffer(value) {
|
|
5
|
+
if (!Buffer.isBuffer(value)) {
|
|
6
|
+
throw TypeError(`Expected Buffer instead of: ${value.constructor.name}`);
|
|
7
|
+
}
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
export function encrypt(key, data, iv) {
|
|
11
|
+
assertBuffer(key);
|
|
12
|
+
assertBuffer(data);
|
|
13
|
+
assertBuffer(iv);
|
|
14
|
+
const cipher = createCipheriv('aes-256-cbc', key, iv);
|
|
15
|
+
return Buffer.concat([cipher.update(data), cipher.final()]);
|
|
16
|
+
}
|
|
17
|
+
export function decrypt(key, data, iv) {
|
|
18
|
+
assertBuffer(key);
|
|
19
|
+
assertBuffer(data);
|
|
20
|
+
assertBuffer(iv);
|
|
21
|
+
const decipher = createDecipheriv('aes-256-cbc', key, iv);
|
|
22
|
+
return Buffer.concat([decipher.update(data), decipher.final()]);
|
|
23
|
+
}
|
|
24
|
+
export function calculateMAC(key, data) {
|
|
25
|
+
assertBuffer(key);
|
|
26
|
+
assertBuffer(data);
|
|
27
|
+
const hmac = createHmac('sha256', key);
|
|
28
|
+
hmac.update(data);
|
|
29
|
+
return Buffer.from(hmac.digest());
|
|
30
|
+
}
|
|
31
|
+
export function hash(data) {
|
|
32
|
+
assertBuffer(data);
|
|
33
|
+
const sha512 = createHash('sha512');
|
|
34
|
+
sha512.update(data);
|
|
35
|
+
return sha512.digest();
|
|
36
|
+
}
|
|
37
|
+
export function deriveSecrets(input, salt, info, chunks) {
|
|
38
|
+
assertBuffer(input);
|
|
39
|
+
assertBuffer(salt);
|
|
40
|
+
assertBuffer(info);
|
|
41
|
+
if (salt.byteLength != 32) {
|
|
42
|
+
throw new Error('Got salt of incorrect length');
|
|
43
|
+
}
|
|
44
|
+
chunks = chunks || 3;
|
|
45
|
+
assert(chunks >= 1 && chunks <= 3);
|
|
46
|
+
const result = hkdf(input, chunks * 32, { salt, info: info.toString('utf8') });
|
|
47
|
+
const output = new Array(chunks);
|
|
48
|
+
for (let i = 0; i < chunks; i++) {
|
|
49
|
+
output[i] = Buffer.from(result.buffer, result.byteOffset + (i * 32), 32);
|
|
50
|
+
}
|
|
51
|
+
return output;
|
|
52
|
+
}
|
|
53
|
+
export function verifyMAC(data, key, mac, length) {
|
|
54
|
+
const calculatedMac = calculateMAC(key, data).slice(0, length);
|
|
55
|
+
if (mac.length !== length || calculatedMac.length !== length) {
|
|
56
|
+
throw new Error('Bad MAC length');
|
|
57
|
+
}
|
|
58
|
+
if (!mac.equals(calculatedMac)) {
|
|
59
|
+
throw new Error('Bad MAC');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function getPublicFromPrivateKey(privKey: any): any;
|
|
2
|
+
export function generateKeyPair(): {
|
|
3
|
+
pubKey: any;
|
|
4
|
+
privKey: any;
|
|
5
|
+
};
|
|
6
|
+
export function calculateAgreement(pubKey: any, privKey: any): any;
|
|
7
|
+
export function calculateSignature(privKey: any, message: any): any;
|
|
8
|
+
export function verifySignature(pubKey: any, msg: any, sig: any, isInit: any): any;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { generateKeyPair as wasmGenerateKeyPair, calculateAgreement as wasmCalculateAgreement, calculateSignature as wasmCalculateSignature, verifySignature as wasmVerifySignature, getPublicFromPrivateKey as wasmGetPublicFromPrivateKey } from 'whatsapp-rust-bridge';
|
|
2
|
+
function validatePrivKey(privKey) {
|
|
3
|
+
if (privKey === undefined) {
|
|
4
|
+
throw new Error('Undefined private key');
|
|
5
|
+
}
|
|
6
|
+
if (!Buffer.isBuffer(privKey)) {
|
|
7
|
+
throw new Error(`Invalid private key type: ${privKey?.constructor?.name ?? typeof privKey}`);
|
|
8
|
+
}
|
|
9
|
+
if (privKey.byteLength != 32) {
|
|
10
|
+
throw new Error(`Incorrect private key length: ${privKey.byteLength}`);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function scrubPubKeyFormat(pubKey) {
|
|
14
|
+
if (!Buffer.isBuffer(pubKey)) {
|
|
15
|
+
throw new Error(`Invalid public key type: ${pubKey?.constructor?.name ?? typeof pubKey}`);
|
|
16
|
+
}
|
|
17
|
+
if ((pubKey.byteLength !== 33 || pubKey[0] !== 5) && pubKey.byteLength !== 32) {
|
|
18
|
+
throw new Error('Invalid public key');
|
|
19
|
+
}
|
|
20
|
+
if (pubKey.byteLength == 33) {
|
|
21
|
+
return pubKey.slice(1);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
console.error('WARNING: Expected pubkey of length 33, please report the ST and client that generated the pubkey');
|
|
25
|
+
return pubKey;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function getPublicFromPrivateKey(privKey) {
|
|
29
|
+
return Buffer.from(wasmGetPublicFromPrivateKey(privKey));
|
|
30
|
+
}
|
|
31
|
+
export function generateKeyPair() {
|
|
32
|
+
const kp = wasmGenerateKeyPair();
|
|
33
|
+
return {
|
|
34
|
+
pubKey: Buffer.from(kp.pubKey),
|
|
35
|
+
privKey: Buffer.from(kp.privKey)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function calculateAgreement(pubKey, privKey) {
|
|
39
|
+
pubKey = scrubPubKeyFormat(pubKey);
|
|
40
|
+
validatePrivKey(privKey);
|
|
41
|
+
return Buffer.from(wasmCalculateAgreement(pubKey, privKey));
|
|
42
|
+
}
|
|
43
|
+
export function calculateSignature(privKey, message) {
|
|
44
|
+
validatePrivKey(privKey);
|
|
45
|
+
if (!message) {
|
|
46
|
+
throw new Error('Invalid message');
|
|
47
|
+
}
|
|
48
|
+
return Buffer.from(wasmCalculateSignature(privKey, message));
|
|
49
|
+
}
|
|
50
|
+
export function verifySignature(pubKey, msg, sig, isInit) {
|
|
51
|
+
if (isInit)
|
|
52
|
+
return true;
|
|
53
|
+
pubKey = scrubPubKeyFormat(pubKey);
|
|
54
|
+
if (!msg) {
|
|
55
|
+
throw new Error('Invalid message');
|
|
56
|
+
}
|
|
57
|
+
if (!sig || sig.byteLength != 64) {
|
|
58
|
+
throw new Error('Invalid signature');
|
|
59
|
+
}
|
|
60
|
+
return wasmVerifySignature(pubKey, msg, sig);
|
|
61
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * as crypto from "./crypto.js";
|
|
2
|
+
export * as curve from "./curve.js";
|
|
3
|
+
export namespace keyhelper {
|
|
4
|
+
export { generateIdentityKeyPair };
|
|
5
|
+
export { generateRegistrationId };
|
|
6
|
+
export { generateSignedPreKey };
|
|
7
|
+
export { generatePreKey };
|
|
8
|
+
}
|
|
9
|
+
export { default as SessionBuilder } from "./session-builder.js";
|
|
10
|
+
export { default as SessionCipher } from "./session-cipher.js";
|
|
11
|
+
export { default as SessionRecord } from "./session-record.js";
|
|
12
|
+
import { generateIdentityKeyPair } from './internals.js';
|
|
13
|
+
import { generateRegistrationId } from './internals.js';
|
|
14
|
+
import { generateSignedPreKey } from './internals.js';
|
|
15
|
+
import { generatePreKey } from './internals.js';
|
|
16
|
+
export { default as ProtocolAddress, SignalError, UntrustedIdentityKeyError, SessionError, MessageCounterError, PreKeyError } from "./internals.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * as crypto from './crypto.js';
|
|
2
|
+
export * as curve from './curve.js';
|
|
3
|
+
import { generateIdentityKeyPair, generateRegistrationId, generateSignedPreKey, generatePreKey } from './internals.js';
|
|
4
|
+
export const keyhelper = { generateIdentityKeyPair, generateRegistrationId, generateSignedPreKey, generatePreKey };
|
|
5
|
+
export { default as ProtocolAddress } from './internals.js';
|
|
6
|
+
export { default as SessionBuilder } from './session-builder.js';
|
|
7
|
+
export { default as SessionCipher } from './session-cipher.js';
|
|
8
|
+
export { default as SessionRecord } from './session-record.js';
|
|
9
|
+
export { SignalError, UntrustedIdentityKeyError, SessionError, MessageCounterError, PreKeyError } from './internals.js';
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export function queueJob(key: any, run: any): Promise<any>;
|
|
2
|
+
export function generateRegistrationId(): number;
|
|
3
|
+
export function generateSignedPreKey(identityKeyPair: any, signedKeyId: any): {
|
|
4
|
+
keyId: any;
|
|
5
|
+
keyPair: {
|
|
6
|
+
pubKey: any;
|
|
7
|
+
privKey: any;
|
|
8
|
+
};
|
|
9
|
+
signature: any;
|
|
10
|
+
};
|
|
11
|
+
export function generatePreKey(keyId: any): {
|
|
12
|
+
keyId: any;
|
|
13
|
+
keyPair: {
|
|
14
|
+
pubKey: any;
|
|
15
|
+
privKey: any;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export namespace BaseKeyType {
|
|
19
|
+
let OURS: number;
|
|
20
|
+
let THEIRS: number;
|
|
21
|
+
}
|
|
22
|
+
export namespace ChainType {
|
|
23
|
+
let SENDING: number;
|
|
24
|
+
let RECEIVING: number;
|
|
25
|
+
}
|
|
26
|
+
export class SignalError extends Error {
|
|
27
|
+
}
|
|
28
|
+
export class UntrustedIdentityKeyError extends SignalError {
|
|
29
|
+
constructor(addr: any, identityKey: any);
|
|
30
|
+
addr: any;
|
|
31
|
+
identityKey: any;
|
|
32
|
+
}
|
|
33
|
+
export class SessionError extends SignalError {
|
|
34
|
+
constructor(message: any);
|
|
35
|
+
}
|
|
36
|
+
export class MessageCounterError extends SessionError {
|
|
37
|
+
}
|
|
38
|
+
export class PreKeyError extends SessionError {
|
|
39
|
+
}
|
|
40
|
+
export namespace errors {
|
|
41
|
+
export { SignalError };
|
|
42
|
+
export { UntrustedIdentityKeyError };
|
|
43
|
+
export { SessionError };
|
|
44
|
+
export { MessageCounterError };
|
|
45
|
+
export { PreKeyError };
|
|
46
|
+
}
|
|
47
|
+
export class ProtocolAddress {
|
|
48
|
+
static from(encodedAddress: any): ProtocolAddress;
|
|
49
|
+
constructor(id: any, deviceId: any);
|
|
50
|
+
id: string;
|
|
51
|
+
deviceId: number;
|
|
52
|
+
toString(): string;
|
|
53
|
+
is(other: any): boolean;
|
|
54
|
+
}
|
|
55
|
+
export const generateIdentityKeyPair: typeof generateKeyPair;
|
|
56
|
+
export default ProtocolAddress;
|
|
57
|
+
import { generateKeyPair } from './curve.js';
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { calculateSignature, generateKeyPair } from './curve.js';
|
|
3
|
+
export const BaseKeyType = {
|
|
4
|
+
OURS: 1,
|
|
5
|
+
THEIRS: 2
|
|
6
|
+
};
|
|
7
|
+
export const ChainType = {
|
|
8
|
+
SENDING: 1,
|
|
9
|
+
RECEIVING: 2
|
|
10
|
+
};
|
|
11
|
+
export class SignalError extends Error {
|
|
12
|
+
}
|
|
13
|
+
export class UntrustedIdentityKeyError extends SignalError {
|
|
14
|
+
constructor(addr, identityKey) {
|
|
15
|
+
super();
|
|
16
|
+
this.name = 'UntrustedIdentityKeyError';
|
|
17
|
+
this.addr = addr;
|
|
18
|
+
this.identityKey = identityKey;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class SessionError extends SignalError {
|
|
22
|
+
constructor(message) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = 'SessionError';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class MessageCounterError extends SessionError {
|
|
28
|
+
constructor(message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'MessageCounterError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class PreKeyError extends SessionError {
|
|
34
|
+
constructor(message) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = 'PreKeyError';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export const errors = {
|
|
40
|
+
SignalError,
|
|
41
|
+
UntrustedIdentityKeyError,
|
|
42
|
+
SessionError,
|
|
43
|
+
MessageCounterError,
|
|
44
|
+
PreKeyError
|
|
45
|
+
};
|
|
46
|
+
const queues = new Map();
|
|
47
|
+
async function drain(key, queue) {
|
|
48
|
+
try {
|
|
49
|
+
while (queue.head < queue.jobs.length) {
|
|
50
|
+
const job = queue.jobs[queue.head++];
|
|
51
|
+
queue.jobs[queue.head - 1] = null;
|
|
52
|
+
try {
|
|
53
|
+
job.resolve(await job.run());
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
job.reject(error);
|
|
57
|
+
}
|
|
58
|
+
if (queue.head >= 1024 && queue.head * 2 >= queue.jobs.length) {
|
|
59
|
+
queue.jobs = queue.jobs.slice(queue.head);
|
|
60
|
+
queue.head = 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
if (queues.get(key) === queue) {
|
|
66
|
+
queues.delete(key);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export function queueJob(key, run) {
|
|
71
|
+
if (typeof run !== 'function') {
|
|
72
|
+
return Promise.reject(new TypeError('Job must be a function'));
|
|
73
|
+
}
|
|
74
|
+
let queue = queues.get(key);
|
|
75
|
+
const start = !queue;
|
|
76
|
+
if (!queue) {
|
|
77
|
+
queue = { jobs: [], head: 0 };
|
|
78
|
+
queues.set(key, queue);
|
|
79
|
+
}
|
|
80
|
+
const promise = new Promise((resolve, reject) => {
|
|
81
|
+
queue.jobs.push({ run, resolve, reject });
|
|
82
|
+
});
|
|
83
|
+
if (start) {
|
|
84
|
+
void drain(key, queue);
|
|
85
|
+
}
|
|
86
|
+
return promise;
|
|
87
|
+
}
|
|
88
|
+
export class ProtocolAddress {
|
|
89
|
+
static from(encodedAddress) {
|
|
90
|
+
if (typeof encodedAddress !== 'string' || !encodedAddress.match(/.*\.\d+/)) {
|
|
91
|
+
throw new Error('Invalid address encoding');
|
|
92
|
+
}
|
|
93
|
+
const parts = encodedAddress.split('.');
|
|
94
|
+
return new this(parts[0], parseInt(parts[1]));
|
|
95
|
+
}
|
|
96
|
+
constructor(id, deviceId) {
|
|
97
|
+
if (typeof id !== 'string') {
|
|
98
|
+
throw new TypeError('id required for addr');
|
|
99
|
+
}
|
|
100
|
+
if (id.indexOf('.') !== -1) {
|
|
101
|
+
throw new TypeError('encoded addr detected');
|
|
102
|
+
}
|
|
103
|
+
this.id = id;
|
|
104
|
+
if (typeof deviceId !== 'number') {
|
|
105
|
+
throw new TypeError('number required for deviceId');
|
|
106
|
+
}
|
|
107
|
+
this.deviceId = deviceId;
|
|
108
|
+
}
|
|
109
|
+
toString() {
|
|
110
|
+
return `${this.id}.${this.deviceId}`;
|
|
111
|
+
}
|
|
112
|
+
is(other) {
|
|
113
|
+
if (!(other instanceof ProtocolAddress)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return other.id === this.id && other.deviceId === this.deviceId;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function isNonNegativeInteger(n) {
|
|
120
|
+
return (typeof n === 'number' && (n % 1) === 0 && n >= 0);
|
|
121
|
+
}
|
|
122
|
+
export const generateIdentityKeyPair = generateKeyPair;
|
|
123
|
+
export function generateRegistrationId() {
|
|
124
|
+
var registrationId = Uint16Array.from(randomBytes(2))[0];
|
|
125
|
+
return registrationId & 0x3fff;
|
|
126
|
+
}
|
|
127
|
+
export function generateSignedPreKey(identityKeyPair, signedKeyId) {
|
|
128
|
+
if (!(identityKeyPair.privKey instanceof Buffer) ||
|
|
129
|
+
identityKeyPair.privKey.byteLength != 32 ||
|
|
130
|
+
!(identityKeyPair.pubKey instanceof Buffer) ||
|
|
131
|
+
identityKeyPair.pubKey.byteLength != 33) {
|
|
132
|
+
throw new TypeError('Invalid argument for identityKeyPair');
|
|
133
|
+
}
|
|
134
|
+
if (!isNonNegativeInteger(signedKeyId)) {
|
|
135
|
+
throw new TypeError('Invalid argument for signedKeyId: ' + signedKeyId);
|
|
136
|
+
}
|
|
137
|
+
const keyPair = generateKeyPair();
|
|
138
|
+
const sig = calculateSignature(identityKeyPair.privKey, keyPair.pubKey);
|
|
139
|
+
return {
|
|
140
|
+
keyId: signedKeyId,
|
|
141
|
+
keyPair: keyPair,
|
|
142
|
+
signature: sig
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
export function generatePreKey(keyId) {
|
|
146
|
+
if (!isNonNegativeInteger(keyId)) {
|
|
147
|
+
throw new TypeError('Invalid argument for keyId: ' + keyId);
|
|
148
|
+
}
|
|
149
|
+
const keyPair = generateKeyPair();
|
|
150
|
+
return {
|
|
151
|
+
keyId,
|
|
152
|
+
keyPair
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
export default ProtocolAddress;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export default SessionBuilder;
|
|
2
|
+
declare class SessionBuilder {
|
|
3
|
+
constructor(storage: any, protocolAddress: any);
|
|
4
|
+
addr: any;
|
|
5
|
+
storage: any;
|
|
6
|
+
initOutgoing(device: any): Promise<any>;
|
|
7
|
+
initIncoming(record: any, message: any): Promise<any>;
|
|
8
|
+
initSession(isInitiator: any, ourEphemeralKey: any, ourSignedKey: any, theirIdentityPubKey: any, theirEphemeralPubKey: any, theirSignedPubKey: any, registrationId: any): Promise<{
|
|
9
|
+
_chains: {};
|
|
10
|
+
toString(): string;
|
|
11
|
+
inspect(): string;
|
|
12
|
+
addChain(key: any, value: any): void;
|
|
13
|
+
getChain(key: any): any;
|
|
14
|
+
deleteChain(key: any): void;
|
|
15
|
+
chains(): Generator<any[], void, unknown>;
|
|
16
|
+
serialize(): {
|
|
17
|
+
registrationId: any;
|
|
18
|
+
currentRatchet: {
|
|
19
|
+
ephemeralKeyPair: {
|
|
20
|
+
pubKey: any;
|
|
21
|
+
privKey: any;
|
|
22
|
+
};
|
|
23
|
+
lastRemoteEphemeralKey: any;
|
|
24
|
+
previousCounter: any;
|
|
25
|
+
rootKey: any;
|
|
26
|
+
};
|
|
27
|
+
indexInfo: {
|
|
28
|
+
baseKey: any;
|
|
29
|
+
baseKeyType: any;
|
|
30
|
+
closed: any;
|
|
31
|
+
used: any;
|
|
32
|
+
created: any;
|
|
33
|
+
remoteIdentityKey: any;
|
|
34
|
+
};
|
|
35
|
+
_chains: {};
|
|
36
|
+
};
|
|
37
|
+
_serialize_chains(chains: any): {};
|
|
38
|
+
}>;
|
|
39
|
+
calculateSendingRatchet(session: any, remoteKey: any): void;
|
|
40
|
+
}
|