mirror of
https://github.com/coracle-social/flotilla.git
synced 2025-12-11 03:17:02 +00:00
Use new welshman nip46 stuff
This commit is contained in:
30
package-lock.json
generated
30
package-lock.json
generated
@@ -31,13 +31,13 @@
|
||||
"@types/throttle-debounce": "^5.0.2",
|
||||
"@vite-pwa/assets-generator": "^0.2.6",
|
||||
"@vite-pwa/sveltekit": "^0.6.6",
|
||||
"@welshman/app": "~0.0.30",
|
||||
"@welshman/app": "~0.0.31",
|
||||
"@welshman/content": "~0.0.12",
|
||||
"@welshman/dvm": "~0.0.10",
|
||||
"@welshman/feeds": "~0.0.26",
|
||||
"@welshman/lib": "~0.0.27",
|
||||
"@welshman/lib": "~0.0.28",
|
||||
"@welshman/net": "~0.0.39",
|
||||
"@welshman/signer": "~0.0.15",
|
||||
"@welshman/signer": "~0.0.16",
|
||||
"@welshman/store": "~0.0.12",
|
||||
"@welshman/util": "~0.0.46",
|
||||
"daisyui": "^4.12.10",
|
||||
@@ -4664,16 +4664,16 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@welshman/app": {
|
||||
"version": "0.0.30",
|
||||
"resolved": "https://registry.npmjs.org/@welshman/app/-/app-0.0.30.tgz",
|
||||
"integrity": "sha512-rD/tZCNR9cdK2Vq4/t5px3mNncBOxrpIWFkv1JLKRiuktAzfpWGWihBlDYT33ts6qQAupU/fF/42+NpMkgxWoQ==",
|
||||
"version": "0.0.31",
|
||||
"resolved": "https://registry.npmjs.org/@welshman/app/-/app-0.0.31.tgz",
|
||||
"integrity": "sha512-Z6h14hbaJ8OFtmxtD4R+bmVDUFUQnctbBWUWxA3ZpRLeGN8xn8zRXXIp5cfKBbRtoAHiPwvDYRrbArsal6md1w==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@welshman/dvm": "~0.0.10",
|
||||
"@welshman/feeds": "~0.0.26",
|
||||
"@welshman/lib": "~0.0.27",
|
||||
"@welshman/lib": "~0.0.28",
|
||||
"@welshman/net": "~0.0.39",
|
||||
"@welshman/signer": "~0.0.15",
|
||||
"@welshman/signer": "~0.0.16",
|
||||
"@welshman/store": "~0.0.12",
|
||||
"@welshman/util": "~0.0.46",
|
||||
"fuse.js": "^7.0.0",
|
||||
@@ -4713,9 +4713,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@welshman/lib": {
|
||||
"version": "0.0.27",
|
||||
"resolved": "https://registry.npmjs.org/@welshman/lib/-/lib-0.0.27.tgz",
|
||||
"integrity": "sha512-VVU4kpEFxvr1XWwkEauOp16CgTYyPg42D1mJiuiqo0I847ZjHIjKYXC+ZY1YePJTPvg1VSiAVG/bSheYexRR7w==",
|
||||
"version": "0.0.28",
|
||||
"resolved": "https://registry.npmjs.org/@welshman/lib/-/lib-0.0.28.tgz",
|
||||
"integrity": "sha512-9BJm7j2NtPveUtlyiJ/b/LhW93ShR/7csmWl0nUXTbra6be3/7vz8RpN4xl+tBmnvgkOSFjYw5y+srKMZ5f2YA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@scure/base": "^1.1.6",
|
||||
@@ -4738,12 +4738,12 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@welshman/signer": {
|
||||
"version": "0.0.15",
|
||||
"resolved": "https://registry.npmjs.org/@welshman/signer/-/signer-0.0.15.tgz",
|
||||
"integrity": "sha512-OBBx4FP73Jcto2wM/R0rseFtZiY9T1qP0NkJ1uttxLnmGIVhBA7yG1UnV1amK3gcMW/gZ7snXNJC76Vg2/NbnQ==",
|
||||
"version": "0.0.16",
|
||||
"resolved": "https://registry.npmjs.org/@welshman/signer/-/signer-0.0.16.tgz",
|
||||
"integrity": "sha512-mztQBZotetvDbRGMbkdGJcciw7cPlY2n1YFEvd8hcUqnzD7cysXnuNPZgli1EbpUhtmlDwiGIcBFBVuzYdlTIA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@welshman/lib": "~0.0.27",
|
||||
"@welshman/lib": "~0.0.28",
|
||||
"@welshman/net": "~0.0.39",
|
||||
"@welshman/util": "~0.0.46",
|
||||
"nostr-tools": "^2.7.2"
|
||||
|
||||
@@ -59,13 +59,13 @@
|
||||
"@types/throttle-debounce": "^5.0.2",
|
||||
"@vite-pwa/assets-generator": "^0.2.6",
|
||||
"@vite-pwa/sveltekit": "^0.6.6",
|
||||
"@welshman/app": "~0.0.30",
|
||||
"@welshman/app": "~0.0.31",
|
||||
"@welshman/content": "~0.0.12",
|
||||
"@welshman/dvm": "~0.0.10",
|
||||
"@welshman/feeds": "~0.0.26",
|
||||
"@welshman/lib": "~0.0.27",
|
||||
"@welshman/lib": "~0.0.28",
|
||||
"@welshman/net": "~0.0.39",
|
||||
"@welshman/signer": "~0.0.15",
|
||||
"@welshman/signer": "~0.0.16",
|
||||
"@welshman/store": "~0.0.12",
|
||||
"@welshman/util": "~0.0.46",
|
||||
"daisyui": "^4.12.10",
|
||||
|
||||
@@ -24,7 +24,6 @@ import type {TrustedEvent, EventTemplate, List} from "@welshman/util"
|
||||
import type {SubscribeRequestWithHandlers, Subscription} from "@welshman/net"
|
||||
import {PublishStatus, AuthStatus, SocketStatus, SubscriptionEvent} from "@welshman/net"
|
||||
import {Nip59, makeSecret, stamp, Nip46Broker} from "@welshman/signer"
|
||||
import type {Nip46Handler} from "@welshman/signer"
|
||||
import {
|
||||
pubkey,
|
||||
signer,
|
||||
@@ -120,18 +119,30 @@ export const subscribePersistent = (request: SubscribeRequestWithHandlers) => {
|
||||
|
||||
// Log in
|
||||
|
||||
export const loginWithNip46 = async (token: string, handler: Nip46Handler) => {
|
||||
const secret = makeSecret()
|
||||
const broker = Nip46Broker.get({secret, handler})
|
||||
const result = await broker.connect(token, NIP46_PERMS)
|
||||
export const loginWithNip46 = async ({
|
||||
relays,
|
||||
signerPubkey,
|
||||
clientSecret = makeSecret(),
|
||||
connectSecret = "",
|
||||
}: {
|
||||
relays: string[]
|
||||
signerPubkey: string
|
||||
clientSecret?: string
|
||||
connectSecret?: string
|
||||
}) => {
|
||||
const broker = Nip46Broker.get({relays, clientSecret, signerPubkey})
|
||||
const result = await broker.connect("", connectSecret, NIP46_PERMS)
|
||||
|
||||
if (!result) return false
|
||||
// TODO: remove ack result
|
||||
if (!["ack", connectSecret].includes(result)) return false
|
||||
|
||||
const pubkey = await broker.getPublicKey()
|
||||
|
||||
if (!pubkey) return false
|
||||
|
||||
addSession({method: "nip46", pubkey, secret, handler})
|
||||
const handler = {relays, pubkey: signerPubkey}
|
||||
|
||||
addSession({method: "nip46", pubkey, secret: clientSecret, handler})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import {onDestroy} from "svelte"
|
||||
import {Nip46Broker} from "@welshman/signer"
|
||||
import {addSession} from "@welshman/app"
|
||||
import {onMount, onDestroy} from "svelte"
|
||||
import {Nip46Broker, makeSecret} from "@welshman/signer"
|
||||
import {addSession, pubkey} from "@welshman/app"
|
||||
import {slideAndFade} from "@lib/transition"
|
||||
import Spinner from "@lib/components/Spinner.svelte"
|
||||
import Button from "@lib/components/Button.svelte"
|
||||
@@ -17,27 +17,22 @@
|
||||
import {pushToast} from "@app/toast"
|
||||
import {NIP46_PERMS, PLATFORM_URL, PLATFORM_NAME, PLATFORM_LOGO, SIGNER_RELAYS} from "@app/state"
|
||||
|
||||
const back = () => history.back()
|
||||
const clientSecret = makeSecret()
|
||||
|
||||
const abortController = new AbortController()
|
||||
|
||||
const init = Nip46Broker.initiate({
|
||||
perms: NIP46_PERMS,
|
||||
url: PLATFORM_URL,
|
||||
name: PLATFORM_NAME,
|
||||
relays: SIGNER_RELAYS,
|
||||
image: PLATFORM_LOGO,
|
||||
abortController,
|
||||
})
|
||||
const broker = Nip46Broker.get({clientSecret, relays: SIGNER_RELAYS})
|
||||
|
||||
const back = () => history.back()
|
||||
|
||||
const onSubmit = async () => {
|
||||
const {pubkey, token, relays} = Nip46Broker.parseBunkerLink(bunker)
|
||||
const {signerPubkey, connectSecret, relays} = broker.parseBunkerUrl(input)
|
||||
|
||||
if (loading) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!pubkey || relays.length === 0) {
|
||||
if (!signerPubkey || relays.length === 0) {
|
||||
return pushToast({
|
||||
theme: "error",
|
||||
message: "Sorry, it looks like that's an invalid bunker link.",
|
||||
@@ -47,16 +42,16 @@
|
||||
loading = true
|
||||
|
||||
try {
|
||||
if (!(await loginWithNip46(token, {pubkey, relays}))) {
|
||||
if (await loginWithNip46({connectSecret, clientSecret, signerPubkey, relays})) {
|
||||
abortController.abort()
|
||||
|
||||
await loadUserData($pubkey!)
|
||||
} else {
|
||||
return pushToast({
|
||||
theme: "error",
|
||||
message: "Something went wrong, please try again!",
|
||||
})
|
||||
}
|
||||
|
||||
abortController.abort()
|
||||
|
||||
await loadUserData(pubkey)
|
||||
} finally {
|
||||
loading = false
|
||||
}
|
||||
@@ -64,21 +59,48 @@
|
||||
clearModals()
|
||||
}
|
||||
|
||||
let bunker = ""
|
||||
let url = ""
|
||||
let input = ""
|
||||
let loading = false
|
||||
|
||||
init.result.then(async pubkey => {
|
||||
if (pubkey) {
|
||||
onMount(async () => {
|
||||
url = await broker.makeNostrconnectUrl({
|
||||
perms: NIP46_PERMS,
|
||||
url: PLATFORM_URL,
|
||||
name: PLATFORM_NAME,
|
||||
image: PLATFORM_LOGO,
|
||||
})
|
||||
|
||||
let response
|
||||
try {
|
||||
response = await broker.waitForNostrconnect(url, abortController)
|
||||
} catch (errorResponse: any) {
|
||||
if (errorResponse?.error) {
|
||||
pushToast({
|
||||
theme: "error",
|
||||
message: `Received error from signer: ${errorResponse.error}`,
|
||||
})
|
||||
} else if (errorResponse) {
|
||||
console.error(errorResponse)
|
||||
}
|
||||
}
|
||||
|
||||
if (response) {
|
||||
loading = true
|
||||
|
||||
const userPubkey = await broker.getPublicKey()
|
||||
|
||||
addSession({
|
||||
pubkey,
|
||||
method: "nip46",
|
||||
secret: init.clientSecret,
|
||||
handler: {pubkey, relays: SIGNER_RELAYS},
|
||||
pubkey: userPubkey,
|
||||
secret: clientSecret,
|
||||
handler: {
|
||||
pubkey: response.event.pubkey,
|
||||
relays: SIGNER_RELAYS,
|
||||
},
|
||||
})
|
||||
|
||||
await loadUserData(pubkey)
|
||||
await loadUserData(userPubkey)
|
||||
|
||||
setChecked("*")
|
||||
clearModals()
|
||||
@@ -97,16 +119,16 @@
|
||||
Connect your signer by scanning the QR code below or pasting a bunker link.
|
||||
</div>
|
||||
</ModalHeader>
|
||||
{#if !loading}
|
||||
{#if !loading && url}
|
||||
<div class="w-xs m-auto" out:slideAndFade>
|
||||
<QRCode code={init.nostrconnect} />
|
||||
<QRCode code={url} />
|
||||
</div>
|
||||
{/if}
|
||||
<Field>
|
||||
<p slot="label">Bunker Link*</p>
|
||||
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
|
||||
<Icon icon="cpu" />
|
||||
<input disabled={loading} bind:value={bunker} class="grow" placeholder="bunker://" />
|
||||
<input disabled={loading} bind:value={input} class="grow" placeholder="bunker://" />
|
||||
</label>
|
||||
<p slot="info">
|
||||
A login link provided by a nostr signing app.
|
||||
@@ -118,7 +140,7 @@
|
||||
<Icon icon="alt-arrow-left" />
|
||||
Go back
|
||||
</Button>
|
||||
<Button type="submit" class="btn btn-primary" disabled={loading || !bunker}>
|
||||
<Button type="submit" class="btn btn-primary" disabled={loading || !input}>
|
||||
<Spinner {loading}>Next</Spinner>
|
||||
<Icon icon="alt-arrow-right" />
|
||||
</Button>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import {makeSecret, Nip46Broker} from "@welshman/signer"
|
||||
import {addSession, loadHandle} from "@welshman/app"
|
||||
import {loadHandle} from "@welshman/app"
|
||||
import Icon from "@lib/components/Icon.svelte"
|
||||
import Field from "@lib/components/Field.svelte"
|
||||
import Link from "@lib/components/Link.svelte"
|
||||
@@ -13,12 +13,18 @@
|
||||
import {setChecked} from "@app/notifications"
|
||||
import {PLATFORM_NAME, NIP46_PERMS} from "@app/state"
|
||||
import {pushToast} from "@app/toast"
|
||||
import {loginWithNip46} from "@app/commands"
|
||||
|
||||
const relays = ["wss://relay.nsec.app"]
|
||||
|
||||
const signerDomain = "nsec.app"
|
||||
|
||||
const signerPubkey = "e24a86943d37a91ab485d6f9a7c66097c25ddd67e8bd1b75ed252a3c266cf9bb"
|
||||
|
||||
const login = () => pushModal(LogIn)
|
||||
|
||||
const trySignup = async () => {
|
||||
const secret = makeSecret()
|
||||
const handle = await loadHandle(`${username}@${handler.domain}`)
|
||||
const handle = await loadHandle(`${username}@${signerDomain}`)
|
||||
|
||||
if (handle?.pubkey) {
|
||||
return pushToast({
|
||||
@@ -27,30 +33,31 @@
|
||||
})
|
||||
}
|
||||
|
||||
const signupBroker = Nip46Broker.get({secret, handler})
|
||||
const pubkey = await signupBroker.createAccount(username, NIP46_PERMS)
|
||||
const clientSecret = makeSecret()
|
||||
const broker = Nip46Broker.get({relays, clientSecret, signerPubkey})
|
||||
|
||||
if (!pubkey) {
|
||||
const userPubkey = await broker.createAccount(username, signerDomain, NIP46_PERMS)
|
||||
|
||||
if (!userPubkey) {
|
||||
return pushToast({
|
||||
theme: "error",
|
||||
message: "Sorry, it looks like something went wrong. Please try again.",
|
||||
})
|
||||
}
|
||||
|
||||
// Gotta use user pubkey as the handler pubkey for historical reasons
|
||||
const loginBroker = Nip46Broker.get({secret, handler: {...handler, pubkey}})
|
||||
// Now we can log in. Use the user's pubkey for the handler (legacy stuff)
|
||||
const success = await loginWithNip46({relays, signerPubkey: userPubkey, clientSecret})
|
||||
|
||||
if (await loginBroker.connect("", NIP46_PERMS)) {
|
||||
addSession({method: "nip46", pubkey, secret, handler: {...handler, pubkey}})
|
||||
pushToast({message: "Successfully logged in!"})
|
||||
setChecked("*")
|
||||
clearModals()
|
||||
} else {
|
||||
pushToast({
|
||||
if (!success) {
|
||||
return pushToast({
|
||||
theme: "error",
|
||||
message: "Something went wrong! Please try again.",
|
||||
message: "Sorry, it looks like something went wrong. Please try again.",
|
||||
})
|
||||
}
|
||||
|
||||
pushToast({message: "Successfully logged in!"})
|
||||
setChecked("*")
|
||||
clearModals()
|
||||
}
|
||||
|
||||
const signup = async () => {
|
||||
|
||||
Reference in New Issue
Block a user