@zetagoaurum-socket/wasocket 2.2.2 → 3.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.
Files changed (96) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +60 -81
  3. package/WAProto/GenerateStatics.sh +4 -0
  4. package/WAProto/WAProto.proto +3344 -0
  5. package/WAProto/index.d.ts +37016 -0
  6. package/WAProto/index.js +79296 -118676
  7. package/WASignalGroup/GroupProtocol.js +1697 -0
  8. package/WASignalGroup/ciphertext_message.js +16 -0
  9. package/WASignalGroup/group_cipher.js +120 -0
  10. package/WASignalGroup/group_session_builder.js +46 -0
  11. package/WASignalGroup/index.js +5 -0
  12. package/WASignalGroup/keyhelper.js +21 -0
  13. package/WASignalGroup/protobufs.js +3 -0
  14. package/WASignalGroup/queue_job.js +69 -0
  15. package/WASignalGroup/sender_chain_key.js +50 -0
  16. package/WASignalGroup/sender_key_distribution_message.js +78 -0
  17. package/WASignalGroup/sender_key_message.js +92 -0
  18. package/WASignalGroup/sender_key_name.js +70 -0
  19. package/WASignalGroup/sender_key_record.js +56 -0
  20. package/WASignalGroup/sender_key_state.js +129 -0
  21. package/{lib/Signal/Group/sender-message-key.js → WASignalGroup/sender_message_key.js} +16 -6
  22. package/lib/Defaults/baileys-version.json +1 -1
  23. package/lib/Defaults/index.js +9 -50
  24. package/lib/Signal/libsignal.js +29 -51
  25. package/lib/Socket/business.d.ts +2 -3
  26. package/lib/Socket/chats.d.ts +28 -215
  27. package/lib/Socket/chats.js +70 -185
  28. package/lib/Socket/dugong.js +141 -432
  29. package/lib/Socket/groups.js +23 -20
  30. package/lib/Socket/index.js +15 -2
  31. package/lib/Socket/messages-recv.d.ts +55 -56
  32. package/lib/Socket/messages-recv.js +112 -370
  33. package/lib/Socket/messages-send.d.ts +2 -3
  34. package/lib/Socket/messages-send.js +377 -464
  35. package/lib/Socket/newsletter.js +28 -175
  36. package/lib/Socket/socket.js +176 -204
  37. package/lib/Socket/usync.d.ts +3 -3
  38. package/lib/Types/GroupMetadata.d.ts +0 -1
  39. package/lib/Types/Newsletter.d.ts +86 -97
  40. package/lib/Types/Newsletter.js +32 -38
  41. package/lib/Utils/chat-utils.js +3 -17
  42. package/lib/Utils/generics.js +31 -68
  43. package/lib/Utils/history.js +1 -6
  44. package/lib/Utils/index.js +1 -0
  45. package/lib/Utils/link-preview.js +2 -4
  46. package/lib/Utils/messages-media.js +57 -145
  47. package/lib/Utils/messages.js +305 -89
  48. package/lib/Utils/rate-limiter.js +55 -0
  49. package/lib/Utils/signal.js +46 -48
  50. package/lib/Utils/use-multi-file-auth-state.js +6 -45
  51. package/lib/Utils/validate-connection.js +65 -91
  52. package/lib/WABinary/constants.d.ts +24 -27
  53. package/lib/WABinary/encode.js +123 -160
  54. package/lib/WABinary/generic-utils.d.ts +0 -2
  55. package/lib/WABinary/generic-utils.js +36 -124
  56. package/lib/WABinary/jid-utils.js +26 -5
  57. package/lib/WAUSync/Fall +1 -0
  58. package/lib/WAUSync/Protocols/Fal +1 -0
  59. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +1 -1
  60. package/lib/index.d.ts +0 -1
  61. package/lib/index.js +0 -9
  62. package/package.json +100 -105
  63. package/engine-requirements.js +0 -10
  64. package/lib/Signal/Group/ciphertext-message.d.ts +0 -9
  65. package/lib/Signal/Group/ciphertext-message.js +0 -15
  66. package/lib/Signal/Group/group-session-builder.d.ts +0 -14
  67. package/lib/Signal/Group/group-session-builder.js +0 -64
  68. package/lib/Signal/Group/group_cipher.d.ts +0 -17
  69. package/lib/Signal/Group/group_cipher.js +0 -96
  70. package/lib/Signal/Group/index.d.ts +0 -11
  71. package/lib/Signal/Group/index.js +0 -57
  72. package/lib/Signal/Group/keyhelper.d.ts +0 -10
  73. package/lib/Signal/Group/keyhelper.js +0 -55
  74. package/lib/Signal/Group/queue-job.d.ts +0 -1
  75. package/lib/Signal/Group/queue-job.js +0 -57
  76. package/lib/Signal/Group/sender-chain-key.d.ts +0 -13
  77. package/lib/Signal/Group/sender-chain-key.js +0 -34
  78. package/lib/Signal/Group/sender-key-distribution-message.d.ts +0 -16
  79. package/lib/Signal/Group/sender-key-distribution-message.js +0 -66
  80. package/lib/Signal/Group/sender-key-message.d.ts +0 -18
  81. package/lib/Signal/Group/sender-key-message.js +0 -69
  82. package/lib/Signal/Group/sender-key-name.d.ts +0 -17
  83. package/lib/Signal/Group/sender-key-name.js +0 -51
  84. package/lib/Signal/Group/sender-key-record.d.ts +0 -30
  85. package/lib/Signal/Group/sender-key-record.js +0 -53
  86. package/lib/Signal/Group/sender-key-state.d.ts +0 -38
  87. package/lib/Signal/Group/sender-key-state.js +0 -99
  88. package/lib/Signal/Group/sender-message-key.d.ts +0 -11
  89. package/lib/Socket/chats.js.bak +0 -981
  90. package/lib/Socket/dugong.d.ts +0 -254
  91. package/lib/Types/USync.d.ts +0 -25
  92. package/lib/Types/USync.js +0 -2
  93. package/lib/Utils/anti-crash.js +0 -20
  94. package/lib/Utils/auto-contact.js +0 -74
  95. package/lib/Utils/generics.js.bak +0 -433
  96. package/lib/Utils/validate-connection.js.bak +0 -237
