@baileys-md/baileys 10.0.7

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 (102) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/WAProto/index.js +155693 -0
  4. package/WASignalGroup/GroupProtocol.js +1697 -0
  5. package/WASignalGroup/ciphertext_message.js +16 -0
  6. package/WASignalGroup/group_cipher.js +120 -0
  7. package/WASignalGroup/group_session_builder.js +46 -0
  8. package/WASignalGroup/index.js +5 -0
  9. package/WASignalGroup/keyhelper.js +21 -0
  10. package/WASignalGroup/protobufs.js +3 -0
  11. package/WASignalGroup/queue_job.js +69 -0
  12. package/WASignalGroup/sender_chain_key.js +50 -0
  13. package/WASignalGroup/sender_key_distribution_message.js +78 -0
  14. package/WASignalGroup/sender_key_message.js +92 -0
  15. package/WASignalGroup/sender_key_name.js +70 -0
  16. package/WASignalGroup/sender_key_record.js +56 -0
  17. package/WASignalGroup/sender_key_state.js +129 -0
  18. package/WASignalGroup/sender_message_key.js +39 -0
  19. package/lib/Defaults/baileys-version.json +3 -0
  20. package/lib/Defaults/index.js +122 -0
  21. package/lib/Defaults/phonenumber-mcc.json +223 -0
  22. package/lib/Signal/libsignal.js +161 -0
  23. package/lib/Socket/Client/abstract-socket-client.js +13 -0
  24. package/lib/Socket/Client/index.js +19 -0
  25. package/lib/Socket/Client/mobile-socket-client.js +65 -0
  26. package/lib/Socket/Client/web-socket-client.js +62 -0
  27. package/lib/Socket/business.js +260 -0
  28. package/lib/Socket/chats.js +882 -0
  29. package/lib/Socket/groups.js +327 -0
  30. package/lib/Socket/index.js +10 -0
  31. package/lib/Socket/messages-recv.js +1043 -0
  32. package/lib/Socket/messages-send.js +836 -0
  33. package/lib/Socket/newsletter.js +248 -0
  34. package/lib/Socket/registration.js +166 -0
  35. package/lib/Socket/socket.js +640 -0
  36. package/lib/Socket/usync.js +70 -0
  37. package/lib/Store/index.js +8 -0
  38. package/lib/Store/make-cache-manager-store.js +83 -0
  39. package/lib/Store/make-in-memory-store.js +431 -0
  40. package/lib/Store/make-mongo-store.js +567 -0
  41. package/lib/Store/make-ordered-dictionary.js +81 -0
  42. package/lib/Store/object-repository.js +27 -0
  43. package/lib/Types/Auth.js +2 -0
  44. package/lib/Types/Call.js +2 -0
  45. package/lib/Types/Chat.js +4 -0
  46. package/lib/Types/Contact.js +2 -0
  47. package/lib/Types/Events.js +2 -0
  48. package/lib/Types/GroupMetadata.js +2 -0
  49. package/lib/Types/Label.js +27 -0
  50. package/lib/Types/LabelAssociation.js +9 -0
  51. package/lib/Types/Message.js +7 -0
  52. package/lib/Types/Newsletter.js +18 -0
  53. package/lib/Types/Product.js +2 -0
  54. package/lib/Types/Signal.js +2 -0
  55. package/lib/Types/Socket.js +2 -0
  56. package/lib/Types/State.js +2 -0
  57. package/lib/Types/USync.js +2 -0
  58. package/lib/Types/index.js +42 -0
  59. package/lib/Utils/auth-utils.js +206 -0
  60. package/lib/Utils/baileys-event-stream.js +63 -0
  61. package/lib/Utils/business.js +234 -0
  62. package/lib/Utils/chat-utils.js +730 -0
  63. package/lib/Utils/crypto.js +151 -0
  64. package/lib/Utils/decode-wa-message.js +198 -0
  65. package/lib/Utils/event-buffer.js +520 -0
  66. package/lib/Utils/generics.js +445 -0
  67. package/lib/Utils/history.js +94 -0
  68. package/lib/Utils/index.js +33 -0
  69. package/lib/Utils/link-preview.js +126 -0
  70. package/lib/Utils/logger.js +7 -0
  71. package/lib/Utils/lt-hash.js +51 -0
  72. package/lib/Utils/make-mutex.js +43 -0
  73. package/lib/Utils/messages-media.js +724 -0
  74. package/lib/Utils/messages.js +1018 -0
  75. package/lib/Utils/noise-handler.js +155 -0
  76. package/lib/Utils/process-message.js +372 -0
  77. package/lib/Utils/signal.js +143 -0
  78. package/lib/Utils/use-multi-file-auth-state.js +93 -0
  79. package/lib/Utils/validate-connection.js +207 -0
  80. package/lib/WABinary/constants.js +40 -0
  81. package/lib/WABinary/decode.js +265 -0
  82. package/lib/WABinary/encode.js +250 -0
  83. package/lib/WABinary/generic-utils.js +110 -0
  84. package/lib/WABinary/index.js +21 -0
  85. package/lib/WABinary/jid-utils.js +69 -0
  86. package/lib/WABinary/types.js +2 -0
  87. package/lib/WAM/BinaryInfo.js +13 -0
  88. package/lib/WAM/constants.js +15350 -0
  89. package/lib/WAM/encode.js +155 -0
  90. package/lib/WAM/index.js +19 -0
  91. package/lib/WAUSync/Protocols/USyncContactProtocol.js +32 -0
  92. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +57 -0
  93. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +30 -0
  94. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +42 -0
  95. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +53 -0
  96. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +24 -0
  97. package/lib/WAUSync/Protocols/index.js +20 -0
  98. package/lib/WAUSync/USyncQuery.js +89 -0
  99. package/lib/WAUSync/USyncUser.js +26 -0
  100. package/lib/WAUSync/index.js +19 -0
  101. package/lib/index.js +33 -0
  102. package/package.json +84 -0
