2024-06-03 22:41:15 +00:00
|
|
|
import { getGetServerSideProps } from '@/api/ssrApollo'
|
|
|
|
import { Form, ClientInput, ClientCheckbox, PasswordInput } from '@/components/form'
|
|
|
|
import { CenterLayout } from '@/components/layout'
|
|
|
|
import { WalletSecurityBanner } from '@/components/banners'
|
2024-06-20 14:48:46 +00:00
|
|
|
import { WalletLogs } from '@/components/wallet-logger'
|
2024-06-03 22:41:15 +00:00
|
|
|
import { useToast } from '@/components/toast'
|
|
|
|
import { useRouter } from 'next/router'
|
2024-07-16 04:09:27 +00:00
|
|
|
import { useWallet, Status } from 'wallets'
|
2024-06-24 10:53:59 +00:00
|
|
|
import Info from '@/components/info'
|
|
|
|
import Text from '@/components/text'
|
2024-06-26 12:31:35 +00:00
|
|
|
import { AutowithdrawSettings } from '@/components/autowithdraw-shared'
|
2024-07-02 03:38:41 +00:00
|
|
|
import dynamic from 'next/dynamic'
|
2024-07-16 22:04:35 +00:00
|
|
|
import { object, string } from 'yup'
|
|
|
|
import { autowithdrawSchemaMembers } from '@/lib/validate'
|
|
|
|
import { useMe } from '@/components/me'
|
|
|
|
import { TOR_REGEXP } from '@/lib/url'
|
2024-07-02 03:38:41 +00:00
|
|
|
|
|
|
|
const WalletButtonBar = dynamic(() => import('@/components/wallet-buttonbar.js'), { ssr: false })
|
2024-06-03 22:41:15 +00:00
|
|
|
|
|
|
|
export const getServerSideProps = getGetServerSideProps({ authRequired: true })
|
|
|
|
|
|
|
|
export default function WalletSettings () {
|
|
|
|
const toaster = useToast()
|
|
|
|
const router = useRouter()
|
|
|
|
const { wallet: name } = router.query
|
|
|
|
const wallet = useWallet(name)
|
2024-07-16 22:04:35 +00:00
|
|
|
const me = useMe()
|
2024-06-03 22:41:15 +00:00
|
|
|
|
|
|
|
const initial = wallet.fields.reduce((acc, field) => {
|
2024-07-05 19:00:37 +00:00
|
|
|
// We still need to run over all wallet fields via reduce
|
2024-07-05 17:17:28 +00:00
|
|
|
// even though we use wallet.config as the initial value
|
2024-07-05 19:00:37 +00:00
|
|
|
// since wallet.config is empty when wallet is not configured.
|
|
|
|
// Also, wallet.config includes general fields like
|
|
|
|
// 'enabled' and 'priority' which are not defined in wallet.fields.
|
2024-06-03 22:41:15 +00:00
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[field.name]: wallet.config?.[field.name] || ''
|
|
|
|
}
|
2024-07-05 17:17:28 +00:00
|
|
|
}, wallet.config)
|
2024-06-03 22:41:15 +00:00
|
|
|
|
2024-07-16 22:04:35 +00:00
|
|
|
const schema = generateSchema(wallet, { me })
|
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
return (
|
|
|
|
<CenterLayout>
|
|
|
|
<h2 className='pb-2'>{wallet.card.title}</h2>
|
2024-07-15 14:23:24 +00:00
|
|
|
<h6 className='text-muted text-center pb-3'><Text>{wallet.card.subtitle}</Text></h6>
|
2024-07-16 20:08:41 +00:00
|
|
|
{!wallet.walletType && <WalletSecurityBanner />}
|
2024-06-03 22:41:15 +00:00
|
|
|
<Form
|
|
|
|
initial={initial}
|
2024-07-16 22:04:35 +00:00
|
|
|
schema={schema}
|
2024-07-05 19:00:37 +00:00
|
|
|
onSubmit={async (values) => {
|
2024-06-03 22:41:15 +00:00
|
|
|
try {
|
2024-06-20 18:16:47 +00:00
|
|
|
const newConfig = !wallet.isConfigured
|
2024-07-04 19:34:36 +00:00
|
|
|
|
|
|
|
// enable wallet if wallet was just configured
|
|
|
|
if (newConfig) {
|
2024-07-05 19:00:37 +00:00
|
|
|
values.enabled = true
|
2024-07-04 19:34:36 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 20:32:06 +00:00
|
|
|
await wallet.save(values)
|
2024-07-04 19:34:36 +00:00
|
|
|
|
2024-07-05 19:00:37 +00:00
|
|
|
if (values.enabled) wallet.enable()
|
2024-06-20 17:56:37 +00:00
|
|
|
else wallet.disable()
|
2024-07-04 19:34:36 +00:00
|
|
|
|
2024-06-03 22:41:15 +00:00
|
|
|
toaster.success('saved settings')
|
|
|
|
router.push('/settings/wallets')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-06-20 17:56:37 +00:00
|
|
|
const message = 'failed to attach: ' + err.message || err.toString?.()
|
|
|
|
toaster.danger(message)
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<WalletFields wallet={wallet} />
|
2024-07-16 20:08:41 +00:00
|
|
|
{wallet.walletType
|
2024-07-04 19:38:54 +00:00
|
|
|
? <AutowithdrawSettings wallet={wallet} />
|
2024-06-26 12:31:35 +00:00
|
|
|
: (
|
|
|
|
<ClientCheckbox
|
2024-07-04 19:38:54 +00:00
|
|
|
disabled={!wallet.isConfigured}
|
2024-06-26 12:31:35 +00:00
|
|
|
initialValue={wallet.status === Status.Enabled}
|
|
|
|
label='enabled'
|
|
|
|
name='enabled'
|
|
|
|
/>
|
|
|
|
)}
|
2024-06-03 22:41:15 +00:00
|
|
|
<WalletButtonBar
|
|
|
|
wallet={wallet} onDelete={async () => {
|
|
|
|
try {
|
2024-07-05 19:00:37 +00:00
|
|
|
await wallet.delete()
|
2024-06-03 22:41:15 +00:00
|
|
|
toaster.success('saved settings')
|
|
|
|
router.push('/settings/wallets')
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2024-06-20 17:56:37 +00:00
|
|
|
const message = 'failed to detach: ' + err.message || err.toString?.()
|
|
|
|
toaster.danger(message)
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
<div className='mt-3 w-100'>
|
|
|
|
<WalletLogs wallet={wallet} embedded />
|
|
|
|
</div>
|
|
|
|
</CenterLayout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function WalletFields ({ wallet: { config, fields } }) {
|
2024-07-07 08:32:01 +00:00
|
|
|
return fields.map(({ name, label, type, help, optional, ...props }, i) => {
|
2024-06-26 12:01:54 +00:00
|
|
|
const rawProps = {
|
|
|
|
...props,
|
|
|
|
name,
|
2024-06-03 22:41:15 +00:00
|
|
|
initialValue: config?.[name],
|
2024-06-24 10:53:59 +00:00
|
|
|
label: (
|
|
|
|
<div className='d-flex align-items-center'>
|
|
|
|
{label}
|
2024-06-26 12:31:35 +00:00
|
|
|
{/* help can be a string or object to customize the label */}
|
2024-06-24 10:53:59 +00:00
|
|
|
{help && (
|
2024-06-26 12:31:35 +00:00
|
|
|
<Info label={help.label || 'help'}>
|
|
|
|
<Text>{help.text || help}</Text>
|
2024-06-24 10:53:59 +00:00
|
|
|
</Info>
|
|
|
|
)}
|
2024-06-26 12:31:35 +00:00
|
|
|
{optional && (
|
|
|
|
<small className='text-muted ms-2'>
|
2024-07-15 14:23:24 +00:00
|
|
|
{typeof optional === 'boolean' ? 'optional' : <Text>{optional}</Text>}
|
2024-06-26 12:31:35 +00:00
|
|
|
</small>
|
|
|
|
)}
|
2024-06-24 10:53:59 +00:00
|
|
|
</div>
|
|
|
|
),
|
|
|
|
required: !optional,
|
2024-06-26 12:01:54 +00:00
|
|
|
autoFocus: i === 0
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
if (type === 'text') {
|
2024-06-26 12:01:54 +00:00
|
|
|
return <ClientInput key={i} {...rawProps} />
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
if (type === 'password') {
|
2024-06-26 12:01:54 +00:00
|
|
|
return <PasswordInput key={i} {...rawProps} newPass />
|
2024-06-03 22:41:15 +00:00
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
2024-07-16 22:04:35 +00:00
|
|
|
|
|
|
|
function generateSchema (wallet, { me }) {
|
|
|
|
if (wallet.schema) return wallet.schema
|
|
|
|
|
|
|
|
const fieldValidator = (field) => {
|
|
|
|
if (!field.validate) {
|
|
|
|
// default validation
|
|
|
|
let validator = string()
|
|
|
|
if (!field.optional) validator = validator.required('required')
|
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
|
|
|
const { type: validationType } = field.validate
|
|
|
|
|
|
|
|
let validator
|
|
|
|
|
|
|
|
const stringTypes = ['url', 'string']
|
|
|
|
|
|
|
|
if (stringTypes.includes(validationType)) {
|
|
|
|
validator = string()
|
|
|
|
|
|
|
|
if (field.validate.length) {
|
|
|
|
validator = validator.length(field.validate.length)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validationType === 'url') {
|
|
|
|
validator = process.env.NODE_ENV === 'development'
|
|
|
|
? validator
|
|
|
|
.or([string().matches(/^(http:\/\/)?localhost:\d+$/), string().url()], 'invalid url')
|
|
|
|
: validator
|
|
|
|
.url()
|
|
|
|
.test(async (url, context) => {
|
|
|
|
if (field.validate.onionAllowed && TOR_REGEXP.test(url)) {
|
|
|
|
// allow HTTP and HTTPS over Tor
|
|
|
|
if (!/^https?:\/\//.test(url)) {
|
|
|
|
return context.createError({ message: 'http or https required' })
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// force HTTPS over clearnet
|
|
|
|
await string().https().validate(url)
|
|
|
|
} catch (err) {
|
|
|
|
return context.createError({ message: err.message })
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!field.optional) validator = validator.required('required')
|
|
|
|
|
|
|
|
return validator
|
|
|
|
}
|
|
|
|
|
|
|
|
return object(
|
|
|
|
wallet.fields.reduce((acc, field) => {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[field.name]: fieldValidator(field)
|
|
|
|
}
|
|
|
|
}, wallet.walletType ? autowithdrawSchemaMembers({ me }) : {})
|
|
|
|
)
|
|
|
|
}
|