-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Typescript conversion of ethereum-chain-utils.js #26103
base: main
Are you sure you want to change the base?
Conversation
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
Converted ethereum-chain-utils.js
to TypeScript, enhancing type safety and maintainability.
- File Added:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
- Converted functions like
validateChainId
,validateSwitchEthereumChainParams
,validateAddEthereumChainParams
, andswitchChain
to TypeScript.
- Converted functions like
- File Added:
app/scripts/lib/rpc-method-middleware/handlers/types.ts
- Defined TypeScript types for handler wrappers, approval flows, caveat retrieval, chain permissions, user approvals, and network settings.
Ensure thorough testing to validate type definitions against actual data structures and usage patterns.
2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
(updates since last review)
Converted ethereum-chain-utils.js
to TypeScript, enhancing type safety and maintainability.
- File Removed:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.js
- Deleted JavaScript file as part of TypeScript migration.
- File Added:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
- Converted utility functions like
validateChainId
,validateSwitchEthereumChainParams
,validateAddEthereumChainParams
, andswitchChain
to TypeScript.
- Converted utility functions like
- File Added:
app/scripts/lib/rpc-method-middleware/handlers/types.ts
- Defined TypeScript types for handler wrappers, approval flows, caveat retrieval, chain permissions, user approvals, and network settings.
Ensure thorough testing to validate type definitions against actual data structures and usage patterns.
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like most of the as assertions being added that could be removed:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
(updates since last review)
Converted ethereum-chain-utils.js
to TypeScript, enhancing type safety and maintainability.
- File Removed:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.js
- Deleted JavaScript file as part of TypeScript migration.
- File Added:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
- Converted utility functions like
validateChainId
,validateSwitchEthereumChainParams
,validateAddEthereumChainParams
, andswitchChain
to TypeScript.
- Converted utility functions like
- File Modified:
app/scripts/lib/rpc-method-middleware/handlers/types.ts
- Added new TypeScript type definitions for
FindNetworkConfigurationBy
,GetCaveat
,GetChainPermissionsFeatureFlag
,RequestPermittedChainsPermission
,RequestUserApproval
, andSetActiveNetwork
. - Removed
HandlerWrapper
type, indicating a possible refactor or simplification.
- Added new TypeScript type definitions for
2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Summary
(updates since last review)
Converted ethereum-chain-utils.js
to TypeScript, enhancing type safety and maintainability.
- File Removed:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.js
- Deleted JavaScript file as part of TypeScript migration.
- File Added:
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
- Converted utility functions like
validateChainId
,validateSwitchEthereumChainParams
,validateAddEthereumChainParams
, andswitchChain
to TypeScript.
- Converted utility functions like
- File Modified:
app/scripts/lib/rpc-method-middleware/handlers/types.ts
- Added new TypeScript type definitions for
FindNetworkConfigurationBy
,GetCaveat
,GetChainPermissionsFeatureFlag
,RequestPermittedChainsPermission
,RequestUserApproval
, andSetActiveNetwork
. - Removed
HandlerWrapper
type, indicating a possible refactor or simplification.
- Added new TypeScript type definitions for
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings
export function validateChainId(chainId) { | ||
const _chainId = typeof chainId === 'string' && chainId.toLowerCase(); | ||
export function validateChainId(chainId: Hex): Hex { | ||
const _chainId = chainId.toLowerCase() as Hex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MajorLift When we do chainId.toLowerCase()
, it seems to be returning type string
. Is there any way for chainId
to be Hex
without the as
assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can also define a type guard here. The value is Hex
predicate is safe to use since any string that's validated by isPrefixedFormattedHexString
must be equivalent to or narrower than the Hex
type.
diff --git a/app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts b/app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
index 002d6554fc..8622b1047c 100644
--- a/app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
+++ b/app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.ts
@@ -58,9 +58,13 @@ export function findExistingNetwork(
}
export function validateChainId(chainId: Hex): Hex {
- const _chainId = chainId.toLowerCase() as Hex;
+ const _chainId = chainId.toLowerCase();
- if (!isPrefixedFormattedHexString(_chainId)) {
+ if (
+ !((value: string): value is Hex => isPrefixedFormattedHexString(value))(
+ _chainId,
+ )
+ ) {
throw ethErrors.rpc.invalidParams({
message: `Expected 0x-prefixed, unpadded, non-zero hexadecimal string 'chainId'. Received:\n${chainId}`,
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toLowerCase
does unnecessarily widen the type of chainId
, and an as assertion is one way to resolve this.
However, there's a better way to reason about the type of _chainId
. Because the code immediately invokes the validator function isPrefixedFormattedHexString
, we are provided with a runtime guarantee that any _chainId
value that doesn't throw an error in the first if-block will be of a type narrower than Hex
.
So now all we have to do is make sure this guarantee is reflected at the type-level as well, which we achieve by using the type guard with the value is Hex
predicate.
This successfully narrows _chainId
back to Hex
past the first if-block. Without relying on any assertions, or introducing any uncertainty about value-level vs. type-level discrepancies.
e72e678
to
3b36759
Compare
…ypes to types.ts file
3b36759
to
a60947e
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #26103 +/- ##
===========================================
+ Coverage 69.98% 70.12% +0.14%
===========================================
Files 1422 1428 +6
Lines 49926 50107 +181
Branches 13861 13897 +36
===========================================
+ Hits 34940 35136 +196
+ Misses 14986 14971 -15 ☔ View full report in Codecov by Sentry. |
Builds ready [a60947e]
Page Load Metrics (82 ± 24 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
Quality Gate passedIssues Measures |
Builds ready [4111fb1]
Page Load Metrics (67 ± 10 ms)
Bundle size diffs [🚨 Warning! Bundle size has increased!]
|
This PR has been automatically marked as stale because it has not had recent activity in the last 60 days. It will be closed in 14 days. Thank you for your contributions. |
Description
Converting the level 6 dependency file
app/scripts/lib/rpc-method-middleware/handlers/ethereum-chain-utils.js
to typescript for contributing to metamask-controller.js.Related issues
Part of #23014
Fixes: #26102
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Pre-merge reviewer checklist