Skip to content

Commit

Permalink
cleaned up requestPermissions and got it mostly working
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaerax authored and ChiaMineJP committed Oct 25, 2024
1 parent 222b753 commit 5e0ec29
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useGetKeysQuery } from '@chia-network/api-react';
import { ConfirmDialog, Flex, LoadingOverlay } from '@chia-network/core';
import { Trans } from '@lingui/macro';
import { Typography, Divider } from '@mui/material';
import React, { type ReactNode, useState, useMemo } from 'react';
import React, { useState, useMemo } from 'react';

import type WalletConnectCommandParam from '../../@types/WalletConnectCommandParam';
import useWalletConnectPairs from '../../hooks/useWalletConnectPairs';
Expand All @@ -11,7 +11,6 @@ import WalletConnectMetadata from './WalletConnectMetadata';

export type WalletConnectRequestPermissionsConfirmDialogProps = {
topic: string;
message: ReactNode;
fingerprint: number;
isDifferentFingerprint: boolean;
params: WalletConnectCommandParam[];
Expand All @@ -26,7 +25,6 @@ export default function WalletConnectRequestPermissionsConfirmDialog(
) {
const {
topic,
message,
fingerprint,
isDifferentFingerprint,
onClose = () => {},
Expand All @@ -37,13 +35,21 @@ export default function WalletConnectRequestPermissionsConfirmDialog(
} = props;

const [values, setValues] = useState(defaultValues);
const { getPairBySession } = useWalletConnectPairs();
const { getPairBySession, bypassCommands } = useWalletConnectPairs();
const { data: keys, isLoading: isLoadingPublicKeys } = useGetKeysQuery();
const key = keys?.find((item) => item.fingerprint === fingerprint);

const pair = useMemo(() => getPairBySession(topic), [topic, getPairBySession]);

function handleClose(confirmed: boolean) {
if (confirmed) {
params.forEach((element) => {
if (element.name === 'commands') {
bypassCommands(topic, values[element.name], true);
}
});
}

onClose?.(confirmed);
}

Expand All @@ -63,7 +69,7 @@ export default function WalletConnectRequestPermissionsConfirmDialog(
>
<LoadingOverlay isLoading={isLoadingPublicKeys}>
<Flex flexDirection="column" gap={2}>
<Typography variant="body1">{message}</Typography>
<Typography variant="body1">An app has requested permission to execute the following commands.</Typography>

{params.length > 0 && (
<Flex flexDirection="column" gap={2}>
Expand Down
16 changes: 12 additions & 4 deletions packages/gui/src/hooks/useWalletConnectCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,21 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand
log(`bypassing command ${command} with value ${pair.bypassCommands[command]}`);
return pair.bypassCommands[command];
}

if (command === 'request_permissions') {
if (command === 'requestPermissions') {
let hasPermissions = true;
if (values.commands) {
values.commands.forEach((cmd: string) => {
if (!pair.bypassCommands || !pair.bypassCommands[cmd]) {
hasPermissions = false;
}
});
}
if (hasPermissions) {
return true;
}
const isConfirmed = await openDialog(
<WalletConnectRequestPermissionsConfirmDialog
topic={topic}
message={message}
fingerprint={fingerprint}
isDifferentFingerprint={isDifferentFingerprint}
params={params}
Expand All @@ -142,7 +151,6 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand
onChange={onChange}
/>
);

return isConfirmed;
}

Expand Down
33 changes: 28 additions & 5 deletions packages/gui/src/hooks/useWalletConnectPairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function useWalletConnectPairs(): Pairs {
const removePairBySession = useCallback((sessionTopic: string) => {
const [, setPairs] = pairsRef.current;
setPairs((pairs: Pair[]) =>
pairs.filter((item) => !item.sessions.find((session) => session.topic === sessionTopic)),
pairs.filter((item) => !item.sessions.find((session) => session.topic === sessionTopic))
);
}, []);

Expand Down Expand Up @@ -94,7 +94,7 @@ export default function useWalletConnectPairs(): Pairs {
pairs.map((pair) => ({
...pair,
sessions: pair.sessions.filter((item) => item.topic !== sessionTopic),
})),
}))
);
}, []);

Expand All @@ -121,6 +121,27 @@ export default function useWalletConnectPairs(): Pairs {
});
}, []);

const bypassCommands = useCallback((sessionTopic: string, commands: string[], confirm: boolean) => {
const [, setPairs] = pairsRef.current;
setPairs((pairs: Pair[]) => {
const pair = pairs.find((item) => item.sessions?.find((session) => session.topic === sessionTopic));
if (!pair) {
throw new Error('Pair not found');
}

return pairs.map((item) => ({
...item,
bypassCommands:
item.topic === pair.topic
? {
...item.bypassCommands,
...commands.reduce((acc, command) => ({ ...acc, [command]: confirm }), {}),
}
: item.bypassCommands,
}));
});
}, []);

const removeBypassCommand = useCallback((sessionTopic: string, command: string) => {
const deleteCommand = (commands: Record<string, boolean> | undefined) => {
const newBypassCommands = { ...commands };
Expand Down Expand Up @@ -152,7 +173,7 @@ export default function useWalletConnectPairs(): Pairs {
pairs.map((item) => ({
...item,
bypassCommands: {},
})),
}))
);
}, []);

Expand All @@ -166,7 +187,7 @@ export default function useWalletConnectPairs(): Pairs {
item.topic === pairTopic
? {} // reset bypass commands
: item.bypassCommands,
})),
}))
);
}, []);

Expand All @@ -185,6 +206,7 @@ export default function useWalletConnectPairs(): Pairs {

removeSessionFromPair,
bypassCommand,
bypassCommands,
removeBypassCommand,
resetBypassForAllPairs,
resetBypassForPair,
Expand All @@ -201,11 +223,12 @@ export default function useWalletConnectPairs(): Pairs {
removePairBySession,
removeSessionFromPair,
bypassCommand,
bypassCommands,
removeBypassCommand,
resetBypassForAllPairs,
resetBypassForPair,
currentPairs,
],
]
);

return pairs;
Expand Down

0 comments on commit 5e0ec29

Please sign in to comment.