@@ -4,7 +4,6 @@ exports.getNextPreKeysNode = exports.getNextPreKeys = exports.extractDeviceJids
4
4
  const Defaults_1 = require("../Defaults");
5
5
  const WABinary_1 = require("../WABinary");
6
6
  const crypto_1 = require("./crypto");
7
- const lodash_1 = require("lodash")
8
7
  const generics_1 = require("./generics");
9
8
  const createSignalIdentity = (wid, accountSignatureKey) => {
10
9
  return {
@@ -59,61 +58,60 @@ const xmppPreKey = (pair, id) => ({
59
58
  exports.xmppPreKey = xmppPreKey;
60
59
  const parseAndInjectE2ESessions = async (node, repository) => {
61
60
  const extractKey = (key) => (key ? ({
62
- keyId: WABinary_1.getBinaryNodeChildUInt(key, 'id', 3),
63
- publicKey: crypto_1.generateSignalPubKey(WABinary_1.getBinaryNodeChildBuffer(key, 'value')),
64
- signature: WABinary_1.getBinaryNodeChildBuffer(key, 'signature')
65
- }) : undefined)
66
- const nodes = WABinary_1.getBinaryNodeChildren(WABinary_1.getBinaryNodeChild(node, 'list'), 'user')
61
+ keyId: (0, WABinary_1.getBinaryNodeChildUInt)(key, 'id', 3),
62
+ publicKey: (0, crypto_1.generateSignalPubKey)((0, WABinary_1.getBinaryNodeChildBuffer)(key, 'value')),
63
+ signature: (0, WABinary_1.getBinaryNodeChildBuffer)(key, 'signature'),
64
+ }) : undefined);
65
+ const nodes = (0, WABinary_1.getBinaryNodeChildren)((0, WABinary_1.getBinaryNodeChild)(node, 'list'), 'user');
67
66
  for (const node of nodes) {
68
- WABinary_1.assertNodeErrorFree(node)
67
+ (0, WABinary_1.assertNodeErrorFree)(node);
69
68
  }
70
- // Most of the work in repository.injectE2ESession is CPU intensive, not IO
71
- // So Promise.all doesn't really help here,
72
- // but blocks even loop if we're using it inside keys.transaction, and it makes it "sync" actually
73
- // This way we chunk it in smaller parts and between those parts we can yield to the event loop
74
- // It's rare case when you need to E2E sessions for so many users, but it's possible
75
- const chunkSize = 100
76
- const chunks = lodash_1.chunk(nodes, chunkSize)
77
- for (const nodesChunk of chunks) {
78
- await Promise.all(nodesChunk.map(async (node) => {
79
- const signedKey = WABinary_1.getBinaryNodeChild(node, 'skey')
80
- const key = WABinary_1.getBinaryNodeChild(node, 'key')
81
- const identity = WABinary_1.getBinaryNodeChildBuffer(node, 'identity')
82
- const jid = node.attrs.jid
83
- const registrationId = WABinary_1.getBinaryNodeChildUInt(node, 'registration', 4)
84
- await repository.injectE2ESession({
85
- jid,
86
- session: {
87
- registrationId: registrationId,
88
- identityKey: crypto_1.generateSignalPubKey(identity),
89
- signedPreKey: extractKey(signedKey),
90
- preKey: extractKey(key)
91
- }
92
- })
93
- }))
94
- }
95
- }
69
+ await Promise.all(nodes.map(async (node) => {
70
+ const signedKey = (0, WABinary_1.getBinaryNodeChild)(node, 'skey');
71
+ const key = (0, WABinary_1.getBinaryNodeChild)(node, 'key');
72
+ const identity = (0, WABinary_1.getBinaryNodeChildBuffer)(node, 'identity');
73
+ const jid = node.attrs.jid;
74
+ const registrationId = (0, WABinary_1.getBinaryNodeChildUInt)(node, 'registration', 4);
75
+ await repository.injectE2ESession({
76
+ jid,
77
+ session: {
78
+ registrationId: registrationId,
79
+ identityKey: (0, crypto_1.generateSignalPubKey)(identity),
80
+ signedPreKey: extractKey(signedKey),
81
+ preKey: extractKey(key)
82
+ }
83
+ });
84
+ }));
85
+ };
96
86
  exports.parseAndInjectE2ESessions = parseAndInjectE2ESessions;
