@itznomo/baileys 0.0.0-stage → 0.0.1

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 (124) hide show
  1. package/BUTTONS.md +153 -0
  2. package/Example/call-bot.mjs +38 -0
  3. package/Example/example.mjs +36 -0
  4. package/Example/full-bot-commands.mjs +319 -0
  5. package/Example/full-bot.mjs +77 -0
  6. package/LICENSE +21 -0
  7. package/README.md +543 -2
  8. package/WAProto/@dnuzi +1 -0
  9. package/WAProto/index.d.ts +15932 -0
  10. package/WAProto/index.js +113300 -0
  11. package/assets/banner.svg +17 -0
  12. package/assets/devider.svg +1 -0
  13. package/assets/features.svg +21 -0
  14. package/docs/CALLS.md +106 -0
  15. package/docs/FEATURES.md +1152 -0
  16. package/engine-requirements.js +19 -0
  17. package/index.d.ts +815 -0
  18. package/package.json +51 -4
  19. package/src/binary/wa-binary-constants.js +1300 -0
  20. package/src/binary/wa-binary.js +627 -0
  21. package/src/constants.js +147 -0
  22. package/src/defaults.js +106 -0
  23. package/src/foundation/ai-rich.js +654 -0
  24. package/src/foundation/audio-duration.js +470 -0
  25. package/src/foundation/audio-samples.js +250 -0
  26. package/src/foundation/boom.js +39 -0
  27. package/src/foundation/builders.js +851 -0
  28. package/src/foundation/cache.js +134 -0
  29. package/src/foundation/concurrency.js +69 -0
  30. package/src/foundation/curve25519.js +189 -0
  31. package/src/foundation/image-thumb.js +65 -0
  32. package/src/foundation/link-preview.js +278 -0
  33. package/src/foundation/logger.js +78 -0
  34. package/src/foundation/long.js +130 -0
  35. package/src/foundation/performance.js +385 -0
  36. package/src/foundation/protobuf-minimal.js +587 -0
  37. package/src/foundation/qrcode-terminal.js +273 -0
  38. package/src/foundation/sql-database.js +716 -0
  39. package/src/foundation/wa-crypto.js +47 -0
  40. package/src/foundation/zip.js +108 -0
  41. package/src/index.js +67 -0
  42. package/src/signal/libsignal.js +996 -0
  43. package/src/signal/signal-group.js +455 -0
  44. package/src/signal/signal-repository.js +635 -0
  45. package/src/socket/business-communities.js +811 -0
  46. package/src/socket/chats.js +702 -0
  47. package/src/socket/groups-newsletter.js +524 -0
  48. package/src/socket/messages-recv.js +1815 -0
  49. package/src/socket/messages-send.js +1526 -0
  50. package/src/socket/socket-builder.js +131 -0
  51. package/src/socket/socket-core.js +913 -0
  52. package/src/socket/usync.js +287 -0
  53. package/src/socket-client/native-websocket.js +344 -0
  54. package/src/socket-client/websocket-client.js +170 -0
  55. package/src/utils/auth-state-core.js +731 -0
  56. package/src/utils/auth-state-storage.js +594 -0
  57. package/src/utils/chat-sync.js +1046 -0
  58. package/src/utils/check-status.js +159 -0
  59. package/src/utils/cstoken.js +17 -0
  60. package/src/utils/media.js +807 -0
  61. package/src/utils/message-compose.js +1747 -0
  62. package/src/utils/message-processing.js +912 -0
  63. package/src/utils/modern-messages.js +435 -0
  64. package/src/utils/nomo-buttons.js +138 -0
  65. package/src/utils/nomo-commands.js +168 -0
  66. package/src/utils/spam-report-utils.js +136 -0
  67. package/src/utils/use-keyv-auth-state.js +307 -0
  68. package/src/utils/username.js +19 -0
  69. package/src/utils/wa-protocol-core.js +1227 -0
  70. package/src/voip/WaVoipCoordinator.js +158 -0
  71. package/src/voip/app-data/WaAppDataStream.js +193 -0
  72. package/src/voip/app-data/protocol.js +172 -0
  73. package/src/voip/auto.js +139 -0
  74. package/src/voip/bytes.js +63 -0
  75. package/src/voip/call/WaCallManager.js +442 -0
  76. package/src/voip/call/WaCallMediaSession.js +1727 -0
  77. package/src/voip/call/call-state.js +203 -0
  78. package/src/voip/crypto/encryption.js +13 -0
  79. package/src/voip/crypto/primitives.js +21 -0
  80. package/src/voip/crypto/rtcp.js +66 -0
  81. package/src/voip/crypto/srtp.js +180 -0
  82. package/src/voip/crypto/ssrc.js +15 -0
  83. package/src/voip/index.js +730 -0
  84. package/src/voip/media/WaAudioEngine.js +486 -0
  85. package/src/voip/media/WaVideoEngine.js +275 -0
  86. package/src/voip/media/audio-codec.js +0 -0
  87. package/src/voip/media/audio-reorder.js +107 -0
  88. package/src/voip/media/h264.js +246 -0
  89. package/src/voip/media/mlow-codec.js +465 -0
  90. package/src/voip/media/rtcp.js +68 -0
  91. package/src/voip/media/rtp.js +242 -0
  92. package/src/voip/protobuf.js +30 -0
  93. package/src/voip/relay/WaManualRelay.js +609 -0
  94. package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
  95. package/src/voip/relay/dtls/aead.js +46 -0
  96. package/src/voip/relay/dtls/cert-builder.js +102 -0
  97. package/src/voip/relay/dtls/der.js +98 -0
  98. package/src/voip/relay/dtls/ecdhe.js +37 -0
  99. package/src/voip/relay/dtls/handshake-framing.js +100 -0
  100. package/src/voip/relay/dtls/handshake-messages.js +158 -0
  101. package/src/voip/relay/dtls/handshake.js +582 -0
  102. package/src/voip/relay/dtls/prf.js +39 -0
  103. package/src/voip/relay/dtls/record.js +56 -0
  104. package/src/voip/relay/relay-ack.js +141 -0
  105. package/src/voip/relay/sctp/association.js +434 -0
  106. package/src/voip/relay/sctp/crc32c.js +21 -0
  107. package/src/voip/relay/sctp/wire.js +226 -0
  108. package/src/voip/relay/stun.js +463 -0
  109. package/src/voip/shim/baileys-resolve.js +7 -0
  110. package/src/voip/shim/core.js +66 -0
  111. package/src/voip/shim/crypto.js +6 -0
  112. package/src/voip/shim/proto.js +10 -0
  113. package/src/voip/shim/protocol.js +130 -0
  114. package/src/voip/shim/transport.js +82 -0
  115. package/src/voip/shim/util.js +207 -0
  116. package/src/voip/signaling/bridge.js +102 -0
  117. package/src/voip/signaling/screen-share.js +60 -0
  118. package/src/voip/signaling/signaling.js +491 -0
  119. package/src/voip/types.js +69 -0
  120. package/src/voip/voip-deps.js +243 -0
  121. package/src/voip/voipClient.js +314 -0
  122. package/src/wam/wam-constants.js +22852 -0
  123. package/src/wam/wam.js +146 -0
  124. package/voip.d.ts +144 -0
