From 9a5163816476519dd7dcec0c0b831b70ad72987b Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Mon, 16 Sep 2024 14:26:56 -0300 Subject: [PATCH] fix: fallback to account chains to get session chains (#2855) --- .../web3js/providers/WalletConnectProvider.ts | 15 ++++++++++- .../tests/WalletConnectProvider.test.ts | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts b/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts index 60eb696962..8815d752b3 100644 --- a/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts +++ b/packages/base/adapters/solana/web3js/providers/WalletConnectProvider.ts @@ -217,7 +217,20 @@ export class WalletConnectProvider extends ProviderEventEmitter implements Provi } private get sessionChains() { - return this.session?.namespaces['solana']?.chains || [] + const solanaNamespace = this.session?.namespaces['solana'] + + if (!solanaNamespace) { + return [] + } + + const chains = solanaNamespace.chains || [] + const accountsChains = solanaNamespace.accounts.map(account => { + const [chainNamespace, chainId] = account.split(':') + + return `${chainNamespace}:${chainId}` + }) + + return Array.from(new Set([...chains, ...accountsChains])) } private serializeTransaction(transaction: AnyTransaction) { diff --git a/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts b/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts index 79876bf81a..262dbf9fc4 100644 --- a/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts +++ b/packages/base/adapters/solana/web3js/tests/WalletConnectProvider.test.ts @@ -289,4 +289,31 @@ describe('WalletConnectProvider specific tests', () => { 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp' ) }) + + it('should get chains from namespace accounts', async () => { + vi.spyOn(provider, 'connect').mockImplementationOnce(() => + Promise.resolve( + mockUniversalProviderSession({ + namespaces: { + solana: { + chains: undefined, + methods: [ + 'solana_signTransaction', + 'solana_signMessage', + 'solana_signAndSendTransaction' + ], + events: [], + accounts: [ + `solana:${TestConstants.chains[0]?.chainId}:${TestConstants.accounts[0].address}` + ] + } + } + }) + ) + ) + + await walletConnectProvider.connect() + + expect(walletConnectProvider.chains).toEqual([TestConstants.chains[0]]) + }) })