From 34bfa89e743503703446e14a00e553f1821e9432 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Mon, 17 Jun 2024 02:59:51 -0500 Subject: [PATCH] wip --- components/wallet-card.js | 17 +++-- components/wallet-configurator.js | 70 ++++++++++++++++++++ components/webln/lnbits2.js | 104 +++++++++++++++++------------- pages/settings/wallets/index.js | 48 +++++++------- 4 files changed, 159 insertions(+), 80 deletions(-) create mode 100644 components/wallet-configurator.js diff --git a/components/wallet-card.js b/components/wallet-card.js index 7abcd304..08a88f2f 100644 --- a/components/wallet-card.js +++ b/components/wallet-card.js @@ -9,7 +9,7 @@ import { Status } from './webln' export const isConfigured = status => [Status.Enabled, Status.Locked, Status.Error, true].includes(status) -export function WalletCard ({ title, badges, provider, status }) { +export function WalletCard ({ title, badges, provider, status, href }) { const configured = isConfigured(status) let indicator = styles.disabled switch (status) { @@ -42,14 +42,13 @@ export function WalletCard ({ title, badges, provider, status }) { )} - {provider && - - - {configured - ? <>configure - : <>attach} - - } + + + {configured + ? <>configure + : <>attach} + + ) } diff --git a/components/wallet-configurator.js b/components/wallet-configurator.js new file mode 100644 index 00000000..a62b01a4 --- /dev/null +++ b/components/wallet-configurator.js @@ -0,0 +1,70 @@ +import { WalletSecurityBanner } from './banners' +import { Form } from './form' +import { CenterLayout } from './layout' + +export default function WalletConfigurator ({ config }) { + const initial = config.provider + + return ( + +

{config.title}

+
use {config.title} for payments
+ +
{ + try { + await saveConfig(values) + if (isDefault) setProvider(lnbits) + toaster.success('saved settings') + router.push('/settings/wallets') + } catch (err) { + console.error(err) + toaster.danger('failed to attach: ' + err.message || err.toString?.()) + } + }} + > + + + + { + try { + await clearConfig() + toaster.success('saved settings') + router.push('/settings/wallets') + } catch (err) { + console.error(err) + toaster.danger('failed to detach: ' + err.message || err.toString?.()) + } + }} + /> + +
+ +
+
+ ) +} diff --git a/components/webln/lnbits2.js b/components/webln/lnbits2.js index c7b5e6c1..c0939c56 100644 --- a/components/webln/lnbits2.js +++ b/components/webln/lnbits2.js @@ -2,57 +2,69 @@ import { bolt11Tags } from '@/lib/bolt11' export const name = 'LNbits' -let config, logger - -export function setConfig (_config) { - config = _config -} - -export function setLogger (_logger) { - logger = _logger -} - -export async function getInfo () { - const response = await getWallet(config.url, config.adminKey) - return { - node: { - alias: response.name, - pubkey: '' +export const config = { + provider: { + url: { + label: 'lnbits url', + type: 'text' }, - methods: [ - 'getInfo', - 'getBalance', - 'sendPayment' - ], - version: '1.0', - supports: ['lightning'] - } -} - -export async function sendPayment (bolt11) { - const { url, adminKey } = config - - const hash = bolt11Tags(bolt11).payment_hash - logger.info('sending payment:', `payment_hash=${hash}`) - - try { - const response = await postPayment(url, adminKey, bolt11) - - const checkResponse = await getPayment(url, adminKey, response.payment_hash) - if (!checkResponse.preimage) { - throw new Error('No preimage') + adminKey: { + label: 'admin key', + type: 'password' } - - const preimage = checkResponse.preimage - logger.ok('payment successful:', `payment_hash=${hash}`, `preimage=${preimage}`) - return { preimage } - } catch (err) { - logger.error('payment failed:', `payment_hash=${hash}`, err.message || err.toString?.()) - throw err + }, + card: { + title: 'LNbits', + badges: ['send only', 'non-custodialish'], + href: '/settings/wallets/lnbits' } } -const getWallet = async (baseUrl, adminKey) => { +export function getInfo ({ config, logger }) { + return async function () { + const response = await getWallet(config.url, config.adminKey) + return { + node: { + alias: response.name, + pubkey: '' + }, + methods: [ + 'getInfo', + 'getBalance', + 'sendPayment' + ], + version: '1.0', + supports: ['lightning'] + } + } +} + +export function sendPayment ({ config, logger }) { + return async function (bolt11) { + const { url, adminKey } = config + + const hash = bolt11Tags(bolt11).payment_hash + logger.info('sending payment:', `payment_hash=${hash}`) + + try { + const response = await postPayment(url, adminKey, bolt11) + + const checkResponse = await getPayment(url, adminKey, response.payment_hash) + if (!checkResponse.preimage) { + throw new Error('No preimage') + } + + const preimage = checkResponse.preimage + logger.ok('payment successful:', `payment_hash=${hash}`, `preimage=${preimage}`) + return { preimage } + } catch (err) { + logger.error('payment failed:', `payment_hash=${hash}`, err.message || err.toString?.()) + throw err + } + } +} + +async function getWallet (baseUrl, adminKey) { const url = baseUrl.replace(/\/+$/, '') const path = '/api/v1/wallet' diff --git a/pages/settings/wallets/index.js b/pages/settings/wallets/index.js index 8adbcec5..4618c942 100644 --- a/pages/settings/wallets/index.js +++ b/pages/settings/wallets/index.js @@ -2,28 +2,32 @@ import { getGetServerSideProps } from '@/api/ssrApollo' import Layout from '@/components/layout' import styles from '@/styles/wallet.module.css' import { WalletCard } from '@/components/wallet-card' -import { LightningAddressWalletCard } from './lightning-address' -import { LNbitsCard } from './lnbits' -import { NWCCard } from './nwc' -import { LNDCard } from './lnd' -import { CLNCard } from './cln' +// import { LightningAddressWalletCard } from './lightning-address' +// import { LNbitsCard } from './lnbits' +// import { NWCCard } from './nwc' +// import { LNDCard } from './lnd' +// import { CLNCard } from './cln' import { WALLETS } from '@/fragments/wallet' -import { useQuery } from '@apollo/client' -import PageLoading from '@/components/page-loading' -import { LNCCard } from './lnc' +// import { useQuery } from '@apollo/client' +// import PageLoading from '@/components/page-loading' +// import { LNCCard } from './lnc' import Link from 'next/link' -import { Wallet as W } from '@/lib/constants' +// import { Wallet as W } from '@/lib/constants' +import { config as lnbitsConfig } from '@/components/webln/lnbits2' + +// TODO: load configs without individual imports? +const walletConfigs = [lnbitsConfig] export const getServerSideProps = getGetServerSideProps({ query: WALLETS, authRequired: true }) export default function Wallet ({ ssrData }) { - const { data } = useQuery(WALLETS) - - if (!data && !ssrData) return - const { wallets } = data || ssrData - const lnd = wallets.find(w => w.type === W.LND.type) - const lnaddr = wallets.find(w => w.type === W.LnAddr.type) - const cln = wallets.find(w => w.type === W.CLN.type) + // const { data } = useQuery(WALLETS) + // + // if (!data && !ssrData) return + // const { wallets } = data || ssrData + // const lnd = wallets.find(w => w.type === W.LND.type) + // const lnaddr = wallets.find(w => w.type === W.LnAddr.type) + // const cln = wallets.find(w => w.type === W.CLN.type) return ( @@ -36,15 +40,9 @@ export default function Wallet ({ ssrData }) {
- - - - - - - - - + {walletConfigs.map((config, i) => ( + + ))}