Błąd oznacza, że biblioteka libsignal-protocol-javascript nie została zainstalowana lub ma problemy z kompatybilnością. Oto rozwiązanie:
🔧 Szybkie rozwiązanie
Opcja 1: Użyj prostszego szyfrowania (ZALECANE)
Zamień zawartość chat.js na prostszą implementację z wbudowanym Node.js crypto:
javascript Copy code
const crypto = require('crypto');
class Chat {
constructor() {
this.sessions = new Map();
this.channels = new Map();
}
// Generuj klucze dla użytkownika
generateKeys() {
const keyPair = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
});
return keyPair;
}
// Szyfruj wiadomość
encryptMessage(message, publicKey) {
const buffer = Buffer.from(message, 'utf8');
const encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString('base64');
}
// Odszyfruj wiadomość
decryptMessage(encryptedMessage, privateKey) {
const buffer = Buffer.from(encryptedMessage, 'base64');
const decrypted = crypto.privateDecrypt(privateKey, buffer);
return decrypted.toString('utf8');
}
async sendMessage(message, nickname, channel) {
// Symulacja szyfrowania
const keys = this.generateKeys();
const encrypted = this.encryptMessage(message, keys.publicKey);
const decrypted = this.decryptMessage(encrypted, keys.privateKey);
console.log(`🔒 Zaszyfrowana wiadomość od ${nickname} na kanale ${channel}`);
console.log(`📝 Odszyfrowana treść: ${decrypted}`);
return { encrypted, decrypted };
}
}
module.exports = new Chat();
Opcja 2: Zainstaluj alternatywną bibliotekę
bash Copy code
# Usuń problematyczną bibliotekę
npm uninstall libsignal-protocol-javascript
# Zainstaluj prostszą alternatywę
npm install tweetnacl tweetnacl-util
Następnie zmień chat.js:
javascript Copy code
const nacl = require('tweetnacl');
const naclUtil = require('tweetnacl-util');
class Chat {
constructor() {
this.sessions = new Map();
}
generateKeyPair() {
return nacl.box.keyPair();
}
encryptMessage(message, theirPublicKey, mySecretKey) {
const nonce = nacl.randomBytes(24);
const messageUint8 = naclUtil.decodeUTF8(message);
const encrypted = nacl.box(messageUint8, nonce, theirPublicKey, mySecretKey);
return {
encrypted: naclUtil.encodeBase64(encrypted),
nonce: naclUtil.encodeBase64(nonce)
};
}
decryptMessage(encryptedData, nonce, theirPublicKey, mySecretKey) {
const encryptedUint8 = naclUtil.decodeBase64(encryptedData);
const nonceUint8 = naclUtil.decodeBase64(nonce);
const decrypted = nacl.box.open(encryptedUint8, nonceUint8, theirPublicKey, mySecretKey);
return naclUtil.encodeUTF8(decrypted);
}
async sendMessage(message, nickname, channel) {
const keyPair = this.generateKeyPair();
const encrypted = this.encryptMessage(message, keyPair.publicKey, keyPair.secretKey);
const decrypted = this.decryptMessage(encrypted.encrypted, encrypted.nonce, keyPair.publicKey, keyPair.secretKey);
console.log(`🔐 Wiadomość od ${nickname} na ${channel}: ${decrypted}`);
return { encrypted, decrypted };
}
}
module.exports = new Chat();
Opcja 3: Kompletnie bez szyfrowania (dla testów)
Jeśli chcesz tylko przetestować aplikację, zamień chat.js na:
javascript Copy code
class Chat {
constructor() {
this.messages = [];
}
async sendMessage(message, nickname, channel) {
const timestamp = new Date().toISOString();
const messageObj = { message, nickname, channel, timestamp };
this.messages.push(messageObj);
console.log(`💬 ${nickname} na #${channel}: ${message}`);
return messageObj;
}
getMessages(channel) {
return this.messages.filter(msg => msg.channel === channel);
}
}
module.exports = new Chat();
🚀 Uruchom ponownie
bash Copy code
npm start
# lub
node index.js
Zalecam Opcję 1 - używa wbudowanego crypto Node.js, jest stabilna i bezpieczna. Która opcja Cię interesuje?