Skip to content

fix(toWalletClient): guard against missing injected provider (closes #212) - #213

Open
chiliec wants to merge 1 commit into
bcnmy:developfrom
chiliec:fix/to-wallet-client-no-injected-provider
Open

chiliec wants to merge 1 commit into
bcnmy:developfrom
chiliec:fix/to-wallet-client-no-injected-provider

Conversation

@chiliec

@chiliec chiliec commented Aug 15, 2026 •

Copy link
Copy Markdown

What

Closes #212.

toNexusAccount throws TypeError: Cannot read properties of undefined (reading 'request') when the user has no wallet extension installed (window.ethereum === undefined) — e.g. when they connect via a 3rd-party email / web2 connector.

Root cause

toWalletClient() decided the browser branch purely from the unresolved signer's transport key (=== "custom") and then built custom(window?.ethereum) unconditionally. The ?. guards window, not window.ethereum, so custom(undefined) produced a transport whose .request is missing — the error surfaces later inside createWalletClient / toNexusAccount.

Fix

Only take the injected-provider branch when an EIP-1193 provider is actually present on window.ethereum. Otherwise fall back to the resolved local signer and the provided transport (the same path non-browser signers already use). Also guards typeof window for non-browser (SSR/node) environments.

Tests

Added src/sdk/account/utils/toWalletClient.test.ts:

  • toWalletClient no longer throws when the transport key is "custom" but no provider is injected.
  • it falls back to the resolved signer account in that case.

Verified genuine RED→GREEN: both tests fail against the current code and pass with the fix.

Validation

  • vitest run (the two new tests) → 2 passed
  • tsc --noEmit → no errors in the changed files
  • biome check on changed files → clean (exit 0)

First-time contributor — happy to adjust the approach or naming.


PR-Codex overview

This PR focuses on improving the handling of the toWalletClient function when the signer is created with a custom transport key but no injected provider (like window.ethereum) is available. It ensures that the application does not crash in such scenarios and falls back to the resolved signer.

Detailed summary

  • Updated logic in toWalletClient to check for an injected provider before using it.
  • Changed the fallback mechanism to use the resolved signer when no provider is present.
  • Added regression tests in toWalletClient.test.ts to cover scenarios where the provider is undefined.
  • Ensured that the application does not throw errors when window.ethereum is not available.

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Closes bcnmy#212. toWalletClient() selected the browser branch purely from the
unresolved signer's transport key ("custom") and then built
custom(window?.ethereum) unconditionally. When a user connects without a wallet
extension (e.g. via a 3rd-party email / web2 connector), window.ethereum is
undefined, so custom(undefined) produced a transport that threw
"Cannot read properties of undefined (reading 'request')" from toNexusAccount.

Only take the injected-provider branch when window.ethereum is actually present;
otherwise fall back to the resolved local signer and the provided transport.

Adds unit tests asserting toWalletClient no longer throws (and uses the resolved
signer account) when the transport key is "custom" but no provider is injected.

This branch has not been deployed

No deployments
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.

[BUG] toNexusAccount fails when the user has no wallet extensions installed

1 participant