2023-11-05 01:16:45 +01:00
|
|
|
import { useState } from "react";
|
|
|
|
import { useAsyncFn } from "react-use";
|
|
|
|
|
|
|
|
import { Button } from "@/components/Button";
|
|
|
|
import { Input } from "@/components/player/internals/ContextMenu/Input";
|
2023-11-05 17:56:56 +01:00
|
|
|
import { useAuth } from "@/hooks/auth/useAuth";
|
2023-11-05 01:16:45 +01:00
|
|
|
import { AccountProfile } from "@/pages/parts/auth/AccountCreatePart";
|
|
|
|
|
|
|
|
interface VerifyPassphraseProps {
|
|
|
|
mnemonic: string | null;
|
2023-11-05 17:56:56 +01:00
|
|
|
userData: AccountProfile | null;
|
2023-11-05 01:16:45 +01:00
|
|
|
onNext?: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function VerifyPassphrase(props: VerifyPassphraseProps) {
|
|
|
|
const [mnemonic, setMnemonic] = useState("");
|
2023-11-05 17:56:56 +01:00
|
|
|
const { register, restore } = useAuth();
|
2023-11-05 01:16:45 +01:00
|
|
|
|
|
|
|
const [result, execute] = useAsyncFn(
|
|
|
|
async (inputMnemonic: string) => {
|
2023-11-05 17:56:56 +01:00
|
|
|
if (!props.mnemonic || !props.userData)
|
2023-11-05 01:16:45 +01:00
|
|
|
throw new Error("invalid input data");
|
|
|
|
if (inputMnemonic !== props.mnemonic)
|
|
|
|
throw new Error("Passphrase doesn't match");
|
|
|
|
|
|
|
|
// TODO captcha?
|
|
|
|
|
2023-11-05 17:56:56 +01:00
|
|
|
await register({
|
|
|
|
mnemonic: inputMnemonic,
|
|
|
|
userData: props.userData,
|
2023-11-05 01:16:45 +01:00
|
|
|
});
|
|
|
|
|
2023-11-05 17:56:56 +01:00
|
|
|
// TODO import (and sort out conflicts)
|
|
|
|
|
|
|
|
await restore();
|
|
|
|
|
2023-11-05 01:16:45 +01:00
|
|
|
props.onNext?.();
|
|
|
|
},
|
2023-11-05 17:56:56 +01:00
|
|
|
[props, register, restore]
|
2023-11-05 01:16:45 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>verify passphrase</p>
|
|
|
|
<Input value={mnemonic} onInput={setMnemonic} />
|
|
|
|
{result.loading ? <p>Loading...</p> : null}
|
|
|
|
{result.error ? <p>error: {result.error.toString()}</p> : null}
|
|
|
|
<Button onClick={() => execute(mnemonic)}>Register</Button>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|