Skip to content

Commit

Permalink
Merge branch 'main' into chore/terms-checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz authored Nov 7, 2024
2 parents da4ff26 + ffb30d3 commit 6f6b3bb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 35 deletions.
24 changes: 24 additions & 0 deletions .changeset/brave-countries-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'@reown/appkit-adapter-solana': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit': patch
'@reown/appkit-core': patch
'@reown/appkit-siwx': patch
'@apps/demo': patch
'@apps/gallery': patch
'@apps/laboratory': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-siwe': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

Fixes for improving SIWX initialization and flows
3 changes: 2 additions & 1 deletion packages/appkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@
"@reown/appkit-utils": "workspace:*",
"@reown/appkit-wallet": "workspace:*",
"@walletconnect/types": "2.17.0",
"@walletconnect/utils": "2.17.0",
"@walletconnect/universal-provider": "2.17.0",
"@walletconnect/utils": "2.17.0",
"bs58": "6.0.0",
"valtio": "1.11.2",
"viem": "2.x"
},
Expand Down
34 changes: 29 additions & 5 deletions packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '@reown/appkit-common'
import { ProviderUtil } from '../store/index.js'
import type { AppKitOptions, AppKitOptionsWithCaipNetworks } from '../utils/TypesUtil.js'
import bs58 from 'bs58'

type Metadata = {
name: string
Expand Down Expand Up @@ -246,12 +247,35 @@ export class UniversalAdapterClient {
throw new Error('connectionControllerClient:signMessage - provider is undefined')
}

const signature = await provider.request({
method: 'personal_sign',
params: [message, address]
})
let signature = ''

if (
ChainController.state.activeCaipNetwork?.chainNamespace ===
CommonConstantsUtil.CHAIN.SOLANA
) {
const response = await provider.request(
{
method: 'solana_signMessage',
params: {
message: bs58.encode(new TextEncoder().encode(message)),
pubkey: address
}
},
ChainController.state.activeCaipNetwork?.caipNetworkId
)

signature = (response as { signature: string }).signature
} else {
signature = await provider.request(
{
method: 'personal_sign',
params: [message, address]
},
ChainController.state.activeCaipNetwork?.caipNetworkId
)
}

return signature as string
return signature
},

estimateGas: async () => await Promise.resolve(BigInt(0)),
Expand Down
54 changes: 25 additions & 29 deletions packages/core/src/controllers/ConnectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,15 @@ export const ConnectionController = {
}
)
}

await this.initializeSWIXIfAvailable()
},

async connectExternal(options: ConnectExternalOptions, chain: ChainNamespace, setChain = true) {
await this._getClient(chain).connectExternal?.(options)

if (setChain) {
ChainController.setActiveNamespace(chain)
StorageUtil.setConnectedConnector(options.type)
}

await this.initializeSWIXIfAvailable()
},

async reconnectExternal(options: ConnectExternalOptions) {
Expand Down Expand Up @@ -250,19 +247,19 @@ export const ConnectionController = {
},

async disconnect() {
const connectionControllerClient = this._getClient()
try {
const connectionControllerClient = this._getClient()

const siwx = OptionsController.state.siwx
if (siwx) {
const activeCaipNetwork = ChainController.getActiveCaipNetwork()
const address = ChainController.getActiveCaipAddress()?.split(':')[2] || ''
const siwx = OptionsController.state.siwx
if (siwx) {
const activeCaipNetwork = ChainController.getActiveCaipNetwork()
const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress())

if (activeCaipNetwork && address) {
siwx.revokeSession(activeCaipNetwork.caipNetworkId, address)
if (activeCaipNetwork && address) {
await siwx.revokeSession(activeCaipNetwork.caipNetworkId, address)
}
}
}

try {
await connectionControllerClient?.disconnect()
this.resetWcConnection()
} catch (error) {
Expand All @@ -277,7 +274,10 @@ export const ConnectionController = {
*/
async initializeSWIXIfAvailable() {
const siwx = OptionsController.state.siwx
if (!siwx) {
const address = CoreHelperUtil.getPlainAddress(ChainController.getActiveCaipAddress())
const network = ChainController.getActiveCaipNetwork()

if (!(siwx && address && network)) {
return
}

Expand All @@ -287,25 +287,21 @@ export const ConnectionController = {
return
}

const activeCaipNetwork = ChainController.getActiveCaipNetwork()
const client = this._getClient(activeCaipNetwork?.chainNamespace)
const client = this._getClient(network?.chainNamespace)

try {
if (!activeCaipNetwork) {
throw new Error('No active chain')
}

const address = ChainController.getActiveCaipAddress()?.split(':')[2] || ''

const sessions = await siwx.getSessions(activeCaipNetwork.caipNetworkId, address)
const sessions = await siwx.getSessions(network.caipNetworkId, address)
if (sessions.length) {
return
}

ModalController.open({ view: 'SIWXSignMessage' })
await ModalController.open({
view:
StorageUtil.getConnectedConnector() === 'AUTH' ? 'ApproveTransaction' : 'SIWXSignMessage'
})

const siwxMessage = await siwx.createMessage({
chainId: activeCaipNetwork.caipNetworkId,
chainId: network.caipNetworkId,
accountAddress: address
})

Expand All @@ -323,10 +319,10 @@ export const ConnectionController = {
} catch (error) {
// eslint-disable-next-line no-console
console.error('Failed to initialize SIWX', error)

await client.disconnect()

throw error
ModalController.setLoading(true)
await client.disconnect().finally(() => {
ModalController.setLoading(false)
})
}
}
}
3 changes: 3 additions & 0 deletions packages/scaffold-ui/src/modal/w3m-modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AccountController,
ApiController,
ChainController,
ConnectionController,
CoreHelperUtil,
EventsController,
ModalController,
Expand Down Expand Up @@ -198,6 +199,8 @@ export class W3mModal extends LitElement {

this.caipAddress = caipAddress

await ConnectionController.initializeSWIXIfAvailable()

if (nextConnected && !isSameAddress && this.isSiweEnabled) {
try {
const { SIWEController } = await import('@reown/appkit-siwe')
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f6b3bb

Please sign in to comment.