Skip to content

Commit

Permalink
feat: walletprovider adapters type narrowing (#5468)
Browse files Browse the repository at this point in the history
* wip: walletprovider type narrowing

* feat: cast KKRestAdapter type

* feat: cast multichain adapter as MetaMaskAdapter

* feat: rm ts-ignore

* fix: infinite dispatch

* feat: rm useless ts-ignore

* wip

* feat: gm, she compiles again

* feat: cleanup

---------

Co-authored-by: woodenfurniture <[email protected]>
Co-authored-by: Apotheosis <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2023
1 parent b7c41f5 commit c790afb
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 275 deletions.
5 changes: 4 additions & 1 deletion src/context/WalletProvider/Coinbase/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { CoinbaseAdapter } from '@shapeshiftoss/hdwallet-coinbase'
import { CoinbaseIcon } from 'components/Icons/CoinbaseIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const CoinbaseConfig: Omit<SupportedWalletInfo, 'routes'> = {
type CoinbaseConfigType = Omit<SupportedWalletInfo<typeof CoinbaseAdapter>, 'routes'>

export const CoinbaseConfig: CoinbaseConfigType = {
adapters: [
{
loadAdapter: () => import('@shapeshiftoss/hdwallet-coinbase').then(m => m.CoinbaseAdapter),
Expand Down
5 changes: 4 additions & 1 deletion src/context/WalletProvider/DemoWallet/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { NativeAdapter } from '@shapeshiftoss/hdwallet-native'
import { FoxIcon } from 'components/Icons/FoxIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const DemoConfig: Omit<SupportedWalletInfo, 'routes'> = {
type DemoConfigType = Omit<SupportedWalletInfo<typeof NativeAdapter>, 'routes'>

export const DemoConfig: DemoConfigType = {
adapters: [
{
loadAdapter: () => import('@shapeshiftoss/hdwallet-native').then(m => m.NativeAdapter),
Expand Down
7 changes: 5 additions & 2 deletions src/context/WalletProvider/KeepKey/components/Connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ModalBody,
ModalHeader,
} from '@chakra-ui/react'
import type { KkRestAdapter } from '@keepkey/hdwallet-keepkey-rest'
import type { Event } from '@shapeshiftoss/hdwallet-core'
import { useCallback, useState } from 'react'
import { CircularProgress } from 'components/CircularProgress/CircularProgress'
Expand Down Expand Up @@ -54,11 +55,12 @@ export const KeepKeyConnect = () => {
setError(null)
setLoading(true)

const firstAdapter = await getAdapter(KeyManager.KeepKey)
const firstAdapter = (await getAdapter(KeyManager.KeepKey)) as KkRestAdapter | null
if (firstAdapter) {
const wallet = await (async () => {
try {
const sdk = await setupKeepKeySDK()
if (!sdk) throw new Error('Failed to setup KeepKey SDK')
const wallet = await firstAdapter.pairDevice(sdk)
if (!wallet) {
setErrorLoading('walletProvider.errors.walletNotFound')
Expand All @@ -67,7 +69,8 @@ export const KeepKeyConnect = () => {
return wallet
} catch (e) {
const secondAdapter = await getAdapter(KeyManager.KeepKey, 1)
const wallet = await secondAdapter?.pairDevice().catch((err: Error) => {
// @ts-ignore TODO(gomes): FIXME, most likely borked because of WebUSBKeepKeyAdapter
const wallet = await secondAdapter.pairDevice().catch(err => {
if (err.name === 'ConflictingApp') {
setErrorLoading('walletProvider.keepKey.connect.conflictingApp')
return
Expand Down
9 changes: 8 additions & 1 deletion src/context/WalletProvider/KeepKey/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { KkRestAdapter } from '@keepkey/hdwallet-keepkey-rest'
import type { WebUSBKeepKeyAdapter } from '@shapeshiftoss/hdwallet-keepkey-webusb'
import { KeepKeyIcon } from 'components/Icons/KeepKeyIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const KeepKeyConfig: Omit<SupportedWalletInfo, 'routes'> = {
type KeepKeyConfigType = Omit<
SupportedWalletInfo<typeof KkRestAdapter | typeof WebUSBKeepKeyAdapter>,
'routes'
>

export const KeepKeyConfig: KeepKeyConfigType = {
adapters: [
{
loadAdapter: () => import('@keepkey/hdwallet-keepkey-rest').then(m => m.KkRestAdapter),
Expand Down
5 changes: 4 additions & 1 deletion src/context/WalletProvider/Keplr/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { KeplrAdapter } from '@shapeshiftoss/hdwallet-keplr'
import { KeplrIcon } from 'components/Icons/KeplrIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const KeplrConfig: Omit<SupportedWalletInfo, 'routes'> = {
type KeplrConfigType = Omit<SupportedWalletInfo<typeof KeplrAdapter>, 'routes'>

export const KeplrConfig: KeplrConfigType = {
adapters: [
{
loadAdapter: () => import('@shapeshiftoss/hdwallet-keplr').then(m => m.KeplrAdapter),
Expand Down
5 changes: 4 additions & 1 deletion src/context/WalletProvider/Ledger/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { WebUSBLedgerAdapter } from '@shapeshiftoss/hdwallet-ledger-webusb'
import { LedgerIcon } from 'components/Icons/LedgerIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const LedgerConfig: Omit<SupportedWalletInfo, 'routes'> = {
type LedgerConfigType = Omit<SupportedWalletInfo<typeof WebUSBLedgerAdapter>, 'routes'>

export const LedgerConfig: LedgerConfigType = {
adapters: [
{
loadAdapter: () =>
Expand Down
9 changes: 7 additions & 2 deletions src/context/WalletProvider/MetaMask/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type { MetaMaskAdapter } from '@shapeshiftoss/hdwallet-metamask'
import { getConfig } from 'config'
import { MetaMaskIcon } from 'components/Icons/MetaMaskIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const MetaMaskConfig: Omit<SupportedWalletInfo, 'routes'> = {
type MetaMaskConfigType = Omit<SupportedWalletInfo<typeof MetaMaskAdapter>, 'routes'>

export const MetaMaskConfig: MetaMaskConfigType = {
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,
)
: import('@shapeshiftoss/hdwallet-metamask').then(m => m.MetaMaskAdapter),
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ export const MobileLoad = ({ history }: RouteComponentProps) => {
try {
const revoker = await getWallet(deviceId)
if (!revoker?.mnemonic) throw new Error(`Mobile wallet not found: ${deviceId}`)
if (!revoker?.id) throw new Error(`Revoker ID not found: ${deviceId}`)

const wallet = await adapter.pairDevice(revoker.id)
await wallet.loadDevice({ mnemonic: revoker.mnemonic })
if (!(await wallet.isInitialized())) {
await wallet.initialize()
await wallet?.loadDevice({ mnemonic: revoker.mnemonic })
if (!(await wallet?.isInitialized())) {
await wallet?.initialize()
}
dispatch({
type: WalletActions.SET_WALLET,
Expand Down
5 changes: 4 additions & 1 deletion src/context/WalletProvider/MobileWallet/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { NativeAdapter } from '@shapeshiftoss/hdwallet-native'
import { FoxIcon } from 'components/Icons/FoxIcon'
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const MobileConfig: Omit<SupportedWalletInfo, 'routes'> = {
type MobileConfigType = Omit<SupportedWalletInfo<typeof NativeAdapter>, 'routes'>

export const MobileConfig: MobileConfigType = {
adapters: [
{
loadAdapter: () => import('@shapeshiftoss/hdwallet-native').then(m => m.NativeAdapter),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export const NativeLoad = ({ history }: RouteComponentProps) => {
const { name, icon } = NativeConfig
try {
const wallet = await adapter.pairDevice(deviceId)
if (!(await wallet.isInitialized())) {
if (!(await wallet?.isInitialized())) {
// This will trigger the password modal and the modal will set the wallet on state
// after the wallet has been decrypted. If we set it now, `getPublicKeys` calls will
// return null, and we don't have a retry mechanism
await wallet.initialize()
await wallet?.initialize()
} else {
dispatch({
type: WalletActions.SET_WALLET,
Expand Down
5 changes: 4 additions & 1 deletion src/context/WalletProvider/NativeWallet/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { NativeAdapter } from '@shapeshiftoss/hdwallet-native'
import { FoxIcon } from 'components/Icons/FoxIcon' // Ensure the import path is correct
import type { SupportedWalletInfo } from 'context/WalletProvider/config'

export const NativeConfig: Omit<SupportedWalletInfo, 'routes'> = {
type NativeConfigType = Omit<SupportedWalletInfo<typeof NativeAdapter>, 'routes'>

export const NativeConfig: NativeConfigType = {
adapters: [
{
loadAdapter: () => import('@shapeshiftoss/hdwallet-native').then(m => m.NativeAdapter),
Expand Down
5 changes: 4 additions & 1 deletion src/context/WalletProvider/WalletConnectV2/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CHAIN_REFERENCE } from '@shapeshiftoss/caip'
import type { WalletConnectV2Adapter } from '@shapeshiftoss/hdwallet-walletconnectv2'
import { getConfig } from 'config'
import type { Chain } from 'viem/chains'
import { arbitrum, avalanche, bsc, gnosis, mainnet, optimism, polygon } from 'viem/chains'
Expand All @@ -7,7 +8,9 @@ import type { SupportedWalletInfo } from 'context/WalletProvider/config'

import type { EthereumProviderOptions } from './constants'

export const WalletConnectV2Config: Omit<SupportedWalletInfo, 'routes'> = {
type WalletConnectV2ConfigType = Omit<SupportedWalletInfo<typeof WalletConnectV2Adapter>, 'routes'>

export const WalletConnectV2Config: WalletConnectV2ConfigType = {
adapters: [
{
loadAdapter: () =>
Expand Down
3 changes: 2 additions & 1 deletion src/context/WalletProvider/WalletContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { createContext } from 'react'

import type { ActionTypes } from './actions'
import type { KeyManager } from './KeyManager'
import type { GetAdapter } from './types'
import type { DeviceState, InitialState, KeyManagerWithProvider } from './WalletProvider'

export interface IWalletContext {
state: InitialState
getAdapter: (keyManager: KeyManager, index?: number) => Promise<any>
getAdapter: GetAdapter
dispatch: React.Dispatch<ActionTypes>
connect: (adapter: KeyManager) => void
create: (adapter: KeyManager) => void
Expand Down
Loading

0 comments on commit c790afb

Please sign in to comment.