diff --git a/packages/wallet/src/components/Header.tsx b/packages/wallet/src/components/Header.tsx index 480fcb6..bc38cb3 100644 --- a/packages/wallet/src/components/Header.tsx +++ b/packages/wallet/src/components/Header.tsx @@ -48,7 +48,7 @@ export default function Header({ onInitialized, }: HeaderProps) { async function onMetamaskClick() { - if (await initializeArsnap()) { + if ((await initializeArsnap()) === "success") { onInitialized(); } } diff --git a/packages/wallet/src/utils/arsnap.ts b/packages/wallet/src/utils/arsnap.ts index 921497b..38e2b2c 100644 --- a/packages/wallet/src/utils/arsnap.ts +++ b/packages/wallet/src/utils/arsnap.ts @@ -1,7 +1,6 @@ import * as adapter from "@pianity/arsnap-adapter"; import { getPermissions, RpcPermission } from "@pianity/arsnap-adapter"; -import { CustomError } from "@/utils/types"; import { REQUIRED_PERMISSIONS } from "@/consts"; export async function getMissingPermissions(permissions: RpcPermission[]) { @@ -14,20 +13,14 @@ export async function getMissingPermissions(permissions: RpcPermission[]) { return missigPermissions; } -export type InitializationErrorKind = - | "WrongMetamaskVersion" - | "InstallationDeclined" - | "PermissionsDeclined"; -export class InitializationError extends CustomError {} - export async function initializeArsnap() { try { await adapter.connect(); } catch (e) { if ((e as any)?.code === -32601) { - throw new InitializationError("WrongMetamaskVersion"); + return "wrongMetamaskVersion"; } - throw new InitializationError("InstallationDeclined"); + return "installationDeclined"; } const missingPermissions = await getMissingPermissions(REQUIRED_PERMISSIONS); @@ -35,9 +28,9 @@ export async function initializeArsnap() { if (missingPermissions.length > 0) { const granted = await adapter.requestPermissions(missingPermissions); if (!granted) { - throw new InitializationError("PermissionsDeclined"); + return "permissionsDeclined"; } } - return true; + return "success"; } diff --git a/packages/wallet/src/views/Welcome.tsx b/packages/wallet/src/views/Welcome.tsx index e68bbf6..203f18a 100644 --- a/packages/wallet/src/views/Welcome.tsx +++ b/packages/wallet/src/views/Welcome.tsx @@ -1,5 +1,5 @@ import arweaveLogo from "@/assets/arweave.svg"; -import { InitializationError, initializeArsnap } from "@/utils/arsnap"; +import { initializeArsnap } from "@/utils/arsnap"; import MetamaskButton from "@/components/interface/MetamaskButton"; import Text from "@/components/interface/typography/Text"; import ViewContainer from "@/components/interface/layout/ViewContainer"; @@ -14,15 +14,12 @@ export default function Welcome({ onInitialized }: WelcomeProps) { if (!window.ethereum) { window.open("https://metamask.io/flask/", "_blank"); } else { - try { - await initializeArsnap(); + const initResult = await initializeArsnap(); + + if (initResult === "success") { onInitialized(); - } catch (e) { - if (e instanceof InitializationError) { - if (e.kind === "WrongMetamaskVersion") { - window.open("https://metamask.io/flask/", "_blank"); - } - } + } else if (initResult === "wrongMetamaskVersion") { + window.open("https://metamask.io/flask/", "_blank"); } } }