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 {
pubkey,
signer,
session,
repository,
publishThunk,
profilesByPubkey,
@@ -56,8 +57,6 @@ import {
getThunkError,
} from "@welshman/app"
import {
wallet,
getWebLn,
PROTECTED,
userMembership,
INDEXER_RELAYS,
@@ -462,16 +461,20 @@ export const makeAlert = async (params: AlertParams) => {
export const publishAlert = async (params: AlertParams) =>
publishThunk({event: await makeAlert(params), relays: [NOTIFIER_RELAY]})
export const payInvoice = async (invoice: string) => {
const $wallet = get(wallet)
// Lightning
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")
}
if ($wallet.type === "nwc") {
return new nwc.NWCClient($wallet.info).payInvoice({invoice})
} else if ($wallet.type === "webln") {
if ($session.wallet.type === "nwc") {
return new nwc.NWCClient($session.wallet.info).payInvoice({invoice})
} else if ($session.wallet.type === "webln") {
return getWebLn()
.enable()
.then(() => getWebLn().sendPayment(invoice))

View File

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

View File

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

View File

@@ -1,13 +1,12 @@
<script lang="ts">
import type {Snippet} from "svelte"
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 Zap from "@app/components/Zap.svelte"
import InfoZapperError from "@app/components/InfoZapperError.svelte"
import WalletConnect from "@app/components/WalletConnect.svelte"
import {pushModal} from "@app/modal"
import {wallet} from "@app/state"
type Props = {
url: string
@@ -24,7 +23,7 @@
const onClick = () => {
if (!$zapper?.allowsNostr) {
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})
} else {
pushModal(WalletConnect, {}, {replaceState})

View File

@@ -355,43 +355,6 @@ export const {
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
export type Alert = {

View File

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