aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/nostr.js
blob: df63d5d8ec0196290e9f434bff1c2ffcd193e5dd (plain) (blame)
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
import { entropyToMnemonic, mnemonicToSeedSync } from 'bip39';
import { Buffer } from 'buffer';
import { bech32m } from 'bech32';
import { bip32, post, stretch } from '$lib/utils';
import { calculateId, signId } from 'nostr';

const { decode, fromWords } = bech32m;

export let send = async (event, { cipher, username, salt }, password) => {
	let mnemonic, key, seed, entropy, child, privkey;
	entropy = Buffer.from(
		await crypto.subtle.decrypt(
			{ name: 'AES-GCM', iv: new Uint8Array(16) },
			await stretch(password, Buffer.from(salt, 'hex')),
			Uint8Array.from(fromWords(decode(cipher, 180).words))
		),
		'hex'
	).toString('hex');

	mnemonic = entropyToMnemonic(entropy);
	seed = mnemonicToSeedSync(mnemonic);
	key = bip32.fromSeed(seed);
	child = key.derivePath("m/44'/1237'/0'/0/0");
	privkey = child.privateKey;

	event.id = await calculateId(event);
	event.sig = await signId(privkey, event.id);

	await post(`/${username}`, { event });
};