|
1 | | -import WalletConnectProvider from '@walletconnect/web3-provider' |
2 | 1 | import { IClientMeta } from '@walletconnect/types' |
3 | 2 | import { WalletModule } from 'bnc-onboard/dist/src/interfaces' |
4 | 3 | import UAParser from 'ua-parser-js' |
5 | 4 |
|
6 | 5 | import { APP_VERSION, PUBLIC_URL } from 'src/utils/constants' |
| 6 | +import { ChainId } from 'src/config/chain' |
7 | 7 | import { getWCWalletInterface, getWalletConnectProvider } from 'src/logic/wallets/walletConnect/utils' |
8 | | -import { _getChainId } from 'src/config' |
9 | 8 |
|
10 | 9 | // Modified version of the built in WC module in Onboard v1.35.5 |
11 | 10 | // https://github.com/blocknative/onboard/blob/release/1.35.5/src/modules/select/wallets/wallet-connect.ts |
@@ -34,59 +33,40 @@ const getClientMeta = (): IClientMeta => { |
34 | 33 | } |
35 | 34 | } |
36 | 35 |
|
37 | | -const createPairingProvider = (): WalletConnectProvider => { |
| 36 | +// Note: this shares a lot of similarities with the patchedWalletConnect module |
| 37 | +const getPairingModule = (chainId: ChainId): WalletModule => { |
38 | 38 | const STORAGE_ID = 'SAFE__pairingProvider' |
39 | 39 | const clientMeta = getClientMeta() |
40 | 40 |
|
41 | | - // Successful pairing does not use chainId of provider but that of the pairee |
42 | | - // so we can use any chainId here |
43 | | - const provider = getWalletConnectProvider(_getChainId(), { |
44 | | - storageId: STORAGE_ID, |
45 | | - qrcode: false, // Don't show QR modal |
46 | | - clientMeta, |
47 | | - }) |
48 | | - |
49 | | - // WalletConnect overrides the clientMeta, so we need to set it back |
50 | | - ;(provider.wc as any).clientMeta = clientMeta |
51 | | - ;(provider.wc as any)._clientMeta = clientMeta |
52 | | - |
53 | | - return provider |
54 | | -} |
55 | | - |
56 | | -let _pairingProvider: WalletConnectProvider | undefined |
57 | | - |
58 | | -export const getPairingProvider = (): WalletConnectProvider => { |
59 | | - // We cannot initialize provider immediately as we need to wait for chains to load RPCs |
60 | | - if (!_pairingProvider) { |
61 | | - _pairingProvider = createPairingProvider() |
62 | | - } |
63 | | - return _pairingProvider |
64 | | -} |
65 | | - |
66 | | -// Note: this shares a lot of similarities with the patchedWalletConnect module |
67 | | -const getPairingModule = (): WalletModule => { |
68 | | - const name = PAIRING_MODULE_NAME |
69 | | - const provider = getPairingProvider() |
70 | | - |
71 | 41 | return { |
72 | | - name, |
| 42 | + name: PAIRING_MODULE_NAME, |
73 | 43 | wallet: async ({ resetWalletState }) => { |
74 | | - // Enable provider when a previously interrupted session exists |
75 | | - if (provider.wc.session.connected) { |
76 | | - provider.enable() |
| 44 | + const provider = getWalletConnectProvider(chainId, { |
| 45 | + storageId: STORAGE_ID, |
| 46 | + qrcode: false, // Don't show QR modal |
| 47 | + clientMeta, |
| 48 | + }) |
| 49 | + |
| 50 | + // WalletConnect overrides the clientMeta, so we need to set it back |
| 51 | + ;(provider.wc as any).clientMeta = clientMeta |
| 52 | + ;(provider.wc as any)._clientMeta = clientMeta |
| 53 | + |
| 54 | + const onDisconnect = () => { |
| 55 | + resetWalletState({ disconnected: true, walletName: PAIRING_MODULE_NAME }) |
77 | 56 | } |
78 | 57 |
|
79 | | - const onDisconnect = () => resetWalletState({ walletName: name, disconnected: true }) |
80 | 58 | provider.wc.on('disconnect', onDisconnect) |
81 | 59 |
|
82 | | - // Kill session if module unmounts (a non-pairing wallet connects) |
83 | 60 | window.addEventListener('unload', onDisconnect, { once: true }) |
84 | 61 |
|
| 62 | + // Establish WC connection |
| 63 | + provider.enable() |
| 64 | + |
85 | 65 | return { |
86 | 66 | provider, |
87 | 67 | interface: { |
88 | 68 | ...getWCWalletInterface(provider), |
89 | | - name, |
| 69 | + name: PAIRING_MODULE_NAME, |
90 | 70 | }, |
91 | 71 | } |
92 | 72 | }, |
|
0 commit comments