-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmongoAuthState.js
More file actions
159 lines (149 loc) · 4.62 KB
/
mongoAuthState.js
File metadata and controls
159 lines (149 loc) · 4.62 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* MongoDB Authentication State Handler for Baileys
*
* This file contains code adapted from the mongo-baileys package:
* https://github.com/hacxk/mongo-baileys
*
* Original Author: HACXK
* Original License: MIT
*
* Modifications made:
* - Simplified implementation for specific use case
* - Removed TypeScript-specific features
* - Modified BufferJSON handling
* - Adjusted storage keys and structure
*
* Original MIT License:
* Copyright (c) HACXK
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
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;
},
};
module.exports = 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 = JSON.stringify(await collection.findOne({ _id: id }));
return JSON.parse(data, BufferJSON.reviver);
} catch (error) {
return null;
}
};
const removeData = async (id) => {
try {
await collection.deleteOne({ _id: id });
} catch (_a) {}
};
const creds = (await readData('creds')) || (0, 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}`;
tasks.push(value ? writeData(value, key) : removeData(key));
}
}
await Promise.all(tasks);
},
},
},
saveCreds: () => {
return writeData(creds, 'creds');
},
};
};