we can't do it with cookies holding the private keys, since many people block cookie usage & cookies are readable by anyone. we'd need to have local storage that the server authenticates for, otherwise any server would be able to read out the private key from storage via js & send it to the attacking server…
cursed cryptography
@niplav You could probably use homomorphic cryptography to encode the private key and send the key used for that encoding to the server. When you need to decode something the server would send you an encoded program for decoding the message. This normally ends up with the "decoded" message still encoded with the server key, but I'm pretty sure this could be worked around.
I'm much more sure there is a better and simpler method for doing this tho. If you are interested in this kind of stuff I would suggest checking out Matrix docs.