Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@adiwajshing/baileys/WASignalGroup/sender_key_distribution_message.js
1126 views
1
const CiphertextMessage = require('./ciphertext_message');
2
const protobufs = require('./protobufs');
3
4
class SenderKeyDistributionMessage extends CiphertextMessage {
5
constructor(
6
id = null,
7
iteration = null,
8
chainKey = null,
9
signatureKey = null,
10
serialized = null
11
) {
12
super();
13
if (serialized) {
14
try {
15
const version = serialized[0];
16
const message = serialized.slice(1);
17
18
const distributionMessage = protobufs.SenderKeyDistributionMessage.decode(
19
message
20
).toJSON();
21
this.serialized = serialized;
22
this.id = distributionMessage.id;
23
this.iteration = distributionMessage.iteration;
24
this.chainKey = distributionMessage.chainKey;
25
this.signatureKey = distributionMessage.signingKey;
26
} catch (e) {
27
throw new Error(e);
28
}
29
} else {
30
const version = this.intsToByteHighAndLow(this.CURRENT_VERSION, this.CURRENT_VERSION);
31
this.id = id;
32
this.iteration = iteration;
33
this.chainKey = chainKey;
34
this.signatureKey = signatureKey;
35
const message = protobufs.SenderKeyDistributionMessage.encode(
36
protobufs.SenderKeyDistributionMessage.create({
37
id,
38
iteration,
39
chainKey,
40
signingKey: this.signatureKey,
41
})
42
).finish();
43
this.serialized = Buffer.concat([Buffer.from([version]), message]);
44
}
45
}
46
47
intsToByteHighAndLow(highValue, lowValue) {
48
return (((highValue << 4) | lowValue) & 0xff) % 256;
49
}
50
51
serialize() {
52
return this.serialized;
53
}
54
55
getType() {
56
return this.SENDERKEY_DISTRIBUTION_TYPE;
57
}
58
59
getIteration() {
60
return this.iteration;
61
}
62
63
getChainKey() {
64
return typeof this.chainKey === 'string' ? Buffer.from(this.chainKey, 'base64') : this.chainKey;
65
}
66
67
getSignatureKey() {
68
return typeof this.signatureKey === 'string'
69
? Buffer.from(this.signatureKey, 'base64')
70
: this.signatureKey;
71
}
72
73
getId() {
74
return this.id;
75
}
76
}
77
78
module.exports = SenderKeyDistributionMessage;
79