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
+11 -8
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))
+9 -5
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)
+3 -2
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()
}
+2 -3
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})
-37
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 = {