Skip to content
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

feat: walletprovider adapters type narrowing #5468

Merged
merged 12 commits into from
Oct 18, 2023
Merged

Conversation

gomesalexandre
Copy link
Contributor

@gomesalexandre gomesalexandre commented Oct 16, 2023

Description

Ensures the types of adapters and related structures/methods across the <WalletProvider /> domain are properly narrowed down, vs. any currently.

Consequently adds additional type safety to accommodate for the added typings.

Pull Request Type

  • 🐛 Bug fix (Non-breaking Change: Fixes an issue)
  • 🛠️ Chore (Non-breaking Change: Doc updates, pkg upgrades, typos, etc..)
  • 💅 New Feature (Breaking/Non-breaking Change)

Issue (if applicable)

Risk

The same as the PRs below - while this mostly tackles things at type-level, there are slight runtime changes, mostly moving pieces arounds, which does present a regression risk

Testing

Engineering

  • ☝🏽

Operations

  • ☝🏽

Screenshots (if applicable)

Copy link
Contributor Author

gomesalexandre commented Oct 16, 2023

@gomesalexandre gomesalexandre changed the title wip: walletprovider type narrowing wip: walletprovider adapters type narrowing Oct 16, 2023
Comment on lines +2 to +11
import type { KkRestAdapter } from '@keepkey/hdwallet-keepkey-rest'
import type { CoinbaseAdapter } from '@shapeshiftoss/hdwallet-coinbase'
import type { WebUSBKeepKeyAdapter } from '@shapeshiftoss/hdwallet-keepkey-webusb'
import type { KeplrAdapter } from '@shapeshiftoss/hdwallet-keplr'
import type { WebUSBLedgerAdapter as LedgerAdapter } from '@shapeshiftoss/hdwallet-ledger-webusb'
import type { MetaMaskAdapter } from '@shapeshiftoss/hdwallet-metamask'
import type { NativeAdapter } from '@shapeshiftoss/hdwallet-native'
import type { MetaMaskAdapter as MetaMaskMultiChainAdapter } from '@shapeshiftoss/hdwallet-shapeshift-multichain'
import type { WalletConnectV2Adapter } from '@shapeshiftoss/hdwallet-walletconnectv2'
import type { XDEFIAdapter } from '@shapeshiftoss/hdwallet-xdefi'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note how all of these are imported as type, which shouldn't bring any import at runtime, ensuring we don't void our lazy guarantees

[KeyManager.Native]: SupportedWalletInfo<typeof NativeAdapter>
[KeyManager.Mobile]: SupportedWalletInfo<typeof NativeAdapter>
[KeyManager.Demo]: SupportedWalletInfo<typeof NativeAdapter>
// TODO(gomes): export WebUSBKeepKeyAdapter as a type in hdwallet, not a declare const
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Most likely the root cause of all the ts-ignore across this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping it as-is for now, the types refactor required to make this work would be a lot more complex, rage quitted after ~2 hours

