secure privkey
This commit is contained in:
@@ -3,10 +3,12 @@ import { NDKEvent, NDKKind, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk';
|
||||
import { useNDK } from '@libs/ndk/provider';
|
||||
|
||||
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 publish = async ({
|
||||
content,
|
||||
@@ -17,8 +19,10 @@ export function usePublish() {
|
||||
kind: NDKKind;
|
||||
tags: string[][];
|
||||
}): Promise<NDKEvent> => {
|
||||
const privkey = await load(account.pubkey);
|
||||
|
||||
const event = new NDKEvent(ndk);
|
||||
const signer = new NDKPrivateKeySigner(account.privkey);
|
||||
const signer = new NDKPrivateKeySigner(privkey);
|
||||
|
||||
event.content = content;
|
||||
event.kind = kind;
|
||||
|
||||
43
src/utils/hooks/useSecureStorage.tsx
Normal file
43
src/utils/hooks/useSecureStorage.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
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');
|
||||
} catch {
|
||||
return await stronghold.createClient('lume');
|
||||
}
|
||||
}
|
||||
|
||||
const save = async (key: string, value: string, userpass?: string) => {
|
||||
const stronghold = await Stronghold.load(
|
||||
`${dir}lume.stronghold`,
|
||||
userpass ? userpass : 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 client = await getClient(stronghold);
|
||||
const store = client.getStore();
|
||||
const value = await store.get(key);
|
||||
const decoded = new TextDecoder().decode(new Uint8Array(value));
|
||||
return decoded;
|
||||
};
|
||||
|
||||
return { save, load };
|
||||
}
|
||||
Reference in New Issue
Block a user