Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/auth-utils.js
1129 views
1
"use strict";
2
Object.defineProperty(exports, "__esModule", { value: true });
3
exports.initAuthCreds = exports.addTransactionCapability = void 0;
4
const boom_1 = require("@hapi/boom");
5
const crypto_1 = require("crypto");
6
const crypto_2 = require("./crypto");
7
const generics_1 = require("./generics");
8
/**
9
* Adds DB like transaction capability (https://en.wikipedia.org/wiki/Database_transaction) to the SignalKeyStore,
10
* this allows batch read & write operations & improves the performance of the lib
11
* @param state the key store to apply this capability to
12
* @param logger logger to log events
13
* @returns SignalKeyStore with transaction capability
14
*/
15
const addTransactionCapability = (state, logger, { maxCommitRetries, delayBetweenTriesMs }) => {
16
let inTransaction = false;
17
// number of queries made to the DB during the transaction
18
// only there for logging purposes
19
let dbQueriesInTransaction = 0;
20
let transactionCache = {};
21
let mutations = {};
22
/**
23
* prefetches some data and stores in memory,
24
* useful if these data points will be used together often
25
* */
26
const prefetch = async (type, ids) => {
27
if (!inTransaction) {
28
throw new boom_1.Boom('Cannot prefetch without transaction');
29
}
30
const dict = transactionCache[type];
31
const idsRequiringFetch = dict ? ids.filter(item => !(item in dict)) : ids;
32
// only fetch if there are any items to fetch
33
if (idsRequiringFetch.length) {
34
dbQueriesInTransaction += 1;
35
const result = await state.get(type, idsRequiringFetch);
36
transactionCache[type] = Object.assign(transactionCache[type] || {}, result);
37
}
38
};
39
return {
40
get: async (type, ids) => {
41
if (inTransaction) {
42
await prefetch(type, ids);
43
return ids.reduce((dict, id) => {
44
var _a;
45
const value = (_a = transactionCache[type]) === null || _a === void 0 ? void 0 : _a[id];
46
if (value) {
47
dict[id] = value;
48
}
49
return dict;
50
}, {});
51
}
52
else {
53
return state.get(type, ids);
54
}
55
},
56
set: data => {
57
if (inTransaction) {
58
logger.trace({ types: Object.keys(data) }, 'caching in transaction');
59
for (const key in data) {
60
transactionCache[key] = transactionCache[key] || {};
61
Object.assign(transactionCache[key], data[key]);
62
mutations[key] = mutations[key] || {};
63
Object.assign(mutations[key], data[key]);
64
}
65
}
66
else {
67
return state.set(data);
68
}
69
},
70
isInTransaction: () => inTransaction,
71
prefetch: (type, ids) => {
72
logger.trace({ type, ids }, 'prefetching');
73
return prefetch(type, ids);
74
},
75
transaction: async (work) => {
76
// if we're already in a transaction,
77
// just execute what needs to be executed -- no commit required
78
if (inTransaction) {
79
await work();
80
}
81
else {
82
logger.trace('entering transaction');
83
inTransaction = true;
84
try {
85
await work();
86
if (Object.keys(mutations).length) {
87
logger.trace('committing transaction');
88
// retry mechanism to ensure we've some recovery
89
// in case a transaction fails in the first attempt
90
let tries = maxCommitRetries;
91
while (tries) {
92
tries -= 1;
93
try {
94
await state.set(mutations);
95
logger.trace({ dbQueriesInTransaction }, 'committed transaction');
96
break;
97
}
98
catch (error) {
99
logger.warn(`failed to commit ${Object.keys(mutations).length} mutations, tries left=${tries}`);
100
await (0, generics_1.delay)(delayBetweenTriesMs);
101
}
102
}
103
}
104
else {
105
logger.trace('no mutations in transaction');
106
}
107
}
108
finally {
109
inTransaction = false;
110
transactionCache = {};
111
mutations = {};
112
dbQueriesInTransaction = 0;
113
}
114
}
115
}
116
};
117
};
118
exports.addTransactionCapability = addTransactionCapability;
119
const initAuthCreds = () => {
120
const identityKey = crypto_2.Curve.generateKeyPair();
121
return {
122
noiseKey: crypto_2.Curve.generateKeyPair(),
123
signedIdentityKey: identityKey,
124
signedPreKey: (0, crypto_2.signedKeyPair)(identityKey, 1),
125
registrationId: (0, generics_1.generateRegistrationId)(),
126
advSecretKey: (0, crypto_1.randomBytes)(32).toString('base64'),
127
processedHistoryMessages: [],
128
nextPreKeyId: 1,
129
firstUnuploadedPreKeyId: 1,
130
accountSettings: {
131
unarchiveChats: false
132
}
133
};
134
};
135
exports.initAuthCreds = initAuthCreds;
136
137