(feat): crypto migrations
This commit is contained in:
parent
930663d495
commit
cd2c2f8167
17 changed files with 839 additions and 825 deletions
|
|
@ -15,7 +15,7 @@ import { useChat } from "../context";
|
|||
import { useMTP } from "@tensamin/mtp";
|
||||
import { log, toast } from "@tensamin/shared/log";
|
||||
import { cn, useIsMobile } from "@tensamin/ui";
|
||||
import { encryptText } from "@tensamin/crypto/worker";
|
||||
|
||||
import { useSession } from "@tensamin/storage/session";
|
||||
import GifPicker from "./gifPicker";
|
||||
|
||||
|
|
@ -28,8 +28,8 @@ export default function InputComponent({
|
|||
}) {
|
||||
const [invertEnterBehavior, setInvertEnterBehavior] = React.useState(false);
|
||||
|
||||
const { send } = useMTP();
|
||||
const { addLiveMessage, sharedSecret, userId, inputBoxRef } = useChat();
|
||||
const { sendEncrypted } = useMTP();
|
||||
const { addLiveMessage, userId, inputBoxRef } = useChat();
|
||||
const { load, save } = useStorage();
|
||||
const { moveUserIdToTop } = useSession();
|
||||
const gifPopoverRef = React.useRef<HTMLDivElement>(null);
|
||||
|
|
@ -64,11 +64,6 @@ export default function InputComponent({
|
|||
return;
|
||||
}
|
||||
|
||||
if (!sharedSecret) {
|
||||
toast("error", "Still getting shared secret...");
|
||||
return;
|
||||
}
|
||||
|
||||
log(3, "chat", "purple", "Message send init, adding live message ...");
|
||||
|
||||
const reference = addLiveMessage({
|
||||
|
|
@ -79,36 +74,38 @@ export default function InputComponent({
|
|||
MessageState: "awaiting",
|
||||
});
|
||||
|
||||
log(3, "chat", "purple", "Live message added, encrypting...");
|
||||
log(3, "chat", "purple", "Live message added, sending encrypted frame...");
|
||||
|
||||
const encryptedContext = await encryptText(
|
||||
sharedSecret,
|
||||
currentValue,
|
||||
).catch((err) => {
|
||||
toast("error", "Failed to encrypt message", String(err));
|
||||
reference.setFailed(true);
|
||||
});
|
||||
|
||||
if (!encryptedContext) return;
|
||||
|
||||
log(3, "chat", "purple", "Content encrypted, sending message...");
|
||||
|
||||
send("MessageSend", {
|
||||
Content: encryptedContext,
|
||||
ReceiverId: userId,
|
||||
SendTime: time,
|
||||
}).catch((e) => {
|
||||
log(0, "Chat", "red", "Failed to send message", e, {
|
||||
content: currentValue,
|
||||
encryptedContext,
|
||||
ReceiverId: userId,
|
||||
SendTime: time,
|
||||
void load("user_id")
|
||||
.then((ownUserId) =>
|
||||
sendEncrypted(
|
||||
"MessageSend",
|
||||
{
|
||||
Content: currentValue,
|
||||
ReceiverId: userId,
|
||||
SendTime: time,
|
||||
},
|
||||
{
|
||||
senderUserId: String(ownUserId),
|
||||
recipientUserId: String(userId),
|
||||
},
|
||||
),
|
||||
)
|
||||
.catch((e) => {
|
||||
log(0, "Chat", "red", "Failed to send encrypted message", e, {
|
||||
ReceiverId: userId,
|
||||
SendTime: time,
|
||||
});
|
||||
reference.setFailed(true);
|
||||
toast(
|
||||
"error",
|
||||
e instanceof Error && e.message.includes("public key")
|
||||
? "Recipient has no encryption public key available"
|
||||
: "Failed to send encrypted message",
|
||||
);
|
||||
});
|
||||
reference.setFailed(true);
|
||||
toast("error", "Failed to send message");
|
||||
});
|
||||
|
||||
log(3, "chat", "purple", "Message sent");
|
||||
log(3, "chat", "purple", "Encrypted message send queued");
|
||||
|
||||
moveUserIdToTop(userId);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue