-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathOptionsController.ts
343 lines (301 loc) · 10.2 KB
/
OptionsController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { subscribeKey as subKey } from 'valtio/vanilla/utils'
import { proxy, snapshot } from 'valtio/vanilla'
import type {
ConnectMethod,
CustomWallet,
DefaultAccountTypes,
Features,
Metadata,
ProjectId,
SdkVersion,
SocialProvider,
Tokens,
WalletFeature
} from '../utils/TypeUtil.js'
import { ConstantsUtil } from '../utils/ConstantsUtil.js'
import type { SIWXConfig } from '../utils/SIWXUtil.js'
// -- Types --------------------------------------------- //
export interface OptionsControllerStatePublic {
/**
* A boolean that allows you to add or remove the "All Wallets" button on the modal
* @default 'SHOW'
* @see https://docs.reown.com/appkit/react/core/options#allwallets
*/
allWallets?: 'SHOW' | 'HIDE' | 'ONLY_MOBILE'
/**
* The project ID for the AppKit. You can find or create your project ID in the Cloud.
* @see https://cloud.walletconnect.com/
*/
projectId: ProjectId
/**
* Array of wallet ids to be shown in the modal's connection view with priority. These wallets will also show up first in `All Wallets` view
* @default []
* @see https://docs.reown.com/appkit/react/core/options#featuredwalletids
*/
featuredWalletIds?: string[]
/**
* Array of wallet ids to be shown (order is respected). Unlike `featuredWalletIds`, these wallets will be the only ones shown in `All Wallets` view and as recommended wallets.
* @default []
* @see https://docs.reown.com/appkit/react/core/options#includewalletids
*/
includeWalletIds?: string[]
/**
* Array of wallet ids to be excluded from the wallet list in the modal.
* @default []
* @see https://docs.reown.com/appkit/react/core/options#excludewalletids
*/
excludeWalletIds?: string[]
/**
* Array of tokens to show the user's balance of. Each key represents the chain id of the token's blockchain
* @default {}
* @see https://docs.reown.com/appkit/react/core/options#tokens
*/
tokens?: Tokens
/**
* Add custom wallets to the modal. CustomWallets is an array of objects, where each object contains specific information of a custom wallet.
* @default []
* @see https://docs.reown.com/appkit/react/core/options#customwallets
*
*/
customWallets?: CustomWallet[]
/**
* You can add an url for the terms and conditions link.
* @default undefined
*/
termsConditionsUrl?: string
/**
* You can add an url for the privacy policy link.
* @default undefined
*/
privacyPolicyUrl?: string
/**
* Set of fields that related to your project which will be used to populate the metadata of the modal.
* @default {}
*/
metadata?: Metadata
/**
* Enable or disable the appending the AppKit to the DOM. Created for specific use cases like WebGL.
* @default false
*/
disableAppend?: boolean
/**
* Enable or disable the all the wallet options (injected, Coinbase, QR, etc.). This is useful if you want to use only email and socials.
* @default true
*/
enableWallets?: boolean
/**
* Enable or disable the EIP6963 feature in your AppKit.
* @default false
*/
enableEIP6963?: boolean
/**
* Enable or disable the Coinbase wallet in your AppKit.
* @default true
*/
enableCoinbase?: boolean
/**
* Enable or disable the Injected wallet in your AppKit.
* @default true
*/
enableInjected?: boolean
/**
* Enable or disable the WalletConnect QR code in your AppKit.
* @default true
*/
enableWalletConnect?: boolean
/**
* Enable or disable the wallet guide footer in AppKit if you have email or social login configured.
* @default true
*/
enableWalletGuide?: boolean
/**
* Enable or disable logs from email/social login.
* @default true
*/
enableAuthLogger?: boolean
/**
* Enable or disable debug mode in your AppKit. This is useful if you want to see UI alerts when debugging.
* @default true
*/
debug?: boolean
/**
* Features configuration object.
* @default { swaps: true, onramp: true, email: true, socials: ['google', 'x', 'discord', 'farcaster', 'github', 'apple', 'facebook'], history: true, analytics: true, allWallets: true }
* @see https://docs.reown.com/appkit/react/core/options#features
*/
features?: Features
/**
* @experimental - This feature is not production ready.
* Enable Sign In With X (SIWX) feature in your AppKit.
* @default undefined
*/
siwx?: SIWXConfig
/**
* Renders the AppKit to DOM instead of the default modal.
* @default false
*/
enableEmbedded?: boolean
/**
* Allow users to switch to an unsupported chain.
* @default false
*/
allowUnsupportedChain?: boolean
/**
* Default account types for each namespace.
* @default "{ bip122: 'payment', eip155: 'smartAccount', polkadot: 'eoa', solana: 'eoa' }"
*/
defaultAccountTypes: DefaultAccountTypes
}
export interface OptionsControllerStateInternal {
sdkType: 'appkit'
sdkVersion: SdkVersion
isSiweEnabled?: boolean
isUniversalProvider?: boolean
hasMultipleAddresses?: boolean
useInjectedUniversalProvider?: boolean
}
type StateKey = keyof OptionsControllerStatePublic | keyof OptionsControllerStateInternal
type OptionsControllerState = OptionsControllerStatePublic & OptionsControllerStateInternal
// -- State --------------------------------------------- //
const state = proxy<OptionsControllerState & OptionsControllerStateInternal>({
features: ConstantsUtil.DEFAULT_FEATURES,
projectId: '',
sdkType: 'appkit',
sdkVersion: 'html-wagmi-undefined',
defaultAccountTypes: ConstantsUtil.DEFAULT_ACCOUNT_TYPES
})
// -- Controller ---------------------------------------- //
export const OptionsController = {
state,
subscribeKey<K extends StateKey>(key: K, callback: (value: OptionsControllerState[K]) => void) {
return subKey(state, key, callback)
},
setOptions(options: OptionsControllerState) {
Object.assign(state, options)
},
setFeatures(features: OptionsControllerState['features'] | undefined) {
if (!features) {
return
}
if (!state.features) {
state.features = ConstantsUtil.DEFAULT_FEATURES
return
}
const newFeatures = { ...state.features, ...features }
state.features = newFeatures
},
setProjectId(projectId: OptionsControllerState['projectId']) {
state.projectId = projectId
},
setAllWallets(allWallets: OptionsControllerState['allWallets']) {
state.allWallets = allWallets
},
setIncludeWalletIds(includeWalletIds: OptionsControllerState['includeWalletIds']) {
state.includeWalletIds = includeWalletIds
},
setExcludeWalletIds(excludeWalletIds: OptionsControllerState['excludeWalletIds']) {
state.excludeWalletIds = excludeWalletIds
},
setFeaturedWalletIds(featuredWalletIds: OptionsControllerState['featuredWalletIds']) {
state.featuredWalletIds = featuredWalletIds
},
setTokens(tokens: OptionsControllerState['tokens']) {
state.tokens = tokens
},
setTermsConditionsUrl(termsConditionsUrl: OptionsControllerState['termsConditionsUrl']) {
state.termsConditionsUrl = termsConditionsUrl
},
setPrivacyPolicyUrl(privacyPolicyUrl: OptionsControllerState['privacyPolicyUrl']) {
state.privacyPolicyUrl = privacyPolicyUrl
},
setCustomWallets(customWallets: OptionsControllerState['customWallets']) {
state.customWallets = customWallets
},
setIsSiweEnabled(isSiweEnabled: OptionsControllerState['isSiweEnabled']) {
state.isSiweEnabled = isSiweEnabled
},
setIsUniversalProvider(isUniversalProvider: OptionsControllerState['isUniversalProvider']) {
state.isUniversalProvider = isUniversalProvider
},
setSdkVersion(sdkVersion: OptionsControllerState['sdkVersion']) {
state.sdkVersion = sdkVersion
},
setMetadata(metadata: OptionsControllerState['metadata']) {
state.metadata = metadata
},
setDisableAppend(disableAppend: OptionsControllerState['disableAppend']) {
state.disableAppend = disableAppend
},
setEIP6963Enabled(enableEIP6963: OptionsControllerState['enableEIP6963']) {
state.enableEIP6963 = enableEIP6963
},
setDebug(debug: OptionsControllerState['debug']) {
state.debug = debug
},
setEnableWalletConnect(enableWalletConnect: OptionsControllerState['enableWalletConnect']) {
state.enableWalletConnect = enableWalletConnect
},
setEnableWalletGuide(enableWalletGuide: OptionsControllerState['enableWalletGuide']) {
state.enableWalletGuide = enableWalletGuide
},
setEnableAuthLogger(enableAuthLogger: OptionsControllerState['enableAuthLogger']) {
state.enableAuthLogger = enableAuthLogger
},
setEnableWallets(enableWallets: OptionsControllerState['enableWallets']) {
state.enableWallets = enableWallets
},
setHasMultipleAddresses(hasMultipleAddresses: OptionsControllerState['hasMultipleAddresses']) {
state.hasMultipleAddresses = hasMultipleAddresses
},
setSIWX(siwx: OptionsControllerState['siwx']) {
state.siwx = siwx
},
setConnectMethodsOrder(connectMethodsOrder: ConnectMethod[]) {
state.features = {
...state.features,
connectMethodsOrder
}
},
setWalletFeaturesOrder(walletFeaturesOrder: WalletFeature[]) {
state.features = {
...state.features,
walletFeaturesOrder
}
},
setSocialsOrder(socialsOrder: SocialProvider[]) {
state.features = {
...state.features,
socials: socialsOrder
}
},
setCollapseWallets(collapseWallets: boolean) {
state.features = {
...state.features,
collapseWallets
}
},
setEnableEmbedded(enableEmbedded: OptionsControllerState['enableEmbedded']) {
state.enableEmbedded = enableEmbedded
},
setAllowUnsupportedChain(allowUnsupportedChain: OptionsControllerState['allowUnsupportedChain']) {
state.allowUnsupportedChain = allowUnsupportedChain
},
setUsingInjectedUniversalProvider(
useInjectedUniversalProvider: OptionsControllerState['useInjectedUniversalProvider']
) {
state.useInjectedUniversalProvider = useInjectedUniversalProvider
},
setDefaultAccountTypes(
defaultAccountType: Partial<OptionsControllerState['defaultAccountTypes']> = {}
) {
Object.entries(defaultAccountType).forEach(([namespace, accountType]) => {
if (accountType) {
// @ts-expect-error - Keys are validated by the param type
state.defaultAccountTypes[namespace] = accountType
}
})
},
getSnapshot() {
return snapshot(state)
}
}