Skip to content

Commit

Permalink
feat: remove dependency on integrations package
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Nov 25, 2024
1 parent d3ce3b8 commit 6be9e40
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 39 deletions.
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/Common/ConnectExtensionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const ConnectExtensionButton = (): JSX.Element => {
<ActionButton backgroundColor="yellow" size="sm" onClick={connect}>
{connectStatus === "connecting" ?
"Connecting..."
: "Connect Keychain"}
: "Connect Keychain"}
</ActionButton>
)}
{extensionAttachStatus === "detached" && (
Expand Down
34 changes: 25 additions & 9 deletions apps/namadillo/src/App/Setup/ExtensionLoader.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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 <PageLoader />;
Expand Down
2 changes: 1 addition & 1 deletion apps/namadillo/src/App/WorkerTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 9 additions & 5 deletions apps/namadillo/src/atoms/accounts/atoms.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getIntegration } from "@namada/integrations";
import { Account, AccountType } from "@namada/types";
import { indexerApiAtom } from "atoms/api";
import { nativeTokenAddressAtom } from "atoms/chain";
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 {
Expand Down Expand Up @@ -67,16 +67,20 @@ export const allDefaultAccountsAtom = atomWithQuery<Account[]>((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()),
};
});

Expand Down
6 changes: 3 additions & 3 deletions apps/namadillo/src/atoms/accounts/services.ts
Original file line number Diff line number Diff line change
@@ -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<readonly Account[]> => {
const namada = getIntegration("namada");
const namada = await new NamadaKeychain().get();
const result = await namada.accounts();
return result || [];
};

export const fetchDefaultAccount = async (): Promise<Account | undefined> => {
const namada = getIntegration("namada");
const namada = await new NamadaKeychain().get();
return await namada.defaultAccount();
};

Expand Down
12 changes: 7 additions & 5 deletions apps/namadillo/src/atoms/arbitraryMessages.ts
Original file line number Diff line number Diff line change
@@ -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;
},
};
Expand Down
4 changes: 2 additions & 2 deletions apps/namadillo/src/atoms/shield/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 3 additions & 6 deletions apps/namadillo/src/hooks/useNamadaKeychain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -66,9 +62,10 @@ export const useNamadaKeychain = (): {
const [connectStatus, setConnectStatus] = useAtom(
namadaExtensionConnectionStatus
);
const [attachStatus] = useAtom(namadaExtensionAttachStatus);
const [error, setError] = useState<string>();
const attachStatus = useAtomValue(namadaExtensionAttachStatus);
const { data: chain } = useAtomValue(chainParametersAtom);

const [error, setError] = useState<string>();
const namadaKeychain = new NamadaKeychain();
const chainId = chain?.chainId;

Expand Down
10 changes: 4 additions & 6 deletions apps/namadillo/src/lib/query.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { getIntegration } from "@namada/integrations/utils";
import { Sdk } from "@namada/sdk/web";
import {
Account,
AccountType,
ExtensionKey,
Signer,
TxMsgValue,
TxProps,
WrapperTxProps,
} 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";
Expand Down Expand Up @@ -142,12 +141,11 @@ export const buildTx = async <T>(
* Asynchronously signs an encoded batch transaction using Namada extension.
*/
export const signTx = async <T>(
wallet: ExtensionKey,
typedEncodedTx: EncodedTxData<T>,
owner: string
): Promise<Uint8Array[]> => {
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);
Expand Down Expand Up @@ -200,7 +198,7 @@ export const buildTxPair = async <T>(
publicKeyRevealed,
memo
);
const signedTxs = await signTx<T>(chain.extensionId, encodedTxData, owner);
const signedTxs = await signTx<T>(encodedTxData, owner);
return {
signedTxs,
encodedTxData,
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/namada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type BalancesProps = {
};

export interface Namada {
accounts(chainId?: string): Promise<Account[] | undefined>;
accounts(chainId?: string): Promise<readonly Account[] | undefined>;
connect(chainId?: string): Promise<void>;
disconnect(chainId?: string): Promise<void>;
isConnected(chainId?: string): Promise<boolean | undefined>;
Expand Down

0 comments on commit 6be9e40

Please sign in to comment.