Skip to content

Commit

Permalink
feat: clear public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
DIY0R committed Nov 28, 2024
1 parent 5fd607e commit 4462228
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const DECRYPT_ERROR = 'Unable to decrypt the message.';
const SEND_ERROR = 'Failed to send the data.';
const PUBLISH_ERROR = 'Failed to publish the data.';
const DEFAULT_TIMEOUT = 10000;
const TWO_DAYS = 2 * 24 * 60 * 60 * 1000;
module.exports = {
PUBLIC_KEY_REQUEST,
PUBLIC_KEY_RESPONSE,
Expand All @@ -27,6 +28,7 @@ module.exports = {
TTL,
CRYPTO_DM,
DM,
TWO_DAYS,
ERROR,
SEND_ERROR,
DECRYPT_ERROR,
Expand Down
32 changes: 27 additions & 5 deletions lib/cryptoRsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
ERROR,
TIMEOUT_ERROR_REQUEST,
DECRYPT_ERROR,
TWO_DAYS,
} = require('./constants');

class CryptoRSA extends Connection {
Expand All @@ -22,13 +23,13 @@ class CryptoRSA extends Connection {

constructor(port, targetNode) {
super(port, targetNode);
this.#generateKeys();
this.#init();
}

_requestPublicKey(targetNode) {
const publicKey = this.#getTargetNodeKey(targetNode);
const nodePublic = this.#getTargetNodeKey(targetNode);
return new Promise((resolve, reject) => {
if (publicKey) return resolve(publicKey);
if (nodePublic) return resolve(nodePublic.publicKey);
const messageId = uuid();
const timerId = setTimeout(
() => reject(new Error(TIMEOUT_ERROR_REQUEST)),
Expand All @@ -45,6 +46,7 @@ class CryptoRSA extends Connection {
messageId,
TTL,
);
console.log(options);
this._broadcast(options);
});
}
Expand Down Expand Up @@ -83,11 +85,23 @@ class CryptoRSA extends Connection {
}
}

clearPublicKeys(dayInMillis = TWO_DAYS) {
const now = Date.now();
this._nodePublicKeys.forEach(({ timestamp }, key) => {
const timestampMillis = new Date(timestamp).getTime();
if (now - timestampMillis > dayInMillis) this._deleteNodeKey(key);
});
}

#getTargetNodeKey(nodeId) {
const publicKey = this._nodePublicKeys.get(nodeId);
if (publicKey) return publicKey;
}

#timerClearKeys() {
setInterval(this.clearPublicKeys.bind(this), TWO_DAYS);
}

#decryptMessage(message) {
const { encryptedMessage, encryptedSymKey, iv } = message;
const decryptedSymKey = crypto.privateDecrypt(
Expand Down Expand Up @@ -149,12 +163,20 @@ class CryptoRSA extends Connection {
messageData,
PUBLIC_KEY_RESPONSE,
);
if (!processedMessage) {
if (processedMessage) {
const { messageId, publicKey } = messageData.message;
this._nodePublicKeys.set(messageData.fromNode, publicKey);
this._nodePublicKeys.set(messageData.fromNode, {
publicKey,
timestamp: new Date().toISOString(),
});
this.#responseEmitter.emit(messageId, publicKey);
}
}

#init() {
this.#generateKeys();
this.#timerClearKeys();
}
}

module.exports = CryptoRSA;

0 comments on commit 4462228

Please sign in to comment.