97
87
  const extractDeviceJids = (result, myJid, excludeZeroDevices) => {
98
- const { user: myUser, device: myDevice } = WABinary_1.jidDecode(myJid)
99
- const extracted = []
100
- for (const userResult of result) {
101
- const { devices, id } = userResult
102
- const { user } = WABinary_1.jidDecode(id)
103
- const deviceList = devices?.deviceList
104
- if (Array.isArray(deviceList)) {
105
- for (const { id: device, keyIndex } of deviceList) {
106
- if ((!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
107
- (myUser !== user || myDevice !== device) && // either different user or if me user, not this device
108
- (device === 0 || !!keyIndex) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
109
- ) {
110
- extracted.push({ user, device })
88
+ var _a;
89
+ const { user: myUser, device: myDevice } = (0, WABinary_1.jidDecode)(myJid);
90
+ const extracted = [];
91
+ for (const node of result.content) {
92
+ const list = (_a = (0, WABinary_1.getBinaryNodeChild)(node, 'list')) === null || _a === void 0 ? void 0 : _a.content;
93
+ if (list && Array.isArray(list)) {
94
+ for (const item of list) {
95
+ const { user } = (0, WABinary_1.jidDecode)(item.attrs.jid);
96
+ const devicesNode = (0, WABinary_1.getBinaryNodeChild)(item, 'devices');
97
+ const deviceListNode = (0, WABinary_1.getBinaryNodeChild)(devicesNode, 'device-list');
98
+ if (Array.isArray(deviceListNode === null || deviceListNode === void 0 ? void 0 : deviceListNode.content)) {
99
+ for (const { tag, attrs } of deviceListNode.content) {
100
+ const device = +attrs.id;
101
+ if (tag === 'device' && // ensure the "device" tag
102
+ (!excludeZeroDevices || device !== 0) && // if zero devices are not-excluded, or device is non zero
103
+ (myUser !== user || myDevice !== device) && // either different user or if me user, not this device
104
+ (device === 0 || !!attrs['key-index']) // ensure that "key-index" is specified for "non-zero" devices, produces a bad req otherwise
105
+ ) {
106
+ extracted.push({ user, device });
107
+ }
108
+ }
111
109
  }
112
110
  }
113
111
  }
114
112
  }
115
- return extracted
116
- }
113
+ return extracted;
114
+ };
117
115
  exports.extractDeviceJids = extractDeviceJids;
118
116
  /**
119
117
  * get the next N keys for upload or processing
@@ -1,21 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useMultiFileAuthState = void 0;
4
- const async_mutex_1 = require("async-mutex");
5
4
  const promises_1 = require("fs/promises");
6
5
  const path_1 = require("path");
7
6
  const WAProto_1 = require("../../WAProto");
8
7
  const auth_utils_1 = require("./auth-utils");
9
8
  const generics_1 = require("./generics");
10
- const fileLocks = new Map();
11
- const getFileLock = (path) => {
12
- let mutex = fileLocks.get(path);
13
- if (!mutex) {
14
- mutex = new async_mutex_1.Mutex();
15
- fileLocks.set(path, mutex);
16
- }
17
- return mutex;
18
- };
19
9
  /**
20
10
  * stores the full authentication state in a single folder.
21
11
  * Far more efficient than singlefileauthstate
@@ -24,31 +14,13 @@ const getFileLock = (path) => {
24
14
  * Would recommend writing an auth state for use with a proper SQL or No-SQL DB
25
15
  * */
26
16
  const useMultiFileAuthState = async (folder) => {
27
- const writeData = async (data, file) => {
28
- const filePath = (0, path_1.join)(folder, fixFileName(file));
29
- const mutex = getFileLock(filePath);
30
- return mutex.acquire().then(async (release) => {
31
- try {
32
- await (0, promises_1.writeFile)(filePath, JSON.stringify(data, generics_1.BufferJSON.replacer));
33
- }
34
- finally {
35
- release();
36
- }
37
- });
17
+ const writeData = (data, file) => {
18
+ return (0, promises_1.writeFile)((0, path_1.join)(folder, fixFileName(file)), JSON.stringify(data, generics_1.BufferJSON.replacer));
38
19
  };
39
20
  const readData = async (file) => {
40
21
  try {
41
- const filePath = (0, path_1.join)(folder, fixFileName(file));
42
- const mutex = getFileLock(filePath);
43
- return await mutex.acquire().then(async (release) => {
44
- try {
45
- const data = await (0, promises_1.readFile)(filePath, { encoding: 'utf-8' });
46
- return JSON.parse(data, generics_1.BufferJSON.reviver);
47
- }
48
- finally {
49
- release();
50
- }
51
- });
22
+ const data = await (0, promises_1.readFile)((0, path_1.join)(folder, fixFileName(file)), { encoding: 'utf-8' });
23
+ return JSON.parse(data, generics_1.BufferJSON.reviver);
52
24
  }
53
25
  catch (error) {
54
26
  return null;
@@ -56,18 +28,7 @@ const useMultiFileAuthState = async (folder) => {
56
28
  };
57
29
  const removeData = async (file) => {
58
30
  try {
59
- const filePath = (0, path_1.join)(folder, fixFileName(file));
60
- const mutex = getFileLock(filePath);
61
- return mutex.acquire().then(async (release) => {
62
- try {
63
- await (0, promises_1.unlink)(filePath);
64
- }
65
- catch (_a) {
66
- }
67
- finally {
68
- release();
69
- }
70
- });
31
+ await (0, promises_1.unlink)((0, path_1.join)(folder, fixFileName(file)));
71
32
  }
72
33
  catch (_a) {
73
34
  }
@@ -111,7 +72,7 @@ const useMultiFileAuthState = async (folder) => {
111
72
  }
112
73
  }
113
74
  },
114
- saveCreds: async () => {
75
+ saveCreds: () => {
115
76
  return writeData(creds, 'creds.json');
116
77
  }
117
78
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.encodeSignedDeviceIdentity = exports.configureSuccessfulPairing = exports.generateRegistrationNode = exports.generateLoginNode = void 0;
3
+ exports.encodeSignedDeviceIdentity = exports.configureSuccessfulPairing = exports.generateRegistrationNode = exports.generateLoginNode = exports.generateMobileNode = void 0;
4
4
  const boom_1 = require("@hapi/boom");
5
5
  const crypto_1 = require("crypto");
6
6
  const WAProto_1 = require("../../WAProto");
@@ -8,110 +8,107 @@ const Defaults_1 = require("../Defaults");
8
8
  const WABinary_1 = require("../WABinary");
9
9
  const crypto_2 = require("./crypto");
10
10
  const generics_1 = require("./generics");
11
- const signal_1 = require("./signal");
12
-
11
+ const signal_1 = require("./signal");
13
12
  const getUserAgent = (config) => {
13
+ var _a, _b;
14
+ const osVersion = config.mobile ? '15.3.1' : '0.1';
15
+ const version = config.mobile ? [2, 24, 6] : config.version;
16
+ const device = config.mobile ? 'iPhone_7' : 'Desktop';
17
+ const manufacturer = config.mobile ? 'Apple' : '';
18
+ const platform = config.mobile ? WAProto_1.proto.ClientPayload.UserAgent.Platform.IOS : WAProto_1.proto.ClientPayload.UserAgent.Platform.WEB;
19
+ const phoneId = config.mobile ? { phoneId: config.auth.creds.phoneId } : {};
14
20
  return {
15
21
  appVersion: {
16
- primary: config.version[0],
17
- secondary: config.version[1],
18
- tertiary: config.version[2],
22
+ primary: version[0],
23
+ secondary: version[1],
24
+ tertiary: version[2],
19
25
  },
20
- platform: WAProto_1.proto.ClientPayload.UserAgent.Platform.WEB,
26
+ platform,
21
27
  releaseChannel: WAProto_1.proto.ClientPayload.UserAgent.ReleaseChannel.RELEASE,
22
- osVersion: '0.1',
23
- device: 'Desktop',
24
- osBuildNumber: '0.1',
28
+ mcc: ((_a = config.auth.creds.registration) === null || _a === void 0 ? void 0 : _a.phoneNumberMobileCountryCode) || '000',
29
+ mnc: ((_b = config.auth.creds.registration) === null || _b === void 0 ? void 0 : _b.phoneNumberMobileNetworkCode) || '000',
30
+ osVersion: osVersion,
31
+ manufacturer,
32
+ device,
33
+ osBuildNumber: osVersion,
25
34
  localeLanguageIso6391: 'en',
26
- mnc: '000',
27
- mcc: '000',
28
- localeCountryIso31661Alpha2: config.countryCode || 'US'
35
+ localeCountryIso31661Alpha2: 'US',
36
+ ...phoneId
29
37
  };
30
38
  };
31
-
32
-
33
-
34
39
  const PLATFORM_MAP = {
35
40
  'Mac OS': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.DARWIN,
36
41
  'Windows': WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WIN32
37
42
  };
38
-
39
43
  const getWebInfo = (config) => {
40
44
  let webSubPlatform = WAProto_1.proto.ClientPayload.WebInfo.WebSubPlatform.WEB_BROWSER;
41
- if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]] && config.browser[1] === 'Desktop') {
45
+ if (config.syncFullHistory && PLATFORM_MAP[config.browser[0]]) {
42
46
  webSubPlatform = PLATFORM_MAP[config.browser[0]];
43
47
  }
44
48
  return { webSubPlatform };
45
49
  };
46
-
47
50
  const getClientPayload = (config) => {
48
51
  const payload = {
49
52
  connectType: WAProto_1.proto.ClientPayload.ConnectType.WIFI_UNKNOWN,
50
53
  connectReason: WAProto_1.proto.ClientPayload.ConnectReason.USER_ACTIVATED,
51
54
  userAgent: getUserAgent(config),
52
55
  };
53
- payload.webInfo = getWebInfo(config);
56
+ if (!config.mobile) {
57
+ payload.webInfo = getWebInfo(config);
58
+ }
54
59
  return payload;
55
60
  };
56
-
61
+ const generateMobileNode = (config) => {
62
+ if (!config.auth.creds) {
63
+ throw new boom_1.Boom('No registration data found', { data: config });
64
+ }
65
+ const payload = {
66
+ ...getClientPayload(config),
67
+ sessionId: Math.floor(Math.random() * 999999999 + 1),
68
+ shortConnect: true,
69
+ connectAttemptCount: 0,
70
+ device: 0,
71
+ dnsSource: {
72
+ appCached: false,
73
+ dnsMethod: WAProto_1.proto.ClientPayload.DNSSource.DNSResolutionMethod.SYSTEM,
74
+ },
75
+ passive: false,
76
+ pushName: 'test',
77
+ username: Number(`${config.auth.creds.registration.phoneNumberCountryCode}${config.auth.creds.registration.phoneNumberNationalNumber}`),
78
+ };
79
+ return WAProto_1.proto.ClientPayload.fromObject(payload);
80
+ };
81
+ exports.generateMobileNode = generateMobileNode;
57
82
  const generateLoginNode = (userJid, config) => {
58
83
  const { user, device } = (0, WABinary_1.jidDecode)(userJid);
59
84
  const payload = {
60
85
  ...getClientPayload(config),
61
86
  passive: true,
62
- pull: true,
63
87
  username: +user,
64
88
  device: device,
65
- lidDbMigrated: false
66
89
  };
67
90
  return WAProto_1.proto.ClientPayload.fromObject(payload);
68
91
  };
69
92
  exports.generateLoginNode = generateLoginNode;
70
-
71
93
  const getPlatformType = (platform) => {
72
94
  const platformType = platform.toUpperCase();
73
- return WAProto_1.proto.DeviceProps.PlatformType[platformType] || WAProto_1.proto.DeviceProps.PlatformType.CHROME;
95
+ return WAProto_1.proto.DeviceProps.PlatformType[platformType] || WAProto_1.proto.DeviceProps.PlatformType.DESKTOP;
74
96
  };
75
-
76
97
  const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentityKey }, config) => {
98
+ // the app version needs to be md5 hashed
99
+ // and passed in
77
100
  const appVersionBuf = (0, crypto_1.createHash)('md5')
78
- .update(config.version.join('.'))
101
+ .update(config.version.join('.')) // join as string
79
102
  .digest();
80
-
81
103
  const companion = {
82
104
  os: config.browser[0],
83
105
  platformType: getPlatformType(config.browser[1]),
84
106
  requireFullSync: config.syncFullHistory,
85
- historySyncConfig: {
86
- storageQuotaMb: 10240,
87
- inlineInitialPayloadInE2EeMsg: true,
88
- recentSyncDaysLimit: undefined,
89
- supportCallLogHistory: false,
90
- supportBotUserAgentChatHistory: true,
91
- supportCagReactionsAndPolls: true,
92
- supportBizHostedMsg: true,
93
- supportRecentSyncChunkMessageCountTuning: true,
94
- supportHostedGroupMsg: true,
95
- supportFbidBotChatHistory: true,
96
- supportAddOnHistorySyncMigration: undefined,
97
- supportMessageAssociation: true,
98
- supportGroupHistory: false,
99
- onDemandReady: undefined,
100
- supportGuestChat: undefined
101
- },
102
- version: {
103
- primary: 10,
104
- secondary: 15,
105
- tertiary: 7
106
- }
107
107
  };
108
-
109
108
  const companionProto = WAProto_1.proto.DeviceProps.encode(companion).finish();
110
-
111
109
  const registerPayload = {
112
110
  ...getClientPayload(config),
113
111
  passive: false,
114
- pull: false,
115
112
  devicePairingData: {
116
113
  buildHash: appVersionBuf,
117
114
  deviceProps: companionProto,
@@ -126,7 +123,6 @@ const generateRegistrationNode = ({ registrationId, signedPreKey, signedIdentity
126
123
  return WAProto_1.proto.ClientPayload.fromObject(registerPayload);
127
124
  };
128
125
  exports.generateRegistrationNode = generateRegistrationNode;
129
-
130
126
  const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, signalIdentities }) => {
131
127
  const msgId = stanza.attrs.id;
132
128
  const pairSuccessNode = (0, WABinary_1.getBinaryNodeChild)(stanza, 'pair-success');
@@ -134,52 +130,30 @@ const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, s
134
130
  const platformNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'platform');
135
131
  const deviceNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'device');
136
132
  const businessNode = (0, WABinary_1.getBinaryNodeChild)(pairSuccessNode, 'biz');
137
-
138
133
  if (!deviceIdentityNode || !deviceNode) {
139
134
  throw new boom_1.Boom('Missing device-identity or device in pair success node', { data: stanza });
140
135
  }
141
-
142
- const bizName = businessNode?.attrs.name;
136
+ const bizName = businessNode === null || businessNode === void 0 ? void 0 : businessNode.attrs.name;
143
137
  const jid = deviceNode.attrs.jid;
144
- const lid = deviceNode.attrs.lid;
145
-
146
- const { details, hmac, accountType } = WAProto_1.proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
147
-
148
- let hmacPrefix = Buffer.from([]);
149
- if (accountType !== undefined && accountType === WAProto_1.proto.ADVEncryptionType.HOSTED) {
150
- hmacPrefix = Buffer.from([0x06, 0x05]);
151
- }
152
-
153
- const advSign = (0, crypto_2.hmacSign)(Buffer.concat([hmacPrefix, details]), Buffer.from(advSecretKey, 'base64'));
138
+ const { details, hmac } = WAProto_1.proto.ADVSignedDeviceIdentityHMAC.decode(deviceIdentityNode.content);
139
+ // check HMAC matches
140
+ const advSign = (0, crypto_2.hmacSign)(details, Buffer.from(advSecretKey, 'base64'));
154
141
  if (Buffer.compare(hmac, advSign) !== 0) {
155
142
  throw new boom_1.Boom('Invalid account signature');
156
143
  }
157
-
158
144
  const account = WAProto_1.proto.ADVSignedDeviceIdentity.decode(details);
159
145
  const { accountSignatureKey, accountSignature, details: deviceDetails } = account;
160
-
161
- const deviceIdentity = WAProto_1.proto.ADVDeviceIdentity.decode(deviceDetails);
162
-
163
- const accountSignaturePrefix = deviceIdentity.deviceType === WAProto_1.proto.ADVEncryptionType.HOSTED
164
- ? Buffer.from([0x06, 0x05])
165
- : Buffer.from([0x06, 0x00]);
166
- const accountMsg = Buffer.concat([accountSignaturePrefix, deviceDetails, signedIdentityKey.public]);
167
-
146
+ // verify the device signature matches
147
+ const accountMsg = Buffer.concat([Buffer.from([6, 0]), deviceDetails, signedIdentityKey.public]);
168
148
  if (!crypto_2.Curve.verify(accountSignatureKey, accountMsg, accountSignature)) {
169
149
  throw new boom_1.Boom('Failed to verify account signature');
170
150
  }
171
-
172
- const deviceMsg = Buffer.concat([
173
- Buffer.from([0x06, 0x01]),
174
- deviceDetails,
175
- signedIdentityKey.public,
176
- accountSignatureKey
177
- ]);
151
+ // sign the details with our identity key
152
+ const deviceMsg = Buffer.concat([Buffer.from([6, 1]), deviceDetails, signedIdentityKey.public, accountSignatureKey]);
178
153
  account.deviceSignature = crypto_2.Curve.sign(signedIdentityKey.private, deviceMsg);
179
-
180
154
  const identity = (0, signal_1.createSignalIdentity)(jid, accountSignatureKey);
181
155
  const accountEnc = (0, exports.encodeSignedDeviceIdentity)(account, false);
182
-
156
+ const deviceIdentity = WAProto_1.proto.ADVDeviceIdentity.decode(account.details);
183
157
  const reply = {
184
158
  tag: 'iq',
185
159
  attrs: {
@@ -201,27 +175,27 @@ const configureSuccessfulPairing = (stanza, { advSecretKey, signedIdentityKey, s
201
175
  }
202
176
  ]
203
177
  };
204
-
205
178
  const authUpdate = {
206
179
  account,
207
- me: { id: jid, name: bizName, lid },
180
+ me: { id: jid, name: bizName },
208
181
  signalIdentities: [
209
182
  ...(signalIdentities || []),
210
183
  identity
211
184
  ],
212
- platform: platformNode?.attrs.name
185
+ platform: platformNode === null || platformNode === void 0 ? void 0 : platformNode.attrs.name
213
186
  };
214
-
215
187
  return {
216
188
  creds: authUpdate,
217
189
  reply
218
190
  };
219
191
  };
220
192
  exports.configureSuccessfulPairing = configureSuccessfulPairing;
221
-
222
193
  const encodeSignedDeviceIdentity = (account, includeSignatureKey) => {
194
+ var _a;
223
195
  account = { ...account };
224
- if (!includeSignatureKey || !account.accountSignatureKey?.length) {
196
+ // set to null if we are not to include the signature key
197
+ // or if we are including the signature key but it is empty
198
+ if (!includeSignatureKey || !((_a = account.accountSignatureKey) === null || _a === void 0 ? void 0 : _a.length)) {
225
199
  account.accountSignatureKey = null;
226
200
  }
227
201
  return WAProto_1.proto.ADVSignedDeviceIdentity
@@ -1,30 +1,27 @@
1
1
  export declare const TAGS: {
2
- LIST_EMPTY: number
3
- DICTIONARY_0: number
4
- DICTIONARY_1: number
5
- DICTIONARY_2: number
6
- DICTIONARY_3: number
7
- INTEROP_JID: number
8
- FB_JID: number
9
- AD_JID: number
10
- LIST_8: number
11
- LIST_16: number
12
- JID_PAIR: number
13
- HEX_8: number
14
- BINARY_8: number
15
- BINARY_20: number
16
- BINARY_32: number
17
- NIBBLE_8: number
18
- PACKED_MAX: number
19
- }
20
-
21
- export declare const DOUBLE_BYTE_TOKENS: string[][]
22
-
23
- export declare const SINGLE_BYTE_TOKENS: (string | null)[]
24
-
2
+ LIST_EMPTY: number;
3
+ DICTIONARY_0: number;
4
+ DICTIONARY_1: number;
5
+ DICTIONARY_2: number;
6
+ DICTIONARY_3: number;
7
+ AD_JID: number;
8
+ LIST_8: number;
9
+ LIST_16: number;
10
+ JID_PAIR: number;
11
+ HEX_8: number;
12
+ BINARY_8: number;
13
+ BINARY_20: number;
14
+ BINARY_32: number;
15
+ NIBBLE_8: number;
16
+ PACKED_MAX: number;
17
+ SINGLE_BYTE_MAX: number;
18
+ STREAM_END: number;
19
+ };
20
+ export declare const DOUBLE_BYTE_TOKENS: string[][];
21
+ export declare const SINGLE_BYTE_TOKENS: (string | null)[];
25
22
  export declare const TOKEN_MAP: {
26
23
  [token: string]: {
27
- dict?: number
28
- index: number
29
- }
30
- }
24
+ dict?: number;
25
+ index: number;
26
+ };
27
+ };