import { Box, Flex, PasswordInput, Text, Transition } from "@mantine/core";
import { useEffect, useState } from "react";
import Lottie from "lottie-react";
import animationData from './lottie.json'
import { useMemory } from "@/app/providers/MemoryProvider/useMemory";
import { mnemonicToSeed } from "web-bip39";
import { useNavigate } from "react-router-dom";
import { modals } from "@mantine/modals";
import { Buffer } from 'buffer'
import { encodeWithPassword, generateHashFromPrivateKey, generateKeyPairFromSeed } from "@/app/crypto/crypto";
import { useAccountProvider } from "@/app/providers/AccountProvider/useAccountProvider";
import { Account } from "@/app/providers/AccountProvider/AccountProvider";
import { useRosettaColors } from "@/app/hooks/useRosettaColors";
import { AnimatedButton } from "@/app/components/AnimatedButton/AnimatedButton";
import { IconChevronRight } from "@tabler/icons-react";
import { SIZE_LOGIN_WIDTH_PX } from "@/app/constants";
export function SetPassword() {
const navigate = useNavigate();
const [password, setPassword] = useState("");
const [confirm, setConfirm] = useState("");
const [phrase, _] = useMemory("seed-phrase", "");
const [mounted, setMounted] = useState(false);
const colors = useRosettaColors();
const { createAccount, loginAccount, selectAccountToLoginDice } = useAccountProvider();
const openInsecurePasswordModal = () => {
modals.openConfirmModal({
title: 'Insecure password',
centered: true,
children: (
Your password is insecure,
such passwords are easy to guess, come up with a new one, or, which is not recommended, leave this one
),
withCloseButton: false,
labels: { confirm: 'Continue', cancel: "I'll come up with a new one" },
confirmProps: { color: 'red' },
onConfirm: doneSetup
});
}
useEffect(() => {
if (phrase.trim() == "") {
navigate("/");
}
setTimeout(() => setMounted(true), 100);
}, []);
const doneSetup = async () => {
let seed = await mnemonicToSeed(phrase);
let hex = Buffer.from(seed).toString('hex');
let keypair = await generateKeyPairFromSeed(hex);
const encrypted = await encodeWithPassword(password, keypair.privateKey);
const privateKeyHash = await generateHashFromPrivateKey(keypair.privateKey);
const account: Account = {
publicKey: keypair.publicKey,
privateKey: encrypted,
privatePlain: keypair.privateKey,
privateHash: privateKeyHash,
seedPhraseEncrypted: await encodeWithPassword(password, phrase)
}
createAccount(account);
loginAccount(account);
selectAccountToLoginDice(account);
navigate("/main");
}
const onDone = async () => {
if (!password.match(/[A-Z]+/) || !password.match(/[0-9]+/) || !password.match(/[$@#&!]+/)) {
openInsecurePasswordModal();
return;
}
doneSetup();
}
return (
Protect account
Create a password to protect your account
{(styles) => (
setPassword(e.target.value)} size="md">
setConfirm(e.target.value)} size="md">
} animated={['#2DA5FF', '#87DBFF']} fullWidth onClick={onDone} size={'md'}>
Start
)}
{(styles) => (
Your password never leaves your device and is never stored anywhere.
)}
)
}