Skip to content

Commit

Permalink
refactor: only update session if required
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Sep 27, 2023
1 parent bc28067 commit 61de435
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/services/walletconnect/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,47 +117,51 @@ class WalletConnectWallet {
namespaces: getNamespaces(chains, proposal.params.requiredNamespaces[EIP155]?.methods ?? SAFE_COMPATIBLE_METHODS),
})

// TODO: Align session via update then filter "fake" addresses? If we filter in updateSession this would be covered

// Workaround: WalletConnect doesn't have a session_add event
this.web3Wallet?.events.emit(SESSION_ADD_EVENT)

return session
}

private async updateSession(session: SessionTypes.Struct, chainId: string, safeAddress: string) {
const currentChains = session.namespaces[EIP155]?.chains || []
const currentAccounts = session.namespaces[EIP155]?.accounts || []
assertWeb3Wallet(this.web3Wallet)

const eip155ChainIds = [...new Set([...currentChains, getEip155ChainId(chainId)])]
const eip155Accounts = [...new Set([...currentAccounts, `${getEip155ChainId(chainId)}:${safeAddress}`])]
const currentEip155ChainIds = session.namespaces[EIP155]?.chains || []
const currentEip155Accounts = session.namespaces[EIP155]?.accounts || []

const namespaces: SessionTypes.Namespaces = {
[EIP155]: {
...session.namespaces[EIP155],
chains: eip155ChainIds,
accounts: eip155Accounts,
},
}
const newEip155ChainId = getEip155ChainId(chainId)
const newEip155Account = `${newEip155ChainId}:${safeAddress}`

const { topic } = session
const hasNewChainId = !currentEip155ChainIds.includes(newEip155ChainId)
const hasNewAccount = !currentEip155Accounts.includes(newEip155Account)

await this.web3Wallet?.updateSession({
topic,
namespaces,
})
// Add new chainId and/or account to the session namespace
if (hasNewChainId || hasNewAccount) {
const uniqueEip155ChainIds = [...new Set([newEip155ChainId, ...currentEip155ChainIds])]
const unqiueEip155Accounts = [...new Set([newEip155Account, ...currentEip155Accounts])]

// TODO: Only emit if new address doesn't match that of current one?
await this.accountsChanged(topic, chainId, safeAddress)
const namespaces: SessionTypes.Namespaces = {
[EIP155]: {
...session.namespaces[EIP155],
chains: uniqueEip155ChainIds,
accounts: unqiueEip155Accounts,
},
}

await this.web3Wallet.updateSession({
topic: session.topic,
namespaces,
})
}

await this.chainChanged(topic, chainId)
// Switch to the new account
await this.accountsChanged(session.topic, chainId, safeAddress)

// TODO: Filter "fake" addresses?
// Switch to the new chain
await this.chainChanged(session.topic, chainId)
}

public async updateSessions(chainId: string, safeAddress: string) {
assertWeb3Wallet(this.web3Wallet)

await Promise.all(this.getActiveSessions().map((session) => this.updateSession(session, chainId, safeAddress)))
}

Expand Down

0 comments on commit 61de435

Please sign in to comment.