Skip to content

Commit

Permalink
Fix type errors and SnapBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Nov 21, 2024
1 parent 2e63b19 commit ac0e3ce
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 84 deletions.
8 changes: 2 additions & 6 deletions app/core/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ import {
} from '@metamask/snaps-controllers';

import { WebViewExecutionService } from '@metamask/snaps-controllers/react-native';
import { NotificationParameters } 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 @@ -897,7 +897,7 @@ export class Engine {
type,
requestData: { content, placeholder },
}),
showInAppNotification: (origin: string, args: NotificationParameters) => {
showInAppNotification: (origin: string, args: NotificationArgs) => {
Logger.log(
'Snaps/ showInAppNotification called with args: ',
args,
Expand Down Expand Up @@ -960,7 +960,6 @@ export class Engine {
includeStakedAssets: isPooledStakingFeatureEnabled(),
});
const permissionController = new PermissionController({
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: this.controllerMessenger.getRestricted({
name: 'PermissionController',
allowedActions: [
Expand Down Expand Up @@ -1053,7 +1052,6 @@ export class Engine {

///: BEGIN:ONLY_INCLUDE_IF(preinstalled-snaps,external-snaps)
this.subjectMetadataController = new SubjectMetadataController({
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: this.controllerMessenger.getRestricted({
name: 'SubjectMetadataController',
allowedActions: [`${permissionController.name}:hasPermissions`],
Expand Down Expand Up @@ -1103,7 +1101,6 @@ export class Engine {
const requireAllowlist = process.env.METAMASK_BUILD_TYPE === 'main';
const disableSnapInstallation = process.env.METAMASK_BUILD_TYPE === 'main';
const allowLocalSnaps = process.env.METAMASK_BUILD_TYPE === 'flask';
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
const snapsRegistryMessenger: SnapsRegistryMessenger =
this.controllerMessenger.getRestricted({
name: 'SnapsRegistry',
Expand All @@ -1123,7 +1120,6 @@ export class Engine {
});

this.snapExecutionService = new WebViewExecutionService({
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: this.controllerMessenger.getRestricted({
name: 'ExecutionService',
allowedActions: [],
Expand Down
1 change: 0 additions & 1 deletion app/core/RPCMethods/RPCMethodMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ describe('getRpcMethodMiddleware', () => {
},
]);
const permissionController = new PermissionController({
// @ts-expect-error TODO: Resolve mismatch between base-controller versions.
messenger: controllerMessenger.getRestricted({
name: 'PermissionController',
allowedActions: [],
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"@metamask/swappable-obj-proxy": "^2.1.0",
"@metamask/swaps-controller": "^10.0.0",
"@metamask/transaction-controller": "^38.3.0",
"@metamask/utils": "^9.2.1",
"@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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5751,10 +5751,10 @@
lodash "^4.17.21"
uuid "^8.3.2"

"@metamask/utils@^10.0.0":
version "10.0.0"
resolved "https://registry.yarnpkg.com/@metamask/utils/-/utils-10.0.0.tgz#9285e6e195810e8b7c875147ac64981b4be51733"
integrity sha512-EoNZJijLqBbir8ikuiHBHfhCqE1s8Odae3bhtRAd8itJB109xmfFF84djY/iaQI+EAp59Sy7iwengfRohaTK8A==
"@metamask/utils@^10.0.0", "@metamask/utils@^10.0.1":
version "10.0.1"
resolved "https://registry.yarnpkg.com/@metamask/utils/-/utils-10.0.1.tgz#a765f96c20e35fc51c068fb9f88a3332b40b215e"
integrity sha512-zHgAitJtRwviVVFnRUA2PLRMaAwatr3jiHgiH7mPicJaeSK4ma01aGR4fHy0iy5tlVo1ZiioTmJ1Hbp8FZ6pSg==
dependencies:
"@ethereumjs/tx" "^4.2.0"
"@metamask/superstruct" "^3.1.0"
Expand Down

0 comments on commit ac0e3ce

Please sign in to comment.