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

fix: adapter connection issue #3489

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ export class AppKit {
}
})
provider.onNotConnected(() => {
const connectedConnector = StorageUtil.getConnectedConnector()
const connectedConnector = StorageUtil.getConnectedConnectorId()
const isConnectedWithAuth = connectedConnector === UtilConstantsUtil.AUTH_CONNECTOR_ID
if (!isConnected && isConnectedWithAuth) {
this.setCaipAddress(undefined, ChainController.state.activeChain as ChainNamespace)
Expand Down Expand Up @@ -1451,9 +1451,7 @@ export class AppKit {
ProviderUtil.setProvider(chainNamespace, this.universalProvider)
}

StorageUtil.setConnectedConnector(
UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT as ConnectorType
)
StorageUtil.setConnectedConnectorId(UtilConstantsUtil.WALLET_CONNECT_CONNECTOR_ID)

let address = ''

Expand Down Expand Up @@ -1538,7 +1536,7 @@ export class AppKit {
ProviderUtil.setProviderId(chainNamespace, type)
ProviderUtil.setProvider(chainNamespace, provider)

StorageUtil.setConnectedConnector(id as ConnectorType)
StorageUtil.setConnectedConnectorId(id)
}

private async syncAccount({
Expand Down Expand Up @@ -1597,7 +1595,7 @@ export class AppKit {
}

private syncConnectedWalletInfo(chainNamespace: ChainNamespace) {
const currentActiveWallet = StorageUtil.getConnectedConnector()
const currentActiveWallet = StorageUtil.getConnectedConnectorId()
const providerType = ProviderUtil.state.providerIds[chainNamespace]

if (
Expand Down Expand Up @@ -1714,10 +1712,10 @@ export class AppKit {
}

private async syncExistingConnection() {
const connectedConnector = StorageUtil.getConnectedConnector() as ConnectorType
const connectedConnector = StorageUtil.getConnectedConnectorId() as ConnectorType
const activeNamespace = StorageUtil.getActiveNamespace()

if (connectedConnector === UtilConstantsUtil.CONNECTOR_TYPE_WALLET_CONNECT && activeNamespace) {
if (connectedConnector === UtilConstantsUtil.WALLET_CONNECT_CONNECTOR_ID && activeNamespace) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about an object like UtilConstantsUtil.CONNECTOR_ID.WALLET_CONNECT?

this.syncWalletConnectAccount()
} else if (
connectedConnector &&
Expand Down
4 changes: 0 additions & 4 deletions packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,6 @@ describe('Base', () => {

vi.spyOn(appKit as any, 'getAdapter').mockReturnValue(mockAdapter)

vi.spyOn(StorageUtil, 'setConnectedConnector').mockImplementation(vi.fn())

vi.spyOn(appKit as any, 'setUnsupportedNetwork').mockImplementation(vi.fn())

vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation((key: string) => {
Expand Down Expand Up @@ -675,8 +673,6 @@ describe('Base', () => {

vi.spyOn(appKit as any, 'getAdapter').mockReturnValue(mockAdapter)

vi.spyOn(StorageUtil, 'setConnectedConnector').mockImplementation(vi.fn())

vi.spyOn(appKit as any, 'setUnsupportedNetwork').mockImplementation(vi.fn())

vi.spyOn(SafeLocalStorage, 'getItem').mockImplementation((key: string) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/utils/SafeLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type SafeLocalStorageItems = {
'@appkit/solana_caip_chain': string
'@appkit/active_caip_network_id': string
'@appkit/connected_connector': string
'@appkit/connected_connector_id': string
'@appkit/connected_social': string
'@appkit/connected_social_username': string
'@appkit/recent_wallets': string
Expand All @@ -24,6 +25,7 @@ export const SafeLocalStorageKeys = {
SOLANA_CAIP_CHAIN: '@appkit/solana_caip_chain',
ACTIVE_CAIP_NETWORK_ID: '@appkit/active_caip_network_id',
CONNECTED_CONNECTOR: '@appkit/connected_connector',
CONNECTED_CONNECTOR_ID: '@appkit/connected_connector_id',
CONNECTED_SOCIAL: '@appkit/connected_social',
CONNECTED_SOCIAL_USERNAME: '@appkit/connected_social_username',
RECENT_WALLETS: '@appkit/recent_wallets',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/controllers/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const ConnectionController = {
},

async connectWalletConnect() {
StorageUtil.setConnectedConnector('WALLET_CONNECT')
StorageUtil.setConnectedConnectorId('walletConnect')

if (CoreHelperUtil.isTelegram()) {
if (wcConnectionPromise) {
Expand Down Expand Up @@ -147,7 +147,7 @@ export const ConnectionController = {

async reconnectExternal(options: ConnectExternalOptions) {
await this._getClient()?.reconnectExternal?.(options)
StorageUtil.setConnectedConnector(options.type === 'AUTH' ? 'ID_AUTH' : options.type)
StorageUtil.setConnectedConnectorId(options.id)
},

async setPreferredAccountType(accountType: W3mFrameTypes.AccountType) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/controllers/SwapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export const SwapController = {
const caipAddress = ChainController.state.activeCaipAddress
const address = CoreHelperUtil.getPlainAddress(caipAddress)
const networkAddress = ChainController.getActiveNetworkTokenAddress()
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()

if (!address) {
throw new Error('No address found to swap the tokens from.')
Expand Down Expand Up @@ -202,7 +202,7 @@ export const SwapController = {
invalidSourceTokenAmount,
availableToSwap:
caipAddress && !invalidToToken && !invalidSourceToken && !invalidSourceTokenAmount,
isAuthConnector: type === 'ID_AUTH'
isAuthConnector: connector === 'ID_AUTH'
}
},

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/SIWXUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const SIWXUtil = {

const message = siwxMessage.toString()

if (StorageUtil.getConnectedConnector() === 'ID_AUTH') {
if (StorageUtil.getConnectedConnectorId() === 'ID_AUTH') {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about using constant here instead of hard coded?

RouterController.pushTransactionStack({
view: null,
goBack: false,
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/utils/StorageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ export const StorageUtil = {
return []
},

setConnectedConnector(connectorType: ConnectorType) {
setConnectedConnectorId(connectorId: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we should probably remove setConnectedConnector

try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connectorType)
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR_ID, connectorId)
} catch {
console.info('Unable to set Connected Connector')
console.info('Unable to set Connected Connector Id')
}
},

Expand All @@ -153,6 +153,16 @@ export const StorageUtil = {
return undefined
},

getConnectedConnectorId() {
try {
return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR_ID) as ConnectorType
} catch {
console.info('Unable to get connected connector id')
}

return undefined
},

setConnectedSocialProvider(socialProvider: SocialProvider) {
try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, socialProvider)
Expand Down
10 changes: 1 addition & 9 deletions packages/core/tests/controllers/ConnectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@ import type {
ConnectionControllerClient,
ConnectorType
} from '../../exports/index.js'
import {
ChainController,
ConnectionController,
ConstantsUtil,
StorageUtil
} from '../../exports/index.js'
import { ChainController, ConnectionController, ConstantsUtil } from '../../exports/index.js'
import { ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common'

// -- Setup --------------------------------------------------------------------
const chain = CommonConstantsUtil.CHAIN.EVM
const walletConnectUri = 'wc://uri?=123'
const externalId = 'coinbaseWallet'
const type = 'WALLET_CONNECT' as ConnectorType
const storageSpy = vi.spyOn(StorageUtil, 'setConnectedConnector')

const client: ConnectionControllerClient = {
connectWalletConnect: async onUri => {
Expand Down Expand Up @@ -109,7 +103,6 @@ describe('ConnectionController', () => {
await ConnectionController.connectWalletConnect()
expect(ConnectionController.state.wcUri).toEqual(walletConnectUri)
expect(ConnectionController.state.wcPairingExpiry).toEqual(ConstantsUtil.FOUR_MINUTES_MS)
expect(storageSpy).toHaveBeenCalledWith('WALLET_CONNECT')
expect(clientConnectWalletConnectSpy).toHaveBeenCalled()

// Just in case
Expand All @@ -119,7 +112,6 @@ describe('ConnectionController', () => {
it('connectExternal() should trigger internal client call and set connector in storage', async () => {
const options = { id: externalId, type }
await ConnectionController.connectExternal(options, chain)
expect(storageSpy).toHaveBeenCalledWith(type)
expect(clientConnectExternalSpy).toHaveBeenCalledWith(options)
})

Expand Down
8 changes: 0 additions & 8 deletions packages/core/tests/utils/StorageUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,6 @@ describe('StorageUtil', () => {
})
})

describe('setConnectedConnector', () => {
it('should set connected connector', () => {
const connector: ConnectorType = 'INJECTED'
StorageUtil.setConnectedConnector(connector)
expect(SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR)).toBe(connector)
})
})

describe('getConnectedConnector', () => {
it('should get connected connector', () => {
const connector: ConnectorType = 'INJECTED'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class W3mAccountAuthButton extends LitElement {

// -- Render -------------------------------------------- //
public override render() {
const type = StorageUtil.getConnectedConnector()
const type = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()

if (!authConnector || type !== 'ID_AUTH') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,14 @@ export class W3mAccountDefaultWidget extends LitElement {
}

private authCardTemplate() {
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()
const { origin } = location
if (!authConnector || type !== 'ID_AUTH' || origin.includes(CommonConstantsUtil.SECURE_SITE)) {
if (
!authConnector ||
connectorId !== 'ID_AUTH' ||
origin.includes(CommonConstantsUtil.SECURE_SITE)
) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ export class W3mAccountSettingsView extends LitElement {

// -- Private ------------------------------------------- //
private chooseNameButtonTemplate() {
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()
const hasNetworkSupport = ChainController.checkIfNamesSupported()
if (!hasNetworkSupport || !authConnector || type !== 'ID_AUTH' || this.profileName) {
if (!hasNetworkSupport || !authConnector || connectorId !== 'ID_AUTH' || this.profileName) {
return null
}

Expand All @@ -175,10 +175,10 @@ export class W3mAccountSettingsView extends LitElement {
}

private authCardTemplate() {
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()
const { origin } = location
if (!authConnector || type !== 'ID_AUTH' || origin.includes(ConstantsUtil.SECURE_SITE)) {
if (!authConnector || connectorId !== 'ID_AUTH' || origin.includes(ConstantsUtil.SECURE_SITE)) {
return null
}

Expand Down Expand Up @@ -214,10 +214,10 @@ export class W3mAccountSettingsView extends LitElement {

private togglePreferredAccountBtnTemplate() {
const networkEnabled = ChainController.checkIfSmartAccountEnabled()
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()

if (!authConnector || type !== 'ID_AUTH' || !networkEnabled) {
if (!authConnector || connectorId !== 'ID_AUTH' || !networkEnabled) {
return null
}

Expand Down
4 changes: 2 additions & 2 deletions packages/scaffold-ui/src/views/w3m-account-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ export class W3mAccountView extends LitElement {
// -- Render -------------------------------------------- //

public override render() {
const connectedConnectorType = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()

return html`
${authConnector && connectedConnectorType === 'ID_AUTH'
${authConnector && connectorId === 'ID_AUTH'
? this.walletFeaturesTemplate()
: this.defaultTemplate()}
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export class W3mNetworkSwitchView extends LitElement {

// -- Private ------------------------------------------- //
private getSubLabel() {
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()
if (authConnector && type === 'AUTH') {
if (authConnector && connectorId === 'AUTH') {
return ''
}

Expand All @@ -107,9 +107,9 @@ export class W3mNetworkSwitchView extends LitElement {
}

private getLabel() {
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()
if (authConnector && type === 'AUTH') {
if (authConnector && connectorId === 'ID_AUTH') {
return `Switching to ${this.network?.name ?? 'Unknown'} network...`
}

Expand Down
6 changes: 3 additions & 3 deletions packages/scaffold-ui/src/views/w3m-networks-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export class W3mNetworksView extends LitElement {
const approvedCaipNetworkIds = ChainController.getAllApprovedCaipNetworkIds()
const supportsAllNetworks =
ChainController.getNetworkProp('supportsAllNetworks', networkNamespace) !== false
const type = StorageUtil.getConnectedConnector()
const connectorId = StorageUtil.getConnectedConnectorId()
const authConnector = ConnectorController.getAuthConnector()
const isConnectedWithAuth = type === 'ID_AUTH' && authConnector
const isConnectedWithAuth = connectorId === 'ID_AUTH' && authConnector

if (!isNamespaceConnected || supportsAllNetworks || isConnectedWithAuth) {
return false
Expand All @@ -160,7 +160,7 @@ export class W3mNetworksView extends LitElement {
network.chainNamespace
)
const isCurrentNetworkConnected = AccountController.state.caipAddress
const isAuthConnected = StorageUtil.getConnectedConnector() === 'ID_AUTH'
const isAuthConnected = StorageUtil.getConnectedConnectorId() === 'ID_AUTH'

if (
isDifferentNamespace &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export class W3mSwitchAddressView extends LitElement {

public readonly currentAddress: string = AccountController.state.address || ''

private connectedConnector = StorageUtil.getConnectedConnector()
private connectedConnector = StorageUtil.getConnectedConnectorId()

// Only show icon for AUTH accounts
private shouldShowIcon = this.connectedConnector === 'AUTH'
private shouldShowIcon = this.connectedConnector === 'ID_AUTH'

private caipNetwork = ChainController.state.activeCaipNetwork

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/composites/wui-list-account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class WuiListAccount extends LitElement {

@property() public accountType = ''

private connectedConnector = StorageUtil.getConnectedConnector()
private connectedConnector = StorageUtil.getConnectedConnectorId()

private labels = AccountController.state.addressLabels

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/composites/wui-profile-button-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export class WuiProfileButtonV2 extends LitElement {

@property() public onCopyClick?: (event: Event) => void

private connectedConnector = StorageUtil.getConnectedConnector()
private connectedConnector = StorageUtil.getConnectedConnectorId()

private shouldShowIcon = this.connectedConnector === 'AUTH'
private shouldShowIcon = this.connectedConnector === 'ID_AUTH'

// -- Render -------------------------------------------- //
public override render() {
Expand Down
Loading