stacker.news/lib/validate.js

806 lines
26 KiB
JavaScript
Raw Normal View History

import { string, ValidationError, number, object, array, addMethod, boolean, date } from 'yup'
2023-11-21 23:32:22 +00:00
import {
BOOST_MIN, MAX_POLL_CHOICE_LENGTH, MAX_TITLE_LENGTH, MAX_POLL_NUM_CHOICES,
MIN_POLL_NUM_CHOICES, MAX_FORWARDS, BOOST_MULT, MAX_TERRITORY_DESC_LENGTH, POST_TYPES,
2024-01-07 17:00:24 +00:00
TERRITORY_BILLING_TYPES, MAX_COMMENT_TEXT_LENGTH, MAX_POST_TEXT_LENGTH, MIN_TITLE_LENGTH, BOUNTY_MIN, BOUNTY_MAX, BALANCE_LIMIT_MSATS
2023-11-21 23:32:22 +00:00
} from './constants'
2023-02-08 19:38:04 +00:00
import { SUPPORTED_CURRENCIES } from './currency'
import { NOSTR_MAX_RELAY_NUM, NOSTR_PUBKEY_BECH32, NOSTR_PUBKEY_HEX } from './nostr'
Support receiving via LNbits (#1278) * Support receiving with LNbits * Remove hardcoded LNbits url on server * Fix saveConfig ignoring save errors * saveConfig was meant to only ignore validation errors, not save errors * on server save errors, we redirected as if save was successful * this is now fixed with a promise chain * logging payments vs receivals was also moved to correct place * Fix enabled falsely disabled on SSR If a wallet was configured for payments but not for receivals and you refreshed the configuration form, enabled was disabled even though payments were enabled. This was the case since we don't know during SSR if it's enabled since this information is stored on the client. * Fix missing 'receivals disabled' log message * Move 'wallet detached for payments' log message * Fix stale walletId during detach If page was reloaded, walletId in clearConfig was stale since callback dependency was missing. * Add missing callback dependencies for saveConfig * Verify that invoiceKey != adminKey * Verify LNbits keys are hex-encoded * Fix local config polluted with server data * Fix creation of duplicate wallets * Remove unused dependency * Fix missing error message in logs * Fix setPriority * Rename: localConfig -> clientConfig * Add description to LNbits autowithdrawals * Rename: receivals -> receives * Use try/catch instead of promise chain in saveConfig * add connect label to lnbits for no url found for lnbits * Fix adminKey not saved * Remove hardcoded LNbits url on server again * Add LNbits ATTACH.md * Delete old docs to attach LNbits with polar * Add missing callback dependencies * Set editable: false * Only set readOnly if field is configured --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-12 22:23:39 +00:00
import { msatsToSats, numWithUnits, abbrNum, ensureB64, B64_URL_REGEX, HEX_REGEX } from './format'
import * as usersFragments from '@/fragments/users'
import * as subsFragments from '@/fragments/subs'
import { isInvoicableMacaroon, isInvoiceMacaroon } from './macaroon'
import { TOR_REGEXP, parseNwcUrl } from './url'
import { datePivot } from './time'
import { decodeRune } from '@/lib/cln'
import bip39Words from './bip39-words'
2023-11-21 23:32:22 +00:00
const { SUB } = subsFragments
const { NAME_QUERY } = usersFragments
2023-02-08 19:38:04 +00:00
export async function ssValidate (schema, data, args) {
2023-02-08 19:38:04 +00:00
try {
if (typeof schema === 'function') {
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
return await schema(args).validate(data)
2023-02-08 19:38:04 +00:00
} else {
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
return await schema.validate(data)
2023-02-08 19:38:04 +00:00
}
} catch (e) {
2023-07-24 22:50:12 +00:00
if (e instanceof ValidationError) {
2023-02-08 19:38:04 +00:00
throw new Error(`${e.path}: ${e.message}`)
}
throw e
}
}
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
export async function formikValidate (validate, data) {
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
const result = await validate(data)
if (Object.keys(result).length > 0) {
const [key, message] = Object.entries(result)[0]
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
throw new Error(`${key}: ${message}`)
}
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
return result
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
}
Support receiving via LNbits (#1278) * Support receiving with LNbits * Remove hardcoded LNbits url on server * Fix saveConfig ignoring save errors * saveConfig was meant to only ignore validation errors, not save errors * on server save errors, we redirected as if save was successful * this is now fixed with a promise chain * logging payments vs receivals was also moved to correct place * Fix enabled falsely disabled on SSR If a wallet was configured for payments but not for receivals and you refreshed the configuration form, enabled was disabled even though payments were enabled. This was the case since we don't know during SSR if it's enabled since this information is stored on the client. * Fix missing 'receivals disabled' log message * Move 'wallet detached for payments' log message * Fix stale walletId during detach If page was reloaded, walletId in clearConfig was stale since callback dependency was missing. * Add missing callback dependencies for saveConfig * Verify that invoiceKey != adminKey * Verify LNbits keys are hex-encoded * Fix local config polluted with server data * Fix creation of duplicate wallets * Remove unused dependency * Fix missing error message in logs * Fix setPriority * Rename: localConfig -> clientConfig * Add description to LNbits autowithdrawals * Rename: receivals -> receives * Use try/catch instead of promise chain in saveConfig * add connect label to lnbits for no url found for lnbits * Fix adminKey not saved * Remove hardcoded LNbits url on server again * Add LNbits ATTACH.md * Delete old docs to attach LNbits with polar * Add missing callback dependencies * Set editable: false * Only set readOnly if field is configured --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-12 22:23:39 +00:00
export async function walletValidate (wallet, data) {
if (typeof wallet.fieldValidation === 'function') {
return await formikValidate(wallet.fieldValidation, data)
} else {
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
return await ssValidate(wallet.fieldValidation, data)
Support receiving via LNbits (#1278) * Support receiving with LNbits * Remove hardcoded LNbits url on server * Fix saveConfig ignoring save errors * saveConfig was meant to only ignore validation errors, not save errors * on server save errors, we redirected as if save was successful * this is now fixed with a promise chain * logging payments vs receivals was also moved to correct place * Fix enabled falsely disabled on SSR If a wallet was configured for payments but not for receivals and you refreshed the configuration form, enabled was disabled even though payments were enabled. This was the case since we don't know during SSR if it's enabled since this information is stored on the client. * Fix missing 'receivals disabled' log message * Move 'wallet detached for payments' log message * Fix stale walletId during detach If page was reloaded, walletId in clearConfig was stale since callback dependency was missing. * Add missing callback dependencies for saveConfig * Verify that invoiceKey != adminKey * Verify LNbits keys are hex-encoded * Fix local config polluted with server data * Fix creation of duplicate wallets * Remove unused dependency * Fix missing error message in logs * Fix setPriority * Rename: localConfig -> clientConfig * Add description to LNbits autowithdrawals * Rename: receivals -> receives * Use try/catch instead of promise chain in saveConfig * add connect label to lnbits for no url found for lnbits * Fix adminKey not saved * Remove hardcoded LNbits url on server again * Add LNbits ATTACH.md * Delete old docs to attach LNbits with polar * Add missing callback dependencies * Set editable: false * Only set readOnly if field is configured --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-12 22:23:39 +00:00
}
}
2023-07-24 22:50:12 +00:00
addMethod(string, 'or', function (schemas, msg) {
2023-02-08 19:38:04 +00:00
return this.test({
name: 'or',
message: msg,
test: value => {
if (Array.isArray(schemas) && schemas.length > 1) {
const resee = schemas.map(schema => schema.isValidSync(value))
return resee.some(res => res)
} else {
throw new TypeError('Schemas is not correct array schema')
}
},
exclusive: false
})
})
addMethod(string, 'url', function (schemas, msg = 'invalid url') {
return this.test({
name: 'url',
message: msg,
test: value => {
try {
// eslint-disable-next-line no-new
new URL(value)
return true
} catch (e) {
try {
// eslint-disable-next-line no-new
new URL(`http://${value}`)
return true
} catch (e) {
return false
}
}
},
exclusive: false
})
})
addMethod(string, 'ws', function (schemas, msg = 'invalid websocket') {
return this.test({
name: 'ws',
message: msg,
test: value => {
2024-02-14 17:53:41 +00:00
if (typeof value === 'undefined') return true
try {
2024-02-14 17:53:41 +00:00
const url = new URL(value)
return url.protocol === 'ws:' || url.protocol === 'wss:'
} catch (e) {
return false
}
},
exclusive: false
})
})
addMethod(string, 'socket', function (schemas, msg = 'invalid socket') {
return this.test({
name: 'socket',
message: msg,
test: value => {
try {
const url = new URL(`http://${value}`)
return url.hostname && url.port && !url.username && !url.password &&
(!url.pathname || url.pathname === '/') && !url.search && !url.hash
} catch (e) {
return false
}
},
exclusive: false
})
})
addMethod(string, 'https', function () {
return this.test({
name: 'https',
message: 'https required',
test: (url) => {
try {
return new URL(url).protocol === 'https:'
} catch {
return false
}
}
})
})
addMethod(string, 'wss', function (msg) {
return this.test({
name: 'wss',
message: msg || 'wss required',
test: (url) => {
try {
return new URL(url).protocol === 'wss:'
} catch {
return false
}
}
})
})
Support receiving via LNbits (#1278) * Support receiving with LNbits * Remove hardcoded LNbits url on server * Fix saveConfig ignoring save errors * saveConfig was meant to only ignore validation errors, not save errors * on server save errors, we redirected as if save was successful * this is now fixed with a promise chain * logging payments vs receivals was also moved to correct place * Fix enabled falsely disabled on SSR If a wallet was configured for payments but not for receivals and you refreshed the configuration form, enabled was disabled even though payments were enabled. This was the case since we don't know during SSR if it's enabled since this information is stored on the client. * Fix missing 'receivals disabled' log message * Move 'wallet detached for payments' log message * Fix stale walletId during detach If page was reloaded, walletId in clearConfig was stale since callback dependency was missing. * Add missing callback dependencies for saveConfig * Verify that invoiceKey != adminKey * Verify LNbits keys are hex-encoded * Fix local config polluted with server data * Fix creation of duplicate wallets * Remove unused dependency * Fix missing error message in logs * Fix setPriority * Rename: localConfig -> clientConfig * Add description to LNbits autowithdrawals * Rename: receivals -> receives * Use try/catch instead of promise chain in saveConfig * add connect label to lnbits for no url found for lnbits * Fix adminKey not saved * Remove hardcoded LNbits url on server again * Add LNbits ATTACH.md * Delete old docs to attach LNbits with polar * Add missing callback dependencies * Set editable: false * Only set readOnly if field is configured --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-12 22:23:39 +00:00
addMethod(string, 'hex', function (msg) {
return this.test({
name: 'hex',
message: msg || 'invalid hex encoding',
test: (value) => !value || HEX_REGEX.test(value)
})
})
Support receiving with NWC (#1310) * Add NWC receives * Refactor sendPayment+createInvoice with nwcCall function * Update badge * Add method support checks * Add timeout to NWC test invoice * Fix NWC isConfigured state All NWC fields are marked as optional but NWC should only be considered configured if one of them is set. * Fix relay.fetch() throws 'crypto is not defined' in node nip04.encrypt() was failing in worker because 'crypto is not defined'. Updating to nostr-tools v2.7.2 fixed that. However, now crypto.randomUUID() in relay.fetch() was throwing 'crypto is not defined'. Importing crypto from 'crypto' fixed that. However, with the import, randomUUID() does not work so I switched to randomBytes(). Running relay.fetch() now works in browser and node. * recv must not support pay_invoice * Fix Relay connection check * this.url was undefined * error was an object * Fix additional isConfigured check runs always It was meant to only catch false positives, not turn negatives into false positives. * Rename testConnectServer to testCreateInvoice * Rename testConnectClient to testSendPayment * Only run testSendPayment if send is configured The return value of testSendPayment was used before but it only returned something for LNC. And for LNC, we only wanted to save the transformation during validation, so it was not needed. * Always use withTimeout in NWC test functions * Fix fragment name * Use get_info command exclusively * Check permissions more efficiently * Log NWC request-response flow * Fix variable name * Call ws.send after listener is added * Fix websocket not closed after timeout * Also check that pay_keysend etc. are not supported * fix lnc session key save --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: k00b <k00b@stacker.news>
2024-08-21 15:13:27 +00:00
addMethod(string, 'nwcUrl', function () {
return this.test({
test: async (nwcUrl, context) => {
if (!nwcUrl) return true
// run validation in sequence to control order of errors
// inspired by https://github.com/jquense/yup/issues/851#issuecomment-1049705180
try {
await string().matches(/^nostr\+?walletconnect:\/\//, { message: 'must start with nostr+walletconnect://' }).validate(nwcUrl)
let relayUrl, walletPubkey, secret
try {
({ relayUrl, walletPubkey, secret } = parseNwcUrl(nwcUrl))
} catch {
// invalid URL error. handle as if pubkey validation failed to not confuse user.
throw new Error('pubkey must be 64 hex chars')
}
await string().required('pubkey required').trim().matches(NOSTR_PUBKEY_HEX, 'pubkey must be 64 hex chars').validate(walletPubkey)
await string().required('relay url required').trim().wss('relay must use wss://').validate(relayUrl)
await string().required('secret required').trim().matches(/^[0-9a-fA-F]{64}$/, 'secret must be 64 hex chars').validate(secret)
} catch (err) {
return context.createError({ message: err.message })
}
return true
}
})
})
2023-07-24 22:50:12 +00:00
const titleValidator = string().required('required').trim().max(
2023-02-08 19:38:04 +00:00
MAX_TITLE_LENGTH,
({ max, value }) => `-${Math.abs(max - value.length)} characters remaining`
2023-12-08 20:31:06 +00:00
).min(MIN_TITLE_LENGTH, `must be at least ${MIN_TITLE_LENGTH} characters`)
2023-02-08 19:38:04 +00:00
2023-12-04 19:20:56 +00:00
const textValidator = (max) => string().trim().max(
max,
({ max, value }) => `-${Math.abs(max - value.length)} characters remaining`
)
2023-11-21 23:32:22 +00:00
const nameValidator = string()
.required('required')
.matches(/^[\w_]+$/, 'only letters, numbers, and _')
.max(32, 'too long')
2023-12-04 19:20:56 +00:00
2023-07-24 22:50:12 +00:00
const intValidator = number().typeError('must be a number').integer('must be whole')
2024-01-07 17:00:24 +00:00
const floatValidator = number().typeError('must be a number')
const lightningAddressValidator = process.env.NODE_ENV === 'development'
? string().or(
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
[string().matches(/^[\w_]+@localhost:\d+$/), string().matches(/^[\w_]+@app:\d+$/), string().email()],
2024-01-07 17:00:24 +00:00
'address is no good')
: string().email('address is no good')
2023-02-08 19:38:04 +00:00
const hexOrBase64Validator = string().test({
name: 'hex-or-base64',
message: 'invalid encoding',
test: (val) => {
2024-04-11 23:59:51 +00:00
if (typeof val === 'undefined') return true
try {
ensureB64(val)
return true
} catch {
return false
}
}
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
}).transform(val => {
try {
return ensureB64(val)
} catch {
return val
}
})
async function usernameExists (name, { client, models }) {
if (!client && !models) {
2023-02-08 19:38:04 +00:00
throw new Error('cannot check for user')
}
// apollo client
if (client) {
const { data } = await client.query({ query: NAME_QUERY, variables: { name } })
2023-02-08 19:38:04 +00:00
return !data.nameAvailable
}
// prisma client
const user = await models.user.findUnique({ where: { name } })
2023-02-08 19:38:04 +00:00
return !!user
}
async function subExists (name, { client, models, me, filter }) {
2023-11-21 23:32:22 +00:00
if (!client && !models) {
throw new Error('cannot check for territory')
}
let sub
// apollo client
if (client) {
const { data } = await client.query({ query: SUB, variables: { sub: name }, fetchPolicy: 'no-cache' })
2023-11-21 23:32:22 +00:00
sub = data?.sub
} else {
sub = await models.sub.findUnique({ where: { name } })
}
return !!sub && (!filter || filter(sub))
2023-11-21 23:32:22 +00:00
}
async function subActive (name, { client, models, me }) {
if (!client && !models) {
throw new Error('cannot check if territory is active')
}
let sub
// apollo client
if (client) {
const { data } = await client.query({ query: SUB, variables: { sub: name } })
sub = data?.sub
} else {
sub = await models.sub.findUnique({ where: { name } })
}
return sub ? sub.status !== 'STOPPED' : undefined
2023-11-21 23:32:22 +00:00
}
async function subHasPostType (name, type, { client, models }) {
if (!client && !models) {
throw new Error('cannot check for territory')
}
// apollo client
if (client) {
const { data } = await client.query({ query: SUB, variables: { name } })
return !!(data?.sub?.postTypes?.includes(type))
}
// prisma client
const sub = await models.sub.findUnique({ where: { name } })
return !!(sub?.postTypes?.includes(type))
}
export function advPostSchemaMembers ({ me, existingBoost = 0, ...args }) {
const boostMin = existingBoost || BOOST_MIN
2023-02-08 19:38:04 +00:00
return {
boost: intValidator
.min(boostMin, `must be ${existingBoost ? '' : 'blank or '}at least ${boostMin}`).test({
2023-02-08 19:38:04 +00:00
name: 'boost',
test: async boost => (!existingBoost && !boost) || boost % BOOST_MULT === 0,
message: `must be divisble be ${BOOST_MULT}`
2023-02-08 19:38:04 +00:00
}),
multiple forwards on a post (#403) * multiple forwards on a post first phase of the multi-forward support * update the graphql mutation for discussion posts to accept and validate multiple forwards * update the discussion form to allow multiple forwards in the UI * start working on db schema changes * uncomment db schema, add migration to create the new model, and update create_item, update_item stored procedures * Propagate updates from discussion to poll, link, and bounty forms Update the create, update poll sql functions for multi forward support * Update gql, typedefs, and resolver to return forwarded users in items responses * UI changes to show multiple forward recipients, and conditional upvote logic changes * Update notification text to reflect multiple forwards upon vote action * Disallow duplicate stacker entries * reduce duplication in populating adv-post-form initial values * Update item_act sql function to implement multi-way forwarding * Update referral functions to scale referral bonuses for forwarded users * Update notification text to reflect non-100% forwarded sats cases * Update wallet history sql queries to accommodate multi-forward use cases * Block zaps for posts you are forwarded zaps at the API layer, in addition to in the UI * Delete fwdUserId column from Item table as part of migration * Fix how we calculate stacked sats after partial forwards in wallet history * Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users * Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct * Reduce duplication in adv post form, and do some style tweaks for better layout * Use MAX_FORWARDS constants * Address various PR feedback * first enhancement pass * enhancement pass too --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
forward: array()
.max(MAX_FORWARDS, `you can only configure ${MAX_FORWARDS} forward recipients`)
.of(object().shape({
nym: string().required('must specify a stacker')
.test({
name: 'nym',
test: async name => {
if (!name || !name.length) return false
return await usernameExists(name, args)
},
message: 'stacker does not exist'
})
.test({
name: 'self',
test: async name => {
return me?.name !== name
},
message: 'cannot forward to yourself'
}),
multiple forwards on a post (#403) * multiple forwards on a post first phase of the multi-forward support * update the graphql mutation for discussion posts to accept and validate multiple forwards * update the discussion form to allow multiple forwards in the UI * start working on db schema changes * uncomment db schema, add migration to create the new model, and update create_item, update_item stored procedures * Propagate updates from discussion to poll, link, and bounty forms Update the create, update poll sql functions for multi forward support * Update gql, typedefs, and resolver to return forwarded users in items responses * UI changes to show multiple forward recipients, and conditional upvote logic changes * Update notification text to reflect multiple forwards upon vote action * Disallow duplicate stacker entries * reduce duplication in populating adv-post-form initial values * Update item_act sql function to implement multi-way forwarding * Update referral functions to scale referral bonuses for forwarded users * Update notification text to reflect non-100% forwarded sats cases * Update wallet history sql queries to accommodate multi-forward use cases * Block zaps for posts you are forwarded zaps at the API layer, in addition to in the UI * Delete fwdUserId column from Item table as part of migration * Fix how we calculate stacked sats after partial forwards in wallet history * Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users * Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct * Reduce duplication in adv post form, and do some style tweaks for better layout * Use MAX_FORWARDS constants * Address various PR feedback * first enhancement pass * enhancement pass too --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
pct: intValidator.required('must specify a percentage').min(1, 'percentage must be at least 1').max(100, 'percentage must not exceed 100')
}))
.compact((v) => !v.nym && !v.pct)
2023-02-08 19:38:04 +00:00
.test({
multiple forwards on a post (#403) * multiple forwards on a post first phase of the multi-forward support * update the graphql mutation for discussion posts to accept and validate multiple forwards * update the discussion form to allow multiple forwards in the UI * start working on db schema changes * uncomment db schema, add migration to create the new model, and update create_item, update_item stored procedures * Propagate updates from discussion to poll, link, and bounty forms Update the create, update poll sql functions for multi forward support * Update gql, typedefs, and resolver to return forwarded users in items responses * UI changes to show multiple forward recipients, and conditional upvote logic changes * Update notification text to reflect multiple forwards upon vote action * Disallow duplicate stacker entries * reduce duplication in populating adv-post-form initial values * Update item_act sql function to implement multi-way forwarding * Update referral functions to scale referral bonuses for forwarded users * Update notification text to reflect non-100% forwarded sats cases * Update wallet history sql queries to accommodate multi-forward use cases * Block zaps for posts you are forwarded zaps at the API layer, in addition to in the UI * Delete fwdUserId column from Item table as part of migration * Fix how we calculate stacked sats after partial forwards in wallet history * Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users * Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct * Reduce duplication in adv post form, and do some style tweaks for better layout * Use MAX_FORWARDS constants * Address various PR feedback * first enhancement pass * enhancement pass too --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
name: 'sum',
test: forwards => forwards ? forwards.map(fwd => Number(fwd.pct)).reduce((sum, cur) => sum + cur, 0) <= 100 : true,
multiple forwards on a post (#403) * multiple forwards on a post first phase of the multi-forward support * update the graphql mutation for discussion posts to accept and validate multiple forwards * update the discussion form to allow multiple forwards in the UI * start working on db schema changes * uncomment db schema, add migration to create the new model, and update create_item, update_item stored procedures * Propagate updates from discussion to poll, link, and bounty forms Update the create, update poll sql functions for multi forward support * Update gql, typedefs, and resolver to return forwarded users in items responses * UI changes to show multiple forward recipients, and conditional upvote logic changes * Update notification text to reflect multiple forwards upon vote action * Disallow duplicate stacker entries * reduce duplication in populating adv-post-form initial values * Update item_act sql function to implement multi-way forwarding * Update referral functions to scale referral bonuses for forwarded users * Update notification text to reflect non-100% forwarded sats cases * Update wallet history sql queries to accommodate multi-forward use cases * Block zaps for posts you are forwarded zaps at the API layer, in addition to in the UI * Delete fwdUserId column from Item table as part of migration * Fix how we calculate stacked sats after partial forwards in wallet history * Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users * Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct * Reduce duplication in adv post form, and do some style tweaks for better layout * Use MAX_FORWARDS constants * Address various PR feedback * first enhancement pass * enhancement pass too --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
message: 'the total forward percentage exceeds 100%'
})
.test({
name: 'uniqueStackers',
test: forwards => forwards ? new Set(forwards.map(fwd => fwd.nym)).size === forwards.length : true,
multiple forwards on a post (#403) * multiple forwards on a post first phase of the multi-forward support * update the graphql mutation for discussion posts to accept and validate multiple forwards * update the discussion form to allow multiple forwards in the UI * start working on db schema changes * uncomment db schema, add migration to create the new model, and update create_item, update_item stored procedures * Propagate updates from discussion to poll, link, and bounty forms Update the create, update poll sql functions for multi forward support * Update gql, typedefs, and resolver to return forwarded users in items responses * UI changes to show multiple forward recipients, and conditional upvote logic changes * Update notification text to reflect multiple forwards upon vote action * Disallow duplicate stacker entries * reduce duplication in populating adv-post-form initial values * Update item_act sql function to implement multi-way forwarding * Update referral functions to scale referral bonuses for forwarded users * Update notification text to reflect non-100% forwarded sats cases * Update wallet history sql queries to accommodate multi-forward use cases * Block zaps for posts you are forwarded zaps at the API layer, in addition to in the UI * Delete fwdUserId column from Item table as part of migration * Fix how we calculate stacked sats after partial forwards in wallet history * Exclude entries from wallet history that are 0 stacked sats from posts with 100% forwarded to other users * Fix wallet history query for forwarded stacked sats to be scaled by the fwd pct * Reduce duplication in adv post form, and do some style tweaks for better layout * Use MAX_FORWARDS constants * Address various PR feedback * first enhancement pass * enhancement pass too --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2023-08-23 22:44:17 +00:00
message: 'duplicate stackers cannot be specified to receive forwarded sats'
2023-02-08 19:38:04 +00:00
})
}
}
2023-11-21 23:32:22 +00:00
export function subSelectSchemaMembers (args) {
2023-05-11 00:26:07 +00:00
return {
2023-11-21 23:32:22 +00:00
sub: string().required('required').test({
name: 'sub',
test: async sub => {
if (!sub || !sub.length) return false
return await subExists(sub, args)
2023-11-21 23:32:22 +00:00
},
message: 'pick valid territory'
}).test({
name: 'sub',
test: async sub => {
if (!sub || !sub.length) return false
return await subActive(sub, args)
2023-11-21 23:32:22 +00:00
},
message: 'territory is not active'
})
2023-05-11 00:26:07 +00:00
}
}
// for testing advPostSchemaMembers in isolation
export function advSchema (args) {
return object({
...advPostSchemaMembers(args)
})
}
2023-05-11 00:26:07 +00:00
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
export const autowithdrawSchemaMembers = {
enabled: boolean(),
autoWithdrawThreshold: intValidator.required('required').min(0, 'must be at least 0').max(msatsToSats(BALANCE_LIMIT_MSATS), `must be at most ${abbrNum(msatsToSats(BALANCE_LIMIT_MSATS))}`),
autoWithdrawMaxFeePercent: floatValidator.required('required').min(0, 'must be at least 0').max(50, 'must not exceed 50')
}
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
export const lnAddrAutowithdrawSchema = object({
address: lightningAddressValidator.required('required').test({
name: 'address',
test: addr => !addr.endsWith('@stacker.news'),
message: 'automated withdrawals must be external'
}),
...autowithdrawSchemaMembers
})
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
export const LNDAutowithdrawSchema = object({
socket: string().socket().required('required'),
macaroon: hexOrBase64Validator.required('required').test({
name: 'macaroon',
test: v => isInvoiceMacaroon(v) || isInvoicableMacaroon(v),
message: 'not an invoice macaroon or an invoicable macaroon'
}),
cert: hexOrBase64Validator,
...autowithdrawSchemaMembers
})
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
export const CLNAutowithdrawSchema = object({
socket: string().socket().required('required'),
rune: string().matches(B64_URL_REGEX, { message: 'invalid rune' }).required('required')
.test({
name: 'rune',
test: (v, context) => {
const decoded = decodeRune(v)
if (!decoded) return context.createError({ message: 'invalid rune' })
if (decoded.restrictions.length === 0) {
return context.createError({ message: 'rune must be restricted to method=invoice' })
}
if (decoded.restrictions.length !== 1 || decoded.restrictions[0].alternatives.length !== 1) {
return context.createError({ message: 'rune must be restricted to method=invoice only' })
}
if (decoded.restrictions[0].alternatives[0] !== 'method=invoice') {
return context.createError({ message: 'rune must be restricted to method=invoice only' })
}
return true
}
}),
cert: hexOrBase64Validator,
...autowithdrawSchemaMembers
})
2024-01-07 17:00:24 +00:00
export function bountySchema (args) {
2023-07-24 22:50:12 +00:00
return object({
2023-02-08 19:38:04 +00:00
title: titleValidator,
2023-12-04 19:20:56 +00:00
text: textValidator(MAX_POST_TEXT_LENGTH),
2023-02-08 19:38:04 +00:00
bounty: intValidator
2023-12-27 16:23:54 +00:00
.min(BOUNTY_MIN, `must be at least ${numWithUnits(BOUNTY_MIN)}`)
.max(BOUNTY_MAX, `must be at most ${numWithUnits(BOUNTY_MAX)}`),
...advPostSchemaMembers(args),
2023-11-21 23:32:22 +00:00
...subSelectSchemaMembers(args)
}).test({
name: 'post-type-supported',
test: ({ sub }) => subHasPostType(sub, 'BOUNTY', args),
message: 'territory does not support bounties'
2023-02-08 19:38:04 +00:00
})
}
export function discussionSchema (args) {
2023-07-24 22:50:12 +00:00
return object({
2023-02-08 19:38:04 +00:00
title: titleValidator,
2023-12-04 19:20:56 +00:00
text: textValidator(MAX_POST_TEXT_LENGTH),
...advPostSchemaMembers(args),
2023-11-21 23:32:22 +00:00
...subSelectSchemaMembers(args)
}).test({
name: 'post-type-supported',
test: ({ sub }) => subHasPostType(sub, 'DISCUSSION', args),
message: 'territory does not support discussions'
2023-02-08 19:38:04 +00:00
})
}
export function linkSchema (args) {
2023-07-24 22:50:12 +00:00
return object({
2023-02-08 19:38:04 +00:00
title: titleValidator,
2023-12-04 19:20:56 +00:00
text: textValidator(MAX_POST_TEXT_LENGTH),
url: string().url().required('required'),
...advPostSchemaMembers(args),
2023-11-21 23:32:22 +00:00
...subSelectSchemaMembers(args)
}).test({
name: 'post-type-supported',
test: ({ sub }) => subHasPostType(sub, 'LINK', args),
message: 'territory does not support links'
2023-02-08 19:38:04 +00:00
})
}
export function pollSchema ({ numExistingChoices = 0, ...args }) {
2023-07-24 22:50:12 +00:00
return object({
2023-02-08 19:38:04 +00:00
title: titleValidator,
2023-12-04 19:20:56 +00:00
text: textValidator(MAX_POST_TEXT_LENGTH),
2023-07-24 22:50:12 +00:00
options: array().of(
string().trim().test('my-test', 'required', function (value) {
2023-02-08 19:38:04 +00:00
return (this.path !== 'options[0]' && this.path !== 'options[1]') || value
}).max(MAX_POLL_CHOICE_LENGTH,
({ max, value }) => `-${Math.abs(max - value.length)} characters remaining`
2023-02-08 19:38:04 +00:00
)
).test({
message: `at most ${MAX_POLL_NUM_CHOICES} choices`,
test: arr => arr.length <= MAX_POLL_NUM_CHOICES - numExistingChoices
}).test({
message: `at least ${MIN_POLL_NUM_CHOICES} choices required`,
test: arr => arr.length >= MIN_POLL_NUM_CHOICES - numExistingChoices
}),
pollExpiresAt: date().nullable().min(datePivot(new Date(), { days: 1 }), 'Expiration must be at least 1 day in the future'),
...advPostSchemaMembers(args),
2023-11-21 23:32:22 +00:00
...subSelectSchemaMembers(args)
}).test({
name: 'post-type-supported',
test: ({ sub }) => subHasPostType(sub, 'POLL', args),
message: 'territory does not support polls'
})
}
export function territorySchema (args) {
return object({
name: nameValidator
.test({
name: 'name',
test: async name => {
if (!name || !name.length) return false
const editing = !!args.sub?.name
// don't block submission on edits or unarchival
const isEdit = sub => sub.name === args.sub.name
const isArchived = sub => sub.status === 'STOPPED'
const filter = sub => editing ? !isEdit(sub) : !isArchived(sub)
const exists = await subExists(name, { ...args, filter })
return !exists
2023-11-21 23:32:22 +00:00
},
message: 'taken'
}),
desc: string().required('required').trim().max(
MAX_TERRITORY_DESC_LENGTH,
({ max, value }) => `-${Math.abs(max - value.length)} characters remaining`
),
baseCost: intValidator
.min(1, 'must be at least 1')
.max(100000, 'must be at most 100k'),
postTypes: array().of(string().oneOf(POST_TYPES)).min(1, 'must support at least one post type'),
billingType: string().required('required').oneOf(TERRITORY_BILLING_TYPES, 'required'),
nsfw: boolean()
2023-02-08 19:38:04 +00:00
})
}
Territory transfers (#878) * Allow founders to transfer territories * Log territory transfers in new AuditLog table * Add territory transfer notifications * Use polymorphic AuditEvent table * Add setting for territory transfer notifications * Add push notification * Rename label from user to stacker * More space between cancel and confirm button * Remove AuditEvent table The audit table is not necessary for territory transfers and only adds complexity and unrelated discussion to this PR. Thinking about a future-proof schema for territory transfers and how/what to audit at the same time made my head spin. Some thoughts I had: 1. Maybe using polymorphism for an audit log / audit events is not a good idea Using polymorphism as is currently used in the code base (user wallets) means that every generic event must map to exactly one specialized event. Is this a good requirement/assumption? It already didn't work well for naive auditing of territory transfers since we want events to be indexable by user (no array column) so every event needs to point to a single user but a territory transfer involves multiple users. This made me wonder: Do we even need a table? Maybe the audit log for a user can be implemented using a view? This would also mean no data denormalization. 2. What to audit and how and why? Most actions are already tracked in some way by necessity: zaps, items, mutes, payments, ... In that case: what is the benefit of tracking these things individually in a separate table? Denormalize simply for convenience or performance? Why no view (see previous point)? Use case needs to be more clearly defined before speccing out a schema. * Fix territory transfer notification id conflict * Use include instead of two separate queries * Drop territory transfer setting * Remove trigger usage * Prevent transfers to yourself
2024-03-05 19:56:02 +00:00
export function territoryTransferSchema ({ me, ...args }) {
return object({
userName: nameValidator
.test({
name: 'name',
test: async name => {
if (!name || !name.length) return false
return await usernameExists(name, args)
},
message: 'user does not exist'
})
.test({
name: 'name',
test: name => !me || me.name !== name,
message: 'cannot transfer to yourself'
})
})
}
export function userSchema (args) {
2023-07-24 22:50:12 +00:00
return object({
2023-11-21 23:32:22 +00:00
name: nameValidator
2023-02-08 19:38:04 +00:00
.test({
name: 'name',
test: async name => {
if (!name || !name.length) return false
return !(await usernameExists(name, args))
2023-02-08 19:38:04 +00:00
},
message: 'taken'
})
})
}
2023-07-24 22:50:12 +00:00
export const commentSchema = object({
2023-12-04 19:20:56 +00:00
text: textValidator(MAX_COMMENT_TEXT_LENGTH).required('required')
2023-02-08 19:38:04 +00:00
})
2023-07-24 22:50:12 +00:00
export const jobSchema = object({
2023-02-08 19:38:04 +00:00
title: titleValidator,
2023-07-24 22:50:12 +00:00
company: string().required('required').trim(),
2023-12-04 19:20:56 +00:00
text: textValidator(MAX_POST_TEXT_LENGTH).required('required'),
2023-07-24 22:50:12 +00:00
url: string()
.or([string().email(), string().url()], 'invalid url or email')
2023-02-08 19:38:04 +00:00
.required('required'),
maxBid: intValidator.min(0, 'must be at least 0').required('required'),
2023-07-24 22:50:12 +00:00
location: string().test(
2023-02-08 19:38:04 +00:00
'no-remote',
"don't write remote, just check the box",
v => !v?.match(/\bremote\b/gi))
.when('remote', {
is: (value) => !value,
2023-07-27 00:18:42 +00:00
then: schema => schema.required('required').trim()
2023-02-08 19:38:04 +00:00
})
})
2023-07-24 22:50:12 +00:00
export const emailSchema = object({
email: string().email('email is no good').required('required')
2023-02-08 19:38:04 +00:00
})
2023-07-24 22:50:12 +00:00
export const urlSchema = object({
url: string().url().required('required')
2023-02-08 19:38:04 +00:00
})
2023-07-24 22:50:12 +00:00
export const namedUrlSchema = object({
text: string().required('required').trim(),
url: string().url().required('required')
2023-02-08 19:38:04 +00:00
})
2023-07-24 22:50:12 +00:00
export const amountSchema = object({
2023-02-08 19:38:04 +00:00
amount: intValidator.required('required').positive('must be positive')
})
2023-12-26 22:51:47 +00:00
export const actSchema = object({
sats: intValidator.required('required').positive('must be positive'),
act: string().required('required').oneOf(['TIP', 'DONT_LIKE_THIS'])
})
export const settingsSchema = object().shape({
2023-02-08 19:38:04 +00:00
tipDefault: intValidator.required('required').positive('must be positive'),
tipRandom: boolean(),
tipRandomMin: intValidator.nullable().positive('must be positive')
.when(['tipRandom', 'tipRandomMax'], ([enabled, max], schema) => {
let res = schema
if (!enabled) return res
if (max) {
res = schema.required('minimum and maximum must either both be omitted or specified').nonNullable()
}
return res.lessThan(max, 'must be less than maximum')
}),
tipRandomMax: intValidator.nullable().positive('must be positive')
.when(['tipRandom', 'tipRandomMin'], ([enabled, min], schema) => {
let res = schema
if (!enabled) return res
if (min) {
res = schema.required('minimum and maximum must either both be omitted or specified').nonNullable()
}
return res.moreThan(min, 'must be more than minimum')
}),
2023-07-24 22:50:12 +00:00
fiatCurrency: string().required('required').oneOf(SUPPORTED_CURRENCIES),
withdrawMaxFeeDefault: intValidator.required('required').positive('must be positive'),
2023-07-24 22:50:12 +00:00
nostrPubkey: string().nullable()
2023-02-08 19:38:04 +00:00
.or([
2023-07-24 22:50:12 +00:00
string().nullable().matches(NOSTR_PUBKEY_HEX, 'must be 64 hex chars'),
string().nullable().matches(NOSTR_PUBKEY_BECH32, 'invalid bech32 encoding')], 'invalid pubkey'),
nostrRelays: array().of(
string().ws()
2023-02-08 19:38:04 +00:00
).max(NOSTR_MAX_RELAY_NUM,
({ max, value }) => `${Math.abs(max - value.length)} too many`),
hideBookmarks: boolean(),
hideGithub: boolean(),
hideNostr: boolean(),
hideTwitter: boolean(),
hideWalletBalance: boolean(),
diagnostics: boolean(),
noReferralLinks: boolean(),
2024-03-25 00:46:12 +00:00
hideIsContributor: boolean(),
disableFreebies: boolean().nullable(),
satsFilter: intValidator.required('required').min(0, 'must be at least 0').max(1000, 'must be at most 1000'),
2024-03-25 08:53:34 +00:00
zapUndos: intValidator.nullable().min(0, 'must be greater or equal to 0')
// exclude from cyclic analysis. see https://github.com/jquense/yup/issues/720
}, [['tipRandomMax', 'tipRandomMin']])
2023-02-08 19:38:04 +00:00
const warningMessage = 'If I logout, even accidentally, I will never be able to access my account again'
2023-07-24 22:50:12 +00:00
export const lastAuthRemovalSchema = object({
warning: string().matches(warningMessage, 'does not match').required('required')
2023-02-08 19:38:04 +00:00
})
2023-07-24 22:50:12 +00:00
export const withdrawlSchema = object({
invoice: string().required('required').trim(),
2023-02-08 19:38:04 +00:00
maxFee: intValidator.required('required').min(0, 'must be at least 0')
})
export const lnAddrSchema = ({ payerData, min, max, commentAllowed } = {}) =>
object({
2024-01-07 17:00:24 +00:00
addr: lightningAddressValidator.required('required'),
amount: (() => {
const schema = intValidator.required('required').positive('must be positive').min(
min || 1, `must be at least ${min || 1}`)
return max ? schema.max(max, `must be at most ${max}`) : schema
})(),
maxFee: intValidator.required('required').min(0, 'must be at least 0'),
comment: commentAllowed
? string().max(commentAllowed, `must be less than ${commentAllowed}`)
: string()
}).concat(object().shape(Object.keys(payerData || {}).reduce((accum, key) => {
const entry = payerData[key]
if (key === 'email') {
accum[key] = string().email()
} else if (key === 'identifier') {
accum[key] = boolean()
} else {
accum[key] = string()
}
if (entry?.mandatory) {
accum[key] = accum[key].required()
}
return accum
}, {})))
2023-02-08 19:38:04 +00:00
Support receiving via LNbits (#1278) * Support receiving with LNbits * Remove hardcoded LNbits url on server * Fix saveConfig ignoring save errors * saveConfig was meant to only ignore validation errors, not save errors * on server save errors, we redirected as if save was successful * this is now fixed with a promise chain * logging payments vs receivals was also moved to correct place * Fix enabled falsely disabled on SSR If a wallet was configured for payments but not for receivals and you refreshed the configuration form, enabled was disabled even though payments were enabled. This was the case since we don't know during SSR if it's enabled since this information is stored on the client. * Fix missing 'receivals disabled' log message * Move 'wallet detached for payments' log message * Fix stale walletId during detach If page was reloaded, walletId in clearConfig was stale since callback dependency was missing. * Add missing callback dependencies for saveConfig * Verify that invoiceKey != adminKey * Verify LNbits keys are hex-encoded * Fix local config polluted with server data * Fix creation of duplicate wallets * Remove unused dependency * Fix missing error message in logs * Fix setPriority * Rename: localConfig -> clientConfig * Add description to LNbits autowithdrawals * Rename: receivals -> receives * Use try/catch instead of promise chain in saveConfig * add connect label to lnbits for no url found for lnbits * Fix adminKey not saved * Remove hardcoded LNbits url on server again * Add LNbits ATTACH.md * Delete old docs to attach LNbits with polar * Add missing callback dependencies * Set editable: false * Only set readOnly if field is configured --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-12 22:23:39 +00:00
export const lnbitsSchema = object().shape({
url: process.env.NODE_ENV === 'development'
? string()
.or([string().matches(/^(http:\/\/)?localhost:\d+$/), string().url()], 'invalid url')
.required('required').trim()
: string().url().required('required').trim()
.test(async (url, context) => {
if (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
}),
Support receiving via LNbits (#1278) * Support receiving with LNbits * Remove hardcoded LNbits url on server * Fix saveConfig ignoring save errors * saveConfig was meant to only ignore validation errors, not save errors * on server save errors, we redirected as if save was successful * this is now fixed with a promise chain * logging payments vs receivals was also moved to correct place * Fix enabled falsely disabled on SSR If a wallet was configured for payments but not for receivals and you refreshed the configuration form, enabled was disabled even though payments were enabled. This was the case since we don't know during SSR if it's enabled since this information is stored on the client. * Fix missing 'receivals disabled' log message * Move 'wallet detached for payments' log message * Fix stale walletId during detach If page was reloaded, walletId in clearConfig was stale since callback dependency was missing. * Add missing callback dependencies for saveConfig * Verify that invoiceKey != adminKey * Verify LNbits keys are hex-encoded * Fix local config polluted with server data * Fix creation of duplicate wallets * Remove unused dependency * Fix missing error message in logs * Fix setPriority * Rename: localConfig -> clientConfig * Add description to LNbits autowithdrawals * Rename: receivals -> receives * Use try/catch instead of promise chain in saveConfig * add connect label to lnbits for no url found for lnbits * Fix adminKey not saved * Remove hardcoded LNbits url on server again * Add LNbits ATTACH.md * Delete old docs to attach LNbits with polar * Add missing callback dependencies * Set editable: false * Only set readOnly if field is configured --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-12 22:23:39 +00:00
adminKey: string().length(32).hex()
.when(['invoiceKey'], ([invoiceKey], schema) => {
if (!invoiceKey) return schema.required('required if invoice key not set')
return schema.test({
test: adminKey => adminKey !== invoiceKey,
message: 'admin key cannot be the same as invoice key'
})
}),
invoiceKey: string().length(32).hex()
.when(['adminKey'], ([adminKey], schema) => {
if (!adminKey) return schema.required('required if admin key not set')
return schema.test({
test: invoiceKey => adminKey !== invoiceKey,
message: 'invoice key cannot be the same as admin key'
})
})
// need to set order to avoid cyclic dependencies in Yup schema
// see https://github.com/jquense/yup/issues/176#issuecomment-367352042
}, ['adminKey', 'invoiceKey'])
Expose WebLN interface via React Context (#749) * Add LNbits card * Save LNbits Provider in WebLN context * Check LNbits connection on save * refactor: put LNbitsProvider into own file * Pay invoices using WebLN provider from context * Remove deprecated FIXME * Try WebLN provider first * Fix unhandled promise rejection * Fix this in sendPayment * Be optimistic regarding WebLN zaps This wraps the WebLN payment promise with Apollo cache updates. We will be optimistics and assume that the payment will succeed and update the cache accordingly. When we notice that the payment failed, we undo this update. * Bold strike on WebLN zap If lightning strike animation is disabled, toaster will be used. * Rename undo variable to amount * Fix zap undo * Add NWC card * Attempt to check NWC connection using info event * Fix NaN on zap Third argument of update is reserved for context * Fix TypeError in catch of QR code * Add basic NWC payments * Wrap LNbits getInfo with try/catch * EOSE is enough to check NWC connection * refactor: Wrap WebLN providers into own context I should have done this earlier * Show red indicator on error * Fix useEffect return value * Fix wrong usage of pubkey The event pubkey is derived from the secret. Doesn't make sense to manually set it. It's also the wrong pubkey: we're not the wallet service. * Use p tag in NWC request * Add comment about required filter field * Aesthetic changes to NWC sendPayment * Add TODO about receipt verification * Fix WebLN attempted again after error * Fix undefined name * Add code to mock NWC relay * Revert "Bold strike on WebLN zap" This reverts commit a9eb27daec0cd2ef30b56294b05e0056fb5b4184. * Fix update undo * Fix lightning strike before payment * WIP: Wrap WebLN payments with toasts * add toasts for pending, error, success * while pending, invoice can be canceled * there are still some race conditions between payiny the invoice / error on payment and invoice cancellation * Fix invoice poll using stale value from cache * Remove unnecessary if * Make sure that pay_invoice is declared as supported * Check if WebLN provider is enabled before calling sendPayment * Fix bad retry If WebLN payments failed due to insufficient balances, the promise resolved and thus the action was retried but failed immediately since the invoice (still) wasn't paid. * Fix cache undo update * Fix no cache update after QR payment * refactor: Use fragments to undo cache updates * Remove console.log * Small changes to NWC relay mocking * Return SendPaymentResponse See https://www.webln.guide/building-lightning-apps/webln-reference/webln.sendpayment * Also undo cache update on retry failure * Disable NWC mocking * Fix initialValue not set But following warning is now shown in console: """ Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components """ * Remove comment since only relevant for blastr (mutiny relay) * Remove TODO * Fix duplicate cache update * Fix QR modal not closed after payment * Ignore lnbits variable unused * Use single relay connection for all NWC events * Fix missing timer and subscription cleanup * Remove TODO Confirmed that nostr-tools verifies events and filters for us. See https://github.com/nbd-wtf/nostr-tools/blob/master/abstract-relay.ts#L161 * Fix switch from controlled to uncontrolled input * Show 'configure' on error * Use budgetable instead of async * Remove EOSE listener Only nostr.mutinywallet.com didn't respond with info events due to implementation-specific reasons. This is no longer the case. * Use invoice expiry for NWC timeout I don't think there was a specific reason why I used 60 seconds initially. * Validate LNbits config on save * Validate NWC config on save * Also show unattach if configuration is invalid If unattach is only shown if configuration is valid, resetting the configuration is not possible while it's invalid. So we're stuck with a red wallet indicator. * Fix detection of WebLN payment It depended on a Apollo cache update function being available. But that is not the case for every WebLN payment. * Fix formik bag lost * Use payment instead of zap in toast * autoscale capture svc by response time * docs and changes for testing lnbits locally * Rename configJSON to config Naming of config object was inconsistent with saveConfig function which was annoying. Also fixed other inconsistencies between LNbits and NWC provider. * Allow setting of default payment provider * Update TODO comment about provider priority The list 'paymentMethods' is not used yet but is already implemented for future iterations. * Add wallet security disclaimer * Update labels --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-02-08 18:33:13 +00:00
Support receiving with NWC (#1310) * Add NWC receives * Refactor sendPayment+createInvoice with nwcCall function * Update badge * Add method support checks * Add timeout to NWC test invoice * Fix NWC isConfigured state All NWC fields are marked as optional but NWC should only be considered configured if one of them is set. * Fix relay.fetch() throws 'crypto is not defined' in node nip04.encrypt() was failing in worker because 'crypto is not defined'. Updating to nostr-tools v2.7.2 fixed that. However, now crypto.randomUUID() in relay.fetch() was throwing 'crypto is not defined'. Importing crypto from 'crypto' fixed that. However, with the import, randomUUID() does not work so I switched to randomBytes(). Running relay.fetch() now works in browser and node. * recv must not support pay_invoice * Fix Relay connection check * this.url was undefined * error was an object * Fix additional isConfigured check runs always It was meant to only catch false positives, not turn negatives into false positives. * Rename testConnectServer to testCreateInvoice * Rename testConnectClient to testSendPayment * Only run testSendPayment if send is configured The return value of testSendPayment was used before but it only returned something for LNC. And for LNC, we only wanted to save the transformation during validation, so it was not needed. * Always use withTimeout in NWC test functions * Fix fragment name * Use get_info command exclusively * Check permissions more efficiently * Log NWC request-response flow * Fix variable name * Call ws.send after listener is added * Fix websocket not closed after timeout * Also check that pay_keysend etc. are not supported * fix lnc session key save --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: k00b <k00b@stacker.news>
2024-08-21 15:13:27 +00:00
export const nwcSchema = object().shape({
nwcUrl: string().nwcUrl().when(['nwcUrlRecv'], ([nwcUrlRecv], schema) => {
if (!nwcUrlRecv) return schema.required('required if connection for receiving not set')
return schema.test({
test: nwcUrl => nwcUrl !== nwcUrlRecv,
message: 'connection for sending cannot be the same as for receiving'
})
Support receiving with NWC (#1310) * Add NWC receives * Refactor sendPayment+createInvoice with nwcCall function * Update badge * Add method support checks * Add timeout to NWC test invoice * Fix NWC isConfigured state All NWC fields are marked as optional but NWC should only be considered configured if one of them is set. * Fix relay.fetch() throws 'crypto is not defined' in node nip04.encrypt() was failing in worker because 'crypto is not defined'. Updating to nostr-tools v2.7.2 fixed that. However, now crypto.randomUUID() in relay.fetch() was throwing 'crypto is not defined'. Importing crypto from 'crypto' fixed that. However, with the import, randomUUID() does not work so I switched to randomBytes(). Running relay.fetch() now works in browser and node. * recv must not support pay_invoice * Fix Relay connection check * this.url was undefined * error was an object * Fix additional isConfigured check runs always It was meant to only catch false positives, not turn negatives into false positives. * Rename testConnectServer to testCreateInvoice * Rename testConnectClient to testSendPayment * Only run testSendPayment if send is configured The return value of testSendPayment was used before but it only returned something for LNC. And for LNC, we only wanted to save the transformation during validation, so it was not needed. * Always use withTimeout in NWC test functions * Fix fragment name * Use get_info command exclusively * Check permissions more efficiently * Log NWC request-response flow * Fix variable name * Call ws.send after listener is added * Fix websocket not closed after timeout * Also check that pay_keysend etc. are not supported * fix lnc session key save --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: k00b <k00b@stacker.news>
2024-08-21 15:13:27 +00:00
}),
nwcUrlRecv: string().nwcUrl().when(['nwcUrl'], ([nwcUrl], schema) => {
if (!nwcUrl) return schema.required('required if connection for sending not set')
return schema.test({
test: nwcUrlRecv => nwcUrlRecv !== nwcUrl,
message: 'connection for receiving cannot be the same as for sending'
})
})
}, ['nwcUrl', 'nwcUrlRecv'])
Expose WebLN interface via React Context (#749) * Add LNbits card * Save LNbits Provider in WebLN context * Check LNbits connection on save * refactor: put LNbitsProvider into own file * Pay invoices using WebLN provider from context * Remove deprecated FIXME * Try WebLN provider first * Fix unhandled promise rejection * Fix this in sendPayment * Be optimistic regarding WebLN zaps This wraps the WebLN payment promise with Apollo cache updates. We will be optimistics and assume that the payment will succeed and update the cache accordingly. When we notice that the payment failed, we undo this update. * Bold strike on WebLN zap If lightning strike animation is disabled, toaster will be used. * Rename undo variable to amount * Fix zap undo * Add NWC card * Attempt to check NWC connection using info event * Fix NaN on zap Third argument of update is reserved for context * Fix TypeError in catch of QR code * Add basic NWC payments * Wrap LNbits getInfo with try/catch * EOSE is enough to check NWC connection * refactor: Wrap WebLN providers into own context I should have done this earlier * Show red indicator on error * Fix useEffect return value * Fix wrong usage of pubkey The event pubkey is derived from the secret. Doesn't make sense to manually set it. It's also the wrong pubkey: we're not the wallet service. * Use p tag in NWC request * Add comment about required filter field * Aesthetic changes to NWC sendPayment * Add TODO about receipt verification * Fix WebLN attempted again after error * Fix undefined name * Add code to mock NWC relay * Revert "Bold strike on WebLN zap" This reverts commit a9eb27daec0cd2ef30b56294b05e0056fb5b4184. * Fix update undo * Fix lightning strike before payment * WIP: Wrap WebLN payments with toasts * add toasts for pending, error, success * while pending, invoice can be canceled * there are still some race conditions between payiny the invoice / error on payment and invoice cancellation * Fix invoice poll using stale value from cache * Remove unnecessary if * Make sure that pay_invoice is declared as supported * Check if WebLN provider is enabled before calling sendPayment * Fix bad retry If WebLN payments failed due to insufficient balances, the promise resolved and thus the action was retried but failed immediately since the invoice (still) wasn't paid. * Fix cache undo update * Fix no cache update after QR payment * refactor: Use fragments to undo cache updates * Remove console.log * Small changes to NWC relay mocking * Return SendPaymentResponse See https://www.webln.guide/building-lightning-apps/webln-reference/webln.sendpayment * Also undo cache update on retry failure * Disable NWC mocking * Fix initialValue not set But following warning is now shown in console: """ Warning: A component is changing a controlled input to be uncontrolled. This is likely caused by the value changing from a defined to undefined, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://reactjs.org/link/controlled-components """ * Remove comment since only relevant for blastr (mutiny relay) * Remove TODO * Fix duplicate cache update * Fix QR modal not closed after payment * Ignore lnbits variable unused * Use single relay connection for all NWC events * Fix missing timer and subscription cleanup * Remove TODO Confirmed that nostr-tools verifies events and filters for us. See https://github.com/nbd-wtf/nostr-tools/blob/master/abstract-relay.ts#L161 * Fix switch from controlled to uncontrolled input * Show 'configure' on error * Use budgetable instead of async * Remove EOSE listener Only nostr.mutinywallet.com didn't respond with info events due to implementation-specific reasons. This is no longer the case. * Use invoice expiry for NWC timeout I don't think there was a specific reason why I used 60 seconds initially. * Validate LNbits config on save * Validate NWC config on save * Also show unattach if configuration is invalid If unattach is only shown if configuration is valid, resetting the configuration is not possible while it's invalid. So we're stuck with a red wallet indicator. * Fix detection of WebLN payment It depended on a Apollo cache update function being available. But that is not the case for every WebLN payment. * Fix formik bag lost * Use payment instead of zap in toast * autoscale capture svc by response time * docs and changes for testing lnbits locally * Rename configJSON to config Naming of config object was inconsistent with saveConfig function which was annoying. Also fixed other inconsistencies between LNbits and NWC provider. * Allow setting of default payment provider * Update TODO comment about provider priority The list 'paymentMethods' is not used yet but is already implemented for future iterations. * Add wallet security disclaimer * Update labels --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-02-08 18:33:13 +00:00
export const blinkSchema = object({
apiKey: string()
.required('required')
.matches(/^blink_[A-Za-z0-9]+$/, { message: 'must match pattern blink_A-Za-z0-9' }),
currency: string()
.transform(value => value ? value.toUpperCase() : 'BTC')
.oneOf(['USD', 'BTC'], 'must be BTC or USD')
})
export const lncSchema = object({
pairingPhrase: string()
.test(async (value, context) => {
const words = value ? value.trim().split(/[\s]+/) : []
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
for (const w of words) {
try {
await string().oneOf(bip39Words).validate(w)
} catch {
return context.createError({ message: `'${w}' is not a valid pairing phrase word` })
}
}
if (words.length < 2) {
return context.createError({ message: 'needs at least two words' })
}
if (words.length > 10) {
return context.createError({ message: 'max 10 words' })
}
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
return true
})
Wallet definitions with uniform interface (#1243) * wip: Use uniform interface for wallets * Fix import error * Update wallet logging + other stuff * add canPay and canSend to wallet definition * rename 'default payment method' to 'enabled' and add enable + disable method * Set canPay, canReceive in useWallet * Enable wallet if just configured * Don't pass logger to sendPayment * Add logging to attach & detach * Add schema to wallet def * Add NWC wallet * Fix unused isDefault saved in config * Fix enableWallet * wrong storage key was used * broke if wallets with no configs existed * Run validation during save * Use INFO level for 'wallet disabled' message * Pass config with spread operator * Support help, optional, hint in wallet fields * wip: Add LNC * Fix 20s page load for /settings/wallets.json?nodata=true For some reason, if nodata is passed (which is the case if going back), the page takes 20s to load. * Fix extremely slow page load for LNC import I noticed that the combination of ``` import { Form, PasswordInput, SubmitButton } from '@/components/form' ``` in components/wallet/lnc.js and the dynamic import via `await import` in components/wallet/index.js caused extremely slow page loads. * Use normal imports * Revert "Fix 20s page load for /settings/wallets.json?nodata=true" This reverts commit deb476b3a966569fefcfdf4082d6b64f90fbd0a2. Not using the dynamic import for LNC fixed the slow page load with ?nodata=true. * Remove follow and show recent logs first * Fix position of log start marker * Add FIXMEs for LNC I can't get LNC to connect. It just hangs forever on lnc.connect(). See FIXMEs. * Remove logger.error since already handled in useWallet * Don't require destructuring to pass props to input * wip: Add LND autowithdrawals * receiving wallets need to export 'server' object field * don't print macaroon error stack * fix missing wallet logs order update * mark autowithdrawl settings as required * fix server wallet logs deletion * remove canPay and canReceive since it was confusing where it is available TODO * also use numeric priority for sending wallets to be consistent with how status for receiving wallets is determined * define createInvoice function in wallet definition * consistent wallet logs: sending wallets use 'wallet attached'+'wallet enabled/disabled' whereas receiving wallets use 'wallet created/updated' * see FIXMEs * Fix TypeError * Fix sendPayment called with empty config * removed useEffect such that config is available on first render * fix hydration error using dynamic import without SSR * Fix confusing UX around enabled * Remove FIXMEs Rebase on master seemed to have fixed these, weird * Use same error format in toast and wallet log * Fix usage of conditional hooks in useConfig * Fix isConfigured * Fix delete wallet logs on server * Fix wallet logs refetch onError does not exist on client.mutate * Fix TypeError in isConfigured if no enabled wallet found * Only include local/server config if required * Fix another hydration error * Fix server config not updated after save or detach * Also use 'enabled' for server wallets * Fix wallet logs not updated after server delete * Consistent logs between local and server wallets * 'wallet attached' on create * 'wallet updated' on config updates * 'wallet enabled' and 'wallet disabled' if checkbox changed * 'wallet detached' on delete * Also enable server wallets on create * Disable checkbox if not configured yet * Move all validation schema into lib/validate * Implement drag & drop w/o persistence * Use dynamic import for WalletCard This fixes a lot of issues with hydration * Save order as priority * Fix autowithdrawSettings not applied Form requires config in flat format but mutation requires autowithdraw settings in a separate 'settings' field. I have decided that config will be in flat form format. It will be transformed into mutation format during save. * Save dedicated enabled flag for server wallets * wallet table now contains boolean column 'enabled' * 'priority' is now a number everywhere * use consistent order between how autowithdrawals are attempted and server wallets cards * Fix onCanceled missing * Fix typo * Fix noisy changes in lib/validate I moved the schema for lnbits, nwc and lnc out of lib/validate only to put them back in there later. This commit should make the changeset cleaner by removing noise. * Split arguments into [value,] config, context * Run lnbits url.replace in validate and sendPayment * Remove unnecessary WALLETS_QUERY * Generate wallet mutation from fields * Generate wallet resolver from fields * Fix import inconsistency between app and worker * Use wallet.createInvoice for autowithdrawals * Fix success autowithdrawal log * Fix wallet security banner shown for server wallets * Add autowithdrawal to lightning address * Add optional wallet short name for logging * Fix draggable * Fix autowithdraw loop * Add missing hints * Add CLN autowithdrawal * Detach wallets and delete logs on logout * Remove Wallet in lib/constants * Use inject function for resolvers and typeDefs * Fix priority ignored when fetching enabled wallet * Fix draggable false on first page load due to SSR * Use touches instead of dnd on mobile Browsers don't support drag events for touch devices. To have a consistent implementation for desktop and mobile, we would need to use mousedown/touchstart, mouseup/touchend and mousemove/touchmove. For now, this commit makes changing the order possible on touch devices with simple touches. * Fix duplicate CLN error * Fix autowithdraw priority order * Fix error per invalid bip39 word * Update LNC code * remove LNC FIXMEs Mhh, I guess the TURN server was down or something? It now magically works. Or maybe it only works once per mnemonic? * also removed the lnc.lnd.lightning.getInfo() call since we don't ask and need permission for this RPC for payments. * setting a password does not work though. It fails with 'The password provided is not valid' which is triggered at https://github.com/lightninglabs/lnc-web/blob/main/lib/util/credentialStore.ts#L81. * Fix order if wallet with no priority exists * Use common sort * Add link to lnbits.com * Add example wallet def * Remove TODOs TODO in components/wallet-logger.js was handled. I don't see a need for the TODO in lib/wallet.js anymore. This function will only be called with the wallet of type LIGHTNING_ADDRESS anyway. * Remove console.log * Toast priority save errors * Fix leaking relay connections * Remove 'tor or clearnet' hint for LN addresses * Remove React dependency from wallet definitions * Generate resolver name from walletField * Move wallets into top level directory wallet/ * Put wallets into own folder * Fix generateMutation * remove resolverName property from wallet defs * move function into lib/wallet * use function in generateMutation on client to fix wrongly generated mutation * Separate client and server imports by files * wallets now consist of an index.js, a client.js and a server.js file * client.js is imported on the client and contains the client portion * server.js is imported on the server and contains the server porition * both reexport index.js so everything in index.js can be shared by client and server * every wallet contains a client.js file since they are all imported on the client to show the cards * client.js of every wallet is reexported as an array in wallets/client.js * server.js of every wallet is reexported as an array in wallets/server.js FIXME: for some reason, worker does not properly import the default export of wallets/server.js * Fix worker import of wallets/server * Fix wallet.server usage * I removed wallet.server in a previous commit * the client couldn't determine which wallet was stored on the server since all server specific fields were set in server.js * walletType and walletField are now set in index.js * walletType is now used to determine if a wallet is stored on the server * also included some formatting changes * Fix w.default usage Since package.json with { "type": "module" } was added, this is no longer needed. * Fix id access in walletPrioritySort * Fix autowithdrawal error log * Generate validation schema for LNbits * Generate validation schema for NWC * Rename to torAllowed * Generate validation schema for LNC * Generate validation schema for LND * Generate validation schema for LnAddr * Remove stringTypes * Generate validation schema for CLN * Make clear that message belongs to test * validate.message was used in tandem with validate.test * it might be confused as the message if the validation for validate.type failed * now validate.test can be a function or an object of { test, message } shape which matches Yup.test * Remove validate.schema as a trap door * make lnc work * Return null if no wallet was found * Revert code around schema generation * Transform autowithdrawSchemaMembers into an object * Rename schema to yupSchema * Fix missing required for LNbits adminKey * Support formik form-level validation * Fix missing addWalletLog import * Fix missing space after = * fix merge conflict resolution mistake * remove non-custodial* badges * create guides for attaching wallets in sndev * Use built-in formik validation or Yup schema but not both * Rename: validate -> testConnectClient, testConnect -> testConnectServer * make lnaddr autowithdraw work in dev * move ATTACH docs to ./wallets and add lnaddr doc * Fix missing rename: yupSchema -> fieldValidation * Remove unused context * Add documentation how to add wallets --------- Co-authored-by: keyan <keyan.kousha+huumn@gmail.com> Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-07-20 22:51:46 +00:00
.required('required')
})
2023-07-24 22:50:12 +00:00
export const bioSchema = object({
bio: string().required('required').trim()
2023-02-08 19:38:04 +00:00
})
2023-07-24 22:50:12 +00:00
export const inviteSchema = object({
2023-02-08 19:38:04 +00:00
gift: intValidator.positive('must be greater than 0').required('required'),
limit: intValidator.positive('must be positive')
})
Service worker rework, Web Target Share API & Web Push API (#324) * npm uninstall next-pwa next-pwa was last updated in August 2022. There is also an issue which mentions that next-pwa is abandoned (?): https://github.com/shadowwalker/next-pwa/issues/482 But the main reason for me uninstalling it is that it adds a lot of preconfigured stuff which is not necessary for us. It even lead to a bug since pages were cached without our knowledge. So I will go with a different PWA approach. This different approach should do the following: - make it more transparent what the service worker is doing - gives us more control to configure the service worker and thus making it easier * Use workbox-webpack-plugin Every other plugin (`next-offline`, `next-workbox-webpack-plugin`, `next-with-workbox`, ...) added unnecessary configuration which felt contrary to how PWAs should be built. (PWAs should progressivly enhance the website in small steps, see https://web.dev/learn/pwa/getting-started/#focus-on-a-feature) These default configurations even lead to worse UX since they made invalid assumptions about stacker.news: We _do not_ want to cache our start url and we _do not_ want to cache anything unless explicitly told to. Almost every page on SN should be fresh for the best UX. To achieve this, by default, the service worker falls back to the network (as if the service worker wasn't there). Therefore, this should be the simplest configuration with a valid precache and cache busting support. In the future, we can try to use prefetching to improve performance of navigation requests. * Add support for Web Share Target API See https://developer.chrome.com/articles/web-share-target/ * Use Web Push API for push notifications I followed this (very good!) guide: https://web.dev/notifications/ * Refactor code related to Web Push * Send push notification to users on events * Merge notifications * Send notification to author of every parent recursively * Remove unused userId param in savePushSubscription As it should be, the user id is retrieved from the authenticated user in the backend. * Resubscribe user if push subscription changed * Update old subscription if oldEndpoint was given * Allow users to unsubscribe * Use LTREE operator instead of recursive query * Always show checkbox for push notifications * Justify checkbox to end * Update title of first push notification * Fix warning from uncontrolled to controlled * Add comment about Notification.requestPermission * Fix timestamp * Catch error on push subscription toggle * Wrap function bodies in try/catch * Use Promise.allSettled * Filter subscriptions by user notification settings * Fix user notification filter * Use skipWaiting --------- Co-authored-by: ekzyis <ek@stacker.news>
2023-07-04 19:36:07 +00:00
2023-07-24 22:50:12 +00:00
export const pushSubscriptionSchema = object({
endpoint: string().url().required('required').trim(),
p256dh: string().required('required').trim(),
auth: string().required('required').trim()
Service worker rework, Web Target Share API & Web Push API (#324) * npm uninstall next-pwa next-pwa was last updated in August 2022. There is also an issue which mentions that next-pwa is abandoned (?): https://github.com/shadowwalker/next-pwa/issues/482 But the main reason for me uninstalling it is that it adds a lot of preconfigured stuff which is not necessary for us. It even lead to a bug since pages were cached without our knowledge. So I will go with a different PWA approach. This different approach should do the following: - make it more transparent what the service worker is doing - gives us more control to configure the service worker and thus making it easier * Use workbox-webpack-plugin Every other plugin (`next-offline`, `next-workbox-webpack-plugin`, `next-with-workbox`, ...) added unnecessary configuration which felt contrary to how PWAs should be built. (PWAs should progressivly enhance the website in small steps, see https://web.dev/learn/pwa/getting-started/#focus-on-a-feature) These default configurations even lead to worse UX since they made invalid assumptions about stacker.news: We _do not_ want to cache our start url and we _do not_ want to cache anything unless explicitly told to. Almost every page on SN should be fresh for the best UX. To achieve this, by default, the service worker falls back to the network (as if the service worker wasn't there). Therefore, this should be the simplest configuration with a valid precache and cache busting support. In the future, we can try to use prefetching to improve performance of navigation requests. * Add support for Web Share Target API See https://developer.chrome.com/articles/web-share-target/ * Use Web Push API for push notifications I followed this (very good!) guide: https://web.dev/notifications/ * Refactor code related to Web Push * Send push notification to users on events * Merge notifications * Send notification to author of every parent recursively * Remove unused userId param in savePushSubscription As it should be, the user id is retrieved from the authenticated user in the backend. * Resubscribe user if push subscription changed * Update old subscription if oldEndpoint was given * Allow users to unsubscribe * Use LTREE operator instead of recursive query * Always show checkbox for push notifications * Justify checkbox to end * Update title of first push notification * Fix warning from uncontrolled to controlled * Add comment about Notification.requestPermission * Fix timestamp * Catch error on push subscription toggle * Wrap function bodies in try/catch * Use Promise.allSettled * Filter subscriptions by user notification settings * Fix user notification filter * Use skipWaiting --------- Co-authored-by: ekzyis <ek@stacker.news>
2023-07-04 19:36:07 +00:00
})
export const lud18PayerDataSchema = (k1) => object({
name: string(),
pubkey: string(),
email: string().email('bad email address'),
identifier: string()
})
2024-01-13 00:09:50 +00:00
// check if something is _really_ a number.
// returns true for every number in this range: [-Infinity, ..., 0, ..., Infinity]
export const isNumber = x => typeof x === 'number' && !Number.isNaN(x)
not-custodial zap beta (#1178) * not-custodial zap scaffolding * invoice forward state machine * small refinements to state machine * make wrap invoice work * get state machine working end to end * untested logic layout for paidAction invoice wraps * perform pessimisitic actions before outgoing payment * working end to end * remove unneeded params from wallets/server/createInvoice * fix cltv relative/absolute confusion + cancelling forwards * small refinements * add p2p wrap info to paidAction docs * fallback to SN invoice when wrap fails * fix paidAction retry description * consistent naming scheme for state machine * refinements * have sn pay bounded outbound fee * remove debug logging * reenable lnc permissions checks * don't p2p zap on item forward splits * make createInvoice params json encodeable * direct -> p2p badge on notifications * allow no tls in dev for core lightning * fix autowithdraw to create invoice with msats * fix autowithdraw msats/sats inconsitency * label p2p zaps properly in satistics * add fees to autowithdrawal notifications * add RETRYING as terminal paid action state * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/paidAction/README.md Co-authored-by: ekzyis <ek@stacker.news> * Update api/lnd/index.js Co-authored-by: ekzyis <ek@stacker.news> * ek suggestions * add bugetable to nwc card * get paranoid with numbers * better finalize retries and better max timeout height * refine forward failure transitions * more accurate satistics p2p status * make sure paidaction cancel in state machine only * dont drop bolt11s unless status is not null * only allow PENDING_HELD to transition to FORWARDING * add mermaid state machine diagrams to paid action doc * fix cancel transition name * cleanup readme * move forwarding outside of transition * refine testServerConnect and make sure ensureB64 transforms * remove unused params from testServerConnect --------- Co-authored-by: ekzyis <ek@stacker.news> Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 14:48:30 +00:00
export const toNumber = (x, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) => {
if (typeof x === 'undefined') {
throw new Error('value is required')
}
const n = Number(x)
if (isNumber(n)) {
if (x < min || x > max) {
throw new Error(`value ${x} must be between ${min} and ${max}`)
}
return n
}
throw new Error(`value ${x} is not a number`)
}
export const toPositiveNumber = (x) => toNumber(x, 0)