Skip to content

Commit

Permalink
fix wagmi solana auth
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp committed Sep 19, 2024
1 parent 9a128d5 commit e23c1f7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
5 changes: 4 additions & 1 deletion packages/adapters/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ export class SolanaAdapter implements ChainAdapter {
const isSolanaNetwork = val?.chainNamespace === this.chainNamespace

if (isSolanaAddress && isSolanaNetwork && caipAddress) {
this.syncAccount({ address: CoreHelperUtil.getPlainAddress(caipAddress), caipNetwork: val })
this.syncAccount({
address: CoreHelperUtil.getPlainAddress(caipAddress),
caipNetwork: val
})
}
})
AccountController.subscribeKey(
Expand Down
21 changes: 16 additions & 5 deletions packages/adapters/solana/src/providers/AuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import {
import { withSolanaNamespace } from '../utils/withSolanaNamespace.js'
import base58 from 'bs58'
import { isVersionedTransaction } from '@solana/wallet-adapter-base'
import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common'
import {
SafeLocalStorage,
SafeLocalStorageKeys,
type CaipNetwork,
type ChainNamespace
} from '@reown/appkit-common'
import { ChainController } from '@reown/appkit-core'

export type AuthProviderConfig = {
getProvider: () => W3mFrameProvider
Expand Down Expand Up @@ -213,7 +219,8 @@ export class AuthProvider extends ProviderEventEmitter implements Provider, Prov
}

private serializeTransaction(transaction: AnyTransaction) {
return base58.encode(transaction.serialize({ verifySignatures: false }))
const serializedTransaction = transaction.serialize({ verifySignatures: false })
return base58.encode(Buffer.from(serializedTransaction))
}

private bindEvents() {
Expand All @@ -231,10 +238,14 @@ export class AuthProvider extends ProviderEventEmitter implements Provider, Prov

this.getProvider().onIsConnected(response => {
this.setSession(response)
const activeNamespace = this.getActiveNamespace()

if (activeNamespace === 'solana') {
this.emit('connect', this.getPublicKey(true))
const storedCaipNetwork = SafeLocalStorage.getItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK)
if (storedCaipNetwork) {
const parsedCaipNetwork = JSON.parse(storedCaipNetwork) as CaipNetwork

if (parsedCaipNetwork.chainNamespace === 'solana') {
this.emit('connect', this.getPublicKey(true))
}
}
})

Expand Down
19 changes: 18 additions & 1 deletion packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,14 @@ export class WagmiAdapter implements ChainAdapter {
)
const chainId = Number(NetworkUtil.caipNetworkIdToNumber(caipNetwork?.id))

if (chainId && this.wagmiConfig) {
if (chainId && this.wagmiConfig && caipNetwork) {
const connectedConnector = SafeLocalStorage.getItem(
SafeLocalStorageKeys.CONNECTED_CONNECTOR
)
const isConnectedWithAuth = connectedConnector === 'AUTH'
if (isConnectedWithAuth) {
this.handleSwitchNetworkAuthConnector(caipNetwork)
}
await switchChain(this.wagmiConfig, { chainId })
}
},
Expand Down Expand Up @@ -787,6 +794,16 @@ export class WagmiAdapter implements ChainAdapter {
this.appKit?.setBalance(undefined, undefined, this.chainNamespace)
}

private async handleSwitchNetworkAuthConnector(caipNetwork: CaipNetwork) {
const connector = this.wagmiConfig.connectors.find(
c => c.id === ConstantsUtil.AUTH_CONNECTOR_ID
) as unknown as AdapterOptions<Config>['wagmiConfig']['connectors'][0]
const provider = (await connector?.getProvider()) as W3mFrameProvider
if (caipNetwork?.id) {
await provider.switchNetwork(caipNetwork?.id)
}
}

private async syncConnectedWalletInfo(connector: GetAccountReturnType['connector']) {
if (!connector) {
throw Error('syncConnectedWalletInfo - connector is undefined')
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export const ChainController = {
if (!state.noAdapters) {
state.activeChain = adapterToActivate?.chainNamespace
PublicStateController.set({ activeChain: adapterToActivate?.chainNamespace })
this.setActiveCaipNetwork(adapterToActivate?.defaultNetwork)
this.setActiveCaipNetwork(
adapterToActivate?.defaultNetwork || adapterToActivate?.caipNetworks[0]
)

adapters.forEach((adapter: ChainsInitializerAdapter) => {
state.chains.set(adapter.chainNamespace, {
Expand Down Expand Up @@ -193,6 +195,9 @@ export const ChainController = {
accountProps: Partial<AccountControllerState>,
replaceState = true
) {
if (accountProps.caipAddress) {
console.trace(chain, accountProps)
}
if (!chain) {
throw new Error('Chain is required to update chain account data')
}
Expand Down Expand Up @@ -262,6 +267,12 @@ export const ChainController = {

if (caipNetwork.chainNamespace !== state.activeChain) {
this.setActiveChain(caipNetwork.chainNamespace, caipNetwork)
state.activeCaipNetwork = caipNetwork
SafeLocalStorage.setItem(
SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK,
JSON.stringify(caipNetwork)
)
SafeLocalStorage.setItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK_ID, caipNetwork.id)

return
}
Expand Down
12 changes: 4 additions & 8 deletions packages/core/src/controllers/ConnectorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,14 @@ export const ConnectorController = {
},

getAuthConnector(): AuthConnector | undefined {
const activeNamespace = ChainController.state.activeChain
const authConnector = state.connectors.find(c => c.id === 'w3mAuth')
const activeNamespace = ChainController.state.activeCaipNetwork?.chainNamespace
const authConnector = state.connectors.find(
c => c.id === 'w3mAuth' && c.chain === activeNamespace
)
if (!authConnector) {
return undefined
}

if (authConnector.type === 'MULTI_CHAIN' && authConnector?.connectors?.length) {
return authConnector.connectors.find(c => c.chain === activeNamespace) as
| AuthConnector
| undefined
}

return authConnector as AuthConnector
},

Expand Down

0 comments on commit e23c1f7

Please sign in to comment.