flotilla/src/app/components/WalletConnect.svelte

192 lines
5.6 KiB
Svelte
Raw Normal View History

2025-07-04 13:22:19 +00:00
<script lang="ts">
2025-07-15 22:49:26 +00:00
import {debounce} from "throttle-debounce"
2025-07-04 13:22:19 +00:00
import {nwc} from "@getalby/sdk"
2026-07-28 16:16:26 +00:00
import {sleep} from "@welshman/lib"
import {WalletType} from "@welshman/util"
import type {NWCInfo} from "@welshman/util"
2025-07-04 13:22:19 +00:00
import Link from "@lib/components/Link.svelte"
2025-09-10 22:26:39 +00:00
import Cpu from "@assets/icons/cpu-bolt.svg?dataurl"
import Lock from "@assets/icons/lock-keyhole.svg?dataurl"
import QrCode from "@assets/icons/qr-code.svg?dataurl"
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
import AltArrowRight from "@assets/icons/alt-arrow-right.svg?dataurl"
2025-07-04 13:22:19 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2025-07-15 22:49:26 +00:00
import Scanner from "@lib/components/Scanner.svelte"
2025-07-04 13:22:19 +00:00
import Spinner from "@lib/components/Spinner.svelte"
import Field from "@lib/components/Field.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
2025-07-04 13:22:19 +00:00
import ModalHeader from "@lib/components/ModalHeader.svelte"
2026-02-02 18:09:14 +00:00
import ModalTitle from "@lib/components/ModalTitle.svelte"
import ModalSubtitle from "@lib/components/ModalSubtitle.svelte"
2025-07-04 13:22:19 +00:00
import ModalFooter from "@lib/components/ModalFooter.svelte"
2026-07-28 16:16:26 +00:00
import Divider from "@lib/components/Divider.svelte"
import WalletAsReceivingAddress from "@app/components/WalletAsReceivingAddress.svelte"
import {deriveUserItem, profiles} from "@app/core"
import {getWebLn, wallet} from "@app/lightning"
2026-06-08 21:14:40 +00:00
import {pushToast} from "@app/toast"
import {pushModal} from "@app/modal"
2026-07-28 16:16:26 +00:00
const userProfile = deriveUserItem($app => $profiles)
2025-07-04 13:22:19 +00:00
const back = () => history.back()
const connectWithWebLn = async () => {
loading = true
try {
await Promise.all([sleep(800), getWebLn().enable()])
const info = await getWebLn().getInfo()
if (!info?.supports?.includes("lightning")) {
pushToast({
theme: "error",
message: "Your extension does not support lightning payments",
})
} else {
2026-07-28 16:16:26 +00:00
wallet.set({type: WalletType.WebLN, info})
2025-07-04 13:22:19 +00:00
pushToast({message: "Wallet successfully connected!"})
await sleep(400)
back()
}
} catch (e) {
console.error(e)
pushToast({
theme: "error",
message: "Wallet failed to connect",
})
} finally {
loading = false
}
}
const connectWithNWC = async () => {
loading = true
try {
const client = new nwc.NWCClient({nostrWalletConnectUrl})
const [_, info] = await Promise.all([sleep(800), client.getInfo()])
if (!info) {
pushToast({
theme: "error",
message: "Wallet failed to connect",
})
} else {
2026-07-28 16:16:26 +00:00
wallet.set({type: WalletType.NWC, info: client.options as unknown as NWCInfo})
2025-07-04 13:22:19 +00:00
pushToast({message: "Wallet successfully connected!"})
await sleep(400)
back()
2026-07-28 16:16:26 +00:00
if (info.lud16 && info.lud16 !== $userProfile?.values.lud16) {
pushModal(WalletAsReceivingAddress)
}
2025-07-04 13:22:19 +00:00
}
} catch (e) {
console.error(e)
pushToast({
theme: "error",
message: "Wallet failed to connect",
})
} finally {
loading = false
}
}
2025-07-15 22:49:26 +00:00
const toggleScanner = () => {
showScanner = !showScanner
}
const onScan = debounce(1000, async (data: string) => {
showScanner = false
nostrWalletConnectUrl = data
await connectWithNWC()
})
2025-07-04 13:22:19 +00:00
let nostrWalletConnectUrl = $state("")
2025-07-15 22:49:26 +00:00
let showScanner = $state(false)
2025-07-04 13:22:19 +00:00
let loading = $state(false)
</script>
2026-01-30 22:14:12 +00:00
<Modal>
<ModalBody>
<ModalHeader>
2026-02-02 18:09:14 +00:00
<ModalTitle>Connect a Wallet</ModalTitle>
<ModalSubtitle
>Use Nostr Wallet Connect to send Bitcoin payments over lightning.</ModalSubtitle>
2026-01-30 22:14:12 +00:00
</ModalHeader>
{#if getWebLn()}
<Button
class="button button-primary"
2026-01-30 22:14:12 +00:00
disabled={Boolean(nostrWalletConnectUrl || loading)}
onclick={connectWithWebLn}>
<Spinner loading={!nostrWalletConnectUrl && loading}>
{#if !nostrWalletConnectUrl && loading}
Connecting...
{:else}
<div class="flex items-center gap-2">
<Icon icon={Cpu} />
Connect with WebLN
</div>
{/if}
</Spinner>
</Button>
<Divider>Or</Divider>
{/if}
<Field>
{#snippet label()}
Connection Secret*
{/snippet}
{#snippet input()}
<label class="input flex w-full items-center gap-2">
2026-01-30 22:14:12 +00:00
<Icon icon={Lock} />
<input
bind:value={nostrWalletConnectUrl}
autocomplete="off"
name="flotilla-nwc"
class="grow"
type="password" />
<Button onclick={toggleScanner}>
<Icon icon={QrCode} />
</Button>
</label>
{/snippet}
{#snippet info()}
You can find this in any wallet that supports
<Link external href="https://nwc.getalby.com/about" class="text-primary"
>Nostr Wallet Connect</Link
>.
{/snippet}
</Field>
{#if showScanner}
<Scanner onscan={onScan} />
{/if}
</ModalBody>
2025-07-04 13:22:19 +00:00
<ModalFooter>
<Button class="button button-link" onclick={back}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2025-07-04 13:22:19 +00:00
Go back
</Button>
<Button
class="button button-primary"
2025-07-04 13:22:19 +00:00
disabled={!nostrWalletConnectUrl || loading}
onclick={connectWithNWC}>
<Spinner loading={Boolean(nostrWalletConnectUrl && loading)}>
{#if nostrWalletConnectUrl && loading}
Connecting...
{:else}
<div class="flex items-center gap-2">
Connect Wallet
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowRight} />
2025-07-04 13:22:19 +00:00
</div>
{/if}
</Spinner>
</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>