@sgintokic/baileys 0.0.2
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.
Potentially problematic release.
This version of @sgintokic/baileys might be problematic. Click here for more details.
- package/LICENSE +22 -0
- package/README.md +1097 -0
- package/WAProto/index.js +2 -0
- package/engine-requirements.js +1 -0
- package/lib/Defaults/index.js +155 -0
- package/lib/Signal/Group/ciphertext-message.js +11 -0
- package/lib/Signal/Group/group-session-builder.js +41 -0
- package/lib/Signal/Group/group_cipher.js +108 -0
- package/lib/Signal/Group/index.js +11 -0
- package/lib/Signal/Group/keyhelper.js +14 -0
- package/lib/Signal/Group/sender-chain-key.js +31 -0
- package/lib/Signal/Group/sender-key-distribution-message.js +66 -0
- package/lib/Signal/Group/sender-key-message.js +79 -0
- package/lib/Signal/Group/sender-key-name.js +49 -0
- package/lib/Signal/Group/sender-key-record.js +46 -0
- package/lib/Signal/Group/sender-key-state.js +104 -0
- package/lib/Signal/Group/sender-message-key.js +29 -0
- package/lib/Signal/libsignal.js +485 -0
- package/lib/Signal/lid-mapping.js +291 -0
- package/lib/Socket/Client/index.js +2 -0
- package/lib/Socket/Client/types.js +10 -0
- package/lib/Socket/Client/websocket.js +64 -0
- package/lib/Socket/business.js +293 -0
- package/lib/Socket/chats.js +1068 -0
- package/lib/Socket/communities.js +476 -0
- package/lib/Socket/groups.js +383 -0
- package/lib/Socket/index.js +8 -0
- package/lib/Socket/messages-recv.js +1830 -0
- package/lib/Socket/messages-send.js +1462 -0
- package/lib/Socket/mex.js +55 -0
- package/lib/Socket/newsletter.js +277 -0
- package/lib/Socket/socket.js +1087 -0
- package/lib/Store/index.js +3 -0
- package/lib/Store/make-in-memory-store.js +517 -0
- package/lib/Store/make-ordered-dictionary.js +75 -0
- package/lib/Store/object-repository.js +23 -0
- package/lib/Types/Auth.js +1 -0
- package/lib/Types/Bussines.js +1 -0
- package/lib/Types/Call.js +1 -0
- package/lib/Types/Chat.js +7 -0
- package/lib/Types/Contact.js +1 -0
- package/lib/Types/Events.js +1 -0
- package/lib/Types/GroupMetadata.js +1 -0
- package/lib/Types/Label.js +24 -0
- package/lib/Types/LabelAssociation.js +6 -0
- package/lib/Types/Message.js +18 -0
- package/lib/Types/Newsletter.js +33 -0
- package/lib/Types/Product.js +1 -0
- package/lib/Types/Signal.js +1 -0
- package/lib/Types/Socket.js +2 -0
- package/lib/Types/State.js +15 -0
- package/lib/Types/USync.js +1 -0
- package/lib/Types/index.js +31 -0
- package/lib/Utils/auth-utils.js +293 -0
- package/lib/Utils/browser-utils.js +32 -0
- package/lib/Utils/business.js +245 -0
- package/lib/Utils/chat-utils.js +959 -0
- package/lib/Utils/crypto.js +133 -0
- package/lib/Utils/decode-wa-message.js +376 -0
- package/lib/Utils/event-buffer.js +620 -0
- package/lib/Utils/generics.js +417 -0
- package/lib/Utils/history.js +150 -0
- package/lib/Utils/identity-change-handler.js +63 -0
- package/lib/Utils/index.js +21 -0
- package/lib/Utils/link-preview.js +91 -0
- package/lib/Utils/logger.js +2 -0
- package/lib/Utils/lt-hash.js +6 -0
- package/lib/Utils/make-mutex.js +31 -0
- package/lib/Utils/message-retry-manager.js +240 -0
- package/lib/Utils/messages-media.js +901 -0
- package/lib/Utils/messages.js +2052 -0
- package/lib/Utils/noise-handler.js +229 -0
- package/lib/Utils/offline-node-processor.js +50 -0
- package/lib/Utils/pre-key-manager.js +119 -0
- package/lib/Utils/process-message.js +641 -0
- package/lib/Utils/reporting-utils.js +346 -0
- package/lib/Utils/signal.js +188 -0
- package/lib/Utils/stanza-ack.js +33 -0
- package/lib/Utils/sync-action-utils.js +53 -0
- package/lib/Utils/tc-token-utils.js +15 -0
- package/lib/Utils/use-multi-file-auth-state.js +116 -0
- package/lib/Utils/use-single-file-auth-state.js +94 -0
- package/lib/Utils/validate-connection.js +235 -0
- package/lib/WABinary/constants.js +1300 -0
- package/lib/WABinary/decode.js +258 -0
- package/lib/WABinary/encode.js +219 -0
- package/lib/WABinary/generic-utils.js +203 -0
- package/lib/WABinary/index.js +5 -0
- package/lib/WABinary/jid-utils.js +93 -0
- package/lib/WABinary/types.js +1 -0
- package/lib/WAM/BinaryInfo.js +9 -0
- package/lib/WAM/constants.js +20669 -0
- package/lib/WAM/encode.js +151 -0
- package/lib/WAM/index.js +3 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +21 -0
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +50 -0
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +20 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +29 -0
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +59 -0
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +21 -0
- package/lib/WAUSync/Protocols/index.js +4 -0
- package/lib/WAUSync/USyncQuery.js +103 -0
- package/lib/WAUSync/USyncUser.js +22 -0
- package/lib/WAUSync/index.js +3 -0
- package/lib/index.js +11 -0
- package/package.json +58 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { Mutex } from "async-mutex";
|
|
2
|
+
import { mkdir, readFile, stat, unlink, writeFile } from "fs/promises";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { proto } from "../../WAProto/index.js";
|
|
5
|
+
import { initAuthCreds } from "./auth-utils.js";
|
|
6
|
+
import { BufferJSON } from "./generics.js";
|
|
7
|
+
// We need to lock files due to the fact that we are using async functions to read and write files
|
|
8
|
+
// https://github.com/WhiskeySockets/Baileys/issues/794
|
|
9
|
+
// https://github.com/nodejs/node/issues/26338
|
|
10
|
+
// Use a Map to store mutexes for each file path
|
|
11
|
+
const fileLocks = new Map();
|
|
12
|
+
// Get or create a mutex for a specific file path
|
|
13
|
+
const getFileLock = (path) => {
|
|
14
|
+
let mutex = fileLocks.get(path);
|
|
15
|
+
if (!mutex) {
|
|
16
|
+
mutex = new Mutex();
|
|
17
|
+
fileLocks.set(path, mutex);
|
|
18
|
+
}
|
|
19
|
+
return mutex;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* stores the full authentication state in a single folder.
|
|
23
|
+
* Far more efficient than singlefileauthstate
|
|
24
|
+
*
|
|
25
|
+
* Again, I wouldn't endorse this for any production level use other than perhaps a bot.
|
|
26
|
+
* Would recommend writing an auth state for use with a proper SQL or No-SQL DB
|
|
27
|
+
* */ export const useMultiFileAuthState = async (folder) => {
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
const writeData = async (data, file) => {
|
|
30
|
+
const filePath = join(folder, fixFileName(file));
|
|
31
|
+
const mutex = getFileLock(filePath);
|
|
32
|
+
return mutex.acquire().then(async (release) => {
|
|
33
|
+
try {
|
|
34
|
+
await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer));
|
|
35
|
+
} finally {
|
|
36
|
+
release();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
const readData = async (file) => {
|
|
41
|
+
try {
|
|
42
|
+
const filePath = join(folder, fixFileName(file));
|
|
43
|
+
const mutex = getFileLock(filePath);
|
|
44
|
+
return await mutex.acquire().then(async (release) => {
|
|
45
|
+
try {
|
|
46
|
+
const data = await readFile(filePath, { encoding: "utf-8" });
|
|
47
|
+
return JSON.parse(data, BufferJSON.reviver);
|
|
48
|
+
} finally {
|
|
49
|
+
release();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
} catch (error) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
const removeData = async (file) => {
|
|
57
|
+
try {
|
|
58
|
+
const filePath = join(folder, fixFileName(file));
|
|
59
|
+
const mutex = getFileLock(filePath);
|
|
60
|
+
return mutex.acquire().then(async (release) => {
|
|
61
|
+
try {
|
|
62
|
+
await unlink(filePath);
|
|
63
|
+
} catch {
|
|
64
|
+
} finally {
|
|
65
|
+
release();
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
} catch {}
|
|
69
|
+
};
|
|
70
|
+
const folderInfo = await stat(folder).catch(() => {});
|
|
71
|
+
if (folderInfo) {
|
|
72
|
+
if (!folderInfo.isDirectory()) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`found something that is not a directory at ${folder}, either delete it or specify a different location`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
await mkdir(folder, { recursive: true });
|
|
79
|
+
}
|
|
80
|
+
const fixFileName = (file) => file?.replace(/\//g, "__")?.replace(/:/g, "-");
|
|
81
|
+
const creds = (await readData("creds.json")) || initAuthCreds();
|
|
82
|
+
return {
|
|
83
|
+
state: {
|
|
84
|
+
creds: creds,
|
|
85
|
+
keys: {
|
|
86
|
+
get: async (type, ids) => {
|
|
87
|
+
const data = {};
|
|
88
|
+
await Promise.all(
|
|
89
|
+
ids.map(async (id) => {
|
|
90
|
+
let value = await readData(`${type}-${id}.json`);
|
|
91
|
+
if (type === "app-state-sync-key" && value) {
|
|
92
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
93
|
+
}
|
|
94
|
+
data[id] = value;
|
|
95
|
+
}),
|
|
96
|
+
);
|
|
97
|
+
return data;
|
|
98
|
+
},
|
|
99
|
+
set: async (data) => {
|
|
100
|
+
const tasks = [];
|
|
101
|
+
for (const category in data) {
|
|
102
|
+
for (const id in data[category]) {
|
|
103
|
+
const value = data[category][id];
|
|
104
|
+
const file = `${category}-${id}.json`;
|
|
105
|
+
tasks.push(value ? writeData(value, file) : removeData(file));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
await Promise.all(tasks);
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
saveCreds: async () => {
|
|
113
|
+
return writeData(creds, "creds.json");
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { readFile, rename, stat, writeFile } from "fs/promises";
|
|
2
|
+
import { DEFAULT_CACHE_TTLS } from "../Defaults/index.js";
|
|
3
|
+
import { proto } from "../../WAProto/index.js";
|
|
4
|
+
import { initAuthCreds } from "./auth-utils.js";
|
|
5
|
+
import { BufferJSON } from "./generics.js";
|
|
6
|
+
// Lia@Changes 25-03-26 --- Add useSingleFileAuthState with integrated cache
|
|
7
|
+
const FLUSH_TIMEOUT_MS = 3e3;
|
|
8
|
+
export const useSingleFileAuthState = async (fileName) => {
|
|
9
|
+
const cache = new Map();
|
|
10
|
+
let isLoaded, isWriting, isNeedWrite, flushTimeout, loadPromise;
|
|
11
|
+
const loadKey = () => {
|
|
12
|
+
if (isLoaded) return;
|
|
13
|
+
if (loadPromise) return loadPromise;
|
|
14
|
+
loadPromise = (async () => {
|
|
15
|
+
try {
|
|
16
|
+
const data = JSON.parse(
|
|
17
|
+
await readFile(fileName, "utf-8"),
|
|
18
|
+
BufferJSON.reviver,
|
|
19
|
+
);
|
|
20
|
+
for (const [keyName, value] of Object.entries(data)) {
|
|
21
|
+
cache.set(keyName, value);
|
|
22
|
+
}
|
|
23
|
+
} catch {}
|
|
24
|
+
isLoaded = true;
|
|
25
|
+
loadPromise = null;
|
|
26
|
+
})();
|
|
27
|
+
return loadPromise;
|
|
28
|
+
};
|
|
29
|
+
const flushKey = () => {
|
|
30
|
+
if (flushTimeout) return;
|
|
31
|
+
flushTimeout = setTimeout(async () => {
|
|
32
|
+
flushTimeout = null;
|
|
33
|
+
if (isWriting) {
|
|
34
|
+
isNeedWrite = true;
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
isWriting = true;
|
|
38
|
+
do {
|
|
39
|
+
isNeedWrite = false;
|
|
40
|
+
const tempFile = fileName + ".temp";
|
|
41
|
+
const value = Object.fromEntries(cache);
|
|
42
|
+
await writeFile(tempFile, JSON.stringify(value, BufferJSON.replacer));
|
|
43
|
+
await rename(tempFile, fileName);
|
|
44
|
+
} while (isNeedWrite);
|
|
45
|
+
isWriting = false;
|
|
46
|
+
}, FLUSH_TIMEOUT_MS);
|
|
47
|
+
};
|
|
48
|
+
const writeKey = (keyName, value) => {
|
|
49
|
+
cache.set(keyName, value);
|
|
50
|
+
flushKey();
|
|
51
|
+
};
|
|
52
|
+
const removeKey = (keyName) => {
|
|
53
|
+
cache.delete(keyName);
|
|
54
|
+
flushKey();
|
|
55
|
+
};
|
|
56
|
+
const fileInfo = await stat(fileName).catch(() => null);
|
|
57
|
+
if (!fileInfo) {
|
|
58
|
+
await writeFile(fileName, "{}");
|
|
59
|
+
} else if (!fileInfo.isFile()) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`found something that is not a file at ${fileName}, either delete it or specify a different location`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
await loadKey();
|
|
65
|
+
const creds = cache.get("creds") || initAuthCreds();
|
|
66
|
+
return {
|
|
67
|
+
state: {
|
|
68
|
+
creds: creds,
|
|
69
|
+
keys: {
|
|
70
|
+
get: (type, ids) => {
|
|
71
|
+
const data = {};
|
|
72
|
+
for (const id of ids) {
|
|
73
|
+
let value = cache.get(type + id);
|
|
74
|
+
if (type === "app-state-sync-key" && value) {
|
|
75
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
76
|
+
}
|
|
77
|
+
data[id] = value;
|
|
78
|
+
}
|
|
79
|
+
return data;
|
|
80
|
+
},
|
|
81
|
+
set: (data) => {
|
|
82
|
+
for (const category in data) {
|
|
83
|
+
for (const id in data[category]) {
|
|
84
|
+
const keyName = category + id;
|
|
85
|
+
const value = data[category][id];
|
|
86
|
+
value ? writeKey(keyName, value) : removeKey(keyName);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
saveCreds: () => writeKey("creds", creds),
|
|
93
|
+
};
|
|
94
|
+
};
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { Boom } from "@hapi/boom";
|
|
2
|
+
import { createHash } from "crypto";
|
|
3
|
+
import { proto } from "../../WAProto/index.js";
|
|
4
|
+
import {
|
|
5
|
+
KEY_BUNDLE_TYPE,
|
|
6
|
+
WA_ADV_ACCOUNT_SIG_PREFIX,
|
|
7
|
+
WA_ADV_DEVICE_SIG_PREFIX,
|
|
8
|
+
WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX,
|
|
9
|
+
} from "../Defaults/index.js";
|
|
10
|
+
import {
|
|
11
|
+
getBinaryNodeChild,
|
|
12
|
+
jidDecode,
|
|
13
|
+
S_WHATSAPP_NET,
|
|
14
|
+
} from "../WABinary/index.js";
|
|
15
|
+
import { Curve, hmacSign } from "./crypto.js";
|
|
16
|
+
import { encodeBigEndian } from "./generics.js";
|
|
17
|
+
import { createSignalIdentity } from "./signal.js";
|
|
18
|
+
const getUserAgent = (config) => {
|
|
19
|
+
return {
|
|
20
|
+
appVersion: {
|
|
21
|
+
primary: config.version[0],
|
|
22
|
+
secondary: config.version[1],
|
|
23
|
+
tertiary: config.version[2],
|
|
24
|
+
},
|
|
25
|
+
platform: config.browser[1].toLocaleLowerCase().includes("android")
|
|
26
|
+
? proto.ClientPayload.UserAgent.Platform.ANDROID
|
|
27
|
+
: proto.ClientPayload.UserAgent.Platform.WEB,
|
|
28
|
+
releaseChannel: proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
|
|
29
|
+
osVersion: "0.1",
|
|
30
|
+
device: "Desktop",
|
|
31
|
+
osBuildNumber: "0.1",
|
|
32
|
+
localeLanguageIso6391: "en",
|
|
33
|
+
mnc: "000",
|
|
34
|
+
mcc: "000",
|
|
35
|
+
localeCountryIso31661Alpha2: config.countryCode,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const PLATFORM_MAP = {
|
|
39
|
+
"Mac OS": proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
|
|
40
|
+
Windows: proto.ClientPayload.WebInfo.WebSubPlatform.WIN32,
|
|
41
|
+
};
|
|
42
|
+
const getWebInfo = (config) => {
|
|
43
|
+
let webSubPlatform = proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
|
|
44
|
+
if (
|
|
45
|
+
config.syncFullHistory &&
|
|
46
|
+
PLATFORM_MAP[config.browser[0]] &&
|
|
47
|
+
config.browser[1] === "Desktop"
|
|
48
|
+
) {
|
|
49
|
+
webSubPlatform = PLATFORM_MAP[config.browser[0]];
|
|
50
|
+
}
|
|
51
|
+
return { webSubPlatform: webSubPlatform };
|
|
52
|
+
};
|
|
53
|
+
const getClientPayload = (config) => {
|
|
54
|
+
const payload = {
|
|
55
|
+
connectType: proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
|
|
56
|
+
connectReason: proto.ClientPayload.ConnectReason.USER_ACTIVATED,
|
|
57
|
+
userAgent: getUserAgent(config),
|
|
58
|
+
};
|
|
59
|
+
if (!config.browser[1].toLocaleLowerCase().includes("android")) {
|
|
60
|
+
payload.webInfo = getWebInfo(config);
|
|
61
|
+
}
|
|
62
|
+
return payload;
|
|
63
|
+
};
|
|
64
|
+
export const generateLoginNode = (userJid, config) => {
|
|
65
|
+
const { user, device } = jidDecode(userJid);
|
|
66
|
+
const payload = {
|
|
67
|
+
...getClientPayload(config),
|
|
68
|
+
passive: true,
|
|
69
|
+
pull: true,
|
|
70
|
+
username: +user,
|
|
71
|
+
device: device,
|
|
72
|
+
// TODO: investigate (hard set as false atm)
|
|
73
|
+
lidDbMigrated: false,
|
|
74
|
+
};
|
|
75
|
+
return proto.ClientPayload.fromObject(payload);
|
|
76
|
+
};
|
|
77
|
+
const getPlatformType = (platform) => {
|
|
78
|
+
const platformType = platform.toUpperCase();
|
|
79
|
+
if (platformType === "ANDROID") {
|
|
80
|
+
return proto.DeviceProps.PlatformType.ANDROID_PHONE;
|
|
81
|
+
}
|
|
82
|
+
return (
|
|
83
|
+
proto.DeviceProps.PlatformType[platformType] ||
|
|
84
|
+
proto.DeviceProps.PlatformType.CHROME
|
|
85
|
+
);
|
|
86
|
+
};
|
|
87
|
+
export const generateRegistrationNode = (
|
|
88
|
+
{ registrationId, signedPreKey, signedIdentityKey },
|
|
89
|
+
config,
|
|
90
|
+
) => {
|
|
91
|
+
// the app version needs to be md5 hashed
|
|
92
|
+
// and passed in
|
|
93
|
+
const appVersionBuf = createHash("md5")
|
|
94
|
+
.update(config.version.join("."))
|
|
95
|
+
.digest();
|
|
96
|
+
const companion = {
|
|
97
|
+
os: config.browser[0],
|
|
98
|
+
platformType: getPlatformType(config.browser[1]),
|
|
99
|
+
requireFullSync: config.syncFullHistory,
|
|
100
|
+
historySyncConfig: {
|
|
101
|
+
storageQuotaMb: 10240,
|
|
102
|
+
inlineInitialPayloadInE2EeMsg: true,
|
|
103
|
+
recentSyncDaysLimit: undefined,
|
|
104
|
+
supportCallLogHistory: false,
|
|
105
|
+
supportBotUserAgentChatHistory: true,
|
|
106
|
+
supportCagReactionsAndPolls: true,
|
|
107
|
+
supportBizHostedMsg: true,
|
|
108
|
+
supportRecentSyncChunkMessageCountTuning: true,
|
|
109
|
+
supportHostedGroupMsg: true,
|
|
110
|
+
supportFbidBotChatHistory: true,
|
|
111
|
+
supportAddOnHistorySyncMigration: undefined,
|
|
112
|
+
supportMessageAssociation: true,
|
|
113
|
+
supportGroupHistory: false,
|
|
114
|
+
onDemandReady: undefined,
|
|
115
|
+
supportGuestChat: undefined,
|
|
116
|
+
},
|
|
117
|
+
version: { primary: 10, secondary: 15, tertiary: 7 },
|
|
118
|
+
};
|
|
119
|
+
const companionProto = proto.DeviceProps.encode(companion).finish();
|
|
120
|
+
const registerPayload = {
|
|
121
|
+
...getClientPayload(config),
|
|
122
|
+
passive: false,
|
|
123
|
+
pull: false,
|
|
124
|
+
devicePairingData: {
|
|
125
|
+
buildHash: appVersionBuf,
|
|
126
|
+
deviceProps: companionProto,
|
|
127
|
+
eRegid: encodeBigEndian(registrationId),
|
|
128
|
+
eKeytype: KEY_BUNDLE_TYPE,
|
|
129
|
+
eIdent: signedIdentityKey.public,
|
|
130
|
+
eSkeyId: encodeBigEndian(signedPreKey.keyId, 3),
|
|
131
|
+
eSkeyVal: signedPreKey.keyPair.public,
|
|
132
|
+
eSkeySig: signedPreKey.signature,
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
return proto.ClientPayload.fromObject(registerPayload);
|
|
136
|
+
};
|
|
137
|
+
export const configureSuccessfulPairing = (
|
|
138
|
+
stanza,
|
|
139
|
+
{ advSecretKey, signedIdentityKey, signalIdentities },
|
|
140
|
+
) => {
|
|
141
|
+
const msgId = stanza.attrs.id;
|
|
142
|
+
const pairSuccessNode = getBinaryNodeChild(stanza, "pair-success");
|
|
143
|
+
const deviceIdentityNode = getBinaryNodeChild(
|
|
144
|
+
pairSuccessNode,
|
|
145
|
+
"device-identity",
|
|
146
|
+
);
|
|
147
|
+
const platformNode = getBinaryNodeChild(pairSuccessNode, "platform");
|
|
148
|
+
const deviceNode = getBinaryNodeChild(pairSuccessNode, "device");
|
|
149
|
+
const businessNode = getBinaryNodeChild(pairSuccessNode, "biz");
|
|
150
|
+
if (!deviceIdentityNode || !deviceNode) {
|
|
151
|
+
throw new Boom("Missing device-identity or device in pair success node", {
|
|
152
|
+
data: stanza,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
const bizName = businessNode?.attrs.name;
|
|
156
|
+
const jid = deviceNode.attrs.jid;
|
|
157
|
+
const lid = deviceNode.attrs.lid;
|
|
158
|
+
const { details, hmac, accountType } =
|
|
159
|
+
proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
|
|
160
|
+
let hmacPrefix = Buffer.from([]);
|
|
161
|
+
if (
|
|
162
|
+
accountType !== undefined &&
|
|
163
|
+
accountType === proto.ADVEncryptionType.HOSTED
|
|
164
|
+
) {
|
|
165
|
+
hmacPrefix = WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX;
|
|
166
|
+
}
|
|
167
|
+
const advSign = hmacSign(
|
|
168
|
+
Buffer.concat([hmacPrefix, details]),
|
|
169
|
+
Buffer.from(advSecretKey, "base64"),
|
|
170
|
+
);
|
|
171
|
+
if (Buffer.compare(hmac, advSign) !== 0) {
|
|
172
|
+
throw new Boom("Invalid account signature");
|
|
173
|
+
}
|
|
174
|
+
const account = proto.ADVSignedDeviceIdentity.decode(details);
|
|
175
|
+
const {
|
|
176
|
+
accountSignatureKey,
|
|
177
|
+
accountSignature,
|
|
178
|
+
details: deviceDetails,
|
|
179
|
+
} = account;
|
|
180
|
+
const deviceIdentity = proto.ADVDeviceIdentity.decode(deviceDetails);
|
|
181
|
+
const accountSignaturePrefix =
|
|
182
|
+
deviceIdentity.deviceType === proto.ADVEncryptionType.HOSTED
|
|
183
|
+
? WA_ADV_HOSTED_ACCOUNT_SIG_PREFIX
|
|
184
|
+
: WA_ADV_ACCOUNT_SIG_PREFIX;
|
|
185
|
+
const accountMsg = Buffer.concat([
|
|
186
|
+
accountSignaturePrefix,
|
|
187
|
+
deviceDetails,
|
|
188
|
+
signedIdentityKey.public,
|
|
189
|
+
]);
|
|
190
|
+
if (!Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
|
|
191
|
+
throw new Boom("Failed to verify account signature");
|
|
192
|
+
}
|
|
193
|
+
const deviceMsg = Buffer.concat([
|
|
194
|
+
WA_ADV_DEVICE_SIG_PREFIX,
|
|
195
|
+
deviceDetails,
|
|
196
|
+
signedIdentityKey.public,
|
|
197
|
+
accountSignatureKey,
|
|
198
|
+
]);
|
|
199
|
+
account.deviceSignature = Curve.sign(signedIdentityKey.private, deviceMsg);
|
|
200
|
+
const identity = createSignalIdentity(lid, accountSignatureKey);
|
|
201
|
+
const accountEnc = encodeSignedDeviceIdentity(account, false);
|
|
202
|
+
const reply = {
|
|
203
|
+
tag: "iq",
|
|
204
|
+
attrs: { to: S_WHATSAPP_NET, type: "result", id: msgId },
|
|
205
|
+
content: [
|
|
206
|
+
{
|
|
207
|
+
tag: "pair-device-sign",
|
|
208
|
+
attrs: {},
|
|
209
|
+
content: [
|
|
210
|
+
{
|
|
211
|
+
tag: "device-identity",
|
|
212
|
+
attrs: { "key-index": deviceIdentity.keyIndex.toString() },
|
|
213
|
+
content: accountEnc,
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
},
|
|
217
|
+
],
|
|
218
|
+
};
|
|
219
|
+
const authUpdate = {
|
|
220
|
+
account: account,
|
|
221
|
+
me: { id: jid, name: bizName, lid: lid },
|
|
222
|
+
signalIdentities: [...(signalIdentities || []), identity],
|
|
223
|
+
platform: platformNode?.attrs.name,
|
|
224
|
+
};
|
|
225
|
+
return { creds: authUpdate, reply: reply };
|
|
226
|
+
};
|
|
227
|
+
export const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
|
|
228
|
+
account = { ...account };
|
|
229
|
+
// set to null if we are not to include the signature key
|
|
230
|
+
// or if we are including the signature key but it is empty
|
|
231
|
+
if (!includeSignatureKey || !account.accountSignatureKey?.length) {
|
|
232
|
+
account.accountSignatureKey = null;
|
|
233
|
+
}
|
|
234
|
+
return proto.ADVSignedDeviceIdentity.encode(account).finish();
|
|
235
|
+
};
|