@@ -0,0 +1,159 @@
1
+ import { initAuthCreds } from './auth-state-core.js'
2
+
3
+ const MOBILE_REGISTRATION_ENDPOINT = 'https://v.whatsapp.net/v2'
4
+ const MOBILE_USERAGENT = 'WhatsApp/2.24.12.78 iOS/17.5.1 Device/Apple-iPhone_13'
5
+ const GRAPHQL_ENDPOINT = 'https://graph.whatsapp.com/graphql'
6
+ const GRAPHQL_ACCESS_TOKEN = 'WA|1015890928915437|3201f239340c1c8ec6262a6dad04200e'
7
+ const GRAPHQL_BAN_STATUS_DOC_ID = '25573756098908502'
8
+
9
+ export async function getBanDetails(appealToken) {
10
+ try {
11
+ const res = await fetch(GRAPHQL_ENDPOINT, {
12
+ method: 'POST',
13
+ headers: {
14
+ 'User-Agent': MOBILE_USERAGENT,
15
+ 'Content-Type': 'application/json'
16
+ },
17
+ body: JSON.stringify({
18
+ variables: JSON.stringify({ app_id: 'dev.app.id', request_token: appealToken }),
19
+ access_token: GRAPHQL_ACCESS_TOKEN,
20
+ doc_id: GRAPHQL_BAN_STATUS_DOC_ID,
21
+ lang: 'en_GB',
22
+ 'Content-Type': 'application/json'
23
+ })
24
+ })
25
+ const data = await res.json().catch(() => ({}))
26
+ return data?.data?.whatsapp_support_ban_appeal_status || null
27
+ } catch {
28
+ return null
29
+ }
30
+ }
31
+
32
+ export function registrationParams(params) {
33
+ const e_regid = Buffer.alloc(4)
34
+ e_regid.writeInt32BE(params.registrationId || 0)
35
+ const e_skey_id = Buffer.alloc(3)
36
+ e_skey_id.writeInt16BE(params.signedPreKey?.keyId || 1)
37
+ const cc = (params.phoneNumberCountryCode || '').replace('+', '').trim()
38
+ const in_num = (params.phoneNumberNationalNumber || '').replace(/[/\-\s)(]/g, '').trim()
39
+ return {
40
+ cc,
41
+ in: in_num,
42
+ Rc: '0',
43
+ lg: 'en',
44
+ lc: 'GB',
45
+ mistyped: '6',
46
+ authkey: Buffer.from(params.noiseKey.public).toString('base64url'),
47
+ e_regid: e_regid.toString('base64url'),
48
+ e_keytype: 'BQ',
49
+ e_ident: Buffer.from(params.signedIdentityKey.public).toString('base64url'),
50
+ e_skey_id: e_skey_id.toString('base64url'),
51
+ e_skey_val: Buffer.from(params.signedPreKey.keyPair.public).toString('base64url'),
52
+ e_skey_sig: Buffer.from(params.signedPreKey.signature).toString('base64url'),
53
+ fdid: params.phoneId,
54
+ network_ratio_type: '1',
55
+ expid: params.deviceId,
56
+ simnum: '1',
57
+ hasav: '2'
58
+ }
59
+ }
60
+
61
+ export async function mobileRegisterExists(params) {
62
+ const p = registrationParams(params)
63
+ const qs = Object.entries(p)
64
+ .filter(([, v]) => v !== null && v !== undefined)
65
+ .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
66
+ .join('&')
67
+ const url = `${MOBILE_REGISTRATION_ENDPOINT}/exist?${qs}`
68
+ const res = await fetch(url, {
69
+ headers: { 'User-Agent': MOBILE_USERAGENT }
70
+ })
71
+ const json = await res.json().catch(() => ({}))
72
+ if (!res.ok || json.reason) throw json
73
+ if (json.status && !['ok', 'sent'].includes(json.status)) throw json
74
+ return json
75
+ }
76
+
77
+ /**
78
+ * Parses international phone numbers into country code and national number.
79
+ * Supports optional `libphonenumber-js` if installed, with a reliable fallback.
80
+ */
81
+ async function parsePhoneNumberParts(formattedNumber) {
82
+ try {
83
+ const libphone = await import('libphonenumber-js')
84
+ const fn = libphone.parsePhoneNumberWithError || libphone.default?.parsePhoneNumberWithError
85
+ if (typeof fn === 'function') {
86
+ const parsed = fn(formattedNumber)
87
+ return { countryCode: String(parsed.countryCallingCode), nationalNumber: String(parsed.nationalNumber) }
88
+ }
89
+ } catch {
90
+ // Fallback without libphonenumber-js
91
+ }
92
+
93
+ const digits = formattedNumber.replace(/\D/g, '')
94
+ // Simple country code matching (1 to 3 digits)
95
+ const prefixes = ['1', '7', '20', '27', '30', '31', '32', '33', '34', '36', '39', '40', '41', '43', '44', '45', '46', '47', '48', '49', '51', '52', '53', '54', '55', '56', '57', '58', '60', '61', '62', '63', '64', '65', '66', '81', '82', '84', '86', '90', '91', '92', '93', '94', '95', '98', '212', '213', '216', '218', '220', '221', '234', '254', '351', '352', '353', '354', '358', '380', '880', '966', '971', '972', '973', '974', '977']
96
+ for (const p of prefixes.sort((a, b) => b.length - a.length)) {
97
+ if (digits.startsWith(p)) {
98
+ return { countryCode: p, nationalNumber: digits.slice(p.length) }
99
+ }
100
+ }
101
+ return { countryCode: digits.slice(0, 2), nationalNumber: digits.slice(2) }
102
+ }
103
+
104
+ /**
105
+ * Check the status of a WhatsApp phone number.
106
+ * Detects whether the number is active, banned (with appeal details), blocked, or rate limited.
107
+ *
108
+ * @param {string} phoneNumber
109
+ * @returns {Promise<{ number: string, status: string, isBanned: boolean, isNeedOfficialWa: boolean, banInfo: object|null }>}
110
+ */
111
+ export const checkStatusWA = async phoneNumber => {
112
+ if (!phoneNumber) throw new Error('Please provide a phone number')
113
+
114
+ let num = String(phoneNumber).trim()
115
+ if (num.includes('@')) num = num.split('@')[0]
116
+ if (num.includes(':')) num = num.split(':')[0]
117
+ if (!num.startsWith('+')) num = '+' + num
118
+
119
+ const { countryCode, nationalNumber } = await parsePhoneNumberParts(num)
120
+ const state = { creds: initAuthCreds() }
121
+
122
+ const build = (status, isBanned, isNeedOfficialWa, banInfo = null) => ({
123
+ number: num,
124
+ status,
125
+ isBanned,
126
+ isNeedOfficialWa,
127
+ banInfo
128
+ })
129
+
130
+ try {
131
+ await mobileRegisterExists({
132
+ ...state.creds,
133
+ phoneNumberCountryCode: countryCode,
134
+ phoneNumberNationalNumber: nationalNumber
135
+ })
136
+ return build('active', false, false)
137
+ } catch (err) {
138
+ if (err?.appeal_token) {
139
+ const banDetails = await getBanDetails(err.appeal_token)
140
+ const appealStatus = banDetails?.status || null
141
+ const banType = appealStatus === 'BANNED' ? 'permanent' : 'temporary'
142
+ return build('banned', true, false, {
143
+ banType,
144
+ violationType: err.violation_type || null,
145
+ violationReason: err.violation_type ? `Type ${err.violation_type}` : 'Unknown',
146
+ canAppeal: banType !== 'permanent',
147
+ appealToken: err.appeal_token,
148
+ banTime: banDetails?.ban_time || null,
149
+ banDate: banDetails?.ban_time ? new Date(banDetails.ban_time * 1000).toISOString() : null,
150
+ appealStatus,
151
+ appealCreatedAt: banDetails?.appeal_creation_time ? new Date(banDetails.appeal_creation_time * 1000).toISOString() : null
152
+ })
153
+ }
154
+ if (err?.custom_block_screen) return build('blocked', false, true)
155
+ if (err?.reason === 'incorrect') return build('active', false, false)
156
+ if (err?.reason === 'temporarily_unavailable') return build('rate_limited', false, false)
157
+ return build('error', false, false)
158
+ }
159
+ }
@@ -0,0 +1,17 @@
1
+ import { isJidBot, isJidMetaAI, isLidUser, isPnUser, jidNormalizedUser } from '../binary/wa-binary.js'
2
+ import { hmacSign } from './wa-protocol-core.js'
3
+
4
+ const PSA_WID = '0@c.us'
5
+
6
+ export const shouldSendCsToken = jid => {
7
+ const n = jidNormalizedUser(jid)
8
+ return !!n && (isPnUser(n) || isLidUser(n)) && n !== jidNormalizedUser(PSA_WID) && !isJidBot(jid) && !isJidMetaAI(jid)
9
+ }
10
+
11
+ export const generateCsToken = async ({ jid, nctSalt, getLIDForPN }) => {
12
+ if (!nctSalt?.length || !shouldSendCsToken(jid)) return undefined
13
+ const n = jidNormalizedUser(jid)
14
+ const lid = isLidUser(n) ? n : jidNormalizedUser(await getLIDForPN(n))
15
+ if (!lid || !isLidUser(lid)) return undefined
16
+ return hmacSign(Buffer.from(lid), nctSalt, 'sha256')
17
+ }