Skip to content

Commit

Permalink
fix: set accounts correctly (#3509)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp authored Dec 19, 2024
1 parent 732736b commit 0926b4d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .changeset/small-penguins-pay.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
---

Fix issue where accounts were not correctly set
3 changes: 2 additions & 1 deletion packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,9 +1202,10 @@ export class AppKit {
CoreHelperUtil.createAccount(
namespace,
account.address,
namespace === 'eip155' ? preferredAccountType : 'eoa'
namespace === 'eip155' ? account.type : 'eoa'
)
)

this.setAllAccounts(
userAccounts || [
CoreHelperUtil.createAccount(namespace, user.address, preferredAccountType)
Expand Down
65 changes: 65 additions & 0 deletions packages/appkit/src/tests/appkit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,71 @@ describe('Base', () => {
expect(AccountController.setPreferredAccountType).toHaveBeenCalledWith('eoa', 'eip155')
})

it('should create accounts with correct account types from user accounts', async () => {
const mockUser = {
address: '0x123',
accounts: [
{ address: '0x1', type: 'eoa' },
{ address: '0x2', type: 'smartAccount' }
],
preferredAccountType: 'eoa'
}

vi.mocked(ChainController).state = {
activeChain: 'eip155',
chains: new Map([['eip155', { namespace: 'eip155' }]])
} as any

vi.mocked(CoreHelperUtil.createAccount).mockImplementation((namespace, address, type) => {
if (namespace === 'eip155') {
return {
address,
type: type as 'eoa' | 'smartAccount',
namespace: 'eip155' as const
}
}
throw new Error('Unexpected namespace')
})

const mockAuthProvider = {
onConnect: vi.fn(callback => callback(mockUser)),
connect: vi.fn(),
getSmartAccountEnabledNetworks: vi.fn(),
onGetSmartAccountEnabledNetworks: vi.fn(),
onSetPreferredAccount: vi.fn(),
onRpcRequest: vi.fn(),
onRpcError: vi.fn(),
onRpcSuccess: vi.fn(),
onNotConnected: vi.fn(),
onIsConnected: vi.fn(),
getLoginEmailUsed: vi.fn().mockReturnValue(false),
isConnected: vi.fn().mockResolvedValue({ isConnected: false })
}

const appKitWithAuth = new AppKit({
...mockOptions,
features: {
email: true
}
})
;(appKitWithAuth as any).authProvider = mockAuthProvider

await (appKitWithAuth as any).listenAuthConnector(mockAuthProvider)

expect(CoreHelperUtil.createAccount).toHaveBeenCalledWith('eip155', '0x1', 'eoa')
expect(CoreHelperUtil.createAccount).toHaveBeenCalledWith('eip155', '0x2', 'smartAccount')

expect(AccountController.setAllAccounts).toHaveBeenCalledWith(
[
{ address: '0x1', type: 'eoa', namespace: 'eip155' },
{ address: '0x2', type: 'smartAccount', namespace: 'eip155' }
],
'eip155'
)

expect(AccountController.setPreferredAccountType).toHaveBeenCalledWith('eoa', 'eip155')
})

it('should get Reown name', async () => {
vi.mocked(EnsController.getNamesForAddress).mockResolvedValue([
{
Expand Down

0 comments on commit 0926b4d

Please sign in to comment.