Skip to content

Commit

Permalink
scaffold remove snap keyring warning
Browse files Browse the repository at this point in the history
  • Loading branch information
owencraston committed Sep 26, 2024
1 parent 4b8abcf commit 758d148
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useEffect, useRef, useState } from 'react';

import { Snap } from '@metamask/snaps-utils';
import BottomSheet, { BottomSheetRef } from 'app/component-library/components/BottomSheets/BottomSheet';
import { selectProviderConfig } from 'app/selectors/networkController';
import { useSelector } from 'react-redux';
import Text, { TextVariant } from 'app/component-library/components/Texts/Text';

interface KeyringRemovalSnapWarningProps {
snap: Snap;
keyringAccounts: { name: string; address: string }[];
onCancel: () => void;
onClose: () => void;
onSubmit: () => void;
onBack: () => void;
}

export default function KeyringRemovalSnapWarning({ snap,
keyringAccounts,
onCancel,
onClose,
onSubmit,
onBack,
}: KeyringRemovalSnapWarningProps) {


const [showConfirmation, setShowConfirmation] = useState(false);
const [confirmedRemoval, setConfirmedRemoval] = useState(false);
const [confirmationInput, setConfirmationInput] = useState('');
const [error, setError] = useState(false);
const { chainId } = useSelector(selectProviderConfig);

const bottomSheetRef = useRef<BottomSheetRef>(null);

useEffect(() => {
setShowConfirmation(keyringAccounts.length === 0);
}, [keyringAccounts]);

return (
<BottomSheet ref={bottomSheetRef} onClose={onClose} >
<Text variant={TextVariant.HeadingSM}>This is a warning</Text>;
</BottomSheet >
);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './KeyringRemovalSnapWarning';
48 changes: 43 additions & 5 deletions app/components/Views/Snaps/SnapSettings/SnapSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///: BEGIN:ONLY_INCLUDE_IF(external-snaps)
import React, { useCallback, useEffect } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { View, ScrollView, SafeAreaView } from 'react-native';

import Engine from '../../../../core/Engine';
Expand Down Expand Up @@ -28,6 +28,9 @@ import { useStyles } from '../../../hooks/useStyles';
import { useSelector } from 'react-redux';
import SNAP_SETTINGS_REMOVE_BUTTON from './SnapSettings.constants';
import { selectPermissionControllerState } from '../../../../selectors/snaps/permissionController';
import KeyringRemovalSnapWarning from './KeyringSnapRemovalWarning/KeyringRemovalSnapWarning';
import { getAccountsBySnapId } from '../../../../core/SnapKeyring/utils/getAccountsBySnapId';
import { selectInternalAccounts } from 'app/selectors/accountsController';

interface SnapSettingsProps {
snap: Snap;
Expand All @@ -42,9 +45,15 @@ const SnapSettings = () => {
const navigation = useNavigation();

const { snap } = useParams<SnapSettingsProps>();

const permissionsState = useSelector(selectPermissionControllerState);


// eslint-disable-next-line no-unused-vars -- Main build does not use setKeyringAccounts
const [keyringAccounts, setKeyringAccounts] = useState([]);
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
const internalAccounts = useSelector(selectInternalAccounts);
///: END:ONLY_INCLUDE_IF

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getPermissionSubjects(state: any) {
Expand All @@ -71,13 +80,29 @@ const SnapSettings = () => {
}, [colors, navigation, snap.manifest.proposedName]);

const removeSnap = useCallback(async () => {
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { SnapController } = Engine.context as any;
const { SnapController } = Engine.context;
await SnapController.removeSnap(snap.id);
navigation.goBack();
}, [navigation, snap.id]);


let isKeyringSnap = false;
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
isKeyringSnap = Boolean(permissionsFromController?.snap_manageAccounts);

useEffect(() => {
if (isKeyringSnap) {
(async () => {
const addresses = await getAccountsBySnapId(snap.id);
const snapIdentities = Object.values(internalAccounts).filter(
(internalAccount) =>
addresses.includes(internalAccount.address.toLowerCase()),
);
setKeyringAccounts(snapIdentities);

Check failure on line 101 in app/components/Views/Snaps/SnapSettings/SnapSettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Argument of type '{ type: "eip155:eoa" | "eip155:erc4337" | "bip122:p2wpkh"; id: string; address: string; options: Record<string, Json>; methods: string[]; metadata: { name: string; importTime: number; keyring: { ...; }; nameLastUpdatedAt?: number | undefined; snap?: { ...; } | undefined; lastSelected?: number | undefined; }; }[]' is not assignable to parameter of type 'SetStateAction<never[]>'.
})();
}
}, [snap?.id, internalAccounts, isKeyringSnap]);

return (
<SafeAreaView style={styles.snapSettingsContainer}>
<ScrollView>
Expand Down Expand Up @@ -115,6 +140,19 @@ const SnapSettings = () => {
onPress={removeSnap}
/>
</View>

{
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
<KeyringRemovalSnapWarning
onClose={() => { }}
snap={snap}
onBack={() => { }}
onCancel={() => { }}
onSubmit={() => { }}
keyringAccounts={keyringAccounts}
/>
///: END:ONLY_INCLUDE_IF
}
</ScrollView>
</SafeAreaView>
);
Expand Down
24 changes: 14 additions & 10 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,7 +1363,7 @@ class Engine {

return Boolean(
hasProperty(showIncomingTransactions, currentChainId) &&
showIncomingTransactions?.[currentHexChainId],
showIncomingTransactions?.[currentHexChainId],
);
},
updateTransactions: true,
Expand Down Expand Up @@ -1716,7 +1716,7 @@ class Engine {
(state: NetworkState) => {
if (
state.networksMetadata[state.selectedNetworkClientId].status ===
NetworkStatus.Available &&
NetworkStatus.Available &&
networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.chainId !== currentChainId
Expand All @@ -1741,10 +1741,9 @@ class Engine {
} catch (error) {
console.error(
error,
`Network ID not changed, current chainId: ${
networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.chainId
`Network ID not changed, current chainId: ${networkController.getNetworkClientById(
networkController?.state.selectedNetworkClientId,
).configuration.chainId
}`,
);
}
Expand Down Expand Up @@ -1876,7 +1875,7 @@ class Engine {
const decimalsToShow = (currentCurrency === 'usd' && 2) || undefined;
if (
accountsByChainId?.[toHexadecimal(chainId)]?.[
selectSelectedInternalAccountChecksummedAddress
selectSelectedInternalAccountChecksummedAddress
]
) {
ethFiat = weiToFiatNumber(
Expand Down Expand Up @@ -1908,9 +1907,9 @@ class Engine {
item.balance ||
(item.address in tokenBalances
? renderFromTokenMinimalUnit(
tokenBalances[item.address],
item.decimals,
)
tokenBalances[item.address],
item.decimals,
)
: undefined);
const tokenBalanceFiat = balanceToFiatNumber(
// TODO: Fix this by handling or eliminating the undefined case
Expand Down Expand Up @@ -2327,4 +2326,9 @@ export default {
assertEngineExists(instance);
return instance.getGlobalEthQuery();
},

getSnapKeyring: () => {
assertEngineExists(instance);
return instance.getSnapKeyring();
}
};
16 changes: 16 additions & 0 deletions app/core/SnapKeyring/utils/getAccountsBySnapId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SnapKeyring } from '@metamask/eth-snap-keyring';
import { SnapId } from '@metamask/snaps-sdk';
import Engine from 'app/core/Engine';

/**
* Get the addresses of the accounts managed by a given Snap.
*
* @param snapId - Snap ID to get accounts for.
* @returns The addresses of the accounts.
*/
export const getAccountsBySnapId = async (
snapId: SnapId,
) => {
const snapKeyring: SnapKeyring = await Engine.getSnapKeyring() as SnapKeyring;
return await snapKeyring.getAccountsBySnapId(snapId);
};
12 changes: 6 additions & 6 deletions ios/MetaMask.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */
07CBADD9D4B441008304F8D3 /* EuclidCircularB-Light.otf in Resources */ = {isa = PBXBuildFile; fileRef = A98029A3662F4C1391489A6B /* EuclidCircularB-Light.otf */; };
08B7A641467C4723B98328E9 /* CentraNo1-Medium.otf in Resources */ = {isa = PBXBuildFile; fileRef = F97653CAD1D04E1B8713C428 /* CentraNo1-Medium.otf */; };
0FD509E0336BF221F6527B24 /* BuildFile in Frameworks */ = {isa = PBXBuildFile; };
0FD509E0336BF221F6527B24 /* (null) in Frameworks */ = {isa = PBXBuildFile; };
13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.m */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
Expand Down Expand Up @@ -134,15 +134,15 @@
B339FF2E289ABD70001B89FB /* EuclidCircularB-SemiboldItalic.otf in Resources */ = {isa = PBXBuildFile; fileRef = 9499B01ECAC44DA29AC44E80 /* EuclidCircularB-SemiboldItalic.otf */; };
B339FF32289ABD70001B89FB /* Branch.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 153F84C92319B8DB00C19B63 /* Branch.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
B339FF3C289ABF2C001B89FB /* MetaMask-QA-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = B339FEA72899852C001B89FB /* MetaMask-QA-Info.plist */; };
B638844E306CAE9147B52C85 /* BuildFile in Frameworks */ = {isa = PBXBuildFile; };
B638844E306CAE9147B52C85 /* (null) in Frameworks */ = {isa = PBXBuildFile; };
BF39E5BAE0F34F9091FF6AC0 /* EuclidCircularB-Semibold.otf in Resources */ = {isa = PBXBuildFile; fileRef = A8DE9C5BC0714D648276E123 /* EuclidCircularB-Semibold.otf */; };
CD13D926E1E84D9ABFE672C0 /* Roboto-BlackItalic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 3E2492C67CF345CABD7B8601 /* Roboto-BlackItalic.ttf */; };
CF9895772A3B49BE00B4C9B5 /* RCTMinimizer.m in Sources */ = {isa = PBXBuildFile; fileRef = CF9895762A3B49BE00B4C9B5 /* RCTMinimizer.m */; };
CF9895782A3B49BE00B4C9B5 /* RCTMinimizer.m in Sources */ = {isa = PBXBuildFile; fileRef = CF9895762A3B49BE00B4C9B5 /* RCTMinimizer.m */; };
CF98DA9C28D9FEB700096782 /* RCTScreenshotDetect.m in Sources */ = {isa = PBXBuildFile; fileRef = CF98DA9B28D9FEB700096782 /* RCTScreenshotDetect.m */; };
CFD8DFC828EDD4C800CC75F6 /* RCTScreenshotDetect.m in Sources */ = {isa = PBXBuildFile; fileRef = CF98DA9B28D9FEB700096782 /* RCTScreenshotDetect.m */; };
D171C39A8BD44DBEB6B68480 /* EuclidCircularB-MediumItalic.otf in Resources */ = {isa = PBXBuildFile; fileRef = 42CBA652072F4BE2A8B815C1 /* EuclidCircularB-MediumItalic.otf */; };
D45BF85DECACCB74EDCBE88A /* BuildFile in Frameworks */ = {isa = PBXBuildFile; };
D45BF85DECACCB74EDCBE88A /* (null) in Frameworks */ = {isa = PBXBuildFile; };
D5BA0E32DFAA451781D5093E /* CentraNo1-BoldItalic.otf in Resources */ = {isa = PBXBuildFile; fileRef = 4560812198A247039A1CF5A5 /* CentraNo1-BoldItalic.otf */; };
DADE8F39CE81410A98B9B805 /* MMSans-Regular.otf in Resources */ = {isa = PBXBuildFile; fileRef = 2EBD310362314C3ABFF40AD1 /* MMSans-Regular.otf */; };
DC6A024F56DD43E1A83B47B1 /* Roboto-MediumItalic.ttf in Resources */ = {isa = PBXBuildFile; fileRef = D5FF0FF1DFB74B3C8BB99E09 /* Roboto-MediumItalic.ttf */; };
Expand Down Expand Up @@ -337,9 +337,9 @@
650F2B9D24DC5FF200C3B9C4 /* libRCTAesForked.a in Frameworks */,
153C1ABB2217BCDC0088EFE0 /* JavaScriptCore.framework in Frameworks */,
153F84CA2319B8FD00C19B63 /* Branch.framework in Frameworks */,
0FD509E0336BF221F6527B24 /* BuildFile in Frameworks */,
D45BF85DECACCB74EDCBE88A /* BuildFile in Frameworks */,
B638844E306CAE9147B52C85 /* BuildFile in Frameworks */,
0FD509E0336BF221F6527B24 /* (null) in Frameworks */,
D45BF85DECACCB74EDCBE88A /* (null) in Frameworks */,
B638844E306CAE9147B52C85 /* (null) in Frameworks */,
ED2E8FE6D71BE9319F3B27D3 /* libPods-MetaMask.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down

0 comments on commit 758d148

Please sign in to comment.