import { BscConnector } from '@binance-chain/bsc-connector' import { hexlify } from '@ethersproject/bytes' import { Web3Provider } from '@ethersproject/providers' import { toUtf8Bytes } from '@ethersproject/strings' import { AbstractConnector } from '@web3-react/abstract-connector' import { InjectedConnector } from '@web3-react/injected-connector' import { WalletConnectConnector, WalletConnectConnectorArguments } from '@web3-react/walletconnect-connector' import { ChainId, NEXT_PUBLIC_CHAIN_ID } from '../configs' import getNodeUrl from './getRpcUrl' export enum ConnectorNames { Injected = 'injected', WalletConnect = 'walletconnect', BSC = 'bsc', Blocto = 'blocto', WalletLink = 'walletlink', } const POLLING_INTERVAL = 12000 const rpcUrl = getNodeUrl() const chainId = NEXT_PUBLIC_CHAIN_ID export const injected = new InjectedConnector({ supportedChainIds: [chainId] }) const walletconnect = new WalletConnectConnector({ rpc: { [chainId]: rpcUrl }, qrcode: true, pollingInterval: POLLING_INTERVAL, } as WalletConnectConnectorArguments) const bscConnector = new BscConnector({ supportedChainIds: [chainId] }) export const connectorsByName = { [ConnectorNames.Injected]: injected, [ConnectorNames.WalletConnect]: walletconnect, [ConnectorNames.BSC]: bscConnector, [ConnectorNames.Blocto]: async () => { const { BloctoConnector } = await import('@blocto/blocto-connector') return new BloctoConnector({ chainId, rpc: rpcUrl }) }, [ConnectorNames.WalletLink]: async () => { const { WalletLinkConnector } = await import('@web3-react/walletlink-connector') return new WalletLinkConnector({ url: rpcUrl, appName: 'PancakeSwap', appLogoUrl: 'https://pancakeswap.com/logo.png', supportedChainIds: [ChainId.MAINNET, ChainId.TESTNET], }) }, } as const export const getLibrary = (provider): Web3Provider => { const library = new Web3Provider(provider) library.pollingInterval = POLLING_INTERVAL return library } /** * BSC Wallet requires a different sign method * @see https://docs.binance.org/smart-chain/wallet/wallet_api.html#binancechainbnbsignaddress-string-message-string-promisepublickey-string-signature-string */ export const signMessage = async ( connector: AbstractConnector, provider: any, account: string, message: string, ): Promise => { if (window.BinanceChain && connector instanceof BscConnector) { const { signature } = await window.BinanceChain.bnbSign(account, message) return signature } /** * Wallet Connect does not sign the message correctly unless you use their method * @see https://github.com/WalletConnect/walletconnect-monorepo/issues/462 */ if (provider.provider?.wc) { const wcMessage = hexlify(toUtf8Bytes(message)) const signature = await provider.provider?.wc.signPersonalMessage([wcMessage, account]) return signature } return provider.getSigner(account).signMessage(message) }