@poucode/baileys 1.0.1 → 2.0.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.
- package/README.md +21 -2
- package/lib/Socket/chats.js +5 -0
- package/lib/Socket/graphql.js +716 -0
- package/lib/Socket/index.js +10 -2
- package/lib/Socket/interop.js +463 -0
- package/lib/Socket/luxu.js +1 -1
- package/lib/Socket/message-builder.js +475 -0
- package/lib/Socket/messages-send.js +29 -4
- package/lib/Socket/privacy.js +318 -0
- package/lib/Store/make-in-memory-store.js +47 -1
- package/lib/Utils/index.js +1 -0
- package/lib/Utils/long-safe.js +33 -0
- package/lib/Utils/messages-media.js +82 -0
- package/lib/Utils/messages.js +137 -3
- package/lib/Utils/sticker.js +133 -0
- package/lib/Utils/use-sqlite-auth-state.js +162 -0
- package/lib/WAUSync/Protocols/USyncBusinessProtocol.js +43 -0
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +53 -50
- package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +56 -52
- package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +29 -25
- package/lib/WAUSync/Protocols/USyncFeatureProtocol.js +58 -0
- package/lib/WAUSync/Protocols/USyncPictureProtocol.js +32 -0
- package/lib/WAUSync/Protocols/USyncSidelistProtocol.js +28 -0
- package/lib/WAUSync/Protocols/USyncStatusProtocol.js +38 -36
- package/lib/WAUSync/Protocols/USyncTextStatusProtocol.js +38 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +26 -23
- package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +52 -49
- package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +29 -27
- package/lib/WAUSync/Protocols/index.js +12 -5
- package/lib/WAUSync/USyncQuery.js +150 -96
- package/lib/WAUSync/USyncUser.js +55 -29
- package/lib/WAUSync/index.js +3 -3
- package/lib/index.js +1 -0
- package/package.json +12 -7
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Boom } from '@hapi/boom';
|
|
2
|
+
import { spawn } from 'child_process';
|
|
3
|
+
import { randomUUID } from 'crypto';
|
|
4
|
+
|
|
5
|
+
const runFfmpeg = (input, args) => new Promise((resolve, reject) => {
|
|
6
|
+
const proc = spawn('ffmpeg', ['-hide_banner', '-loglevel', 'error', '-i', 'pipe:0', ...args, 'pipe:1'], { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
7
|
+
const outChunks = [];
|
|
8
|
+
const errChunks = [];
|
|
9
|
+
proc.stdout.on('data', chunk => outChunks.push(chunk));
|
|
10
|
+
proc.stderr.on('data', chunk => errChunks.push(chunk));
|
|
11
|
+
proc.on('error', err => reject(new Boom(`ffmpeg failed to start: ${err.message}`, { statusCode: 500 })));
|
|
12
|
+
proc.on('close', code => {
|
|
13
|
+
if (code !== 0) {
|
|
14
|
+
reject(new Boom(`ffmpeg exited with code ${code}: ${Buffer.concat(errChunks).toString('utf8').slice(0, 500)}`, { statusCode: 500 }));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
resolve(Buffer.concat(outChunks));
|
|
18
|
+
});
|
|
19
|
+
proc.stdin.on('error', () => { });
|
|
20
|
+
proc.stdin.end(input);
|
|
21
|
+
});
|
|
22
|
+
export const isLikelyVideoBuffer = (buffer) => {
|
|
23
|
+
if (!Buffer.isBuffer(buffer) || buffer.length < 4) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const header = buffer.subarray(0, 16).toString('ascii');
|
|
27
|
+
if (header.startsWith('RIFF') && /AVI|WEBM/.test(header)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
if (buffer.subarray(0, 4).toString('hex') === '1a45dfa3') {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
const ftyp = buffer.toString('ascii', 4, 12);
|
|
34
|
+
if (buffer.toString('ascii', 0, 4) === 'ftyp' || /^(ftyp)/.test(buffer.toString('ascii', 0, 8))) {
|
|
35
|
+
return /mp4|isom|M4V|M4A|qt\s/.test(ftyp);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
};
|
|
39
|
+
export const convertToWebp = async (buffer, { animated = false, quality = 60 } = {}) => {
|
|
40
|
+
const scalePad = "scale='min(512,iw)':'min(512,ih)':force_original_aspect_ratio=decrease,format=rgba,pad=512:512:-1:-1:color=#00000000";
|
|
41
|
+
const args = animated
|
|
42
|
+
? ['-vf', `${scalePad},fps=15`, '-loop', '0', '-preset', 'default', '-an', '-vsync', '0', '-vcodec', 'libwebp', '-q:v', String(quality), '-f', 'webp']
|
|
43
|
+
: ['-vf', `${scalePad}`, '-vframes', '1', '-vcodec', 'libwebp', '-q:v', String(quality), '-f', 'webp'];
|
|
44
|
+
return runFfmpeg(buffer, args);
|
|
45
|
+
};
|
|
46
|
+
export const convertToTrayIcon = async (buffer) => {
|
|
47
|
+
const scalePad = "scale='min(96,iw)':'min(96,ih)':force_original_aspect_ratio=decrease,format=rgba,pad=96:96:-1:-1:color=#00000000";
|
|
48
|
+
const args = ['-vf', scalePad, '-vframes', '1', '-vcodec', 'png', '-f', 'image2'];
|
|
49
|
+
return runFfmpeg(buffer, args);
|
|
50
|
+
};
|
|
51
|
+
const readChunks = (buf) => {
|
|
52
|
+
const chunks = [];
|
|
53
|
+
let offset = 12;
|
|
54
|
+
while (offset + 8 <= buf.length) {
|
|
55
|
+
const fourCC = buf.toString('ascii', offset, offset + 4);
|
|
56
|
+
const size = buf.readUInt32LE(offset + 4);
|
|
57
|
+
const dataStart = offset + 8;
|
|
58
|
+
const data = buf.subarray(dataStart, dataStart + size);
|
|
59
|
+
chunks.push({ fourCC, data });
|
|
60
|
+
offset = dataStart + size + (size % 2);
|
|
61
|
+
}
|
|
62
|
+
return chunks;
|
|
63
|
+
};
|
|
64
|
+
const buildChunk = (fourCC, data) => {
|
|
65
|
+
const header = Buffer.alloc(8);
|
|
66
|
+
header.write(fourCC, 0, 4, 'ascii');
|
|
67
|
+
header.writeUInt32LE(data.length, 4);
|
|
68
|
+
const pad = data.length % 2 === 1 ? Buffer.from([0]) : Buffer.alloc(0);
|
|
69
|
+
return Buffer.concat([header, data, pad]);
|
|
70
|
+
};
|
|
71
|
+
const buildExifChunkData = (json) => {
|
|
72
|
+
const header = Buffer.from([0x49, 0x49, 0x2a, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00]);
|
|
73
|
+
const payload = Buffer.from(JSON.stringify(json), 'utf8');
|
|
74
|
+
header.writeUInt32LE(payload.length, 14);
|
|
75
|
+
return Buffer.concat([header, payload]);
|
|
76
|
+
};
|
|
77
|
+
export const writeExifToWebp = (webpBuffer, { packId, packName = '', publisher = '', emojis = [], isAvatar = false } = {}) => {
|
|
78
|
+
if (webpBuffer.toString('ascii', 0, 4) !== 'RIFF' || webpBuffer.toString('ascii', 8, 12) !== 'WEBP') {
|
|
79
|
+
throw new Boom('writeExifToWebp: not a valid webp buffer', { statusCode: 400 });
|
|
80
|
+
}
|
|
81
|
+
const chunks = readChunks(webpBuffer);
|
|
82
|
+
const exifData = buildExifChunkData({
|
|
83
|
+
'sticker-pack-id': packId || randomUUID(),
|
|
84
|
+
'sticker-pack-name': packName,
|
|
85
|
+
'sticker-pack-publisher': publisher,
|
|
86
|
+
emojis: emojis.length ? emojis : ['🤖'],
|
|
87
|
+
'is-avatar-sticker': isAvatar ? 1 : 0
|
|
88
|
+
});
|
|
89
|
+
let vp8x = chunks.find(c => c.fourCC === 'VP8X');
|
|
90
|
+
const otherChunks = chunks.filter(c => c.fourCC !== 'VP8X' && c.fourCC !== 'EXIF');
|
|
91
|
+
if (!vp8x) {
|
|
92
|
+
const img = otherChunks.find(c => c.fourCC === 'VP8 ' || c.fourCC === 'VP8L');
|
|
93
|
+
if (!img) {
|
|
94
|
+
throw new Boom('writeExifToWebp: no VP8/VP8L image data found', { statusCode: 400 });
|
|
95
|
+
}
|
|
96
|
+
let width;
|
|
97
|
+
let height;
|
|
98
|
+
if (img.fourCC === 'VP8 ') {
|
|
99
|
+
width = (img.data.readUInt16LE(6) & 0x3fff);
|
|
100
|
+
height = (img.data.readUInt16LE(8) & 0x3fff);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
const b = img.data;
|
|
104
|
+
width = 1 + (((b[2] & 0x3f) << 8) | b[1]);
|
|
105
|
+
height = 1 + (((b[4] & 0xf) << 10) | (b[3] << 2) | ((b[2] & 0xc0) >> 6));
|
|
106
|
+
}
|
|
107
|
+
const flags = Buffer.alloc(4);
|
|
108
|
+
flags[0] = 0x08;
|
|
109
|
+
const dims = Buffer.alloc(6);
|
|
110
|
+
dims.writeUIntLE(width - 1, 0, 3);
|
|
111
|
+
dims.writeUIntLE(height - 1, 3, 3);
|
|
112
|
+
vp8x = { fourCC: 'VP8X', data: Buffer.concat([flags, dims]) };
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
const flagsByte = vp8x.data[0] | 0x08;
|
|
116
|
+
vp8x = { fourCC: 'VP8X', data: Buffer.concat([Buffer.from([flagsByte]), vp8x.data.subarray(1)]) };
|
|
117
|
+
}
|
|
118
|
+
const rebuilt = [vp8x, ...otherChunks, { fourCC: 'EXIF', data: exifData }];
|
|
119
|
+
const chunkBuffers = rebuilt.map(c => buildChunk(c.fourCC, c.data));
|
|
120
|
+
const payload = Buffer.concat(chunkBuffers);
|
|
121
|
+
const riffSize = 4 + payload.length;
|
|
122
|
+
const out = Buffer.alloc(8 + 4 + payload.length);
|
|
123
|
+
out.write('RIFF', 0, 4, 'ascii');
|
|
124
|
+
out.writeUInt32LE(riffSize, 4);
|
|
125
|
+
out.write('WEBP', 8, 4, 'ascii');
|
|
126
|
+
payload.copy(out, 12);
|
|
127
|
+
return out;
|
|
128
|
+
};
|
|
129
|
+
export const makeSticker = async (buffer, { pack = '', author = '', emojis = [], isPrivate = false, animated = false, quality = 60 } = {}) => {
|
|
130
|
+
const shouldAnimate = animated || isLikelyVideoBuffer(buffer);
|
|
131
|
+
const webp = await convertToWebp(buffer, { animated: shouldAnimate, quality });
|
|
132
|
+
return writeExifToWebp(webp, { packName: pack, publisher: author, emojis, isAvatar: isPrivate });
|
|
133
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile } from 'fs/promises';
|
|
2
|
+
import { dirname, join } from 'path';
|
|
3
|
+
import { proto } from '../../WAProto/index.js';
|
|
4
|
+
import { initAuthCreds } from './auth-utils.js';
|
|
5
|
+
export const useSqliteAuthState = async (pathOrFolder, options = {}) => {
|
|
6
|
+
const {
|
|
7
|
+
fileName = 'auth.db',
|
|
8
|
+
migrateFromFolder,
|
|
9
|
+
logger
|
|
10
|
+
} = options;
|
|
11
|
+
const dbPath = /\.(db|sqlite|sqlite3)$/i.test(pathOrFolder) ? pathOrFolder : join(pathOrFolder, fileName);
|
|
12
|
+
const bufReplacer = (_, value) => {
|
|
13
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array || value?.type === 'Buffer') {
|
|
14
|
+
return {
|
|
15
|
+
type: 'Buffer',
|
|
16
|
+
data: Buffer.from(value?.data || value).toString('base64')
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
};
|
|
21
|
+
const bufReviver = (_, value) => {
|
|
22
|
+
if (value && typeof value === 'object' && value.type === 'Buffer' && typeof value.data === 'string') {
|
|
23
|
+
return Buffer.from(value.data, 'base64');
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
};
|
|
27
|
+
const encode = value => {
|
|
28
|
+
if (Buffer.isBuffer(value) || value instanceof Uint8Array) {
|
|
29
|
+
return Buffer.concat([Buffer.from([1]), Buffer.from(value)]);
|
|
30
|
+
}
|
|
31
|
+
return Buffer.concat([Buffer.from([0]), Buffer.from(JSON.stringify(value, bufReplacer), 'utf8')]);
|
|
32
|
+
};
|
|
33
|
+
const decode = blob => {
|
|
34
|
+
if (!blob || blob.length === 0) return null;
|
|
35
|
+
if (blob[0] === 1) return Buffer.from(blob.subarray(1));
|
|
36
|
+
return JSON.parse(Buffer.from(blob.subarray(1)).toString('utf8'), bufReviver);
|
|
37
|
+
};
|
|
38
|
+
const fixName = s => s?.replace(/\//g, '__')?.replace(/:/g, '-');
|
|
39
|
+
const keyOf = (category, id) => fixName(`${category}-${id}`);
|
|
40
|
+
let DatabaseSync;
|
|
41
|
+
try {
|
|
42
|
+
({
|
|
43
|
+
DatabaseSync
|
|
44
|
+
} = await import('node:sqlite'));
|
|
45
|
+
} catch (err) {
|
|
46
|
+
throw new Error("useSqliteAuthState needs the built-in 'node:sqlite' module (Node 22.5+). Upgrade Node, or use useMultiFileAuthState instead.");
|
|
47
|
+
}
|
|
48
|
+
await mkdir(dirname(dbPath), {
|
|
49
|
+
recursive: true
|
|
50
|
+
}).catch(() => {});
|
|
51
|
+
const db = new DatabaseSync(dbPath);
|
|
52
|
+
db.exec('PRAGMA journal_mode = TRUNCATE');
|
|
53
|
+
db.exec('PRAGMA synchronous = NORMAL');
|
|
54
|
+
db.exec('PRAGMA busy_timeout = 5000');
|
|
55
|
+
db.exec('CREATE TABLE IF NOT EXISTS auth_state (k TEXT PRIMARY KEY, v BLOB NOT NULL) WITHOUT ROWID');
|
|
56
|
+
const qGet = db.prepare('SELECT v FROM auth_state WHERE k = ?');
|
|
57
|
+
const qUpsert = db.prepare('INSERT INTO auth_state(k,v) VALUES(?,?) ON CONFLICT(k) DO UPDATE SET v=excluded.v');
|
|
58
|
+
const qDelete = db.prepare('DELETE FROM auth_state WHERE k = ?');
|
|
59
|
+
const runTx = fn => {
|
|
60
|
+
db.exec('BEGIN');
|
|
61
|
+
try {
|
|
62
|
+
const r = fn();
|
|
63
|
+
db.exec('COMMIT');
|
|
64
|
+
return r;
|
|
65
|
+
} catch (err) {
|
|
66
|
+
db.exec('ROLLBACK');
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const readRaw = k => {
|
|
71
|
+
const row = qGet.get(k);
|
|
72
|
+
if (!row) return null;
|
|
73
|
+
try {
|
|
74
|
+
return decode(row.v);
|
|
75
|
+
} catch (err) {
|
|
76
|
+
logger?.warn?.({
|
|
77
|
+
k,
|
|
78
|
+
err: err?.message
|
|
79
|
+
}, 'sqlite-auth: failed to decode row, treating as missing');
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const runMigration = async folder => {
|
|
84
|
+
let files;
|
|
85
|
+
try {
|
|
86
|
+
files = await readdir(folder);
|
|
87
|
+
} catch {
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
90
|
+
const rows = [];
|
|
91
|
+
for (const file of files) {
|
|
92
|
+
if (!file.endsWith('.json')) continue;
|
|
93
|
+
let value;
|
|
94
|
+
try {
|
|
95
|
+
value = JSON.parse(await readFile(join(folder, file), 'utf8'), bufReviver);
|
|
96
|
+
} catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (value === null || value === undefined) continue;
|
|
100
|
+
rows.push([file.slice(0, -'.json'.length), encode(value)]);
|
|
101
|
+
}
|
|
102
|
+
runTx(() => {
|
|
103
|
+
for (const [k, v] of rows) qUpsert.run(k, v);
|
|
104
|
+
});
|
|
105
|
+
return rows.length;
|
|
106
|
+
};
|
|
107
|
+
const hasCreds = () => !!qGet.get('creds');
|
|
108
|
+
if (migrateFromFolder && !hasCreds()) {
|
|
109
|
+
const n = await runMigration(migrateFromFolder);
|
|
110
|
+
if (n > 0) logger?.info?.({
|
|
111
|
+
count: n,
|
|
112
|
+
from: migrateFromFolder
|
|
113
|
+
}, 'sqlite-auth: migrated legacy auth state');
|
|
114
|
+
}
|
|
115
|
+
let creds = readRaw('creds');
|
|
116
|
+
if (!creds) {
|
|
117
|
+
creds = initAuthCreds();
|
|
118
|
+
qUpsert.run('creds', encode(creds));
|
|
119
|
+
}
|
|
120
|
+
const keys = {
|
|
121
|
+
get: async (type, ids) => {
|
|
122
|
+
const data = {};
|
|
123
|
+
for (const id of ids) {
|
|
124
|
+
let value = readRaw(keyOf(type, id));
|
|
125
|
+
if (type === 'app-state-sync-key' && value) {
|
|
126
|
+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
|
|
127
|
+
}
|
|
128
|
+
if (value !== null && value !== undefined) {
|
|
129
|
+
data[id] = value;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return data;
|
|
133
|
+
},
|
|
134
|
+
set: async data => {
|
|
135
|
+
const ops = [];
|
|
136
|
+
for (const category in data) {
|
|
137
|
+
for (const id in data[category]) {
|
|
138
|
+
ops.push([keyOf(category, id), data[category][id]]);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
runTx(() => {
|
|
142
|
+
for (const [k, value] of ops) {
|
|
143
|
+
if (value === null || value === undefined) qDelete.run(k);else qUpsert.run(k, encode(value));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
},
|
|
147
|
+
clear: async () => {
|
|
148
|
+
db.prepare("DELETE FROM auth_state WHERE k <> 'creds'").run();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
state: {
|
|
153
|
+
creds,
|
|
154
|
+
keys
|
|
155
|
+
},
|
|
156
|
+
saveCreds: async () => {
|
|
157
|
+
qUpsert.run('creds', encode(creds));
|
|
158
|
+
},
|
|
159
|
+
db,
|
|
160
|
+
close: () => db.close()
|
|
161
|
+
};
|
|
162
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { assertNodeErrorFree, getBinaryNodeChild } from '../../WABinary/index.js'
|
|
2
|
+
|
|
3
|
+
export class USyncBusinessProtocol {
|
|
4
|
+
constructor(profileVersion = '2') {
|
|
5
|
+
this.name = 'business'
|
|
6
|
+
this.profileVersion = profileVersion
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
getQueryElement() {
|
|
10
|
+
return {
|
|
11
|
+
tag: 'business',
|
|
12
|
+
attrs: {},
|
|
13
|
+
content: [
|
|
14
|
+
{ tag: 'verified_name', attrs: {} },
|
|
15
|
+
{ tag: 'profile', attrs: { v: this.profileVersion } }
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
getUserElement(user) {
|
|
21
|
+
const children = []
|
|
22
|
+
if (user.verifiedNameSerial) {
|
|
23
|
+
children.push({ tag: 'verified_name', attrs: { serial: user.verifiedNameSerial } })
|
|
24
|
+
}
|
|
25
|
+
if (user.businessProfileTag) {
|
|
26
|
+
children.push({ tag: 'profile', attrs: { tag: user.businessProfileTag } })
|
|
27
|
+
}
|
|
28
|
+
return children.length > 0 ? { tag: 'business', attrs: {}, content: children } : null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
parser(node) {
|
|
32
|
+
if (node.tag !== 'business') return null
|
|
33
|
+
assertNodeErrorFree(node)
|
|
34
|
+
const verifiedNameNode = getBinaryNodeChild(node, 'verified_name')
|
|
35
|
+
const profileNode = getBinaryNodeChild(node, 'profile')
|
|
36
|
+
return {
|
|
37
|
+
verifiedName: verifiedNameNode?.content ?? null,
|
|
38
|
+
verifiedLevel: verifiedNameNode?.attrs?.verified_level ?? null,
|
|
39
|
+
profileTag: profileNode?.attrs?.tag ?? null,
|
|
40
|
+
pnJid: node.attrs?.pn_jid ?? null
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,51 +1,54 @@
|
|
|
1
|
-
import { assertNodeErrorFree } from '../../WABinary/index.js'
|
|
2
|
-
|
|
1
|
+
import { assertNodeErrorFree } from '../../WABinary/index.js'
|
|
2
|
+
|
|
3
3
|
export class USyncContactProtocol {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
4
|
+
constructor() {
|
|
5
|
+
this.name = 'contact'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getQueryElement() {
|
|
9
|
+
return {
|
|
10
|
+
tag: 'contact',
|
|
11
|
+
attrs: {}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getUserElement(user) {
|
|
16
|
+
if (user.phone) {
|
|
17
|
+
return {
|
|
18
|
+
tag: 'contact',
|
|
19
|
+
attrs: {},
|
|
20
|
+
content: user.phone
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (user.username) {
|
|
24
|
+
return {
|
|
25
|
+
tag: 'contact',
|
|
26
|
+
attrs: {
|
|
27
|
+
username: user.username,
|
|
28
|
+
...(user.usernameKey ? { pin: user.usernameKey } : {}),
|
|
29
|
+
...(user.lid ? { lid: user.lid } : {})
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (user.type) {
|
|
34
|
+
return {
|
|
35
|
+
tag: 'contact',
|
|
36
|
+
attrs: {
|
|
37
|
+
type: user.type
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
tag: 'contact',
|
|
43
|
+
attrs: {}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
parser(node) {
|
|
48
|
+
if (node.tag === 'contact') {
|
|
49
|
+
assertNodeErrorFree(node)
|
|
50
|
+
return node?.attrs?.type === 'in'
|
|
51
|
+
}
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -1,53 +1,57 @@
|
|
|
1
|
-
import { assertNodeErrorFree, getBinaryNodeChild } from '../../WABinary/index.js'
|
|
1
|
+
import { assertNodeErrorFree, getBinaryNodeChild } from '../../WABinary/index.js'
|
|
2
|
+
|
|
2
3
|
export class USyncDeviceProtocol {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
4
|
+
constructor() {
|
|
5
|
+
this.name = 'devices'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getQueryElement() {
|
|
9
|
+
return {
|
|
10
|
+
tag: 'devices',
|
|
11
|
+
attrs: {
|
|
12
|
+
version: '2'
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
getUserElement(/* user */) {
|
|
18
|
+
//TODO: Implement device phashing, ts and expectedTs
|
|
19
|
+
//TODO: if all are not present, return null <- current behavior
|
|
20
|
+
//TODO: otherwise return a node w tag 'devices' w those as attrs
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
parser(node) {
|
|
25
|
+
const deviceList = []
|
|
26
|
+
let keyIndex = undefined
|
|
27
|
+
if (node.tag === 'devices') {
|
|
28
|
+
assertNodeErrorFree(node)
|
|
29
|
+
const deviceListNode = getBinaryNodeChild(node, 'device-list')
|
|
30
|
+
const keyIndexNode = getBinaryNodeChild(node, 'key-index-list')
|
|
31
|
+
if (Array.isArray(deviceListNode?.content)) {
|
|
32
|
+
for (const { tag, attrs } of deviceListNode.content) {
|
|
33
|
+
const id = +attrs.id
|
|
34
|
+
const keyIndex = +attrs['key-index']
|
|
35
|
+
if (tag === 'device') {
|
|
36
|
+
deviceList.push({
|
|
37
|
+
id,
|
|
38
|
+
keyIndex,
|
|
39
|
+
isHosted: !!(attrs['is_hosted'] && attrs['is_hosted'] === 'true')
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (keyIndexNode?.tag === 'key-index-list') {
|
|
45
|
+
keyIndex = {
|
|
46
|
+
timestamp: +keyIndexNode.attrs['ts'],
|
|
47
|
+
signedKeyIndex: keyIndexNode?.content,
|
|
48
|
+
expectedTimestamp: keyIndexNode.attrs['expected_ts'] ? +keyIndexNode.attrs['expected_ts'] : undefined
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
deviceList,
|
|
54
|
+
keyIndex
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import { assertNodeErrorFree } from '../../WABinary/index.js'
|
|
1
|
+
import { assertNodeErrorFree } from '../../WABinary/index.js'
|
|
2
|
+
|
|
2
3
|
export class USyncDisappearingModeProtocol {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
4
|
+
constructor() {
|
|
5
|
+
this.name = 'disappearing_mode'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getQueryElement() {
|
|
9
|
+
return {
|
|
10
|
+
tag: 'disappearing_mode',
|
|
11
|
+
attrs: {}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getUserElement() {
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
parser(node) {
|
|
20
|
+
if (node.tag === 'disappearing_mode') {
|
|
21
|
+
assertNodeErrorFree(node)
|
|
22
|
+
const duration = +node?.attrs.duration
|
|
23
|
+
const setAt = new Date(+(node?.attrs.t || 0) * 1000)
|
|
24
|
+
return {
|
|
25
|
+
duration,
|
|
26
|
+
setAt
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { getBinaryNodeChild } from '../../WABinary/index.js'
|
|
2
|
+
|
|
3
|
+
// Device feature flags that can be queried, ported from WhatsApp Web's
|
|
4
|
+
// WAWebUsyncFeature module.
|
|
5
|
+
export const USYNC_FEATURES = [
|
|
6
|
+
'document',
|
|
7
|
+
'encrypt',
|
|
8
|
+
'encrypt_blist',
|
|
9
|
+
'encrypt_contact',
|
|
10
|
+
'encrypt_group_gen2',
|
|
11
|
+
'encrypt_image',
|
|
12
|
+
'encrypt_location',
|
|
13
|
+
'encrypt_url',
|
|
14
|
+
'encrypt_v2',
|
|
15
|
+
'voip',
|
|
16
|
+
'multi_agent'
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
export class USyncFeatureProtocol {
|
|
20
|
+
/**
|
|
21
|
+
* @param {string[]} [features] feature names to query (defaults to all known)
|
|
22
|
+
*/
|
|
23
|
+
constructor(features) {
|
|
24
|
+
this.name = 'feature'
|
|
25
|
+
this.features = features && features.length ? features : USYNC_FEATURES
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getQueryElement() {
|
|
29
|
+
return {
|
|
30
|
+
tag: 'feature',
|
|
31
|
+
attrs: {},
|
|
32
|
+
content: this.features.map(feature => ({ tag: feature, attrs: {} }))
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
getUserElement() {
|
|
37
|
+
return null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
parser(node) {
|
|
41
|
+
if (node.tag !== 'feature') return null
|
|
42
|
+
const errorNode = getBinaryNodeChild(node, 'error')
|
|
43
|
+
if (errorNode) {
|
|
44
|
+
return {
|
|
45
|
+
errorCode: errorNode.attrs?.code ? +errorNode.attrs.code : undefined,
|
|
46
|
+
errorText: errorNode.attrs?.text
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const features = {}
|
|
50
|
+
const children = Array.isArray(node.content) ? node.content : []
|
|
51
|
+
for (const child of children) {
|
|
52
|
+
if (child?.attrs && child.attrs.value !== undefined) {
|
|
53
|
+
features[child.tag] = child.attrs.value
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return features
|
|
57
|
+
}
|
|
58
|
+
}
|