@gomesalexandre gomesalexandre changed the title wip: walletprovider adapters type narrowing feat: walletprovider adapters type narrowing Oct 16, 2023
@@ -65,6 +67,7 @@ export const KeepKeyConnect = () => {
return wallet
} catch (e) {
const secondAdapter = await getAdapter(KeyManager.KeepKey, 1)
// @ts-ignore TODO(gomes): FIXME, most likely borked because of WebUSBKeepKeyAdapter
Copy link
Contributor Author

Choose a reason for hiding this comment

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

WebUSBKeepKeyAdapter types are too borked, fiddled for two hours in hdwallet with no success here, leaving this as-is for now - there is no effective runtime change here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping it as-is for now, the types refactor required to make this work would be a lot more complex, rage quitted after ~2 hours

try {
currentAdapters.set(localWalletType, [adapter])
// @ts-ignore TODO(gomes): FIXME, most likely borked because of WebUSBKeepKeyAdapter
Copy link
Contributor Author

Choose a reason for hiding this comment

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

WebUSBKeepKeyAdapter types are too borked, fiddled for two hours in hdwallet with no success here, leaving this as-is for now - there is no effective runtime change here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually a different issue - things are not narrowed down enough at this stage and need to declare the adapter in each switch case before assigning it - 875f795

const sdk = await setupKeepKeySDK()
return await keepKeyAdapters[0]?.pairDevice(sdk)
// @ts-ignore TODO(gomes): FIXME, most likely borked because of WebUSBKeepKeyAdapter
Copy link
Contributor Author

Choose a reason for hiding this comment

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

WebUSBKeepKeyAdapter types are too borked, fiddled for two hours in hdwallet with no success here, leaving this as-is for now - there is no effective runtime change here

adapters: [
{
loadAdapter: () =>
// @ts-ignore TODO(gomes): FIXME
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not happy with this, but probably our best bet a0622d8

@gomesalexandre gomesalexandre marked this pull request as ready for review October 16, 2023 17:17
@gomesalexandre gomesalexandre requested a review from a team as a code owner October 16, 2023 17:17
@@ -900,7 +900,7 @@ export const WalletProvider = ({ children }: { children: React.ReactNode }): JSX
})
}, [])

useEffect(() => load(), [load, state.adapters, state.keyring])
useEffect(() => load(), [load, state.keyring])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't want to run this reactively on state.adapters anymore after this stack/PR refactor:

  1. This stack's refactor makes it so that we getAdapter() so we don't need to be reactive on state.adapters anymore
  2. This PR's refactor moved some initialization logic in each switch case instead of the previous outer scope, meaning we would have infinite dispatch and reactivity after the dispatch of WalletActions.SET_ADAPTERS in load()

adapters: [
{
loadAdapter: () =>
getConfig().REACT_APP_EXPERIMENTAL_MM_SNAPPY_FINGERS
? import('@shapeshiftoss/hdwallet-shapeshift-multichain').then(m => m.MetaMaskAdapter)
? import('@shapeshiftoss/hdwallet-shapeshift-multichain').then(
m => m.MetaMaskAdapter as typeof MetaMaskAdapter,
Copy link
Member

Choose a reason for hiding this comment

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

is this cast still required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still is unfortunately:

          Type 'Promise<MetaMaskShapeShiftMultiChainHDWallet | undefined>' is not assignable to type 'Promise<MetaMaskHDWallet | undefined>'.
            Type 'MetaMaskShapeShiftMultiChainHDWallet | undefined' is not assignable to type 'MetaMaskHDWallet | undefined'.
              Type 'MetaMaskShapeShiftMultiChainHDWallet' is missing the following properties from type 'MetaMaskHDWallet': _supportsSecretInfo, _supportsSecret, _supportsKava, _supportsKavaInfo, and 2 more. [2322]

This is the type error we're seeing, which is why we're lying to TS that this is always a MetaMaskAdapter

Base automatically changed from feat_perf_lazy_3 to develop October 17, 2023 00:40
Copy link
Member

@0xApotheosis 0xApotheosis left a comment

Choose a reason for hiding this comment

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

Tested wallet connections to Native, MetaMask and WalletConnect as a sanity check, all seemed gucci.

Nice one.

src/context/WalletProvider/WalletProvider.tsx Outdated Show resolved Hide resolved
@gomesalexandre gomesalexandre enabled auto-merge (squash) October 18, 2023 19:01
@0xApotheosis 0xApotheosis disabled auto-merge October 18, 2023 22:43
@0xApotheosis 0xApotheosis merged commit c790afb into develop Oct 18, 2023
3 checks passed
@0xApotheosis 0xApotheosis deleted the feat_perf_lazy_4 branch October 18, 2023 22:43
@gomesalexandre gomesalexandre restored the feat_perf_lazy_4 branch October 19, 2023 11:53
@0xean 0xean deleted the feat_perf_lazy_4 branch December 6, 2023 15:35
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.

3 participants