1
1
import {
2
2
ComethWallet ,
3
3
ConnectAdaptor ,
4
+ SupportedNetworks ,
4
5
UIConfig ,
5
6
webAuthnOptions
6
7
} from '@cometh/connect-sdk'
@@ -36,16 +37,20 @@ export type ComethConnectorOptions = WagmiConfigConnectorParams & {
36
37
shimDisconnect ?: boolean
37
38
}
38
39
40
+ type ConnectFunctionConfig = {
41
+ /** Target chain to connect to. */
42
+ chainId ?: number
43
+ }
44
+
39
45
export class ComethConnectConnector extends Connector <
40
46
undefined ,
41
47
ComethConnectorOptions
42
48
> {
43
49
id = 'cometh-connect'
44
50
name = 'Cometh Connect'
45
- ready = true
46
- wallet : ComethWallet
47
- client : ConnectClient
48
- walletAddress ?: string
51
+ ready = false
52
+ wallet ?: ComethWallet
53
+ client ?: ConnectClient
49
54
50
55
protected shimDisconnectKey = `${ this . id } .shimDisconnect`
51
56
@@ -56,24 +61,21 @@ export class ComethConnectConnector extends Connector<
56
61
chains : Chain [ ]
57
62
options : WagmiConfigConnectorParams
58
63
} ) {
59
- if ( chains . length !== 1 )
60
- throw new Error ( 'Cometh Connect does not support multi network in config' )
61
-
62
64
const options = {
63
65
shimDisconnect : true ,
64
66
...options_
65
67
}
66
68
67
- chains . forEach ( ( chain ) => {
68
- if ( ! isSupportedNetwork ( toHex ( chain . id ) ) )
69
- console . warn ( `${ chain . name } not yet supported by cometh connect` )
70
- } )
71
-
72
69
super ( {
73
- chains : chains . filter ( ( chain ) => isSupportedNetwork ( toHex ( chain . id ) ) ) ,
70
+ chains,
74
71
options
75
72
} )
76
73
74
+ this . ready = true
75
+ }
76
+
77
+ /* eslint-disable */
78
+ async connect ( { chainId } : ConnectFunctionConfig = { } ) {
77
79
const {
78
80
apiKey,
79
81
walletAddress,
@@ -86,39 +88,30 @@ export class ComethConnectConnector extends Connector<
86
88
rpcUrl
87
89
} = this . options
88
90
89
- const chainId = toHex ( this . chains [ 0 ] . id )
90
-
91
- if ( isSupportedNetwork ( chainId ) ) {
92
- this . wallet = new ComethWallet ( {
93
- authAdapter : new ConnectAdaptor ( {
94
- chainId,
95
- apiKey,
96
- disableEoaFallback,
97
- encryptionSalt,
98
- webAuthnOptions,
99
- passKeyName,
100
- rpcUrl,
101
- baseUrl
102
- } ) ,
91
+ const selectedChainId = this . _getSelectedChainId ( chainId )
92
+
93
+ this . wallet = new ComethWallet ( {
94
+ authAdapter : new ConnectAdaptor ( {
95
+ chainId : selectedChainId ,
103
96
apiKey,
104
- uiConfig,
97
+ disableEoaFallback,
98
+ encryptionSalt,
99
+ webAuthnOptions,
100
+ passKeyName,
105
101
rpcUrl,
106
102
baseUrl
107
- } )
103
+ } ) ,
104
+ apiKey,
105
+ uiConfig,
106
+ rpcUrl,
107
+ baseUrl
108
+ } )
108
109
109
- this . client = getConnectViemClient ( { wallet : this . wallet , apiKey } )
110
- this . walletAddress = walletAddress
111
- this . ready = true
112
- } else {
113
- throw new Error ( 'Network not supported' )
114
- }
115
- }
110
+ this . client = getConnectViemClient ( { wallet : this . wallet , apiKey } )
116
111
117
- /* eslint-disable */
118
- async connect ( ) {
119
- if ( this . walletAddress ) {
120
- await this . wallet . connect ( this . walletAddress )
121
- window . localStorage . setItem ( 'walletAddress' , this . walletAddress )
112
+ if ( walletAddress ) {
113
+ await this . wallet . connect ( walletAddress )
114
+ window . localStorage . setItem ( 'walletAddress' , walletAddress )
122
115
} else {
123
116
const localStorageAddress = window . localStorage . getItem ( 'walletAddress' )
124
117
@@ -133,10 +126,7 @@ export class ComethConnectConnector extends Connector<
133
126
if ( this . options . shimDisconnect )
134
127
this . storage ?. setItem ( this . shimDisconnectKey , true )
135
128
136
- console . warn ( `Connected to ${ this . chains [ 0 ] . name } ` )
137
-
138
- if ( this . chains . length > 1 )
139
- console . warn ( `Cometh connect does not support multichain` )
129
+ console . warn ( `Connected to ${ selectedChainId } ` )
140
130
141
131
return {
142
132
account : this . wallet . getAddress ( ) as `0x${string } `,
@@ -146,18 +136,55 @@ export class ComethConnectConnector extends Connector<
146
136
}
147
137
}
148
138
}
139
+
140
+ private _getSelectedChainId ( chainId ?: number ) {
141
+ if ( chainId ) {
142
+ const providedChainId = toHex ( chainId )
143
+
144
+ if ( ! isSupportedNetwork ( providedChainId ) )
145
+ throw new Error ( 'Network not supported' )
146
+
147
+ return providedChainId
148
+ } else {
149
+ const supportedChains = this . chains . reduce (
150
+ ( chains : SupportedNetworks [ ] , chain : Chain ) => {
151
+ const chainId = toHex ( chain . id )
152
+ if ( ! isSupportedNetwork ( chainId ) ) {
153
+ console . warn ( `${ chain . name } not yet supported by cometh connect` )
154
+ } else {
155
+ chains . push ( chainId )
156
+ }
157
+ return chains
158
+ } ,
159
+ [ ]
160
+ )
161
+ if ( supportedChains . length === 0 )
162
+ throw new Error ( 'Cometh Connect does not support the provided chains' )
163
+
164
+ if ( supportedChains . length > 1 )
165
+ console . warn ( `Cometh connect does not support multichain` )
166
+
167
+ return supportedChains [ 0 ]
168
+ }
169
+ }
149
170
disconnect ( ) : Promise < void > {
171
+ if ( ! this . wallet ) throw new Error ( 'no' )
150
172
// Remove shim signalling wallet is disconnected
151
173
if ( this . options . shimDisconnect )
152
174
this . storage ?. removeItem ( this . shimDisconnectKey )
153
175
154
176
return this . wallet . logout ( )
155
177
}
156
178
async getAccount ( ) : Promise < `0x${string } `> {
179
+ if ( ! this . wallet ) throw new Error ( 'no' )
157
180
return this . wallet . getAddress ( ) as `0x${string } `
158
181
}
159
182
async getChainId ( ) : Promise < number > {
160
- return await this . wallet . chainId
183
+ if ( ! this . wallet ) {
184
+ return this . chains [ 0 ] . id
185
+ } else {
186
+ return await this . wallet . chainId
187
+ }
161
188
}
162
189
/* eslint-disable */
163
190
async getProvider ( ) : Promise < any > {
0 commit comments