@neelegirl/baileys 1.5.2 → 1.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +195 -187
- package/WAProto/WAProto.proto +537 -236
- package/WAProto/index.d.ts +5971 -2388
- package/WAProto/index.js +17298 -6513
- package/lib/Defaults/baileys-version.json +3 -3
- package/lib/Defaults/index.d.ts +77 -67
- package/lib/Defaults/index.js +148 -136
- package/lib/Defaults/phonenumber-mcc.json +223 -223
- package/lib/Signal/WASignalGroup/GroupProtocol.js +1908 -1908
- package/lib/Signal/WASignalGroup/ciphertext-message.d.ts +9 -0
- package/lib/Signal/WASignalGroup/ciphertext-message.js +19 -0
- package/lib/Signal/WASignalGroup/ciphertext_message.js +15 -15
- package/lib/Signal/WASignalGroup/group-session-builder.d.ts +17 -0
- package/lib/Signal/WASignalGroup/group-session-builder.js +72 -0
- package/lib/Signal/WASignalGroup/group.proto +41 -41
- package/lib/Signal/WASignalGroup/group_cipher.d.ts +19 -0
- package/lib/Signal/WASignalGroup/group_cipher.js +101 -110
- package/lib/Signal/WASignalGroup/group_session_builder.js +45 -45
- package/lib/Signal/WASignalGroup/index.d.ts +11 -0
- package/lib/Signal/WASignalGroup/index.js +61 -6
- package/lib/Signal/WASignalGroup/keyhelper.d.ts +16 -0
- package/lib/Signal/WASignalGroup/keyhelper.js +58 -13
- package/lib/Signal/WASignalGroup/protobufs.js +2 -2
- package/lib/Signal/WASignalGroup/queue_job.js +68 -68
- package/lib/Signal/WASignalGroup/readme.md +5 -5
- package/lib/Signal/WASignalGroup/sender-chain-key.d.ts +14 -0
- package/lib/Signal/WASignalGroup/sender-chain-key.js +47 -0
- package/lib/Signal/WASignalGroup/sender-key-distribution-message.d.ts +17 -0
- package/lib/Signal/WASignalGroup/sender-key-distribution-message.js +71 -0
- package/lib/Signal/WASignalGroup/sender-key-message.d.ts +19 -0
- package/lib/Signal/WASignalGroup/sender-key-message.js +73 -0
- package/lib/Signal/WASignalGroup/sender-key-name.d.ts +19 -0
- package/lib/Signal/WASignalGroup/sender-key-name.js +59 -0
- package/lib/Signal/WASignalGroup/sender-key-record.d.ts +32 -0
- package/lib/Signal/WASignalGroup/sender-key-record.js +58 -0
- package/lib/Signal/WASignalGroup/sender-key-state.d.ts +44 -0
- package/lib/Signal/WASignalGroup/sender-key-state.js +147 -0
- package/lib/Signal/WASignalGroup/sender-message-key.d.ts +11 -0
- package/lib/Signal/WASignalGroup/sender-message-key.js +33 -0
- package/lib/Signal/WASignalGroup/sender_chain_key.js +49 -49
- package/lib/Signal/WASignalGroup/sender_key_distribution_message.js +77 -77
- package/lib/Signal/WASignalGroup/sender_key_message.js +91 -91
- package/lib/Signal/WASignalGroup/sender_key_name.js +69 -69
- package/lib/Signal/WASignalGroup/sender_key_record.js +55 -55
- package/lib/Signal/WASignalGroup/sender_key_state.js +128 -128
- package/lib/Signal/WASignalGroup/sender_message_key.js +38 -38
- package/lib/Signal/libsignal.d.ts +5 -1
- package/lib/Signal/libsignal.js +390 -161
- package/lib/Signal/lid-mapping.d.ts +28 -0
- package/lib/Signal/lid-mapping.js +184 -0
- package/lib/Socket/Client/abstract-socket-client.d.ts +15 -15
- package/lib/Socket/Client/abstract-socket-client.js +13 -13
- package/lib/Socket/Client/index.d.ts +2 -2
- package/lib/Socket/Client/mobile-socket-client.d.ts +12 -12
- package/lib/Socket/Client/mobile-socket-client.js +65 -65
- package/lib/Socket/Client/types.d.ts +1 -1
- package/lib/Socket/Client/websocket.d.ts +1 -1
- package/lib/Socket/business.d.ts +6 -6
- package/lib/Socket/business.js +152 -5
- package/lib/Socket/chats.d.ts +3 -4
- package/lib/Socket/chats.js +31 -26
- package/lib/Socket/communities.d.ts +223 -223
- package/lib/Socket/communities.js +432 -432
- package/lib/Socket/groups.d.ts +2 -4
- package/lib/Socket/groups.js +22 -14
- package/lib/Socket/index.d.ts +69 -69
- package/lib/Socket/index.js +3 -2
- package/lib/Socket/messages-recv.d.ts +3 -6
- package/lib/Socket/messages-recv.js +1449 -1707
- package/lib/Socket/messages-send.d.ts +2 -4
- package/lib/Socket/messages-send.js +617 -126
- package/lib/Socket/mex.d.ts +2 -2
- package/lib/Socket/mex.js +46 -46
- package/lib/Socket/newsletter.d.ts +2 -4
- package/lib/Socket/newsletter.js +294 -285
- package/lib/Socket/socket.js +318 -132
- package/lib/Socket/usync.js +3 -3
- package/lib/Store/index.d.ts +4 -4
- package/lib/Store/index.js +23 -23
- package/lib/Store/make-cache-manager-store.d.ts +13 -13
- package/lib/Store/make-cache-manager-store.js +89 -89
- package/lib/Store/make-in-memory-store.d.ts +122 -122
- package/lib/Store/make-in-memory-store.js +428 -428
- package/lib/Store/make-ordered-dictionary.d.ts +11 -11
- package/lib/Store/make-ordered-dictionary.js +85 -85
- package/lib/Store/object-repository.d.ts +9 -9
- package/lib/Store/object-repository.js +30 -30
- package/lib/Types/Auth.d.ts +5 -4
- package/lib/Types/Bussines.js +3 -0
- package/lib/Types/Bussiness.d.ts +28 -0
- package/lib/Types/Chat.d.ts +13 -8
- package/lib/Types/Contact.d.ts +4 -1
- package/lib/Types/Events.d.ts +13 -16
- package/lib/Types/GroupMetadata.d.ts +1 -1
- package/lib/Types/Message.d.ts +18 -7
- package/lib/Types/Message.js +7 -1
- package/lib/Types/MexUpdates.d.ts +8 -8
- package/lib/Types/MexUpdates.js +17 -17
- package/lib/Types/Newsletter.d.ts +1 -1
- package/lib/Types/Product.d.ts +1 -1
- package/lib/Types/Signal.d.ts +31 -1
- package/lib/Types/Socket.d.ts +34 -13
- package/lib/Types/State.d.ts +1 -1
- package/lib/Types/USync.d.ts +2 -2
- package/lib/Types/index.d.ts +16 -15
- package/lib/Types/index.js +4 -2
- package/lib/Utils/auth-utils.d.ts +20 -20
- package/lib/Utils/auth-utils.js +527 -204
- package/lib/Utils/baileys-event-stream.d.ts +17 -17
- package/lib/Utils/baileys-event-stream.js +69 -69
- package/lib/Utils/business.d.ts +28 -28
- package/lib/Utils/business.js +254 -254
- package/lib/Utils/chat-utils.d.ts +81 -81
- package/lib/Utils/chat-utils.js +808 -780
- package/lib/Utils/crypto.d.ts +55 -55
- package/lib/Utils/crypto.js +188 -178
- package/lib/Utils/decode-wa-message.d.ts +52 -40
- package/lib/Utils/decode-wa-message.js +322 -252
- package/lib/Utils/event-buffer.d.ts +38 -38
- package/lib/Utils/event-buffer.js +594 -564
- package/lib/Utils/generics.d.ts +131 -129
- package/lib/Utils/generics.js +629 -623
- package/lib/Utils/history.d.ts +22 -22
- package/lib/Utils/history.js +103 -109
- package/lib/Utils/index.d.ts +20 -19
- package/lib/Utils/index.js +39 -38
- package/lib/Utils/link-preview.d.ts +22 -22
- package/lib/Utils/link-preview.js +119 -119
- package/lib/Utils/logger.d.ts +13 -13
- package/lib/Utils/logger.js +7 -7
- package/lib/Utils/lt-hash.d.ts +13 -13
- package/lib/Utils/lt-hash.js +57 -57
- package/lib/Utils/make-mutex.d.ts +8 -8
- package/lib/Utils/make-mutex.js +48 -48
- package/lib/Utils/message-retry-manager.d.ts +88 -0
- package/lib/Utils/message-retry-manager.js +160 -0
- package/lib/Utils/messages-media.d.ts +134 -128
- package/lib/Utils/messages-media.js +868 -805
- package/lib/Utils/messages.d.ts +104 -102
- package/lib/Utils/messages.js +1744 -1578
- package/lib/Utils/noise-handler.d.ts +20 -19
- package/lib/Utils/noise-handler.js +164 -154
- package/lib/Utils/process-message.d.ts +48 -48
- package/lib/Utils/process-message.js +427 -428
- package/lib/Utils/signal.d.ts +41 -41
- package/lib/Utils/signal.js +165 -165
- package/lib/Utils/use-mongo-file-auth-state.d.ts +5 -5
- package/lib/Utils/use-mongo-file-auth-state.js +83 -83
- package/lib/Utils/use-multi-file-auth-state.d.ts +17 -17
- package/lib/Utils/use-multi-file-auth-state.js +237 -237
- package/lib/Utils/use-single-file-auth-state.d.ts +12 -12
- package/lib/Utils/use-single-file-auth-state.js +79 -79
- package/lib/Utils/validate-connection.d.ts +12 -12
- package/lib/Utils/validate-connection.js +219 -186
- package/lib/WABinary/constants.d.ts +29 -29
- package/lib/WABinary/constants.js +1315 -1315
- package/lib/WABinary/decode.d.ts +8 -8
- package/lib/WABinary/decode.js +287 -287
- package/lib/WABinary/encode.d.ts +2 -2
- package/lib/WABinary/encode.js +264 -264
- package/lib/WABinary/generic-utils.d.ts +27 -27
- package/lib/WABinary/generic-utils.js +141 -141
- package/lib/WABinary/index.d.ts +5 -5
- package/lib/WABinary/index.js +24 -24
- package/lib/WABinary/jid-utils.d.ts +58 -53
- package/lib/WABinary/jid-utils.js +103 -91
- package/lib/WABinary/types.d.ts +21 -21
- package/lib/WABinary/types.js +2 -2
- package/lib/WAM/BinaryInfo.d.ts +15 -15
- package/lib/WAM/BinaryInfo.js +16 -16
- package/lib/WAM/constants.d.ts +46 -46
- package/lib/WAM/constants.js +15370 -15370
- package/lib/WAM/encode.d.ts +2 -2
- package/lib/WAM/encode.js +163 -164
- package/lib/WAM/index.d.ts +3 -3
- package/lib/WAM/index.js +22 -22
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.d.ts +27 -27
- package/lib/WAUSync/Protocols/USyncBotProfileProtocol.js +68 -68
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +3 -3
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +2 -2
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +2 -2
- package/lib/WAUSync/Protocols/USyncLIDProtocol.d.ts +9 -8
- package/lib/WAUSync/Protocols/USyncLIDProtocol.js +37 -29
- package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +2 -2
- package/lib/WAUSync/Protocols/index.d.ts +6 -6
- package/lib/WAUSync/USyncQuery.d.ts +3 -3
- package/lib/WAUSync/index.d.ts +3 -3
- package/lib/index.d.ts +13 -13
- package/lib/index.js +33 -33
- package/package.json +96 -94
- package/lib/Socket/registration.d.ts +0 -266
- package/lib/Socket/registration.js +0 -166
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { generateKeyPair } from '@neelegirl/libsignal/src/curve'
|
|
2
|
+
|
|
3
|
+
type KeyPairType = ReturnType<typeof generateKeyPair>
|
|
4
|
+
|
|
5
|
+
export declare function generateSenderKey(): Buffer
|
|
6
|
+
|
|
7
|
+
export declare function generateSenderKeyId(): number
|
|
8
|
+
|
|
9
|
+
export interface SigningKeyPair {
|
|
10
|
+
public: Buffer
|
|
11
|
+
private: Buffer
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare function generateSenderSigningKey(key?: KeyPairType): SigningKeyPair
|
|
15
|
+
|
|
16
|
+
export {}
|
|
@@ -1,21 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
const nodeCrypto = require('crypto');
|
|
1
|
+
"use strict"
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k)
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k] } }
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc)
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k
|
|
12
|
+
o[k2] = m[k]
|
|
13
|
+
}))
|
|
14
|
+
|
|
15
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v })
|
|
17
|
+
}) : function(o, v) {
|
|
18
|
+
o["default"] = v
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = []
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k
|
|
26
|
+
return ar
|
|
27
|
+
}
|
|
28
|
+
return ownKeys(o)
|
|
29
|
+
}
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod
|
|
32
|
+
var result = {}
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i])
|
|
34
|
+
__setModuleDefault(result, mod)
|
|
35
|
+
return result
|
|
36
|
+
}
|
|
37
|
+
})()
|
|
38
|
+
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
40
|
+
|
|
41
|
+
const nodeCrypto = __importStar(require("crypto"))
|
|
42
|
+
const curve_1 = require("@neelegirl/libsignal/src/curve")
|
|
43
|
+
|
|
44
|
+
function generateSenderKey() {
|
|
45
|
+
return nodeCrypto.randomBytes(32)
|
|
6
46
|
}
|
|
7
47
|
|
|
8
|
-
|
|
9
|
-
return nodeCrypto.randomInt(2147483647)
|
|
48
|
+
function generateSenderKeyId() {
|
|
49
|
+
return nodeCrypto.randomInt(2147483647)
|
|
10
50
|
}
|
|
11
51
|
|
|
12
|
-
|
|
52
|
+
function generateSenderSigningKey(key) {
|
|
13
53
|
if (!key) {
|
|
14
|
-
key =
|
|
54
|
+
key = curve_1.generateKeyPair()
|
|
15
55
|
}
|
|
16
|
-
|
|
17
56
|
return {
|
|
18
|
-
public: key.pubKey,
|
|
19
|
-
private: key.privKey
|
|
20
|
-
}
|
|
21
|
-
}
|
|
57
|
+
public: Buffer.from(key.pubKey),
|
|
58
|
+
private: Buffer.from(key.privKey)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = {
|
|
63
|
+
generateSenderKey,
|
|
64
|
+
generateSenderKeyId,
|
|
65
|
+
generateSenderSigningKey
|
|
66
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
const { groupproto } = require('./GroupProtocol')
|
|
2
|
-
|
|
1
|
+
const { groupproto } = require('./GroupProtocol')
|
|
2
|
+
|
|
3
3
|
module.exports = groupproto
|
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
// vim: ts=4:sw=4:expandtab
|
|
2
|
-
|
|
3
|
-
/*
|
|
4
|
-
* jobQueue manages multiple queues indexed by device to serialize
|
|
5
|
-
* session io ops on the database.
|
|
6
|
-
*/
|
|
7
|
-
'use strict';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const _queueAsyncBuckets = new Map();
|
|
11
|
-
const _gcLimit = 10000;
|
|
12
|
-
|
|
13
|
-
async function _asyncQueueExecutor(queue, cleanup) {
|
|
14
|
-
let offt = 0;
|
|
15
|
-
while (true) {
|
|
16
|
-
let limit = Math.min(queue.length, _gcLimit); // Break up thundering hurds for GC duty.
|
|
17
|
-
for (let i = offt; i < limit; i++) {
|
|
18
|
-
const job = queue[i];
|
|
19
|
-
try {
|
|
20
|
-
job.resolve(await job.awaitable());
|
|
21
|
-
} catch (e) {
|
|
22
|
-
job.reject(e);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (limit < queue.length) {
|
|
26
|
-
/* Perform lazy GC of queue for faster iteration. */
|
|
27
|
-
if (limit >= _gcLimit) {
|
|
28
|
-
queue.splice(0, limit);
|
|
29
|
-
offt = 0;
|
|
30
|
-
} else {
|
|
31
|
-
offt = limit;
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
cleanup();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
module.exports = function (bucket, awaitable) {
|
|
41
|
-
/* Run the async awaitable only when all other async calls registered
|
|
42
|
-
* here have completed (or thrown). The bucket argument is a hashable
|
|
43
|
-
* key representing the task queue to use. */
|
|
44
|
-
if (!awaitable.name) {
|
|
45
|
-
// Make debuging easier by adding a name to this function.
|
|
46
|
-
Object.defineProperty(awaitable, 'name', { writable: true });
|
|
47
|
-
if (typeof bucket === 'string') {
|
|
48
|
-
awaitable.name = bucket;
|
|
49
|
-
} else {
|
|
50
|
-
console.warn("Unhandled bucket type (for naming):", typeof bucket, bucket);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
let inactive;
|
|
54
|
-
if (!_queueAsyncBuckets.has(bucket)) {
|
|
55
|
-
_queueAsyncBuckets.set(bucket, []);
|
|
56
|
-
inactive = true;
|
|
57
|
-
}
|
|
58
|
-
const queue = _queueAsyncBuckets.get(bucket);
|
|
59
|
-
const job = new Promise((resolve, reject) => queue.push({
|
|
60
|
-
awaitable,
|
|
61
|
-
resolve,
|
|
62
|
-
reject
|
|
63
|
-
}));
|
|
64
|
-
if (inactive) {
|
|
65
|
-
/* An executor is not currently active; Start one now. */
|
|
66
|
-
_asyncQueueExecutor(queue, () => _queueAsyncBuckets.delete(bucket));
|
|
67
|
-
}
|
|
68
|
-
return job;
|
|
1
|
+
// vim: ts=4:sw=4:expandtab
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* jobQueue manages multiple queues indexed by device to serialize
|
|
5
|
+
* session io ops on the database.
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const _queueAsyncBuckets = new Map();
|
|
11
|
+
const _gcLimit = 10000;
|
|
12
|
+
|
|
13
|
+
async function _asyncQueueExecutor(queue, cleanup) {
|
|
14
|
+
let offt = 0;
|
|
15
|
+
while (true) {
|
|
16
|
+
let limit = Math.min(queue.length, _gcLimit); // Break up thundering hurds for GC duty.
|
|
17
|
+
for (let i = offt; i < limit; i++) {
|
|
18
|
+
const job = queue[i];
|
|
19
|
+
try {
|
|
20
|
+
job.resolve(await job.awaitable());
|
|
21
|
+
} catch (e) {
|
|
22
|
+
job.reject(e);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (limit < queue.length) {
|
|
26
|
+
/* Perform lazy GC of queue for faster iteration. */
|
|
27
|
+
if (limit >= _gcLimit) {
|
|
28
|
+
queue.splice(0, limit);
|
|
29
|
+
offt = 0;
|
|
30
|
+
} else {
|
|
31
|
+
offt = limit;
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
cleanup();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = function (bucket, awaitable) {
|
|
41
|
+
/* Run the async awaitable only when all other async calls registered
|
|
42
|
+
* here have completed (or thrown). The bucket argument is a hashable
|
|
43
|
+
* key representing the task queue to use. */
|
|
44
|
+
if (!awaitable.name) {
|
|
45
|
+
// Make debuging easier by adding a name to this function.
|
|
46
|
+
Object.defineProperty(awaitable, 'name', { writable: true });
|
|
47
|
+
if (typeof bucket === 'string') {
|
|
48
|
+
awaitable.name = bucket;
|
|
49
|
+
} else {
|
|
50
|
+
console.warn("Unhandled bucket type (for naming):", typeof bucket, bucket);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
let inactive;
|
|
54
|
+
if (!_queueAsyncBuckets.has(bucket)) {
|
|
55
|
+
_queueAsyncBuckets.set(bucket, []);
|
|
56
|
+
inactive = true;
|
|
57
|
+
}
|
|
58
|
+
const queue = _queueAsyncBuckets.get(bucket);
|
|
59
|
+
const job = new Promise((resolve, reject) => queue.push({
|
|
60
|
+
awaitable,
|
|
61
|
+
resolve,
|
|
62
|
+
reject
|
|
63
|
+
}));
|
|
64
|
+
if (inactive) {
|
|
65
|
+
/* An executor is not currently active; Start one now. */
|
|
66
|
+
_asyncQueueExecutor(queue, () => _queueAsyncBuckets.delete(bucket));
|
|
67
|
+
}
|
|
68
|
+
return job;
|
|
69
69
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
# Signal-Group
|
|
2
|
-
|
|
3
|
-
This contains the code to decrypt/encrypt WA group messages.
|
|
4
|
-
Originally from [pokearaujo/libsignal-node](https://github.com/pokearaujo/libsignal-node)
|
|
5
|
-
|
|
1
|
+
# Signal-Group
|
|
2
|
+
|
|
3
|
+
This contains the code to decrypt/encrypt WA group messages.
|
|
4
|
+
Originally from [pokearaujo/libsignal-node](https://github.com/pokearaujo/libsignal-node)
|
|
5
|
+
|
|
6
6
|
The code has been moved outside the signal package as I felt it didn't belong in ths signal package, as it isn't inherently a part of signal but of WA.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SenderMessageKey } from './sender-message-key'
|
|
2
|
+
|
|
3
|
+
export declare class SenderChainKey {
|
|
4
|
+
private readonly MESSAGE_KEY_SEED
|
|
5
|
+
private readonly CHAIN_KEY_SEED
|
|
6
|
+
private readonly iteration
|
|
7
|
+
private readonly chainKey
|
|
8
|
+
constructor(iteration: number, chainKey: any)
|
|
9
|
+
getIteration(): number
|
|
10
|
+
getSenderMessageKey(): SenderMessageKey
|
|
11
|
+
getNext(): SenderChainKey
|
|
12
|
+
getSeed(): Uint8Array
|
|
13
|
+
private getDerivative
|
|
14
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
4
|
+
|
|
5
|
+
const crypto_1 = require("@neelegirl/libsignal/src/crypto")
|
|
6
|
+
const sender_message_key_1 = require("./sender-message-key")
|
|
7
|
+
|
|
8
|
+
class SenderChainKey {
|
|
9
|
+
constructor(iteration, chainKey) {
|
|
10
|
+
this.MESSAGE_KEY_SEED = Buffer.from([0x01])
|
|
11
|
+
this.CHAIN_KEY_SEED = Buffer.from([0x02])
|
|
12
|
+
this.iteration = iteration
|
|
13
|
+
if (Buffer.isBuffer(chainKey)) {
|
|
14
|
+
// backported from @MartinSchere's PR
|
|
15
|
+
this.chainKey = chainKey
|
|
16
|
+
}
|
|
17
|
+
else if (chainKey instanceof Uint8Array) {
|
|
18
|
+
this.chainKey = Buffer.from(chainKey)
|
|
19
|
+
}
|
|
20
|
+
else if (chainKey && typeof chainKey === 'object') {
|
|
21
|
+
// backported from @MartinSchere (#1741)
|
|
22
|
+
this.chainKey = Buffer.from(Object.values(chainKey)) // temp fix // backported from @MartinSchere (#1741)
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
this.chainKey = Buffer.alloc(0)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
getIteration() {
|
|
29
|
+
return this.iteration
|
|
30
|
+
}
|
|
31
|
+
getSenderMessageKey() {
|
|
32
|
+
return new sender_message_key_1.SenderMessageKey(this.iteration, this.getDerivative(this.MESSAGE_KEY_SEED, this.chainKey))
|
|
33
|
+
}
|
|
34
|
+
getNext() {
|
|
35
|
+
return new SenderChainKey(this.iteration + 1, this.getDerivative(this.CHAIN_KEY_SEED, this.chainKey))
|
|
36
|
+
}
|
|
37
|
+
getSeed() {
|
|
38
|
+
return this.chainKey
|
|
39
|
+
}
|
|
40
|
+
getDerivative(seed, key) {
|
|
41
|
+
return (0, crypto_1.calculateMAC)(key, seed)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
module.exports = {
|
|
46
|
+
SenderChainKey
|
|
47
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { CiphertextMessage } from './ciphertext-message'
|
|
2
|
+
|
|
3
|
+
export declare class SenderKeyDistributionMessage extends CiphertextMessage {
|
|
4
|
+
private readonly id
|
|
5
|
+
private readonly iteration
|
|
6
|
+
private readonly chainKey
|
|
7
|
+
private readonly signatureKey
|
|
8
|
+
private readonly serialized
|
|
9
|
+
constructor(id?: number | null, iteration?: number | null, chainKey?: Uint8Array | null, signatureKey?: Uint8Array | null, serialized?: Uint8Array | null)
|
|
10
|
+
private intsToByteHighAndLow
|
|
11
|
+
serialize(): Uint8Array
|
|
12
|
+
getType(): number
|
|
13
|
+
getIteration(): number
|
|
14
|
+
getChainKey(): Uint8Array
|
|
15
|
+
getSignatureKey(): Uint8Array
|
|
16
|
+
getId(): number
|
|
17
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
4
|
+
|
|
5
|
+
const WAProto_1 = require("../../../WAProto")
|
|
6
|
+
const ciphertext_message_1 = require("./ciphertext-message")
|
|
7
|
+
|
|
8
|
+
class SenderKeyDistributionMessage extends ciphertext_message_1.CiphertextMessage {
|
|
9
|
+
constructor(id, iteration, chainKey, signatureKey, serialized) {
|
|
10
|
+
super()
|
|
11
|
+
if (serialized) {
|
|
12
|
+
try {
|
|
13
|
+
const message = serialized.slice(1)
|
|
14
|
+
const distributionMessage = WAProto_1.proto.SenderKeyDistributionMessage.decode(message).toJSON()
|
|
15
|
+
this.serialized = serialized
|
|
16
|
+
this.id = distributionMessage.id
|
|
17
|
+
this.iteration = distributionMessage.iteration
|
|
18
|
+
this.chainKey =
|
|
19
|
+
typeof distributionMessage.chainKey === 'string'
|
|
20
|
+
? Buffer.from(distributionMessage.chainKey, 'base64')
|
|
21
|
+
: distributionMessage.chainKey
|
|
22
|
+
this.signatureKey =
|
|
23
|
+
typeof distributionMessage.signingKey === 'string'
|
|
24
|
+
? Buffer.from(distributionMessage.signingKey, 'base64')
|
|
25
|
+
: distributionMessage.signingKey
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
throw new Error(String(e))
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION)
|
|
33
|
+
this.id = id
|
|
34
|
+
this.iteration = iteration
|
|
35
|
+
this.chainKey = chainKey
|
|
36
|
+
this.signatureKey = signatureKey
|
|
37
|
+
const message = WAProto_1.proto.SenderKeyDistributionMessage.encode(WAProto_1.proto.SenderKeyDistributionMessage.create({
|
|
38
|
+
id,
|
|
39
|
+
iteration,
|
|
40
|
+
chainKey,
|
|
41
|
+
signingKey: this.signatureKey
|
|
42
|
+
})).finish()
|
|
43
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message])
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
intsToByteHighAndLow(highValue, lowValue) {
|
|
47
|
+
return (((highValue << 4) | lowValue) & 0xff) % 256
|
|
48
|
+
}
|
|
49
|
+
serialize() {
|
|
50
|
+
return this.serialized
|
|
51
|
+
}
|
|
52
|
+
getType() {
|
|
53
|
+
return this.SENDERKEY_DISTRIBUTION_TYPE
|
|
54
|
+
}
|
|
55
|
+
getIteration() {
|
|
56
|
+
return this.iteration
|
|
57
|
+
}
|
|
58
|
+
getChainKey() {
|
|
59
|
+
return typeof this.chainKey === 'string' ? Buffer.from(this.chainKey, 'base64') : this.chainKey
|
|
60
|
+
}
|
|
61
|
+
getSignatureKey() {
|
|
62
|
+
return typeof this.signatureKey === 'string' ? Buffer.from(this.signatureKey, 'base64') : this.signatureKey
|
|
63
|
+
}
|
|
64
|
+
getId() {
|
|
65
|
+
return this.id
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = {
|
|
70
|
+
SenderKeyDistributionMessage
|
|
71
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CiphertextMessage } from './ciphertext-message'
|
|
2
|
+
|
|
3
|
+
export declare class SenderKeyMessage extends CiphertextMessage {
|
|
4
|
+
private readonly SIGNATURE_LENGTH
|
|
5
|
+
private readonly messageVersion
|
|
6
|
+
private readonly keyId
|
|
7
|
+
private readonly iteration
|
|
8
|
+
private readonly ciphertext
|
|
9
|
+
private readonly signature
|
|
10
|
+
private readonly serialized
|
|
11
|
+
constructor(keyId?: number | null, iteration?: number | null, ciphertext?: Uint8Array | null, signatureKey?: Uint8Array | null, serialized?: Uint8Array | null)
|
|
12
|
+
getKeyId(): number
|
|
13
|
+
getIteration(): number
|
|
14
|
+
getCipherText(): Uint8Array
|
|
15
|
+
verifySignature(signatureKey: Uint8Array): void
|
|
16
|
+
private getSignature
|
|
17
|
+
serialize(): Uint8Array
|
|
18
|
+
getType(): number
|
|
19
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
3
|
+
|
|
4
|
+
const curve_1 = require("@neelegirl/libsignal/src/curve")
|
|
5
|
+
const WAProto_1 = require("../../../WAProto")
|
|
6
|
+
const ciphertext_message_1 = require("./ciphertext-message")
|
|
7
|
+
|
|
8
|
+
class SenderKeyMessage extends ciphertext_message_1.CiphertextMessage {
|
|
9
|
+
constructor(keyId, iteration, ciphertext, signatureKey, serialized) {
|
|
10
|
+
super()
|
|
11
|
+
this.SIGNATURE_LENGTH = 64
|
|
12
|
+
if (serialized) {
|
|
13
|
+
const version = serialized[0]
|
|
14
|
+
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH)
|
|
15
|
+
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH)
|
|
16
|
+
const senderKeyMessage = WAProto_1.proto.SenderKeyMessage.decode(message).toJSON()
|
|
17
|
+
this.serialized = serialized
|
|
18
|
+
this.messageVersion = (version & 0xff) >> 4
|
|
19
|
+
this.keyId = senderKeyMessage.id
|
|
20
|
+
this.iteration = senderKeyMessage.iteration
|
|
21
|
+
this.ciphertext =
|
|
22
|
+
typeof senderKeyMessage.ciphertext === 'string'
|
|
23
|
+
? Buffer.from(senderKeyMessage.ciphertext, 'base64')
|
|
24
|
+
: senderKeyMessage.ciphertext
|
|
25
|
+
this.signature = signature
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256
|
|
29
|
+
const ciphertextBuffer = Buffer.from(ciphertext)
|
|
30
|
+
const message = WAProto_1.proto.SenderKeyMessage.encode(WAProto_1.proto.SenderKeyMessage.create({
|
|
31
|
+
id: keyId,
|
|
32
|
+
iteration: iteration,
|
|
33
|
+
ciphertext: ciphertextBuffer
|
|
34
|
+
})).finish()
|
|
35
|
+
const signature = this.getSignature(signatureKey, Buffer.concat([Buffer.from([version]), message]))
|
|
36
|
+
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)])
|
|
37
|
+
this.messageVersion = this.CURRENT_VERSION
|
|
38
|
+
this.keyId = keyId
|
|
39
|
+
this.iteration = iteration
|
|
40
|
+
this.ciphertext = ciphertextBuffer
|
|
41
|
+
this.signature = signature
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
getKeyId() {
|
|
45
|
+
return this.keyId
|
|
46
|
+
}
|
|
47
|
+
getIteration() {
|
|
48
|
+
return this.iteration
|
|
49
|
+
}
|
|
50
|
+
getCipherText() {
|
|
51
|
+
return this.ciphertext
|
|
52
|
+
}
|
|
53
|
+
verifySignature(signatureKey) {
|
|
54
|
+
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH)
|
|
55
|
+
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH)
|
|
56
|
+
const res = curve_1.verifySignature(signatureKey, part1, part2)
|
|
57
|
+
if (!res)
|
|
58
|
+
throw new Error('Invalid signature!')
|
|
59
|
+
}
|
|
60
|
+
getSignature(signatureKey, serialized) {
|
|
61
|
+
return Buffer.from(curve_1.calculateSignature(signatureKey, serialized))
|
|
62
|
+
}
|
|
63
|
+
serialize() {
|
|
64
|
+
return this.serialized
|
|
65
|
+
}
|
|
66
|
+
getType() {
|
|
67
|
+
return 4
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = {
|
|
72
|
+
SenderKeyMessage
|
|
73
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface Sender {
|
|
2
|
+
id: string
|
|
3
|
+
deviceId: number
|
|
4
|
+
toString(): string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export declare class SenderKeyName {
|
|
8
|
+
private readonly groupId
|
|
9
|
+
private readonly sender
|
|
10
|
+
constructor(groupId: string, sender: Sender)
|
|
11
|
+
getGroupId(): string
|
|
12
|
+
getSender(): Sender
|
|
13
|
+
serialize(): string
|
|
14
|
+
toString(): string
|
|
15
|
+
equals(other: SenderKeyName | null): boolean
|
|
16
|
+
hashCode(): number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export {}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
4
|
+
|
|
5
|
+
function isNull(str) {
|
|
6
|
+
return str === null || str === ''
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function intValue(num) {
|
|
10
|
+
const MAX_VALUE = 0x7fffffff
|
|
11
|
+
const MIN_VALUE = -0x80000000
|
|
12
|
+
if (num > MAX_VALUE || num < MIN_VALUE) {
|
|
13
|
+
return num & 0xffffffff
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return num
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function hashCode(strKey) {
|
|
20
|
+
let hash = 0
|
|
21
|
+
if (!isNull(strKey)) {
|
|
22
|
+
for (let i = 0; i < strKey.length; i++) {
|
|
23
|
+
hash = hash * 31 + strKey.charCodeAt(i)
|
|
24
|
+
hash = intValue(hash)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return hash
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class SenderKeyName {
|
|
31
|
+
constructor(groupId, sender) {
|
|
32
|
+
this.groupId = groupId
|
|
33
|
+
this.sender = sender
|
|
34
|
+
}
|
|
35
|
+
getGroupId() {
|
|
36
|
+
return this.groupId
|
|
37
|
+
}
|
|
38
|
+
getSender() {
|
|
39
|
+
return this.sender
|
|
40
|
+
}
|
|
41
|
+
serialize() {
|
|
42
|
+
return `${this.groupId}::${this.sender.id}::${this.sender.deviceId}`
|
|
43
|
+
}
|
|
44
|
+
toString() {
|
|
45
|
+
return this.serialize()
|
|
46
|
+
}
|
|
47
|
+
equals(other) {
|
|
48
|
+
if (other === null)
|
|
49
|
+
return false
|
|
50
|
+
return this.groupId === other.groupId && this.sender.toString() === other.sender.toString()
|
|
51
|
+
}
|
|
52
|
+
hashCode() {
|
|
53
|
+
return hashCode(this.groupId) ^ hashCode(this.sender.toString())
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = {
|
|
58
|
+
SenderKeyName
|
|
59
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SenderKeyState } from './sender-key-state'
|
|
2
|
+
|
|
3
|
+
export interface SenderKeyStateStructure {
|
|
4
|
+
senderKeyId: number
|
|
5
|
+
senderChainKey: {
|
|
6
|
+
iteration: number
|
|
7
|
+
seed: Uint8Array
|
|
8
|
+
}
|
|
9
|
+
senderSigningKey: {
|
|
10
|
+
public: Uint8Array
|
|
11
|
+
private?: Uint8Array
|
|
12
|
+
}
|
|
13
|
+
senderMessageKeys: Array<{
|
|
14
|
+
iteration: number
|
|
15
|
+
seed: Uint8Array
|
|
16
|
+
}>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export declare class SenderKeyRecord {
|
|
20
|
+
private readonly MAX_STATES
|
|
21
|
+
private readonly senderKeyStates
|
|
22
|
+
constructor(serialized?: SenderKeyStateStructure[])
|
|
23
|
+
isEmpty(): boolean
|
|
24
|
+
getSenderKeyState(keyId?: number): SenderKeyState | undefined
|
|
25
|
+
addSenderKeyState(id: number, iteration: number, chainKey: Uint8Array, signatureKey: Uint8Array): void
|
|
26
|
+
setSenderKeyState(id: number, iteration: number, chainKey: Uint8Array, keyPair: {
|
|
27
|
+
public: Uint8Array
|
|
28
|
+
private: Uint8Array
|
|
29
|
+
}): void
|
|
30
|
+
serialize(): SenderKeyStateStructure[]
|
|
31
|
+
static deserialize(data: Uint8Array | string | SenderKeyStateStructure[]): SenderKeyRecord
|
|
32
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true })
|
|
4
|
+
|
|
5
|
+
const generics_1 = require("../../Utils/generics")
|
|
6
|
+
const sender_key_state_1 = require("./sender-key-state")
|
|
7
|
+
|
|
8
|
+
class SenderKeyRecord {
|
|
9
|
+
constructor(serialized) {
|
|
10
|
+
this.MAX_STATES = 5
|
|
11
|
+
this.senderKeyStates = []
|
|
12
|
+
if (serialized) {
|
|
13
|
+
for (const structure of serialized) {
|
|
14
|
+
this.senderKeyStates.push(new sender_key_state_1.SenderKeyState(null, null, null, null, null, null, structure))
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
isEmpty() {
|
|
19
|
+
return this.senderKeyStates.length === 0
|
|
20
|
+
}
|
|
21
|
+
getSenderKeyState(keyId) {
|
|
22
|
+
if (keyId === undefined && this.senderKeyStates.length) {
|
|
23
|
+
return this.senderKeyStates[this.senderKeyStates.length - 1]
|
|
24
|
+
}
|
|
25
|
+
return this.senderKeyStates.find(state => state.getKeyId() === keyId)
|
|
26
|
+
}
|
|
27
|
+
addSenderKeyState(id, iteration, chainKey, signatureKey) {
|
|
28
|
+
this.senderKeyStates.push(new sender_key_state_1.SenderKeyState(id, iteration, chainKey, null, signatureKey))
|
|
29
|
+
if (this.senderKeyStates.length > this.MAX_STATES) {
|
|
30
|
+
this.senderKeyStates.shift()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
setSenderKeyState(id, iteration, chainKey, keyPair) {
|
|
34
|
+
this.senderKeyStates.length = 0
|
|
35
|
+
this.senderKeyStates.push(new sender_key_state_1.SenderKeyState(id, iteration, chainKey, keyPair))
|
|
36
|
+
}
|
|
37
|
+
serialize() {
|
|
38
|
+
return this.senderKeyStates.map(state => state.getStructure())
|
|
39
|
+
}
|
|
40
|
+
static deserialize(data) {
|
|
41
|
+
let parsed
|
|
42
|
+
if (typeof data === 'string') {
|
|
43
|
+
parsed = JSON.parse(data, generics_1.BufferJSON.reviver)
|
|
44
|
+
}
|
|
45
|
+
else if (data instanceof Uint8Array) {
|
|
46
|
+
const str = Buffer.from(data).toString('utf-8')
|
|
47
|
+
parsed = JSON.parse(str, generics_1.BufferJSON.reviver)
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
parsed = data
|
|
51
|
+
}
|
|
52
|
+
return new SenderKeyRecord(parsed)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = {
|
|
57
|
+
SenderKeyRecord
|
|
58
|
+
}
|