Skip to content

Commit

Permalink
fix: typescript + linting (#4788)
Browse files Browse the repository at this point in the history
## Explanation
This PR fixes a lot of the linting and typescript errors. still some
left but this covers a lot of it.


<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/package-a`

- **<CATEGORY>**: Your change here
- **<CATEGORY>**: Your change here

### `@metamask/package-b`

- **<CATEGORY>**: Your change here
- **<CATEGORY>**: Your change here

## Checklist

- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes

---------

Co-authored-by: Jiexi Luan <[email protected]>
  • Loading branch information
shanejonas and jiexi authored Oct 11, 2024
1 parent 4e52fc0 commit 151eaac
Show file tree
Hide file tree
Showing 36 changed files with 452 additions and 262 deletions.
8 changes: 4 additions & 4 deletions packages/multichain/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
branches: 83.05,
functions: 87.37,
lines: 86.73,
statements: 87.17,
},
},
});
30 changes: 20 additions & 10 deletions packages/multichain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,43 @@
"sideEffects": false,
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/types/index.d.ts",
"main": "./dist/index.cjs",
"types": "./dist/index.d.cts",
"files": [
"dist/"
],
"scripts": {
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
"build:docs": "typedoc",
"changelog:update": "../../scripts/update-changelog.sh @metamask/multichain",
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/multichain",
"publish:preview": "yarn npm publish --tag preview",
"test": "jest --reporters=jest-silent-reporter",
"test:clean": "jest --clearCache",
"test:verbose": "jest --verbose",
"test:watch": "jest --watch"
"since-latest-release": "../../scripts/since-latest-release.sh",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
},
"dependencies": {
"@metamask/api-specs": "^0.10.12",
"@metamask/controller-utils": "^11.3.0",
"@metamask/eth-json-rpc-filters": "^7.0.0",
"@metamask/json-rpc-engine": "^9.0.3",
"@metamask/rpc-errors": "^6.3.1",
"@metamask/safe-event-emitter": "^3.0.0",
"@metamask/utils": "^9.1.0",
"@open-rpc/schema-utils-js": "^2.0.5",
"jsonschema": "^1.2.4",
"lodash": "^4.17.21"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Caip25CaveatValue } from '../caip25Permission';
import type { Caip25CaveatValue } from '../caip25Permission';
import {
getEthAccounts,
setEthAccounts,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {
CaipAccountId,
Hex,
type CaipAccountId,
type Hex,
KnownCaipNamespace,
parseCaipAccountId,
} from '@metamask/utils';
import { Caip25CaveatValue } from '../caip25Permission';
import {
mergeScopes,
parseScopeString,
ScopesObject,
ScopeString,
} from '../scope';

import type { Caip25CaveatValue } from '../caip25Permission';
import type { ScopesObject } from '../scope';
import { mergeScopes, parseScopeString, type ScopeString } from '../scope';

const isEip155ScopeString = (scopeString: ScopeString) => {
const { namespace, reference } = parseScopeString(scopeString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { providerErrors } from '@metamask/rpc-errors';
import type { JsonRpcRequest } from '@metamask/utils';

import {
Caip25CaveatType,
Caip25EndowmentPermissionName,
} from '../caip25permissions';
import { CaipPermissionAdapterMiddleware } from './caip-permission-adapter-middleware';
} from '../caip25Permission';
import { caipPermissionAdapterMiddleware } from './caip-permission-adapter-middleware';

const baseRequest = {
id: 1,
jsonrpc: '2.0' as const,
origin: 'http://test.com',
networkClientId: 'mainnet',
method: 'eth_call',
Expand Down Expand Up @@ -48,7 +52,7 @@ const createMockedHandler = () => {
});
const getNetworkConfigurationByNetworkClientId = jest
.fn()
.mockImplementation((networkClientId) => {
.mockImplementation((networkClientId: string) => {
const chainId =
{
mainnet: '0x1',
Expand All @@ -58,8 +62,13 @@ const createMockedHandler = () => {
chainId,
};
});
const handler = (request) =>
CaipPermissionAdapterMiddleware(request, {}, next, end, {
const handler = (
request: JsonRpcRequest & {
networkClientId: string;
origin: string;
},
) =>
caipPermissionAdapterMiddleware(request, {}, next, end, {
getCaveat,
getNetworkConfigurationByNetworkClientId,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import type { NetworkConfiguration } from '@metamask/network-controller';
import type { Caveat } from '@metamask/permission-controller';
import { providerErrors } from '@metamask/rpc-errors';
import type { JsonRpcRequest } from '@metamask/utils';

import type { Caip25CaveatValue } from '../caip25Permission';
import {
Caip25CaveatType,
Caip25EndowmentPermissionName,
} from '../caip25permissions';
} from '../caip25Permission';
import type { ScopeString } from '../scope';
import { mergeScopes } from '../scope';

export async function CaipPermissionAdapterMiddleware(
request,
_response,
next,
end,
hooks,
/**
* Middleware to handle CAIP-25 permission requests.
*
* @param request - The request object.
* @param _response - The response object.
* @param next - The next middleware function.
* @param end - The end function.
* @param hooks - The hooks object.
* @param hooks.getCaveat - Function to retrieve a caveat.
* @param hooks.getNetworkConfigurationByNetworkClientId - Function to retrieve a network configuration.
*/
export async function caipPermissionAdapterMiddleware(
request: JsonRpcRequest & {
networkClientId: string;
origin: string;
},
_response: unknown,
next: () => Promise<void>,
end: (error?: Error) => void,
hooks: {
getCaveat: (
...args: unknown[]
) => Caveat<typeof Caip25CaveatType, Caip25CaveatValue>;
getNetworkConfigurationByNetworkClientId: (
networkClientId: string,
) => NetworkConfiguration;
},
) {
const { networkClientId, method } = request;

Expand All @@ -31,7 +58,7 @@ export async function CaipPermissionAdapterMiddleware(
const { chainId } =
hooks.getNetworkConfigurationByNetworkClientId(networkClientId);

const scope = `eip155:${parseInt(chainId, 16)}`;
const scope: ScopeString = `eip155:${parseInt(chainId, 16)}`;

const scopesObject = mergeScopes(
caveat.value.requiredScopes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Caip25CaveatValue } from '../caip25Permission';
import type { Caip25CaveatValue } from '../caip25Permission';
import { KnownNotifications, KnownRpcMethods } from '../scope';
import {
addPermittedEthChainId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Hex, KnownCaipNamespace } from '@metamask/utils';
import { toHex } from '@metamask/controller-utils';
import { Caip25CaveatValue } from '../caip25Permission';
import type { Hex } from '@metamask/utils';
import { KnownCaipNamespace } from '@metamask/utils';

import type { Caip25CaveatValue } from '../caip25Permission';
import type { ScopesObject, ScopeString } from '../scope';
import {
KnownNotifications,
KnownRpcMethods,
mergeScopes,
parseScopeString,
ScopesObject,
ScopeString,
} from '../scope';

export const getPermittedEthChainIds = (
Expand Down
59 changes: 31 additions & 28 deletions packages/multichain/src/caip25Permission.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import type { NonEmptyArray } from '@metamask/controller-utils';
import type { CaveatConstraint } from '@metamask/permission-controller';
import {
CaveatConstraint,
CaveatMutatorOperation,
PermissionType,
SubjectType,
} from '@metamask/permission-controller';
import { NonEmptyArray } from '@metamask/controller-utils';
import * as Scope from './scope';

import type { Caip25CaveatValue } from './caip25Permission';
import {
Caip25CaveatType,
Caip25CaveatValue,
caip25EndowmentBuilder,
Caip25EndowmentPermissionName,
Caip25CaveatMutatorFactories,
removeScope,
} from './caip25Permission';
import * as Scope from './scope';

jest.mock('./scope', () => ({
validateAndFlattenScopes: jest.fn(),
Expand Down Expand Up @@ -655,34 +656,36 @@ describe('endowment:caip25', () => {
},
},
});
validator({
caveats: [
{
type: Caip25CaveatType,
value: {
requiredScopes: {
'eip155:1': {
methods: ['eth_chainId'],
notifications: [],
accounts: ['eip155:1:0xdead'],
expect(
validator({
caveats: [
{
type: Caip25CaveatType,
value: {
requiredScopes: {
'eip155:1': {
methods: ['eth_chainId'],
notifications: [],
accounts: ['eip155:1:0xdead'],
},
},
},
optionalScopes: {
'eip155:5': {
methods: [],
notifications: [],
accounts: ['eip155:5:0xbeef'],
optionalScopes: {
'eip155:5': {
methods: [],
notifications: [],
accounts: ['eip155:5:0xbeef'],
},
},
isMultichainOrigin: true,
},
isMultichainOrigin: true,
},
},
],
date: 1234,
id: '1',
invoker: 'test.com',
parentCapability: Caip25EndowmentPermissionName,
});
],
date: 1234,
id: '1',
invoker: 'test.com',
parentCapability: Caip25EndowmentPermissionName,
}),
).toBeUndefined();
});
});
});
Loading

0 comments on commit 151eaac

Please sign in to comment.