Skip to content

Commit

Permalink
fix(extension): [lw-10539] add isSharedWallet key on wallet slive level
Browse files Browse the repository at this point in the history
  • Loading branch information
vetalcore committed Jun 20, 2024
1 parent f5f9810 commit 99fbcb4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { WalletType } from '@cardano-sdk/web-extension';
import { BlockchainProviderSlice, SliceCreator, WalletInfoSlice } from '../types';
import { Wallet } from '@lace/cardano';
import { ObservableWalletState } from '@hooks/useWalletState';
import { isSharedWallet } from '@src/utils/is-shared-wallet';

/**
* has all wallet info related actions and states
Expand All @@ -23,6 +24,7 @@ export const walletInfoSlice: SliceCreator<WalletInfoSlice & BlockchainProviderS
initialHdDiscoveryCompleted: false,
isInMemoryWallet: undefined,
isHardwareWallet: undefined,
isSharedWallet: undefined,
walletType: undefined,
stayOnAllDonePage: false,
setAddressesDiscoveryCompleted: (addressesDiscoveryCompleted) =>
Expand All @@ -36,7 +38,8 @@ export const walletInfoSlice: SliceCreator<WalletInfoSlice & BlockchainProviderS
cardanoWallet: wallet,
walletType: wallet?.source.wallet.type,
isInMemoryWallet: wallet?.source.wallet.type === WalletType.InMemory,
isHardwareWallet: [WalletType.Ledger, WalletType.Trezor].includes(wallet?.source.wallet.type)
isHardwareWallet: [WalletType.Ledger, WalletType.Trezor].includes(wallet?.source.wallet.type),
isSharedWallet: isSharedWallet(wallet)
}),
setCurrentChain: (chain: Wallet.ChainName) => {
set({ currentChain: Wallet.Cardano.ChainIds[chain], environmentName: chain });
Expand Down
1 change: 1 addition & 0 deletions apps/browser-extension-wallet/src/stores/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export interface WalletInfoSlice {
walletType: WalletType;
isInMemoryWallet: boolean;
isHardwareWallet: boolean;
isSharedWallet: boolean;
deletingWallet?: boolean;
stayOnAllDonePage?: boolean;
setDeletingWallet: (deletingWallet: boolean) => void;
Expand Down
28 changes: 28 additions & 0 deletions apps/browser-extension-wallet/src/utils/is-shared-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Cardano } from '@cardano-sdk/core';
import { WalletType } from '@cardano-sdk/web-extension';
import { Wallet } from '@lace/cardano';

// TODO: consume from the wallet package (cardano-js-sdk)
const isValidSharedWalletScript = (script: Cardano.NativeScript): boolean => {
switch (script.kind) {
case Cardano.NativeScriptKind.RequireAllOf:
case Cardano.NativeScriptKind.RequireAnyOf:
case Cardano.NativeScriptKind.RequireNOf:
return script.scripts.every((nativeScript) => nativeScript.kind === Cardano.NativeScriptKind.RequireSignature);
default:
return false;
}
};

export const isSharedWallet = (wallet?: Wallet.CardanoWallet): boolean => {
if (wallet?.source.wallet.type !== WalletType.Script) return false;

const { paymentScript, stakingScript } = wallet?.source.wallet;

return (
Cardano.isNativeScript(paymentScript) &&
isValidSharedWalletScript(paymentScript) &&
Cardano.isNativeScript(stakingScript) &&
isValidSharedWalletScript(stakingScript)
);
};
1 change: 1 addition & 0 deletions apps/browser-extension-wallet/src/utils/mocks/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const walletStoreMock = async (
walletType: WalletType.InMemory,
isInMemoryWallet: true,
isHardwareWallet: false,
isSharedWallet: false,
// TODO: mock [LW-5454]
cardanoWallet: undefined,
isWalletLocked: jest.fn(() => false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { useAnalyticsContext } from '@providers';
import { PostHogAction } from '@providers/AnalyticsProvider/analyticsTracker';
import cn from 'classnames';
import { getWalletAccountsQtyString } from '@src/utils/get-wallet-count-string';
import { useIsSharedWallet } from './utils/isSharedWallet';

const { Title, Text } = Typography;

Expand All @@ -22,10 +21,9 @@ export const SettingsRemoveWallet = ({ popupView }: { popupView?: boolean }): Re

const [isRemoveWalletAlertVisible, setIsRemoveWalletAlertVisible] = useState(false);
const { deleteWallet, walletRepository } = useWalletManager();
const { walletInfo, setDeletingWallet, inMemoryWallet } = useWalletStore();
const { walletInfo, setDeletingWallet, isSharedWallet } = useWalletStore();
const backgroundServices = useBackgroundServiceAPIContext();
const analytics = useAnalyticsContext();
const isSharedWallet = useIsSharedWallet(inMemoryWallet);

const toggleRemoveWalletAlert = () => {
if (isSharedWallet) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useAnalyticsContext, useAppSettingsContext } from '@providers';
import { PHRASE_FREQUENCY_OPTIONS } from '@src/utils/constants';
import { EnhancedAnalyticsOptInStatus, PostHogAction } from '@providers/AnalyticsProvider/analyticsTracker';
import { ENHANCED_ANALYTICS_OPT_IN_STATUS_LS_KEY } from '@providers/AnalyticsProvider/config';
import { useIsSharedWallet } from './utils/isSharedWallet';

const { Title } = Typography;
interface SettingsSecurityProps {
Expand All @@ -28,7 +27,7 @@ export const SettingsSecurity = ({
const [isShowPassphraseDrawerOpen, setIsShowPassphraseDrawerOpen] = useState(false);
const [hideShowPassphraseSetting, setHideShowPassphraseSetting] = useState(true);
const { t } = useTranslation();
const { isWalletLocked, isInMemoryWallet, inMemoryWallet } = useWalletStore();
const { isWalletLocked, isInMemoryWallet, isSharedWallet } = useWalletStore();
const [settings] = useAppSettingsContext();
const { mnemonicVerificationFrequency } = settings;
const frequency = PHRASE_FREQUENCY_OPTIONS.find(({ value }) => value === mnemonicVerificationFrequency)?.label;
Expand All @@ -38,7 +37,6 @@ export const SettingsSecurity = ({
);
const analytics = useAnalyticsContext();
const showPassphraseVerification = process.env.USE_PASSWORD_VERIFICATION === 'true';
const isSharedWallet = useIsSharedWallet(inMemoryWallet);

const handleAnalyticsChoice = async (isOptedIn: boolean) => {
const status = isOptedIn ? EnhancedAnalyticsOptInStatus.OptedIn : EnhancedAnalyticsOptInStatus.OptedOut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import uniq from 'lodash/uniq';
import { isKeyHashAddress } from '@cardano-sdk/wallet';
import { AddressesDiscoveryStatus } from '@lib/communication/addresses-discoverer';
import { CustomSubmitApiDrawer } from './CustomSubmitApiDrawer';
import { useIsSharedWallet } from './utils/isSharedWallet';

const { Title } = Typography;

Expand Down Expand Up @@ -63,7 +62,7 @@ export const SettingsWalletBase = <AdditionalDrawers extends string>({
const closeDrawer = useRedirection(walletRoutePaths.settings);

const { t } = useTranslation();
const { environmentName, inMemoryWallet, walletInfo, setHdDiscoveryStatus } = useWalletStore();
const { environmentName, inMemoryWallet, walletInfo, setHdDiscoveryStatus, isSharedWallet } = useWalletStore();
const { AVAILABLE_CHAINS } = config();

const unspendable = useObservable(inMemoryWallet?.balance?.utxo.unspendable$);
Expand All @@ -73,7 +72,6 @@ export const SettingsWalletBase = <AdditionalDrawers extends string>({
const analytics = useAnalyticsContext();
const { getCustomSubmitApiForNetwork } = useCustomSubmitApi();

const isSharedWallet = useIsSharedWallet(inMemoryWallet);
const isNetworkChoiceEnabled = AVAILABLE_CHAINS.length > 1;
const authorizedAppsEnabled = process.env.USE_DAPP_CONNECTOR === 'true' && !isSharedWallet;

Expand Down

This file was deleted.

0 comments on commit 99fbcb4

Please sign in to comment.