-
Notifications
You must be signed in to change notification settings - Fork 191
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
Conversation
Current dependencies on/for this PR:
This comment was auto-generated by Graphite. |
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' |
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.
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 |
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.
Most likely the root cause of all the ts-ignore across this PR
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.
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
@@ -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 |
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.
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
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.
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 |
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.
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
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.
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 |
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.
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 |
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.
Not happy with this, but probably our best bet a0622d8
@@ -900,7 +900,7 @@ export const WalletProvider = ({ children }: { children: React.ReactNode }): JSX | |||
}) | |||
}, []) | |||
|
|||
useEffect(() => load(), [load, state.adapters, state.keyring]) | |||
useEffect(() => load(), [load, state.keyring]) |
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 don't want to run this reactively on state.adapters
anymore after this stack/PR refactor:
- This stack's refactor makes it so that we
getAdapter()
so we don't need to be reactive onstate.adapters
anymore - 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
inload()
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, |
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.
is this cast still required?
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.
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
2a6e615
to
7094513
Compare
bfdb922
to
764ea6b
Compare
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.
Tested wallet connections to Native, MetaMask and WalletConnect as a sanity check, all seemed gucci.
Nice one.
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
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)