mirror of
https://github.com/luminous-devs/lume.git
synced 2024-09-19 19:46:34 +00:00
wip: use new storage layer
This commit is contained in:
parent
adca37223c
commit
6e28bcdb96
@ -4,7 +4,7 @@ import { generatePrivateKey, getPublicKey, nip19 } from 'nostr-tools';
|
|||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import { createAccount } from '@libs/storage';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { Button } from '@shared/button';
|
import { Button } from '@shared/button';
|
||||||
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
||||||
@ -14,6 +14,8 @@ import { useOnboarding } from '@stores/onboarding';
|
|||||||
import { useStronghold } from '@stores/stronghold';
|
import { useStronghold } from '@stores/stronghold';
|
||||||
|
|
||||||
export function CreateStep1Screen() {
|
export function CreateStep1Screen() {
|
||||||
|
const { db } = useStorage();
|
||||||
|
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const setPrivkey = useStronghold((state) => state.setPrivkey);
|
const setPrivkey = useStronghold((state) => state.setPrivkey);
|
||||||
@ -53,10 +55,10 @@ export function CreateStep1Screen() {
|
|||||||
follows: null | string[][];
|
follows: null | string[][];
|
||||||
is_active: number;
|
is_active: number;
|
||||||
}) => {
|
}) => {
|
||||||
return createAccount(data.npub, data.pubkey, null, 1);
|
return db.createAccount(data.npub, data.pubkey);
|
||||||
},
|
},
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
queryClient.setQueryData(['currentAccount'], data);
|
queryClient.setQueryData(['account'], data);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -75,7 +77,7 @@ export function CreateStep1Screen() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// redirect to next step
|
// redirect to next step
|
||||||
setTimeout(() => navigate('/auth/create/step-2', { replace: true }), 1200);
|
navigate('/auth/create/step-2', { replace: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
|
import { appConfigDir } from '@tauri-apps/api/path';
|
||||||
|
import { Stronghold } from '@tauri-apps/plugin-stronghold';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Resolver, useForm } from 'react-hook-form';
|
import { Resolver, useForm } from 'react-hook-form';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
||||||
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
|
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
|
||||||
|
|
||||||
import { useOnboarding } from '@stores/onboarding';
|
import { useOnboarding } from '@stores/onboarding';
|
||||||
import { useStronghold } from '@stores/stronghold';
|
import { useStronghold } from '@stores/stronghold';
|
||||||
|
|
||||||
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
|
|
||||||
|
|
||||||
type FormValues = {
|
type FormValues = {
|
||||||
password: string;
|
password: string;
|
||||||
};
|
};
|
||||||
@ -37,7 +39,7 @@ export function CreateStep2Screen() {
|
|||||||
const [passwordInput, setPasswordInput] = useState('password');
|
const [passwordInput, setPasswordInput] = useState('password');
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const { save } = useSecureStorage();
|
const { db } = useStorage();
|
||||||
|
|
||||||
// toggle private key
|
// toggle private key
|
||||||
const showPassword = () => {
|
const showPassword = () => {
|
||||||
@ -58,8 +60,13 @@ export function CreateStep2Screen() {
|
|||||||
const onSubmit = async (data: { [x: string]: string }) => {
|
const onSubmit = async (data: { [x: string]: string }) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
if (data.password.length > 3) {
|
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
|
// save privkey to secure storage
|
||||||
await save(pubkey, privkey, data.password);
|
await db.secureSave(pubkey, privkey);
|
||||||
|
|
||||||
// redirect to next step
|
// redirect to next step
|
||||||
navigate('/auth/create/step-3', { replace: true });
|
navigate('/auth/create/step-3', { replace: true });
|
||||||
|
@ -47,10 +47,10 @@ export function CreateStep3Screen() {
|
|||||||
tags: [],
|
tags: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
queryClient.invalidateQueries(['currentAccount']);
|
queryClient.invalidateQueries(['account']);
|
||||||
|
|
||||||
if (event) {
|
if (event) {
|
||||||
setTimeout(() => navigate('/auth/onboarding', { replace: true }), 1000);
|
navigate('/auth/onboarding', { replace: true });
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('error: ', e);
|
console.log('error: ', e);
|
||||||
|
@ -4,7 +4,7 @@ import { useEffect, useState } from 'react';
|
|||||||
import { Resolver, useForm } from 'react-hook-form';
|
import { Resolver, useForm } from 'react-hook-form';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import { createAccount } from '@libs/storage';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { LoaderIcon } from '@shared/icons';
|
import { LoaderIcon } from '@shared/icons';
|
||||||
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
|
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
|
||||||
@ -31,6 +31,8 @@ const resolver: Resolver<FormValues> = async (values) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function ImportStep1Screen() {
|
export function ImportStep1Screen() {
|
||||||
|
const { db } = useStorage();
|
||||||
|
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const setPrivkey = useStronghold((state) => state.setPrivkey);
|
const setPrivkey = useStronghold((state) => state.setPrivkey);
|
||||||
@ -47,10 +49,10 @@ export function ImportStep1Screen() {
|
|||||||
follows: null | string[];
|
follows: null | string[];
|
||||||
is_active: number | boolean;
|
is_active: number | boolean;
|
||||||
}) => {
|
}) => {
|
||||||
return createAccount(data.npub, data.pubkey, null, 1);
|
return db.createAccount(data.npub, data.pubkey);
|
||||||
},
|
},
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
queryClient.setQueryData(['currentAccount'], data);
|
queryClient.setQueryData(['account'], data);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -87,7 +89,7 @@ export function ImportStep1Screen() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// redirect to step 2
|
// redirect to step 2
|
||||||
setTimeout(() => navigate('/auth/import/step-2', { replace: true }), 1200);
|
navigate('/auth/import/step-2', { replace: true });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError('privkey', {
|
setError('privkey', {
|
||||||
|
@ -1,15 +1,17 @@
|
|||||||
|
import { appConfigDir } from '@tauri-apps/api/path';
|
||||||
|
import { Stronghold } from '@tauri-apps/plugin-stronghold';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Resolver, useForm } from 'react-hook-form';
|
import { Resolver, useForm } from 'react-hook-form';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
||||||
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
|
import { ArrowRightCircleIcon } from '@shared/icons/arrowRightCircle';
|
||||||
|
|
||||||
import { useOnboarding } from '@stores/onboarding';
|
import { useOnboarding } from '@stores/onboarding';
|
||||||
import { useStronghold } from '@stores/stronghold';
|
import { useStronghold } from '@stores/stronghold';
|
||||||
|
|
||||||
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
|
|
||||||
|
|
||||||
type FormValues = {
|
type FormValues = {
|
||||||
password: string;
|
password: string;
|
||||||
};
|
};
|
||||||
@ -37,7 +39,7 @@ export function ImportStep2Screen() {
|
|||||||
const [passwordInput, setPasswordInput] = useState('password');
|
const [passwordInput, setPasswordInput] = useState('password');
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const { save } = useSecureStorage();
|
const { db } = useStorage();
|
||||||
|
|
||||||
// toggle private key
|
// toggle private key
|
||||||
const showPassword = () => {
|
const showPassword = () => {
|
||||||
@ -58,8 +60,13 @@ export function ImportStep2Screen() {
|
|||||||
const onSubmit = async (data: { [x: string]: string }) => {
|
const onSubmit = async (data: { [x: string]: string }) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
if (data.password.length > 3) {
|
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
|
// save privkey to secure storage
|
||||||
await save(pubkey, privkey, data.password);
|
await db.secureSave(pubkey, privkey);
|
||||||
|
|
||||||
// redirect to next step
|
// redirect to next step
|
||||||
navigate('/auth/import/step-3', { replace: true });
|
navigate('/auth/import/step-3', { replace: true });
|
||||||
|
@ -4,7 +4,7 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
|
|
||||||
import { User } from '@app/auth/components/user';
|
import { User } from '@app/auth/components/user';
|
||||||
|
|
||||||
import { updateLastLogin } from '@libs/storage';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { ArrowRightCircleIcon, LoaderIcon } from '@shared/icons';
|
import { ArrowRightCircleIcon, LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
@ -20,22 +20,27 @@ export function ImportStep3Screen() {
|
|||||||
|
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const { db } = useStorage();
|
||||||
const { status, account } = useAccount();
|
const { status, account } = useAccount();
|
||||||
const { fetchNotes, fetchChats } = useNostr();
|
const { fetchUserData } = useNostr();
|
||||||
|
|
||||||
const submit = async () => {
|
const submit = async () => {
|
||||||
try {
|
try {
|
||||||
// show loading indicator
|
// show loading indicator
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const now = Math.floor(Date.now() / 1000);
|
const data = await fetchUserData();
|
||||||
await fetchNotes();
|
|
||||||
await fetchChats();
|
|
||||||
await updateLastLogin(now);
|
|
||||||
|
|
||||||
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) {
|
} catch (e) {
|
||||||
console.log('error: ', e);
|
console.log('error: ', e);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
import { useQueryClient } from '@tanstack/react-query';
|
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 { useState } from 'react';
|
||||||
import { Resolver, useForm } from 'react-hook-form';
|
import { Resolver, useForm } from 'react-hook-form';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import { removePrivkey } from '@libs/storage';
|
import { removePrivkey } from '@libs/storage';
|
||||||
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
import { useStronghold } from '@stores/stronghold';
|
import { useStronghold } from '@stores/stronghold';
|
||||||
|
|
||||||
import { useAccount } from '@utils/hooks/useAccount';
|
import { useAccount } from '@utils/hooks/useAccount';
|
||||||
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
|
|
||||||
|
|
||||||
type FormValues = {
|
type FormValues = {
|
||||||
password: string;
|
password: string;
|
||||||
@ -39,7 +41,7 @@ export function MigrateScreen() {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const { account } = useAccount();
|
const { account } = useAccount();
|
||||||
const { save } = useSecureStorage();
|
const { db } = useStorage();
|
||||||
|
|
||||||
// toggle private key
|
// toggle private key
|
||||||
const showPassword = () => {
|
const showPassword = () => {
|
||||||
@ -63,13 +65,18 @@ export function MigrateScreen() {
|
|||||||
// load private in secure storage
|
// load private in secure storage
|
||||||
try {
|
try {
|
||||||
// save privkey to secure storage
|
// 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
|
// add privkey to state
|
||||||
setPrivkey(account.privkey);
|
setPrivkey(account.privkey);
|
||||||
// remove privkey in db
|
// remove privkey in db
|
||||||
await removePrivkey();
|
await removePrivkey();
|
||||||
// clear cache
|
// clear cache
|
||||||
await queryClient.invalidateQueries(['currentAccount']);
|
await queryClient.invalidateQueries(['account']);
|
||||||
// redirect to home
|
// redirect to home
|
||||||
navigate('/', { replace: true });
|
navigate('/', { replace: true });
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -4,7 +4,7 @@ import { Link, useNavigate } from 'react-router-dom';
|
|||||||
|
|
||||||
import { User } from '@app/auth/components/user';
|
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';
|
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
@ -19,7 +19,8 @@ export function OnboardStep1Screen() {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const setStep = useOnboarding((state) => state.setStep);
|
const setStep = useOnboarding((state) => state.setStep);
|
||||||
|
|
||||||
const { publish, fetchNotes } = useNostr();
|
const { db } = useStorage();
|
||||||
|
const { publish, fetchUserData } = useNostr();
|
||||||
const { account } = useAccount();
|
const { account } = useAccount();
|
||||||
const { status, data } = useQuery(['trending-profiles'], async () => {
|
const { status, data } = useQuery(['trending-profiles'], async () => {
|
||||||
const res = await fetch('https://api.nostr.band/v0/trending/profiles');
|
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 tags = arrayToNIP02([...follows, account.pubkey]);
|
||||||
const event = await publish({ content: '', kind: 3, tags: tags });
|
const event = await publish({ content: '', kind: 3, tags: tags });
|
||||||
await updateAccount('follows', follows);
|
await db.updateAccount('follows', follows);
|
||||||
|
|
||||||
// prefetch notes with current follows
|
// prefetch notes with current follows
|
||||||
const notes = await fetchNotes(follows);
|
const data = await fetchUserData(follows);
|
||||||
|
|
||||||
// redirect to next step
|
// redirect to next step
|
||||||
if (event && notes) {
|
if (event && data.status === 'ok') {
|
||||||
setTimeout(() => {
|
queryClient.invalidateQueries(['account']);
|
||||||
queryClient.invalidateQueries(['currentAccount']);
|
navigate('/auth/onboarding/step-2', { replace: true });
|
||||||
navigate('/auth/onboarding/step-2', { replace: true });
|
} else {
|
||||||
}, 1000);
|
setLoading(false);
|
||||||
|
console.log('error: ', data.message);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch (e) {
|
||||||
console.log('error');
|
setLoading(false);
|
||||||
|
console.log('error: ', e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Link, useNavigate } from 'react-router-dom';
|
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';
|
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
@ -33,6 +33,8 @@ export function OnboardStep2Screen() {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [tags, setTags] = useState(new Set<string>());
|
const [tags, setTags] = useState(new Set<string>());
|
||||||
|
|
||||||
|
const { db } = useStorage();
|
||||||
|
|
||||||
const toggleTag = (tag: string) => {
|
const toggleTag = (tag: string) => {
|
||||||
if (tags.has(tag)) {
|
if (tags.has(tag)) {
|
||||||
setTags((prev) => {
|
setTags((prev) => {
|
||||||
@ -50,10 +52,10 @@ export function OnboardStep2Screen() {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
for (const tag of tags) {
|
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 {
|
} catch {
|
||||||
console.log('error');
|
console.log('error');
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import { useNavigate } from 'react-router-dom';
|
|||||||
import { UserRelay } from '@app/auth/components/userRelay';
|
import { UserRelay } from '@app/auth/components/userRelay';
|
||||||
|
|
||||||
import { useNDK } from '@libs/ndk/provider';
|
import { useNDK } from '@libs/ndk/provider';
|
||||||
import { createRelay } from '@libs/storage';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
|
import { ArrowRightCircleIcon, CheckCircleIcon, LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
@ -22,6 +22,7 @@ export function OnboardStep3Screen() {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [relays, setRelays] = useState(new Set<string>());
|
const [relays, setRelays] = useState(new Set<string>());
|
||||||
|
|
||||||
|
const { db } = useStorage();
|
||||||
const { publish } = useNostr();
|
const { publish } = useNostr();
|
||||||
const { account } = useAccount();
|
const { account } = useAccount();
|
||||||
const { ndk } = useNDK();
|
const { ndk } = useNDK();
|
||||||
@ -62,21 +63,19 @@ export function OnboardStep3Screen() {
|
|||||||
try {
|
try {
|
||||||
if (!skip) {
|
if (!skip) {
|
||||||
for (const relay of relays) {
|
for (const relay of relays) {
|
||||||
await createRelay(relay);
|
await db.createRelay(relay);
|
||||||
}
|
}
|
||||||
|
|
||||||
const tags = Array.from(relays).map((relay) => ['r', relay.replace(/\/+$/, '')]);
|
const tags = Array.from(relays).map((relay) => ['r', relay.replace(/\/+$/, '')]);
|
||||||
await publish({ content: '', kind: 10002, tags: tags });
|
await publish({ content: '', kind: 10002, tags: tags });
|
||||||
} else {
|
} else {
|
||||||
for (const relay of FULL_RELAYS) {
|
for (const relay of FULL_RELAYS) {
|
||||||
await createRelay(relay);
|
await db.createRelay(relay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(() => {
|
clearStep();
|
||||||
clearStep();
|
navigate('/', { replace: true });
|
||||||
navigate('/', { replace: true });
|
|
||||||
}, 1000);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
console.log('error: ', e);
|
console.log('error: ', e);
|
||||||
|
@ -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 { getPublicKey, nip19 } from 'nostr-tools';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { Resolver, useForm } from 'react-hook-form';
|
import { Resolver, useForm } from 'react-hook-form';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
import { useStronghold } from '@stores/stronghold';
|
import { useStronghold } from '@stores/stronghold';
|
||||||
|
|
||||||
import { useAccount } from '@utils/hooks/useAccount';
|
import { useAccount } from '@utils/hooks/useAccount';
|
||||||
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
|
|
||||||
|
|
||||||
type FormValues = {
|
type FormValues = {
|
||||||
password: string;
|
password: string;
|
||||||
@ -37,7 +40,7 @@ export function ResetScreen() {
|
|||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
const { account } = useAccount();
|
const { account } = useAccount();
|
||||||
const { save, reset } = useSecureStorage();
|
const { db } = useStorage();
|
||||||
|
|
||||||
// toggle private key
|
// toggle private key
|
||||||
const showPassword = () => {
|
const showPassword = () => {
|
||||||
@ -75,9 +78,18 @@ export function ResetScreen() {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// remove old stronghold
|
// remove old stronghold
|
||||||
await reset();
|
await db.secureReset();
|
||||||
|
|
||||||
// save privkey to secure storage
|
// 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
|
// add privkey to state
|
||||||
setPrivkey(account.privkey);
|
setPrivkey(account.privkey);
|
||||||
// redirect to home
|
// redirect to home
|
||||||
|
@ -54,7 +54,7 @@ export function UnlockScreen() {
|
|||||||
const dir = await appConfigDir();
|
const dir = await appConfigDir();
|
||||||
const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password);
|
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);
|
const privkey = await db.secureLoad(account.pubkey);
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import { invoke } from '@tauri-apps/api/tauri';
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { useNDK } from '@libs/ndk/provider';
|
import { useNDK } from '@libs/ndk/provider';
|
||||||
import { updateLastLogin } from '@libs/storage';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { LoaderIcon } from '@shared/icons';
|
import { LoaderIcon } from '@shared/icons';
|
||||||
|
|
||||||
@ -10,6 +10,7 @@ import { useAccount } from '@utils/hooks/useAccount';
|
|||||||
import { useNostr } from '@utils/hooks/useNostr';
|
import { useNostr } from '@utils/hooks/useNostr';
|
||||||
|
|
||||||
export function SplashScreen() {
|
export function SplashScreen() {
|
||||||
|
const { db } = useStorage();
|
||||||
const { ndk, relayUrls } = useNDK();
|
const { ndk, relayUrls } = useNDK();
|
||||||
const { status, account } = useAccount();
|
const { status, account } = useAccount();
|
||||||
const { fetchUserData } = useNostr();
|
const { fetchUserData } = useNostr();
|
||||||
@ -30,7 +31,7 @@ export function SplashScreen() {
|
|||||||
const user = await fetchUserData();
|
const user = await fetchUserData();
|
||||||
if (user.status === 'ok') {
|
if (user.status === 'ok') {
|
||||||
const now = Math.floor(Date.now() / 1000);
|
const now = Math.floor(Date.now() / 1000);
|
||||||
await updateLastLogin(now);
|
await db.updateLastLogin(now);
|
||||||
invoke('close_splashscreen');
|
invoke('close_splashscreen');
|
||||||
} else {
|
} else {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
import { NDKCacheAdapter } from '@nostr-dev-kit/ndk';
|
import { NDKCacheAdapter } from '@nostr-dev-kit/ndk';
|
||||||
import { NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk';
|
import { NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk';
|
||||||
import { Store } from '@tauri-apps/plugin-store';
|
|
||||||
|
import { LumeStorage } from '@libs/storage/instance';
|
||||||
|
|
||||||
export default class TauriAdapter implements NDKCacheAdapter {
|
export default class TauriAdapter implements NDKCacheAdapter {
|
||||||
public store: Store;
|
public store: LumeStorage;
|
||||||
readonly locking: boolean;
|
readonly locking: boolean;
|
||||||
|
|
||||||
constructor() {
|
constructor(db: LumeStorage) {
|
||||||
this.store = new Store('.ndkcache.dat');
|
this.store = db;
|
||||||
this.locking = true;
|
this.locking = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ export default class TauriAdapter implements NDKCacheAdapter {
|
|||||||
for (const author of filter.authors) {
|
for (const author of filter.authors) {
|
||||||
for (const kind of filter.kinds) {
|
for (const kind of filter.kinds) {
|
||||||
const key = `${author}:${kind}`;
|
const key = `${author}:${kind}`;
|
||||||
promises.push(this.store.get(key));
|
promises.push(this.store.getEventByKey(key));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,25 +29,26 @@ export default class TauriAdapter implements NDKCacheAdapter {
|
|||||||
|
|
||||||
for (const result of results) {
|
for (const result of results) {
|
||||||
if (result) {
|
if (result) {
|
||||||
const event = await this.store.get(result as string);
|
console.log('cache hit: ', result);
|
||||||
|
const ndkEvent = new NDKEvent(
|
||||||
if (event) {
|
subscription.ndk,
|
||||||
console.log('cache hit: ', result);
|
JSON.parse(result.event as string)
|
||||||
const ndkEvent = new NDKEvent(subscription.ndk, JSON.parse(event as string));
|
);
|
||||||
subscription.eventReceived(ndkEvent, undefined, true);
|
subscription.eventReceived(ndkEvent, undefined, true);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filter.ids) {
|
if (filter.ids) {
|
||||||
for (const id of filter.ids) {
|
for (const id of filter.ids) {
|
||||||
const key = id;
|
const cacheEvent = await this.store.getEventByID(id);
|
||||||
const event = await this.store.get(key);
|
|
||||||
|
|
||||||
if (event) {
|
if (cacheEvent) {
|
||||||
console.log('cache hit: ', id);
|
console.log('cache hit: ', id);
|
||||||
const ndkEvent = new NDKEvent(subscription.ndk, JSON.parse(event as string));
|
const ndkEvent = new NDKEvent(
|
||||||
|
subscription.ndk,
|
||||||
|
JSON.parse(cacheEvent.event as string)
|
||||||
|
);
|
||||||
subscription.eventReceived(ndkEvent, undefined, true);
|
subscription.eventReceived(ndkEvent, undefined, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,13 +61,8 @@ export default class TauriAdapter implements NDKCacheAdapter {
|
|||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
Promise.all([
|
Promise.all([
|
||||||
this.store.set(event.id, JSON.stringify(nostrEvent)),
|
this.store.createEvent(key, event.id, event.kind, JSON.stringify(nostrEvent)),
|
||||||
this.store.set(key, event.id),
|
|
||||||
]).then(() => resolve());
|
]).then(() => resolve());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public save() {
|
|
||||||
return this.store.save();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,20 @@
|
|||||||
// inspire by: https://github.com/nostr-dev-kit/ndk-react/
|
// inspire by: https://github.com/nostr-dev-kit/ndk-react/
|
||||||
import NDK from '@nostr-dev-kit/ndk';
|
import NDK from '@nostr-dev-kit/ndk';
|
||||||
import { fetch } from '@tauri-apps/plugin-http';
|
import { fetch } from '@tauri-apps/plugin-http';
|
||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import TauriAdapter from '@libs/ndk/cache';
|
import TauriAdapter from '@libs/ndk/cache';
|
||||||
import { getExplicitRelayUrls } from '@libs/storage';
|
import { getExplicitRelayUrls } from '@libs/storage';
|
||||||
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { FULL_RELAYS } from '@stores/constants';
|
import { FULL_RELAYS } from '@stores/constants';
|
||||||
|
|
||||||
export const NDKInstance = () => {
|
export const NDKInstance = () => {
|
||||||
|
const { db } = useStorage();
|
||||||
|
|
||||||
const [ndk, setNDK] = useState<NDK | undefined>(undefined);
|
const [ndk, setNDK] = useState<NDK | undefined>(undefined);
|
||||||
const [relayUrls, setRelayUrls] = useState<string[]>([]);
|
const [relayUrls, setRelayUrls] = useState<string[]>([]);
|
||||||
|
|
||||||
const cacheAdapter = useMemo(() => new TauriAdapter(), []);
|
|
||||||
|
|
||||||
// TODO: fully support NIP-11
|
// TODO: fully support NIP-11
|
||||||
async function verifyRelays(relays: string[]) {
|
async function verifyRelays(relays: string[]) {
|
||||||
const verifiedRelays: string[] = [];
|
const verifiedRelays: string[] = [];
|
||||||
@ -57,6 +58,8 @@ export const NDKInstance = () => {
|
|||||||
explicitRelayUrls = await verifyRelays(FULL_RELAYS);
|
explicitRelayUrls = await verifyRelays(FULL_RELAYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cacheAdapter = new TauriAdapter(db);
|
||||||
|
console.log('ndk cache adapter: ', cacheAdapter);
|
||||||
const instance = new NDK({ explicitRelayUrls, cacheAdapter });
|
const instance = new NDK({ explicitRelayUrls, cacheAdapter });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -71,10 +74,6 @@ export const NDKInstance = () => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!ndk) initNDK();
|
if (!ndk) initNDK();
|
||||||
|
|
||||||
return () => {
|
|
||||||
cacheAdapter.save();
|
|
||||||
};
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
import { BaseDirectory, removeFile } from '@tauri-apps/plugin-fs';
|
||||||
import Database from '@tauri-apps/plugin-sql';
|
import Database from '@tauri-apps/plugin-sql';
|
||||||
import { Stronghold } from '@tauri-apps/plugin-stronghold';
|
import { Stronghold } from '@tauri-apps/plugin-stronghold';
|
||||||
|
|
||||||
import { Account, Widget } from '@utils/types';
|
import { Account, Relays, Widget } from '@utils/types';
|
||||||
|
|
||||||
export class LumeStorage {
|
export class LumeStorage {
|
||||||
public db: Database;
|
public db: Database;
|
||||||
@ -39,6 +40,10 @@ export class LumeStorage {
|
|||||||
return decoded;
|
return decoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async secureReset() {
|
||||||
|
return await removeFile('lume.stronghold', { dir: BaseDirectory.AppConfig });
|
||||||
|
}
|
||||||
|
|
||||||
public async getActiveAccount() {
|
public async getActiveAccount() {
|
||||||
const account: Account = await this.db.select(
|
const account: Account = await this.db.select(
|
||||||
'SELECT * FROM accounts WHERE is_active = 1;'
|
'SELECT * FROM accounts WHERE is_active = 1;'
|
||||||
@ -123,7 +128,10 @@ export class LumeStorage {
|
|||||||
'SELECT * FROM events WHERE cache_key = $1 ORDER BY id DESC LIMIT 1;',
|
'SELECT * FROM events WHERE cache_key = $1 ORDER BY id DESC LIMIT 1;',
|
||||||
[cacheKey]
|
[cacheKey]
|
||||||
)?.[0];
|
)?.[0];
|
||||||
if (!event) console.error('failed to get event by cache_key: ', cacheKey);
|
if (!event) {
|
||||||
|
console.error('failed to get event by cache_key: ', cacheKey);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,10 +140,42 @@ export class LumeStorage {
|
|||||||
'SELECT * FROM events WHERE event_id = $1 ORDER BY id DESC LIMIT 1;',
|
'SELECT * FROM events WHERE event_id = $1 ORDER BY id DESC LIMIT 1;',
|
||||||
[id]
|
[id]
|
||||||
)?.[0];
|
)?.[0];
|
||||||
if (!event) console.error('failed to get event by id: ', id);
|
if (!event) {
|
||||||
|
console.error('failed to get event by id: ', id);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getExplicitRelayUrls() {
|
||||||
|
const account = await this.getActiveAccount();
|
||||||
|
const result: Relays[] = await this.db.select(
|
||||||
|
`SELECT * FROM relays WHERE account_id = "${account.id}";`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.length > 0) return result.map((el) => el.relay);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async createRelay(relay: string, purpose?: string) {
|
||||||
|
const account = await this.getActiveAccount();
|
||||||
|
return await this.db.execute(
|
||||||
|
'INSERT OR IGNORE INTO relays (account_id, relay, purpose) VALUES ($1, $2, $3);',
|
||||||
|
[account.id, relay, purpose || '']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async removeRelay(relay: string) {
|
||||||
|
return await this.db.execute(`DELETE FROM relays WHERE relay = "${relay}";`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async updateLastLogin(time: number) {
|
||||||
|
return await this.db.execute(
|
||||||
|
'UPDATE settings SET value = $1 WHERE key = "last_login";',
|
||||||
|
[time]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public async close() {
|
public async close() {
|
||||||
return this.db.close();
|
return this.db.close();
|
||||||
}
|
}
|
||||||
|
@ -28,15 +28,9 @@ const StorageProvider = ({ children }: PropsWithChildren<object>) => {
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
if (db) {
|
||||||
<StorageContext.Provider
|
return <StorageContext.Provider value={{ db }}>{children}</StorageContext.Provider>;
|
||||||
value={{
|
}
|
||||||
db,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{children}
|
|
||||||
</StorageContext.Provider>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const useStorage = () => {
|
const useStorage = () => {
|
||||||
|
@ -6,7 +6,7 @@ import { getActiveAccount } from '@libs/storage';
|
|||||||
export function useAccount() {
|
export function useAccount() {
|
||||||
const { ndk } = useNDK();
|
const { ndk } = useNDK();
|
||||||
const { status, data: account } = useQuery(
|
const { status, data: account } = useQuery(
|
||||||
['currentAccount'],
|
['account'],
|
||||||
async () => {
|
async () => {
|
||||||
const account = await getActiveAccount();
|
const account = await getActiveAccount();
|
||||||
console.log('account: ', account);
|
console.log('account: ', account);
|
||||||
|
@ -15,7 +15,7 @@ import { nip19 } from 'nostr-tools';
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import { useNDK } from '@libs/ndk/provider';
|
import { useNDK } from '@libs/ndk/provider';
|
||||||
import { updateAccount } from '@libs/storage';
|
import { useStorage } from '@libs/storage/provider';
|
||||||
|
|
||||||
import { useStronghold } from '@stores/stronghold';
|
import { useStronghold } from '@stores/stronghold';
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ import { useAccount } from '@utils/hooks/useAccount';
|
|||||||
export function useNostr() {
|
export function useNostr() {
|
||||||
const { ndk, relayUrls } = useNDK();
|
const { ndk, relayUrls } = useNDK();
|
||||||
const { account } = useAccount();
|
const { account } = useAccount();
|
||||||
|
const { db } = useStorage();
|
||||||
|
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const privkey = useStronghold((state) => state.privkey);
|
const privkey = useStronghold((state) => state.privkey);
|
||||||
@ -74,10 +75,10 @@ export function useNostr() {
|
|||||||
|
|
||||||
const network = [...lruNetwork.values()] as string[];
|
const network = [...lruNetwork.values()] as string[];
|
||||||
|
|
||||||
await updateAccount('follows', [...follows]);
|
await db.updateAccount('follows', [...follows]);
|
||||||
await updateAccount('network', [...new Set([...follows, ...network])]);
|
await db.updateAccount('network', [...new Set([...follows, ...network])]);
|
||||||
|
|
||||||
queryClient.invalidateQueries(['currentAccount']);
|
queryClient.invalidateQueries(['account']);
|
||||||
|
|
||||||
return { status: 'ok' };
|
return { status: 'ok' };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user