Skip to content

Commit

Permalink
refactor: Typescript conversion of log-web3-shim-usage.js (#23732)
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.
-->
Part of #23014 
Fixes #23470 

Converting the level 6 dependency file
`app/scripts/lib/rpc-method-middleware/handlers/log-web3-shim-usage.js`
to typescript for contributing to `metamask-controller.js`.
## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/23732?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
NiranjanaBinoy authored Oct 8, 2024
1 parent 83455b8 commit 29bc2f5
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type {
JsonRpcParams,
Hex,
} from '@metamask/utils';
import { OriginString } from '@metamask/permission-controller';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
import {
HandlerWrapper,
Expand All @@ -28,7 +27,7 @@ export type ProviderStateHandlerResult = {
};

export type GetProviderState = (
origin: OriginString,
origin: string,
) => Promise<ProviderStateHandlerResult>;

type GetProviderStateConstraint<Params extends JsonRpcParams = JsonRpcParams> =
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { JsonRpcEngineEndCallback } from 'json-rpc-engine';
import { PendingJsonRpcResponse } from '@metamask/utils';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
import { HandlerRequestType as LogWeb3ShimUsageHandlerRequest } from './types';
import logWeb3ShimUsage, {
GetWeb3ShimUsageState,
SetWeb3ShimUsageRecorded,
} from './log-web3-shim-usage';

describe('logWeb3ShimUsage', () => {
let mockEnd: JsonRpcEngineEndCallback;
let mockGetWeb3ShimUsageState: GetWeb3ShimUsageState;
let mockSetWeb3ShimUsageRecorded: SetWeb3ShimUsageRecorded;

beforeEach(() => {
mockEnd = jest.fn();
mockGetWeb3ShimUsageState = jest.fn().mockReturnValue(undefined);
mockSetWeb3ShimUsageRecorded = jest.fn();
});

it('should call getWeb3ShimUsageState and setWeb3ShimUsageRecorded when the handler is invoked', async () => {
const req: LogWeb3ShimUsageHandlerRequest = {
origin: 'testOrigin',
params: [],
id: '22',
jsonrpc: '2.0',
method: MESSAGE_TYPE.LOG_WEB3_SHIM_USAGE,
};

const res: PendingJsonRpcResponse<true> = {
id: '22',
jsonrpc: '2.0',
result: true,
};

logWeb3ShimUsage.implementation(req, res, jest.fn(), mockEnd, {
getWeb3ShimUsageState: mockGetWeb3ShimUsageState,
setWeb3ShimUsageRecorded: mockSetWeb3ShimUsageRecorded,
});

expect(mockGetWeb3ShimUsageState).toHaveBeenCalledWith(req.origin);
expect(mockSetWeb3ShimUsageRecorded).toHaveBeenCalled();
expect(res.result).toStrictEqual(true);
expect(mockEnd).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type {
JsonRpcEngineNextCallback,
JsonRpcEngineEndCallback,
} from 'json-rpc-engine';
import type { JsonRpcParams, PendingJsonRpcResponse } from '@metamask/utils';
import { MESSAGE_TYPE } from '../../../../../shared/constants/app';
import {
HandlerWrapper,
HandlerRequestType as LogWeb3ShimUsageHandlerRequest,
} from './types';

export type GetWeb3ShimUsageState = (origin: string) => undefined | 1 | 2;
export type SetWeb3ShimUsageRecorded = (origin: string) => void;

export type LogWeb3ShimUsageOptions = {
getWeb3ShimUsageState: GetWeb3ShimUsageState;
setWeb3ShimUsageRecorded: SetWeb3ShimUsageRecorded;
};
type LogWeb3ShimUsageConstraint<Params extends JsonRpcParams = JsonRpcParams> =
{
implementation: (
req: LogWeb3ShimUsageHandlerRequest<Params>,
res: PendingJsonRpcResponse<true>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
{
getWeb3ShimUsageState,
setWeb3ShimUsageRecorded,
}: LogWeb3ShimUsageOptions,
) => void;
} & HandlerWrapper;
/**
* This RPC method is called by the inpage provider whenever it detects the
* accessing of a non-existent property on our window.web3 shim. We use this
* to alert the user that they are using a legacy dapp, and will have to take
* further steps to be able to use it.
*/
const logWeb3ShimUsage = {
methodNames: [MESSAGE_TYPE.LOG_WEB3_SHIM_USAGE],
implementation: logWeb3ShimUsageHandler,
hookNames: {
getWeb3ShimUsageState: true,
setWeb3ShimUsageRecorded: true,
},
} satisfies LogWeb3ShimUsageConstraint;

export default logWeb3ShimUsage;

/**
* @param req - The JSON-RPC request object.
* @param res - The JSON-RPC response object.
* @param _next - The json-rpc-engine 'next' callback.
* @param end - The json-rpc-engine 'end' callback.
* @param options
* @param options.getWeb3ShimUsageState - A function that gets web3 shim
* usage state for the given origin.
* @param options.setWeb3ShimUsageRecorded - A function that records web3 shim
* usage for a particular origin.
*/
function logWeb3ShimUsageHandler<Params extends JsonRpcParams = JsonRpcParams>(
req: LogWeb3ShimUsageHandlerRequest<Params>,
res: PendingJsonRpcResponse<true>,
_next: JsonRpcEngineNextCallback,
end: JsonRpcEngineEndCallback,
{ getWeb3ShimUsageState, setWeb3ShimUsageRecorded }: LogWeb3ShimUsageOptions,
): void {
const { origin } = req;
if (getWeb3ShimUsageState(origin) === undefined) {
setWeb3ShimUsageRecorded(origin);
}

res.result = true;
return end();
}
3 changes: 1 addition & 2 deletions app/scripts/lib/rpc-method-middleware/handlers/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { OriginString } from '@metamask/permission-controller';
import { JsonRpcParams, JsonRpcRequest } from '@metamask/utils';
import { MessageType } from '../../../../../shared/constants/app';

Expand All @@ -9,5 +8,5 @@ export type HandlerWrapper = {

export type HandlerRequestType<Params extends JsonRpcParams = JsonRpcParams> =
Required<JsonRpcRequest<Params>> & {
origin: OriginString;
origin: string;
};

0 comments on commit 29bc2f5

Please sign in to comment.