@@ -13,6 +13,7 @@ import {
1313
1414import { EnsController } from '../controllers/EnsController.js'
1515import type { OptionsControllerState } from '../controllers/OptionsController.js'
16+ import { matchNonWildcardPattern , matchWildcardPattern , parseUrl } from './UrlUtils.js'
1617
1718interface ListenWcProviderParams {
1819 universalProvider : UniversalProvider
@@ -70,9 +71,21 @@ export const WcHelpersUtil = {
7071 USER_REJECTED : 5000 ,
7172 USER_REJECTED_METHODS : 5002
7273 } ,
74+
75+ /**
76+ * Retrieves the array of supported methods for a given chain namespace.
77+ * @param chainNamespace - The chain namespace.
78+ * @returns An array of method strings.
79+ */
7380 getMethodsByChainNamespace ( chainNamespace : ChainNamespace ) : string [ ] {
7481 return DEFAULT_METHODS [ chainNamespace as keyof typeof DEFAULT_METHODS ] || [ ]
7582 } ,
83+
84+ /**
85+ * Creates a default WalletConnect namespace configuration for the given chain namespace.
86+ * @param chainNamespace - The chain namespace.
87+ * @returns The default Namespace object.
88+ */
7689 createDefaultNamespace ( chainNamespace : ChainNamespace ) : Namespace {
7790 return {
7891 methods : this . getMethodsByChainNamespace ( chainNamespace ) ,
@@ -82,6 +95,12 @@ export const WcHelpersUtil = {
8295 }
8396 } ,
8497
98+ /**
99+ * Applies overrides to the base WalletConnect NamespaceConfig.
100+ * @param baseNamespaces - The base namespace configuration.
101+ * @param overrides - Optional overrides for methods, chains, events, rpcMap.
102+ * @returns The resulting NamespaceConfig with overrides applied.
103+ */
85104 applyNamespaceOverrides (
86105 baseNamespaces : NamespaceConfig ,
87106 overrides ?: OptionsControllerState [ 'universalProviderConfigOverride' ]
@@ -170,6 +189,13 @@ export const WcHelpersUtil = {
170189 return result
171190 } ,
172191
192+ /**
193+ * Creates WalletConnect namespaces based on CAIP network definitions,
194+ * optionally applying custom overrides.
195+ * @param caipNetworks - Array of CaipNetwork definitions.
196+ * @param configOverride - Optional overrides for namespaces.
197+ * @returns The resulting NamespaceConfig.
198+ */
173199 createNamespaces (
174200 caipNetworks : CaipNetwork [ ] ,
175201 configOverride ?: OptionsControllerState [ 'universalProviderConfigOverride' ]
@@ -210,13 +236,25 @@ export const WcHelpersUtil = {
210236 return this . applyNamespaceOverrides ( defaultNamespaces , configOverride )
211237 } ,
212238
239+ /**
240+ * Resolves a Reown/ENS name to its first matching address across configured networks.
241+ * @param name - The ENS or Reown name to resolve.
242+ * @returns The resolved address as a string, or false if not found.
243+ */
213244 resolveReownName : async ( name : string ) => {
214245 const wcNameAddress = await EnsController . resolveName ( name )
215- const networkNameAddresses = Object . values ( wcNameAddress ?. addresses ) || [ ]
246+ const networkNameAddresses = wcNameAddress ?. addresses
247+ ? Object . values ( wcNameAddress . addresses )
248+ : [ ]
216249
217250 return networkNameAddresses [ 0 ] ?. address || false
218251 } ,
219252
253+ /**
254+ * Extracts all CAIP network IDs used in given WalletConnect namespaces.
255+ * @param namespaces - WalletConnect Namespaces object.
256+ * @returns Array of CAIP network IDs (chainNamespace:chainId).
257+ */
220258 getChainsFromNamespaces ( namespaces : SessionTypes . Namespaces = { } ) : CaipNetworkId [ ] {
221259 return Object . values ( namespaces ) . flatMap < CaipNetworkId > ( namespace => {
222260 const chains = ( namespace . chains || [ ] ) as CaipNetworkId [ ]
@@ -230,6 +268,11 @@ export const WcHelpersUtil = {
230268 } )
231269 } ,
232270
271+ /**
272+ * Type guard to check if an object is a WalletConnect session event data.
273+ * @param data - The data to check.
274+ * @returns True if data matches SessionEventData structure.
275+ */
233276 isSessionEventData ( data : unknown ) : data is WcHelpersUtil . SessionEventData {
234277 return (
235278 typeof data === 'object' &&
@@ -246,6 +289,11 @@ export const WcHelpersUtil = {
246289 )
247290 } ,
248291
292+ /**
293+ * Detects if an error object represents a user-rejected WalletConnect request.
294+ * @param error - The error object to check.
295+ * @returns True if user rejected request, otherwise false.
296+ */
249297 isUserRejectedRequestError ( error : unknown ) {
250298 try {
251299 if ( typeof error === 'object' && error !== null ) {
@@ -266,42 +314,54 @@ export const WcHelpersUtil = {
266314 }
267315 } ,
268316
317+ /**
318+ * Checks if a current origin is allowed by configured allowed and default origin patterns.
319+ * Localhost and 127.0.0.1 are always allowed.
320+ * @param currentOrigin - The current web origin.
321+ * @param allowedPatterns - Patterns from project configuration.
322+ * @param defaultAllowedOrigins - Built-in or default allowed patterns.
323+ * @returns True if the origin is allowed, false otherwise.
324+ */
269325 isOriginAllowed (
270326 currentOrigin : string ,
271327 allowedPatterns : string [ ] ,
272328 defaultAllowedOrigins : string [ ]
273329 ) : boolean {
274- for ( const pattern of [ ...allowedPatterns , ...defaultAllowedOrigins ] ) {
275- if ( pattern . includes ( '*' ) ) {
276- // Convert wildcard pattern to regex, escape special chars, replace *, match whole string
277- const escapedPattern = pattern . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / gu, '\\$&' )
278- const regexString = `^${ escapedPattern . replace ( / \\ \* / gu, '.*' ) } $`
279- const regex = new RegExp ( regexString , 'u' )
330+ const patterns = [ ...allowedPatterns , ...defaultAllowedOrigins ]
331+ // Spec: empty allowlist allows all origins
332+ if ( allowedPatterns . length === 0 ) {
333+ return true
334+ }
335+ // Parse current origin up-front
336+ const current = parseUrl ( currentOrigin )
337+ if ( ! current ) {
338+ // Legacy exact string equality when pattern has no wildcard
339+ return patterns . some ( pattern => ! pattern . includes ( '*' ) && pattern === currentOrigin )
340+ }
341+
342+ // Local development is always permitted
343+ if ( current . hostname === 'localhost' || current . hostname === '127.0.0.1' ) {
344+ return true
345+ }
280346
281- if ( regex . test ( currentOrigin ) ) {
347+ for ( const pattern of patterns ) {
348+ if ( pattern . includes ( '*' ) ) {
349+ if ( matchWildcardPattern ( current , currentOrigin , pattern ) ) {
282350 return true
283351 }
284- } else {
285- /**
286- * There are some cases where pattern is getting just the origin, where using new URL(pattern).origin will throw an error
287- * thus we a try catch to handle this case
288- */
289- try {
290- if ( new URL ( pattern ) . origin === currentOrigin ) {
291- return true
292- }
293- } catch ( e ) {
294- if ( pattern === currentOrigin ) {
295- return true
296- }
297- }
352+ // Keep checking remaining patterns
353+ } else if ( matchNonWildcardPattern ( currentOrigin , pattern ) ) {
354+ return true
298355 }
299356 }
300357
301- // No match found
302358 return false
303359 } ,
304360
361+ /**
362+ * Attaches event listeners to a UniversalProvider instance for WalletConnect events.
363+ * @param params - The listener parameters including handlers for connect, disconnect, etc.
364+ */
305365 listenWcProvider ( {
306366 universalProvider,
307367 namespace,
@@ -384,6 +444,12 @@ export const WcHelpersUtil = {
384444 }
385445 } ,
386446
447+ /**
448+ * Retrieves and parses the unique set of accounts for a given WalletConnect namespace.
449+ * @param universalProvider - The UniversalProvider instance.
450+ * @param namespace - The chain namespace to extract accounts for.
451+ * @returns Array of parsed CaipAddress objects.
452+ */
387453 getWalletConnectAccounts ( universalProvider : UniversalProvider , namespace : ChainNamespace ) {
388454 const accountsAdded = new Set < string > ( )
389455
0 commit comments