Skip to content

Commit

Permalink
base keyring exists checks on recoil state (#3358)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlinton authored Mar 14, 2023
1 parent f2ba23b commit 8d107d3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import type { Blockchain, SignedWalletDescriptor } from "@coral-xyz/common";
import {
getAddMessage,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
UI_RPC_METHOD_LEDGER_IMPORT,
} from "@coral-xyz/common";
import { useBackgroundClient } from "@coral-xyz/recoil";
import { useBackgroundClient, useEnabledBlockchains } from "@coral-xyz/recoil";

import { HardwareOnboard } from "../../../../Onboarding/pages/HardwareOnboard";

Expand All @@ -24,16 +23,12 @@ export function ConnectHardware({
onComplete: () => void;
}) {
const background = useBackgroundClient();
const enabledBlockchains = useEnabledBlockchains();
const keyringExists = enabledBlockchains.includes(blockchain);

const handleHardwareOnboardComplete = async (
signedWalletDescriptor: SignedWalletDescriptor
) => {
const blockchainKeyrings = await background.request({
method: UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
params: [],
});
const keyringExists = blockchainKeyrings.includes(blockchain);

if (keyringExists) {
// Just import the wallet because the keyring already exists
await background.request({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
getAddMessage,
openConnectHardware,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
UI_RPC_METHOD_FIND_WALLET_DESCRIPTOR,
UI_RPC_METHOD_KEYRING_DERIVE_WALLET,
UI_RPC_METHOD_KEYRING_IMPORT_WALLET,
Expand All @@ -17,6 +16,7 @@ import {
} from "@coral-xyz/react-common";
import {
useBackgroundClient,
useEnabledBlockchains,
useKeyringHasMnemonic,
useRpcRequests,
useUser,
Expand All @@ -38,12 +38,14 @@ export function CreateMenu({ blockchain }: { blockchain: Blockchain }) {
const background = useBackgroundClient();
const hasMnemonic = useKeyringHasMnemonic();
const user = useUser();
const [keyringExists, setKeyringExists] = useState(false);
const enabledBlockchains = useEnabledBlockchains();
const keyringExists = enabledBlockchains.includes(blockchain);
const { close: closeParentDrawer } = useDrawerContext();
const { signMessageForWallet } = useRpcRequests();

const [newPublicKey, setNewPublicKey] = useState("");
const [openDrawer, setOpenDrawer] = useState(false);
const [loading, setLoading] = useState(false);
const { close: closeParentDrawer } = useDrawerContext();
const { signMessageForWallet } = useRpcRequests();

// If the keyring or if we don't have any public keys of the type we are
// adding then additional logic is required to select the account index of
Expand Down Expand Up @@ -75,16 +77,6 @@ export function CreateMenu({ blockchain }: { blockchain: Blockchain }) {
};
}, [nav]);

useEffect(() => {
(async () => {
const blockchainKeyrings = await background.request({
method: UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
params: [],
});
setKeyringExists(blockchainKeyrings.includes(blockchain));
})();
}, [background, blockchain]);

const createNewWithPhrase = async () => {
// Mnemonic based keyring. This is the simple case because we don't
// need to prompt for the user to open their Ledger app to get the
Expand Down Expand Up @@ -138,8 +130,6 @@ export function CreateMenu({ blockchain }: { blockchain: Blockchain }) {
});
}
newPublicKey = walletDescriptor.publicKey;
// Keyring now exists, toggle to other options
setKeyringExists(true);
} else {
newPublicKey = await background.request({
method: UI_RPC_METHOD_KEYRING_DERIVE_WALLET,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useEffect, useState } from "react";
import { useEffect } from "react";
import type { Blockchain } from "@coral-xyz/common";
import {
openConnectHardware,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
} from "@coral-xyz/common";
import { openConnectHardware } from "@coral-xyz/common";
import {
BackpackMnemonicIcon,
HardwareIcon,
Expand All @@ -12,7 +9,7 @@ import {
SecretKeyIcon,
} from "@coral-xyz/react-common";
import {
useBackgroundClient,
useEnabledBlockchains,
useKeyringHasMnemonic,
useUser,
} from "@coral-xyz/recoil";
Expand All @@ -24,10 +21,10 @@ import { SettingsList } from "../../../common/Settings/List";

export function ImportMenu({ blockchain }: { blockchain: Blockchain }) {
const navigation = useNavigation();
const background = useBackgroundClient();
const hasMnemonic = useKeyringHasMnemonic();
const user = useUser();
const [keyringExists, setKeyringExists] = useState(false);
const enabledBlockchains = useEnabledBlockchains();
const keyringExists = enabledBlockchains.includes(blockchain);

useEffect(() => {
const prevTitle = navigation.title;
Expand All @@ -37,16 +34,6 @@ export function ImportMenu({ blockchain }: { blockchain: Blockchain }) {
};
}, [navigation]);

useEffect(() => {
(async () => {
const blockchainKeyrings = await background.request({
method: UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
params: [],
});
setKeyringExists(blockchainKeyrings.includes(blockchain));
})();
}, [background, blockchain]);

const importMenu = {
...(hasMnemonic
? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import type { Blockchain } from "@coral-xyz/common";
import {
getAddMessage,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
UI_RPC_METHOD_KEYRING_IMPORT_SECRET_KEY,
} from "@coral-xyz/common";
import { useBackgroundClient, useRpcRequests } from "@coral-xyz/recoil";
import {
useBackgroundClient,
useEnabledBlockchains,
useRpcRequests,
} from "@coral-xyz/recoil";
import { useCustomTheme } from "@coral-xyz/themes";

import { PrivateKeyInput } from "../../../common/Account/PrivateKeyInput";
Expand All @@ -28,21 +31,13 @@ export function ImportSecretKey({
const background = useBackgroundClient();
const nav = useNavigation();
const theme = useCustomTheme();
const [keyringExists, setKeyringExists] = useState(false);
const [openDrawer, setOpenDrawer] = useState(false);
const [newPublicKey, setNewPublicKey] = useState("");
const enabledBlockchains = useEnabledBlockchains();
const keyringExists = enabledBlockchains.includes(blockchain);
const { signMessageForWallet } = useRpcRequests();
const { close: closeParentDrawer } = useDrawerContext();

useEffect(() => {
(async () => {
const blockchainKeyrings = await background.request({
method: UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
params: [],
});
setKeyringExists(blockchainKeyrings.includes(blockchain));
})();
}, [background, blockchain]);
const [openDrawer, setOpenDrawer] = useState(false);
const [newPublicKey, setNewPublicKey] = useState("");

useEffect(() => {
const prevTitle = nav.title;
Expand Down
3 changes: 0 additions & 3 deletions packages/background/src/frontend/server-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
UI_RPC_METHOD_APPROVED_ORIGINS_READ,
UI_RPC_METHOD_APPROVED_ORIGINS_UPDATE,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD,
UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ,
UI_RPC_METHOD_ETHEREUM_CHAIN_ID_READ,
UI_RPC_METHOD_ETHEREUM_CHAIN_ID_UPDATE,
UI_RPC_METHOD_ETHEREUM_CONNECTION_URL_READ,
Expand Down Expand Up @@ -311,8 +310,6 @@ async function handle<T = any>(
// @ts-ignore
...params
);
case UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ:
return await handleBlockchainKeyringsRead(ctx);
case UI_RPC_METHOD_KEY_IS_COLD_UPDATE:
return await handleKeyIsColdUpdate(ctx, params[0], params[1]);
//
Expand Down
2 changes: 0 additions & 2 deletions packages/common/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ export const UI_RPC_METHOD_BLOCKCHAINS_ENABLED_DELETE =
"ui-rpc-method-blockchains-enabled-delete";
export const UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_ADD =
"ui-rpc-method-blockchain-keyrings-add";
export const UI_RPC_METHOD_BLOCKCHAIN_KEYRINGS_READ =
"ui-rpc-method-blockchain-keyrings-read";
export const UI_RPC_METHOD_FIND_SERVER_PUBLIC_KEY_CONFLICTS =
"ui-rpc-method-find-server-public-key-conflicts";
export const UI_RPC_METHOD_FIND_WALLET_DESCRIPTOR =
Expand Down

1 comment on commit 8d107d3

@vercel
Copy link

@vercel vercel bot commented on 8d107d3 Mar 14, 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.