From 10c20c0c5b130c389680e37e4e4aba8e2abdbf1e Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 24 Dec 2022 05:22:50 -0800 Subject: password prompt and pin fixes --- src/lib/nostr.js | 30 ++++++++++++++++++++++++++++++ src/lib/store.js | 1 + src/lib/utils.js | 5 +++-- 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/lib/nostr.js (limited to 'src/lib') diff --git a/src/lib/nostr.js b/src/lib/nostr.js new file mode 100644 index 00000000..df63d5d8 --- /dev/null +++ b/src/lib/nostr.js @@ -0,0 +1,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 }); +}; diff --git a/src/lib/store.js b/src/lib/store.js index 1a1a88c2..9660bf55 100644 --- a/src/lib/store.js +++ b/src/lib/store.js @@ -56,3 +56,4 @@ export const invoice = writable(); export const txns = persistLocal('transactions', []); export const events = writable({}); export const password = writable(); +export const passwordPrompt = writable(); diff --git a/src/lib/utils.js b/src/lib/utils.js index 79509cc1..d4d78007 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -49,9 +49,10 @@ export const post = (url, body, headers) => { try { let { message } = JSON.parse(text); if (message) return JSON.stringify({ name: 'Error', message }); - } catch (e) {} + } catch (e) { + } - return text; + return { error: text }; }) .then((body) => { try { -- cgit v1.2.3