From 7e8243e3911fcfc13682e2b02fee1100b1a912b7 Mon Sep 17 00:00:00 2001 From: Matej Rifelj Date: Thu, 31 Aug 2023 18:08:02 +0200 Subject: [PATCH] "Skip Confirmation For All Readonly Commands" into Settings --- .../settings/SettingsIntegration.tsx | 32 +++++++++++++++++++ .../WalletConnectAddConnectionDialog.tsx | 14 ++++++-- .../gui/src/hooks/useWalletConnectCommand.tsx | 12 +++---- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/packages/gui/src/components/settings/SettingsIntegration.tsx b/packages/gui/src/components/settings/SettingsIntegration.tsx index 81d1e6ecb3..aebbc7a014 100644 --- a/packages/gui/src/components/settings/SettingsIntegration.tsx +++ b/packages/gui/src/components/settings/SettingsIntegration.tsx @@ -1,3 +1,4 @@ +import { useLocalStorage, useGetLoggedInFingerprintQuery } from '@chia-network/api-react'; import { Flex, MenuItem, SettingsHR, SettingsSection, SettingsTitle, SettingsText } from '@chia-network/core'; import { t, Trans } from '@lingui/macro'; import { @@ -45,6 +46,9 @@ export default function SettingsIntegration() { useWalletConnectPairs(); const pairs = get(); + const { data: fingerprint } = useGetLoggedInFingerprintQuery(); + + const [bypassReadonlyCommands, setBypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', {}); const refreshBypassCommands = React.useCallback(() => { if (selectedPair.current) { @@ -131,6 +135,9 @@ export default function SettingsIntegration() { } }, [topic, pairs, refreshBypassCommands]); + const readonlySkipValue = + bypassReadonlyCommands && topic && fingerprint ? bypassReadonlyCommands[topic][fingerprint] : false; + return ( @@ -334,6 +341,31 @@ export default function SettingsIntegration() { )} + + + Skip Confirmation For All Readonly Commands + + + + + + + {topic && commands.length > 0 && ( <> diff --git a/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx b/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx index d60073cd5e..74c762d428 100644 --- a/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx +++ b/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx @@ -51,7 +51,8 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd const { onClose = () => {}, open = false } = props; const [step, setStep] = useState(Step.CONNECT); - const [bypassReadonlyCommands, toggleBypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', false); + const [bypassReadonlyCommands, setBypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', {}); + const [bypassCheckbox, toggleBypassCheckbox] = useState(false); const { pair, isLoading: isLoadingWallet } = useWalletConnectContext(); const { data: keys, isLoading: isLoadingPublicKeys } = useGetKeysQuery({}); const [sortedWallets] = usePrefs('sortedWallets', []); @@ -119,6 +120,13 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd } const topic = await pair(uri, selectedFingerprints, mainnet); + if (bypassCheckbox) { + let skipReadOnlyObject = {}; + selectedFingerprints.forEach((f: number) => { + skipReadOnlyObject = { ...skipReadOnlyObject, [f]: true }; + }); + setBypassReadonlyCommands({ ...bypassReadonlyCommands, [topic.toString()]: skipReadOnlyObject }); + } onClose(topic); } @@ -219,10 +227,10 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd sx={{ cursor: 'pointer' }} alignItems="center" onClick={() => { - toggleBypassReadonlyCommands(!bypassReadonlyCommands); + toggleBypassCheckbox(!bypassCheckbox); }} > - + Bypass confirmation for all read-only commands diff --git a/packages/gui/src/hooks/useWalletConnectCommand.tsx b/packages/gui/src/hooks/useWalletConnectCommand.tsx index a7abb0bddf..069a7f40fb 100644 --- a/packages/gui/src/hooks/useWalletConnectCommand.tsx +++ b/packages/gui/src/hooks/useWalletConnectCommand.tsx @@ -80,7 +80,7 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand const isLoading = isLoadingLoggedInFingerprint; - const [bypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', false); + const [bypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', {}); async function confirm(props: { topic: string; @@ -141,8 +141,9 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand const { fingerprint } = requestedParams; + const pair = getPairBySession(topic); + const pairedTopic = pair?.topic!; if (command === 'showNotification') { - const pair = getPairBySession(topic); if (!pair) { throw new Error('Invalid session topic'); } @@ -174,13 +175,10 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand values = newValues; } - const isReadOnly = - ['spend', 'cancel', 'create', 'transfer', 'send', 'take', 'add', 'set'].filter( - (startsWith: string) => command.indexOf(startsWith) === 0 - ).length === 0; + const isReadOnlyEnabled = bypassReadonlyCommands?.[pairedTopic]?.[fingerprint]; const confirmed = - (bypassReadonlyCommands && isReadOnly) || + (isReadOnlyEnabled && definition.bypassConfirm) || (await confirm({ topic, message: