sauruspro-bail 5.0.0 → 5.0.2-alpha.spr.test

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 (41) hide show
  1. package/README.md +12 -9
  2. package/docs/CHANGELOG.md +35 -4
  3. package/docs/SPR.md +61 -24
  4. package/lib/Defaults/index.js +5 -1
  5. package/lib/Socket/dinonic-handler.d.ts +27 -0
  6. package/lib/Socket/dinonic-handler.js +882 -0
  7. package/lib/Socket/index.js +8 -1
  8. package/lib/Socket/messages-recv.js +26 -0
  9. package/lib/Socket/messages-send.js +25 -0
  10. package/lib/Utils/MessageBuilder.d.ts +215 -0
  11. package/lib/Utils/MessageBuilder.js +3991 -0
  12. package/lib/Utils/a2ui.d.ts +202 -0
  13. package/lib/Utils/a2ui.js +732 -0
  14. package/lib/Utils/airich-registry.js +3 -0
  15. package/lib/Utils/baileys-event-stream.js +58 -0
  16. package/lib/Utils/bot-signature.d.ts +22 -0
  17. package/lib/Utils/bot-signature.js +124 -0
  18. package/lib/Utils/dinonic-spr.d.ts +73 -0
  19. package/lib/Utils/dinonic-spr.js +739 -0
  20. package/lib/Utils/extras.d.ts +449 -0
  21. package/lib/Utils/extras.js +1377 -0
  22. package/lib/Utils/html-app.d.ts +39 -0
  23. package/lib/Utils/html-app.js +291 -0
  24. package/lib/Utils/index.js +14 -2
  25. package/lib/Utils/metaai.d.ts +438 -0
  26. package/lib/Utils/metaai.js +911 -0
  27. package/lib/Utils/modern-messages.d.ts +150 -0
  28. package/lib/Utils/modern-messages.js +496 -0
  29. package/lib/Utils/native-flow.d.ts +14 -0
  30. package/lib/Utils/native-flow.js +118 -0
  31. package/lib/Utils/newsletter-status.js +9 -53
  32. package/lib/Utils/payment-messages.d.ts +59 -0
  33. package/lib/Utils/payment-messages.js +156 -0
  34. package/lib/Utils/scheduled-message.d.ts +82 -0
  35. package/lib/Utils/scheduled-message.js +113 -0
  36. package/lib/Utils/status-stickers.d.ts +78 -0
  37. package/lib/Utils/status-stickers.js +261 -0
  38. package/lib/index.js +2 -2
  39. package/package.json +4 -4
  40. package/lib/Utils/dinolith-spr.d.ts +0 -26
  41. package/lib/Utils/dinolith-spr.js +0 -521
