Skip to content

fix: update rpcUrl handling in Eip1193Provider and TurnkeyWalletStrategy#670

Merged
ThomasRalee merged 1 commit into
masterfrom
chore/turnkey-alchemy
May 20, 2026
Merged

fix: update rpcUrl handling in Eip1193Provider and TurnkeyWalletStrategy#670
ThomasRalee merged 1 commit into
masterfrom
chore/turnkey-alchemy

Conversation

@ivan-angjelkoski
Copy link
Copy Markdown
Contributor

@ivan-angjelkoski ivan-angjelkoski commented May 20, 2026

This pull request improves the flexibility and correctness of how RPC URLs are handled for EVM chains in the wallet packages. The main changes standardize the use of chain IDs as numbers (instead of enums) for RPC URL mapping, add utility functions for parsing chain IDs, and ensure that the correct RPC URL is selected based on the current chain context. These updates make it easier to support multiple networks and improve compatibility with various chain ID formats.

EVM Chain ID and RPC URL Handling Improvements:

  • Changed the type of rpcUrls in WalletStrategyEvmOptions and related code to use Partial<Record<number, string>>, ensuring chain IDs are consistently treated as numbers, which improves flexibility for multi-chain support.
  • Added a parseChainId utility to handle both string (including hex) and number chain ID formats, ensuring robust parsing throughout the codebase. [1] [2]
  • Updated the CustomEip1193Provider and its instantiation to accept and correctly prioritize rpcUrl and rpcUrls parameters, allowing dynamic selection of the appropriate RPC URL for the active chain. [1] [2] [3] [4] [5] [6] [7] [8]

Strategy Logic Updates:

  • Modified logic in TurnkeyWalletStrategy to always prioritize rpcUrls[chainId] over rpcUrl for selecting the RPC endpoint, ensuring the correct URL is used for each chain. [1] [2]

EIP-1193 Compliance and Consistency:

  • Ensured that the provider returns the chain ID in hexadecimal format for the eth_chainId method, as required by the EIP-1193 specification.

Summary by CodeRabbit

  • New Features
    • Enhanced RPC URL configuration with support for per-chain endpoints, enabling more granular control over network connections
    • Expanded chain ID compatibility to accept both numeric and string formats for increased flexibility

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

📝 Walkthrough

Walkthrough

