Skip to content

Commit

Permalink
Revert "fix: cannot connect with WC to Opensea (safe-global#3437)"
Browse files Browse the repository at this point in the history
This reverts commit 3e49165.
  • Loading branch information
katspaugh committed Mar 27, 2024
1 parent d03567f commit d46c800
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 73 deletions.
18 changes: 4 additions & 14 deletions src/features/walletconnect/services/WalletConnectWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,15 @@ class WalletConnectWallet {
})
}

private getNamespaces(
proposal: Web3WalletTypes.SessionProposal,
currentChainId: string,
safeAddress: string,
optional = false,
) {
private getNamespaces(proposal: Web3WalletTypes.SessionProposal, currentChainId: string, safeAddress: string) {
// Most dApps require mainnet, but we aren't always on mainnet
// As workaround, we pretend include all required and optional chains with the Safe chainId
const requiredChains = proposal.params.requiredNamespaces[EIP155]?.chains || []
const optionalChains = proposal.params.optionalNamespaces[EIP155]?.chains || []

const supportedChainIds = [currentChainId].concat(
requiredChains.map(stripEip155Prefix),
optional ? optionalChains.map(stripEip155Prefix) : [],
optionalChains.map(stripEip155Prefix),
)

const eip155ChainIds = supportedChainIds.map(getEip155ChainId)
Expand Down Expand Up @@ -126,13 +123,6 @@ class WalletConnectWallet {
namespaces,
})

const updatedNamespaces = {
...session.namespaces,
...this.getNamespaces(proposal, currentChainId, safeAddress, true),
}

await this.web3Wallet.updateSession({ topic: session.topic, namespaces: updatedNamespaces })

await this.chainChanged(session.topic, currentChainId)

// Workaround: WalletConnect doesn't have a session_add event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ 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')
const updateSessionSpy = jest.spyOn((wallet as any).web3Wallet as IWeb3Wallet, 'updateSession')
approveSessionSpy.mockResolvedValue({
topic: 'abc',
namespaces: {
eip155: {},
},
Expand Down Expand Up @@ -161,23 +159,7 @@ describe('WalletConnectWallet', () => {
toBeHex('0x123', 20),
)

const namespaceWithOnlyRequired = {
eip155: {
chains: ['eip155:1'],
methods: [
'eth_sendTransaction',
'personal_sign',
'eth_accounts',
'eth_sign',
'eth_signTypedData',
'eth_signTypedData_v4',
'wallet_switchEthereumChain',
],
events: ['chainChanged', 'accountsChanged'],
accounts: [`eip155:1:${toBeHex('0x123', 20)}`],
},
}
const namespacesWithOptional = {
const namespaces = {
eip155: {
chains: [
'eip155:1',
Expand Down Expand Up @@ -212,12 +194,7 @@ describe('WalletConnectWallet', () => {

expect(approveSessionSpy).toHaveBeenCalledWith({
id: 123,
namespaces: namespaceWithOnlyRequired,
})

expect(updateSessionSpy).toHaveBeenCalledWith({
topic: 'abc',
namespaces: namespacesWithOptional,
namespaces,
})
})

Expand Down Expand Up @@ -253,51 +230,26 @@ describe('WalletConnectWallet', () => {
toBeHex('0x123', 20),
)

let chains = ['eip155:43114', 'eip155:42161', 'eip155:8453', 'eip155:100', 'eip155:137', 'eip155:1101']
let accounts = [
`eip155:43114:${toBeHex('0x123', 20)}`,
`eip155:42161:${toBeHex('0x123', 20)}`,
`eip155:8453:${toBeHex('0x123', 20)}`,
`eip155:100:${toBeHex('0x123', 20)}`,
`eip155:137:${toBeHex('0x123', 20)}`,
`eip155:1101:${toBeHex('0x123', 20)}`,
]
const namespaces = {
eip155: {
chains: [],
chains: ['eip155:43114', 'eip155:42161', 'eip155:8453', 'eip155:100', 'eip155:137', 'eip155:1101'],
methods: ['eth_accounts', 'personal_sign', 'eth_sendTransaction'],
events: ['chainChanged', 'accountsChanged'],
accounts: [],
accounts: [
`eip155:43114:${toBeHex('0x123', 20)}`,
`eip155:42161:${toBeHex('0x123', 20)}`,
`eip155:8453:${toBeHex('0x123', 20)}`,
`eip155:100:${toBeHex('0x123', 20)}`,
`eip155:137:${toBeHex('0x123', 20)}`,
`eip155:1101:${toBeHex('0x123', 20)}`,
],
},
}

await wallet.approveSession(
proposal,
'69420', // not in proposal, therefore not supported
toBeHex('0x123', 20),
)

expect(approveSessionSpy).toHaveBeenCalledWith({
id: 123,
namespaces,
})

await wallet.approveSession(
proposal,
'42161', // in proposal, therefore supported
toBeHex('0x123', 20),
)
expect(approveSessionSpy).toHaveBeenCalledWith({
id: 123,
namespaces: {
...namespaces,
eip155: {
...namespaces.eip155,
chains: [chains[1]],
accounts: [accounts[1]],
},
},
})
})

it('should call updateSession with the correct parameters', async () => {
Expand Down

0 comments on commit d46c800

Please sign in to comment.