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_message.js
1126 views
1
const CiphertextMessage = require('./ciphertext_message');
2
const curve = require('libsignal/src/curve');
3
const protobufs = require('./protobufs');
4
5
class SenderKeyMessage extends CiphertextMessage {
6
SIGNATURE_LENGTH = 64;
7
8
constructor(
9
keyId = null,
10
iteration = null,
11
ciphertext = null,
12
signatureKey = null,
13
serialized = null
14
) {
15
super();
16
if (serialized) {
17
const version = serialized[0];
18
const message = serialized.slice(1, serialized.length - this.SIGNATURE_LENGTH);
19
const signature = serialized.slice(-1 * this.SIGNATURE_LENGTH);
20
const senderKeyMessage = protobufs.SenderKeyMessage.decode(message).toJSON();
21
senderKeyMessage.ciphertext = Buffer.from(senderKeyMessage.ciphertext, 'base64');
22
23
this.serialized = serialized;
24
this.messageVersion = (version & 0xff) >> 4;
25
26
this.keyId = senderKeyMessage.id;
27
this.iteration = senderKeyMessage.iteration;
28
this.ciphertext = senderKeyMessage.ciphertext;
29
this.signature = signature;
30
} else {
31
const version = (((this.CURRENT_VERSION << 4) | this.CURRENT_VERSION) & 0xff) % 256;
32
ciphertext = Buffer.from(ciphertext); // .toString('base64');
33
const message = protobufs.SenderKeyMessage.encode(
34
protobufs.SenderKeyMessage.create({
35
id: keyId,
36
iteration,
37
ciphertext,
38
})
39
).finish();
40
41
const signature = this.getSignature(
42
signatureKey,
43
Buffer.concat([Buffer.from([version]), message])
44
);
45
this.serialized = Buffer.concat([Buffer.from([version]), message, Buffer.from(signature)]);
46
this.messageVersion = this.CURRENT_VERSION;
47
this.keyId = keyId;
48
this.iteration = iteration;
49
this.ciphertext = ciphertext;
50
this.signature = signature;
51
}
52
}
53
54
getKeyId() {
55
return this.keyId;
56
}
57
58
getIteration() {
59
return this.iteration;
60
}
61
62
getCipherText() {
63
return this.ciphertext;
64
}
65
66
verifySignature(signatureKey) {
67
const part1 = this.serialized.slice(0, this.serialized.length - this.SIGNATURE_LENGTH + 1);
68
const part2 = this.serialized.slice(-1 * this.SIGNATURE_LENGTH);
69
const res = curve.verifySignature(signatureKey, part1, part2);
70
if (!res) throw new Error('Invalid signature!');
71
}
72
73
getSignature(signatureKey, serialized) {
74
const signature = Buffer.from(
75
curve.calculateSignature(
76
signatureKey,
77
serialized
78
)
79
);
80
return signature;
81
}
82
83
serialize() {
84
return this.serialized;
85
}
86
87
getType() {
88
return 4;
89
}
90
}
91
92
module.exports = SenderKeyMessage;
93