This PR refactors RPC URL configuration across the wallet packages to support per-chain RPC endpoints. The base type WalletStrategyEvmOptions now accepts rpcUrls with numeric chain IDs instead of a branded alias. The Eip1193Provider is extended to accept both a default rpcUrl and a chain-specific rpcUrls map, and implements a selector that chooses the URL based on the current chain with fallback. The strategy layer updates RPC URL selection logic to prefer per-chain URLs and passes configuration through to the provider during initialization.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: updating RPC URL handling in two key files (Eip1193Provider and TurnkeyWalletStrategy). It is concise, specific, and directly relates to the primary objective of standardizing RPC URL handling across the wallet packages.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/turnkey-alchemy

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/wallets/wallet-turnkey/src/strategy/Eip1193Provider.ts`:
- Around line 11-21: The parseChainId helper can return NaN for malformed
inputs; update parseChainId (and its callers) to validate the parsed result
(e.g., return null/undefined or throw on invalid parsing) and then guard the
place where you assign to this.chainId (the assignment in the Eip1193Provider
code around the existing this.chainId update) so you only mutate provider state
when the parsed chainId is a finite number (Number.isFinite or !Number.isNaN).
If parsing fails, do not set this.chainId — instead surface an error or reject
the request so eth_chainId and RPC client creation never persist an invalid
value. Ensure you reference parseChainId and the this.chainId assignment when
making the change.

In `@packages/wallets/wallet-turnkey/src/strategy/strategy.ts`:
- Around line 173-174: The error message that validates presence of an RPC
endpoint should be updated to reflect support for both options.rpcUrl and
options.rpcUrls (used in the selection `const url = options.rpcUrls?.[chainId]
|| options.rpcUrl`); find the places that throw when `url` is falsy (including
the other occurrence around lines 380-381) and change the thrown message to
mention "rpcUrl or rpcUrls (indexed by chainId)" or similar so callers using
either option see an accurate error.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8d256294-c6f3-446c-af8a-584d7730389f

📥 Commits

Reviewing files that changed from the base of the PR and between a5d536d and 9b53077.

📒 Files selected for processing (3)
  • packages/wallets/wallet-base/src/types/strategy.ts
  • packages/wallets/wallet-turnkey/src/strategy/Eip1193Provider.ts
  • packages/wallets/wallet-turnkey/src/strategy/strategy.ts

Comment on lines +11 to +21
const parseChainId = (chainId: number | string) => {
if (typeof chainId === 'number') {
return chainId
}

if (chainId.startsWith('0x')) {
return parseInt(chainId.replace('0x', ''), 16)
}

return parseInt(chainId, 10)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Validate parsed chain IDs before mutating provider state.

Line 20 can yield NaN for malformed input, and Line 133 then persists that invalid value into this.chainId. That can break eth_chainId and RPC client creation.

💡 Proposed fix
 const parseChainId = (chainId: number | string) => {
-  if (typeof chainId === 'number') {
-    return chainId
-  }
-
-  if (chainId.startsWith('0x')) {
-    return parseInt(chainId.replace('0x', ''), 16)
-  }
-
-  return parseInt(chainId, 10)
+  const parsed =
+    typeof chainId === 'number'
+      ? chainId
+      : /^0x/i.test(chainId.trim())
+        ? Number.parseInt(chainId.trim().slice(2), 16)
+        : Number.parseInt(chainId.trim(), 10)
+
+  if (!Number.isInteger(parsed) || parsed < 0) {
+    throw new Error('Invalid chainId')
+  }
+
+  return parsed
 }

Also applies to: 133-133

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/wallets/wallet-turnkey/src/strategy/Eip1193Provider.ts` around lines
11 - 21, The parseChainId helper can return NaN for malformed inputs; update
parseChainId (and its callers) to validate the parsed result (e.g., return
null/undefined or throw on invalid parsing) and then guard the place where you
assign to this.chainId (the assignment in the Eip1193Provider code around the
existing this.chainId update) so you only mutate provider state when the parsed
chainId is a finite number (Number.isFinite or !Number.isNaN). If parsing fails,
do not set this.chainId — instead surface an error or reject the request so
eth_chainId and RPC client creation never persist an invalid value. Ensure you
reference parseChainId and the this.chainId assignment when making the change.

Comment on lines +173 to 174
const url = options.rpcUrls?.[chainId] || options.rpcUrl

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Update error messaging to reflect rpcUrls support.

After changing selection precedence, the thrown message still asks only for rpcUrl, which is now inaccurate for callers using rpcUrls.

💡 Proposed fix
- new Error('Please pass rpcUrl within the evmOptions'),
+ new Error('Please pass rpcUrl or rpcUrls within the evmOptions'),

Also applies to: 380-381

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/wallets/wallet-turnkey/src/strategy/strategy.ts` around lines 173 -
174, The error message that validates presence of an RPC endpoint should be
updated to reflect support for both options.rpcUrl and options.rpcUrls (used in
the selection `const url = options.rpcUrls?.[chainId] || options.rpcUrl`); find
the places that throw when `url` is falsy (including the other occurrence around
lines 380-381) and change the thrown message to mention "rpcUrl or rpcUrls
(indexed by chainId)" or similar so callers using either option see an accurate
error.

@ThomasRalee ThomasRalee merged commit 389dcf1 into master May 20, 2026
5 checks passed
@ThomasRalee ThomasRalee deleted the chore/turnkey-alchemy branch May 20, 2026 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants