2024-06-03 22:41:15 +00:00
|
|
|
import { useCallback } from 'react'
|
|
|
|
import { useMe } from '@/components/me'
|
2024-07-03 01:34:45 +00:00
|
|
|
import useLocalConfig from '@/components/use-local-state'
|
2024-06-20 14:48:46 +00:00
|
|
|
import { useWalletLogger } from '@/components/wallet-logger'
|
2024-06-03 22:41:15 +00:00
|
|
|
import { SSR } from '@/lib/constants'
|
2024-06-20 17:56:37 +00:00
|
|
|
import { bolt11Tags } from '@/lib/bolt11'
|
2024-06-24 12:15:14 +00:00
|
|
|
|
|
|
|
import * as lnbits from '@/components/wallet/lnbits'
|
|
|
|
import * as nwc from '@/components/wallet/nwc'
|
2024-06-24 12:06:51 +00:00
|
|
|
import * as lnc from '@/components/wallet/lnc'
|
2024-06-26 12:31:35 +00:00
|
|
|
import * as lnd from '@/components/wallet/lnd'
|
2024-07-07 07:36:48 +00:00
|
|
|
import * as lnAddr from '@/components/wallet/lightning-address'
|
2024-07-07 08:41:44 +00:00
|
|
|
import * as cln from '@/components/wallet/cln'
|
2024-07-06 02:56:28 +00:00
|
|
|
import { gql, useApolloClient, useQuery } from '@apollo/client'
|
2024-06-26 12:31:35 +00:00
|
|
|
import { REMOVE_WALLET, WALLET_BY_TYPE } from '@/fragments/wallet'
|
|
|
|
import { autowithdrawInitial } from '../autowithdraw-shared'
|
2024-06-03 22:41:15 +00:00
|
|
|
|
|
|
|
// wallet definitions
|
2024-07-07 08:41:44 +00:00
|
|
|
export const WALLET_DEFS = [lnbits, nwc, lnc, lnd, lnAddr, cln]
|
2024-06-03 22:41:15 +00:00
|
|
|
|
|
|
|
export const Status = {
|
|
|
|
Initialized: 'Initialized',
|
|
|
|
Enabled: 'Enabled',
|
|
|
|
Locked: 'Locked',
|
|
|
|
Error: 'Error'
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useWallet (name) {
|
|
|
|
const me = useMe()
|
|
|
|
|
2024-06-26 12:31:35 +00:00
|
|
|
const wallet = name ? getWalletByName(name) : getEnabledWallet(me)
|
2024-06-20 17:56:37 +00:00
|
|
|
const { logger } = useWalletLogger(wallet)
|
2024-06-03 22:41:15 +00:00
|
|
|
|
2024-06-26 12:31:35 +00:00
|
|
|
const [config, saveConfig, clearConfig] = useConfig(wallet)
|
2024-07-04 00:48:20 +00:00
|
|
|
const _isConfigured = isConfigured({ ...wallet, config })
|
2024-06-26 12:31:35 +00:00
|
|
|
|
2024-07-05 19:00:37 +00:00
|
|
|
const status = config?.enabled ? Status.Enabled : Status.Initialized
|
2024-07-05 06:22:47 +00:00
|
|
|
const enabled = status === Status.Enabled
|
|
|
|
const priority = config?.priority
|
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
const sendPayment = useCallback(async (bolt11) => {
|
2024-06-20 17:56:37 +00:00
|
|
|
const hash = bolt11Tags(bolt11).payment_hash
|
|
|
|
logger.info('sending payment:', `payment_hash=${hash}`)
|
|
|
|
try {
|
2024-07-06 01:03:40 +00:00
|
|
|
const { preimage } = await wallet.sendPayment(bolt11, config, { logger })
|
2024-06-20 17:56:37 +00:00
|
|
|
logger.ok('payment successful:', `payment_hash=${hash}`, `preimage=${preimage}`)
|
|
|
|
} catch (err) {
|
|
|
|
const message = err.message || err.toString?.()
|
|
|
|
logger.error('payment failed:', `payment_hash=${hash}`, message)
|
|
|
|
throw err
|
|
|
|
}
|
2024-06-03 22:41:15 +00:00
|
|
|
}, [wallet, config, logger])
|
|
|
|
|
|
|
|
const enable = useCallback(() => {
|
|
|
|
enableWallet(name, me)
|
2024-06-20 17:56:37 +00:00
|
|
|
logger.ok('wallet enabled')
|
|
|
|
}, [name, me, logger])
|
|
|
|
|
|
|
|
const disable = useCallback(() => {
|
|
|
|
disableWallet(name, me)
|
2024-06-21 20:35:19 +00:00
|
|
|
logger.info('wallet disabled')
|
2024-06-20 17:56:37 +00:00
|
|
|
}, [name, me, logger])
|
2024-06-03 22:41:15 +00:00
|
|
|
|
2024-07-05 06:22:47 +00:00
|
|
|
const setPriority = useCallback(async (priority) => {
|
|
|
|
if (_isConfigured && priority !== config.priority) {
|
|
|
|
await saveConfig({ ...config, priority })
|
|
|
|
}
|
|
|
|
}, [wallet, config, logger])
|
|
|
|
|
2024-07-04 00:48:20 +00:00
|
|
|
const save = useCallback(async (newConfig) => {
|
2024-06-20 18:34:09 +00:00
|
|
|
try {
|
2024-06-21 20:32:06 +00:00
|
|
|
// validate should log custom INFO and OK message
|
2024-06-26 12:31:35 +00:00
|
|
|
// validate is optional since validation might happen during save on server
|
2024-06-21 20:32:06 +00:00
|
|
|
// TODO: add timeout
|
2024-07-06 01:03:40 +00:00
|
|
|
await wallet.validate?.(newConfig, { me, logger })
|
2024-07-04 00:48:20 +00:00
|
|
|
await saveConfig(newConfig)
|
|
|
|
logger.ok(_isConfigured ? 'wallet updated' : 'wallet attached')
|
2024-06-20 18:34:09 +00:00
|
|
|
} catch (err) {
|
2024-06-21 20:32:06 +00:00
|
|
|
const message = err.message || err.toString?.()
|
2024-07-03 00:50:51 +00:00
|
|
|
logger.error('failed to attach: ' + message)
|
2024-06-20 18:34:09 +00:00
|
|
|
throw err
|
|
|
|
}
|
2024-07-04 00:48:20 +00:00
|
|
|
}, [_isConfigured, saveConfig, me, logger])
|
2024-06-20 18:34:09 +00:00
|
|
|
|
|
|
|
// delete is a reserved keyword
|
2024-07-05 19:00:37 +00:00
|
|
|
const delete_ = useCallback(async () => {
|
2024-06-20 18:34:09 +00:00
|
|
|
try {
|
2024-07-05 19:00:37 +00:00
|
|
|
await clearConfig()
|
2024-06-20 18:34:09 +00:00
|
|
|
logger.ok('wallet detached')
|
2024-07-04 00:48:20 +00:00
|
|
|
disable()
|
2024-06-20 18:34:09 +00:00
|
|
|
} catch (err) {
|
2024-06-21 20:32:06 +00:00
|
|
|
const message = err.message || err.toString?.()
|
2024-06-20 18:34:09 +00:00
|
|
|
logger.error(message)
|
|
|
|
throw err
|
|
|
|
}
|
2024-07-04 00:48:20 +00:00
|
|
|
}, [clearConfig, logger, disable])
|
2024-06-20 18:34:09 +00:00
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
return {
|
|
|
|
...wallet,
|
|
|
|
sendPayment,
|
|
|
|
config,
|
2024-06-20 18:34:09 +00:00
|
|
|
save,
|
|
|
|
delete: delete_,
|
2024-06-03 22:41:15 +00:00
|
|
|
enable,
|
2024-06-20 17:56:37 +00:00
|
|
|
disable,
|
2024-07-05 06:22:47 +00:00
|
|
|
setPriority,
|
2024-07-04 00:48:20 +00:00
|
|
|
isConfigured: _isConfigured,
|
2024-07-05 06:22:47 +00:00
|
|
|
status,
|
|
|
|
enabled,
|
|
|
|
priority,
|
2024-06-20 17:56:37 +00:00
|
|
|
logger
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:31:35 +00:00
|
|
|
function useConfig (wallet) {
|
2024-07-03 01:34:45 +00:00
|
|
|
const me = useMe()
|
2024-07-03 16:24:05 +00:00
|
|
|
|
2024-07-03 01:34:45 +00:00
|
|
|
const storageKey = getStorageKey(wallet?.name, me)
|
|
|
|
const [localConfig, setLocalConfig, clearLocalConfig] = useLocalConfig(storageKey)
|
2024-07-03 16:24:05 +00:00
|
|
|
|
2024-07-03 01:34:45 +00:00
|
|
|
const [serverConfig, setServerConfig, clearServerConfig] = useServerConfig(wallet)
|
2024-06-26 12:31:35 +00:00
|
|
|
|
2024-07-03 16:24:05 +00:00
|
|
|
const hasLocalConfig = !!wallet?.sendPayment
|
|
|
|
const hasServerConfig = !!wallet?.server
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
// only include config if it makes sense for this wallet
|
|
|
|
// since server config always returns default values for autowithdraw settings
|
|
|
|
// which might be confusing to have for wallets that don't support autowithdraw
|
|
|
|
...(hasLocalConfig ? localConfig : {}),
|
|
|
|
...(hasServerConfig ? serverConfig : {})
|
|
|
|
}
|
2024-06-26 12:31:35 +00:00
|
|
|
|
2024-07-03 01:34:45 +00:00
|
|
|
const saveConfig = useCallback(async (config) => {
|
2024-07-03 16:24:05 +00:00
|
|
|
if (hasLocalConfig) setLocalConfig(config)
|
|
|
|
if (hasServerConfig) await setServerConfig(config)
|
2024-07-03 01:34:45 +00:00
|
|
|
}, [wallet])
|
2024-06-26 12:31:35 +00:00
|
|
|
|
2024-07-03 01:34:45 +00:00
|
|
|
const clearConfig = useCallback(async () => {
|
2024-07-03 16:24:05 +00:00
|
|
|
if (hasLocalConfig) clearLocalConfig()
|
|
|
|
if (hasServerConfig) await clearServerConfig()
|
|
|
|
}, [wallet])
|
2024-06-26 12:31:35 +00:00
|
|
|
|
2024-07-03 01:34:45 +00:00
|
|
|
return [config, saveConfig, clearConfig]
|
2024-06-26 12:31:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 16:23:45 +00:00
|
|
|
function isConfigured ({ fields, config }) {
|
|
|
|
if (!config || !fields) return false
|
2024-07-03 02:21:20 +00:00
|
|
|
|
2024-07-05 19:34:48 +00:00
|
|
|
// a wallet is configured if all of its required fields are set
|
2024-07-03 16:23:45 +00:00
|
|
|
const val = fields.every(field => {
|
|
|
|
return field.optional ? true : !!config?.[field.name]
|
2024-07-03 02:21:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:31:35 +00:00
|
|
|
function useServerConfig (wallet) {
|
|
|
|
const client = useApolloClient()
|
|
|
|
const me = useMe()
|
|
|
|
|
2024-07-03 16:55:15 +00:00
|
|
|
const { data, refetch: refetchConfig } = useQuery(WALLET_BY_TYPE, { variables: { type: wallet?.server?.walletType }, skip: !wallet?.server })
|
2024-06-26 12:31:35 +00:00
|
|
|
|
2024-07-02 00:11:42 +00:00
|
|
|
const walletId = data?.walletByType?.id
|
2024-07-05 19:00:37 +00:00
|
|
|
const serverConfig = {
|
|
|
|
id: walletId,
|
|
|
|
priority: data?.walletByType?.priority,
|
|
|
|
enabled: data?.walletByType?.enabled,
|
|
|
|
...data?.walletByType?.wallet
|
|
|
|
}
|
|
|
|
const autowithdrawSettings = autowithdrawInitial({ me })
|
2024-07-05 06:22:47 +00:00
|
|
|
const config = { ...serverConfig, ...autowithdrawSettings }
|
2024-06-26 12:31:35 +00:00
|
|
|
|
|
|
|
const saveConfig = useCallback(async ({
|
|
|
|
autoWithdrawThreshold,
|
|
|
|
autoWithdrawMaxFeePercent,
|
|
|
|
priority,
|
2024-07-05 19:00:37 +00:00
|
|
|
enabled,
|
2024-06-26 12:31:35 +00:00
|
|
|
...config
|
|
|
|
}) => {
|
2024-07-03 02:45:28 +00:00
|
|
|
try {
|
2024-07-06 02:56:28 +00:00
|
|
|
const mutation = generateMutation(wallet)
|
2024-07-03 02:45:28 +00:00
|
|
|
return await client.mutate({
|
2024-07-06 02:56:28 +00:00
|
|
|
mutation,
|
2024-07-03 02:45:28 +00:00
|
|
|
variables: {
|
|
|
|
id: walletId,
|
|
|
|
...config,
|
|
|
|
settings: {
|
|
|
|
autoWithdrawThreshold: Number(autoWithdrawThreshold),
|
|
|
|
autoWithdrawMaxFeePercent: Number(autoWithdrawMaxFeePercent),
|
2024-07-05 19:00:37 +00:00
|
|
|
priority,
|
|
|
|
enabled
|
2024-07-03 02:45:28 +00:00
|
|
|
}
|
2024-06-26 12:31:35 +00:00
|
|
|
}
|
2024-07-03 02:45:28 +00:00
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
client.refetchQueries({ include: ['WalletLogs'] })
|
2024-07-03 16:55:15 +00:00
|
|
|
refetchConfig()
|
2024-07-03 02:45:28 +00:00
|
|
|
}
|
2024-07-03 01:34:45 +00:00
|
|
|
}, [client, walletId])
|
2024-06-26 12:31:35 +00:00
|
|
|
|
|
|
|
const clearConfig = useCallback(async () => {
|
2024-07-04 19:24:34 +00:00
|
|
|
try {
|
|
|
|
await client.mutate({
|
|
|
|
mutation: REMOVE_WALLET,
|
|
|
|
variables: { id: walletId }
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
client.refetchQueries({ include: ['WalletLogs'] })
|
|
|
|
refetchConfig()
|
|
|
|
}
|
2024-07-03 01:34:45 +00:00
|
|
|
}, [client, walletId])
|
2024-06-26 12:31:35 +00:00
|
|
|
|
|
|
|
return [config, saveConfig, clearConfig]
|
|
|
|
}
|
|
|
|
|
2024-07-06 02:56:28 +00:00
|
|
|
function generateMutation (wallet) {
|
|
|
|
const { resolverName } = wallet.server
|
|
|
|
|
|
|
|
let headerArgs = '$id: ID, '
|
|
|
|
headerArgs += wallet.fields.map(f => {
|
|
|
|
let arg = `$${f.name}: String`
|
|
|
|
if (!f.optional) {
|
|
|
|
arg += '!'
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
}).join(', ')
|
|
|
|
headerArgs += ', $settings: AutowithdrawSettings!'
|
|
|
|
|
|
|
|
let inputArgs = 'id: $id, '
|
|
|
|
inputArgs += wallet.fields.map(f => `${f.name}: $${f.name}`).join(', ')
|
|
|
|
inputArgs += ', settings: $settings'
|
|
|
|
|
|
|
|
return gql`mutation ${resolverName}(${headerArgs}) {
|
|
|
|
${resolverName}(${inputArgs})
|
|
|
|
}`
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:31:35 +00:00
|
|
|
export function getWalletByName (name) {
|
2024-06-20 17:56:37 +00:00
|
|
|
return WALLET_DEFS.find(def => def.name === name)
|
|
|
|
}
|
|
|
|
|
2024-06-26 12:31:35 +00:00
|
|
|
export function getServerWallet (type) {
|
|
|
|
return WALLET_DEFS.find(def => def.server?.walletType === type)
|
|
|
|
}
|
|
|
|
|
2024-06-20 17:56:37 +00:00
|
|
|
export function getEnabledWallet (me) {
|
|
|
|
// TODO: handle multiple enabled wallets
|
|
|
|
return WALLET_DEFS
|
2024-06-20 18:16:27 +00:00
|
|
|
.filter(def => !!def.sendPayment)
|
2024-06-20 17:56:37 +00:00
|
|
|
.find(def => {
|
2024-06-03 22:41:15 +00:00
|
|
|
const key = getStorageKey(def.name, me)
|
|
|
|
const config = SSR ? null : JSON.parse(window?.localStorage.getItem(key))
|
|
|
|
return config?.enabled
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-05 06:22:47 +00:00
|
|
|
export function useWallets () {
|
|
|
|
return WALLET_DEFS.map(def => useWallet(def.name))
|
|
|
|
}
|
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
function getStorageKey (name, me) {
|
|
|
|
let storageKey = `wallet:${name}`
|
|
|
|
if (me) {
|
|
|
|
storageKey = `${storageKey}:${me.id}`
|
|
|
|
}
|
|
|
|
return storageKey
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableWallet (name, me) {
|
2024-07-03 00:34:14 +00:00
|
|
|
const key = getStorageKey(name, me)
|
|
|
|
const config = JSON.parse(window.localStorage.getItem(key))
|
|
|
|
if (!config) return
|
|
|
|
config.enabled = true
|
|
|
|
window.localStorage.setItem(key, JSON.stringify(config))
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
2024-06-20 17:56:37 +00:00
|
|
|
|
|
|
|
function disableWallet (name, me) {
|
|
|
|
const key = getStorageKey(name, me)
|
|
|
|
const config = JSON.parse(window.localStorage.getItem(key))
|
|
|
|
if (!config) return
|
|
|
|
config.enabled = false
|
|
|
|
window.localStorage.setItem(key, JSON.stringify(config))
|
|
|
|
}
|