Skip to content

Commit

Permalink
fix: update session after approval
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Oct 5, 2023
1 parent c6a159a commit cfbfebe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
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
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

0 comments on commit cfbfebe

Please sign in to comment.