flotilla/src/app/components/LogInBunker.svelte

157 lines
4.5 KiB
Svelte
Raw Normal View History

<script lang="ts">
import {onMount, onDestroy} from "svelte"
2025-03-20 19:55:12 +00:00
import type {Nip46ResponseWithResult} from "@welshman/signer"
2025-12-01 16:45:35 +00:00
import {Nip46Broker} from "@welshman/signer"
import {makeSecret} from "@welshman/util"
2025-04-11 18:51:15 +00:00
import {loginWithNip01, loginWithNip46} from "@welshman/app"
2025-02-04 03:28:29 +00:00
import {preventDefault} from "@lib/html"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
2025-09-10 22:26:39 +00:00
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
import AltArrowRight from "@assets/icons/alt-arrow-right.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
import BunkerConnect from "@app/components/BunkerConnect.svelte"
2025-03-20 19:55:12 +00:00
import BunkerUrl from "@app/components/BunkerUrl.svelte"
import {Nip46Controller} from "@app/util/nip46"
import {clearModals} from "@app/util/modal"
import {setChecked} from "@app/util/notifications"
import {pushToast} from "@app/util/toast"
import {SIGNER_RELAYS, NIP46_PERMS} from "@app/core/state"
const back = () => {
if (mode === "connect") {
selectBunker()
} else {
history.back()
}
}
const controller = new Nip46Controller({
2025-03-20 19:55:12 +00:00
onNostrConnect: async (response: Nip46ResponseWithResult) => {
2025-04-11 18:51:15 +00:00
const pubkey = await controller.broker.getPublicKey()
2025-10-01 17:06:21 +00:00
loginWithNip46(pubkey, controller.clientSecret, response.event.pubkey, SIGNER_RELAYS)
2025-03-20 19:55:12 +00:00
setChecked("*")
clearModals()
},
})
const {loading, bunker} = controller
const onSubmit = async () => {
if ($loading) return
try {
const {signerPubkey, connectSecret, relays} = Nip46Broker.parseBunkerUrl($bunker)
2025-09-22 17:35:31 +00:00
if (!signerPubkey) {
return pushToast({
theme: "error",
message: "Sorry, it looks like that's an invalid bunker link.",
})
}
2025-09-22 17:35:31 +00:00
if (relays.length === 0) {
return pushToast({
theme: "error",
message: "That bunker link does not include any relays.",
})
}
controller.loading.set(true)
2025-03-20 19:55:12 +00:00
const {clientSecret} = controller
2025-05-07 20:53:58 +00:00
const broker = new Nip46Broker({relays, clientSecret, signerPubkey})
2025-04-11 18:51:15 +00:00
const result = await broker.connect(connectSecret, NIP46_PERMS)
const pubkey = await broker.getPublicKey()
2024-12-03 00:21:54 +00:00
2025-04-11 18:51:15 +00:00
// TODO: remove ack result
if (pubkey && ["ack", connectSecret].includes(result)) {
2025-05-07 20:53:58 +00:00
broker.cleanup()
2025-03-20 19:55:12 +00:00
controller.stop()
2025-04-11 18:51:15 +00:00
loginWithNip46(pubkey, clientSecret, signerPubkey, relays)
2024-12-03 00:21:54 +00:00
} else {
2024-10-29 21:40:37 +00:00
return pushToast({
theme: "error",
message: "Something went wrong, please try again!",
})
}
} catch (e) {
console.error(e)
return pushToast({
theme: "error",
message: "Something went wrong, please try again!",
})
2024-10-29 21:40:37 +00:00
} finally {
controller.loading.set(false)
}
2024-10-29 21:40:37 +00:00
clearModals()
}
const selectConnect = () => {
2025-09-22 17:35:31 +00:00
controller.loading.set(false)
mode = "connect"
}
const selectBunker = () => {
mode = "bunker"
}
let mode: string = $state("bunker")
2025-02-04 03:28:29 +00:00
$effect(() => {
// For testing and for play store reviewers
if ($bunker === "reviewkey") {
2025-04-11 18:51:15 +00:00
loginWithNip01(makeSecret())
}
2025-02-04 00:37:14 +00:00
})
onMount(() => {
controller.start()
})
onDestroy(() => {
controller.stop()
})
</script>
2026-01-30 22:14:12 +00:00
<Modal tag="form" onsubmit={preventDefault(onSubmit)}>
<ModalBody>
<ModalHeader>
{#snippet title()}
<div>Log In with a Signer</div>
{/snippet}
{#snippet info()}
<div>Using a remote signer app helps you keep your keys safe.</div>
{/snippet}
</ModalHeader>
<div class:hidden={mode !== "bunker"}></div>
{#if mode === "connect"}
<BunkerConnect {controller} />
{:else}
<BunkerUrl {controller} />
<Button class="btn {$bunker ? 'btn-neutral' : 'btn-primary'}" onclick={selectConnect}
>Log in with a QR code instead</Button>
{/if}
</ModalBody>
<ModalFooter>
<Button class="btn btn-link" onclick={back} disabled={$loading}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
Go back
</Button>
{#if mode === "bunker"}
<Button type="submit" class="btn btn-primary" disabled={$loading || !$bunker}>
<Spinner loading={$loading}>Next</Spinner>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowRight} />
</Button>
{/if}
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>