1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
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 {
type EventTemplate,
finalizeEvent,
getPublicKey,
nip04,
nip19,
} from "nostr-tools";
import { get } from "svelte/store";
import {
decrypt as nip49decrypt,
encrypt as nip49encrypt,
} from "nostr-tools/nip49";
import { bech32m, hex } from "@scure/base";
import { entropyToMnemonic, mnemonicToEntropy } from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english";
import { generateSeedWords, privateKeyFromSeedWords } from "nostr-tools/nip06";
type User = {
[key: string]: any;
};
type EncryptParams = {
message: string;
recipient: string;
user: User;
};
const { encode, decode, toWords, fromWords } = bech32m;
export const generate = async (user: any) => {
const p = get(pin);
if (p && p.length !== 6) return;
const salt = crypto.getRandomValues(new Uint8Array(16));
const mnemonic = generateSeedWords();
const sk = privateKeyFromSeedWords(mnemonic);
const bytes = new Uint8Array(
await crypto.subtle.encrypt(
{ name: "AES-GCM", iv: new Uint8Array(16) },
await stretch(await getPassword(), salt),
mnemonicToEntropy(mnemonic, wordlist),
),
);
user.pubkey = getPublicKey(sk);
user.cipher = encode("en", toWords(bytes), 180);
user.salt = Buffer.from(salt).toString("hex");
};
export const encrypt = async ({ message, recipient, user }: EncryptParams) => {
const sk = await getPrivateKey(user);
return nip04.encrypt(sk, recipient, message);
};
export const decrypt = async ({ event, user }) => {
const cache = get(decrypted);
try {
let { content, pubkey, id } = event;
if (cache[id]) return cache[id];
if (pubkey === user.pubkey) pubkey = event.tags[0][1];
const message = await nip04.decrypt(
await getPrivateKey(user),
pubkey,
content,
);
cache[id] = Buffer.from(message).toString("utf8");
decrypted.set(cache);
return cache[id];
} catch (e) {
// console.log(e);
}
};
export const getPrivateKey = async (user: User): Promise<Uint8Array> => {
let k;
if (browser) {
k = localStorage.getItem("nsec");
if (k) {
return nip19.decode(k).data as Uint8Array;
}
}
const { nsec } = user;
if (nsec) {
k = nip49decrypt(nsec, await getPassword());
} else k = privateKeyFromSeedWords(await getMnemonic(user));
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;
throw new Error("invalid nsec");
};
export const getNsec = async (user: User) => {
return nip19.nsecEncode(await getPrivateKey(user));
};
export const setNsec = async (user: User, nsec: string) => {
user.pubkey = getPublicKey(decodeNsec(nsec));
user.nsec = await encryptNsec(nsec);
};
export const encryptNsec = async (nsec: string) => {
const d = decodeNsec(nsec);
return nip49encrypt(d, await getPassword());
};
type SignParams = {
event: EventTemplate;
user: User;
};
export const sign = async ({ event, user }: SignParams) => {
event = finalizeEvent(event, await getPrivateKey(user));
};
export const send = (event: EventTemplate) => {
return post("/events", { event });
};
const getPassword = async (): Promise<string> => {
if (!get(pw)) passwordPrompt.set(true);
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);
};
|