diff --git a/server.js b/server.js index d9c11ca..d415377 100644 --- a/server.js +++ b/server.js @@ -9,18 +9,20 @@ var libsignal = require('libsignal'); const chatkit = new Chatkit.default({ instanceLocator: 'v1:us1:366480cc-91f1-4cbd-8c04-68dc96e8ccdf', - key: - '982eaea8-66f5-4bdb-9a2d-14de0fa8b92b:WSgfMJzA8cvZ/fwEqjupJhOpnwQ7qbAEOVze44y+XK0=' + key: '982eaea8-66f5-4bdb-9a2d-14de0fa8b92b:WSgfMJzA8cvZ/fwEqjupJhOpnwQ7qbAEOVze44y+XK0=' }) const app = express() -app.use(bodyParser.urlencoded({ extended: false })) +app.use(bodyParser.urlencoded({ + extended: false +})) app.use(bodyParser.json()) app.use(cors()) //-------- var KeyHelper = libsignal.keyhelper; -var sessionProm = null; +var sessionPromise = null; + function SignalProtocolStore() { this.store = {}; } @@ -104,7 +106,10 @@ SignalProtocolStore.prototype = { loadPreKey: function (keyId) { var res = this.get('25519KeypreKey' + keyId); if (res !== undefined) { - res = { pubKey: res.pubKey, privKey: res.privKey }; + res = { + pubKey: res.pubKey, + privKey: res.privKey + }; } return Promise.resolve(res); }, @@ -115,11 +120,14 @@ SignalProtocolStore.prototype = { return Promise.resolve(this.remove('25519KeypreKey' + keyId)); }, - /* Returns a signed keypair object or undefined */ + /* Returns aa signed keypair object or undefined */ loadSignedPreKey: function (keyId) { var res = this.get('25519KeysignedKey' + keyId); if (res !== undefined) { - res = { pubKey: res.pubKey, privKey: res.privKey }; + res = { + pubKey: res.pubKey, + privKey: res.privKey + }; } return Promise.resolve(res); }, @@ -148,6 +156,11 @@ SignalProtocolStore.prototype = { return Promise.resolve(); } }; + + +var senderStore = new SignalProtocolStore(); + +var receiverStore = new SignalProtocolStore(); //--------------- function generateIdentity(store) { return Promise.all([ @@ -199,6 +212,7 @@ function generatePreKeyBundle(store, preKeyId, signedPreKeyId) { //--------- app.post("/cryptMessage", async (req, res) => { + console.log("UĐEEEEEEEEEEEEE"); //sessionPromise=req.body.data.sessionPromise; message = req.body.data.message; //senderStore = req.body.data.senderStore; @@ -206,17 +220,17 @@ app.post("/cryptMessage", async (req, res) => { //---------------- - var senderAdress = req.body.data.senderAdress;//"xxxxxxxxx" - var receiverAdress = req.body.data.receiverAdress;//""yyyyyyyyyyyyy"" - var receiverPreKeyId = req.body.data.receiverPreKeyId;// 1337; - var receiverSignedKeyId = req.body.data.receiverSignedKeyId;// 1; + var senderAdress = req.body.data.senderAdress; //"xxxxxxxxx" + var receiverAdress = req.body.data.receiverAdress; //""yyyyyyyyyyyyy"" + var receiverPreKeyId = req.body.data.receiverPreKeyId; // 1337; + var receiverSignedKeyId = req.body.data.receiverSignedKeyId; // 1; var SENDER_ADDRESS = new libsignal.ProtocolAddress(senderAdress, 1); var RECEIVER_ADDRESS = new libsignal.ProtocolAddress(receiverAdress, 1); - var senderStore = new SignalProtocolStore(); + //var senderStore = new SignalProtocolStore(); - var receiverStore = new SignalProtocolStore(); + //var receiverStore = new SignalProtocolStore(); var receiverSignedKeyId = 1; @@ -229,7 +243,8 @@ app.post("/cryptMessage", async (req, res) => { return generatePreKeyBundle(receiverStore, receiverPreKeyId, receiverSignedKeyId); }).then(function (preKeyBundle) { var builder = new libsignal.SessionBuilder(senderStore, RECEIVER_ADDRESS); - sessionPromise = builder.initOutgoing(preKeyBundle).then(function () { + sessionPromise = builder.initOutgoing(preKeyBundle); + sessionPromise.then(function () { var originalMessage = Buffer.from(message, 'utf8'); var senderSessionCipher = new libsignal.SessionCipher(senderStore, RECEIVER_ADDRESS); @@ -237,79 +252,48 @@ app.post("/cryptMessage", async (req, res) => { //var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS); senderSessionCipher.encrypt(originalMessage).then(function (ciphertext) { + console.log("KRI, RECSTORE:", receiverStore); + console.log("KRI, RECadress:", SENDER_ADDRESS); - res.json({ data: { receiverStore: receiverStore, senderAdress: senderAdress, message: ciphertext.body } }) - - + res.json({ + data: { + receiverStore: receiverStore, + SENDER_ADDRESS: SENDER_ADDRESS, + message: ciphertext.body + } + }) }); - }); }); + }); -app.post("/decryptMessage2", async (req, res) => { - //sessionPromise=req.body.data.sessionPromise; - message = req.body.data.message; - //senderStore = req.body.data.senderStore; - //RECEIVER_ADDRESS = new libsignal.ProtocolAddress(req.body.data.receiverAdress.id, req.body.data.receiverAdress.deviceId); - //---------------- - - - var senderAdress = req.body.data.senderAdress;//"xxxxxxxxx" - var receiverAdress = req.body.data.receiverAdress;//""yyyyyyyyyyyyy"" - var receiverPreKeyId = req.body.data.receiverPreKeyId;// 1337; - var receiverSignedKeyId = req.body.data.receiverSignedKeyId;// 1; - - var SENDER_ADDRESS = new libsignal.ProtocolAddress(senderAdress, 1); - var RECEIVER_ADDRESS = new libsignal.ProtocolAddress(receiverAdress, 1); - - var senderStore = new SignalProtocolStore(); - - var receiverStore = new SignalProtocolStore(); - - var receiverSignedKeyId = 1; - - var Curve = libsignal.Curve; - - await Promise.all([ - generateIdentity(senderStore), - generateIdentity(receiverStore), - ]).then(function () { - return generatePreKeyBundle(receiverStore, receiverPreKeyId, receiverSignedKeyId); - }).then(function (preKeyBundle) { - var builder = new libsignal.SessionBuilder(senderStore, RECEIVER_ADDRESS); - sessionPromise = builder.initOutgoing(preKeyBundle).then(function () { - - const buf5 = Buffer.from(message.text.split(',')); - console.log(message) - var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS);//ubaceno - receiverSessionCipher.decryptPreKeyWhisperMessage(buf5, 'binary').then(function (ciphertext) { - console.log("DEKODIRANO:", ciphertext) - res.json({ data: { message: ciphertext } }) - - - }); - - }); - - }); -}); - app.post('/deCryptMessage', (req, res) => { - var receiverStore = req.body.data.receiverStore;//"xxxxxxxxx" - var senderAdress = req.body.data.senderAdress;//""yyyyyyyyyyyyy"" - var message = req.body.data.message;//""yyyyyyyyyyyyy"" - const buf5 = Buffer.from(message.text.split(',')); + + message = req.body.data.message; + const buf5 = Buffer.from(message.split(',')); + //receiverStore = req.body.data.receiverStore; + //SENDER_ADDRESS = req.body.data.SENDER_ADDRESS; + senderAdress = req.body.data.senderAdress; var SENDER_ADDRESS = new libsignal.ProtocolAddress(senderAdress, 1); + console.log("DEKRI, RECSTORE:", receiverStore.store.identityKey.pubKey); + console.log("DEKRI, RECSTORE:", SENDER_ADDRESS); + //var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS); - var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS);//ubaceno + //odavde + + var receiverSessionCipher = new libsignal.SessionCipher(receiverStore, SENDER_ADDRESS); //ubaceno receiverSessionCipher.decryptPreKeyWhisperMessage(buf5, 'binary').then(function (ciphertext) { - console.log("DEKODIRANO:", ciphertext) - res.json({ data: { message: ciphertext } }) + poruka = ciphertext.toString(); + res.json({ + data: { + message: poruka + } + }) }); @@ -320,8 +304,13 @@ app.post('/deCryptMessage', (req, res) => { app.post('/users', (req, res) => { - const { username } = req.body - const user = { name: username, id: username } + const { + username + } = req.body + const user = { + name: username, + id: username + } chatkit .createUser(user) .then(() => { diff --git a/src/Chat.js b/src/Chat.js index a3686b9..61ac7c6 100644 --- a/src/Chat.js +++ b/src/Chat.js @@ -16,7 +16,8 @@ class Chat extends Component { poruka: "", messages: [], receiverStore: null, - receiverAdress: null, + SENDER_ADDRESS: null, + message: "" } @@ -37,14 +38,14 @@ class Chat extends Component { this.setState({ currentUser }) console.log('Bleep bloop 🤖 You are connected to Chatkit') return currentUser.subscribeToRoom({ - roomId: 19377131, // Replace with YOUR ROOM ID + roomId: 19377773, // Replace with YOUR ROOM ID messageLimit: 100, hooks: { onNewMessage: message => { - //dekodiranje + if (message != "") { console.log(message) - this.onDeCryptingMessage(message) + this.onDeCryptingMessage2(this.state.poruka); } }, @@ -64,7 +65,7 @@ class Chat extends Component { onDeCryptingMessage = message => { - console.log("DEKRIPCIJA"); + console.log("DEKRIPCIJA, poruka primljena:", message); fetch('http://localhost:3001/decryptMessage2', { method: 'POST', headers: { @@ -98,6 +99,106 @@ class Chat extends Component { }) } + + onSend = text => { + //ubaceno + console.log("Primljena poruka:",text); + fetch('http://localhost:3001/cryptMessage', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + data: { + senderAdress: "xxxxxxxxx", + receiverAdress: "yyyyyyyyyyyyy", + receiverPreKeyId: 1337, + receiverSignedKeyId: 1, + message: text + } + }) + }) + .then(response => { console.log("REPSONSE:", response); return response.json(); }) + .then(data => { + this.state.poruka = data.data.message.data.toString(); + this.setState({ + receiverStore: data.data.receiverStore, + SENDER_ADDRESS: data.data.SENDER_ADDRESS + }) + + }) + .catch(error => { + console.error('error', error) + }).catch(error => { + console.error('error', error) + }) + + + //kraj + + this.state.currentUser.sendMessage({ + text, + roomId: this.state.currentRoom.id + }) + } + + + onDeCryptingMessage2 = message => { + + console.log("DEKRIPCIJA, poruka primljena:", message); + fetch('http://localhost:3001/decryptMessage', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + data: { + receiverStore: this.state.receiverStore, + SENDER_ADDRESS: this.state.SENDER_ADDRESS, + message: message, + senderAdress: "xxxxxxxxx" + } + }) + }) + .then(response => { console.log("REPSONSE:", response); return response.json(); }) + .then(data => { + console.log("DATA:", data.data) + this.setState({ + message: data.data.message + }) + this.setState({ + messages: [...this.state.messages, this.state.message] + }) + }) + .catch(error => { + console.error('error', error) + }).catch(error => { + console.error('error', error) + }) + } + + render() { + return ( +
+
+ +
+
+ + +
+
+ ) + } +} + +export default Chat + + +/* onCryptingMessage = message => { fetch('http://localhost:3001/cryptMessage', { method: 'POST', @@ -133,70 +234,4 @@ class Chat extends Component { }) } - - - onSend = text => { - //ubaceno - fetch('http://localhost:3001/cryptMessage', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - data: { - senderAdress: "xxxxxxxxx", - receiverAdress: "yyyyyyyyyyyyy", - receiverPreKeyId: 1337, - receiverSignedKeyId: 1, - message: text - } - }) - }) - .then(response => { console.log("REPSONSE:", response); return response.json(); }) - .then(data => { - - this.state.poruka = data.data.message.data.toString(); - this.setState({ - receiverStore: data.data.receiverStore, - senderAdress: data.data.senderAdress - }) - - this.state.currentUser.sendMessage({ - text: this.state.poruka , - roomId: this.state.currentRoom.id - }) - return data.data.message - }) - .catch(error => { - console.error('error', error) - }).catch(error => { - console.error('error', error) - }) - - - //kraj - - /*this.state.currentUser.sendMessage({ - text, - roomId: this.state.currentRoom.id - })*/ - } - render() { - return ( -
-
- -
-
- - -
-
- ) - } -} - -export default Chat \ No newline at end of file +*/ \ No newline at end of file diff --git a/src/MessageList.js b/src/MessageList.js index 775133c..2de40cc 100644 --- a/src/MessageList.js +++ b/src/MessageList.js @@ -23,6 +23,23 @@ class MessageList extends Component { } renderItem(message) { + return ( + + + {"Posiljalac:"}: + + + {message} + + + ) + } +} + +export default MessageList + +/* +renderItem(message) { return ( @@ -34,6 +51,5 @@ class MessageList extends Component { ) } -} -export default MessageList \ No newline at end of file +*/ \ No newline at end of file