Skip to content

Commit

Permalink
Move initNip46 to commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Staab committed Nov 11, 2024
1 parent ea7b90e commit f6c38fd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 50 deletions.
27 changes: 4 additions & 23 deletions src/app/views/Login.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import {last, prop, objOf} from "ramda"
import {Capacitor} from "@capacitor/core"
import {HANDLER_INFORMATION, NOSTR_CONNECT, normalizeRelayUrl} from "@welshman/util"
import {getNip07, Nip07Signer, getNip55, Nip55Signer, Nip46Broker} from "@welshman/signer"
import {loadHandle, nip46Perms, addSession} from "@welshman/app"
import {getNip07, Nip07Signer, getNip55, Nip55Signer} from "@welshman/signer"
import {loadHandle} from "@welshman/app"
import {parseJson} from "src/util/misc"
import {appName} from "src/partials/state"
import {showWarning} from "src/partials/Toast.svelte"
import Anchor from "src/partials/Anchor.svelte"
import SearchSelect from "src/partials/SearchSelect.svelte"
import FlexColumn from "src/partials/FlexColumn.svelte"
import Heading from "src/partials/Heading.svelte"
import {load, loginWithNip07, loginWithNip55} from "src/engine"
import {load, initNip46, loginWithNip07, loginWithNip55} from "src/engine"
import {router} from "src/app/util/router"
import {boot} from "src/app/state"
Expand Down Expand Up @@ -68,31 +68,12 @@
return showWarning("Sorry, we weren't able to find that provider.")
}
const init = Nip46Broker.initiate({
name: appName,
perms: nip46Perms,
relays: handler.relays,
url: import.meta.env.VITE_APP_URL,
image: import.meta.env.VITE_APP_URL + import.meta.env.VITE_APP_LOGO,
abortController,
})
window.open(init.getLink("use.nsec.app"))
const pubkey = await init.result
const pubkey = await initNip46(handler, {abortController})
if (!pubkey) {
return showWarning("Sorry, we weren't able to connect you. Please try again.")
}
addSession({
pubkey,
method: "nip46",
secret: init.clientSecret,
// Goofy legacy stuff, someday this will be gone
handler: {...handler, pubkey},
})
boot()
} finally {
abortController = undefined
Expand Down
48 changes: 24 additions & 24 deletions src/app/views/OnboardingKeys.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import {Capacitor} from "@capacitor/core"
import {assoc} from "@welshman/lib"
import {makeSecret, Nip46Broker} from "@welshman/signer"
import {nip46Perms, updateSession} from "@welshman/app"
import {loadHandle, nip46Perms, updateSession} from "@welshman/app"
import Field from "src/partials/Field.svelte"
import Input from "src/partials/Input.svelte"
import Anchor from "src/partials/Anchor.svelte"
import {showWarning} from "src/partials/Toast.svelte"
Expand All @@ -13,13 +13,14 @@
const handler = {
domain: "nsec.app",
relays: ["wss://relay.nsec.app"],
relays: ["wss://relay.nsec.app/"],
pubkey: "e24a86943d37a91ab485d6f9a7c66097c25ddd67e8bd1b75ed252a3c266cf9bb",
}
const prev = () => setStage("intro")
const broker = Nip46Broker.get({secret: makeSecret(), handler})
const secret = makeSecret()
const broker = Nip46Broker.get({secret, handler})
const next = async () => {
if (state.pubkey) {
Expand All @@ -34,6 +35,12 @@
return showWarning("Please use only numbers and lowercase letters.")
}
const handle = await loadHandle(`${state.username}@${handler.domain}`)
if (handle?.pubkey) {
return showWarning("Sorry, it looks like that username is already taken.")
}
loading = true
try {
Expand All @@ -44,7 +51,7 @@
}
// Now we can log in. Use the user's pubkey for the handler (legacy stuff)
const success = await loginWithNip46("", {...handler, pubkey: state.pubkey})
const success = await loginWithNip46("", {...handler, pubkey: state.pubkey}, secret)
if (!success) {
return showWarning("Something went wrong, please try again!")
Expand All @@ -68,36 +75,29 @@
<p class="text-2xl font-bold">Secure your Keys</p>
</div>
<p>
Keep your account secure by using a remote signer to store your keys. We recommend getting started
with <Anchor external underline href="https://nsec.app">nsec.app</Anchor>.
Nostr uses cryptographic key pairs instead of passwords, which puts you in full control of your
social identity!
</p>
<p>
Enter a username below and click "continue". You'll be redirected to nsec.app and asked to choose
a password.
Handing your keys to every app you log into can be risky though, which is why it's wise to use a <Anchor
underline
modal
href="/help/remote-signers">remote signer</Anchor> to keep them for you.
</p>
<p>
If you'd like to hold your own keys instead, try using
{#if Capacitor.isNativePlatform()}
<Anchor external underline href="https://github.com/greenart7c3/Amber">a signer app</Anchor>.
{:else}
<Anchor
external
underline
href="https://guides.getalby.com/user-guide/alby-account-and-browser-extension/alby-browser-extension/features/nostr"
>a signer extension</Anchor
>.
{/if}
To get you started, we'll redirect you to our favorite signer for beginners:
<Anchor external underline href="https://nsec.app">nsec.app</Anchor>. They'll ask you a couple
questions and send you back here to set up your own profile.
</p>
<div class="flex gap-2">
<Field label="Your User Name">
<Input
bind:value={state.username}
disabled={loading || state.pubkey}
placeholder="Username"
placeholder="nostrnewb27"
class="flex-grow">
<i slot="before" class="fa fa-user-astronaut" />
</Input>
<p>@nsec.app</p>
</div>
</Field>
<div class="flex gap-2">
<Anchor button on:click={prev} disabled={loading}><i class="fa fa-arrow-left" /> Back</Anchor>
<Anchor button accent class="flex-grow" {loading} on:click={next}>Continue</Anchor>
Expand Down
41 changes: 38 additions & 3 deletions src/engine/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
remove,
splitAt,
} from "@welshman/lib"
import type {Nip46Handler} from "@welshman/signer"
import type {Nip46Handler, Nip46InitiateParams} from "@welshman/signer"
import {Nip01Signer, Nip46Broker, Nip59, makeSecret} from "@welshman/signer"
import type {Profile, TrustedEvent} from "@welshman/util"
import {
Expand Down Expand Up @@ -491,8 +491,11 @@ export const loginWithNip07 = pubkey => addSession({method: "nip07", pubkey})
export const loginWithNip55 = (pubkey, pkg) =>
addSession({method: "nip55", pubkey: pubkey, signer: pkg})

export const loginWithNip46 = async (token: string, handler: Nip46Handler) => {
const secret = makeSecret()
export const loginWithNip46 = async (
token: string,
handler: Nip46Handler,
secret = makeSecret(),
) => {
const broker = Nip46Broker.get({secret, handler})
const result = await broker.connect(token, nip46Perms)

Expand All @@ -507,6 +510,38 @@ export const loginWithNip46 = async (token: string, handler: Nip46Handler) => {
return true
}

export const initNip46 = async (
handler: Nip46Handler,
params: Partial<Nip46InitiateParams> = {},
) => {
const init = Nip46Broker.initiate({
perms: nip46Perms,
relays: handler.relays,
url: import.meta.env.VITE_APP_URL,
name: import.meta.env.VITE_APP_NAME,
image: import.meta.env.VITE_APP_URL + import.meta.env.VITE_APP_LOGO,
...params,
})

window.open(init.getLink("use.nsec.app"))

const pubkey = await init.result

if (!pubkey) {
return undefined
}

addSession({
pubkey,
method: "nip46",
secret: init.clientSecret,
// Goofy legacy stuff, someday this will be gone
handler: {...handler, pubkey},
})

return pubkey
}

export const logoutPubkey = pubkey => {
if (session.get().pubkey === pubkey) {
throw new Error("Can't destroy the current session, use logout instead")
Expand Down

0 comments on commit f6c38fd

Please sign in to comment.