-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Typescript conversion of log-web3-shim-usage.js (#23732)
<!-- 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
1 parent
83455b8
commit 29bc2f5
Showing
5 changed files
with
122 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
app/scripts/lib/rpc-method-middleware/handlers/log-web3-shim-usage.js
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
app/scripts/lib/rpc-method-middleware/handlers/log-web3-shim-usage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
app/scripts/lib/rpc-method-middleware/handlers/log-web3-shim-usage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters