init ratchets

This commit is contained in:
Sam Hadow 2025-03-09 17:43:20 +01:00
parent 35ff9cb240
commit 744e1fae29
3 changed files with 55 additions and 17 deletions

View File

@ -139,15 +139,23 @@ const database = {
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
try {
const roomsRes = await pool.query(
'SELECT m.room_uuid FROM room_members m, users u WHERE m.user_uuid = u.uuid AND u.pubkey = $1',
`SELECT m.room_uuid, u.pubkey
FROM room_members m, users u
WHERE m.user_uuid = u.uuid
AND m.room_uuid IN (
SELECT room_uuid
FROM room_members
WHERE user_uuid != (SELECT uuid FROM users WHERE pubkey = $1)
) AND u.pubkey != $1`,
[pubkey]
);
return roomsRes.rows.map(row => row.room_uuid);
return roomsRes.rows.map(row => [row.room_uuid, row.pubkey]);
} catch (err) {
console.error('Error retrieving rooms:', err);
throw err;
}
},
},
getRoomMembers: async (roomid) => {
await retryWithDelay(database.checkIfDatabaseIsUp, 5);
try {

View File

@ -5,6 +5,31 @@ import { keccakKDF } from "./kdf.js";
const socket = io();
let secret = null;
let sharedsecret = {};
let dh_ratchets = {};
let sending_ratchets = {};
let receiving_ratchets = {};
function init_ratchets(order, user_pubkey) {
let dh_ratchet = new keccakKDF()
let key_1 = dh_ratchet.init(sharedsecret[user_pubkey], new Uint8Array(0));
let key_2 = dh_ratchet.next(new Uint8Array(0));
dh_ratchets[user_pubkey] = dh_ratchet;
let sending_ratchet = new keccakKDF();
let receiving_ratchet = new keccakKDF();
switch (order) {
case 0:
sending_ratchet.init(key_1, new Uint8Array(0));
receiving_ratchet.init(key_2, new Uint8Array(0));
break;
case 1:
sending_ratchet.init(key_2, new Uint8Array(0));
receiving_ratchet.init(key_1, new Uint8Array(0));
break;
}
sending_ratchets[user_pubkey] = sending_ratchet;
receiving_ratchets[user_pubkey] = receiving_ratchet;
}
socket.on('chat message', (msg, room) => {
console.log(`received: ${msg}`);
@ -30,10 +55,12 @@ socket.on('key exchange', (user_pubkey, pubkey, part) => {
sharedsecret[user_pubkey] = sharedKey(secret, fromHexString(pubkey));
socket.emit('key exchange', user_pubkey, toHexString(keys.pubkey), 2);
console.log(`shared secret: ${toHexString(sharedsecret[user_pubkey])}`);
init_ratchets(0, user_pubkey)
break;
case 2:
sharedsecret[user_pubkey] = sharedKey(secret, fromHexString(pubkey));
console.log(`shared secret: ${toHexString(sharedsecret[user_pubkey])}`);
init_ratchets(1, user_pubkey)
break;
}
});

View File

@ -3,25 +3,28 @@ import { create_listener } from "./chat.js";
render_rooms_wrapper()
async function render_rooms_wrapper() {
const room_ids = await request_rooms();
console.log(room_ids);
render_rooms(room_ids);
const room_info = await request_rooms();
console.log(room_info);
render_rooms(room_info);
}
function render_rooms(room_id) {
function render_rooms(room_info) {
const roomsContainer = document.getElementById('rooms');
roomsContainer.innerHTML= '';
room_id.forEach((roomId) => {
room_info.forEach(([roomId, pubkey]) => {
const roomDiv = document.createElement('div');
roomDiv.id = `room-${roomId}`;
roomDiv.classList.add(`key-${pubkey}`);
const messagesUl = document.createElement('ul');
messagesUl.id = `messages-${roomId}`;
const form = document.createElement('form');
form.id = `form-${roomId}`;
form.classList.add(`key-${pubkey}`);
form.action = '';
const input = document.createElement('input');
input.id = `input-${roomId}`;
input.classList.add(`key-${pubkey}`);
input.autocomplete = 'off';
const button = document.createElement('button');
button.textContent = 'Send';