Skip to content

Commit

Permalink
chore(default-appkit-config): inject default appKit config
Browse files Browse the repository at this point in the history
  • Loading branch information
Cali93 committed Jun 19, 2024
1 parent 637c31b commit 15c37a9
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 39 deletions.
4 changes: 2 additions & 2 deletions apps/laboratory/src/pages/library/wagmi-siwe.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createWeb3Modal } from '@web3modal/wagmi/react'
import { appKitAuthConfig } from '@web3modal/siwe'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { WagmiProvider } from 'wagmi'
import { Web3ModalButtons } from '../../components/Web3ModalButtons'
Expand All @@ -9,6 +8,7 @@ import { getWagmiConfig } from '../../utils/WagmiConstants'
import { SiweData } from '../../components/Siwe/SiweData'
import { ConstantsUtil } from '../../utils/ConstantsUtil'
import { WagmiModalInfo } from '../../components/Wagmi/WagmiModalInfo'
import { siweConfig } from '../../utils/SiweUtils'

const queryClient = new QueryClient()

Expand All @@ -18,7 +18,7 @@ const modal = createWeb3Modal({
projectId: ConstantsUtil.ProjectId,
enableAnalytics: true,
metadata: ConstantsUtil.Metadata,
siweConfig: appKitAuthConfig,
siweConfig,
enableOnramp: true,
customWallets: ConstantsUtil.CustomWallets
})
Expand Down
19 changes: 4 additions & 15 deletions packages/core/src/controllers/ApiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,29 +228,18 @@ export const ApiController = {
ApiController.fetchFeaturedWallets(),
ApiController.fetchRecommendedWallets(),
ApiController.fetchNetworkImages(),
ApiController.fetchConnectorImages(),
ApiController.fetchProjectConfig()
ApiController.fetchConnectorImages()
]

state.prefetchPromise = Promise.race([Promise.allSettled(promises), CoreHelperUtil.wait(3000)])
},

