diff options
| author | Adam Soltys <[email protected]> | 2022-12-23 13:51:52 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2022-12-23 13:51:52 -0800 |
| commit | 588b8ca814f3c86c5880f075ba0fb58faada873f (patch) | |
| tree | fec923b0d2787fffe53e1eb92cdb456c47b767e7 /src | |
| parent | 599ae2f6aad7456612f2d7d3fa3c79b7b60ea9c2 (diff) | |
| download | coinos-ui-588b8ca814f3c86c5880f075ba0fb58faada873f.tar.gz coinos-ui-588b8ca814f3c86c5880f075ba0fb58faada873f.zip | |
require pin on key gen
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/utils.js | 9 | ||||
| -rw-r--r-- | src/routes/(app)/[username]/generate/+page.svelte | 29 | ||||
| -rw-r--r-- | src/routes/(app)/[username]/generate/+server.js | 12 | ||||
| -rw-r--r-- | src/routes/(app)/send/[...text]/+page.server.js | 2 |
4 files changed, 43 insertions, 9 deletions
diff --git a/src/lib/utils.js b/src/lib/utils.js index 5ffc24e2..79509cc1 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -44,7 +44,14 @@ export const post = (url, body, headers) => { return fetch(base + url, { method: 'POST', body: JSON.stringify(body), headers }) .then(async (r) => { if (r.ok) return r.text(); - throw new Error(await r.text()); + let text = await r.text(); + + try { + let { message } = JSON.parse(text); + if (message) return JSON.stringify({ name: 'Error', message }); + } catch (e) {} + + return text; }) .then((body) => { try { diff --git a/src/routes/(app)/[username]/generate/+page.svelte b/src/routes/(app)/[username]/generate/+page.svelte index 345b35a4..559d0d14 100644 --- a/src/routes/(app)/[username]/generate/+page.svelte +++ b/src/routes/(app)/[username]/generate/+page.svelte @@ -1,4 +1,5 @@ <script> + import { failure } from '$lib/utils'; import { Buffer } from 'buffer'; import { onMount } from 'svelte'; import { bech32m } from 'bech32'; @@ -7,15 +8,23 @@ import { bip32, stretch, post } from '$lib/utils'; import { goto, invalidate } from '$app/navigation'; import { loginRedirect } from '$lib/store'; + import { Pin } from '$comp'; + import { pin } from '$lib/store'; export let data; let { user } = data; const { encode, toWords } = bech32m; - let mnemonic, key, password, seed, entropy, cipher, pubkey, salt, child; + let loaded, mnemonic, key, password, seed, entropy, cipher, pubkey, salt, child; onMount(async () => { + setTimeout(() => (loaded = true), 50); + generate(); + }); + + let generate = async () => { + if (!$pin?.length === 6) return setTimeout(generate, 1000); mnemonic = generateMnemonic(); seed = mnemonicToSeedSync(mnemonic); entropy = mnemonicToEntropy(mnemonic); @@ -34,10 +43,24 @@ user.pubkey = Buffer.from(ecc.xOnlyPointFromPoint(child.publicKey)).toString('hex'); user.cipher = encode('en', toWords(bytes), 180); user.salt = Buffer.from(salt).toString('hex'); + user.pin = $pin; - await post(`/${user.username}/generate`, user); + try { + await post(`/${user.username}/generate`, user); + } catch (e) { + if (e.message?.startsWith('Pin')) { + $pin = ''; + failure('Wrong pin, try again'); + } + } invalidate('app:user'); setTimeout(() => goto($loginRedirect || `/${user.username}/dashboard`), 10); - }); + }; + + $: $pin?.length === 6 && generate(); </script> + +{#if loaded && user.haspin && $pin?.length !== 6} + <Pin /> +{/if} diff --git a/src/routes/(app)/[username]/generate/+server.js b/src/routes/(app)/[username]/generate/+server.js index c63ab5a6..608f0575 100644 --- a/src/routes/(app)/[username]/generate/+server.js +++ b/src/routes/(app)/[username]/generate/+server.js @@ -1,8 +1,12 @@ import { auth, post } from '$lib/utils'; -import { json } from '@sveltejs/kit'; +import { error, json } from '@sveltejs/kit'; export async function POST({ cookies, request }) { - let body = await request.json(); - let user = await post(`/user`, body, auth(cookies)); - return json(user); + try { + let body = await request.json(); + let user = await post(`/user`, body, auth(cookies)); + return json(user); + } catch (e) { + throw error(500, e.message); + } } diff --git a/src/routes/(app)/send/[...text]/+page.server.js b/src/routes/(app)/send/[...text]/+page.server.js index 27472006..15305da9 100644 --- a/src/routes/(app)/send/[...text]/+page.server.js +++ b/src/routes/(app)/send/[...text]/+page.server.js @@ -8,7 +8,7 @@ let parse = async (t, host) => { let amount, user, uuid; - t = t.trim(); + t = t.trim(); t.toLowerCase().startsWith('bitcoin:') && ({ address: t, |
