update unlock page

This commit is contained in:
Ren Amamiya
2023-07-09 17:38:54 +07:00
parent a099f4fc85
commit 5787eff7d5
6 changed files with 353 additions and 267 deletions

View File

@@ -1,4 +1,3 @@
import { getPublicKey } from 'nostr-tools';
import { useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
@@ -30,10 +29,13 @@ const resolver: Resolver<FormValues> = async (values) => {
export function UnlockScreen() {
const navigate = useNavigate();
const setPassword = useStronghold((state) => state.setPassword);
const [passwordInput, setPasswordInput] = useState('password');
const [loading, setLoading] = useState(false);
const [setPrivkey, setPassword] = useStronghold((state) => [
state.setPrivkey,
state.setPassword,
]);
const { account } = useAccount();
const { load } = useSecureStorage();
@@ -61,22 +63,12 @@ export function UnlockScreen() {
setPassword(data.password);
// load private in secure storage
const privkey = await load(account.pubkey, data.password);
if (!privkey) {
setLoading(false);
setError('password', {
type: 'custom',
message: "Can't get private key",
});
}
const tempPubkey = getPublicKey(privkey);
if (tempPubkey === account.pubkey) {
// redirect to next step
try {
const privkey = await load(account.pubkey, data.password);
setPrivkey(privkey);
// redirect to home
navigate('/', { replace: true });
} else {
} catch {
setLoading(false);
setError('password', {
type: 'custom',

View File

@@ -2,12 +2,18 @@ import { create } from 'zustand';
interface StrongholdState {
password: null | string;
privkey: null | string;
setPassword: (password: string) => void;
setPrivkey: (privkey: string) => void;
}
export const useStronghold = create<StrongholdState>((set) => ({
password: null,
privkey: null,
setPassword: (password: string) => {
set({ password: password });
},
setPrivkey: (privkey: string) => {
set({ privkey: privkey });
},
}));

View File

@@ -2,6 +2,8 @@ import { NDKEvent, NDKKind, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';
import { useNDK } from '@libs/ndk/provider';
import { useStronghold } from '@stores/stronghold';
import { useAccount } from '@utils/hooks/useAccount';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
@@ -10,6 +12,8 @@ export function usePublish() {
const { account } = useAccount();
const { load } = useSecureStorage();
const privkey = useStronghold((state) => state.privkey);
const publish = async ({
content,
kind,
@@ -19,10 +23,10 @@ export function usePublish() {
kind: NDKKind;
tags: string[][];
}): Promise<NDKEvent> => {
const privkey = await load(account.pubkey);
const securePrivkey = await load(account.pubkey);
const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(privkey);
const signer = new NDKPrivateKeySigner(privkey ? privkey : securePrivkey);
event.content = content;
event.kind = kind;