@@ -0,0 +1,248 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractNewsletterMetadata = exports.makeNewsletterSocket = void 0;
4
+ const Types_1 = require("../Types");
5
+ const Utils_1 = require("../Utils");
6
+ const WABinary_1 = require("../WABinary");
7
+ const groups_1 = require("./groups");
8
+ var QueryIds;
9
+ (function (QueryIds) {
10
+ QueryIds["JOB_MUTATION"] = "7150902998257522";
11
+ QueryIds["METADATA"] = "6620195908089573";
12
+ QueryIds["UNFOLLOW"] = "7238632346214362";
13
+ QueryIds["FOLLOW"] = "7871414976211147";
14
+ QueryIds["UNMUTE"] = "7337137176362961";
15
+ QueryIds["MUTE"] = "25151904754424642";
16
+ QueryIds["CREATE"] = "6996806640408138";
17
+ QueryIds["ADMIN_COUNT"] = "7130823597031706";
18
+ QueryIds["CHANGE_OWNER"] = "7341777602580933";
19
+ QueryIds["DELETE"] = "8316537688363079";
20
+ QueryIds["DEMOTE"] = "6551828931592903";
21
+ })(QueryIds || (QueryIds = {}));
22
+ const makeNewsletterSocket = (config) => {
23
+ const sock = (0, groups_1.makeGroupsSocket)(config);
24
+ const { authState, signalRepository, query, generateMessageTag } = sock;
25
+ const encoder = new TextEncoder();
26
+ const newsletterQuery = async (jid, type, content) => (query({
27
+ tag: 'iq',
28
+ attrs: {
29
+ id: generateMessageTag(),
30
+ type,
31
+ xmlns: 'newsletter',
32
+ to: jid,
33
+ },
34
+ content
35
+ }));
36
+ const newsletterWMexQuery = async (jid, query_id, content) => (query({
37
+ tag: 'iq',
38
+ attrs: {
39
+ id: generateMessageTag(),
40
+ type: 'get',
41
+ xmlns: 'w:mex',
42
+ to: WABinary_1.S_WHATSAPP_NET,
43
+ },
44
+ content: [
45
+ {
46
+ tag: 'query',
47
+ attrs: { query_id },
48
+ content: encoder.encode(JSON.stringify({ variables: { newsletter_id: jid, ...content } }))
49
+ }
50
+ ]
51
+ }));
52
+ const parseFetchedUpdates = async (node, type) => {
53
+ let child;
54
+ if (type === 'messages')
55
+ child = (0, WABinary_1.getBinaryNodeChild)(node, 'messages');
56
+ else {
57
+ const parent = (0, WABinary_1.getBinaryNodeChild)(node, 'message_updates');
58
+ child = (0, WABinary_1.getBinaryNodeChild)(parent, 'messages');
59
+ }
60
+ return await Promise.all((0, WABinary_1.getAllBinaryNodeChildren)(child).map(async (messageNode) => {
61
+ var _a, _b;
62
+ messageNode.attrs.from = child === null || child === void 0 ? void 0 : child.attrs.jid;
63
+ const views = (_b = (_a = (0, WABinary_1.getBinaryNodeChild)(messageNode, 'views_count')) === null || _a === void 0 ? void 0 : _a.attrs) === null || _b === void 0 ? void 0 : _b.count;
64
+ const reactionNode = (0, WABinary_1.getBinaryNodeChild)(messageNode, 'reactions');
65
+ const reactions = (0, WABinary_1.getBinaryNodeChildren)(reactionNode, 'reaction')
66
+ .map(({ attrs }) => ({ count: +attrs.count, code: attrs.code }));
67
+ let data;
68
+ if (type === 'messages') {
69
+ const { fullMessage: message, decrypt } = await (0, Utils_1.decryptMessageNode)(messageNode, authState.creds.me.id, authState.creds.me.lid || '', signalRepository, config.logger);
70
+ await decrypt();
71
+ data = {
72
+ server_id: messageNode.attrs.server_id,
73
+ views: views ? +views : undefined,
74
+ reactions,
75
+ message
76
+ };
77
+ return data;
78
+ }
79
+ else {
80
+ data = {
81
+ server_id: messageNode.attrs.server_id,
82
+ views: views ? +views : undefined,
83
+ reactions
84
+ };
85
+ return data;
86
+ }
87
+ }));
88
+ };
89
+ return {
90
+ ...sock,
91
+ subscribeNewsletterUpdates: async (jid) => {
92
+ var _a;
93
+ const result = await newsletterQuery(jid, 'set', [{ tag: 'live_updates', attrs: {}, content: [] }]);
94
+ return (_a = (0, WABinary_1.getBinaryNodeChild)(result, 'live_updates')) === null || _a === void 0 ? void 0 : _a.attrs;
95
+ },
96
+ newsletterReactionMode: async (jid, mode) => {
97
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
98
+ updates: { settings: { reaction_codes: { value: mode } } }
99
+ });
100
+ },
101
+ newsletterUpdateDescription: async (jid, description) => {
102
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
103
+ updates: { description: description || '', settings: null }
104
+ });
105
+ },
106
+ newsletterUpdateName: async (jid, name) => {
107
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
108
+ updates: { name, settings: null }
109
+ });
110
+ },
111
+ newsletterUpdatePicture: async (jid, content) => {
112
+ const { img } = await (0, Utils_1.generateProfilePicture)(content);
113
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
114
+ updates: { picture: img.toString('base64'), settings: null }
115
+ });
116
+ },
117
+ newsletterRemovePicture: async (jid) => {
118
+ await newsletterWMexQuery(jid, QueryIds.JOB_MUTATION, {
119
+ updates: { picture: '', settings: null }
120
+ });
121
+ },
122
+ newsletterUnfollow: async (jid) => {
123
+ await newsletterWMexQuery(jid, QueryIds.UNFOLLOW);
124
+ },
125
+ newsletterFollow: async (jid) => {
126
+ await newsletterWMexQuery(jid, QueryIds.FOLLOW);
127
+ },
128
+ newsletterUnmute: async (jid) => {
129
+ await newsletterWMexQuery(jid, QueryIds.UNMUTE);
130
+ },
131
+ newsletterMute: async (jid) => {
132
+ await newsletterWMexQuery(jid, QueryIds.MUTE);
133
+ },
134
+ newsletterCreate: async (name, description) => {
135
+ await query({
136
+ tag: 'iq',
137
+ attrs: {
138
+ to: WABinary_1.S_WHATSAPP_NET,
139
+ xmlns: 'tos',
140
+ id: generateMessageTag(),
141
+ type: 'set'
142
+ },
143
+ content: [
144
+ {
145
+ tag: 'notice',
146
+ attrs: {
147
+ id: '20601218',
148
+ stage: '5'
149
+ },
150
+ content: []
151
+ }
152
+ ]
153
+ });
154
+ const result = await newsletterWMexQuery(undefined, QueryIds.CREATE, {
155
+ input: { name, description }
156
+ });
157
+ return (0, exports.extractNewsletterMetadata)(result, true);
158
+ },
159
+ newsletterMetadata: async (type, key, role) => {
160
+ const result = await newsletterWMexQuery(undefined, QueryIds.METADATA, {
161
+ input: {
162
+ key,
163
+ type: type.toUpperCase(),
164
+ view_role: role || 'GUEST'
165
+ },
166
+ fetch_viewer_metadata: true,
167
+ fetch_full_image: true,
168
+ fetch_creation_time: true
169
+ });
170
+ return (0, exports.extractNewsletterMetadata)(result);
171
+ },
172
+ newsletterAdminCount: async (jid) => {
173
+ var _a, _b;
174
+ const result = await newsletterWMexQuery(jid, QueryIds.ADMIN_COUNT);
175
+ const buff = (_b = (_a = (0, WABinary_1.getBinaryNodeChild)(result, 'result')) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.toString();
176
+ return JSON.parse(buff).data[Types_1.XWAPaths.ADMIN_COUNT].admin_count;
177
+ },
178
+ /**user is Lid, not Jid */
179
+ newsletterChangeOwner: async (jid, user) => {
180
+ await newsletterWMexQuery(jid, QueryIds.CHANGE_OWNER, {
181
+ user_id: user
182
+ });
183
+ },
184
+ /**user is Lid, not Jid */
185
+ newsletterDemote: async (jid, user) => {
186
+ await newsletterWMexQuery(jid, QueryIds.DEMOTE, {
187
+ user_id: user
188
+ });
189
+ },
190
+ newsletterDelete: async (jid) => {
191
+ await newsletterWMexQuery(jid, QueryIds.DELETE);
192
+ },
193
+ /**if code wasn't passed, the reaction will be removed (if is reacted) */
194
+ newsletterReactMessage: async (jid, server_id, code) => {
195
+ await query({
196
+ tag: 'message',
197
+ attrs: { to: jid, ...(!code ? { edit: '7' } : {}), type: 'reaction', server_id, id: (0, Utils_1.generateMessageID)() },
198
+ content: [{
199
+ tag: 'reaction',
200
+ attrs: code ? { code } : {}
201
+ }]
202
+ });
203
+ },
204
+ newsletterFetchMessages: async (type, key, count, after) => {
205
+ const result = await newsletterQuery(WABinary_1.S_WHATSAPP_NET, 'get', [
206
+ {
207
+ tag: 'messages',
208
+ attrs: { type, ...(type === 'invite' ? { key } : { jid: key }), count: count.toString(), after: (after === null || after === void 0 ? void 0 : after.toString()) || '100' }
209
+ }
210
+ ]);
211
+ return await parseFetchedUpdates(result, 'messages');
212
+ },
213
+ newsletterFetchUpdates: async (jid, count, after, since) => {
214
+ const result = await newsletterQuery(jid, 'get', [
215
+ {
216
+ tag: 'message_updates',
217
+ attrs: { count: count.toString(), after: (after === null || after === void 0 ? void 0 : after.toString()) || '100', since: (since === null || since === void 0 ? void 0 : since.toString()) || '0' }
218
+ }
219
+ ]);
220
+ return await parseFetchedUpdates(result, 'updates');
221
+ }
222
+ };
223
+ };
224
+ exports.makeNewsletterSocket = makeNewsletterSocket;
225
+ const extractNewsletterMetadata = (node, isCreate) => {
226
+ var _a, _b, _c, _d;
227
+ const result = (_b = (_a = (0, WABinary_1.getBinaryNodeChild)(node, 'result')) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.toString();
228
+ const metadataPath = JSON.parse(result).data[isCreate ? Types_1.XWAPaths.CREATE : Types_1.XWAPaths.NEWSLETTER];
229
+ const metadata = {
230
+ id: metadataPath.id,
231
+ state: metadataPath.state.type,
232
+ creation_time: +metadataPath.thread_metadata.creation_time,
233
+ name: metadataPath.thread_metadata.name.text,
234
+ nameTime: +metadataPath.thread_metadata.name.update_time,
235
+ description: metadataPath.thread_metadata.description.text,
236
+ descriptionTime: +metadataPath.thread_metadata.description.update_time,
237
+ invite: metadataPath.thread_metadata.invite,
238
+ handle: metadataPath.thread_metadata.handle,
239
+ picture: ((_c = metadataPath.thread_metadata.picture) === null || _c === void 0 ? void 0 : _c.direct_path) || null,
240
+ preview: ((_d = metadataPath.thread_metadata.preview) === null || _d === void 0 ? void 0 : _d.direct_path) || null,
241
+ reaction_codes: metadataPath.thread_metadata.settings.reaction_codes.value,
242
+ subscribers: +metadataPath.thread_metadata.subscribers_count,
243
+ verification: metadataPath.thread_metadata.verification,
244
+ viewer_metadata: metadataPath.viewer_metadata
245
+ };
246
+ return metadata;
247
+ };
248
+ exports.extractNewsletterMetadata = extractNewsletterMetadata;
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.makeRegistrationSocket = void 0;
7
+ exports.registrationParams = registrationParams;
8
+ exports.mobileRegisterCode = mobileRegisterCode;
9
+ exports.mobileRegisterExists = mobileRegisterExists;
10
+ exports.mobileRegister = mobileRegister;
11
+ exports.mobileRegisterEncrypt = mobileRegisterEncrypt;
12
+ exports.mobileRegisterFetch = mobileRegisterFetch;
13
+ /* eslint-disable camelcase */
14
+ const axios_1 = __importDefault(require("axios"));
15
+ const Defaults_1 = require("../Defaults");
16
+ const crypto_1 = require("../Utils/crypto");
17
+ const WABinary_1 = require("../WABinary");
18
+ const business_1 = require("./business");
19
+ function urlencode(str) {
20
+ return str.replace(/-/g, '%2d').replace(/_/g, '%5f').replace(/~/g, '%7e');
21
+ }
22
+ const validRegistrationOptions = (config) => (config === null || config === void 0 ? void 0 : config.phoneNumberCountryCode) &&
23
+ config.phoneNumberNationalNumber &&
24
+ config.phoneNumberMobileCountryCode;
25
+ const makeRegistrationSocket = (config) => {
26
+ const sock = (0, business_1.makeBusinessSocket)(config);
27
+ const register = async (code) => {
28
+ if (!validRegistrationOptions(config.auth.creds.registration)) {
29
+ throw new Error('please specify the registration options');
30
+ }
31
+ const result = await mobileRegister({ ...sock.authState.creds, ...sock.authState.creds.registration, code }, config.options);
32
+ sock.authState.creds.me = {
33
+ id: (0, WABinary_1.jidEncode)(result.login, 's.whatsapp.net'),
34
+ name: '~'
35
+ };
36
+ sock.authState.creds.registered = true;
37
+ sock.ev.emit('creds.update', sock.authState.creds);
38
+ return result;
39
+ };
40
+ const requestRegistrationCode = async (registrationOptions) => {
41
+ registrationOptions = registrationOptions || config.auth.creds.registration;
42
+ if (!validRegistrationOptions(registrationOptions)) {
43
+ throw new Error('Invalid registration options');
44
+ }
45
+ sock.authState.creds.registration = registrationOptions;
46
+ sock.ev.emit('creds.update', sock.authState.creds);
47
+ return mobileRegisterCode({ ...config.auth.creds, ...registrationOptions }, config.options);
48
+ };
49
+ return {
50
+ ...sock,
51
+ register,
52
+ requestRegistrationCode,
53
+ };
54
+ };
55
+ exports.makeRegistrationSocket = makeRegistrationSocket;
56
+ function convertBufferToUrlHex(buffer) {
57
+ var id = '';
58
+ buffer.forEach((x) => {
59
+ // encode random identity_id buffer as percentage url encoding
60
+ id += `%${x.toString(16).padStart(2, '0').toLowerCase()}`;
61
+ });
62
+ return id;
63
+ }
64
+ function registrationParams(params) {
65
+ const e_regid = Buffer.alloc(4);
66
+ e_regid.writeInt32BE(params.registrationId);
67
+ const e_skey_id = Buffer.alloc(3);
68
+ e_skey_id.writeInt16BE(params.signedPreKey.keyId);
69
+ params.phoneNumberCountryCode = params.phoneNumberCountryCode.replace('+', '').trim();
70
+ params.phoneNumberNationalNumber = params.phoneNumberNationalNumber.replace(/[/-\s)(]/g, '').trim();
71
+ return {
72
+ cc: params.phoneNumberCountryCode,
73
+ in: params.phoneNumberNationalNumber,
74
+ Rc: '0',
75
+ lg: 'en',
76
+ lc: 'GB',
77
+ mistyped: '6',
78
+ authkey: Buffer.from(params.noiseKey.public).toString('base64url'),
79
+ e_regid: e_regid.toString('base64url'),
80
+ e_keytype: 'BQ',
81
+ e_ident: Buffer.from(params.signedIdentityKey.public).toString('base64url'),
82
+ // e_skey_id: e_skey_id.toString('base64url'),
83
+ e_skey_id: 'AAAA',
84
+ e_skey_val: Buffer.from(params.signedPreKey.keyPair.public).toString('base64url'),
85
+ e_skey_sig: Buffer.from(params.signedPreKey.signature).toString('base64url'),
86
+ fdid: params.phoneId,
87
+ network_ratio_type: '1',
88
+ expid: params.deviceId,
89
+ simnum: '1',
90
+ hasinrc: '1',
91
+ pid: Math.floor(Math.random() * 1000).toString(),
92
+ id: convertBufferToUrlHex(params.identityId),
93
+ backup_token: convertBufferToUrlHex(params.backupToken),
94
+ token: (0, crypto_1.md5)(Buffer.concat([Defaults_1.MOBILE_TOKEN, Buffer.from(params.phoneNumberNationalNumber)])).toString('hex'),
95
+ fraud_checkpoint_code: params.captcha,
96
+ };
97
+ }
98
+ /**
99
+ * Requests a registration code for the given phone number.
100
+ */
101
+ function mobileRegisterCode(params, fetchOptions) {
102
+ return mobileRegisterFetch('/code', {
103
+ params: {
104
+ ...registrationParams(params),
105
+ mcc: `${params.phoneNumberMobileCountryCode}`.padStart(3, '0'),
106
+ mnc: `${params.phoneNumberMobileNetworkCode || '001'}`.padStart(3, '0'),
107
+ sim_mcc: '000',
108
+ sim_mnc: '000',
109
+ method: (params === null || params === void 0 ? void 0 : params.method) || 'sms',
110
+ reason: '',
111
+ hasav: '1'
112
+ },
113
+ ...fetchOptions,
114
+ });
115
+ }
116
+ function mobileRegisterExists(params, fetchOptions) {
117
+ return mobileRegisterFetch('/exist', {
118
+ params: registrationParams(params),
119
+ ...fetchOptions
120
+ });
121
+ }
122
+ /**
123
+ * Registers the phone number on whatsapp with the received OTP code.
124
+ */
125
+ async function mobileRegister(params, fetchOptions) {
126
+ //const result = await mobileRegisterFetch(`/reg_onboard_abprop?cc=${params.phoneNumberCountryCode}&in=${params.phoneNumberNationalNumber}&rc=0`)
127
+ return mobileRegisterFetch('/register', {
128
+ params: { ...registrationParams(params), code: params.code.replace('-', '') },
129
+ ...fetchOptions,
130
+ });
131
+ }
132
+ /**
133
+ * Encrypts the given string as AEAD aes-256-gcm with the public whatsapp key and a random keypair.
134
+ */
135
+ function mobileRegisterEncrypt(data) {
136
+ const keypair = crypto_1.Curve.generateKeyPair();
137
+ const key = crypto_1.Curve.sharedKey(keypair.private, Defaults_1.REGISTRATION_PUBLIC_KEY);
138
+ const buffer = (0, crypto_1.aesEncryptGCM)(Buffer.from(data), new Uint8Array(key), Buffer.alloc(12), Buffer.alloc(0));
139
+ return Buffer.concat([Buffer.from(keypair.public), buffer]).toString('base64url');
140
+ }
141
+ async function mobileRegisterFetch(path, opts = {}) {
142
+ let url = `${Defaults_1.MOBILE_REGISTRATION_ENDPOINT}${path}`;
143
+ if (opts.params) {
144
+ const parameter = [];
145
+ for (const param in opts.params) {
146
+ if (opts.params[param] !== null && opts.params[param] !== undefined) {
147
+ parameter.push(param + '=' + urlencode(opts.params[param]));
148
+ }
149
+ }
150
+ url += `?${parameter.join('&')}`;
151
+ delete opts.params;
152
+ }
153
+ if (!opts.headers) {
154
+ opts.headers = {};
155
+ }
156
+ opts.headers['User-Agent'] = Defaults_1.MOBILE_USERAGENT;
157
+ const response = await (0, axios_1.default)(url, opts);
158
+ var json = response.data;
159
+ if (response.status > 300 || json.reason) {
160
+ throw json;
161
+ }
162
+ if (json.status && !['ok', 'sent'].includes(json.status)) {
163
+ throw json;
164
+ }
165
+ return json;
166
+ }