cache decrypted nsec

maybe fix mobile height issue
This commit is contained in:
hzrd149 2023-03-19 14:12:34 -06:00
parent 0193f31cd3
commit 340458aa52
3 changed files with 14 additions and 4 deletions

View File

@ -27,7 +27,7 @@ export const Page = ({ children }: { children: React.ReactNode }) => {
size="lg"
display="flex"
flexDirection="column"
height="100vh"
height="100%"
overflow="hidden"
position="relative"
padding="0"

View File

@ -3,6 +3,8 @@ import { Account } from "./account";
import db from "./db";
import { nip04, signEvent, getEventHash, getPublicKey } from "nostr-tools";
const decryptedKeys = new Map<string, string>();
class SigningService {
private async getSalt() {
let salt = await db.get("settings", "salt");
@ -16,7 +18,9 @@ class SigningService {
}
private async getKeyMaterial() {
const password = window.prompt("Enter local encryption password");
const password = window.prompt(
"Enter local encryption password. This password is used to keep your secret key save."
);
if (!password) throw new Error("Password required");
const enc = new TextEncoder();
return window.crypto.subtle.importKey("raw", enc.encode(password), "PBKDF2", false, ["deriveBits", "deriveKey"]);
@ -53,12 +57,18 @@ class SigningService {
async decryptSecKey(account: Account) {
if (!account.secKey) throw new Error("Account dose not have a secret key");
const cache = decryptedKeys.get(account.pubkey);
if (cache) return cache;
const key = await this.getEncryptionKey();
const decode = new TextDecoder();
try {
const decrypted = await window.crypto.subtle.decrypt({ name: "AES-GCM", iv: account.iv }, key, account.secKey);
return decode.decode(decrypted);
const secKey = decode.decode(decrypted);
decryptedKeys.set(account.pubkey, secKey);
return secKey;
} catch (e) {
throw new Error("Failed to decrypt secret key");
}

View File

@ -44,7 +44,7 @@ const UserNotesTab = () => {
`${truncatedId(pubkey)}-notes`,
relays,
{ authors: [pubkey], kinds: [1] },
{ pageSize: moment.duration(1, "day").asSeconds(), startLimit: 20 }
{ pageSize: moment.duration(2, "day").asSeconds(), startLimit: 20 }
);
const timeline = showReplies ? events : events.filter(isNote);