async fetchProjectConfig() {
const { isAnalyticsEnabled, isAppKitAuthEnabled } = await api.get<ApiGetProjectConfigResponse>({
path: '/getProjectConfig',
const { isAnalyticsEnabled } = await api.get<ApiGetProjectConfigResponse>({
path: '/getAnalyticsConfig',
headers: ApiController._getApiHeaders()
})
// Only set the analytics state if it's not already set through the SDK config
if (OptionsController.state.enableAnalytics === undefined) {
OptionsController.setEnableAnalytics(isAnalyticsEnabled)
}

if (isAppKitAuthEnabled) {
const { SIWEController, appKitAuthConfig } = await import('@web3modal/siwe')

SIWEController.setSIWEClient(appKitAuthConfig)

OptionsController.setIsSiweEnabled(isAppKitAuthEnabled)
}
return { isAnalyticsEnabled, isAppKitAuthEnabled: true }
}
}
5 changes: 4 additions & 1 deletion packages/core/tests/controllers/ApiController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ describe('ApiController', () => {
.spyOn(ApiController, 'fetchConnectorImages')
.mockResolvedValue()

const fetchAnalyticsSpy = vi.spyOn(ApiController, 'fetchProjectConfig').mockResolvedValue()
const fetchAnalyticsSpy = vi.spyOn(ApiController, 'fetchProjectConfig').mockResolvedValue({
isAnalyticsEnabled: true,
isAppKitAuthEnabled: false
})

ApiController.prefetch()

Expand Down
12 changes: 6 additions & 6 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,12 @@ export class Web3Modal extends Web3ModalScaffold {
onUri(uri)
})

if (siweConfig?.options?.enabled) {
const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe')
const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe')
if (SIWEController.state._client) {
const result = await WalletConnectProvider.authenticate({
nonce: await siweConfig.getNonce(),
nonce: await SIWEController.getNonce(),
methods: OPTIONAL_METHODS,
...(await siweConfig.getMessageParams())
...(await SIWEController.state._client.getMessageParams())
})
// Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
const signedCacao = result?.auths?.[0]
Expand Down Expand Up @@ -313,8 +313,8 @@ export class Web3Modal extends Web3ModalScaffold {
const providerType = EthersStoreUtil.state.providerType
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
if (siweConfig?.options?.signOutOnDisconnect) {
const { SIWEController } = await import('@web3modal/siwe')
const { SIWEController } = await import('@web3modal/siwe')
if (SIWEController.state?._client?.options?.signOutOnDisconnect) {
await SIWEController.signOut()
}
if (
Expand Down
12 changes: 6 additions & 6 deletions packages/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ export class Web3Modal extends Web3ModalScaffold {
onUri(uri)
})

if (siweConfig?.options?.enabled) {
const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe')
const { SIWEController, getDidChainId, getDidAddress } = await import('@web3modal/siwe')
if (SIWEController.state._client) {
const result = await WalletConnectProvider.authenticate({
nonce: await siweConfig.getNonce(),
nonce: await SIWEController.getNonce(),
methods: OPTIONAL_METHODS,
...(await siweConfig.getMessageParams())
...(await SIWEController.state._client.getMessageParams())
})
// Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
const signedCacao = result?.auths?.[0]
Expand Down Expand Up @@ -286,8 +286,8 @@ export class Web3Modal extends Web3ModalScaffold {
const providerType = EthersStoreUtil.state.providerType
localStorage.removeItem(EthersConstantsUtil.WALLET_ID)
EthersStoreUtil.reset()
if (siweConfig?.options?.signOutOnDisconnect) {
const { SIWEController } = await import('@web3modal/siwe')
const { SIWEController } = await import('@web3modal/siwe')
if (SIWEController.state._client?.options?.signOutOnDisconnect) {
await SIWEController.signOut()
}
if (providerType === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) {
Expand Down
16 changes: 12 additions & 4 deletions packages/scaffold/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
ThemeController,
SnackController,
RouterController,
EnsController
EnsController,
ApiController
} from '@web3modal/core'
import { setColorTheme, setThemeVariables } from '@web3modal/ui'
import type { SIWEControllerClient } from '@web3modal/siwe'
Expand Down Expand Up @@ -309,10 +310,17 @@ export class Web3ModalScaffold {
NetworkController.setAllowUnsupportedChain(options.allowUnsupportedChain)
}

if (options.siweControllerClient) {
const { SIWEController } = await import('@web3modal/siwe')
const { isAppKitAuthEnabled, isAnalyticsEnabled } = await ApiController.fetchProjectConfig()

SIWEController.setSIWEClient(options.siweControllerClient)
// Only set the analytics state if it's not already set through the SDK config
if (options.enableAnalytics === undefined) {
OptionsController.setEnableAnalytics(isAnalyticsEnabled)
}

if (options.siweControllerClient || isAppKitAuthEnabled) {
const { SIWEController, appKitAuthConfig } = await import('@web3modal/siwe')
OptionsController.setIsSiweEnabled(true)
SIWEController.setSIWEClient(options.siweControllerClient ?? appKitAuthConfig)
}

ConnectionController.setClient(options.connectionControllerClient)
Expand Down
3 changes: 1 addition & 2 deletions packages/scaffold/src/partials/w3m-header/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export class W3mHeader extends LitElement {

// -- Private ------------------------------------------- //

// Temporarily added to test connecting with SIWE, replace with 'WhatIsAWallet' again when approved
private onWalletHelp() {
EventsController.sendEvent({ type: 'track', event: 'CLICK_WALLET_HELP' })
RouterController.push('WhatIsAWallet')
Expand All @@ -129,7 +128,7 @@ export class W3mHeader extends LitElement {
private async onClose() {
if (OptionsController.state.isSiweEnabled) {
const { SIWEController } = await import('@web3modal/siwe')
if (SIWEController.state.status !== 'success') {
if (SIWEController.state.status !== 'success' && this.isConnected) {
await ConnectionController.disconnect()
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/siwe/core/controller/SIWEController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const SIWEController = {
try {
const client = this._getClient()
const session = await client.getSession()
if (session) {
if (session?.address && session?.chainId) {
this.setSession(session)
this.setStatus('success')
}
Expand Down
4 changes: 2 additions & 2 deletions packages/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ export class Web3Modal extends Web3ModalScaffold {

disconnect: async () => {
await disconnect(this.wagmiConfig)
if (siweConfig?.options?.signOutOnDisconnect) {
const { SIWEController } = await import('@web3modal/siwe')
const { SIWEController } = await import('@web3modal/siwe')
if (SIWEController.state._client?.options?.signOutOnDisconnect) {
await SIWEController.signOut()
}
},
Expand Down

0 comments on commit 15c37a9

Please sign in to comment.