84 lines
3.3 KiB
JavaScript
84 lines
3.3 KiB
JavaScript
import { SenderChainKey } from './sender-chain-key.js';
|
|
import { SenderMessageKey } from './sender-message-key.js';
|
|
export class SenderKeyState {
|
|
constructor(id, iteration, chainKey, signatureKeyPair, signatureKeyPublic, signatureKeyPrivate, senderKeyStateStructure) {
|
|
this.MAX_MESSAGE_KEYS = 2000;
|
|
if (senderKeyStateStructure) {
|
|
this.senderKeyStateStructure = {
|
|
...senderKeyStateStructure,
|
|
senderMessageKeys: Array.isArray(senderKeyStateStructure.senderMessageKeys)
|
|
? senderKeyStateStructure.senderMessageKeys
|
|
: []
|
|
};
|
|
}
|
|
else {
|
|
if (signatureKeyPair) {
|
|
signatureKeyPublic = signatureKeyPair.public;
|
|
signatureKeyPrivate = signatureKeyPair.private;
|
|
}
|
|
this.senderKeyStateStructure = {
|
|
senderKeyId: id || 0,
|
|
senderChainKey: {
|
|
iteration: iteration || 0,
|
|
seed: Buffer.from(chainKey || [])
|
|
},
|
|
senderSigningKey: {
|
|
public: Buffer.from(signatureKeyPublic || []),
|
|
private: Buffer.from(signatureKeyPrivate || [])
|
|
},
|
|
senderMessageKeys: []
|
|
};
|
|
}
|
|
}
|
|
getKeyId() {
|
|
return this.senderKeyStateStructure.senderKeyId;
|
|
}
|
|
getSenderChainKey() {
|
|
return new SenderChainKey(this.senderKeyStateStructure.senderChainKey.iteration, this.senderKeyStateStructure.senderChainKey.seed);
|
|
}
|
|
setSenderChainKey(chainKey) {
|
|
this.senderKeyStateStructure.senderChainKey = {
|
|
iteration: chainKey.getIteration(),
|
|
seed: chainKey.getSeed()
|
|
};
|
|
}
|
|
getSigningKeyPublic() {
|
|
const publicKey = Buffer.from(this.senderKeyStateStructure.senderSigningKey.public);
|
|
if (publicKey.length === 32) {
|
|
const fixed = Buffer.alloc(33);
|
|
fixed[0] = 0x05;
|
|
publicKey.copy(fixed, 1);
|
|
return fixed;
|
|
}
|
|
return publicKey;
|
|
}
|
|
getSigningKeyPrivate() {
|
|
const privateKey = this.senderKeyStateStructure.senderSigningKey.private;
|
|
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 SenderMessageKey(messageKey.iteration, messageKey.seed);
|
|
}
|
|
return null;
|
|
}
|
|
getStructure() {
|
|
return this.senderKeyStateStructure;
|
|
}
|
|
}
|
|
//# sourceMappingURL=sender-key-state.js.map
|