Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WalletConnect, reject proposal if no supported network #2287

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions apps/web/src/components/WalletConnect/WalletConnectProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
walletKit,
} from "@umami/state";
import { type Network } from "@umami/tezos";
import { CustomError, WalletConnectError } from "@umami/utils";
import { WalletConnectError } from "@umami/utils";
import { formatJsonRpcError } from "@walletconnect/jsonrpc-utils";
import { type SessionTypes } from "@walletconnect/types";
import { type SdkErrorKey, getSdkError } from "@walletconnect/utils";
Expand Down Expand Up @@ -49,27 +49,39 @@ export const WalletConnectProvider = ({ children }: PropsWithChildren) => {
.map(([key, values]) => (key.includes(":") ? key : (values.chains ?? [])))
.flat()
.filter(Boolean);
const optionalNetworks = Object.entries(proposal.params.optionalNamespaces)
.map(([key, values]) => (key.includes(":") ? key : (values.chains ?? [])))
.flat()
.filter(Boolean);

if (requiredNetworks.length !== 1) {
throw new CustomError(
`Umami supports only one network per request, got required networks: ${requiredNetworks}`
throw new WalletConnectError(
`Umami supports only one network per request, got required networks: ${requiredNetworks}, optional networks: ${optionalNetworks}`,
"UNSUPPORTED_CHAINS",
null
);
}
const network = requiredNetworks[0] as NetworkType;
const availablenetworks = availableNetworks.map(network => network.name);
// the network contains a namespace, e.g. tezos:mainnet
if (!availablenetworks.includes(network.split(":")[1])) {
throw new CustomError(
`The requested required network "${network}" is not supported. Available: ${availablenetworks}`
const availableNetworkNames = availableNetworks.map(network => `tezos:${network.name}`);
throw new WalletConnectError(
`The requested required network "${network}" is not supported. Available: ${availableNetworkNames}`,
"UNSUPPORTED_CHAINS",
null
);
}

await openWith(<SessionProposalModal network={network} proposal={proposal} />, {});
}).catch(async () => {
}).catch(async error => {
const sdkErrorKey: SdkErrorKey =
error instanceof WalletConnectError ? error.sdkError : "SESSION_SETTLEMENT_FAILED";
console.warn("WC session proposal failed", sdkErrorKey, error, proposal);
// dApp is waiting so we need to notify it
await walletKit.rejectSession({
id: proposal.id,
reason: getSdkError("UNSUPPORTED_CHAINS"),
reason: getSdkError(sdkErrorKey),
});
}),
[availableNetworks, openWith, handleAsyncActionUnsafe]
Expand Down
Loading