@nerxa/socketon 0.0.0-stage → 1.21.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/EXPERIMENTAL.md +350 -0
- package/LICENSE +24 -0
- package/README.md +6317 -2
- package/WAProto/index.d.ts +17301 -0
- package/WAProto/index.js +125088 -0
- package/engine-requirements.js +14 -0
- package/package.json +100 -4
- package/src/Defaults/index.d.ts +152 -0
- package/src/Defaults/index.js +164 -0
- package/src/MessageBuilder/bot-signature.d.ts +22 -0
- package/src/MessageBuilder/bot-signature.js +122 -0
- package/src/MessageBuilder/extras.d.ts +449 -0
- package/src/MessageBuilder/extras.js +1356 -0
- package/src/MessageBuilder/index.d.ts +210 -0
- package/src/MessageBuilder/index.js +3594 -0
- package/src/MessageBuilder/metaai.d.ts +438 -0
- package/src/MessageBuilder/metaai.js +890 -0
- package/src/Signal/Group/ciphertext-message.d.ts +9 -0
- package/src/Signal/Group/ciphertext-message.js +11 -0
- package/src/Signal/Group/group-session-builder.d.ts +7 -0
- package/src/Signal/Group/group-session-builder.js +27 -0
- package/src/Signal/Group/group_cipher.d.ts +11 -0
- package/src/Signal/Group/group_cipher.js +78 -0
- package/src/Signal/Group/index.d.ts +11 -0
- package/src/Signal/Group/index.js +11 -0
- package/src/Signal/Group/keyhelper.d.ts +6 -0
- package/src/Signal/Group/keyhelper.js +17 -0
- package/src/Signal/Group/sender-chain-key.d.ts +13 -0
- package/src/Signal/Group/sender-chain-key.js +25 -0
- package/src/Signal/Group/sender-key-distribution-message.d.ts +16 -0
- package/src/Signal/Group/sender-key-distribution-message.js +62 -0
- package/src/Signal/Group/sender-key-message.d.ts +18 -0
- package/src/Signal/Group/sender-key-message.js +65 -0
- package/src/Signal/Group/sender-key-name.d.ts +11 -0
- package/src/Signal/Group/sender-key-name.js +47 -0
- package/src/Signal/Group/sender-key-record.d.ts +12 -0
- package/src/Signal/Group/sender-key-record.js +40 -0
- package/src/Signal/Group/sender-key-state.d.ts +16 -0
- package/src/Signal/Group/sender-key-state.js +83 -0
- package/src/Signal/Group/sender-message-key.d.ts +11 -0
- package/src/Signal/Group/sender-message-key.js +25 -0
- package/src/Signal/libsignal.d.ts +54 -0
- package/src/Signal/libsignal.js +427 -0
- package/src/Signal/lid-mapping.d.ts +20 -0
- package/src/Signal/lid-mapping.js +274 -0
- package/src/Socket/Client/index.d.ts +2 -0
- package/src/Socket/Client/index.js +3 -0
- package/src/Socket/Client/tcp.js +177 -0
- package/src/Socket/Client/types.d.ts +5 -0
- package/src/Socket/Client/types.js +9 -0
- package/src/Socket/Client/websocket.d.ts +12 -0
- package/src/Socket/Client/websocket.js +53 -0
- package/src/Socket/business.d.ts +431 -0
- package/src/Socket/business.js +379 -0
- package/src/Socket/chats.d.ts +247 -0
- package/src/Socket/chats.js +1484 -0
- package/src/Socket/communities.d.ts +598 -0
- package/src/Socket/communities.js +421 -0
- package/src/Socket/email.js +145 -0
- package/src/Socket/groups.d.ts +298 -0
- package/src/Socket/groups.js +339 -0
- package/src/Socket/index.d.ts +581 -0
- package/src/Socket/index.js +37 -0
- package/src/Socket/messages-recv.d.ts +370 -0
- package/src/Socket/messages-recv.js +1864 -0
- package/src/Socket/messages-send.d.ts +390 -0
- package/src/Socket/messages-send.js +1574 -0
- package/src/Socket/mex.d.ts +1 -0
- package/src/Socket/mex.js +44 -0
- package/src/Socket/mobile-builders.js +72 -0
- package/src/Socket/mobile.js +424 -0
- package/src/Socket/newsletter.d.ts +468 -0
- package/src/Socket/newsletter.js +671 -0
- package/src/Socket/socket.d.ts +77 -0
- package/src/Socket/socket.js +1106 -0
- package/src/Store/index.d.ts +3 -0
- package/src/Store/index.js +3 -0
- package/src/Store/make-in-memory-store.d.ts +62 -0
- package/src/Store/make-in-memory-store.js +396 -0
- package/src/Store/make-ordered-dictionary.d.ts +12 -0
- package/src/Store/make-ordered-dictionary.js +78 -0
- package/src/Store/object-repository.d.ts +10 -0
- package/src/Store/object-repository.js +23 -0
- package/src/Types/Auth.d.ts +1 -0
- package/src/Types/Auth.js +1 -0
- package/src/Types/Bussines.d.ts +1 -0
- package/src/Types/Bussines.js +1 -0
- package/src/Types/Call.d.ts +1 -0
- package/src/Types/Call.js +1 -0
- package/src/Types/Chat.d.ts +2 -0
- package/src/Types/Chat.js +24 -0
- package/src/Types/Contact.d.ts +1 -0
- package/src/Types/Contact.js +1 -0
- package/src/Types/Events.d.ts +1 -0
- package/src/Types/Events.js +0 -0
- package/src/Types/GroupMetadata.d.ts +5 -0
- package/src/Types/GroupMetadata.js +6 -0
- package/src/Types/Label.d.ts +2 -0
- package/src/Types/Label.js +24 -0
- package/src/Types/LabelAssociation.d.ts +2 -0
- package/src/Types/LabelAssociation.js +6 -0
- package/src/Types/Message.d.ts +13 -0
- package/src/Types/Message.js +23 -0
- package/src/Types/Mex.d.ts +2 -0
- package/src/Types/Mex.js +95 -0
- package/src/Types/Product.d.ts +1 -0
- package/src/Types/Product.js +1 -0
- package/src/Types/Signal.d.ts +1 -0
- package/src/Types/Signal.js +0 -0
- package/src/Types/Socket.d.ts +1 -0
- package/src/Types/Socket.js +0 -0
- package/src/Types/State.d.ts +5 -0
- package/src/Types/State.js +54 -0
- package/src/Types/USync.d.ts +1 -0
- package/src/Types/USync.js +0 -0
- package/src/Types/index.d.ts +13 -0
- package/src/Types/index.js +25 -0
- package/src/Utils/auth-store.d.ts +29 -0
- package/src/Utils/auth-store.js +76 -0
- package/src/Utils/auth-utils.d.ts +58 -0
- package/src/Utils/auth-utils.js +273 -0
- package/src/Utils/browser-utils.d.ts +9 -0
- package/src/Utils/browser-utils.js +28 -0
- package/src/Utils/business.d.ts +49 -0
- package/src/Utils/business.js +226 -0
- package/src/Utils/chat-utils.d.ts +408 -0
- package/src/Utils/chat-utils.js +846 -0
- package/src/Utils/companion-reg-client-utils.d.ts +4 -0
- package/src/Utils/companion-reg-client-utils.js +39 -0
- package/src/Utils/crypto.d.ts +39 -0
- package/src/Utils/crypto.js +110 -0
- package/src/Utils/decode-wa-message.d.ts +99 -0
- package/src/Utils/decode-wa-message.js +373 -0
- package/src/Utils/event-buffer.d.ts +22 -0
- package/src/Utils/event-buffer.js +624 -0
- package/src/Utils/generics.d.ts +63 -0
- package/src/Utils/generics.js +375 -0
- package/src/Utils/group-metadata-cache.d.ts +4 -0
- package/src/Utils/group-metadata-cache.js +25 -0
- package/src/Utils/history.d.ts +75 -0
- package/src/Utils/history.js +131 -0
- package/src/Utils/html-app.d.ts +19 -0
- package/src/Utils/html-app.js +96 -0
- package/src/Utils/html-multiplayer.d.ts +10 -0
- package/src/Utils/html-multiplayer.js +94 -0
- package/src/Utils/identity-change-handler.d.ts +13 -0
- package/src/Utils/identity-change-handler.js +48 -0
- package/src/Utils/index.d.ts +44 -0
- package/src/Utils/index.js +44 -0
- package/src/Utils/link-preview-metadata.d.ts +47 -0
- package/src/Utils/link-preview-metadata.js +88 -0
- package/src/Utils/link-preview.d.ts +12 -0
- package/src/Utils/link-preview.js +79 -0
- package/src/Utils/logger.d.ts +2 -0
- package/src/Utils/logger.js +2 -0
- package/src/Utils/lt-hash.d.ts +6 -0
- package/src/Utils/lt-hash.js +3 -0
- package/src/Utils/make-mutex.d.ts +6 -0
- package/src/Utils/make-mutex.js +32 -0
- package/src/Utils/message-retry-manager.d.ts +79 -0
- package/src/Utils/message-retry-manager.js +255 -0
- package/src/Utils/messages-media.d.ts +136 -0
- package/src/Utils/messages-media.js +814 -0
- package/src/Utils/messages.d.ts +62 -0
- package/src/Utils/messages.js +2223 -0
- package/src/Utils/mobile-companion-crypto.js +191 -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 +115 -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 +20 -0
- package/src/Utils/noise-handler.js +200 -0
- package/src/Utils/offline-node-processor.d.ts +9 -0
- package/src/Utils/offline-node-processor.js +33 -0
- package/src/Utils/optional-media.d.ts +3 -0
- package/src/Utils/optional-media.js +45 -0
- package/src/Utils/payment-messages.d.ts +66 -0
- package/src/Utils/payment-messages.js +160 -0
- package/src/Utils/pre-key-manager.d.ts +25 -0
- package/src/Utils/pre-key-manager.js +95 -0
- package/src/Utils/process-message.d.ts +60 -0
- package/src/Utils/process-message.js +688 -0
- package/src/Utils/reporting-utils.d.ts +12 -0
- package/src/Utils/reporting-utils.js +256 -0
- package/src/Utils/scheduled-message.d.ts +51 -0
- package/src/Utils/scheduled-message.js +112 -0
- package/src/Utils/sender-key-memory.d.ts +19 -0
- package/src/Utils/sender-key-memory.js +51 -0
- package/src/Utils/signal.d.ts +82 -0
- package/src/Utils/signal.js +193 -0
- package/src/Utils/stanza-ack.d.ts +16 -0
- package/src/Utils/stanza-ack.js +30 -0
- package/src/Utils/status-stickers.d.ts +127 -0
- package/src/Utils/status-stickers.js +260 -0
- package/src/Utils/sync-action-utils.d.ts +2 -0
- package/src/Utils/sync-action-utils.js +44 -0
- package/src/Utils/tc-token-utils.d.ts +29 -0
- package/src/Utils/tc-token-utils.js +155 -0
- package/src/Utils/ttl-cache.d.ts +28 -0
- package/src/Utils/ttl-cache.js +120 -0
- package/src/Utils/use-mongo-auth-state.d.ts +21 -0
- package/src/Utils/use-mongo-auth-state.js +67 -0
- package/src/Utils/use-multi-file-auth-state.d.ts +11 -0
- package/src/Utils/use-multi-file-auth-state.js +106 -0
- package/src/Utils/use-mysql-auth-state.d.ts +19 -0
- package/src/Utils/use-mysql-auth-state.js +79 -0
- package/src/Utils/use-postgres-auth-state.d.ts +19 -0
- package/src/Utils/use-postgres-auth-state.js +79 -0
- package/src/Utils/use-redis-auth-state.d.ts +19 -0
- package/src/Utils/use-redis-auth-state.js +89 -0
- package/src/Utils/use-single-file-auth-state.d.ts +10 -0
- package/src/Utils/use-single-file-auth-state.js +108 -0
- package/src/Utils/use-sqlite-auth-state.d.ts +12 -0
- package/src/Utils/use-sqlite-auth-state.js +120 -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 +58 -0
- package/src/Utils/validate-connection.d.ts +43 -0
- package/src/Utils/validate-connection.js +277 -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 +24 -0
- package/src/Voip/audio-feeder.js +268 -0
- package/src/Voip/index.d.ts +88 -0
- package/src/Voip/index.js +936 -0
- package/src/Voip/relay-transport.d.ts +5 -0
- package/src/Voip/relay-transport.js +824 -0
- package/src/Voip/signaling.d.ts +11 -0
- package/src/Voip/signaling.js +1099 -0
- package/src/Voip/types.d.ts +2 -0
- package/src/Voip/types.js +12 -0
- package/src/Voip/video-feeder.d.ts +24 -0
- package/src/Voip/video-feeder.js +268 -0
- package/src/Voip/wasm-engine.d.ts +26 -0
- package/src/Voip/wasm-engine.js +1307 -0
- package/src/Voip/worker-bootstrap.js +1023 -0
- package/src/WABinary/constants.d.ts +22 -0
- package/src/WABinary/constants.js +1300 -0
- package/src/WABinary/decode.d.ts +9 -0
- package/src/WABinary/decode.js +261 -0
- package/src/WABinary/encode.d.ts +2 -0
- package/src/WABinary/encode.js +219 -0
- package/src/WABinary/generic-utils.d.ts +76 -0
- package/src/WABinary/generic-utils.js +214 -0
- package/src/WABinary/index.d.ts +5 -0
- package/src/WABinary/index.js +5 -0
- package/src/WABinary/jid-utils.d.ts +30 -0
- package/src/WABinary/jid-utils.js +97 -0
- package/src/WABinary/types.d.ts +1 -0
- package/src/WABinary/types.js +0 -0
- package/src/WAM/BinaryInfo.d.ts +7 -0
- package/src/WAM/BinaryInfo.js +9 -0
- package/src/WAM/constants.d.ts +34926 -0
- package/src/WAM/constants.js +22852 -0
- package/src/WAM/encode.d.ts +1 -0
- package/src/WAM/encode.js +148 -0
- package/src/WAM/index.d.ts +3 -0
- package/src/WAM/index.js +3 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.d.ts +36 -0
- package/src/WAUSync/Protocols/USyncContactProtocol.js +50 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.d.ts +22 -0
- package/src/WAUSync/Protocols/USyncDeviceProtocol.js +51 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +12 -0
- package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.js +26 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.d.ts +12 -0
- package/src/WAUSync/Protocols/USyncStatusProtocol.js +37 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.d.ts +9 -0
- package/src/WAUSync/Protocols/USyncUsernameProtocol.js +23 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +38 -0
- package/src/WAUSync/Protocols/UsyncBotProfileProtocol.js +49 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.d.ts +14 -0
- package/src/WAUSync/Protocols/UsyncLIDProtocol.js +28 -0
- package/src/WAUSync/Protocols/index.d.ts +5 -0
- package/src/WAUSync/Protocols/index.js +5 -0
- package/src/WAUSync/USyncQuery.d.ts +20 -0
- package/src/WAUSync/USyncQuery.js +93 -0
- package/src/WAUSync/USyncUser.d.ts +16 -0
- package/src/WAUSync/USyncUser.js +30 -0
- package/src/WAUSync/index.d.ts +3 -0
- package/src/WAUSync/index.js +3 -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 +43 -0
- package/src/index.js +48 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export function makeLibSignalRepository(auth: any, logger: any, pnToLIDFunc: any): {
|
|
2
|
+
decryptGroupMessage({ group, authorJid, msg }: {
|
|
3
|
+
group: any;
|
|
4
|
+
authorJid: any;
|
|
5
|
+
msg: any;
|
|
6
|
+
}): any;
|
|
7
|
+
processSenderKeyDistributionMessage({ item, authorJid }: {
|
|
8
|
+
item: any;
|
|
9
|
+
authorJid: any;
|
|
10
|
+
}): Promise<any>;
|
|
11
|
+
decryptMessage({ jid, type, ciphertext }: {
|
|
12
|
+
jid: any;
|
|
13
|
+
type: any;
|
|
14
|
+
ciphertext: any;
|
|
15
|
+
}): Promise<any>;
|
|
16
|
+
encryptMessage({ jid, data }: {
|
|
17
|
+
jid: any;
|
|
18
|
+
data: any;
|
|
19
|
+
}): Promise<any>;
|
|
20
|
+
encryptGroupMessage({ group, meId, data }: {
|
|
21
|
+
group: any;
|
|
22
|
+
meId: any;
|
|
23
|
+
data: any;
|
|
24
|
+
}): Promise<any>;
|
|
25
|
+
getSenderKeyDistributionMessage({ group, meId }: {
|
|
26
|
+
group: any;
|
|
27
|
+
meId: any;
|
|
28
|
+
}): Promise<any>;
|
|
29
|
+
hasSenderKey({ group, meId }: {
|
|
30
|
+
group: any;
|
|
31
|
+
meId: any;
|
|
32
|
+
}): Promise<boolean>;
|
|
33
|
+
getSessionInfo(jid: any): Promise<{
|
|
34
|
+
baseKey: Uint8Array<any>;
|
|
35
|
+
registrationId: number;
|
|
36
|
+
} | null>;
|
|
37
|
+
injectE2ESession({ jid, session }: {
|
|
38
|
+
jid: any;
|
|
39
|
+
session: any;
|
|
40
|
+
}): Promise<any>;
|
|
41
|
+
jidToSignalProtocolAddress(jid: any): any;
|
|
42
|
+
lidMapping: LIDMappingStore;
|
|
43
|
+
validateSession(jid: any): Promise<{
|
|
44
|
+
exists: boolean;
|
|
45
|
+
reason: string;
|
|
46
|
+
} | {
|
|
47
|
+
exists: boolean;
|
|
48
|
+
reason?: undefined;
|
|
49
|
+
}>;
|
|
50
|
+
deleteSession(jids: any): Promise<any>;
|
|
51
|
+
close(): void;
|
|
52
|
+
migrateSession(fromJid: any, toJid: any): Promise<any>;
|
|
53
|
+
};
|
|
54
|
+
import { LIDMappingStore } from './lid-mapping.js';
|
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
import * as libsignal from 'libsignal';
|
|
2
|
+
|
|
3
|
+
import { PreKeyWhisperMessage } from 'libsignal/src/protobufs.js';
|
|
4
|
+
import { LRUCache } from 'lru-cache';
|
|
5
|
+
import { generateSignalPubKey } from '../Utils/index.js';
|
|
6
|
+
import { isHostedLidUser, isHostedPnUser, isLidUser, isPnUser, jidDecode, transferDevice, WAJIDDomains } from '../WABinary/index.js';
|
|
7
|
+
import { SenderKeyName } from './Group/sender-key-name.js';
|
|
8
|
+
import { SenderKeyRecord } from './Group/sender-key-record.js';
|
|
9
|
+
import { GroupCipher, GroupSessionBuilder, SenderKeyDistributionMessage } from './Group/index.js';
|
|
10
|
+
import { LIDMappingStore } from './lid-mapping.js';
|
|
11
|
+
|
|
12
|
+
function extractIdentityFromPkmsg(ciphertext) {
|
|
13
|
+
try {
|
|
14
|
+
if (!ciphertext || ciphertext.length < 2) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const version = ciphertext[0];
|
|
19
|
+
if ((version & 0xf) !== 3) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const preKeyProto = PreKeyWhisperMessage.decode(ciphertext.slice(1));
|
|
24
|
+
if (preKeyProto.identityKey?.length === 33) {
|
|
25
|
+
return new Uint8Array(preKeyProto.identityKey);
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function makeLibSignalRepository(auth, logger, pnToLIDFunc) {
|
|
34
|
+
const lidMapping = new LIDMappingStore(auth.keys, logger, pnToLIDFunc);
|
|
35
|
+
const storage = signalStorage(auth, lidMapping);
|
|
36
|
+
const parsedKeys = auth.keys;
|
|
37
|
+
const migratedSessionCache = new LRUCache({
|
|
38
|
+
ttl: 3 * 24 * 60 * 60 * 1000,
|
|
39
|
+
ttlAutopurge: true,
|
|
40
|
+
updateAgeOnGet: true
|
|
41
|
+
});
|
|
42
|
+
const ensureSenderKeyAndCreateSkdm = async (group, meId) => {
|
|
43
|
+
const senderName = jidToSignalSenderKeyName(group, meId);
|
|
44
|
+
const senderNameStr = senderName.toString();
|
|
45
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
46
|
+
if (!senderKey) {
|
|
47
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
48
|
+
}
|
|
49
|
+
const skdm = await new GroupSessionBuilder(storage).create(senderName);
|
|
50
|
+
return { senderName, skdm };
|
|
51
|
+
};
|
|
52
|
+
const repository = {
|
|
53
|
+
decryptGroupMessage({ group, authorJid, msg }) {
|
|
54
|
+
const senderName = jidToSignalSenderKeyName(group, authorJid);
|
|
55
|
+
const cipher = new GroupCipher(storage, senderName);
|
|
56
|
+
|
|
57
|
+
return parsedKeys.transaction(async () => {
|
|
58
|
+
return cipher.decrypt(msg);
|
|
59
|
+
}, group);
|
|
60
|
+
},
|
|
61
|
+
async processSenderKeyDistributionMessage({ item, authorJid }) {
|
|
62
|
+
const builder = new GroupSessionBuilder(storage);
|
|
63
|
+
if (!item.groupId) {
|
|
64
|
+
throw new Error('Group ID is required for sender key distribution message');
|
|
65
|
+
}
|
|
66
|
+
const senderName = jidToSignalSenderKeyName(item.groupId, authorJid);
|
|
67
|
+
const senderMsg = new SenderKeyDistributionMessage(null, null, null, null, item.axolotlSenderKeyDistributionMessage);
|
|
68
|
+
const senderNameStr = senderName.toString();
|
|
69
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
70
|
+
if (!senderKey) {
|
|
71
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
72
|
+
}
|
|
73
|
+
return parsedKeys.transaction(async () => {
|
|
74
|
+
const { [senderNameStr]: senderKey } = await auth.keys.get('sender-key', [senderNameStr]);
|
|
75
|
+
if (!senderKey) {
|
|
76
|
+
await storage.storeSenderKey(senderName, new SenderKeyRecord());
|
|
77
|
+
}
|
|
78
|
+
await builder.process(senderName, senderMsg);
|
|
79
|
+
}, item.groupId);
|
|
80
|
+
},
|
|
81
|
+
async decryptMessage({ jid, type, ciphertext }) {
|
|
82
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
83
|
+
const session = new libsignal.SessionCipher(storage, addr);
|
|
84
|
+
|
|
85
|
+
if (type === 'pkmsg') {
|
|
86
|
+
const identityKey = extractIdentityFromPkmsg(ciphertext);
|
|
87
|
+
if (identityKey) {
|
|
88
|
+
const addrStr = addr.toString();
|
|
89
|
+
const identityChanged = await storage.saveIdentity(addrStr, identityKey);
|
|
90
|
+
if (identityChanged) {
|
|
91
|
+
logger.info({ jid, addr: addrStr }, 'identity key changed or new contact, session will be re-established');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function doDecrypt() {
|
|
96
|
+
let result;
|
|
97
|
+
switch (type) {
|
|
98
|
+
case 'pkmsg':
|
|
99
|
+
result = await session.decryptPreKeyWhisperMessage(ciphertext);
|
|
100
|
+
break;
|
|
101
|
+
case 'msg':
|
|
102
|
+
result = await session.decryptWhisperMessage(ciphertext);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
return result;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return parsedKeys.transaction(async () => {
|
|
109
|
+
return await doDecrypt();
|
|
110
|
+
}, jid);
|
|
111
|
+
},
|
|
112
|
+
async encryptMessage({ jid, data }) {
|
|
113
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
114
|
+
const cipher = new libsignal.SessionCipher(storage, addr);
|
|
115
|
+
|
|
116
|
+
return parsedKeys.transaction(async () => {
|
|
117
|
+
const { type: sigType, body } = await cipher.encrypt(data);
|
|
118
|
+
const type = sigType === 3 ? 'pkmsg' : 'msg';
|
|
119
|
+
return { type, ciphertext: Buffer.from(body, 'binary') };
|
|
120
|
+
}, jid);
|
|
121
|
+
},
|
|
122
|
+
async encryptGroupMessage({ group, meId, data }) {
|
|
123
|
+
return parsedKeys.transaction(async () => {
|
|
124
|
+
const { senderName, skdm } = await ensureSenderKeyAndCreateSkdm(group, meId);
|
|
125
|
+
const ciphertext = await new GroupCipher(storage, senderName).encrypt(data);
|
|
126
|
+
return { ciphertext, senderKeyDistributionMessage: skdm.serialize() };
|
|
127
|
+
}, group);
|
|
128
|
+
},
|
|
129
|
+
async getSenderKeyDistributionMessage({ group, meId }) {
|
|
130
|
+
return parsedKeys.transaction(async () => {
|
|
131
|
+
const { skdm } = await ensureSenderKeyAndCreateSkdm(group, meId);
|
|
132
|
+
return skdm.serialize();
|
|
133
|
+
}, group);
|
|
134
|
+
},
|
|
135
|
+
async hasSenderKey({ group, meId }) {
|
|
136
|
+
const senderName = jidToSignalSenderKeyName(group, meId).toString();
|
|
137
|
+
const { [senderName]: key } = await auth.keys.get('sender-key', [senderName]);
|
|
138
|
+
return !!key;
|
|
139
|
+
},
|
|
140
|
+
async getSessionInfo(jid) {
|
|
141
|
+
const addr = jidToSignalProtocolAddress(jid).toString();
|
|
142
|
+
const session = (await storage.loadSession(addr));
|
|
143
|
+
if (!session) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
const open = session.getOpenSession?.();
|
|
147
|
+
const baseKey = open?.indexInfo?.baseKey;
|
|
148
|
+
const registrationId = open?.registrationId;
|
|
149
|
+
if (!baseKey || typeof registrationId !== 'number') {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
return { baseKey: new Uint8Array(baseKey), registrationId };
|
|
153
|
+
},
|
|
154
|
+
async injectE2ESession({ jid, session }) {
|
|
155
|
+
logger.trace({ jid }, 'injecting E2EE session');
|
|
156
|
+
const cipher = new libsignal.SessionBuilder(storage, jidToSignalProtocolAddress(jid));
|
|
157
|
+
return parsedKeys.transaction(async () => {
|
|
158
|
+
|
|
159
|
+
await cipher.initOutgoing(session);
|
|
160
|
+
}, jid);
|
|
161
|
+
},
|
|
162
|
+
jidToSignalProtocolAddress(jid) {
|
|
163
|
+
return jidToSignalProtocolAddress(jid).toString();
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
lidMapping,
|
|
167
|
+
async validateSession(jid) {
|
|
168
|
+
try {
|
|
169
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
170
|
+
const session = await storage.loadSession(addr.toString());
|
|
171
|
+
if (!session) {
|
|
172
|
+
return { exists: false, reason: 'no session' };
|
|
173
|
+
}
|
|
174
|
+
if (!session.haveOpenSession()) {
|
|
175
|
+
return { exists: false, reason: 'no open session' };
|
|
176
|
+
}
|
|
177
|
+
return { exists: true };
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
return { exists: false, reason: 'validation error' };
|
|
181
|
+
}
|
|
182
|
+
},
|
|
183
|
+
async deleteSession(jids) {
|
|
184
|
+
if (!jids.length)
|
|
185
|
+
return;
|
|
186
|
+
|
|
187
|
+
const sessionUpdates = {};
|
|
188
|
+
jids.forEach(jid => {
|
|
189
|
+
const addr = jidToSignalProtocolAddress(jid);
|
|
190
|
+
sessionUpdates[addr.toString()] = null;
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
return parsedKeys.transaction(async () => {
|
|
194
|
+
await auth.keys.set({ session: sessionUpdates });
|
|
195
|
+
}, `delete-${jids.length}-sessions`);
|
|
196
|
+
},
|
|
197
|
+
close() {
|
|
198
|
+
migratedSessionCache.clear();
|
|
199
|
+
lidMapping.close();
|
|
200
|
+
},
|
|
201
|
+
async migrateSession(fromJid, toJid) {
|
|
202
|
+
|
|
203
|
+
if (!fromJid || (!isLidUser(toJid) && !isHostedLidUser(toJid)))
|
|
204
|
+
return { migrated: 0, skipped: 0, total: 0 };
|
|
205
|
+
|
|
206
|
+
if (!isPnUser(fromJid) && !isHostedPnUser(fromJid)) {
|
|
207
|
+
return { migrated: 0, skipped: 0, total: 1 };
|
|
208
|
+
}
|
|
209
|
+
const { user } = jidDecode(fromJid);
|
|
210
|
+
logger.debug({ fromJid }, 'bulk device migration - loading all user devices');
|
|
211
|
+
|
|
212
|
+
const { [user]: userDevices } = await parsedKeys.get('device-list', [user]);
|
|
213
|
+
if (!userDevices) {
|
|
214
|
+
return { migrated: 0, skipped: 0, total: 0 };
|
|
215
|
+
}
|
|
216
|
+
const { device: fromDevice } = jidDecode(fromJid);
|
|
217
|
+
const fromDeviceStr = fromDevice?.toString() || '0';
|
|
218
|
+
if (!userDevices.includes(fromDeviceStr)) {
|
|
219
|
+
userDevices.push(fromDeviceStr);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const uncachedDevices = userDevices.filter(device => {
|
|
223
|
+
const deviceKey = `${user}.${device}`;
|
|
224
|
+
return !migratedSessionCache.has(deviceKey);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
const deviceSessionKeys = uncachedDevices.map(device => `${user}.${device}`);
|
|
228
|
+
const existingSessions = await parsedKeys.get('session', deviceSessionKeys);
|
|
229
|
+
|
|
230
|
+
const deviceJids = [];
|
|
231
|
+
for (const [sessionKey, sessionData] of Object.entries(existingSessions)) {
|
|
232
|
+
if (sessionData) {
|
|
233
|
+
|
|
234
|
+
const deviceStr = sessionKey.split('.')[1];
|
|
235
|
+
if (!deviceStr)
|
|
236
|
+
continue;
|
|
237
|
+
const deviceNum = parseInt(deviceStr);
|
|
238
|
+
let jid = deviceNum === 0 ? `${user}@s.whatsapp.net` : `${user}:${deviceNum}@s.whatsapp.net`;
|
|
239
|
+
if (deviceNum === 99) {
|
|
240
|
+
jid = `${user}:99@hosted`;
|
|
241
|
+
}
|
|
242
|
+
deviceJids.push(jid);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
logger.debug({
|
|
246
|
+
fromJid,
|
|
247
|
+
totalDevices: userDevices.length,
|
|
248
|
+
devicesWithSessions: deviceJids.length,
|
|
249
|
+
devices: deviceJids
|
|
250
|
+
}, 'bulk device migration complete - all user devices processed');
|
|
251
|
+
|
|
252
|
+
return parsedKeys.transaction(async () => {
|
|
253
|
+
const migrationOps = deviceJids.map(jid => {
|
|
254
|
+
const lidWithDevice = transferDevice(jid, toJid);
|
|
255
|
+
const fromDecoded = jidDecode(jid);
|
|
256
|
+
const toDecoded = jidDecode(lidWithDevice);
|
|
257
|
+
return {
|
|
258
|
+
fromJid: jid,
|
|
259
|
+
toJid: lidWithDevice,
|
|
260
|
+
pnUser: fromDecoded.user,
|
|
261
|
+
lidUser: toDecoded.user,
|
|
262
|
+
deviceId: fromDecoded.device || 0,
|
|
263
|
+
fromAddr: jidToSignalProtocolAddress(jid),
|
|
264
|
+
toAddr: jidToSignalProtocolAddress(lidWithDevice)
|
|
265
|
+
};
|
|
266
|
+
});
|
|
267
|
+
const totalOps = migrationOps.length;
|
|
268
|
+
let migratedCount = 0;
|
|
269
|
+
|
|
270
|
+
const pnAddrStrings = Array.from(new Set(migrationOps.map(op => op.fromAddr.toString())));
|
|
271
|
+
const pnSessions = await parsedKeys.get('session', pnAddrStrings);
|
|
272
|
+
|
|
273
|
+
const sessionUpdates = {};
|
|
274
|
+
for (const op of migrationOps) {
|
|
275
|
+
const pnAddrStr = op.fromAddr.toString();
|
|
276
|
+
const lidAddrStr = op.toAddr.toString();
|
|
277
|
+
const pnSession = pnSessions[pnAddrStr];
|
|
278
|
+
if (pnSession) {
|
|
279
|
+
|
|
280
|
+
const fromSession = libsignal.SessionRecord.deserialize(pnSession);
|
|
281
|
+
if (fromSession.haveOpenSession()) {
|
|
282
|
+
|
|
283
|
+
sessionUpdates[lidAddrStr] = fromSession.serialize();
|
|
284
|
+
sessionUpdates[pnAddrStr] = null;
|
|
285
|
+
migratedCount++;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (Object.keys(sessionUpdates).length > 0) {
|
|
291
|
+
await parsedKeys.set({ session: sessionUpdates });
|
|
292
|
+
logger.debug({ migratedSessions: migratedCount }, 'bulk session migration complete');
|
|
293
|
+
|
|
294
|
+
for (const op of migrationOps) {
|
|
295
|
+
if (sessionUpdates[op.toAddr.toString()]) {
|
|
296
|
+
const deviceKey = `${op.pnUser}.${op.deviceId}`;
|
|
297
|
+
migratedSessionCache.set(deviceKey, true);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const skippedCount = totalOps - migratedCount;
|
|
302
|
+
return { migrated: migratedCount, skipped: skippedCount, total: totalOps };
|
|
303
|
+
}, `migrate-${deviceJids.length}-sessions-${jidDecode(toJid)?.user}`);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
return repository;
|
|
307
|
+
}
|
|
308
|
+
const jidToSignalProtocolAddress = (jid) => {
|
|
309
|
+
const decoded = jidDecode(jid);
|
|
310
|
+
const { user, device, server, domainType } = decoded;
|
|
311
|
+
if (!user) {
|
|
312
|
+
throw new Error(`JID decoded but user is empty: "${jid}" -> user: "${user}", server: "${server}", device: ${device}`);
|
|
313
|
+
}
|
|
314
|
+
const signalUser = domainType !== WAJIDDomains.WHATSAPP ? `${user}_${domainType}` : user;
|
|
315
|
+
const finalDevice = device || 0;
|
|
316
|
+
if (device === 99 && decoded.server !== 'hosted' && decoded.server !== 'hosted.lid') {
|
|
317
|
+
throw new Error('Unexpected non-hosted device JID with device 99. This ID seems invalid. ID:' + jid);
|
|
318
|
+
}
|
|
319
|
+
return new libsignal.ProtocolAddress(signalUser, finalDevice);
|
|
320
|
+
};
|
|
321
|
+
const jidToSignalSenderKeyName = (group, user) => {
|
|
322
|
+
return new SenderKeyName(group, jidToSignalProtocolAddress(user));
|
|
323
|
+
};
|
|
324
|
+
function signalStorage({ creds, keys }, lidMapping) {
|
|
325
|
+
|
|
326
|
+
const resolveLIDSignalAddress = async (id) => {
|
|
327
|
+
if (id.includes('.')) {
|
|
328
|
+
const [deviceId, device] = id.split('.');
|
|
329
|
+
const [user, domainType_] = deviceId.split('_');
|
|
330
|
+
const domainType = parseInt(domainType_ || '0');
|
|
331
|
+
if (domainType === WAJIDDomains.LID || domainType === WAJIDDomains.HOSTED_LID)
|
|
332
|
+
return id;
|
|
333
|
+
const pnJid = `${user}${device !== '0' ? `:${device}` : ''}@${domainType === WAJIDDomains.HOSTED ? 'hosted' : 's.whatsapp.net'}`;
|
|
334
|
+
const lidForPN = await lidMapping.getLIDForPN(pnJid);
|
|
335
|
+
if (lidForPN) {
|
|
336
|
+
const lidAddr = jidToSignalProtocolAddress(lidForPN);
|
|
337
|
+
return lidAddr.toString();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return id;
|
|
341
|
+
};
|
|
342
|
+
return {
|
|
343
|
+
loadSession: async (id) => {
|
|
344
|
+
try {
|
|
345
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
346
|
+
const { [wireJid]: sess } = await keys.get('session', [wireJid]);
|
|
347
|
+
if (sess) {
|
|
348
|
+
return libsignal.SessionRecord.deserialize(sess);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
catch (e) {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
return null;
|
|
355
|
+
},
|
|
356
|
+
storeSession: async (id, session) => {
|
|
357
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
358
|
+
await keys.set({ session: { [wireJid]: session.serialize() } });
|
|
359
|
+
},
|
|
360
|
+
isTrustedIdentity: () => {
|
|
361
|
+
return true;
|
|
362
|
+
},
|
|
363
|
+
loadIdentityKey: async (id) => {
|
|
364
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
365
|
+
const { [wireJid]: key } = await keys.get('identity-key', [wireJid]);
|
|
366
|
+
return key || undefined;
|
|
367
|
+
},
|
|
368
|
+
saveIdentity: async (id, identityKey) => {
|
|
369
|
+
const wireJid = await resolveLIDSignalAddress(id);
|
|
370
|
+
const { [wireJid]: existingKey } = await keys.get('identity-key', [wireJid]);
|
|
371
|
+
const keysMatch = existingKey?.length === identityKey.length && existingKey.every((byte, i) => byte === identityKey[i]);
|
|
372
|
+
if (existingKey && !keysMatch) {
|
|
373
|
+
|
|
374
|
+
await keys.set({
|
|
375
|
+
session: { [wireJid]: null },
|
|
376
|
+
'identity-key': { [wireJid]: identityKey }
|
|
377
|
+
});
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
if (!existingKey) {
|
|
381
|
+
|
|
382
|
+
await keys.set({ 'identity-key': { [wireJid]: identityKey } });
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
return false;
|
|
386
|
+
},
|
|
387
|
+
loadPreKey: async (id) => {
|
|
388
|
+
const keyId = id.toString();
|
|
389
|
+
const { [keyId]: key } = await keys.get('pre-key', [keyId]);
|
|
390
|
+
if (key) {
|
|
391
|
+
return {
|
|
392
|
+
privKey: Buffer.from(key.private),
|
|
393
|
+
pubKey: Buffer.from(key.public)
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
},
|
|
397
|
+
removePreKey: (id) => keys.set({ 'pre-key': { [id]: null } }),
|
|
398
|
+
loadSignedPreKey: () => {
|
|
399
|
+
const key = creds.signedPreKey;
|
|
400
|
+
return {
|
|
401
|
+
privKey: Buffer.from(key.keyPair.private),
|
|
402
|
+
pubKey: Buffer.from(key.keyPair.public)
|
|
403
|
+
};
|
|
404
|
+
},
|
|
405
|
+
loadSenderKey: async (senderKeyName) => {
|
|
406
|
+
const keyId = senderKeyName.toString();
|
|
407
|
+
const { [keyId]: key } = await keys.get('sender-key', [keyId]);
|
|
408
|
+
if (key) {
|
|
409
|
+
return SenderKeyRecord.deserialize(key);
|
|
410
|
+
}
|
|
411
|
+
return new SenderKeyRecord();
|
|
412
|
+
},
|
|
413
|
+
storeSenderKey: async (senderKeyName, key) => {
|
|
414
|
+
const keyId = senderKeyName.toString();
|
|
415
|
+
const serialized = JSON.stringify(key.serialize());
|
|
416
|
+
await keys.set({ 'sender-key': { [keyId]: Buffer.from(serialized, 'utf-8') } });
|
|
417
|
+
},
|
|
418
|
+
getOurRegistrationId: () => creds.registrationId,
|
|
419
|
+
getOurIdentity: () => {
|
|
420
|
+
const { signedIdentityKey } = creds;
|
|
421
|
+
return {
|
|
422
|
+
privKey: Buffer.from(signedIdentityKey.private),
|
|
423
|
+
pubKey: Buffer.from(generateSignalPubKey(signedIdentityKey.public))
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export class LIDMappingStore {
|
|
2
|
+
constructor(keys: any, logger: any, pnToLIDFunc: any);
|
|
3
|
+
mappingCache: any;
|
|
4
|
+
inflightLIDLookups: Map<any, any>;
|
|
5
|
+
inflightPNLookups: Map<any, any>;
|
|
6
|
+
keys: any;
|
|
7
|
+
pnToLIDFunc: any;
|
|
8
|
+
logger: any;
|
|
9
|
+
storeLIDPNMappings(pairs: any): Promise<void>;
|
|
10
|
+
getLIDForPN(pn: any): Promise<any>;
|
|
11
|
+
getLIDsForPNs(pns: any): Promise<any>;
|
|
12
|
+
_getLIDsForPNsImpl(pns: any): Promise<any[] | null>;
|
|
13
|
+
getPNForLID(lid: any): Promise<any>;
|
|
14
|
+
getPNsForLIDs(lids: any): Promise<any>;
|
|
15
|
+
_getPNsForLIDsImpl(lids: any): Promise<any[] | null>;
|
|
16
|
+
/**
|
|
17
|
+
* Close the cache and release resources
|
|
18
|
+
*/
|
|
19
|
+
close(): void;
|
|
20
|
+
}
|