@hiroosy/baileys-lite 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.
- package/README.md +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native replacement for `protobufjs/minimal` (+ @protobufjs/* + long), Node-only.
|
|
3
|
+
*
|
|
4
|
+
* Provides exactly the surface the generated WAProto/index.js consumes:
|
|
5
|
+
* Reader (BufferReader semantics), Writer (BufferWriter semantics), util, roots.
|
|
6
|
+
* Wire output and decode results are byte/shape-identical to protobufjs 7.5.x —
|
|
7
|
+
* verified by test/native-protobuf.mjs (differential + fuzz against the original).
|
|
8
|
+
*
|
|
9
|
+
* Intentionally NOT supported (unused by WAProto, browser-only in protobufjs):
|
|
10
|
+
* plain-Array buffers, Uint8Array pool, rpc, reflection, eventemitter, float polyfill.
|
|
11
|
+
*/
|
|
12
|
+
import { Long } from './long.js'
|
|
13
|
+
|
|
14
|
+
/* ------------------------------------------------------------------ */
|
|
15
|
+
/* LongBits (lo/hi as unsigned 32-bit) — port of protobufjs util/longbits */
|
|
16
|
+
/* ------------------------------------------------------------------ */
|
|
17
|
+
|
|
18
|
+
export class LongBits {
|
|
19
|
+
constructor(lo, hi) {
|
|
20
|
+
this.lo = lo >>> 0
|
|
21
|
+
this.hi = hi >>> 0
|
|
22
|
+
}
|
|
23
|
+
static fromNumber(value) {
|
|
24
|
+
if (value === 0) return LongBits.zero
|
|
25
|
+
const sign = value < 0
|
|
26
|
+
if (sign) value = -value
|
|
27
|
+
let lo = value >>> 0
|
|
28
|
+
let hi = ((value - lo) / 4294967296) >>> 0
|
|
29
|
+
if (sign) {
|
|
30
|
+
hi = ~hi >>> 0
|
|
31
|
+
lo = ~lo >>> 0
|
|
32
|
+
if (++lo > 4294967295) {
|
|
33
|
+
lo = 0
|
|
34
|
+
if (++hi > 4294967295) hi = 0
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return new LongBits(lo, hi)
|
|
38
|
+
}
|
|
39
|
+
static from(value) {
|
|
40
|
+
if (typeof value === 'number') return LongBits.fromNumber(value)
|
|
41
|
+
if (typeof value === 'string' || value instanceof String) return LongBits.from(Long.fromString(String(value)))
|
|
42
|
+
return value.low || value.high ? new LongBits(value.low >>> 0, value.high >>> 0) : LongBits.zero
|
|
43
|
+
}
|
|
44
|
+
toNumber(unsigned) {
|
|
45
|
+
if (!unsigned && this.hi >>> 31) {
|
|
46
|
+
const lo = ~this.lo + 1 >>> 0
|
|
47
|
+
let hi = ~this.hi >>> 0
|
|
48
|
+
if (!lo) hi = (hi + 1) >>> 0
|
|
49
|
+
return -(lo + hi * 4294967296)
|
|
50
|
+
}
|
|
51
|
+
return this.lo + this.hi * 4294967296
|
|
52
|
+
}
|
|
53
|
+
toLong(unsigned) {
|
|
54
|
+
return new Long(this.lo | 0, this.hi | 0, Boolean(unsigned))
|
|
55
|
+
}
|
|
56
|
+
zzEncode() {
|
|
57
|
+
const mask = this.hi >> 31
|
|
58
|
+
this.hi = (((this.hi << 1) | (this.lo >>> 31)) ^ mask) >>> 0
|
|
59
|
+
this.lo = ((this.lo << 1) ^ mask) >>> 0
|
|
60
|
+
return this
|
|
61
|
+
}
|
|
62
|
+
zzDecode() {
|
|
63
|
+
const mask = -(this.lo & 1)
|
|
64
|
+
this.lo = (((this.lo >>> 1) | (this.hi << 31)) ^ mask) >>> 0
|
|
65
|
+
this.hi = ((this.hi >>> 1) ^ mask) >>> 0
|
|
66
|
+
return this
|
|
67
|
+
}
|
|
68
|
+
length() {
|
|
69
|
+
const part0 = this.lo
|
|
70
|
+
const part1 = ((this.lo >>> 28) | (this.hi << 4)) >>> 0
|
|
71
|
+
const part2 = this.hi >>> 24
|
|
72
|
+
return part2 === 0
|
|
73
|
+
? part1 === 0
|
|
74
|
+
? part0 < 16384
|
|
75
|
+
? part0 < 128
|
|
76
|
+
? 1
|
|
77
|
+
: 2
|
|
78
|
+
: part0 < 2097152
|
|
79
|
+
? 3
|
|
80
|
+
: 4
|
|
81
|
+
: part1 < 16384
|
|
82
|
+
? part1 < 128
|
|
83
|
+
? 5
|
|
84
|
+
: 6
|
|
85
|
+
: part1 < 2097152
|
|
86
|
+
? 7
|
|
87
|
+
: 8
|
|
88
|
+
: part2 < 128
|
|
89
|
+
? 9
|
|
90
|
+
: 10
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
LongBits.zero = new LongBits(0, 0)
|
|
94
|
+
// the shared zero instance must stay immutable-in-effect (zz* return `this`, as in protobufjs)
|
|
95
|
+
LongBits.zero.toNumber = () => 0
|
|
96
|
+
LongBits.zero.zzEncode = LongBits.zero.zzDecode = function () {
|
|
97
|
+
return this
|
|
98
|
+
}
|
|
99
|
+
LongBits.zero.length = () => 1
|
|
100
|
+
|
|
101
|
+
/* ------------------------------------------------------------------ */
|
|
102
|
+
/* Reader */
|
|
103
|
+
/* ------------------------------------------------------------------ */
|
|
104
|
+
|
|
105
|
+
const indexOutOfRange = (reader, writeLength) =>
|
|
106
|
+
RangeError('index out of range: ' + reader.pos + ' + ' + (writeLength || 1) + ' > ' + reader.len)
|
|
107
|
+
|
|
108
|
+
function readVarint32NearEnd(reader) {
|
|
109
|
+
// Safely read up to four bytes of a varint32 near the reader limit
|
|
110
|
+
let value = 0
|
|
111
|
+
for (let i = 0; i < 4; ++i) {
|
|
112
|
+
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
113
|
+
const b = reader.buf[reader.pos++]
|
|
114
|
+
value = (value | ((b & 127) << (i * 7))) >>> 0
|
|
115
|
+
if (b < 128) return value
|
|
116
|
+
}
|
|
117
|
+
throw indexOutOfRange(reader)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function readLongVarint(reader) {
|
|
121
|
+
const bits = new LongBits(0, 0)
|
|
122
|
+
let i = 0
|
|
123
|
+
if (reader.len - reader.pos > 4) {
|
|
124
|
+
// fast route (lo)
|
|
125
|
+
for (; i < 4; ++i) {
|
|
126
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << (i * 7))) >>> 0
|
|
127
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
128
|
+
}
|
|
129
|
+
// 5th
|
|
130
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << 28)) >>> 0
|
|
131
|
+
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) >> 4)) >>> 0
|
|
132
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
133
|
+
i = 0
|
|
134
|
+
} else {
|
|
135
|
+
for (; i < 3; ++i) {
|
|
136
|
+
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
137
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos] & 127) << (i * 7))) >>> 0
|
|
138
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
139
|
+
}
|
|
140
|
+
// 4th — protobufjs reads this byte unchecked and returns regardless of its continuation bit
|
|
141
|
+
bits.lo = (bits.lo | ((reader.buf[reader.pos++] & 127) << (i * 7))) >>> 0
|
|
142
|
+
return bits
|
|
143
|
+
}
|
|
144
|
+
if (reader.len - reader.pos > 4) {
|
|
145
|
+
// fast route (hi)
|
|
146
|
+
for (; i < 5; ++i) {
|
|
147
|
+
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) << (i * 7 + 3))) >>> 0
|
|
148
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
149
|
+
}
|
|
150
|
+
} else {
|
|
151
|
+
for (; i < 5; ++i) {
|
|
152
|
+
if (reader.pos >= reader.len) throw indexOutOfRange(reader)
|
|
153
|
+
bits.hi = (bits.hi | ((reader.buf[reader.pos] & 127) << (i * 7 + 3))) >>> 0
|
|
154
|
+
if (reader.buf[reader.pos++] < 128) return bits
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
throw Error('invalid varint encoding')
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const readFixed32End = (buf, end) => (buf[end - 4] | (buf[end - 3] << 8) | (buf[end - 2] << 16) | (buf[end - 1] << 24)) >>> 0
|
|
161
|
+
|
|
162
|
+
export class Reader {
|
|
163
|
+
constructor(buffer) {
|
|
164
|
+
this.buf = buffer
|
|
165
|
+
this.pos = 0
|
|
166
|
+
this.len = buffer.length
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** @returns {Reader} BufferReader-equivalent for Buffers; accepts Uint8Array too (like protobufjs) */
|
|
170
|
+
static create(buffer) {
|
|
171
|
+
if (Buffer.isBuffer(buffer) || buffer instanceof Uint8Array || Array.isArray(buffer)) return new Reader(buffer)
|
|
172
|
+
throw Error('illegal buffer')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
uint32() {
|
|
176
|
+
if (this.len - this.pos < 5) {
|
|
177
|
+
if (this.pos >= this.len) throw indexOutOfRange(this)
|
|
178
|
+
if (this.buf[this.pos] >= 128) return readVarint32NearEnd(this)
|
|
179
|
+
}
|
|
180
|
+
let value = (this.buf[this.pos] & 127) >>> 0
|
|
181
|
+
if (this.buf[this.pos++] < 128) return value
|
|
182
|
+
value = (value | ((this.buf[this.pos] & 127) << 7)) >>> 0
|
|
183
|
+
if (this.buf[this.pos++] < 128) return value
|
|
184
|
+
value = (value | ((this.buf[this.pos] & 127) << 14)) >>> 0
|
|
185
|
+
if (this.buf[this.pos++] < 128) return value
|
|
186
|
+
value = (value | ((this.buf[this.pos] & 127) << 21)) >>> 0
|
|
187
|
+
if (this.buf[this.pos++] < 128) return value
|
|
188
|
+
value = (value | ((this.buf[this.pos] & 15) << 28)) >>> 0
|
|
189
|
+
if (this.buf[this.pos++] < 128) return value
|
|
190
|
+
if ((this.pos += 5) > this.len) {
|
|
191
|
+
this.pos = this.len
|
|
192
|
+
throw indexOutOfRange(this, 10)
|
|
193
|
+
}
|
|
194
|
+
return value
|
|
195
|
+
}
|
|
196
|
+
int32() {
|
|
197
|
+
return this.uint32() | 0
|
|
198
|
+
}
|
|
199
|
+
sint32() {
|
|
200
|
+
const value = this.uint32()
|
|
201
|
+
return ((value >>> 1) ^ -(value & 1)) | 0
|
|
202
|
+
}
|
|
203
|
+
int64() {
|
|
204
|
+
return readLongVarint(this).toLong(false)
|
|
205
|
+
}
|
|
206
|
+
uint64() {
|
|
207
|
+
return readLongVarint(this).toLong(true)
|
|
208
|
+
}
|
|
209
|
+
sint64() {
|
|
210
|
+
return readLongVarint(this).zzDecode().toLong(false)
|
|
211
|
+
}
|
|
212
|
+
bool() {
|
|
213
|
+
return this.uint32() !== 0
|
|
214
|
+
}
|
|
215
|
+
fixed32() {
|
|
216
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4)
|
|
217
|
+
return readFixed32End(this.buf, (this.pos += 4))
|
|
218
|
+
}
|
|
219
|
+
sfixed32() {
|
|
220
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4)
|
|
221
|
+
return readFixed32End(this.buf, (this.pos += 4)) | 0
|
|
222
|
+
}
|
|
223
|
+
_fixed64() {
|
|
224
|
+
if (this.pos + 8 > this.len) throw indexOutOfRange(this, 8)
|
|
225
|
+
return new LongBits(readFixed32End(this.buf, (this.pos += 4)), readFixed32End(this.buf, (this.pos += 4)))
|
|
226
|
+
}
|
|
227
|
+
fixed64() {
|
|
228
|
+
return this._fixed64().toLong(true)
|
|
229
|
+
}
|
|
230
|
+
sfixed64() {
|
|
231
|
+
return this._fixed64().toLong(false)
|
|
232
|
+
}
|
|
233
|
+
float() {
|
|
234
|
+
if (this.pos + 4 > this.len) throw indexOutOfRange(this, 4)
|
|
235
|
+
const value = Buffer.prototype.readFloatLE.call(this.buf, this.pos)
|
|
236
|
+
this.pos += 4
|
|
237
|
+
return value
|
|
238
|
+
}
|
|
239
|
+
double() {
|
|
240
|
+
if (this.pos + 8 > this.len) throw indexOutOfRange(this, 4)
|
|
241
|
+
const value = Buffer.prototype.readDoubleLE.call(this.buf, this.pos)
|
|
242
|
+
this.pos += 8
|
|
243
|
+
return value
|
|
244
|
+
}
|
|
245
|
+
bytes() {
|
|
246
|
+
const length = this.uint32()
|
|
247
|
+
const start = this.pos
|
|
248
|
+
const end = this.pos + length
|
|
249
|
+
if (end > this.len) throw indexOutOfRange(this, length)
|
|
250
|
+
this.pos += length
|
|
251
|
+
if (Array.isArray(this.buf)) return this.buf.slice(start, end)
|
|
252
|
+
if (start === end) return Buffer.alloc(0)
|
|
253
|
+
// Buffer#slice = zero-copy VIEW (same as protobufjs BufferReader); Uint8Array input -> subarray
|
|
254
|
+
return Buffer.isBuffer(this.buf) ? this.buf.slice(start, end) : this.buf.subarray(start, end)
|
|
255
|
+
}
|
|
256
|
+
string() {
|
|
257
|
+
if (Buffer.isBuffer(this.buf)) {
|
|
258
|
+
const len = this.uint32() // modifies pos
|
|
259
|
+
// BufferReader: clamps to buffer end instead of throwing
|
|
260
|
+
return this.buf.utf8Slice(this.pos, (this.pos = Math.min(this.pos + len, this.len)))
|
|
261
|
+
}
|
|
262
|
+
const b = this.bytes()
|
|
263
|
+
return Buffer.from(b.buffer, b.byteOffset, b.byteLength).toString('utf8')
|
|
264
|
+
}
|
|
265
|
+
skip(length) {
|
|
266
|
+
if (typeof length === 'number') {
|
|
267
|
+
if (this.pos + length > this.len) throw indexOutOfRange(this, length)
|
|
268
|
+
this.pos += length
|
|
269
|
+
} else {
|
|
270
|
+
do {
|
|
271
|
+
if (this.pos >= this.len) throw indexOutOfRange(this)
|
|
272
|
+
} while (this.buf[this.pos++] & 128)
|
|
273
|
+
}
|
|
274
|
+
return this
|
|
275
|
+
}
|
|
276
|
+
skipType(wireType, depth) {
|
|
277
|
+
if (depth === undefined) depth = 0
|
|
278
|
+
if (depth > Reader.recursionLimit) throw Error('maximum nesting depth exceeded')
|
|
279
|
+
switch (wireType) {
|
|
280
|
+
case 0:
|
|
281
|
+
this.skip()
|
|
282
|
+
break
|
|
283
|
+
case 1:
|
|
284
|
+
this.skip(8)
|
|
285
|
+
break
|
|
286
|
+
case 2:
|
|
287
|
+
this.skip(this.uint32())
|
|
288
|
+
break
|
|
289
|
+
case 3:
|
|
290
|
+
while ((wireType = this.uint32() & 7) !== 4) this.skipType(wireType, depth + 1)
|
|
291
|
+
break
|
|
292
|
+
case 5:
|
|
293
|
+
this.skip(4)
|
|
294
|
+
break
|
|
295
|
+
default:
|
|
296
|
+
throw Error('invalid wire type ' + wireType + ' at offset ' + this.pos)
|
|
297
|
+
}
|
|
298
|
+
return this
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
Reader.recursionLimit = 100 // protoc: CodedInputStream::default_recursion_limit_
|
|
302
|
+
|
|
303
|
+
/* ------------------------------------------------------------------ */
|
|
304
|
+
/* Writer (linked list of ops, like protobufjs; needed for fork/ldelim) */
|
|
305
|
+
/* ------------------------------------------------------------------ */
|
|
306
|
+
|
|
307
|
+
class Op {
|
|
308
|
+
constructor(fn, len, val) {
|
|
309
|
+
this.fn = fn
|
|
310
|
+
this.len = len
|
|
311
|
+
this.next = undefined
|
|
312
|
+
this.val = val
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const noop = () => {}
|
|
316
|
+
|
|
317
|
+
class State {
|
|
318
|
+
constructor(writer) {
|
|
319
|
+
this.head = writer.head
|
|
320
|
+
this.tail = writer.tail
|
|
321
|
+
this.len = writer.len
|
|
322
|
+
this.next = writer.states
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const writeByte = (val, buf, pos) => {
|
|
327
|
+
buf[pos] = val & 255
|
|
328
|
+
}
|
|
329
|
+
const writeVarint32 = (val, buf, pos) => {
|
|
330
|
+
while (val > 127) {
|
|
331
|
+
buf[pos++] = (val & 127) | 128
|
|
332
|
+
val >>>= 7
|
|
333
|
+
}
|
|
334
|
+
buf[pos] = val
|
|
335
|
+
}
|
|
336
|
+
const writeVarint64 = (val, buf, pos) => {
|
|
337
|
+
let lo = val.lo
|
|
338
|
+
let hi = val.hi
|
|
339
|
+
while (hi) {
|
|
340
|
+
buf[pos++] = (lo & 127) | 128
|
|
341
|
+
lo = ((lo >>> 7) | (hi << 25)) >>> 0
|
|
342
|
+
hi >>>= 7
|
|
343
|
+
}
|
|
344
|
+
while (lo > 127) {
|
|
345
|
+
buf[pos++] = (lo & 127) | 128
|
|
346
|
+
lo = lo >>> 7
|
|
347
|
+
}
|
|
348
|
+
buf[pos++] = lo
|
|
349
|
+
}
|
|
350
|
+
const writeFixed32 = (val, buf, pos) => {
|
|
351
|
+
buf[pos] = val & 255
|
|
352
|
+
buf[pos + 1] = (val >>> 8) & 255
|
|
353
|
+
buf[pos + 2] = (val >>> 16) & 255
|
|
354
|
+
buf[pos + 3] = val >>> 24
|
|
355
|
+
}
|
|
356
|
+
const writeFloat = (val, buf, pos) => {
|
|
357
|
+
Buffer.prototype.writeFloatLE.call(buf, val, pos)
|
|
358
|
+
}
|
|
359
|
+
const writeDouble = (val, buf, pos) => {
|
|
360
|
+
Buffer.prototype.writeDoubleLE.call(buf, val, pos)
|
|
361
|
+
}
|
|
362
|
+
const writeBytes = (val, buf, pos) => {
|
|
363
|
+
buf.set(val, pos) // also works for plain array values
|
|
364
|
+
}
|
|
365
|
+
const writeString = (val, buf, pos) => {
|
|
366
|
+
buf.utf8Write(val, pos)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export class Writer {
|
|
370
|
+
constructor() {
|
|
371
|
+
this.len = 0
|
|
372
|
+
this.head = new Op(noop, 0, 0)
|
|
373
|
+
this.tail = this.head
|
|
374
|
+
this.states = null
|
|
375
|
+
}
|
|
376
|
+
static create() {
|
|
377
|
+
return new Writer()
|
|
378
|
+
}
|
|
379
|
+
static alloc(size) {
|
|
380
|
+
return Buffer.allocUnsafe(size)
|
|
381
|
+
}
|
|
382
|
+
_push(fn, len, val) {
|
|
383
|
+
this.tail = this.tail.next = new Op(fn, len, val)
|
|
384
|
+
this.len += len
|
|
385
|
+
return this
|
|
386
|
+
}
|
|
387
|
+
uint32(value) {
|
|
388
|
+
value = value >>> 0
|
|
389
|
+
return this._push(writeVarint32, value < 128 ? 1 : value < 16384 ? 2 : value < 2097152 ? 3 : value < 268435456 ? 4 : 5, value)
|
|
390
|
+
}
|
|
391
|
+
int32(value) {
|
|
392
|
+
return (value |= 0) < 0
|
|
393
|
+
? this._push(writeVarint64, 10, LongBits.fromNumber(value)) // 10 bytes per spec
|
|
394
|
+
: this.uint32(value)
|
|
395
|
+
}
|
|
396
|
+
sint32(value) {
|
|
397
|
+
return this.uint32(((value << 1) ^ (value >> 31)) >>> 0)
|
|
398
|
+
}
|
|
399
|
+
uint64(value) {
|
|
400
|
+
const bits = LongBits.from(value)
|
|
401
|
+
return this._push(writeVarint64, bits.length(), bits)
|
|
402
|
+
}
|
|
403
|
+
int64(value) {
|
|
404
|
+
return this.uint64(value)
|
|
405
|
+
}
|
|
406
|
+
sint64(value) {
|
|
407
|
+
const bits = LongBits.from(value).zzEncode()
|
|
408
|
+
return this._push(writeVarint64, bits.length(), bits)
|
|
409
|
+
}
|
|
410
|
+
bool(value) {
|
|
411
|
+
return this._push(writeByte, 1, value ? 1 : 0)
|
|
412
|
+
}
|
|
413
|
+
fixed32(value) {
|
|
414
|
+
return this._push(writeFixed32, 4, value >>> 0)
|
|
415
|
+
}
|
|
416
|
+
sfixed32(value) {
|
|
417
|
+
return this.fixed32(value)
|
|
418
|
+
}
|
|
419
|
+
fixed64(value) {
|
|
420
|
+
const bits = LongBits.from(value)
|
|
421
|
+
return this._push(writeFixed32, 4, bits.lo)._push(writeFixed32, 4, bits.hi)
|
|
422
|
+
}
|
|
423
|
+
sfixed64(value) {
|
|
424
|
+
return this.fixed64(value)
|
|
425
|
+
}
|
|
426
|
+
float(value) {
|
|
427
|
+
return this._push(writeFloat, 4, value)
|
|
428
|
+
}
|
|
429
|
+
double(value) {
|
|
430
|
+
return this._push(writeDouble, 8, value)
|
|
431
|
+
}
|
|
432
|
+
bytes(value) {
|
|
433
|
+
// BufferWriter semantics: base64 strings are decoded
|
|
434
|
+
if (typeof value === 'string' || value instanceof String) value = Buffer.from(value, 'base64')
|
|
435
|
+
const len = value.length >>> 0
|
|
436
|
+
this.uint32(len)
|
|
437
|
+
if (len) this._push(writeBytes, len, value)
|
|
438
|
+
return this
|
|
439
|
+
}
|
|
440
|
+
string(value) {
|
|
441
|
+
const len = Buffer.byteLength(value)
|
|
442
|
+
this.uint32(len)
|
|
443
|
+
if (len) this._push(writeString, len, value)
|
|
444
|
+
return this
|
|
445
|
+
}
|
|
446
|
+
fork() {
|
|
447
|
+
this.states = new State(this)
|
|
448
|
+
this.head = this.tail = new Op(noop, 0, 0)
|
|
449
|
+
this.len = 0
|
|
450
|
+
return this
|
|
451
|
+
}
|
|
452
|
+
reset() {
|
|
453
|
+
if (this.states) {
|
|
454
|
+
this.head = this.states.head
|
|
455
|
+
this.tail = this.states.tail
|
|
456
|
+
this.len = this.states.len
|
|
457
|
+
this.states = this.states.next
|
|
458
|
+
} else {
|
|
459
|
+
this.head = this.tail = new Op(noop, 0, 0)
|
|
460
|
+
this.len = 0
|
|
461
|
+
}
|
|
462
|
+
return this
|
|
463
|
+
}
|
|
464
|
+
ldelim() {
|
|
465
|
+
const head = this.head
|
|
466
|
+
const tail = this.tail
|
|
467
|
+
const len = this.len
|
|
468
|
+
this.reset().uint32(len)
|
|
469
|
+
if (len) {
|
|
470
|
+
this.tail.next = head.next // skip noop
|
|
471
|
+
this.tail = tail
|
|
472
|
+
this.len += len
|
|
473
|
+
}
|
|
474
|
+
return this
|
|
475
|
+
}
|
|
476
|
+
finish() {
|
|
477
|
+
let head = this.head.next // skip noop
|
|
478
|
+
const buf = Writer.alloc(this.len)
|
|
479
|
+
let pos = 0
|
|
480
|
+
while (head) {
|
|
481
|
+
head.fn(head.val, buf, pos)
|
|
482
|
+
pos += head.len
|
|
483
|
+
head = head.next
|
|
484
|
+
}
|
|
485
|
+
return buf
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/* ------------------------------------------------------------------ */
|
|
490
|
+
/* util */
|
|
491
|
+
/* ------------------------------------------------------------------ */
|
|
492
|
+
|
|
493
|
+
// base64 tables (same construction as @protobufjs/base64): B64 = index -> char code, S64 = char code -> index (sparse)
|
|
494
|
+
const B64 = new Array(64)
|
|
495
|
+
const S64 = new Array(123)
|
|
496
|
+
for (let i = 0; i < 64; ) S64[(B64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : (i - 59) | 43)] = i++
|
|
497
|
+
|
|
498
|
+
const isUnsafeProperty = key => key === '__proto__' || key === 'prototype' || key === 'constructor'
|
|
499
|
+
|
|
500
|
+
export const util = {
|
|
501
|
+
Long,
|
|
502
|
+
LongBits,
|
|
503
|
+
isUnsafeProperty,
|
|
504
|
+
emptyArray: Object.freeze([]),
|
|
505
|
+
emptyObject: Object.freeze({}),
|
|
506
|
+
recursionLimit: 100,
|
|
507
|
+
nestingLimit: 32,
|
|
508
|
+
toJSONOptions: { longs: String, enums: String, bytes: String, json: true },
|
|
509
|
+
newBuffer: sizeOrArray => (typeof sizeOrArray === 'number' ? Buffer.allocUnsafe(sizeOrArray) : Buffer.from(sizeOrArray)),
|
|
510
|
+
base64: {
|
|
511
|
+
/** byte length of the decoded content (tolerates missing padding) — port of @protobufjs/base64 */
|
|
512
|
+
length(string) {
|
|
513
|
+
let p = string.length
|
|
514
|
+
if (!p) return 0
|
|
515
|
+
let n = 0
|
|
516
|
+
while (--p % 4 > 1 && string.charAt(p) === '=') ++n
|
|
517
|
+
return Math.ceil(string.length * 3) / 4 - n
|
|
518
|
+
},
|
|
519
|
+
/** Standard base64 (with '=' padding) of buffer[start, end) — port of @protobufjs/base64 encode. */
|
|
520
|
+
encode(buffer, start, end) {
|
|
521
|
+
let parts = null
|
|
522
|
+
let chunk = []
|
|
523
|
+
let i = 0 // output index
|
|
524
|
+
let j = 0 // goto index
|
|
525
|
+
let t // temporary
|
|
526
|
+
while (start < end) {
|
|
527
|
+
const b = buffer[start++]
|
|
528
|
+
switch (j) {
|
|
529
|
+
case 0:
|
|
530
|
+
chunk[i++] = B64[b >> 2]
|
|
531
|
+
t = (b & 3) << 4
|
|
532
|
+
j = 1
|
|
533
|
+
break
|
|
534
|
+
case 1:
|
|
535
|
+
chunk[i++] = B64[t | (b >> 4)]
|
|
536
|
+
t = (b & 15) << 2
|
|
537
|
+
j = 2
|
|
538
|
+
break
|
|
539
|
+
case 2:
|
|
540
|
+
chunk[i++] = B64[t | (b >> 6)]
|
|
541
|
+
chunk[i++] = B64[b & 63]
|
|
542
|
+
j = 0
|
|
543
|
+
break
|
|
544
|
+
}
|
|
545
|
+
if (i > 8191) {
|
|
546
|
+
;(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk))
|
|
547
|
+
i = 0
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
if (j) {
|
|
551
|
+
chunk[i++] = B64[t]
|
|
552
|
+
chunk[i++] = 61
|
|
553
|
+
if (j === 1) chunk[i++] = 61
|
|
554
|
+
}
|
|
555
|
+
if (parts) {
|
|
556
|
+
if (i) parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)))
|
|
557
|
+
return parts.join('')
|
|
558
|
+
}
|
|
559
|
+
return String.fromCharCode.apply(String, chunk.slice(0, i))
|
|
560
|
+
},
|
|
561
|
+
/**
|
|
562
|
+
* Strict standard-alphabet decode. Throws 'invalid encoding' on any character outside
|
|
563
|
+
* A-Za-z0-9+/ (so URL-safe '-'/'_', whitespace etc. are REJECTED — unlike Buffer.from(...,'base64')).
|
|
564
|
+
*/
|
|
565
|
+
decode(string, buffer, offset) {
|
|
566
|
+
const start = offset
|
|
567
|
+
let j = 0
|
|
568
|
+
let t
|
|
569
|
+
for (let i = 0; i < string.length; ) {
|
|
570
|
+
let c = string.charCodeAt(i++)
|
|
571
|
+
if (c === 61 && j > 1) break
|
|
572
|
+
c = S64[c]
|
|
573
|
+
if (c === undefined) throw Error('invalid encoding')
|
|
574
|
+
switch (j) {
|
|
575
|
+
case 0:
|
|
576
|
+
t = c
|
|
577
|
+
j = 1
|
|
578
|
+
break
|
|
579
|
+
case 1:
|
|
580
|
+
buffer[offset++] = (t << 2) | ((c & 48) >> 4)
|
|
581
|
+
t = c
|
|
582
|
+
j = 2
|
|
583
|
+
break
|
|
584
|
+
case 2:
|
|
585
|
+
buffer[offset++] = ((t & 15) << 4) | ((c & 60) >> 2)
|
|
586
|
+
t = c
|
|
587
|
+
j = 3
|
|
588
|
+
break
|
|
589
|
+
case 3:
|
|
590
|
+
buffer[offset++] = ((t & 3) << 6) | c
|
|
591
|
+
j = 0
|
|
592
|
+
break
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (j === 1) throw Error('invalid encoding')
|
|
596
|
+
return offset - start
|
|
597
|
+
}
|
|
598
|
+
},
|
|
599
|
+
makeProp(obj, key) {
|
|
600
|
+
Object.defineProperty(obj, key, { enumerable: true, configurable: true, writable: true })
|
|
601
|
+
},
|
|
602
|
+
oneOfGetter(fieldNames) {
|
|
603
|
+
const fieldMap = {}
|
|
604
|
+
for (let i = 0; i < fieldNames.length; ++i) fieldMap[fieldNames[i]] = 1
|
|
605
|
+
return function () {
|
|
606
|
+
for (let keys = Object.keys(this), i = keys.length - 1; i > -1; --i)
|
|
607
|
+
if (fieldMap[keys[i]] === 1 && this[keys[i]] !== undefined && this[keys[i]] !== null) return keys[i]
|
|
608
|
+
}
|
|
609
|
+
},
|
|
610
|
+
oneOfSetter(fieldNames) {
|
|
611
|
+
return function (name) {
|
|
612
|
+
for (let i = 0; i < fieldNames.length; ++i) if (fieldNames[i] !== name) delete this[fieldNames[i]]
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
export const roots = {}
|
|
618
|
+
|
|
619
|
+
export default { Reader, Writer, util, roots, LongBits }
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native replacements for the four `whatsapp-rust-bridge` (WASM) functions
|
|
3
|
+
* that baileys-lite used: md5, hkdf, expandAppStateKeys, LTHashAntiTampering.
|
|
4
|
+
*
|
|
5
|
+
* All built on `node:crypto`. Behavior (return types, error conditions, byte
|
|
6
|
+
* output) verified against the WASM module in test/native-deps.mjs.
|
|
7
|
+
*/
|
|
8
|
+
import { createHash, hkdfSync } from 'node:crypto'
|
|
9
|
+
|
|
10
|
+
const u8 = b => new Uint8Array(b.buffer, b.byteOffset, b.byteLength)
|
|
11
|
+
|
|
12
|
+
/** MD5 digest, returned as Uint8Array (same as the bridge). */
|
|
13
|
+
export const md5 = buffer => u8(createHash('md5').update(buffer).digest())
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* HKDF-SHA256 (RFC 5869).
|
|
17
|
+
* @param {Uint8Array} buffer input keying material
|
|
18
|
+
* @param {number} expandedLength output length in bytes (max 255 * 32)
|
|
19
|
+
* @param {{ salt?: Uint8Array, info?: string }} [options] `info` is UTF-8 encoded; missing salt = zero salt
|
|
20
|
+
* @returns {Uint8Array}
|
|
21
|
+
*/
|
|
22
|
+
export const hkdf = (buffer, expandedLength, { salt, info } = {}) => {
|
|
23
|
+
if (expandedLength > 255 * 32) throw new Error('HKDF expansion failed')
|
|
24
|
+
if (expandedLength === 0) return new Uint8Array(0) // node's hkdfSync throws on 0; the bridge returns empty
|
|
25
|
+
return u8(Buffer.from(hkdfSync('sha256', buffer, salt ?? Buffer.alloc(0), Buffer.from(info ?? '', 'utf8'), expandedLength)))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Derive the five WhatsApp app-state (syncd) keys from a 32-byte key.
|
|
30
|
+
* Keys are Uint8Array views, in the order WhatsApp Web uses.
|
|
31
|
+
*/
|
|
32
|
+
export const expandAppStateKeys = keyData => {
|
|
33
|
+
const expanded = hkdf(keyData, 160, { info: 'WhatsApp Mutation Keys' })
|
|
34
|
+
const part = i => expanded.slice(i * 32, i * 32 + 32)
|
|
35
|
+
return {
|
|
36
|
+
indexKey: part(0),
|
|
37
|
+
valueEncryptionKey: part(1),
|
|
38
|
+
valueMacKey: part(2),
|
|
39
|
+
snapshotMacKey: part(3),
|
|
40
|
+
patchMacKey: part(4)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* LT Hash (lattice/summation hash): each value MAC is HKDF-expanded to 128 bytes
|
|
46
|
+
* and added to / subtracted from the running hash as 64 little-endian uint16 lanes.
|
|
47
|
+
*/
|
|
48
|
+
export class LTHashAntiTampering {
|
|
49
|
+
constructor() {
|
|
50
|
+
this.salt = 'WhatsApp Patch Integrity'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {Uint8Array} base current 128-byte hash
|
|
55
|
+
* @param {Uint8Array[]} subtract value MACs to remove
|
|
56
|
+
* @param {Uint8Array[]} add value MACs to add
|
|
57
|
+
* @returns {Uint8Array} new 128-byte hash (inputs are not mutated)
|
|
58
|
+
*/
|
|
59
|
+
subtractThenAdd(base, subtract, add) {
|
|
60
|
+
if (base.length !== 128) throw new Error(`Base hash must be 128 bytes, got ${base.length}`)
|
|
61
|
+
const lanes = new Uint16Array(64)
|
|
62
|
+
const view = Buffer.from(base.buffer, base.byteOffset, base.byteLength)
|
|
63
|
+
for (let i = 0; i < 64; i++) lanes[i] = view.readUInt16LE(i * 2)
|
|
64
|
+
const apply = (items, sign) => {
|
|
65
|
+
for (const item of items) {
|
|
66
|
+
const ex = Buffer.from(hkdf(item, 128, { info: this.salt }))
|
|
67
|
+
for (let i = 0; i < 64; i++) lanes[i] += sign * ex.readUInt16LE(i * 2) // Uint16Array wraps mod 2^16
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
apply(subtract, -1)
|
|
71
|
+
apply(add, +1)
|
|
72
|
+
const out = Buffer.alloc(128)
|
|
73
|
+
for (let i = 0; i < 64; i++) out.writeUInt16LE(lanes[i], i * 2)
|
|
74
|
+
return u8(out)
|
|
75
|
+
}
|
|
76
|
+
}
|