forked from coracle/flotilla
131 lines
4.2 KiB
Svelte
131 lines
4.2 KiB
Svelte
<script lang="ts">
|
|
import {bytesToHex} from "@welshman/lib"
|
|
import {loginWithNip01} from "@welshman/app"
|
|
import {decrypt} from "nostr-tools/nip49"
|
|
import {preventDefault} from "@lib/html"
|
|
import {nsecDecode} from "@lib/util"
|
|
import Spinner from "@lib/components2/Spinner.svelte"
|
|
import Link from "@lib/components2/Link.svelte"
|
|
import Button from "@lib/components2/Button.svelte"
|
|
import FieldInline from "@lib/components2/FieldInline.svelte"
|
|
import Alert from "@lib/components2/Alert.svelte"
|
|
import Key from "@assets/icons/key.svg?dataurl"
|
|
import Danger from "@assets/icons/danger-triangle.svg?dataurl"
|
|
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
|
|
import AltArrowRight from "@assets/icons/alt-arrow-right.svg?dataurl"
|
|
import Icon from "@lib/components2/Icon.svelte"
|
|
import Modal from "@lib/components2/Modal.svelte"
|
|
import ModalBody from "@lib/components2/ModalBody.svelte"
|
|
import ModalHeader from "@lib/components2/ModalHeader.svelte"
|
|
import ModalTitle from "@lib/components2/ModalTitle.svelte"
|
|
import ModalSubtitle from "@lib/components2/ModalSubtitle.svelte"
|
|
import ModalFooter from "@lib/components2/ModalFooter.svelte"
|
|
import {clearModals} from "@app/modal"
|
|
import {setChecked} from "@app/notifications"
|
|
import {pushToast} from "@app/toast"
|
|
|
|
let loading = $state(false)
|
|
let keyInput = $state("")
|
|
let password = $state("")
|
|
|
|
const back = () => history.back()
|
|
|
|
const isHex = $derived(keyInput.match(/^[0-9a-f]{64}$/i))
|
|
|
|
const isNsec = $derived(keyInput.startsWith("nsec1"))
|
|
|
|
const isNcryptsec = $derived(keyInput.startsWith("ncryptsec1"))
|
|
|
|
const canSubmit = $derived(!loading && (isHex || isNsec || isNcryptsec))
|
|
|
|
const onSubmit = async () => {
|
|
loading = true
|
|
|
|
try {
|
|
let secret: string
|
|
|
|
if (isNcryptsec) {
|
|
secret = bytesToHex(decrypt(keyInput, password))
|
|
} else if (isNsec) {
|
|
secret = nsecDecode(keyInput)
|
|
} else if (isHex) {
|
|
secret = keyInput.toLowerCase()
|
|
} else {
|
|
return pushToast({
|
|
theme: "error",
|
|
message: "Invalid key format. Please enter a hex key, nsec, or ncryptsec.",
|
|
})
|
|
}
|
|
|
|
loginWithNip01(secret)
|
|
setChecked("*")
|
|
clearModals()
|
|
} catch (e) {
|
|
console.error(e)
|
|
|
|
pushToast({
|
|
theme: "error",
|
|
message: isNcryptsec
|
|
? "Failed to decrypt key. Please check your password."
|
|
: "Invalid key format. Please check your input.",
|
|
})
|
|
} finally {
|
|
loading = false
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<Modal tag="form" onsubmit={preventDefault(onSubmit)}>
|
|
<ModalBody>
|
|
<ModalHeader>
|
|
<ModalTitle>Log In with Key</ModalTitle>
|
|
<ModalSubtitle>Enter your nostr private key to log in.</ModalSubtitle>
|
|
</ModalHeader>
|
|
<FieldInline>
|
|
{#snippet label()}
|
|
<p>Your Key*</p>
|
|
{/snippet}
|
|
{#snippet input()}
|
|
<label class="input-cl input-cl-group w-full">
|
|
<Icon icon={Key} />
|
|
<input type="password" bind:value={keyInput} placeholder="nsec1..." />
|
|
</label>
|
|
{/snippet}
|
|
</FieldInline>
|
|
{#if isNcryptsec}
|
|
<FieldInline>
|
|
{#snippet label()}
|
|
<p>Password*</p>
|
|
{/snippet}
|
|
{#snippet input()}
|
|
<label class="input-cl input-cl-group w-full">
|
|
<Icon icon={Key} />
|
|
<input type="password" bind:value={password} placeholder="Your password" />
|
|
</label>
|
|
{/snippet}
|
|
</FieldInline>
|
|
{/if}
|
|
<Alert variant="warning" class="flex-col text-sm">
|
|
<strong class="flex items-center gap-2">
|
|
<Icon icon={Danger} />
|
|
Please note!
|
|
</strong>
|
|
<p>
|
|
Logging in this way is not a best practice. For better security, please consider using a
|
|
<Link external styled href="https://nostrapps.com#signers">signer app</Link>
|
|
to keep your keys safe.
|
|
</p>
|
|
</Alert>
|
|
</ModalBody>
|
|
<ModalFooter>
|
|
<Button variant="ghost" onclick={back} disabled={loading}>
|
|
<Icon icon={AltArrowLeft} />
|
|
Go back
|
|
</Button>
|
|
<Button type="submit" variant="primary" disabled={!canSubmit}>
|
|
<Spinner {loading}>Log in</Spinner>
|
|
<Icon icon={AltArrowRight} />
|
|
</Button>
|
|
</ModalFooter>
|
|
</Modal>
|