Repository navigation
Expand file tree
/
Copy pathmongoAuthState.js
More file actions
125 lines (116 loc) · 3.17 KB
/
Copy pathmongoAuthState.js
File metadata and controls
125 lines (116 loc) · 3.17 KB
1
2
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const { proto } = require("@whiskeysockets/baileys/WAProto");
const {
Curve,
signedKeyPair,
} = require("@whiskeysockets/baileys/lib/Utils/crypto");
const {
generateRegistrationId,
} = require("@whiskeysockets/baileys/lib/Utils/generics");
const { randomBytes } = require("crypto");
const initAuthCreds = () => {
const identityKey = Curve.generateKeyPair();
return {
noiseKey: Curve.generateKeyPair(),
signedIdentityKey: identityKey,
signedPreKey: signedKeyPair(identityKey, 1),
registrationId: generateRegistrationId(),
advSecretKey: randomBytes(32).toString("base64"),
processedHistoryMessages: [],
nextPreKeyId: 1,
firstUnuploadedPreKeyId: 1,
accountSettings: {
unarchiveChats: false,
},
};
};
const BufferJSON = {
replacer: (k, value) => {
if (
Buffer.isBuffer(value) ||
value instanceof Uint8Array ||
value?.type === "Buffer"
) {
return {
type: "Buffer",
data: Buffer.from(value?.data || value).toString("base64"),
};
}
return value;
},
reviver: (_, value) => {
if (
typeof value === "object" &&
!!value &&
(value.buffer === true || value.type === "Buffer")
) {
const val = value.data || value.value;
return typeof val === "string"
? Buffer.from(val, "base64")
: Buffer.from(val || []);
}
return value;
},
};
const useMongoDBAuthState = async (collection) => {
const writeData = (data, id) => {
const informationToStore = JSON.parse(
JSON.stringify(data, BufferJSON.replacer)
);
const update = {
$set: {
...informationToStore,
},
};
return collection.updateOne({ _id: id }, update, { upsert: true });
};
const readData = async (id) => {
try {
const data = await collection.findOne({ _id: id });
return data ? JSON.parse(JSON.stringify(data), BufferJSON.reviver) : null;
} catch (error) {
return null;
}
};
const removeData = async (id) => {
try {
await collection.deleteOne({ _id: id });
} catch (_a) {}
};
const creds = (await readData("creds")) || initAuthCreds();
return {
state: {
creds,
keys: {
get: async (type, ids) => {
const data = {};
await Promise.all(
ids.map(async (id) => {
let value = await readData(`${type}-${id}`);
if (type === "app-state-sync-key" && value) {
value = proto.Message.AppStateSyncKeyData.fromObject(value);
}
data[id] = value;
})
);
return data;
},
set: async (data) => {
const tasks = [];
for (const category of Object.keys(data)) {
for (const id of Object.keys(data[category])) {
const value = data[category][id];
const key = `${category}-${id}`;
// console.log("id", id);
tasks.push(value ? writeData(value, key) : removeData(key));
}
}
await Promise.all(tasks);
},
},
},
saveCreds: () => {
return writeData(creds, "creds");
},
};
};
module.exports = useMongoDBAuthState;