Skip to content

Commit

Permalink
chore: Bump Snaps packages (#12350)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

Bumps all Snaps packages to their latest versions and deletes some
unnecessary fetch polyfilling code. This fixes an issue where local
Snaps would fail to install in the Flask flavor of mobile.
  • Loading branch information
FrederikBolding authored Nov 28, 2024
1 parent ffe45d0 commit bd06be7
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 180 deletions.
17 changes: 6 additions & 11 deletions app/core/Engine/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
} from '@metamask/snaps-controllers';

import { WebViewExecutionService } from '@metamask/snaps-controllers/react-native';
import { NotificationArgs } from '@metamask/snaps-rpc-methods/dist/restricted/notify.cjs';
import type { NotificationArgs } from '@metamask/snaps-rpc-methods/dist/restricted/notify.cjs';
import { getSnapsWebViewPromise } from '../../lib/snaps';
import {
buildSnapEndowmentSpecifications,
Expand Down Expand Up @@ -109,8 +109,6 @@ import {
ExcludedSnapPermissions,
EndowmentPermissions,
detectSnapLocation,
fetchFunction,
DetectSnapLocationOptions,
} from '../Snaps';
import { getRpcMethodMiddleware } from '../RPCMethods/RPCMethodMiddleware';

Expand Down Expand Up @@ -906,6 +904,10 @@ export class Engine {

this.snapController = new SnapController({
environmentEndowmentPermissions: Object.values(EndowmentPermissions),
excludedPermissions: {
...ExcludedSnapPermissions,
...ExcludedSnapEndowments,
},
featureFlags: {
requireAllowlist,
allowLocalSnaps,
Expand All @@ -915,14 +917,7 @@ export class Engine {
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
messenger: snapControllerMessenger as any,
detectSnapLocation: (
location: string | URL,
options?: DetectSnapLocationOptions,
) =>
detectSnapLocation(location, {
...options,
fetch: fetchFunction,
}),
detectSnapLocation,
//@ts-expect-error types need to be aligned with snaps-controllers
preinstalledSnaps: PREINSTALLED_SNAPS,
//@ts-expect-error types need to be aligned between new encryptor and snaps-controllers
Expand Down
101 changes: 31 additions & 70 deletions app/core/Snaps/SnapBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import {
} from '@metamask/swappable-obj-proxy';
import { JsonRpcEngine } from 'json-rpc-engine';
import { createEngineStream } from '@metamask/json-rpc-middleware-stream';
import { NetworksChainId } from '@metamask/controller-utils';
import EthQuery from '@metamask/eth-query';

import Engine from '../Engine';
import { setupMultiplex } from '../../util/streams';
import Logger from '../../util/Logger';
import { getAllNetworks } from '../../util/networks';
import snapMethodMiddlewareBuilder from './SnapsMethodMiddleware';
import { SubjectType } from '@metamask/permission-controller';

Expand Down Expand Up @@ -68,6 +67,7 @@ export default class SnapBridge {
this.snapId = snapId;
this.stream = connectionStream;
this.getRPCMethodMiddleware = getRPCMethodMiddleware;
this.deprecatedNetworkVersions = {};

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -109,11 +109,10 @@ export default class SnapBridge {
this.blockTracker = blockTracker;
};

getProviderState() {
const memState = this.getState();
async getProviderState() {
return {
isUnlocked: this.isUnlocked(),
...this.getProviderNetworkState(memState),
...(await this.getProviderNetworkState(this.snapId)),
};
}

Expand Down Expand Up @@ -193,84 +192,46 @@ export default class SnapBridge {
return engine;
};

getNetworkState = ({ network }: { network: string }) => {
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { NetworkController } = Engine.context as any;
const networkType = NetworkController.state.providerConfig.type;
const networkProvider = NetworkController.state.providerConfig;

const isInitialNetwork =
networkType && getAllNetworks().includes(networkType);
let chainId;

if (isInitialNetwork) {
chainId = NetworksChainId[networkType];
} else if (networkType === 'rpc') {
chainId = networkProvider.chainId;
}
if (chainId && !chainId.startsWith('0x')) {
// Convert to hex
chainId = `0x${parseInt(chainId, 10).toString(16)}`;
}

const result = {
networkVersion: network,
chainId,
};
return result;
};

isUnlocked = (): boolean => {
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { KeyringController } = Engine.context as any;
return KeyringController.isUnlocked();
};

getState = () => {
const { context, datamodel } = Engine;
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { KeyringController } = context as any;
const vault = KeyringController.state.vault;
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { network, selectedAddress } = datamodel.flatState as any;
return {
isInitialized: !!vault,
isUnlocked: true,
network,
selectedAddress,
};
};

getProviderNetworkState({ network }: { network: string }) {
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { NetworkController } = Engine.context as any;
const networkType = NetworkController.state.providerConfig.type;
const networkProvider = NetworkController.state.providerConfig;
async getProviderNetworkState(origin: string) {
const networkClientId = Engine.controllerMessenger.call(
'SelectedNetworkController:getNetworkClientIdForDomain',
origin,
);

const isInitialNetwork =
networkType && getAllNetworks().includes(networkType);
let chainId;
const networkClient = Engine.controllerMessenger.call(
'NetworkController:getNetworkClientById',
networkClientId,
);

if (isInitialNetwork) {
chainId = NetworksChainId[networkType];
} else if (networkType === 'rpc') {
chainId = networkProvider.chainId;
}
if (chainId && !chainId.startsWith('0x')) {
// Convert to hex
chainId = `0x${parseInt(chainId, 10).toString(16)}`;
const { chainId } = networkClient.configuration;

let networkVersion = this.deprecatedNetworkVersions[networkClientId];
if (!networkVersion) {
const ethQuery = new EthQuery(networkClient.provider);
networkVersion = await new Promise((resolve) => {
ethQuery.sendAsync({ method: 'net_version' }, (error, result) => {
if (error) {
console.error(error);
resolve(null);
} else {
resolve(result);
}
});
});
this.deprecatedNetworkVersions[networkClientId] = networkVersion;
}

const result = {
networkVersion: network,
return {
chainId,
networkVersion: networkVersion ?? 'loading',
};
return result;
}
}
///: END:ONLY_INCLUDE_IF
2 changes: 0 additions & 2 deletions app/core/Snaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from './permissions/permissions';
import {
detectSnapLocation,
fetchFunction,
DetectSnapLocationOptions,
} from './location';

Expand All @@ -16,7 +15,6 @@ export {
ExcludedSnapPermissions,
ExcludedSnapEndowments,
EndowmentPermissions,
fetchFunction,
detectSnapLocation,
};
export type { DetectSnapLocationOptions };
Expand Down
53 changes: 0 additions & 53 deletions app/core/Snaps/location/fetch.ts

This file was deleted.

1 change: 0 additions & 1 deletion app/core/Snaps/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
export * from './location';
export * from './fetch';
///: END:ONLY_INCLUDE_IF
12 changes: 10 additions & 2 deletions app/core/Snaps/permissions/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
// TODO: Figure out which permissions should be disabled at this point
export const ExcludedSnapPermissions = Object.freeze([]);
export const ExcludedSnapEndowments = Object.freeze([]);
export const ExcludedSnapPermissions = Object.freeze({
eth_accounts:
'eth_accounts is disabled. For more information please see https://github.com/MetaMask/snaps/issues/990.',
});
export const ExcludedSnapEndowments = Object.freeze({});

export const EndowmentPermissions = Object.freeze({
'endowment:network-access': 'endowment:network-access',
'endowment:transaction-insight': 'endowment:transaction-insight',
'endowment:cronjob': 'endowment:cronjob',
'endowment:ethereum-provider': 'endowment:ethereum-provider',
'endowment:rpc': 'endowment:rpc',
'endowment:webassembly': 'endowment:webassembly',
'endowment:lifecycle-hooks': 'endowment:lifecycle-hooks',
'endowment:page-home': 'endowment:page-home',
'endowment:signature-insight': 'endowment:signature-insight',
'endowment:name-lookup': 'endowment:name-lookup',
'endowment:keyring': 'endowment:keyring',
} as const);
///: END:ONLY_INCLUDE_IF
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,16 @@
"@metamask/signature-controller": "^22.0.0",
"@metamask/slip44": "3.1.0",
"@metamask/smart-transactions-controller": "^14.0.0",
"@metamask/snaps-controllers": "^9.8.0",
"@metamask/snaps-execution-environments": "^6.7.2",
"@metamask/snaps-rpc-methods": "^11.1.1",
"@metamask/snaps-sdk": "^6.5.1",
"@metamask/snaps-utils": "^8.1.1",
"@metamask/snaps-controllers": "^9.13.0",
"@metamask/snaps-execution-environments": "^6.10.0",
"@metamask/snaps-rpc-methods": "^11.5.1",
"@metamask/snaps-sdk": "^6.11.0",
"@metamask/snaps-utils": "^8.6.0",
"@metamask/stake-sdk": "^0.2.13",
"@metamask/swappable-obj-proxy": "^2.1.0",
"@metamask/swaps-controller": "^11.0.0",
"@metamask/transaction-controller": "^39.1.0",
"@metamask/utils": "^10.0.0",
"@metamask/utils": "^10.0.1",
"@ngraveio/bc-ur": "^1.1.6",
"@notifee/react-native": "^9.0.0",
"@react-native-async-storage/async-storage": "^1.23.1",
Expand Down
Loading

0 comments on commit bd06be7

Please sign in to comment.