From cd9b471c50f5c97426dc387dc55b352f97d40afd Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Mon, 1 Jul 2024 10:22:10 +0200 Subject: [PATCH 1/2] update session on account creation --- .../actions/addAccountToAllDappSessions.ts | 46 +++++++++++++++++++ .../auxiliary/wallet-connect/actions/index.ts | 1 + .../actions/tryCreateAdditionalAccount.ts | 12 +++-- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/actions/addAccountToAllDappSessions.ts diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/addAccountToAllDappSessions.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/addAccountToAllDappSessions.ts new file mode 100644 index 0000000000..4cd381155c --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/addAccountToAllDappSessions.ts @@ -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 { + 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, + account: IAccountState +): Promise { + 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 }) +} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts index 2969f8bb30..beaf1289f1 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts @@ -1,3 +1,4 @@ +export * from './addAccountToAllDappSessions' export * from './approveSession' export * from './approveSessionAuthenticate' export * from './buildSupportedNamespaceFromSelections' diff --git a/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts b/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts index 03126be33f..7cd1e6c88c 100644 --- a/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts +++ b/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts @@ -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 { updateAccountForConnectedDapps } from '@auxiliary/wallet-connect/actions' export async function tryCreateAdditionalAccount(alias: string, color: string): Promise { try { @@ -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(() => { void pollEvmBalancesForAccount(activeProfile.id, account) - } + void account + void updateAccountForConnectedDapps(account) + }) } return Promise.resolve() From e85482420e297164afb5497138e0c80770b4817a Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Mon, 1 Jul 2024 10:36:22 +0200 Subject: [PATCH 2/2] fix dapp reload --- .../core/account/actions/tryCreateAdditionalAccount.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts b/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts index 7cd1e6c88c..eff9a0ee56 100644 --- a/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts +++ b/packages/shared/src/lib/core/account/actions/tryCreateAdditionalAccount.ts @@ -9,7 +9,7 @@ import { ProfileType } from '@core/profile' import { generateAndStoreEvmAddressForAccounts, pollEvmBalancesForAccount } from '@core/layer-2/actions' import { IError } from '@core/error/interfaces' import { DEFAULT_COIN_TYPE, SupportedNetworkId } from '@core/network/constants' -import { updateAccountForConnectedDapps } from '@auxiliary/wallet-connect/actions' +import { addAccountToAllDappSessions, updateAccountForConnectedDapps } from '@auxiliary/wallet-connect/actions' export async function tryCreateAdditionalAccount(alias: string, color: string): Promise { try { @@ -23,10 +23,10 @@ export async function tryCreateAdditionalAccount(alias: string, color: string): const activeProfile = getActiveProfile() if (activeProfile.type === ProfileType.Software) { const coinType = DEFAULT_COIN_TYPE[SupportedNetworkId.Ethereum] - generateAndStoreEvmAddressForAccounts(activeProfile.type, coinType, account).then(() => { + generateAndStoreEvmAddressForAccounts(activeProfile.type, coinType, account).then(async () => { void pollEvmBalancesForAccount(activeProfile.id, account) - void account - void updateAccountForConnectedDapps(account) + await addAccountToAllDappSessions(account) + await updateAccountForConnectedDapps(account) }) }