remove nip05 login, use nostrconnect instead

This commit is contained in:
Jon Staab
2024-11-12 13:27:19 -08:00
parent 1b15767a17
commit d5412811b2
13 changed files with 1263 additions and 157 deletions

1178
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -33,6 +33,8 @@
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@capacitor/cli": "^6.1.2",
"@capacitor/core": "^6.1.2",
"@noble/curves": "^1.5.0", "@noble/curves": "^1.5.0",
"@noble/hashes": "^1.4.0", "@noble/hashes": "^1.4.0",
"@poppanator/sveltekit-svg": "^4.2.1", "@poppanator/sveltekit-svg": "^4.2.1",
@@ -52,13 +54,13 @@
"@types/throttle-debounce": "^5.0.2", "@types/throttle-debounce": "^5.0.2",
"@vite-pwa/assets-generator": "^0.2.6", "@vite-pwa/assets-generator": "^0.2.6",
"@vite-pwa/sveltekit": "^0.6.6", "@vite-pwa/sveltekit": "^0.6.6",
"@welshman/app": "~0.0.23", "@welshman/app": "~0.0.25",
"@welshman/content": "~0.0.12", "@welshman/content": "~0.0.12",
"@welshman/dvm": "~0.0.10", "@welshman/dvm": "~0.0.10",
"@welshman/feeds": "~0.0.23", "@welshman/feeds": "~0.0.25",
"@welshman/lib": "~0.0.24", "@welshman/lib": "~0.0.24",
"@welshman/net": "~0.0.33", "@welshman/net": "~0.0.35",
"@welshman/signer": "~0.0.11", "@welshman/signer": "~0.0.13",
"@welshman/store": "~0.0.12", "@welshman/store": "~0.0.12",
"@welshman/util": "~0.0.45", "@welshman/util": "~0.0.45",
"daisyui": "^4.12.10", "daisyui": "^4.12.10",
@@ -70,6 +72,7 @@
"nostr-editor": "^0.0.3", "nostr-editor": "^0.0.3",
"nostr-tools": "^2.7.2", "nostr-tools": "^2.7.2",
"prettier-plugin-tailwindcss": "^0.6.5", "prettier-plugin-tailwindcss": "^0.6.5",
"qrcode": "^1.5.4",
"svelte-tiptap": "^1.1.3", "svelte-tiptap": "^1.1.3",
"throttle-debounce": "^5.0.2" "throttle-debounce": "^5.0.2"
} }

View File

