update stronghold

This commit is contained in:
Ren Amamiya
2023-07-20 08:57:58 +07:00
parent a80477b40e
commit bbfdb139c6
16 changed files with 74 additions and 127 deletions

View File

@@ -5,14 +5,12 @@ import { useNDK } from '@libs/ndk/provider';
import { useStronghold } from '@stores/stronghold';
import { useAccount } from '@utils/hooks/useAccount';
import { useSecureStorage } from '@utils/hooks/useSecureStorage';
export function usePublish() {
const { ndk } = useNDK();
const { account } = useAccount();
const { load } = useSecureStorage();
const cachePrivkey = useStronghold((state) => state.privkey);
const privkey = useStronghold((state) => state.privkey);
const publish = async ({
content,
@@ -23,12 +21,7 @@ export function usePublish() {
kind: NDKKind | number;
tags: string[][];
}): Promise<NDKEvent> => {
let privkey: string;
if (cachePrivkey) {
privkey = cachePrivkey;
} else {
privkey = await load(account.pubkey);
}
if (!privkey) throw new Error('Private key not found');
const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(privkey);

View File

@@ -1,13 +1,9 @@
import { appConfigDir } from '@tauri-apps/api/path';
import { Stronghold } from 'tauri-plugin-stronghold-api';
import { useStronghold } from '@stores/stronghold';
const dir = await appConfigDir();
export function useSecureStorage() {
const password = useStronghold((state) => state.password);
async function getClient(stronghold: Stronghold) {
try {
return await stronghold.loadClient('lume');
@@ -16,22 +12,16 @@ export function useSecureStorage() {
}
}
const save = async (key: string, value: string, userpass?: string) => {
const stronghold = await Stronghold.load(
`${dir}lume.stronghold`,
userpass ? userpass : password
);
const save = async (key: string, value: string, password: string) => {
const stronghold = await Stronghold.load(`${dir}lume.stronghold`, password);
const client = await getClient(stronghold);
const store = client.getStore();
await store.insert(key, Array.from(new TextEncoder().encode(value)));
return await stronghold.save();
};
const load = async (key: string, userpass?: string) => {
const stronghold = await Stronghold.load(
`${dir}lume.stronghold`,
userpass ? userpass : password
);
const load = async (key: string, password: string) => {
const stronghold = await Stronghold.load(`${dir}lume.stronghold`, password);
const client = await getClient(stronghold);
const store = client.getStore();
const value = await store.get(key);