Skip to content

Commit

Permalink
WalletConnect fixes (#81)
Browse files Browse the repository at this point in the history
- Fix WalletConnect connections for cosmos by using undocumented `useChains` hook
- Fix image load errors in WalletConnect modal by adjusting CSP header
  • Loading branch information
jmrossy authored Nov 13, 2023
1 parent 039ba44 commit 3c2ca72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const securityHeaders = [
key: 'Content-Security-Policy',
value: `default-src 'self'; script-src 'self'${
isDev ? " 'unsafe-eval'" : ''
}; connect-src *; img-src 'self' data:; style-src 'self' 'unsafe-inline' https://*.googleapis.com; font-src 'self' data:; base-uri 'self'; form-action 'self'; frame-src 'self' https://*.solflare.com https://*.walletconnect.com;`,
}; connect-src *; img-src 'self' data: https://*.walletconnect.com; style-src 'self' 'unsafe-inline' https://*.googleapis.com; font-src 'self' data:; base-uri 'self'; form-action 'self'; frame-src 'self' https://*.solflare.com https://*.walletconnect.com;`,
},
]

Expand Down
56 changes: 19 additions & 37 deletions src/features/wallet/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { DeliverTxResponse, ExecuteResult } from '@cosmjs/cosmwasm-stargate';
import {
useChain as useCosmosChain,
useChains as useCosmosChains,
useWalletClient as useCosmosWallet,
} from '@cosmos-kit/react';
import { useChain as useCosmosChain, useChains as useCosmosChains } from '@cosmos-kit/react';
import { useConnectModal as useEvmModal } from '@rainbow-me/rainbowkit';
import { useConnection, useWallet as useSolanaWallet } from '@solana/wallet-adapter-react';
import { useWalletModal as useSolanaModal } from '@solana/wallet-adapter-react-ui';
Expand All @@ -13,7 +9,7 @@ import {
switchNetwork as switchEvmNetwork,
} from '@wagmi/core';
import { providers } from 'ethers';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useMemo } from 'react';
import { toast } from 'react-toastify';
import {
useAccount as useEvmAccount,
Expand Down Expand Up @@ -89,40 +85,26 @@ export function useAccounts(): {
);

// Cosmos
const { wallet: cosmWallet, isWalletConnected: isCosmAccountReady } =
useCosmosChain(PLACEHOLDER_COSMOS_CHAIN);
const { status: cosmWalletStatus, client: cosmWalletClient } = useCosmosWallet();
const [cosmAccounts, setCosmAccounts] = useState<Array<ChainAddress>>([]);

useEffect(() => {
if (cosmWalletStatus !== 'Done' || !cosmWalletClient) return;
const cosmosChainToContext = useCosmosChains(getCosmosChainNames());
const cosmAccountInfo: AccountInfo = useMemo(() => {
const cosmAddresses: Array<ChainAddress> = [];
let cosmConnectorName: string | undefined = undefined;
let isCosmAccountReady = false;
const multiProvider = getMultiProvider();
const cosmChainNames = getCosmosChainNames();
const cosmChainCaip2Ids = cosmChainNames.map((c) =>
getCaip2Id(ProtocolType.Cosmos, multiProvider.getChainId(c)),
);
cosmWalletClient.enable?.(cosmChainNames).catch((err) => logger.error(err));
const cosmosAccountPromises = cosmChainNames.map((c) => cosmWalletClient.getAccount?.(c));
Promise.all(cosmosAccountPromises)
.then((cosmosAccounts) => {
setCosmAccounts(
cosmosAccounts
.map((a, i) => ({ address: a?.address, chainCaip2Id: cosmChainCaip2Ids[i] }))
.filter((a) => !!a.address) as Array<ChainAddress>,
);
})
.catch((err) => logger.error(err));
}, [cosmWalletStatus, cosmWalletClient]);

const cosmAccountInfo: AccountInfo = useMemo(
() => ({
for (const [chainName, context] of Object.entries(cosmosChainToContext)) {
if (!context.address) continue;
const caip2Id = getCaip2Id(ProtocolType.Cosmos, multiProvider.getChainId(chainName));
cosmAddresses.push({ address: context.address, chainCaip2Id: caip2Id });
isCosmAccountReady = true;
cosmConnectorName ||= context.wallet?.prettyName;
}
return {
protocol: ProtocolType.Cosmos,
addresses: cosmAccounts,
connectorName: cosmWallet?.prettyName,
addresses: cosmAddresses,
connectorName: cosmConnectorName,
isReady: isCosmAccountReady,
}),
[cosmAccounts, cosmWallet, isCosmAccountReady],
);
};
}, [cosmosChainToContext]);

// Filtered ready accounts
const readyAccounts = useMemo(
Expand Down

1 comment on commit 3c2ca72

@vercel
Copy link

@vercel vercel bot commented on 3c2ca72 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.