Skip to content

Commit

Permalink
:fix wc deeplink issue & missing metadata (#3239)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp authored Nov 14, 2024
1 parent bd840ef commit 0cc2b86
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
52 changes: 47 additions & 5 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,12 @@ export class AppKit {
}
) {
this.caipNetworks = this.extendCaipNetworks(options)

this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options)
await this.initControllers(options)
this.createAuthProvider()
await this.createUniversalProvider()
this.createClients()
ChainController.initialize(options.adapters ?? [])
this.defaultCaipNetwork = this.extendDefaultCaipNetwork(options)
this.initControllers(options)
this.chainAdapters = await this.createAdapters(
options.adapters as unknown as AdapterBlueprint[]
)
Expand Down Expand Up @@ -805,11 +804,11 @@ export class AppKit {
})

if (res) {
await this.syncAccount({
this.syncProvider({
...res,
chainNamespace: chain || (ChainController.state.activeChain as ChainNamespace)
})
this.syncProvider({
await this.syncAccount({
...res,
chainNamespace: chain || (ChainController.state.activeChain as ChainNamespace)
})
Expand Down Expand Up @@ -1388,6 +1387,7 @@ export class AppKit {
this.setCaipNetwork(this.caipNetworks?.find(n => n.chainNamespace === chainNamespace))
}

this.syncConnectedWalletInfo(chainNamespace)
const adapter = this.getAdapter(chainNamespace)

const balance = await adapter?.getBalance({
Expand All @@ -1403,6 +1403,48 @@ export class AppKit {
}
}

private syncConnectedWalletInfo(chainNamespace: ChainNamespace) {
const currentActiveWallet = SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR)
const providerType = ProviderUtil.state.providerIds[chainNamespace]

if (
providerType === UtilConstantsUtil.CONNECTOR_TYPE_ANNOUNCED ||
providerType === UtilConstantsUtil.CONNECTOR_TYPE_INJECTED
) {
if (currentActiveWallet) {
const connector = this.getConnectors().find(c => c.id === currentActiveWallet)

if (connector?.info) {
this.setConnectedWalletInfo({ ...connector.info }, chainNamespace)
}
}
} else if (providerType === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT) {
const provider = ProviderUtil.getProvider(chainNamespace)

if (provider?.session) {
this.setConnectedWalletInfo(
{
...provider.session.peer.metadata,
name: provider.session.peer.metadata.name,
icon: provider.session.peer.metadata.icons?.[0]
},
chainNamespace
)
}
} else if (providerType === UtilConstantsUtil.COINBASE_CONNECTOR_ID) {
const connector = this.getConnectors().find(
c => c.id === UtilConstantsUtil.COINBASE_CONNECTOR_ID
)

this.setConnectedWalletInfo(
{ name: 'Coinbase Wallet', icon: this.getConnectorImage(connector) },
chainNamespace
)
} else if (currentActiveWallet) {
this.setConnectedWalletInfo({ name: currentActiveWallet }, chainNamespace)
}
}

private async syncIdentity({
address,
chainId,
Expand Down
40 changes: 39 additions & 1 deletion packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
ChainController,
type Connector
} from '@reown/appkit-core'
import type { CaipNetwork } from '@reown/appkit-common'
import {
SafeLocalStorage,
SafeLocalStorageKeys,
type CaipNetwork,
type SafeLocalStorageItems
} from '@reown/appkit-common'
import { mockOptions } from './mocks/Options'

// Mock all controllers and UniversalAdapterClient
Expand Down Expand Up @@ -465,5 +470,38 @@ describe('Base', () => {

expect(ChainController.switchActiveNetwork).toHaveBeenCalledTimes(1)
})

it('should set connected wallet info when syncing account', async () => {
// Mock the connector data
const mockConnector = {
id: 'test-wallet'
} as Connector

vi.mocked(ConnectorController.getConnectors).mockReturnValue([mockConnector])

const mockAccountData = {
address: '0x123',
chainId: '1',
chainNamespace: 'eip155' as const
}

vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation(
(key: keyof SafeLocalStorageItems) => {
if (key === SafeLocalStorageKeys.CONNECTED_CONNECTOR) {
return mockConnector.id
}
return undefined
}
)

await appKit['syncAccount'](mockAccountData)

expect(AccountController.setConnectedWalletInfo).toHaveBeenCalledWith(
expect.objectContaining({
name: mockConnector.id
}),
'eip155'
)
})
})
})
6 changes: 2 additions & 4 deletions packages/common/src/utils/SafeLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export type SafeLocalStorageItems = {
'@appkit/connected_social': string
'@appkit/connected_social_username': string
'@appkit/recent_wallets': string
'@appkit/deeplink_choice': string
'@appkit/connected_namespace': string
/*
* DO NOT CHANGE: @walletconnect/universal-provider requires us to set this specific key
Expand All @@ -27,9 +26,8 @@ export const SafeLocalStorageKeys = {
CONNECTED_SOCIAL: '@appkit/connected_social',
CONNECTED_SOCIAL_USERNAME: '@appkit/connected_social_username',
RECENT_WALLETS: '@appkit/recent_wallets',
DEEPLINK_CHOICE: '@appkit/deeplink_choice',
CONNECTED_NAMESPACE: '@appkit/connected_namespace',
WALLETCONNECT_DEEPLINK_CHOICE: 'WALLETCONNECT_DEEPLINK_CHOICE'
DEEPLINK_CHOICE: 'WALLETCONNECT_DEEPLINK_CHOICE',
CONNECTED_NAMESPACE: '@appkit/connected_namespace'
} as const

export const SafeLocalStorage = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/utils/StorageUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('StorageUtil', () => {
it('should set WalletConnect deep link in localStorage', () => {
const deepLink = { href: 'https://example.com', name: 'Example Wallet' }
StorageUtil.setWalletConnectDeepLink(deepLink)
expect(SafeLocalStorageKeys.DEEPLINK_CHOICE).toBe('WALLETCONNECT_DEEPLINK_CHOICE')
const savedDL = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE)
expect(savedDL).toBe(JSON.stringify({ href: deepLink.href, name: deepLink.name }))
})
Expand Down

0 comments on commit 0cc2b86

Please sign in to comment.