Skip to content

Commit

Permalink
feat(refactor): Remove ChainSafe Snap (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat authored Nov 20, 2024
1 parent 77dc9b8 commit d46b8ee
Show file tree
Hide file tree
Showing 10 changed files with 623 additions and 524 deletions.
2 changes: 1 addition & 1 deletion library/extension-assets/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@w3ux/extension-assets-source",
"license": "GPL-3.0-only",
"version": "0.4.0",
"version": "1.0.0-beta.1",
"type": "module",
"scripts": {
"clear": "rm -rf node_modules dist tsconfig.tsbuildinfo",
Expand Down
1 change: 0 additions & 1 deletion library/extension-assets/src/ChainSafeSnap/icon.svg

This file was deleted.

11 changes: 0 additions & 11 deletions library/extension-assets/src/ChainSafeSnap/info.json

This file was deleted.

14 changes: 6 additions & 8 deletions library/react-connect-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
{
"name": "@w3ux/react-connect-kit-source",
"license": "GPL-3.0-only",
"version": "1.9.0",
"version": "2.0.0-beta.2",
"type": "module",
"scripts": {
"clear": "rm -rf node_modules dist tsconfig.tsbuildinfo",
"build": "gulp --silent"
},
"dependencies": {
"@chainsafe/metamask-polkadot-adapter": "^0.6.0",
"@polkagate/extension-dapp": "^0.46.13",
"@w3ux/extension-assets": "^0.4.0",
"@w3ux/hooks": "^1.2.1",
"@w3ux/utils": "^1.1.0"
"@polkagate/extension-dapp": "^0.48.2",
"@w3ux/extension-assets": "1.0.0-beta.1",
"@w3ux/hooks": "^1.3.1-beta.7",
"@w3ux/utils": "^1.1.1-beta.11"
},
"devDependencies": {
"@chainsafe/metamask-polkadot-types": "^0.7.0",
"@types/react": "^18",
"@w3ux/types": "^0.2.0",
"@w3ux/types": "^0.2.1-beta.1",
"gulp": "^5.0.0",
"gulp-sourcemaps": "^3.0.0",
"gulp-strip-comments": "^2.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
localStorageOrDefault,
setStateWithRef,
} from "@w3ux/utils";
import { DEFAULT_SS58, defaultExtensionAccountsContext } from "./defaults";
import { defaultExtensionAccountsContext } from "./defaults";
import { ImportedAccount } from "../types";
import {
ExtensionAccount,
Expand All @@ -18,8 +18,6 @@ import {
ExtensionAccountsProviderProps,
} from "./types";
import { useImportExtension } from "./useImportExtension";
import { initPolkadotSnap } from "./snap";
import { SnapNetworks } from "@chainsafe/metamask-polkadot-types";
import { Extensions } from "./Extensions";
import {
connectActiveExtensionAccount,
Expand Down Expand Up @@ -104,9 +102,6 @@ export const ExtensionAccountsProvider = ({
return;
}

// Pre-connect: Inject extensions into `injectedWeb3` if not already injected.
await handleExtensionAdapters(extensionIds);

// Iterate previously connected extensions and retreive valid `enable` functions.
// ------------------------------------------------------------------------------
const rawExtensions = Extensions.getFromIds(extensionIds);
Expand Down Expand Up @@ -211,9 +206,6 @@ export const ExtensionAccountsProvider = ({
`unknown_extension_${extensionsInitialisedRef.current.length + 1}`
);
} else {
// Pre-connect: Inject into `injectedWeb3` if the provided extension is not already injected.
await handleExtensionAdapters([id]);

try {
// Attempt to get extension `enable` property.
const { enable } = window.injectedWeb3[id];
Expand Down Expand Up @@ -306,22 +298,6 @@ export const ExtensionAccountsProvider = ({
updateInitialisedExtensions(id);
};

// Handle adaptors for extensions that are not supported by `injectedWeb3`.
const handleExtensionAdapters = async (extensionIds: string[]) => {
try {
// Connect to Metamask Polkadot Snap and inject into `injectedWeb3` if avaialble.
if (extensionIds.find((id) => id === "metamask-polkadot-snap")) {
await initPolkadotSnap({
networkName: network as SnapNetworks,
addressPrefix: DEFAULT_SS58,
});
}
} catch (e) {
// Provided network is not supported, or something else went wrong with initialisation.
// Silently fail.
}
};

// Update initialised extensions.
const updateInitialisedExtensions = (id: string) => {
if (!extensionsInitialisedRef.current.includes(id)) {
Expand Down
98 changes: 0 additions & 98 deletions library/react-connect-kit/src/ExtensionAccountsProvider/snap.ts

This file was deleted.

86 changes: 31 additions & 55 deletions library/react-connect-kit/src/ExtensionsProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from "react";
import type { ExtensionStatus, ExtensionsContextInterface } from "./types";
import { defaultExtensionsContext } from "./defaults";
import { polkadotSnapAvailable } from "./utils";
import extensions from "@w3ux/extension-assets";
import { web3Enable } from "@polkagate/extension-dapp";

Expand Down Expand Up @@ -49,11 +48,6 @@ export const ExtensionsProvider = ({
>({});
const extensionsStatusRef = useRef(extensionsStatus);

// Store whether Metamask Snaps are enabled.
const [chainSafeSnapEnabled] = useState<boolean>(
options?.chainSafeSnapEnabled || false
);

// Store whether Metamask Snaps are enabled.
const [polkaGateSnapEnabled] = useState<boolean>(
options?.polkagateSnapEnabled || false
Expand All @@ -66,35 +60,46 @@ export const ExtensionsProvider = ({
// Handle completed interval check for `injectedWeb3`.
//
// Clear interval and move on to checking for Metamask Polkadot Snap.
const handleClearInterval = (hasInjectedWeb3: boolean) => {
const handleClearInterval = async (hasInjectedWeb3: boolean) => {
clearInterval(injectedWeb3Interval);
// Check if Metamask Polkadot Snap is available.
handleSnapInjection(hasInjectedWeb3);
};

// Handle injecting of `metamask-polkadot-snap` into injectedWeb3 if avaialble, and complete
// `injectedWeb3` syncing process.
const handleSnapInjection = async (hasInjectedWeb3: boolean) => {
// Inject ChainSafe Snap if enabled.
const snapAvailable =
(await polkadotSnapAvailable()) && chainSafeSnapEnabled;

// Inject PolkaGate Snap if enabled.
// Check if Metamask PolkaGate Snap is available.
if (polkaGateSnapEnabled) {
await withTimeout(500, web3Enable("snap_only"));
}

if (hasInjectedWeb3 || snapAvailable) {
setStateWithRef(
getExtensionsStatus(snapAvailable),
setExtensionsStatus,
extensionsStatusRef
);
if (hasInjectedWeb3) {
setStateWithRef(
getExtensionsStatus(),
setExtensionsStatus,
extensionsStatusRef
);
}
}

setStateWithRef(false, setCheckingInjectedWeb3, checkingInjectedWeb3Ref);
};

// Getter for the currently installed extensions.
//
// Loops through the supported extensios and checks if they are present in `injectedWeb3`. Adds
// `installed` status to the extension if it is present.
const getExtensionsStatus = () => {
const { injectedWeb3 } = window;
const newExtensionsStatus = { ...extensionsStatus };
const extensionsAsArray = Object.entries(extensions).map(
([key, value]) => ({
id: key,
...value,
})
);
extensionsAsArray.forEach((e) => {
if (injectedWeb3[e.id] !== undefined) {
newExtensionsStatus[e.id] = "installed";
}
});

return newExtensionsStatus;
};

// Setter for an extension status.
const setExtensionStatus = (id: string, status: ExtensionStatus) => {
setStateWithRef(
Expand All @@ -119,34 +124,6 @@ export const ExtensionsProvider = ({
);
};

// Getter for the currently installed extensions.
//
// Loops through the supported extensios and checks if they are present in `injectedWeb3`. Adds
// `installed` status to the extension if it is present.
const getExtensionsStatus = (snapAvailable: boolean) => {
const { injectedWeb3 } = window;

const newExtensionsStatus = { ...extensionsStatus };
if (snapAvailable) {
newExtensionsStatus["metamask-polkadot-snap"] = "installed";
}

const extensionsAsArray = Object.entries(extensions).map(
([key, value]) => ({
id: key,
...value,
})
);

extensionsAsArray.forEach((e) => {
if (injectedWeb3[e.id] !== undefined) {
newExtensionsStatus[e.id] = "installed";
}
});

return newExtensionsStatus;
};

// Checks if an extension has been installed.
const extensionInstalled = (id: string): boolean =>
extensionsStatus[id] !== undefined;
Expand Down Expand Up @@ -179,7 +156,6 @@ export const ExtensionsProvider = ({

injectedWeb3Interval = setInterval(() => {
injectCounter.current++;

if (injectCounter.current === totalChecks) {
handleClearInterval(false);
} else {
Expand Down
Loading

0 comments on commit d46b8ee

Please sign in to comment.