@ibradecode/bails
Version:
WhatsApp API
100 lines (99 loc) • 4.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SenderKeyState = void 0;
const sender_chain_key_1 = require("./sender-chain-key");
const sender_message_key_1 = require("./sender-message-key");
class SenderKeyState {
constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
this.MAX_MESSAGE_KEYS = 2000;
if (senderKeyStateStructure) {
this.senderKeyStateStructure = senderKeyStateStructure;
}
else {
if (signatureKeyPair) {
signatureKeyPublic = signatureKeyPair.public;
signatureKeyPrivate = signatureKeyPair.private;
}
chainKey = typeof chainKey === 'string' ? Buffer.from(chainKey, 'base64') : chainKey;
const senderChainKeyStructure = {
iteration: iteration || 0,
seed: chainKey || Buffer.alloc(0)
};
const signingKeyStructure = {
public: typeof signatureKeyPublic === 'string'
? Buffer.from(signatureKeyPublic, 'base64')
: signatureKeyPublic || Buffer.alloc(0)
};
if (signatureKeyPrivate) {
signingKeyStructure.private =
typeof signatureKeyPrivate === 'string' ? Buffer.from(signatureKeyPrivate, 'base64') : signatureKeyPrivate;
}
this.senderKeyStateStructure = {
senderKeyId: id || 0,
senderChainKey: senderChainKeyStructure,
senderSigningKey: signingKeyStructure,
senderMessageKeys: []
};
}
}
getKeyId() {
return this.senderKeyStateStructure.senderKeyId;
}
getSenderChainKey() {
return new sender_chain_key_1.SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
}
setSenderChainKey(chainKey) {
this.senderKeyStateStructure.senderChainKey = {
iteration: chainKey.getIteration(),
seed: chainKey.getSeed()
};
}
getSigningKeyPublic() {
const publicKey = this.senderKeyStateStructure.senderSigningKey.public;
if (publicKey instanceof Buffer) {
return publicKey;
}
else if (typeof publicKey === 'string') {
return Buffer.from(publicKey, 'base64');
}
return Buffer.from(publicKey || []);
}
getSigningKeyPrivate() {
const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
if (!privateKey) {
return undefined;
}
if (privateKey instanceof Buffer) {
return privateKey;
}
else if (typeof privateKey === 'string') {
return Buffer.from(privateKey, 'base64');
}
return Buffer.from(privateKey || []);
}
hasSenderMessageKey(iteration) {
return this.senderKeyStateStructure.senderMessageKeys.some(key => key.iteration === iteration);
}
addSenderMessageKey(senderMessageKey) {
this.senderKeyStateStructure.senderMessageKeys.push({
iteration: senderMessageKey.getIteration(),
seed: senderMessageKey.getSeed()
});
if (this.senderKeyStateStructure.senderMessageKeys.length > this.MAX_MESSAGE_KEYS) {
this.senderKeyStateStructure.senderMessageKeys.shift();
}
}
removeSenderMessageKey(iteration) {
const index = this.senderKeyStateStructure.senderMessageKeys.findIndex(key => key.iteration === iteration);
if (index !== -1) {
const messageKey = this.senderKeyStateStructure.senderMessageKeys[index];
this.senderKeyStateStructure.senderMessageKeys.splice(index, 1);
return new sender_message_key_1.SenderMessageKey(messageKey.iteration, messageKey.seed);
}
return null;
}
getStructure() {
return this.senderKeyStateStructure;
}
}
exports.SenderKeyState = SenderKeyState;