Skip to content

Commit 4e487eb

Browse files
ganchoradkovGancho Radkovbkrem
authored
fix: undefined actions (#543)
* fix: fixed bug where approved methods that are not supported by react dapp resulted in un unhandled exception due to missing actions * Update advanced/dapps/react-dapp-v2/src/pages/index.tsx Co-authored-by: Ben Kremer <ben@walletconnect.com> --------- Co-authored-by: Gancho Radkov <ganchoradkov@gmail.com> Co-authored-by: Ben Kremer <ben@walletconnect.com>
1 parent 704e55b commit 4e487eb

File tree

1 file changed

+49
-24
lines changed
  • advanced/dapps/react-dapp-v2/src/pages

1 file changed

+49
-24
lines changed

advanced/dapps/react-dapp-v2/src/pages/index.tsx

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ const Home: NextPage = () => {
149149
});
150150
}
151151

152-
const getEthereumActions = (chainId:string,address:string): AccountAction[] => {
152+
const getEthereumActions = (
153+
chainId: string,
154+
address: string
155+
): AccountAction[] => {
153156
const actions = {
154157
[DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION]: {
155158
method: DEFAULT_EIP155_METHODS.ETH_SEND_TRANSACTION,
@@ -217,35 +220,52 @@ const Home: NextPage = () => {
217220
};
218221

219222
let availableActions: AccountAction[] = [];
220-
const chainIdAsHex = `0x${numberToHex(parseInt(chainId))}`
221-
const capabilitiesJson = session?.sessionProperties?.['capabilities']
222-
const walletCapabilities = capabilitiesJson && JSON.parse(capabilitiesJson)
223+
const chainIdAsHex = `0x${numberToHex(parseInt(chainId))}`;
224+
const capabilitiesJson = session?.sessionProperties?.["capabilities"];
225+
const walletCapabilities = capabilitiesJson && JSON.parse(capabilitiesJson);
223226
session?.namespaces?.["eip155"].methods.forEach((methodName) => {
224-
const action: AccountAction | undefined = actions[methodName as keyof typeof actions];
225-
// Determine if the method requires additional capability checks
226-
const requiresCapabilityCheck = ["wallet_sendCalls", "wallet_getCallsStatus"].includes(methodName);
227-
// Check capabilities only if the method requires it
228-
if (!requiresCapabilityCheck || hasEIP7592RequiredCapabilities(address, chainIdAsHex, walletCapabilities)) {
229-
availableActions.push(action);
230-
}
227+
const action: AccountAction | undefined =
228+
actions[methodName as keyof typeof actions];
229+
// Determine if the method requires additional capability checks
230+
const requiresCapabilityCheck = [
231+
"wallet_sendCalls",
232+
"wallet_getCallsStatus",
233+
].includes(methodName);
234+
// Check capabilities only if the method requires it
235+
if (
236+
!requiresCapabilityCheck ||
237+
hasEIP7592RequiredCapabilities(
238+
address,
239+
chainIdAsHex,
240+
walletCapabilities
241+
)
242+
) {
243+
availableActions.push(action);
231244
}
232-
);
245+
});
233246

234-
return availableActions;
247+
// if a method is approved in the session thats not supported by the app, it will result in an undefined item in the array
248+
return availableActions.filter((action) => action !== undefined);
235249
};
236250

237-
const hasEIP7592RequiredCapabilities = (address: string, chainId: string, walletCapabilities: any): boolean => {
238-
if(!walletCapabilities) return false;
239-
const addressCapabilities: GetCapabilitiesResult | undefined = walletCapabilities[address];
251+
const hasEIP7592RequiredCapabilities = (
252+
address: string,
253+
chainId: string,
254+
walletCapabilities: any
255+
): boolean => {
256+
if (!walletCapabilities) return false;
257+
const addressCapabilities: GetCapabilitiesResult | undefined =
258+
walletCapabilities[address];
240259
if (
241260
addressCapabilities &&
242261
addressCapabilities[chainId] &&
243-
(addressCapabilities[chainId]['atomicBatch']?.supported ||
244-
addressCapabilities[chainId]['paymasterService']?.supported ||
245-
addressCapabilities[chainId]['sessionKey']?.supported)
246-
) return true; // Capabilities are supported
262+
(addressCapabilities[chainId]["atomicBatch"]?.supported ||
263+
addressCapabilities[chainId]["paymasterService"]?.supported ||
264+
addressCapabilities[chainId]["sessionKey"]?.supported)
265+
)
266+
return true; // Capabilities are supported
247267
return false; // Capabilities are not supported or not defined
248-
}
268+
};
249269

250270
const getCosmosActions = (): AccountAction[] => {
251271
const onSignDirect = async (chainId: string, address: string) => {
@@ -447,11 +467,11 @@ const Home: NextPage = () => {
447467
];
448468
};
449469

450-
const getBlockchainActions = (account:string) => {
470+
const getBlockchainActions = (account: string) => {
451471
const [namespace, chainId, address] = account.split(":");
452472
switch (namespace) {
453473
case "eip155":
454-
return getEthereumActions(chainId,address);
474+
return getEthereumActions(chainId, address);
455475
case "cosmos":
456476
return getCosmosActions();
457477
case "solana":
@@ -503,7 +523,12 @@ const Home: NextPage = () => {
503523
case "ping":
504524
return <PingModal pending={isRpcRequestPending} result={rpcResult} />;
505525
case "requestLoader":
506-
return <RequestLoaderModal pending={isRpcRequestPending} result={rpcResult} />;
526+
return (
527+
<RequestLoaderModal
528+
pending={isRpcRequestPending}
529+
result={rpcResult}
530+
/>
531+
);
507532
case "disconnect":
508533
return <LoaderModal title={"Disconnecting..."} />;
509534
default:

0 commit comments

Comments
 (0)