diff --git a/apps/namadillo/src/App/Common/ConnectExtensionButton.tsx b/apps/namadillo/src/App/Common/ConnectExtensionButton.tsx index 077dd69fa..136961926 100644 --- a/apps/namadillo/src/App/Common/ConnectExtensionButton.tsx +++ b/apps/namadillo/src/App/Common/ConnectExtensionButton.tsx @@ -21,7 +21,7 @@ export const ConnectExtensionButton = (): JSX.Element => { {connectStatus === "connecting" ? "Connecting..." - : "Connect Keychain"} + : "Connect Keychain"} )} {extensionAttachStatus === "detached" && ( diff --git a/apps/namadillo/src/App/Setup/ExtensionLoader.tsx b/apps/namadillo/src/App/Setup/ExtensionLoader.tsx index 2a324a49f..2ae083adc 100644 --- a/apps/namadillo/src/App/Setup/ExtensionLoader.tsx +++ b/apps/namadillo/src/App/Setup/ExtensionLoader.tsx @@ -1,9 +1,10 @@ -import { useIntegration } from "@namada/integrations"; +import { chainParametersAtom } from "atoms/chain"; import { namadaExtensionAttachStatus, namadaExtensionConnectionStatus, } from "atoms/settings"; -import { useAtom, useSetAtom } from "jotai"; +import { useNamadaKeychain } from "hooks/useNamadaKeychain"; +import { useAtom, useAtomValue, useSetAtom } from "jotai"; import { ReactNode, useEffect } from "react"; import { PageLoader } from "../Common/PageLoader"; @@ -12,19 +13,34 @@ export const ExtensionLoader = ({ }: { children: ReactNode; }): JSX.Element => { + const { namadaKeychain } = useNamadaKeychain(); const [attachStatus, setAttachStatus] = useAtom(namadaExtensionAttachStatus); + const { data: chain } = useAtomValue(chainParametersAtom); const setConnectionStatus = useSetAtom(namadaExtensionConnectionStatus); - const integration = useIntegration("namada"); - useEffect(() => { - setAttachStatus(integration.detect() ? "attached" : "detached"); + const chainId = chain?.chainId; + const keychainPromise = namadaKeychain.get(); - integration.isConnected().then((isConnected) => { - if (isConnected) { - setConnectionStatus("connected"); + useEffect(() => { + keychainPromise.then((injectedNamada) => { + if (injectedNamada) { + return setAttachStatus("attached"); } + setAttachStatus("detached"); }); - }, [integration]); + }, []); + + useEffect(() => { + if (chainId && attachStatus === "attached") { + keychainPromise.then((injectedNamada) => { + injectedNamada.isConnected(chainId).then((isConnected) => { + if (isConnected) { + setConnectionStatus("connected"); + } + }); + }); + } + }, [attachStatus, chainId]); if (attachStatus === "pending") { return ; diff --git a/apps/namadillo/src/App/WorkerTest.tsx b/apps/namadillo/src/App/WorkerTest.tsx index 6cdb83906..68dd435f2 100644 --- a/apps/namadillo/src/App/WorkerTest.tsx +++ b/apps/namadillo/src/App/WorkerTest.tsx @@ -90,7 +90,7 @@ export function WorkerTest(): JSX.Element { const { payload: encodedTx } = await shieldWorker.shield(msg); - const signedTxs = await signTx("namada", encodedTx, account?.address || ""); + const signedTxs = await signTx(encodedTx, account?.address || ""); await shieldWorker.broadcast({ type: "broadcast", diff --git a/apps/namadillo/src/atoms/accounts/atoms.ts b/apps/namadillo/src/atoms/accounts/atoms.ts index e0a2f7540..99bad14ee 100644 --- a/apps/namadillo/src/atoms/accounts/atoms.ts +++ b/apps/namadillo/src/atoms/accounts/atoms.ts @@ -1,4 +1,3 @@ -import { getIntegration } from "@namada/integrations"; import { Account, AccountType } from "@namada/types"; import { indexerApiAtom } from "atoms/api"; import { nativeTokenAddressAtom } from "atoms/chain"; @@ -6,6 +5,7 @@ import { shouldUpdateBalanceAtom } from "atoms/etc"; import { namadaExtensionConnectedAtom } from "atoms/settings"; import { queryDependentFn } from "atoms/utils"; import BigNumber from "bignumber.js"; +import { NamadaKeychain } from "hooks/useNamadaKeychain"; import { atomWithMutation, atomWithQuery } from "jotai-tanstack-query"; import { chainConfigByName } from "registry"; import { @@ -67,16 +67,20 @@ export const allDefaultAccountsAtom = atomWithQuery((get) => { }); export const updateDefaultAccountAtom = atomWithMutation(() => { - const integration = getIntegration("namada"); + const namadaPromise = new NamadaKeychain().get(); return { - mutationFn: (address: string) => integration.updateDefaultAccount(address), + mutationFn: (address: string) => + namadaPromise.then((injectedNamada) => + injectedNamada.updateDefaultAccount(address) + ), }; }); export const disconnectAccountAtom = atomWithMutation(() => { - const integration = getIntegration("namada"); + const namadaPromise = new NamadaKeychain().get(); return { - mutationFn: () => integration.disconnect(), + mutationFn: () => + namadaPromise.then((injectedNamada) => injectedNamada.disconnect()), }; }); diff --git a/apps/namadillo/src/atoms/accounts/services.ts b/apps/namadillo/src/atoms/accounts/services.ts index 87107af4a..d63ca6671 100644 --- a/apps/namadillo/src/atoms/accounts/services.ts +++ b/apps/namadillo/src/atoms/accounts/services.ts @@ -1,16 +1,16 @@ import { Balance, DefaultApi } from "@namada/indexer-client"; -import { getIntegration } from "@namada/integrations"; import { Account } from "@namada/types"; import BigNumber from "bignumber.js"; +import { NamadaKeychain } from "hooks/useNamadaKeychain"; export const fetchAccounts = async (): Promise => { - const namada = getIntegration("namada"); + const namada = await new NamadaKeychain().get(); const result = await namada.accounts(); return result || []; }; export const fetchDefaultAccount = async (): Promise => { - const namada = getIntegration("namada"); + const namada = await new NamadaKeychain().get(); return await namada.defaultAccount(); }; diff --git a/apps/namadillo/src/atoms/arbitraryMessages.ts b/apps/namadillo/src/atoms/arbitraryMessages.ts index 658f9ac8f..69d7f9abc 100644 --- a/apps/namadillo/src/atoms/arbitraryMessages.ts +++ b/apps/namadillo/src/atoms/arbitraryMessages.ts @@ -1,18 +1,20 @@ -import { getIntegration } from "@namada/integrations"; +import { NamadaKeychain } from "hooks/useNamadaKeychain"; import invariant from "invariant"; import { atomWithMutation } from "jotai-tanstack-query"; import { defaultAccountAtom } from "./accounts"; export const arbitraryMessagesAtom = atomWithMutation((get) => { - const integration = getIntegration("namada"); + const namadaPromise = new NamadaKeychain().get(); const account = get(defaultAccountAtom); return { enabled: account.data, mutationFn: async (message: string) => { invariant(account.data, "Default account is not selected"); - const response = await integration - .signer() - ?.signArbitrary(account.data?.address, message); + const response = await namadaPromise.then((injectedNamada) => + injectedNamada + ?.getSigner() + .signArbitrary(account.data!.address, message) + ); return response; }, }; diff --git a/apps/namadillo/src/atoms/shield/services.ts b/apps/namadillo/src/atoms/shield/services.ts index fa0d64772..37cc338b7 100644 --- a/apps/namadillo/src/atoms/shield/services.ts +++ b/apps/namadillo/src/atoms/shield/services.ts @@ -77,7 +77,7 @@ export const submitShieldTx = async ( const { payload: encodedTx } = await shieldWorker.shield(msg); - const signedTxs = await signTx("namada", encodedTx, source); + const signedTxs = await signTx(encodedTx, source); await shieldWorker.broadcast({ type: "broadcast", @@ -133,7 +133,7 @@ export const submitUnshieldTx = async ( const { payload: encodedTx } = await unshieldWorker.unshield(msg); - const signedTxs = await signTx("namada", encodedTx, source); + const signedTxs = await signTx(encodedTx, source); await unshieldWorker.broadcast({ type: "broadcast", diff --git a/apps/namadillo/src/hooks/useNamadaKeychain.ts b/apps/namadillo/src/hooks/useNamadaKeychain.ts index 880e8bbf2..b382357dd 100644 --- a/apps/namadillo/src/hooks/useNamadaKeychain.ts +++ b/apps/namadillo/src/hooks/useNamadaKeychain.ts @@ -11,10 +11,6 @@ import { useCallback, useState } from "react"; export type InjectedNamada = WindowWithNamada["namada"]; export class NamadaKeychain implements Wallet { - static detect(): boolean { - return Boolean((window as WindowWithNamada).namada); - } - // TODO: Should we use this, or keep our existing download buttons? install(): void { console.warn( @@ -66,9 +62,10 @@ export const useNamadaKeychain = (): { const [connectStatus, setConnectStatus] = useAtom( namadaExtensionConnectionStatus ); - const [attachStatus] = useAtom(namadaExtensionAttachStatus); - const [error, setError] = useState(); + const attachStatus = useAtomValue(namadaExtensionAttachStatus); const { data: chain } = useAtomValue(chainParametersAtom); + + const [error, setError] = useState(); const namadaKeychain = new NamadaKeychain(); const chainId = chain?.chainId; diff --git a/apps/namadillo/src/lib/query.ts b/apps/namadillo/src/lib/query.ts index e65f72fc9..31facd927 100644 --- a/apps/namadillo/src/lib/query.ts +++ b/apps/namadillo/src/lib/query.ts @@ -1,9 +1,7 @@ -import { getIntegration } from "@namada/integrations/utils"; import { Sdk } from "@namada/sdk/web"; import { Account, AccountType, - ExtensionKey, Signer, TxMsgValue, TxProps, @@ -11,6 +9,7 @@ import { } from "@namada/types"; import { getIndexerApi } from "atoms/api"; import { chainParametersAtom } from "atoms/chain"; +import { NamadaKeychain } from "hooks/useNamadaKeychain"; import invariant from "invariant"; import { getDefaultStore } from "jotai"; import { Address, ChainSettings, GasConfig } from "types"; @@ -142,12 +141,11 @@ export const buildTx = async ( * Asynchronously signs an encoded batch transaction using Namada extension. */ export const signTx = async ( - wallet: ExtensionKey, typedEncodedTx: EncodedTxData, owner: string ): Promise => { - const integration = getIntegration(wallet); - const signingClient = integration.signer() as Signer; + const namada = await new NamadaKeychain().get(); + const signingClient = namada.getSigner() as Signer; const store = getDefaultStore(); const { data: chainParameters } = store.get(chainParametersAtom); @@ -200,7 +198,7 @@ export const buildTxPair = async ( publicKeyRevealed, memo ); - const signedTxs = await signTx(chain.extensionId, encodedTxData, owner); + const signedTxs = await signTx(encodedTxData, owner); return { signedTxs, encodedTxData, diff --git a/packages/types/src/namada.ts b/packages/types/src/namada.ts index 0701a6346..b41601140 100644 --- a/packages/types/src/namada.ts +++ b/packages/types/src/namada.ts @@ -26,7 +26,7 @@ export type BalancesProps = { }; export interface Namada { - accounts(chainId?: string): Promise; + accounts(chainId?: string): Promise; connect(chainId?: string): Promise; disconnect(chainId?: string): Promise; isConnected(chainId?: string): Promise;