flotilla/src/app/components/SignUpKey.svelte

120 lines
3.4 KiB
Svelte
Raw Normal View History

2025-02-14 18:53:36 +00:00
<script lang="ts">
2025-08-18 22:02:17 +00:00
import {nsecEncode} from "nostr-tools/nip19"
2025-02-14 18:53:36 +00:00
import {encrypt} from "nostr-tools/nip49"
2025-06-06 23:38:11 +00:00
import {hexToBytes} from "@welshman/lib"
2025-04-11 18:51:15 +00:00
import {makeSecret} from "@welshman/signer"
2025-08-18 22:02:17 +00:00
import type {Profile} from "@welshman/util"
2025-02-14 18:53:36 +00:00
import {preventDefault, downloadText} from "@lib/html"
import Icon from "@lib/components/Icon.svelte"
import Field from "@lib/components/Field.svelte"
import Button from "@lib/components/Button.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
2025-08-18 22:02:17 +00:00
import SignUpComplete from "@app/components/SignUpComplete.svelte"
import {pushToast} from "@app/util/toast"
import {pushModal} from "@app/util/modal"
2025-02-14 18:53:36 +00:00
2025-08-18 22:02:17 +00:00
type Props = {
profile: Profile
}
const {profile}: Props = $props()
2025-02-14 18:53:36 +00:00
const secret = makeSecret()
const back = () => history.back()
2025-08-18 22:02:17 +00:00
const downloadKey = () => {
if (usePassword) {
if (password.length < 12) {
return pushToast({
theme: "error",
message: "Your password must be at least 12 characters long.",
})
}
const ncryptsec = encrypt(hexToBytes(secret), password)
downloadText("Nostr Secret Key.txt", ncryptsec)
} else {
const nsec = nsecEncode(hexToBytes(secret))
downloadText("Nostr Secret Key.txt", nsec)
2025-02-14 18:53:36 +00:00
}
2025-08-18 22:02:17 +00:00
didDownload = true
}
2025-02-14 18:53:36 +00:00
2025-08-18 22:02:17 +00:00
const next = () => {
pushModal(SignUpComplete, {profile, secret})
}
2025-02-14 18:53:36 +00:00
2025-08-18 22:02:17 +00:00
const onPasswordChange = () => {
didDownload = false
2025-02-14 18:53:36 +00:00
}
2025-08-18 22:02:17 +00:00
const toggleUsePassword = () => {
usePassword = !usePassword
didDownload = false
}
let password = $state("")
let usePassword = $state(false)
let didDownload = $state(false)
2025-02-14 18:53:36 +00:00
</script>
<form class="column gap-4" onsubmit={preventDefault(next)}>
<ModalHeader>
{#snippet title()}
2025-08-18 22:02:17 +00:00
<div>Your Keys are Ready!</div>
2025-02-14 18:53:36 +00:00
{/snippet}
</ModalHeader>
<p>
2025-08-18 22:02:17 +00:00
A cryptographic key pair has two parts: your <strong>public key</strong> identifies your
account, while your <strong>private key</strong> acts sort of like a master password.
2025-02-14 18:53:36 +00:00
</p>
<p>
2025-08-18 22:02:17 +00:00
Securing your private key is very important, so make sure to take the time to save your key in a
secure place (like a password manager).
2025-02-14 18:53:36 +00:00
</p>
2025-08-18 22:02:17 +00:00
{#if usePassword}
<Field>
{#snippet label()}
Password*
{/snippet}
{#snippet input()}
<label class="input input-bordered flex w-full items-center gap-2">
<Icon icon="key" />
<input bind:value={password} onchange={onPasswordChange} class="grow" type="password" />
</label>
{/snippet}
{#snippet info()}
<p>Passwords should be at least 12 characters long. Write this down!</p>
{/snippet}
</Field>
{/if}
<div class="flex flex-col">
<Button class="btn {didDownload ? 'btn-neutral' : 'btn-primary'}" onclick={downloadKey}>
Download my key
<Icon icon="arrow-down" />
</Button>
<Button class="btn btn-link no-underline" onclick={toggleUsePassword}>
{#if usePassword}
Nevermind, I want to download the plain version
{:else}
I want to download an encrypted version
{/if}
</Button>
</div>
2025-02-14 18:53:36 +00:00
<ModalFooter>
<Button class="btn btn-link" onclick={back}>
<Icon icon="alt-arrow-left" />
Go back
</Button>
2025-08-18 22:02:17 +00:00
<Button disabled={!didDownload} class="btn btn-primary" type="submit">
Continue
2025-02-14 18:53:36 +00:00
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
</form>