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: update session after approval #2595

Merged
merged 2 commits into from
Oct 6, 2023
Merged
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
18 changes: 14 additions & 4 deletions src/services/walletconnect/WalletConnectContext.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getSdkError } from '@walletconnect/utils'
import { formatJsonRpcError } from '@walletconnect/jsonrpc-utils'
import { type ReactNode, createContext, useEffect, useState } from 'react'

import useSafeInfo from '@/hooks/useSafeInfo'
Expand Down Expand Up @@ -62,16 +64,24 @@ export const WalletConnectProvider = ({ children }: { children: ReactNode }) =>
const session = walletConnect.getActiveSessions().find((s) => s.topic === topic)
const requestChainId = stripEip155Prefix(event.params.chainId)

if (!session || requestChainId !== chainId) return
const getResponse = () => {
// Get error if wrong chain
if (!session || requestChainId !== chainId) {
const error = getSdkError('UNSUPPORTED_CHAINS')
return formatJsonRpcError(event.id, error)
}

try {
// Get response from the Safe Wallet Provider
const response = await safeWalletProvider.request(event.id, event.params.request, {
// Get response from Safe Wallet Provider
return safeWalletProvider.request(event.id, event.params.request, {
name: session.peer.metadata.name,
description: session.peer.metadata.description,
url: session.peer.metadata.url,
iconUrl: session.peer.metadata.icons[0],
})
}

try {
const response = await getResponse()

// Send response to WalletConnect
await walletConnect.sendSessionResponse(topic, response)
Expand Down
5 changes: 4 additions & 1 deletion src/services/walletconnect/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ class WalletConnectWallet {
namespaces: getNamespaces(chains, proposal.params.requiredNamespaces[EIP155]?.methods ?? SAFE_COMPATIBLE_METHODS),
})

await this.updateSession(session, currentChainId, safeAddress)

// Workaround: WalletConnect doesn't have a session_add event
this.web3Wallet?.events.emit(SESSION_ADD_EVENT)

return session
// Return updated session as it may have changed
return this.getActiveSessions().find(({ topic }) => topic === session.topic) ?? session
}

private async updateSession(session: SessionTypes.Struct, chainId: string, safeAddress: string) {
Expand Down
20 changes: 16 additions & 4 deletions src/services/walletconnect/__tests__/WalletConnectContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,14 @@ describe('WalletConnectProvider', () => {
},
} as unknown as Web3WalletTypes.SessionRequest)

expect(mockRequest).not.toHaveBeenCalled()
expect(sendSessionResponseSpy).not.toHaveBeenCalled()
await waitFor(() => {
expect(sendSessionResponseSpy).toHaveBeenCalledWith('topic', {
error: { code: 5100, message: 'Unsupported chains.' },
id: 1,
jsonrpc: '2.0',
})
expect(mockRequest).not.toHaveBeenCalled()
})
})

it('does not continue with the request if there is no matching chainId', async () => {
Expand Down Expand Up @@ -376,8 +382,14 @@ describe('WalletConnectProvider', () => {
},
} as unknown as Web3WalletTypes.SessionRequest)

expect(mockRequest).not.toHaveBeenCalled()
expect(sendSessionResponseSpy).not.toHaveBeenCalled()
await waitFor(() => {
expect(sendSessionResponseSpy).toHaveBeenCalledWith('topic', {
error: { code: 5100, message: 'Unsupported chains.' },
id: 1,
jsonrpc: '2.0',
})
expect(mockRequest).not.toHaveBeenCalled()
})
})

it('passes the request onto the Safe Wallet Provider and sends the response to WalletConnect', async () => {
Expand Down
56 changes: 56 additions & 0 deletions src/services/walletconnect/__tests__/WalletConnectWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ describe('WalletConnectWallet', () => {
describe('approveSession', () => {
it('should approve the session with proposed required/optional chains/methods and required events', async () => {
const approveSessionSpy = jest.spyOn((wallet as any).web3Wallet as IWeb3Wallet, 'approveSession')
approveSessionSpy.mockResolvedValue({
namespaces: {
eip155: {},
},
} as unknown as SessionTypes.Struct)

const proposal = {
id: 123,
Expand Down Expand Up @@ -185,8 +190,59 @@ describe('WalletConnectWallet', () => {
})
})

it('should call updateSession with the correct parameters', async () => {
const emitSessionEventSpy = jest.spyOn((wallet as any).web3Wallet as IWeb3Wallet, 'emitSessionEvent')
jest.spyOn((wallet as any).web3Wallet as IWeb3Wallet, 'approveSession').mockResolvedValue({
topic: 'topic',
namespaces: {
eip155: {},
},
} as unknown as SessionTypes.Struct)

await wallet.approveSession(
{
id: 1,
params: {
id: 1,
expiry: 1,
relays: [],
proposer: {
publicKey: '123',
metadata: {} as SignClientTypes.Metadata,
},
requiredNamespaces: {} as ProposalTypes.RequiredNamespaces,
optionalNamespaces: {} as ProposalTypes.OptionalNamespaces,
},
verifyContext: {} as Verify.Context,
},
'1',
hexZeroPad('0x123', 20),
)

expect(emitSessionEventSpy).toHaveBeenCalledTimes(2)
expect(emitSessionEventSpy).toHaveBeenNthCalledWith(1, {
topic: 'topic',
event: {
name: 'accountsChanged',
data: [hexZeroPad('0x123', 20)],
},
chainId: 'eip155:1',
})
expect(emitSessionEventSpy).toHaveBeenNthCalledWith(2, {
topic: 'topic',
event: { data: 1, name: 'chainChanged' },
chainId: 'eip155:1',
})
})

it('should call emitSessionEvent with the correct parameters', async () => {
const emitSpy = jest.spyOn(((wallet as any).web3Wallet as IWeb3Wallet).events, 'emit')
jest.spyOn((wallet as any).web3Wallet as IWeb3Wallet, 'approveSession').mockResolvedValue({
topic: 'topic',
namespaces: {
eip155: {},
},
} as unknown as SessionTypes.Struct)

await wallet.approveSession(
{
Expand Down