Update wallet to use welshman's session wallet

This commit is contained in:
Jon Staab
2025-08-18 13:26:28 -07:00
parent 2a30ca5306
commit 38e0fc53ad
6 changed files with 34 additions and 63 deletions

View File

@@ -39,6 +39,7 @@ import {Router} from "@welshman/router"
import { import {
pubkey, pubkey,
signer, signer,
session,
repository, repository,
publishThunk, publishThunk,
profilesByPubkey, profilesByPubkey,
@@ -56,8 +57,6 @@ import {
getThunkError, getThunkError,
} from "@welshman/app" } from "@welshman/app"
import { import {
wallet,
getWebLn,
PROTECTED, PROTECTED,
userMembership, userMembership,
INDEXER_RELAYS, INDEXER_RELAYS,
@@ -462,16 +461,20 @@ export const makeAlert = async (params: AlertParams) => {
export const publishAlert = async (params: AlertParams) => export const publishAlert = async (params: AlertParams) =>
publishThunk({event: await makeAlert(params), relays: [NOTIFIER_RELAY]}) publishThunk({event: await makeAlert(params), relays: [NOTIFIER_RELAY]})
export const payInvoice = async (invoice: string) => { // Lightning
const $wallet = get(wallet)
if (!$wallet) { export const getWebLn = () => (window as any).webln
export const payInvoice = async (invoice: string) => {
const $session = session.get()
if (!$session?.wallet) {
throw new Error("No wallet is connected") throw new Error("No wallet is connected")
} }
if ($wallet.type === "nwc") { if ($session.wallet.type === "nwc") {
return new nwc.NWCClient($wallet.info).payInvoice({invoice}) return new nwc.NWCClient($session.wallet.info).payInvoice({invoice})
} else if ($wallet.type === "webln") { } else if ($session.wallet.type === "webln") {
return getWebLn() return getWebLn()
.enable() .enable()
.then(() => getWebLn().sendPayment(invoice)) .then(() => getWebLn().sendPayment(invoice))

View File

@@ -1,7 +1,9 @@
<script lang="ts"> <script lang="ts">
import {debounce} from "throttle-debounce" import {debounce} from "throttle-debounce"
import {nwc} from "@getalby/sdk" import {nwc} from "@getalby/sdk"
import {sleep} from "@welshman/lib" import {sleep, assoc} from "@welshman/lib"
import type {NWCInfo} from "@welshman/util"
import {pubkey, updateSession} from "@welshman/app"
import Link from "@lib/components/Link.svelte" import Link from "@lib/components/Link.svelte"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
@@ -11,8 +13,7 @@
import Divider from "@lib/components/Divider.svelte" import Divider from "@lib/components/Divider.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 type {NWCInfo} from "@app/state" import {getWebLn} from "@app/commands"
import {wallet, getWebLn} from "@app/state"
import {pushToast} from "@app/toast" import {pushToast} from "@app/toast"
const back = () => history.back() const back = () => history.back()
@@ -30,7 +31,7 @@
message: "Your extension does not support lightning payments", message: "Your extension does not support lightning payments",
}) })
} else { } else {
wallet.set({type: "webln", info}) updateSession($pubkey!, assoc("wallet", {type: "webln", info}))
pushToast({message: "Wallet successfully connected!"}) pushToast({message: "Wallet successfully connected!"})
await sleep(400) await sleep(400)
@@ -61,7 +62,10 @@
message: "Wallet failed to connect", message: "Wallet failed to connect",
}) })
} else { } else {
wallet.set({type: "nwc", info: client.options as unknown as NWCInfo}) updateSession(
$pubkey!,
assoc("wallet", {type: "nwc", info: client.options as unknown as NWCInfo}),
)
pushToast({message: "Wallet successfully connected!"}) pushToast({message: "Wallet successfully connected!"})
await sleep(400) await sleep(400)

View File

@@ -1,10 +1,11 @@
<script lang="ts"> <script lang="ts">
import {dissoc} from "@welshman/lib"
import {pubkey, updateSession} from "@welshman/app"
import Confirm from "@lib/components/Confirm.svelte" import Confirm from "@lib/components/Confirm.svelte"
import {wallet} from "@app/state"
import {clearModals} from "@app/modal" import {clearModals} from "@app/modal"
const confirm = async () => { const confirm = async () => {
wallet.set(undefined) updateSession($pubkey!, dissoc("wallet"))
clearModals() clearModals()
} }

View File

@@ -1,13 +1,12 @@
<script lang="ts"> <script lang="ts">
import type {Snippet} from "svelte" import type {Snippet} from "svelte"
import type {TrustedEvent} from "@welshman/util" import type {TrustedEvent} from "@welshman/util"
import {deriveZapperForPubkey} from "@welshman/app" import {session, deriveZapperForPubkey} from "@welshman/app"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import Zap from "@app/components/Zap.svelte" import Zap from "@app/components/Zap.svelte"
import InfoZapperError from "@app/components/InfoZapperError.svelte" import InfoZapperError from "@app/components/InfoZapperError.svelte"
import WalletConnect from "@app/components/WalletConnect.svelte" import WalletConnect from "@app/components/WalletConnect.svelte"
import {pushModal} from "@app/modal" import {pushModal} from "@app/modal"
import {wallet} from "@app/state"
type Props = { type Props = {
url: string url: string
@@ -24,7 +23,7 @@
const onClick = () => { const onClick = () => {
if (!$zapper?.allowsNostr) { if (!$zapper?.allowsNostr) {
pushModal(InfoZapperError, {url, pubkey: event.pubkey, eventId: event.id}, {replaceState}) pushModal(InfoZapperError, {url, pubkey: event.pubkey, eventId: event.id}, {replaceState})
} else if ($wallet) { } else if ($session?.wallet) {
pushModal(Zap, {url, pubkey: event.pubkey, eventId: event.id}, {replaceState}) pushModal(Zap, {url, pubkey: event.pubkey, eventId: event.id}, {replaceState})
} else { } else {
pushModal(WalletConnect, {}, {replaceState}) pushModal(WalletConnect, {}, {replaceState})

View File

@@ -355,43 +355,6 @@ export const {
load: makeOutboxLoader(SETTINGS), load: makeOutboxLoader(SETTINGS),
}) })
// Wallets
export type WebLNInfo = {
methods?: string[]
supports?: string[]
version?: string
node?: {
alias: string
}
}
export type NWCInfo = {
lud16: string
secret: string
relayUrl: string
walletPubkey: string
nostrWalletConnectUrl: string
}
export type Wallet =
| {
type: "webln"
info: WebLNInfo
}
| {
type: "nwc"
info: NWCInfo
}
export const wallet = synced<Wallet | undefined>({
key: "wallet",
defaultValue: undefined,
storage: localStorageProvider,
})
export const getWebLn = () => (window as any).webln
// Alerts // Alerts
export type Alert = { export type Alert = {

View File

@@ -2,12 +2,13 @@
import {nwc} from "@getalby/sdk" import {nwc} from "@getalby/sdk"
import {LOCALE} from "@welshman/lib" import {LOCALE} from "@welshman/lib"
import {displayRelayUrl, fromMsats} from "@welshman/util" import {displayRelayUrl, fromMsats} from "@welshman/util"
import {session} from "@welshman/app"
import Icon from "@lib/components/Icon.svelte" import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import WalletConnect from "@app/components/WalletConnect.svelte" import WalletConnect from "@app/components/WalletConnect.svelte"
import WalletDisconnect from "@app/components/WalletDisconnect.svelte" import WalletDisconnect from "@app/components/WalletDisconnect.svelte"
import {pushModal} from "@app/modal" import {pushModal} from "@app/modal"
import {wallet, getWebLn} from "@app/state" import {getWebLn} from "@app/commands"
const connect = () => pushModal(WalletConnect) const connect = () => pushModal(WalletConnect)
@@ -21,7 +22,7 @@
<Icon icon="wallet" /> <Icon icon="wallet" />
Wallet Wallet
</strong> </strong>
{#if $wallet} {#if $session?.wallet}
<div class="flex items-center gap-2 text-sm text-success"> <div class="flex items-center gap-2 text-sm text-success">
<Icon icon="check-circle" size={4} /> <Icon icon="check-circle" size={4} />
Connected Connected
@@ -34,13 +35,13 @@
{/if} {/if}
</div> </div>
<div class="col-4"> <div class="col-4">
{#if $wallet} {#if $session?.wallet}
{#if $wallet?.type === "webln"} {#if $session.wallet.type === "webln"}
{@const {node, version} = $wallet.info} {@const {node, version} = $session.wallet.info}
<div class="flex flex-col justify-between gap-2 lg:flex-row"> <div class="flex flex-col justify-between gap-2 lg:flex-row">
<p> <p>
Connected to <strong>{node?.alias || version || "unknown wallet"}</strong> Connected to <strong>{node?.alias || version || "unknown wallet"}</strong>
via <strong>{$wallet.type}</strong> via <strong>{$session.wallet.type}</strong>
</p> </p>
<p class="flex gap-2 whitespace-nowrap"> <p class="flex gap-2 whitespace-nowrap">
Balance: Balance:
@@ -56,8 +57,8 @@
sats sats
</p> </p>
</div> </div>
{:else if $wallet.type === "nwc"} {:else if $session.wallet.type === "nwc"}
{@const {lud16, relayUrl, nostrWalletConnectUrl} = $wallet.info} {@const {lud16, relayUrl, nostrWalletConnectUrl} = $session.wallet.info}
<div class="flex flex-col justify-between gap-2 lg:flex-row"> <div class="flex flex-col justify-between gap-2 lg:flex-row">
<p> <p>
Connected to <strong>{lud16}</strong> via <strong>{displayRelayUrl(relayUrl)}</strong> Connected to <strong>{lud16}</strong> via <strong>{displayRelayUrl(relayUrl)}</strong>