2024-05-18 01:27:34 +00:00
|
|
|
import { createContext, useCallback, useContext, useEffect, useMemo, useState } from 'react'
|
2024-04-27 02:22:30 +00:00
|
|
|
import { useWalletLogger } from '../logger'
|
2024-05-03 21:42:00 +00:00
|
|
|
import { Status, migrateLocalStorage } from '.'
|
2024-04-27 02:22:30 +00:00
|
|
|
import { bolt11Tags } from '@/lib/bolt11'
|
|
|
|
import useModal from '../modal'
|
|
|
|
import { Form, PasswordInput, SubmitButton } from '../form'
|
|
|
|
import CancelButton from '../cancel-button'
|
|
|
|
import { Mutex } from 'async-mutex'
|
2024-05-03 19:14:33 +00:00
|
|
|
import { Wallet } from '@/lib/constants'
|
2024-05-03 21:42:00 +00:00
|
|
|
import { useMe } from '../me'
|
2024-05-28 17:18:54 +00:00
|
|
|
import { InvoiceCanceledError, InvoiceExpiredError } from '@/components/payment'
|
2024-04-27 02:22:30 +00:00
|
|
|
|
|
|
|
const LNCContext = createContext()
|
|
|
|
const mutex = new Mutex()
|
|
|
|
|
2024-05-03 21:42:00 +00:00
|
|
|
async function getLNC ({ me }) {
|
2024-04-27 02:22:30 +00:00
|
|
|
if (window.lnc) return window.lnc
|
2024-07-08 21:10:19 +00:00
|
|
|
const { default: LNC } = await import('@lightninglabs/lnc-web')
|
2024-05-03 21:42:00 +00:00
|
|
|
// backwards compatibility: migrate to new storage key
|
2024-05-13 16:08:06 +00:00
|
|
|
if (me) migrateLocalStorage('lnc-web:default', `lnc-web:stacker:${me.id}`)
|
|
|
|
window.lnc = new LNC({ namespace: me?.id ? `stacker:${me.id}` : undefined })
|
2024-04-27 02:22:30 +00:00
|
|
|
return window.lnc
|
|
|
|
}
|
|
|
|
|
|
|
|
// default password if the user hasn't set one
|
|
|
|
export const XXX_DEFAULT_PASSWORD = 'password'
|
|
|
|
|
|
|
|
function validateNarrowPerms (lnc) {
|
|
|
|
if (!lnc.hasPerms('lnrpc.Lightning.SendPaymentSync')) {
|
|
|
|
throw new Error('missing permission: lnrpc.Lightning.SendPaymentSync')
|
|
|
|
}
|
|
|
|
if (lnc.hasPerms('lnrpc.Lightning.SendCoins')) {
|
|
|
|
throw new Error('too broad permission: lnrpc.Wallet.SendCoins')
|
|
|
|
}
|
|
|
|
// TODO: need to check for more narrow permissions
|
|
|
|
// blocked by https://github.com/lightninglabs/lnc-web/issues/112
|
|
|
|
}
|
|
|
|
|
|
|
|
export function LNCProvider ({ children }) {
|
2024-05-03 21:42:00 +00:00
|
|
|
const me = useMe()
|
2024-05-03 19:49:31 +00:00
|
|
|
const { logger } = useWalletLogger(Wallet.LNC)
|
2024-04-27 02:22:30 +00:00
|
|
|
const [config, setConfig] = useState({})
|
|
|
|
const [lnc, setLNC] = useState()
|
|
|
|
const [status, setStatus] = useState()
|
|
|
|
const [modal, showModal] = useModal()
|
|
|
|
|
|
|
|
const getInfo = useCallback(async () => {
|
|
|
|
logger.info('getInfo called')
|
|
|
|
return await lnc.lightning.getInfo()
|
|
|
|
}, [logger, lnc])
|
|
|
|
|
2024-04-28 01:00:54 +00:00
|
|
|
const unlock = useCallback(async (connect) => {
|
|
|
|
if (status === Status.Enabled) return config.password
|
|
|
|
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
const cancelAndReject = async () => {
|
|
|
|
reject(new Error('password canceled'))
|
|
|
|
}
|
|
|
|
showModal(onClose => {
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
password: ''
|
|
|
|
}}
|
|
|
|
onSubmit={async (values) => {
|
|
|
|
try {
|
|
|
|
lnc.credentials.password = values?.password
|
|
|
|
setStatus(Status.Enabled)
|
|
|
|
setConfig({ pairingPhrase: lnc.credentials.pairingPhrase, password: values.password })
|
|
|
|
logger.ok('wallet enabled')
|
|
|
|
onClose()
|
|
|
|
resolve(values.password)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('failed attempt to unlock wallet', err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<h4 className='text-center mb-3'>Unlock LNC</h4>
|
|
|
|
<PasswordInput
|
|
|
|
label='password'
|
|
|
|
name='password'
|
|
|
|
/>
|
|
|
|
<div className='mt-5 d-flex justify-content-between'>
|
|
|
|
<CancelButton onClick={() => { onClose(); cancelAndReject() }} />
|
|
|
|
<SubmitButton variant='primary'>unlock</SubmitButton>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}, { onClose: cancelAndReject })
|
|
|
|
})
|
|
|
|
}, [logger, showModal, setConfig, lnc, status])
|
|
|
|
|
2024-04-27 02:22:30 +00:00
|
|
|
const sendPayment = useCallback(async (bolt11) => {
|
|
|
|
const hash = bolt11Tags(bolt11).payment_hash
|
|
|
|
logger.info('sending payment:', `payment_hash=${hash}`)
|
|
|
|
|
|
|
|
return await mutex.runExclusive(async () => {
|
|
|
|
try {
|
2024-04-28 01:00:54 +00:00
|
|
|
const password = await unlock()
|
2024-04-27 02:22:30 +00:00
|
|
|
// credentials need to be decrypted before connecting after a disconnect
|
2024-04-28 01:00:54 +00:00
|
|
|
lnc.credentials.password = password || XXX_DEFAULT_PASSWORD
|
2024-04-27 02:22:30 +00:00
|
|
|
await lnc.connect()
|
|
|
|
const { paymentError, paymentPreimage: preimage } =
|
|
|
|
await lnc.lnd.lightning.sendPaymentSync({ payment_request: bolt11 })
|
|
|
|
|
|
|
|
if (paymentError) throw new Error(paymentError)
|
|
|
|
if (!preimage) throw new Error('No preimage in response')
|
|
|
|
|
|
|
|
logger.ok('payment successful:', `payment_hash=${hash}`, `preimage=${preimage}`)
|
|
|
|
return { preimage }
|
|
|
|
} catch (err) {
|
2024-05-28 17:18:54 +00:00
|
|
|
const msg = err.message || err.toString?.()
|
|
|
|
logger.error('payment failed:', `payment_hash=${hash}`, msg)
|
|
|
|
if (msg.includes('invoice expired')) {
|
|
|
|
throw new InvoiceExpiredError(hash)
|
|
|
|
}
|
|
|
|
if (msg.includes('canceled')) {
|
|
|
|
throw new InvoiceCanceledError(hash)
|
|
|
|
}
|
2024-04-27 02:22:30 +00:00
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
lnc.disconnect()
|
|
|
|
logger.info('disconnecting after:', `payment_hash=${hash}`)
|
|
|
|
// wait for lnc to disconnect before releasing the mutex
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
let counter = 0
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
if (lnc.isConnected) {
|
|
|
|
if (counter++ > 100) {
|
|
|
|
logger.error('failed to disconnect from lnc')
|
|
|
|
clearInterval(interval)
|
|
|
|
reject(new Error('failed to disconnect from lnc'))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clearInterval(interval)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}, 50)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('failed to disconnect from lnc', err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2024-04-28 01:00:54 +00:00
|
|
|
}, [logger, lnc, unlock])
|
2024-04-27 02:22:30 +00:00
|
|
|
|
|
|
|
const saveConfig = useCallback(async config => {
|
|
|
|
setConfig(config)
|
|
|
|
|
|
|
|
try {
|
|
|
|
lnc.credentials.pairingPhrase = config.pairingPhrase
|
|
|
|
await lnc.connect()
|
|
|
|
await validateNarrowPerms(lnc)
|
|
|
|
lnc.credentials.password = config?.password || XXX_DEFAULT_PASSWORD
|
|
|
|
setStatus(Status.Enabled)
|
|
|
|
logger.ok('wallet enabled')
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('invalid config:', err)
|
2024-04-29 16:57:35 +00:00
|
|
|
setStatus(Status.Error)
|
2024-04-27 02:22:30 +00:00
|
|
|
logger.info('wallet disabled')
|
|
|
|
throw err
|
|
|
|
} finally {
|
|
|
|
lnc.disconnect()
|
|
|
|
}
|
|
|
|
}, [logger, lnc])
|
|
|
|
|
|
|
|
const clearConfig = useCallback(async () => {
|
|
|
|
await lnc.credentials.clear(false)
|
|
|
|
if (lnc.isConnected) lnc.disconnect()
|
|
|
|
setStatus(undefined)
|
2024-04-27 23:30:03 +00:00
|
|
|
setConfig({})
|
2024-04-27 02:22:30 +00:00
|
|
|
logger.info('cleared config')
|
|
|
|
}, [logger, lnc])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
try {
|
2024-05-03 21:42:00 +00:00
|
|
|
const lnc = await getLNC({ me })
|
2024-04-27 02:22:30 +00:00
|
|
|
setLNC(lnc)
|
|
|
|
setStatus(Status.Initialized)
|
|
|
|
if (lnc.credentials.isPaired) {
|
|
|
|
try {
|
|
|
|
// try the default password
|
|
|
|
lnc.credentials.password = XXX_DEFAULT_PASSWORD
|
|
|
|
} catch (err) {
|
|
|
|
setStatus(Status.Locked)
|
|
|
|
logger.info('wallet needs password before enabling')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setStatus(Status.Enabled)
|
|
|
|
setConfig({ pairingPhrase: lnc.credentials.pairingPhrase, password: lnc.credentials.password })
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2024-04-29 16:57:35 +00:00
|
|
|
logger.error('wallet could not be loaded:', err)
|
2024-04-27 02:22:30 +00:00
|
|
|
setStatus(Status.Error)
|
|
|
|
}
|
|
|
|
})()
|
2024-05-03 21:42:00 +00:00
|
|
|
}, [me, setStatus, setConfig, logger])
|
2024-04-27 02:22:30 +00:00
|
|
|
|
2024-05-18 01:27:34 +00:00
|
|
|
const value = useMemo(
|
|
|
|
() => ({ name: 'lnc', status, unlock, getInfo, sendPayment, config, saveConfig, clearConfig }),
|
|
|
|
[status, unlock, getInfo, sendPayment, config, saveConfig, clearConfig])
|
2024-04-27 02:22:30 +00:00
|
|
|
return (
|
2024-05-18 01:27:34 +00:00
|
|
|
<LNCContext.Provider value={value}>
|
2024-04-27 02:22:30 +00:00
|
|
|
{children}
|
|
|
|
{modal}
|
|
|
|
</LNCContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useLNC () {
|
|
|
|
return useContext(LNCContext)
|
|
|
|
}
|