Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/auth-utils.js
2593 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.useSingleFileAuthState = exports.initAuthCreds = exports.addTransactionCapability = void 0;
4
const boom_1 = require("@hapi/boom");
5
const crypto_1 = require("crypto");
6
const WAProto_1 = require("../../WAProto");
7
const crypto_2 = require("./crypto");
8
const generics_1 = require("./generics");
9
const KEY_MAP = {
10
'pre-key': 'preKeys',
11
'session': 'sessions',
12
'sender-key': 'senderKeys',
13
'app-state-sync-key': 'appStateSyncKeys',
14
'app-state-sync-version': 'appStateVersions',
15
'sender-key-memory': 'senderKeyMemory'
16
};
17
const addTransactionCapability = (state, logger) => {
18
let inTransaction = false;
19
let transactionCache = {};
20
let mutations = {};
21
const prefetch = async (type, ids) => {
22
if (!inTransaction) {
23
throw new boom_1.Boom('Cannot prefetch without transaction');
24
}
25
const dict = transactionCache[type];
26
const idsRequiringFetch = dict ? ids.filter(item => !(item in dict)) : ids;
27
// only fetch if there are any items to fetch
28
if (idsRequiringFetch.length) {
29
const result = await state.get(type, idsRequiringFetch);
30
transactionCache[type] = transactionCache[type] || {};
31
Object.assign(transactionCache[type], result);
32
}
33
};
34
return {
35
get: async (type, ids) => {
36
if (inTransaction) {
37
await prefetch(type, ids);
38
return ids.reduce((dict, id) => {
39
var _a;
40
const value = (_a = transactionCache[type]) === null || _a === void 0 ? void 0 : _a[id];
41
if (value) {
42
dict[id] = value;
43
}
44
return dict;
45
}, {});
46
}
47
else {
48
return state.get(type, ids);
49
}
50
},
51
set: data => {
52
if (inTransaction) {
53
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
54
for (const key in data) {
55
transactionCache[key] = transactionCache[key] || {};
56
Object.assign(transactionCache[key], data[key]);
57
mutations[key] = mutations[key] || {};
58
Object.assign(mutations[key], data[key]);
59
}
60
}
61
else {
62
return state.set(data);
63
}
64
},
65
isInTransaction: () => inTransaction,
66
prefetch: (type, ids) => {
67
logger.trace({ type, ids }, 'prefetching');
68
return prefetch(type, ids);
69
},
70
transaction: async (work) => {
71
if (inTransaction) {
72
await work();
73
}
74
else {
75
logger.debug('entering transaction');
76
inTransaction = true;
77
try {
78
await work();
79
if (Object.keys(mutations).length) {
80
logger.debug('committing transaction');
81
await state.set(mutations);
82
}
83
else {
84
logger.debug('no mutations in transaction');
85
}
86
}
87
finally {
88
inTransaction = false;
89
transactionCache = {};
90
mutations = {};
91
}
92
}
93
}
94
};
95
};
96
exports.addTransactionCapability = addTransactionCapability;
97
const initAuthCreds = () => {
98
const identityKey = crypto_2.Curve.generateKeyPair();
99
return {
100
noiseKey: crypto_2.Curve.generateKeyPair(),
101
signedIdentityKey: identityKey,
102
signedPreKey: crypto_2.signedKeyPair(identityKey, 1),
103
registrationId: generics_1.generateRegistrationId(),
104
advSecretKey: crypto_1.randomBytes(32).toString('base64'),
105
nextPreKeyId: 1,
106
firstUnuploadedPreKeyId: 1,
107
serverHasPreKeys: false
108
};
109
};
110
exports.initAuthCreds = initAuthCreds;
111
/** stores the full authentication state in a single JSON file */
112
const useSingleFileAuthState = (filename, logger) => {
113
// require fs here so that in case "fs" is not available -- the app does not crash
114
const { readFileSync, writeFileSync, existsSync } = require('fs');
115
let creds;
116
let keys = {};
117
// save the authentication state to a file
118
const saveState = () => {
119
logger && logger.trace('saving auth state');
120
writeFileSync(filename,
121
// BufferJSON replacer utility saves buffers nicely
122
JSON.stringify({ creds, keys }, generics_1.BufferJSON.replacer, 2));
123
};
124
if (existsSync(filename)) {
125
const result = JSON.parse(readFileSync(filename, { encoding: 'utf-8' }), generics_1.BufferJSON.reviver);
126
creds = result.creds;
127
keys = result.keys;
128
}
129
else {
130
creds = exports.initAuthCreds();
131
keys = {};
132
}
133
return {
134
state: {
135
creds,
136
keys: {
137
get: (type, ids) => {
138
const key = KEY_MAP[type];
139
return ids.reduce((dict, id) => {
140
var _a;
141
let value = (_a = keys[key]) === null || _a === void 0 ? void 0 : _a[id];
142
if (value) {
143
if (type === 'app-state-sync-key') {
144
value = WAProto_1.proto.AppStateSyncKeyData.fromObject(value);
145
}
146
dict[id] = value;
147
}
148
return dict;
149
}, {});
150
},
151
set: (data) => {
152
for (const _key in data) {
153
const key = KEY_MAP[_key];
154
keys[key] = keys[key] || {};
155
Object.assign(keys[key], data[_key]);
156
}
157
saveState();
158
}
159
}
160
},
161
saveState
162
};
163
};
164
exports.useSingleFileAuthState = useSingleFileAuthState;
165
166