Skip to content

Commit

Permalink
fix: add default value if namespace is not available on upa getAccoun…
Browse files Browse the repository at this point in the history
…ts (#3637)
  • Loading branch information
zoruka authored Jan 14, 2025
1 parent a66de04 commit 40ef5c7
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
23 changes: 23 additions & 0 deletions .changeset/eight-forks-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit': patch
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Add default value if namespace is not available on upa getAccounts
4 changes: 2 additions & 2 deletions packages/appkit/src/universal-adapter/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export class UniversalAdapter extends AdapterBlueprint {
namespace: ChainNamespace
}): Promise<AdapterBlueprint.GetAccountsResult> {
const provider = this.provider as UniversalProvider
const addresses = provider?.session?.namespaces?.[namespace]?.accounts
const addresses = (provider?.session?.namespaces?.[namespace]?.accounts
?.map(account => {
const [, , address] = account.split(':')

return address
})
.filter((address, index, self) => self.indexOf(address) === index) as string[]
.filter((address, index, self) => self.indexOf(address) === index) || []) as string[]

return Promise.resolve({
accounts: addresses.map(address =>
Expand Down
44 changes: 44 additions & 0 deletions packages/appkit/tests/universal-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,48 @@ describe('UniversalAdapter', () => {
).rejects.toThrow('UniversalAdapter:switchNetwork - provider is undefined')
})
})

describe('getAccounts', () => {
it('should return empty array if there is no accounts', async () => {
mockProvider.session = undefined
const accounts = await adapter.getAccounts({ id: '', namespace: 'eip155' })

expect(accounts).toEqual({ accounts: [] })
})

it('should return accounts successfully', async () => {
mockProvider.session = {
namespaces: {
eip155: {
accounts: ['eip155:mock_network:mock_address_1', 'eip155:mock_network:mock_address_2']
}
}
} as any

Object.assign(adapter, {
provider: mockProvider
})

const accounts = await adapter.getAccounts({ id: '', namespace: 'eip155' })

expect(accounts).toEqual({
accounts: [
{
address: 'mock_address_1',
namespace: 'eip155',
path: undefined,
publicKey: undefined,
type: 'eoa'
},
{
address: 'mock_address_2',
namespace: 'eip155',
path: undefined,
publicKey: undefined,
type: 'eoa'
}
]
})
})
})
})

0 comments on commit 40ef5c7

Please sign in to comment.