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: add check for missing address or chainId when getting siwe session #3332

Merged
merged 12 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions .changeset/curly-cameras-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-siwe': patch
'@apps/demo': patch
'@apps/gallery': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

Adds check for missing address or chainId when getting siwe session for guarantee of backwards compatibility.
16 changes: 13 additions & 3 deletions packages/siwe/src/mapToSIWX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ const subscriptions: (() => void)[] = []
export function mapToSIWX(siwe: AppKitSIWEClient): SIWXConfig {
async function getSession() {
try {
return await siwe.methods.getSession()
const response = await siwe.methods.getSession()

if (!response?.address) {
throw new Error('SIWE session is missing address')
}

if (!response?.chainId) {
throw new Error('SIWE session is missing chainId')
}

return response
} catch (error) {
console.warn('AppKit:SIWE:getSession - error:', error)

Expand Down Expand Up @@ -55,7 +65,7 @@ export function mapToSIWX(siwe: AppKitSIWEClient): SIWXConfig {
if (siwe.options.signOutOnAccountChange) {
const session = await getSession()

const lowercaseSessionAddress = session?.address.toLowerCase()
const lowercaseSessionAddress = session?.address?.toLowerCase()
const lowercaseCaipAddress =
CoreHelperUtil?.getPlainAddress(activeCaipAddress)?.toLowerCase()

Expand Down Expand Up @@ -164,7 +174,7 @@ export function mapToSIWX(siwe: AppKitSIWEClient): SIWXConfig {

const siweSession = await getSession()
const siweCaipNetworkId = `eip155:${siweSession?.chainId}`
const lowercaseSessionAddress = siweSession?.address.toLowerCase()
const lowercaseSessionAddress = siweSession?.address?.toLowerCase()
const lowercaseCaipAddress = address?.toLowerCase()

if (
Expand Down
18 changes: 18 additions & 0 deletions packages/siwe/tests/mapToSIWX.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ describe('SIWE: mapToSIWX', () => {

await expect(siwx.getSessions('eip155:1', 'mock-address')).resolves.toMatchObject([])
})

it('should accept undefined address or chain', async () => {
const siwx = mapToSIWX(siweConfig)

vi.spyOn(siweConfig.methods, 'getSession').mockResolvedValueOnce({
address: undefined as any,
chainId: 1
})

await expect(siwx.getSessions('eip155:1', 'mock-address')).resolves.toMatchObject([])

vi.spyOn(siweConfig.methods, 'getSession').mockResolvedValueOnce({
address: 'mock-address',
chainId: undefined as any
})

await expect(siwx.getSessions('eip155:1', 'mock-address')).resolves.toMatchObject([])
})
})

describe('siwe.options.signOutOnNetworkChange', () => {
Expand Down
Loading