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

fix: universal provider accounts changed event #3462

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions .changeset/swift-melons-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@reown/appkit': patch
'@reown/appkit-core': 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-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
---

Adds parsing of Universal Provider session_event to get accountsChanged event
15 changes: 15 additions & 0 deletions packages/appkit/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import type { SessionTypes } from '@walletconnect/types'
import type { UniversalProviderOpts } from '@walletconnect/universal-provider'
import { W3mFrameProviderSingleton } from './auth-provider/W3MFrameProviderSingleton.js'
import { WcHelpersUtil } from './utils/HelpersUtil.js'

declare global {
interface Window {
Expand Down Expand Up @@ -1286,6 +1287,20 @@
this.setCaipNetwork(caipNetwork)
}
})

this.universalProvider.on('session_event', (callbackData: unknown) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm parsing through sesion_event because only accountsChanged doesn't bring the chain data

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting

if (WcHelpersUtil.isSessionEventData(callbackData)) {
const { name, data } = callbackData.params.event

if (name === 'accountsChanged' && Array.isArray(data)) {
const caipAddress = CoreHelperUtil.parseCaipAddress(data[0])

if (caipAddress) {
this.syncAccount(caipAddress)
}
}
}
})
}
}

Expand Down Expand Up @@ -1790,7 +1805,7 @@
}

private createAuthProvider() {
const emailEnabled =

Check warning on line 1808 in packages/appkit/src/client.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Variable name `emailEnabled` must have one of the following prefixes: is, has, can, should, will, did
this.options?.features?.email === undefined
? CoreConstantsUtil.DEFAULT_FEATURES.email
: this.options?.features?.email
Expand Down
23 changes: 23 additions & 0 deletions packages/appkit/src/tests/utils/HelpersUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,27 @@ describe('WcHelpersUtil', () => {
])
})
})

describe('isSessionEventData', () => {
test.each([
[undefined, false],
[{}, false],
[
{
id: 1734112958243866,
topic: 'b2cb2748499532d9c307846c444b364dd881c959d9a080e30d63b6a76270a0f8',
params: {
event: {
name: 'accountsChanged',
data: ['eip155:1:0x53F31e8972Ebddac1553E37887C25C1b748485A6']
},
chainId: 'eip155:1'
}
},
true
]
])('should validate session event data', (data, expected) => {
expect(WcHelpersUtil.isSessionEventData(data)).toBe(expected)
})
})
})
24 changes: 24 additions & 0 deletions packages/appkit/src/utils/HelpersUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,29 @@ export const WcHelpersUtil = {

return Array.from(new Set([...chains, ...accountsChains]))
})
},

isSessionEventData(data: unknown): data is WcHelpersUtil.SessionEventData {
return (
typeof data === 'object' &&
data !== null &&
'id' in data &&
'topic' in data &&
'params' in data &&
typeof data.params === 'object' &&
data.params !== null &&
'chainId' in data.params &&
'event' in data.params &&
typeof data.params.event === 'object' &&
data.params.event !== null
)
}
}

export namespace WcHelpersUtil {
export type SessionEventData = {
id: string
topic: string
params: { chainId: string; event: { data: unknown; name: string } }
}
}
31 changes: 31 additions & 0 deletions packages/core/src/utils/CoreHelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,36 @@ export const CoreHelperUtil = {
address,
type
} as AccountTypeMap[N]
},

isCaipAddress(address?: unknown): address is CaipAddress {
if (typeof address !== 'string') {
return false
}

const sections = address.split(':')
const namespace = sections[0]

return sections.length === 3 && (namespace as string) in CommonConstants.CHAIN_NAME_MAP
},

parseCaipAddress(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import it from here ?

caipAddress?: unknown
): { chainNamespace: ChainNamespace; chainId: string; address: string } | undefined {
if (CoreHelperUtil.isCaipAddress(caipAddress)) {
const [chainNamespace, chainId, address] = caipAddress.split(':') as [
ChainNamespace,
string,
string
]

return {
chainNamespace,
chainId,
address
}
}

return undefined
}
}
33 changes: 33 additions & 0 deletions packages/core/tests/utils/CoreHelperUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,37 @@ describe('CoreHelperUtil', () => {
global.window.self = originalSelf
}
})

it.each([
[undefined, false],
[{}, false],
['0x0', false],
['invalid_namespace:mock_chain_id:address', false],
['eip155:mock_chain_id:mock_address', true],
['solana:mock_chain_id:mock_address', true],
['bip122:mock_chain_id:mock_address', true]
])('should validate the value $s is valid caip address $b', (caipAddress, expected) => {
expect(CoreHelperUtil.isCaipAddress(caipAddress)).toEqual(expected)
})

it.each([
[undefined, undefined],
[{}, undefined],
['0x0', undefined],
['invalid_namespace:mock_chain_id:address', undefined],
[
'eip155:mock_chain_id:mock_address',
{ chainNamespace: 'eip155', chainId: 'mock_chain_id', address: 'mock_address' }
],
[
'solana:mock_chain_id:mock_address',
{ chainNamespace: 'solana', chainId: 'mock_chain_id', address: 'mock_address' }
],
[
'bip122:mock_chain_id:mock_address',
{ chainNamespace: 'bip122', chainId: 'mock_chain_id', address: 'mock_address' }
]
])('should validate the value $s is valid caip address $b', (caipAddress, expected) => {
expect(CoreHelperUtil.parseCaipAddress(caipAddress)).toEqual(expected)
})
})
Loading