Skip to content

Commit

Permalink
:fix update siweParams with AppKit chains (#2889)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp authored Sep 20, 2024
1 parent e2e39e0 commit dc7b895
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 53 deletions.
38 changes: 38 additions & 0 deletions .changeset/proud-cows-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
'@reown/appkit-adapter-wagmi': patch
'@apps/demo': patch
'@apps/gallery': patch
'@apps/laboratory': patch
'@examples/html-ethers': patch
'@examples/html-ethers5': patch
'@examples/html-wagmi': patch
'@examples/next-ethers': patch
'@examples/next-wagmi': patch
'@examples/react-ethers': patch
'@examples/react-ethers5': patch
'@examples/react-solana': patch
'@examples/react-wagmi': patch
'@examples/vue-ethers5': patch
'@examples/vue-solana': patch
'@examples/vue-wagmi': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-ethers': patch
'@reown/appkit-ethers5': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-solana': patch
'@reown/appkit-ui': patch
'@reown/appkit-wagmi': patch
'@reown/appkit-wallet': patch
---

Update siweParams with AppKit chains
4 changes: 2 additions & 2 deletions apps/laboratory/src/utils/SiweUtils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getCsrfToken, signIn, signOut, getSession } from 'next-auth/react'
import type { SIWEVerifyMessageArgs, SIWECreateMessageArgs, SIWESession } from '@reown/appkit-siwe'
import { createSIWEConfig, formatMessage } from '@reown/appkit-siwe'
import { arbitrum, base, binanceSmartChain, mainnet, polygon } from '@reown/appkit/networks'
import { arbitrum, mainnet, optimism, polygon, zkSync, sepolia } from '@reown/appkit/networks'

const chains = [mainnet, polygon, base, binanceSmartChain, arbitrum]
const chains = [mainnet, optimism, polygon, zkSync, arbitrum, sepolia]

export const siweConfig = createSIWEConfig({
signOutOnAccountChange: true,
Expand Down
102 changes: 51 additions & 51 deletions packages/adapters/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,68 +283,68 @@ export class WagmiAdapter implements ChainAdapter {
>

const siweParams = await this.options?.siweConfig?.getMessageParams?.()

const isSiweEnabled = this.options?.siweConfig?.options?.enabled
const isProviderSupported = typeof provider?.authenticate === 'function'
const isSiweParamsValid = siweParams && Object.keys(siweParams || {}).length > 0
const siweConfig = this.options?.siweConfig

if (isSiweEnabled && isProviderSupported && isSiweParamsValid) {
if (isSiweEnabled && isProviderSupported && isSiweParamsValid && siweConfig) {
// @ts-expect-error - setting requested chains beforehand avoids wagmi auto disconnecting the session when `connect` is called because it things chains are stale
await connector.setRequestedChainsIds(siweParams.chains)

const siweConfig = this.options?.siweConfig
const { SIWEController, getDidChainId, getDidAddress } = await import(
'@reown/appkit-siwe'
)

const params = await siweConfig?.getMessageParams?.()
const chains = this.caipNetworks
?.filter(network => network.chainNamespace === 'eip155')
.map(chain => chain.id) as string[]

if (siweConfig?.options?.enabled && params && Object.keys(params || {}).length > 0) {
const { SIWEController, getDidChainId, getDidAddress } = await import(
'@reown/appkit-siwe'
)
siweParams.chains = this.caipNetworks
?.filter(network => network.chainNamespace === 'eip155')
.map(chain => chain.chainId) as number[]

const chains = this.caipNetworks
?.filter(network => network.chainNamespace === 'eip155')
.map(chain => chain.id) as string[]
const result = await provider.authenticate({
nonce: await siweConfig.getNonce(),
methods: [...OPTIONAL_METHODS],
...siweParams,
chains
})
// Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
const signedCacao = result?.auths?.[0]

if (signedCacao) {
const { p, s } = signedCacao
const cacaoChainId = getDidChainId(p.iss)
const address = getDidAddress(p.iss)
if (address && cacaoChainId) {
SIWEController.setSession({
address,
chainId: parseInt(cacaoChainId, 10)
})
}

const result = await provider.authenticate({
nonce: await siweConfig.getNonce(),
methods: [...OPTIONAL_METHODS],
...params,
chains
})
// Auths is an array of signed CACAO objects https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-74.md
const signedCacao = result?.auths?.[0]

if (signedCacao) {
const { p, s } = signedCacao
const cacaoChainId = getDidChainId(p.iss)
const address = getDidAddress(p.iss)
if (address && cacaoChainId) {
SIWEController.setSession({
address,
chainId: parseInt(cacaoChainId, 10)
})
}

try {
// Kicks off verifyMessage and populates external states
const message = provider.client.formatAuthMessage({
request: p,
iss: p.iss
})

await SIWEController.verifyMessage({
message,
signature: s.s,
cacao: signedCacao
})
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error verifying message', error)
// eslint-disable-next-line no-console
await provider.disconnect().catch(console.error)
// eslint-disable-next-line no-console
await SIWEController.signOut().catch(console.error)
throw error
}
try {
// Kicks off verifyMessage and populates external states
const message = provider.client.formatAuthMessage({
request: p,
iss: p.iss
})

await SIWEController.verifyMessage({
message,
signature: s.s,
cacao: signedCacao
})
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error verifying message', error)
// eslint-disable-next-line no-console
await provider.disconnect().catch(console.error)
// eslint-disable-next-line no-console
await SIWEController.signOut().catch(console.error)
throw error
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ export class UniversalAdapterClient {
?.filter(network => network.chainNamespace === 'eip155')
.map(chain => chain.id) as string[]

siweParams.chains = this.caipNetworks
?.filter(network => network.chainNamespace === 'eip155')
.map(chain => chain.chainId) as number[]

const result = await WalletConnectProvider.authenticate({
nonce: await siweConfig?.getNonce?.(),
methods: [...OPTIONAL_METHODS],
Expand Down

0 comments on commit dc7b895

Please sign in to comment.