aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/nostr.ts63
-rw-r--r--src/lib/store.ts1
2 files changed, 19 insertions, 45 deletions
diff --git a/src/lib/nostr.ts b/src/lib/nostr.ts
index e45ce634..77c1d60a 100644
--- a/src/lib/nostr.ts
+++ b/src/lib/nostr.ts
@@ -1,7 +1,12 @@
import { Buffer } from "buffer";
import { browser } from "$app/environment";
-import { decrypted, password as pw, passwordPrompt, pin } from "$lib/store";
-import { post, stretch, wait } from "$lib/utils";
+import {
+ decrypted,
+ password as pw,
+ passwordPrompt,
+ signaturePrompt,
+} from "$lib/store";
+import { post, wait } from "$lib/utils";
import {
type EventTemplate,
finalizeEvent,
@@ -16,11 +21,7 @@ import {
encrypt as nip49encrypt,
} from "nostr-tools/nip49";
-import { bech32m, hex } from "@scure/base";
-import { entropyToMnemonic } from "@scure/bip39";
-import { wordlist } from "@scure/bip39/wordlists/english";
-
-import { privateKeyFromSeedWords } from "nostr-tools/nip06";
+import { bech32m } from "@scure/base";
type User = {
[key: string]: any;
@@ -32,8 +33,6 @@ type EncryptParams = {
user: User;
};
-const { encode, decode, toWords, fromWords } = bech32m;
-
export const encrypt = async ({ message, recipient, user }: EncryptParams) => {
const sk = await getPrivateKey(user);
return nip04.encrypt(sk, recipient, message);
@@ -74,25 +73,14 @@ export const getPrivateKey = async (user: User): Promise<Uint8Array> => {
if (nsec) {
k = nip49decrypt(nsec, await getPassword());
- } else k = privateKeyFromSeedWords(await getMnemonic(user));
+ } else {
+ throw new Error("nsec not available");
+ }
localStorage.setItem("nsec", nip19.nsecEncode(k));
return k;
};
-export const getMnemonic = async (user: User) => {
- const { cipher, salt } = user;
- const entropy = new Uint8Array(
- await crypto.subtle.decrypt(
- { name: "AES-GCM", iv: new Uint8Array(16) },
- await stretch(await getPassword(), Buffer.from(salt, "hex")),
- Uint8Array.from(fromWords(decode(cipher, 180).words)),
- ),
- );
-
- return entropyToMnemonic(entropy, wordlist);
-};
-
const decodeNsec = (nsec: string): Uint8Array => {
const { type, data } = nip19.decode(nsec);
if (type === "nsec" && data instanceof Uint8Array) return data;
@@ -119,8 +107,13 @@ type SignParams = {
};
export const sign = async ({ event, user }: SignParams) => {
- const sk = await getPrivateKey(user);
- event = finalizeEvent(event, sk);
+ let sk;
+ if (user.nsec) {
+ sk = await getPrivateKey(user);
+ event = finalizeEvent(event, sk);
+ } else {
+ signaturePrompt.set(event);
+ }
};
export const send = (event: EventTemplate) => {
@@ -132,23 +125,3 @@ const getPassword = async (): Promise<string> => {
await wait(() => !!get(pw));
return get(pw) || "";
};
-
-export const reEncryptEntropy = async (user: User, newPassword: string) => {
- const { cipher, salt } = user;
-
- const entropy = await crypto.subtle.decrypt(
- { name: "AES-GCM", iv: new Uint8Array(16) },
- await stretch(await getPassword(), hex.decode(salt)),
- Uint8Array.from(fromWords(decode(cipher, 180).words)),
- );
-
- const bytes = new Uint8Array(
- await crypto.subtle.encrypt(
- { name: "AES-GCM", iv: new Uint8Array(16) },
- await stretch(newPassword, hex.decode(salt)),
- entropy,
- ),
- );
-
- return encode("en", toWords(bytes), 180);
-};
diff --git a/src/lib/store.ts b/src/lib/store.ts
index 2532018d..0aba3d46 100644
--- a/src/lib/store.ts
+++ b/src/lib/store.ts
@@ -54,6 +54,7 @@ export const mnemonic = persistSession("mnemonic");
export const newPayment = persistLocal("newPayment");
export const password = writable<string | undefined>();
export const passwordPrompt = writable();
+export const signaturePrompt = writable();
export const pin = persistLocal("pin");
export const rate = writable();
export const request = writable();