@@ -82,7 +82,7 @@
</Tippy> </Tippy>
<div class="flex flex-col"> <div class="flex flex-col">
<LongPress <LongPress
class="chat-bubble mx-1 flex max-w-sm flex-col gap-1 text-left bg-alt cursor-auto" class="bg-alt chat-bubble mx-1 flex max-w-sm cursor-auto flex-col gap-1 text-left"
onLongPress={showMobileMenu}> onLongPress={showMobileMenu}>
{#if showPubkey && event.pubkey !== $pubkey} {#if showPubkey && event.pubkey !== $pubkey}
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">

View File

@@ -27,7 +27,8 @@
<p> <p>
If you don't have a signer yet, <Link external class="link" href="https://nsec.app/" If you don't have a signer yet, <Link external class="link" href="https://nsec.app/"
>nsec.app</Link> >nsec.app</Link>
is a great way to get started. is a great way to get started. You can find more signers on <Link external class="link" href="https://nostrapps.com#signers"
>nostrapps.com</Link>.
</p> </p>
<Button class="btn btn-primary" on:click={() => history.back()}>Got it</Button> <Button class="btn btn-primary" on:click={() => history.back()}>Got it</Button>
</div> </div>

View File

@@ -20,7 +20,7 @@
<p class="text-center">The chat app built for self-hosted communities.</p> <p class="text-center">The chat app built for self-hosted communities.</p>
</div> </div>
<Button on:click={logIn}> <Button on:click={logIn}>
<CardButton> <CardButton class="!btn-primary">
<div slot="icon"><Icon icon="login-2" size={7} /></div> <div slot="icon"><Icon icon="login-2" size={7} /></div>
<div slot="title">Log in</div> <div slot="title">Log in</div>
<div slot="info">If you've been here before, you know the drill.</div> <div slot="info">If you've been here before, you know the drill.</div>

View File

@@ -1,15 +1,13 @@
<script lang="ts"> <script lang="ts">
import {onMount} from "svelte" import {onMount} from "svelte"
import {makeSecret, getNip07, Nip46Broker, getNip55, Nip55Signer} from "@welshman/signer" import {Capacitor} from '@capacitor/core'
import {addSession, loadHandle, nip46Perms, type Session} from "@welshman/app" import {getNip07, getNip55, Nip55Signer} from "@welshman/signer"
import {addSession, type Session} from "@welshman/app"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import Tippy from "@lib/components/Tippy.svelte" import Link from "@lib/components/Link.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import Divider from "@lib/components/Divider.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import SignUp from "@app/components/SignUp.svelte" import SignUp from "@app/components/SignUp.svelte"
import InfoNostr from "@app/components/InfoNostr.svelte" import InfoNostr from "@app/components/InfoNostr.svelte"
import LogInInfoRemoteSigner from "@app/components/LogInInfoRemoteSigner.svelte"
import LogInBunker from "@app/components/LogInBunker.svelte" import LogInBunker from "@app/components/LogInBunker.svelte"
import {pushModal, clearModals} from "@app/modal" import {pushModal, clearModals} from "@app/modal"
import {PLATFORM_NAME} from "@app/state" import {PLATFORM_NAME} from "@app/state"
@@ -37,45 +35,6 @@
clearModals() clearModals()
} }
const loginWithNip46 = withLoading(async () => {
const rootHandle = await loadHandle(`_@${domain}`)
if (!rootHandle?.pubkey) {
return pushToast({
theme: "error",
message: "Sorry, we couldn't find that remote signer.",
})
}
const secret = makeSecret()
const {pubkey, nip46, relays = []} = (await loadHandle(`${username}@${domain}`)) || {}
if (!pubkey) {
return pushToast({
theme: "error",
message: "Sorry, it looks like you don't have an account yet. Try signing up instead.",
})
}
const handler = {
domain,
pubkey: rootHandle.pubkey,
relays: rootHandle.nip46 || rootHandle.relays || nip46 || relays,
}
// Gotta use user pubkey as the handler pubkey for historical reasons
const broker = Nip46Broker.get({secret, handler: {...handler, pubkey}})
if (await broker.connect("", nip46Perms)) {
await onSuccess({method: "nip46", pubkey, secret, handler: {...handler, pubkey}}, relays)
} else {
pushToast({
theme: "error",
message: "Something went wrong! Please try again.",
})
}
})
const loginWithNip07 = withLoading(async () => { const loginWithNip07 = withLoading(async () => {
const pubkey = await getNip07()?.getPublicKey() const pubkey = await getNip07()?.getPublicKey()
@@ -105,55 +64,30 @@
const loginWithBunker = () => pushModal(LogInBunker) const loginWithBunker = () => pushModal(LogInBunker)
let username = ""
let domain = "nsec.app"
let loading = false let loading = false
let signers: any[] = [] let signers: any[] = []
let hasNativeSigner = Boolean(getNip07())
onMount(async () => { onMount(async () => {
signers = await getNip55() if (Capacitor.isNativePlatform()) {
signers = await getNip55()
if (signers.length > 0) {
hasNativeSigner = true
}
}
}) })
</script> </script>
<form class="column gap-4" on:submit|preventDefault={loginWithNip46}> <div class="column gap-4">
<h1 class="heading">Log in with Nostr</h1> <h1 class="heading">Log in with Nostr</h1>
<p class="m-auto max-w-sm text-center"> <p class="m-auto max-w-sm text-center">
{PLATFORM_NAME} is built using the {PLATFORM_NAME} is built using the
<Button class="link" on:click={() => pushModal(InfoNostr)}>nostr protocol</Button>, which allows <Button class="link" on:click={() => pushModal(InfoNostr)}>nostr protocol</Button>, which allows
you to own your social identity. you to own your social identity.
</p> </p>
<div class="grid grid-cols-3 items-center gap-3">
<p class="font-bold">Username</p>
<label class="input input-bordered col-span-2 flex w-full items-center gap-2">
<Icon icon="user-circle" />
<input
bind:value={username}
disabled={loading}
class="grow"
type="text"
placeholder="username" />
</label>
<Tippy component={LogInInfoRemoteSigner} params={{interactive: true}}>
<p class="flex cursor-pointer items-center gap-2 font-bold">
Remote Signer
<Icon icon="info-circle" class="opacity-50" />
</p>
</Tippy>
<label class="input input-bordered col-span-2 flex w-full items-center gap-2">
<Icon icon="key-minimalistic-square-3" />
<input bind:value={domain} disabled={loading} class="grow" type="text" />
</label>
</div>
<Button type="submit" class="btn btn-primary flex-grow" disabled={!username || loading}>
<Spinner {loading}>Log In</Spinner>
<Icon icon="alt-arrow-right" />
</Button>
<Divider>Or</Divider>
{#if getNip07()} {#if getNip07()}
<Button <Button disabled={loading} on:click={loginWithNip07} class="btn btn-primary">
disabled={loading}
on:click={loginWithNip07}
class="btn {username ? 'btn-neutral' : 'btn-primary'}">
<Icon icon="widget" /> <Icon icon="widget" />
Log in with Extension Log in with Extension
</Button> </Button>
@@ -164,12 +98,22 @@
Log in with {app.name} Log in with {app.name}
</Button> </Button>
{/each} {/each}
<Button disabled={loading} on:click={loginWithBunker} class="btn btn-neutral"> <Button
disabled={loading}
on:click={loginWithBunker}
class="btn {hasNativeSigner ? 'btn-neutral' : 'btn-primary'}">
<Icon icon="cpu" /> <Icon icon="cpu" />
Log in with Bunker Link Log in with Remote Signer
</Button> </Button>
<Link
external
href="https://nostrapps.com#signers"
class="btn {hasNativeSigner ? '' : 'btn-neutral'}">
<Icon icon="compass" />
Browse Signer Apps
</Link>
<div class="text-sm"> <div class="text-sm">
Need an account? Need an account?
<Button class="link" on:click={signUp}>Register instead</Button> <Button class="link" on:click={signUp}>Register instead</Button>
</div> </div>
</form> </div>

View File

@@ -1,22 +1,40 @@
<script lang="ts"> <script lang="ts">
import {Nip46Broker} from "@welshman/signer" import {Nip46Broker} from "@welshman/signer"
import {nip46Perms, addSession} from "@welshman/app"
import {slideAndFade} from '@lib/transition'
import Spinner from "@lib/components/Spinner.svelte" import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import Field from "@lib/components/Field.svelte" import Field from "@lib/components/Field.svelte"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte" import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte" import ModalFooter from "@lib/components/ModalFooter.svelte"
import QRCode from "@app/components/QRCode.svelte"
import InfoBunker from "@app/components/InfoBunker.svelte" import InfoBunker from "@app/components/InfoBunker.svelte"
import {loginWithNip46, loadUserData} from "@app/commands" import {loginWithNip46, loadUserData} from "@app/commands"
import {pushModal, clearModals} from "@app/modal" import {pushModal, clearModals} from "@app/modal"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
import {PLATFORM_NAME} from "@app/state" import {PLATFORM_URL, PLATFORM_NAME, PLATFORM_LOGO, SIGNER_RELAYS} from "@app/state"
const back = () => history.back() const back = () => history.back()
const abortController = new AbortController()
const init = Nip46Broker.initiate({
perms: nip46Perms,
url: PLATFORM_URL,
name: PLATFORM_NAME,
relays: SIGNER_RELAYS,
image: PLATFORM_LOGO,
abortController,
})
const onSubmit = async () => { const onSubmit = async () => {
const {pubkey, token, relays} = Nip46Broker.parseBunkerLink(bunker) const {pubkey, token, relays} = Nip46Broker.parseBunkerLink(bunker)
if (loading) {
return
}
if (!pubkey || relays.length === 0) { if (!pubkey || relays.length === 0) {
return pushToast({ return pushToast({
theme: "error", theme: "error",
@@ -34,6 +52,8 @@
}) })
} }
abortController.abort()
await loadUserData(pubkey) await loadUserData(pubkey)
} finally { } finally {
loading = false loading = false
@@ -44,18 +64,42 @@
let bunker = "" let bunker = ""
let loading = false let loading = false
init.result.then(async pubkey => {
if (pubkey) {
loading = true
addSession({
pubkey,
method: "nip46",
secret: init.clientSecret,
handler: {pubkey, relays: SIGNER_RELAYS},
})
await loadUserData(pubkey)
clearModals()
}
})
</script> </script>
<form class="column gap-4" on:submit|preventDefault={onSubmit}> <form class="column gap-4" on:submit|preventDefault={onSubmit}>
<ModalHeader> <ModalHeader>
<div slot="title">Log In</div> <div slot="title">Log In</div>
<div slot="info">Connect your signer app with {PLATFORM_NAME} using a bunker link.</div> <div slot="info">
Connect your signer by scanning the QR code below or pasting a bunker link.
</div>
</ModalHeader> </ModalHeader>
{#if !loading}
<div class="m-auto w-xs" out:slideAndFade>
<QRCode code={init.nostrconnect} />
</div>
{/if}
<Field> <Field>
<p slot="label">Bunker Link*</p> <p slot="label">Bunker Link*</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input"> <label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="cpu" /> <Icon icon="cpu" />
<input bind:value={bunker} class="grow" placeholder="bunker://" /> <input disabled={loading} bind:value={bunker} class="grow" placeholder="bunker://" />
</label> </label>
<p slot="info"> <p slot="info">
A login link provided by a nostr signing app. A login link provided by a nostr signing app.
@@ -63,7 +107,7 @@
</p> </p>
</Field> </Field>
<ModalFooter> <ModalFooter>
<Button class="btn btn-link" on:click={back}> <Button class="btn btn-link" on:click={back} disabled={loading}>
<Icon icon="alt-arrow-left" /> <Icon icon="alt-arrow-left" />
Go back Go back
</Button> </Button>

View File

@@ -1,10 +0,0 @@
<script lang="ts">
import Button from "@lib/components/Button.svelte"
import InfoKeys from "@app/components/InfoKeys.svelte"
import {pushModal} from "@app/modal"
</script>
<p class="card2 bg-alt text-sm transition-all">
A remote signer is a simple way to protect your private key.
<Button class="link" on:click={() => pushModal(InfoKeys)}>What is a private key?</Button>
</p>

View File

@@ -4,7 +4,6 @@
import {deriveEvents} from "@welshman/store" import {deriveEvents} from "@welshman/store"
import {feedFromFilter} from "@welshman/feeds" import {feedFromFilter} from "@welshman/feeds"
import {NOTE, getAncestorTags} from "@welshman/util" import {NOTE, getAncestorTags} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import {repository, createFeedController} from "@welshman/app" import {repository, createFeedController} from "@welshman/app"
import {createScroller} from "@lib/html" import {createScroller} from "@lib/html"
import Spinner from "@lib/components/Spinner.svelte" import Spinner from "@lib/components/Spinner.svelte"
@@ -24,7 +23,7 @@
element, element,
delay: 300, delay: 300,
threshold: 3000, threshold: 3000,
onScroll: () => ctrl.load(5) onScroll: () => ctrl.load(5),
}) })
return () => scroller.stop() return () => scroller.stop()

View File

@@ -0,0 +1,31 @@
<script lang="ts">
import QRCode from "qrcode"
import {onMount} from "svelte"
import Button from "@lib/components/Button.svelte"
import {clip} from "@app/toast"
export let code
let canvas, wrapper
let scale = 0.1
let height = null
onMount(() => {
QRCode.toCanvas(canvas, code)
const wrapperRect = wrapper.getBoundingClientRect()
const canvasRect = canvas.getBoundingClientRect()
scale = wrapperRect.width / (canvasRect.width * 10)
height = canvasRect.width * 10 * scale
})
</script>
<Button on:click={() => clip(code)}>
<div bind:this={wrapper} style={`height: ${height}px`}>
<canvas
class="rounded-box"
bind:this={canvas}
style={`transform-origin: top left; transform: scale(${scale}, ${scale})`} />
</div>
</Button>

View File

@@ -77,7 +77,11 @@ export const INDEXER_RELAYS = [
"wss://relay.nostr.band/", "wss://relay.nostr.band/",
] ]
export const PLATFORM_LOGO = "/pwa-192x192.png" export const SIGNER_RELAYS = ["wss://relay.nsec.app/", "wss://bucket.coracle.social/"]
export const PLATFORM_URL = window.location.origin
export const PLATFORM_LOGO = PLATFORM_URL + "/pwa-192x192.png"
export const PLATFORM_NAME = import.meta.env.VITE_PLATFORM_NAME export const PLATFORM_NAME = import.meta.env.VITE_PLATFORM_NAME

View File

@@ -13,7 +13,7 @@
$: url = decodeRelay($page.params.relay) $: url = decodeRelay($page.params.relay)
const ifLet = <T>(x: T | undefined, f: (x: T) => void) => x === undefined ? undefined : f(x) const ifLet = <T,>(x: T | undefined, f: (x: T) => void) => (x === undefined ? undefined : f(x))
const checkConnection = async () => { const checkConnection = async () => {
ifLet(await checkRelayConnection(url), error => { ifLet(await checkRelayConnection(url), error => {

View File

@@ -35,14 +35,14 @@
onMount(() => { onMount(() => {
const ctrl = createFeedController({ const ctrl = createFeedController({
feed: feedsFromFilters(filters), feed,
onExhausted: () => { onExhausted: () => {
loading = false loading = false
}, },
}) })
const unsub = subscribePersistent({ const unsub = subscribePersistent({
filters: filters.map(assoc('since', ago(30))), filters: filters.map(assoc("since", ago(30))),
relays: [url], relays: [url],
}) })
@@ -50,7 +50,11 @@
element, element,
delay: 300, delay: 300,
threshold: 3000, threshold: 3000,
onScroll: () => ctrl.load(5), onScroll: async () => {
limit += 5
await ctrl.load(5)
},
}) })
return () => { return () => {