Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update wallet connect session when new accounts addresses are created #2699

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IAccountState, getAddressFromAccountForNetwork } from '@core/account'
import { get } from 'svelte/store'
import { ISupportedNamespace } from '../types'
import { getWalletClient, walletClient } from '../stores'
import { EvmNetworkId } from '@core/network/types'
import { buildCaip10Address } from '../utils'
import { connectedDapps } from '../stores'

export async function addAccountToAllDappSessions(account: IAccountState): Promise<void> {
const dapps = get(connectedDapps)
for (const dapp of dapps) {
if (dapp.sessionTopic && dapp.namespaces) {
await addAccountForDappSession(dapp.sessionTopic, dapp.namespaces, account)
}
}
}

export async function addAccountForDappSession(
sessionTopic: string,
namespaces: Record<string, ISupportedNamespace>,
account: IAccountState
): Promise<void> {
const walletConnectClient = get(walletClient)
if (!walletConnectClient) {
return
}

const protocols = Object.keys(namespaces ?? {})
for (const protocol of protocols) {
if (!namespaces[protocol]) {
continue
}

const chainsForSession = (namespaces[protocol].chains ?? []) as EvmNetworkId[]
const addressOfAccountForEachChain = chainsForSession
.map((chainId) => {
const address = getAddressFromAccountForNetwork(account, chainId)
return address ? buildCaip10Address(address, chainId) : undefined
})
.filter(Boolean) as string[]

namespaces[protocol].accounts = [...addressOfAccountForEachChain, ...namespaces[protocol].accounts]
}

await getWalletClient()?.updateSession({ topic: sessionTopic, namespaces })
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './addAccountToAllDappSessions'
export * from './approveSession'
export * from './approveSessionAuthenticate'
export * from './buildSupportedNamespaceFromSelections'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { setSelectedAccount } from './setSelectedAccount'
import { getActiveProfile } from '@core/profile/stores'
import { ProfileType } from '@core/profile'
import { generateAndStoreEvmAddressForAccounts, pollEvmBalancesForAccount } from '@core/layer-2/actions'
import { getEvmNetworks } from '@core/network/stores'
import { IError } from '@core/error/interfaces'
import { DEFAULT_COIN_TYPE, SupportedNetworkId } from '@core/network/constants'
import { addAccountToAllDappSessions, updateAccountForConnectedDapps } from '@auxiliary/wallet-connect/actions'

export async function tryCreateAdditionalAccount(alias: string, color: string): Promise<void> {
try {
Expand All @@ -21,11 +22,12 @@ export async function tryCreateAdditionalAccount(alias: string, color: string):

const activeProfile = getActiveProfile()
if (activeProfile.type === ProfileType.Software) {
const coinType = getEvmNetworks()[0]?.coinType
if (coinType !== undefined) {
void generateAndStoreEvmAddressForAccounts(activeProfile.type, coinType, account)
const coinType = DEFAULT_COIN_TYPE[SupportedNetworkId.Ethereum]
generateAndStoreEvmAddressForAccounts(activeProfile.type, coinType, account).then(async () => {
jeeanribeiro marked this conversation as resolved.
Show resolved Hide resolved
void pollEvmBalancesForAccount(activeProfile.id, account)
}
await addAccountToAllDappSessions(account)
await updateAccountForConnectedDapps(account)
})
}

return Promise.resolve()
Expand Down
Loading