wip: use new storage layer

This commit is contained in:
Ren Amamiya
2023-08-15 08:29:04 +07:00
parent adca37223c
commit 6e28bcdb96
19 changed files with 182 additions and 104 deletions

View File

@@ -4,7 +4,7 @@ import { generatePrivateKey, getPublicKey, nip19 } from 'nostr-tools';
import { useEffect, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { createAccount } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { Button } from '@shared/button';
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
@@ -14,6 +14,8 @@ import { useOnboarding } from '@stores/onboarding';
import { useStronghold } from '@stores/stronghold';
export function CreateStep1Screen() {
const { db } = useStorage();
const queryClient = useQueryClient();
const navigate = useNavigate();
const setPrivkey = useStronghold((state) => state.setPrivkey);
@@ -53,10 +55,10 @@ export function CreateStep1Screen() {
follows: null | string[][];
is_active: number;
}) => {
return createAccount(data.npub, data.pubkey, null, 1);
return db.createAccount(data.npub, data.pubkey);
},
onSuccess: (data) => {
queryClient.setQueryData(['currentAccount'], data);
queryClient.setQueryData(['account'], data);
},
});
@@ -75,7 +77,7 @@ export function CreateStep1Screen() {
});
// redirect to next step
setTimeout(() => navigate('/auth/create/step-2', { replace: true }), 1200);
navigate('/auth/create/step-2', { replace: true });
};
useEffect(() => {

View File

@@ -1,15 +1,17 @@
import { appConfigDir } from '@tauri-apps/api/path';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
import { useEffect, useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { useStorage } from '@libs/storage/provider';
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
import { useOnboarding } from '@stores/onboarding';
import { useStronghold } from '@stores/stronghold';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
type FormValues = {
password: string;
};
@@ -37,7 +39,7 @@ export function CreateStep2Screen() {
const [passwordInput, setPasswordInput] = useState('password');
const [loading, setLoading] = useState(false);
const { save } = useSecureStorage();
const { db } = useStorage();
// toggle private key
const showPassword = () => {
@@ -58,8 +60,13 @@ export function CreateStep2Screen() {
const onSubmit = async (data: { [x: string]: string }) => {
setLoading(true);
if (data.password.length > 3) {
const dir = await appConfigDir();
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password);
db.secureDB = stronghold;
// save privkey to secure storage
await save(pubkey, privkey, data.password);
await db.secureSave(pubkey, privkey);
// redirect to next step
navigate('/auth/create/step-3', { replace: true });

View File

@@ -47,10 +47,10 @@ export function CreateStep3Screen() {
tags: [],
});
queryClient.invalidateQueries(['currentAccount']);
queryClient.invalidateQueries(['account']);
if (event) {
setTimeout(() => navigate('/auth/onboarding', { replace: true }), 1000);
navigate('/auth/onboarding', { replace: true });
}
} catch (e) {
console.log('error: ', e);

View File

@@ -4,7 +4,7 @@ import { useEffect, useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { createAccount } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { LoaderIcon } from '@shared/icons';
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
@@ -31,6 +31,8 @@ const resolver: Resolver<FormValues> = async (values) => {
};
export function ImportStep1Screen() {
const { db } = useStorage();
const queryClient = useQueryClient();
const navigate = useNavigate();
const setPrivkey = useStronghold((state) => state.setPrivkey);
@@ -47,10 +49,10 @@ export function ImportStep1Screen() {
follows: null | string[];
is_active: number | boolean;
}) => {
return createAccount(data.npub, data.pubkey, null, 1);
return db.createAccount(data.npub, data.pubkey);
},
onSuccess: (data) => {
queryClient.setQueryData(['currentAccount'], data);
queryClient.setQueryData(['account'], data);
},
});
@@ -87,7 +89,7 @@ export function ImportStep1Screen() {
});
// redirect to step 2
setTimeout(() => navigate('/auth/import/step-2', { replace: true }), 1200);
navigate('/auth/import/step-2', { replace: true });
}
} catch (error) {
setError('privkey', {

View File

@@ -1,15 +1,17 @@
import { appConfigDir } from '@tauri-apps/api/path';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
import { useEffect, useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { useStorage } from '@libs/storage/provider';
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
import { useOnboarding } from '@stores/onboarding';
import { useStronghold } from '@stores/stronghold';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
type FormValues = {
password: string;
};
@@ -37,7 +39,7 @@ export function ImportStep2Screen() {
const [passwordInput, setPasswordInput] = useState('password');
const [loading, setLoading] = useState(false);
const { save } = useSecureStorage();
const { db } = useStorage();
// toggle private key
const showPassword = () => {
@@ -58,8 +60,13 @@ export function ImportStep2Screen() {
const onSubmit = async (data: { [x: string]: string }) => {
setLoading(true);
if (data.password.length > 3) {
const dir = await appConfigDir();
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password);
db.secureDB = stronghold;
// save privkey to secure storage
await save(pubkey, privkey, data.password);
await db.secureSave(pubkey, privkey);
// redirect to next step
navigate('/auth/import/step-3', { replace: true });

View File

@@ -4,7 +4,7 @@ import { useNavigate } from 'react-router-dom';
import { User } from '@app/auth/components/user';
import { updateLastLogin } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { ArrowRightCircleIcon, LoaderIcon } from '@shared/icons';
@@ -20,22 +20,27 @@ export function ImportStep3Screen() {
const [loading, setLoading] = useState(false);
const { db } = useStorage();
const { status, account } = useAccount();
const { fetchNotes, fetchChats } = useNostr();
const { fetchUserData } = useNostr();
const submit = async () => {
try {
// show loading indicator
setLoading(true);
const now = Math.floor(Date.now() / 1000);
await fetchNotes();
await fetchChats();
await updateLastLogin(now);
const data = await fetchUserData();
queryClient.invalidateQueries(['currentAccount']);
if (data.status === 'ok') {
// update last login
await db.updateLastLogin(Math.floor(Date.now() / 1000));
navigate('/auth/onboarding/step-2', { replace: true });
queryClient.invalidateQueries(['account']);
navigate('/auth/onboarding/step-2', { replace: true });
} else {
console.log('error: ', data.message);
setLoading(false);
}
} catch (e) {
console.log('error: ', e);
setLoading(false);

View File

@@ -1,16 +1,18 @@
import { useQueryClient } from '@tanstack/react-query';
import { appConfigDir } from '@tauri-apps/api/path';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
import { useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { removePrivkey } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
import { useStronghold } from '@stores/stronghold';
import { useAccount } from '@utils/hooks/useAccount';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
type FormValues = {
password: string;
@@ -39,7 +41,7 @@ export function MigrateScreen() {
const [loading, setLoading] = useState(false);
const { account } = useAccount();
const { save } = useSecureStorage();
const { db } = useStorage();
// toggle private key
const showPassword = () => {
@@ -63,13 +65,18 @@ export function MigrateScreen() {
// load private in secure storage
try {
// save privkey to secure storage
await save(account.pubkey, account.privkey, data.password);
const dir = await appConfigDir();
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password);
if (!db.secureDB) db.secureDB = stronghold;
await db.secureSave(account.pubkey, account.privkey);
// add privkey to state
setPrivkey(account.privkey);
// remove privkey in db
await removePrivkey();
// clear cache
await queryClient.invalidateQueries(['currentAccount']);
await queryClient.invalidateQueries(['account']);
// redirect to home
navigate('/', { replace: true });
} catch {

View File

@@ -4,7 +4,7 @@ import { Link, useNavigate } from 'react-router-dom';
import { User } from '@app/auth/components/user';
import { updateAccount } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
@@ -19,7 +19,8 @@ export function OnboardStep1Screen() {
const navigate = useNavigate();
const setStep = useOnboarding((state) => state.setStep);
const { publish, fetchNotes } = useNostr();
const { db } = useStorage();
const { publish, fetchUserData } = useNostr();
const { account } = useAccount();
const { status, data } = useQuery(['trending-profiles'], async () => {
const res = await fetch('https://api.nostr.band/v0/trending/profiles');
@@ -46,20 +47,22 @@ export function OnboardStep1Screen() {
const tags = arrayToNIP02([...follows, account.pubkey]);
const event = await publish({ content: '', kind: 3, tags: tags });
await updateAccount('follows', follows);
await db.updateAccount('follows', follows);
// prefetch notes with current follows
const notes = await fetchNotes(follows);
const data = await fetchUserData(follows);
// redirect to next step
if (event && notes) {
setTimeout(() => {
queryClient.invalidateQueries(['currentAccount']);
navigate('/auth/onboarding/step-2', { replace: true });
}, 1000);
if (event && data.status === 'ok') {
queryClient.invalidateQueries(['account']);
navigate('/auth/onboarding/step-2', { replace: true });
} else {
setLoading(false);
console.log('error: ', data.message);
}
} catch {
console.log('error');
} catch (e) {
setLoading(false);
console.log('error: ', e);
}
};

View File

@@ -1,7 +1,7 @@
import { useEffect, useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { createWidget } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
@@ -33,6 +33,8 @@ export function OnboardStep2Screen() {
const [loading, setLoading] = useState(false);
const [tags, setTags] = useState(new Set<string>());
const { db } = useStorage();
const toggleTag = (tag: string) => {
if (tags.has(tag)) {
setTags((prev) => {
@@ -50,10 +52,10 @@ export function OnboardStep2Screen() {
setLoading(true);
for (const tag of tags) {
await createWidget(BLOCK_KINDS.hashtag, tag, tag.replace('#', ''));
await db.createWidget(BLOCK_KINDS.hashtag, tag, tag.replace('#', ''));
}
setTimeout(() => navigate('/auth/onboarding/step-3', { replace: true }), 1000);
navigate('/auth/onboarding/step-3', { replace: true });
} catch {
console.log('error');
}

View File

@@ -5,7 +5,7 @@ import { useNavigate } from 'react-router-dom';
import { UserRelay } from '@app/auth/components/userRelay';
import { useNDK } from '@libs/ndk/provider';
import { createRelay } from '@libs/storage';
import { useStorage } from '@libs/storage/provider';
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
@@ -22,6 +22,7 @@ export function OnboardStep3Screen() {
const [loading, setLoading] = useState(false);
const [relays, setRelays] = useState(new Set<string>());
const { db } = useStorage();
const { publish } = useNostr();
const { account } = useAccount();
const { ndk } = useNDK();
@@ -62,21 +63,19 @@ export function OnboardStep3Screen() {
try {
if (!skip) {
for (const relay of relays) {
await createRelay(relay);
await db.createRelay(relay);
}
const tags = Array.from(relays).map((relay) => ['r', relay.replace(/\/+$/, '')]);
await publish({ content: '', kind: 10002, tags: tags });
} else {
for (const relay of FULL_RELAYS) {
await createRelay(relay);
await db.createRelay(relay);
}
}
setTimeout(() => {
clearStep();
navigate('/', { replace: true });
}, 1000);
clearStep();
navigate('/', { replace: true });
} catch (e) {
setLoading(false);
console.log('error: ', e);

View File

@@ -1,14 +1,17 @@
import { appConfigDir } from '@tauri-apps/api/path';
import { Stronghold } from '@tauri-apps/plugin-stronghold';
import { getPublicKey, nip19 } from 'nostr-tools';
import { useState } from 'react';
import { Resolver, useForm } from 'react-hook-form';
import { useNavigate } from 'react-router-dom';
import { useStorage } from '@libs/storage/provider';
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
import { useStronghold } from '@stores/stronghold';
import { useAccount } from '@utils/hooks/useAccount';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
type FormValues = {
password: string;
@@ -37,7 +40,7 @@ export function ResetScreen() {
const [loading, setLoading] = useState(false);
const { account } = useAccount();
const { save, reset } = useSecureStorage();
const { db } = useStorage();
// toggle private key
const showPassword = () => {
@@ -75,9 +78,18 @@ export function ResetScreen() {
});
} else {
// remove old stronghold
await reset();
await db.secureReset();
// save privkey to secure storage
await save(account.pubkey, account.privkey, data.password);
const dir = await appConfigDir();
const stronghold = await Stronghold.load(
`${dir}/lume.stronghold`,
data.password
);
if (!db.secureDB) db.secureDB = stronghold;
await db.secureSave(account.pubkey, account.privkey);
// add privkey to state
setPrivkey(account.privkey);
// redirect to home

View File

@@ -54,7 +54,7 @@ export function UnlockScreen() {
const dir = await appConfigDir();
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password);
db.secureDB = stronghold;
if (!db.secureDB) db.secureDB = stronghold;
const privkey = await db.secureLoad(account.pubkey);