|
1 |
| -// Only for WallectConnect v2, v1 is not supported |
2 |
| -import { Wallet, WalletMetadata, WalletProvider, WalletProviderOptions } from '../types'; |
3 |
| -import { EthereumProvider } from '@walletconnect/ethereum-provider'; |
4 |
| - |
5 |
| -export interface WalletConnectConfig { |
6 |
| - projectId: string; |
7 |
| - showQrModal?: boolean; |
8 |
| -} |
9 |
| - |
10 |
| -export class WalletConnectProvider implements WalletProvider { |
11 |
| - metadata: WalletMetadata = { |
12 |
| - icon: 'https://docs.walletconnect.com/img/walletconnect-logo-black.svg#light-mode-only', |
13 |
| - name: 'WalletConnect', |
14 |
| - remark: 'Connect with mobile APP', |
15 |
| - app: { |
16 |
| - link: 'https://walletconnect.com/', |
17 |
| - }, |
18 |
| - group: 'Popular', |
19 |
| - }; |
20 |
| - |
21 |
| - private qrCodeLinkPromise: Promise<string> | undefined; |
22 |
| - private resolveQrCodeLink: ((uri: string) => void) | undefined; |
23 |
| - |
24 |
| - constructor(private config?: WalletConnectConfig) {} |
25 |
| - |
26 |
| - create = async (options?: WalletProviderOptions): Promise<Wallet> => { |
27 |
| - const { projectId, showQrModal = false } = this.config || {}; |
28 |
| - if (!projectId) { |
29 |
| - throw new Error('walletConnectProjectId is required'); |
30 |
| - } |
31 |
| - let chains = [1]; |
32 |
| - if (options?.chains && options?.chains?.length > 0) { |
33 |
| - chains = options.chains.map((chain) => chain.id as number); |
34 |
| - } |
35 |
| - // docs: https://docs.walletconnect.com/advanced/providers/ethereum |
36 |
| - const provider = await EthereumProvider.init({ |
37 |
| - projectId: projectId, |
38 |
| - chains: [chains[0]], |
39 |
| - optionalChains: chains, |
40 |
| - showQrModal, |
41 |
| - methods: ['eth_requestAccounts', 'eth_accounts'], |
42 |
| - events: [], |
43 |
| - }); |
44 |
| - |
45 |
| - provider.on('display_uri', (uri: string) => { |
46 |
| - this.resolveQrCodeLink?.(uri); |
47 |
| - }); |
48 |
| - |
49 |
| - provider.on('disconnect', () => { |
50 |
| - this.initQrCodePromise(); |
51 |
| - }); |
52 |
| - |
53 |
| - this.initQrCodePromise(); |
54 |
| - |
55 |
| - return { |
56 |
| - provider, |
57 |
| - ...this.metadata, |
58 |
| - getQrCode: async () => { |
59 |
| - if (!this.qrCodeLinkPromise) { |
60 |
| - throw new Error('WalletConnect is not initialized'); |
61 |
| - } |
62 |
| - const uri = await this.qrCodeLinkPromise; |
63 |
| - return { |
64 |
| - uri, |
65 |
| - }; |
66 |
| - }, |
67 |
| - }; |
68 |
| - }; |
69 |
| - |
70 |
| - hasBrowserExtensionInstalled = async (): Promise<boolean> => { |
71 |
| - // If showQrModal is true, it means use WalletConnet offcial modal, Think it's the same as installing extension |
72 |
| - return this.config?.showQrModal ?? false; |
73 |
| - }; |
74 |
| - |
75 |
| - private initQrCodePromise = () => { |
76 |
| - this.qrCodeLinkPromise = new Promise((resolve) => { |
77 |
| - this.resolveQrCodeLink = resolve; |
78 |
| - }); |
79 |
| - }; |
80 |
| -} |
| 1 | +import { WalletMetadata } from '../types'; |
| 2 | + |
| 3 | +export const metadata_WalletConnect: WalletMetadata = { |
| 4 | + icon: 'https://docs.walletconnect.com/img/walletconnect-logo-black.svg#light-mode-only', |
| 5 | + name: 'WalletConnect', |
| 6 | + remark: 'Connect with mobile APP', |
| 7 | + app: { |
| 8 | + link: 'https://walletconnect.com/', |
| 9 | + }, |
| 10 | + group: 'Popular', |
| 11 | +}; |
0 commit comments