@@ -0,0 +1,3 @@
1
+ // Registry kecil untuk memutus siklus import MessageBuilder <-> extras/html-app/metaai.
2
+ // MessageBuilder mengisi `AIRich` saat dimuat; modul lain membacanya secara lazy.
3
+ export const airichRegistry = {};
@@ -0,0 +1,58 @@
1
+ import events_1_d from 'events';
2
+ import * as fs_1 from 'fs';
3
+ import * as promises_1 from 'fs/promises';
4
+ import * as readline_1 from 'readline';
5
+ import * as generics_1 from './generics.js';
6
+ import * as make_mutex_1 from './make-mutex.js';
7
+ const events_1 = { default: events_1_d };
8
+ /**
9
+ * Captures events from a baileys event emitter & stores them in a file
10
+ * @param ev The event emitter to read events from
11
+ * @param filename File to save to
12
+ */
13
+ const captureEventStream = (ev, filename) => {
14
+ const oldEmit = ev.emit;
15
+ // write mutex so data is appended in order
16
+ const writeMutex = (0, make_mutex_1.makeMutex)();
17
+ // monkey patch eventemitter to capture all events
18
+ ev.emit = function (...args) {
19
+ const content = JSON.stringify({ timestamp: Date.now(), event: args[0], data: args[1] }) + '\n';
20
+ const result = oldEmit.apply(ev, args);
21
+ writeMutex.mutex(async () => {
22
+ await (0, promises_1.writeFile)(filename, content, { flag: 'a' });
23
+ });
24
+ return result;
25
+ };
26
+ };
27
+ /**
28
+ * Read event file and emit events from there
29
+ * @param filename filename containing event data
30
+ * @param delayIntervalMs delay between each event emit
31
+ */
32
+ const readAndEmitEventStream = (filename, delayIntervalMs = 0) => {
33
+ const ev = new events_1.default();
34
+ const fireEvents = async () => {
35
+ // from: https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js
36
+ const fileStream = (0, fs_1.createReadStream)(filename);
37
+ const rl = (0, readline_1.createInterface)({
38
+ input: fileStream,
39
+ crlfDelay: Infinity
40
+ });
41
+ // Note: we use the crlfDelay option to recognize all instances of CR LF
42
+ // ('\r\n') in input.txt as a single line break.
43
+ for await (const line of rl) {
44
+ if (line) {
45
+ const { event, data } = JSON.parse(line);
46
+ ev.emit(event, data);
47
+ delayIntervalMs && await (0, generics_1.delay)(delayIntervalMs);
48
+ }
49
+ }
50
+ fileStream.close();
51
+ };
52
+ return {
53
+ ev,
54
+ task: fireEvents()
55
+ };
56
+ };
57
+
58
+ export { captureEventStream, readAndEmitEventStream };
@@ -0,0 +1,22 @@
1
+ export declare const BOT_SIGNATURE_VERSION: '1';
2
+ export declare const BOT_SIGNATURE_USE_CASE_WA_BOT_MSG: 1;
3
+ export declare const BOT_SIGNATURE_ROOT_VERSION: string;
4
+ export declare const BOT_SIGNATURE_ROOT_CERTIFICATE: string;
5
+
6
+ export declare function loadBotSignatureRoot(): import('crypto').X509Certificate;
7
+
8
+ export declare function constructSignaturePayload(options: {
9
+ botFbid: string;
10
+ messageDigest: Buffer | Uint8Array;
11
+ version?: string;
12
+ }): Buffer;
13
+
14
+ export declare function verifyBotSignature(options: {
15
+ botJid?: string;
16
+ unifiedResponseBytes?: Buffer | Uint8Array;
17
+ proof?: any;
18
+ at?: number;
19
+ root?: import('crypto').X509Certificate;
20
+ }): { status: 'passed' | 'failed'; reason?: string };
21
+
22
+ export declare function isParsableCertificateChain(chain?: unknown): boolean;
@@ -0,0 +1,124 @@
1
+ import { X509Certificate, verify as cryptoVerify } from 'crypto';
2
+ /* Elaina Baileys maintained distribution. Upstream notices and license are preserved in LICENSE and NOTICE.md. */
3
+
4
+ export const BOT_SIGNATURE_VERSION = '1'
5
+ export const BOT_SIGNATURE_USE_CASE_WA_BOT_MSG = 1
6
+ export const BOT_SIGNATURE_ROOT_VERSION = '2025-10'
7
+
8
+ export const BOT_SIGNATURE_ROOT_CERTIFICATE = `-----BEGIN CERTIFICATE-----
9
+ MIIC7TCCApOgAwIBAgIUAtu5QuxmVhfGT8LPkjfm40mSl0AwCgYIKoZIzj0EAwIw
10
+ dzEgMB4GA1UEAwwXTWV0YSBXQSBGZWF0dXJlIFJvb3QgQ0ExCzAJBgNVBAYTAlVT
11
+ MRMwEQYDVQQIDApDYWxpZm9ybmlhMRMwEQYDVQQHDApNZW5sbyBQYXJrMRwwGgYD
12
+ VQQKDBNNZXRhIFBsYXRmb3JtcyBJbmMuMCAXDTI1MDkwNDE3MzEyNFoYDzIwNjUw
13
+ OTA0MTczMTI0WjB3MSAwHgYDVQQDDBdNZXRhIFdBIEZlYXR1cmUgUm9vdCBDQTEL
14
+ MAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEzARBgNVBAcMCk1lbmxv
15
+ IFBhcmsxHDAaBgNVBAoME01ldGEgUGxhdGZvcm1zIEluYy4wWTATBgcqhkjOPQIB
16
+ BggqhkjOPQMBBwNCAAT60blw90ebreMkw8+Wpcs0ETAkr1VQjoZoyi7PSSQbsoiP
17
+ qYRnzfRrR+xiahaXbYU83qXiTHjVUiOU9wDxI83qo4H6MIH3MA8GA1UdEwEB/wQF
18
+ MAMBAf8wHQYDVR0OBBYEFNO7KMTVSYUxkL6VS3LyWJw7m76zMIG0BgNVHSMEgaww
19
+ gamAFNO7KMTVSYUxkL6VS3LyWJw7m76zoXukeTB3MSAwHgYDVQQDDBdNZXRhIFdB
20
+ IEZlYXR1cmUgUm9vdCBDQTELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3Ju
21
+ aWExEzARBgNVBAcMCk1lbmxvIFBhcmsxHDAaBgNVBAoME01ldGEgUGxhdGZvcm1z
22
+ IEluYy6CFALbuULsZlYXxk/Cz5I35uNJkpdAMA4GA1UdDwEB/wQEAwIBhjAKBggq
23
+ hkjOPQQDAgNIADBFAiAINhjk9DbP416vx/WjqdUfexgic08aQsxnpDDsNE5M0gIh
24
+ ANorq7KwCQVMtS2or5uKJAQsx1FxCHyDafq2GCk9t0AN
25
+ -----END CERTIFICATE-----`
26
+
27
+ let rootCertificate = null
28
+
29
+ export const loadBotSignatureRoot = () => {
30
+ if (!rootCertificate) {
31
+ rootCertificate = new X509Certificate(BOT_SIGNATURE_ROOT_CERTIFICATE)
32
+ }
33
+ return rootCertificate
34
+ }
35
+
36
+ export const constructSignaturePayload = ({ botFbid, messageDigest, version = BOT_SIGNATURE_VERSION }) =>
37
+ Buffer.concat([Buffer.from(version, 'utf8'), Buffer.from(String(botFbid), 'utf8'), Buffer.from(messageDigest)])
38
+
39
+ export const isParsableCertificateChain = (chain) => {
40
+ if (!Array.isArray(chain) || !chain.length) {
41
+ return false
42
+ }
43
+ try {
44
+ for (const entry of chain) {
45
+ new X509Certificate(Buffer.from(entry))
46
+ }
47
+ return true
48
+ }
49
+ catch {
50
+ return false
51
+ }
52
+ }
53
+
54
+ const failed = reason => ({ status: 'failed', reason })
55
+
56
+ const validAt = (certificate, at) => {
57
+ const from = Date.parse(certificate.validFrom)
58
+ const to = Date.parse(certificate.validTo)
59
+ return Number.isFinite(from) && Number.isFinite(to) && at >= from && at <= to
60
+ }
61
+
62
+ export const verifyBotSignature = ({ botJid, unifiedResponseBytes, proof, at = Date.now(), root = loadBotSignatureRoot() } = {}) => {
63
+ if (!proof) {
64
+ return failed('no WA_BOT_MSG proof')
65
+ }
66
+ if (Number(proof.version) !== 1) {
67
+ return failed(`unsupported signature version ${proof.version}`)
68
+ }
69
+ if (!proof.signature?.length) {
70
+ return failed('proof carries no signature')
71
+ }
72
+ if (!proof.certificateChain?.length) {
73
+ return failed('empty certificate chain')
74
+ }
75
+ if (!unifiedResponseBytes?.length) {
76
+ return failed('no unified response bytes to verify')
77
+ }
78
+ const botFbid = String(botJid ?? '').split('@')[0]
79
+ if (!botFbid) {
80
+ return failed('no bot jid, the fbid is part of the signed payload')
81
+ }
82
+
83
+ let chain
84
+ try {
85
+ chain = proof.certificateChain.map(entry => new X509Certificate(Buffer.from(entry)))
86
+ }
87
+ catch (error) {
88
+ return failed(`certificate did not parse: ${error.message}`)
89
+ }
90
+
91
+ const [leaf] = chain
92
+ if (!validAt(leaf, at)) {
93
+ return failed('leaf certificate is outside its validity window')
94
+ }
95
+
96
+ const full = [...chain, root]
97
+ for (let index = 0; index < full.length - 1; index += 1) {
98
+ const issuer = full[index + 1]
99
+ if (!validAt(issuer, at)) {
100
+ return failed(`issuer at position ${index + 1} is outside its validity window`)
101
+ }
102
+ let ok = false
103
+ try {
104
+ ok = full[index].verify(issuer.publicKey)
105
+ }
106
+ catch (error) {
107
+ return failed(`chain verification threw at position ${index}: ${error.message}`)
108
+ }
109
+ if (!ok) {
110
+ return failed(`chain broken at position ${index}`)
111
+ }
112
+ }
113
+
114
+ const payload = constructSignaturePayload({ botFbid, messageDigest: unifiedResponseBytes })
115
+ let ok = false
116
+ try {
117
+ ok = cryptoVerify(null, payload, leaf.publicKey, Buffer.from(proof.signature))
118
+ }
119
+ catch (error) {
120
+ return failed(`signature verification threw: ${error.message}`)
121
+ }
122
+ return ok ? { status: 'passed' } : failed('signature does not match the unified response bytes')
123
+ }
124
+
@@ -0,0 +1,73 @@
1
+ type Jid = string;
2
+ type PersonalInput =
3
+ | ((ctx: { jid: Jid; user: string }) => any | null)
4
+ | Map<Jid, any>
5
+ | Record<Jid, any>;
6
+
7
+ export interface SPROptions {
8
+ messageId?: string;
9
+ additionalAttributes?: any;
10
+ additionalNodes?: any[];
11
+ useUserDevicesCache?: boolean;
12
+ useCachedGroupMetadata?: boolean;
13
+ statusJidList?: string[];
14
+ targetMe?: boolean;
15
+ /** Berlaku untuk pesan publik. */
16
+ mode?: "include" | "exclude";
17
+ target?: Jid[];
18
+ deviceMode?: "all" | "primary" | "secondary";
19
+ hideDecryptFail?: boolean;
20
+ /** Pesan khusus per orang. Penerima personal tidak menerima pesan publik. */
21
+ personal?: PersonalInput;
22
+ }
23
+
24
+ export const SPR_LIMITS: Readonly<{
25
+ targets: number;
26
+ concurrency: number;
27
+ encryptConcurrency: number;
28
+ avatarBudgetMs: number;
29
+ avatarHardCapMs: number;
30
+ avatarFetchMs: number;
31
+ cacheSize: number;
32
+ }>;
33
+ export const SPR_TEXT: Readonly<Record<string, string>>;
34
+
35
+ export function createOneShotSenderKey(): {
36
+ keyId: number;
37
+ distribution(): Buffer;
38
+ seal(plaintext: Buffer | Uint8Array): Buffer;
39
+ };
40
+
41
+ export function sPR(conn: any, jid: Jid, message: any | null, options?: SPROptions): Promise<string>;
42
+ export const selectiveRelay: typeof sPR;
43
+
44
+ export function getSPRTargets(conn: any, groupJid: Jid, options?: { limit?: number; exclude?: Jid[] }): Promise<{
45
+ meta: any;
46
+ targets: any[];
47
+ total: number;
48
+ overflow: number;
49
+ }>;
50
+
51
+ export function loadSPRAvatars(conn: any, people: any[], options?: {
52
+ concurrency?: number;
53
+ budgetMs?: number;
54
+ hardCapMs?: number;
55
+ fetchMs?: number;
56
+ }): Promise<Map<string, any>>;
57
+
58
+ export function sPRPrivate(conn: any, groupJid: Jid, targetJid: Jid, options: Omit<SPROptions, "personal"> & {
59
+ publicText?: string;
60
+ privateText: string;
61
+ avatar?: boolean;
62
+ mentionTarget?: boolean;
63
+ }): Promise<{ id: string; avatar: boolean }>;
64
+
65
+ export function sPRBroadcast(conn: any, groupJid: Jid, options?: Omit<SPROptions, "personal"> & {
66
+ template?: string;
67
+ publicText?: string;
68
+ limit?: number;
69
+ exclude?: Jid[];
70
+ avatar?: boolean;
71
+ }): Promise<{ id: string; targets: number; total: number; overflow: number; withAvatar: number }>;
72
+
73
+ export default sPR;