@@ -12,6 +12,7 @@ import {
1212 assert ,
1313 assertIsSemVerRange ,
1414 Duration ,
15+ gtRange ,
1516 inMilliseconds ,
1617 satisfiesVersionRange ,
1718} from '@metamask/utils' ;
@@ -39,6 +40,11 @@ type JsonSnapsRegistryUrl = {
3940 signature : string ;
4041} ;
4142
43+ export type ClientConfig = {
44+ type : 'extension' | 'mobile' ;
45+ version : SemVerVersion ;
46+ } ;
47+
4248export type JsonSnapsRegistryArgs = {
4349 messenger : SnapsRegistryMessenger ;
4450 state ?: SnapsRegistryState ;
@@ -47,6 +53,7 @@ export type JsonSnapsRegistryArgs = {
4753 recentFetchThreshold ?: number ;
4854 refetchOnAllowlistMiss ?: boolean ;
4955 publicKey ?: Hex ;
56+ clientConfig : ClientConfig ;
5057} ;
5158
5259export type GetResult = {
@@ -117,6 +124,8 @@ export class JsonSnapsRegistry extends BaseController<
117124
118125 readonly #publicKey: Hex ;
119126
127+ readonly #clientConfig: ClientConfig ;
128+
120129 readonly #fetchFunction: typeof fetch ;
121130
122131 readonly #recentFetchThreshold: number ;
@@ -133,6 +142,7 @@ export class JsonSnapsRegistry extends BaseController<
133142 signature : SNAP_REGISTRY_SIGNATURE_URL ,
134143 } ,
135144 publicKey = DEFAULT_PUBLIC_KEY ,
145+ clientConfig,
136146 fetchFunction = globalThis . fetch . bind ( undefined ) ,
137147 recentFetchThreshold = inMilliseconds ( 5 , Duration . Minute ) ,
138148 refetchOnAllowlistMiss = true ,
@@ -167,6 +177,7 @@ export class JsonSnapsRegistry extends BaseController<
167177 } ) ;
168178 this . #url = url ;
169179 this . #publicKey = publicKey ;
180+ this . #clientConfig = clientConfig ;
170181 this . #fetchFunction = fetchFunction ;
171182 this . #recentFetchThreshold = recentFetchThreshold ;
172183 this . #refetchOnAllowlistMiss = refetchOnAllowlistMiss ;
@@ -338,11 +349,20 @@ export class JsonSnapsRegistry extends BaseController<
338349 return versionRange ;
339350 }
340351
341- const targetVersion = getTargetVersion (
342- Object . keys ( versions ) as SemVerVersion [ ] ,
343- versionRange ,
352+ const compatibleVersions = Object . entries ( versions ) . reduce < SemVerVersion [ ] > (
353+ ( accumulator , [ version , metadata ] ) => {
354+ const clientRange = metadata . clientVersions ?. [ this . #clientConfig. type ] ;
355+ if ( ! clientRange || gtRange ( this . #clientConfig. version , clientRange ) ) {
356+ accumulator . push ( version as SemVerVersion ) ;
357+ }
358+
359+ return accumulator ;
360+ } ,
361+ [ ] ,
344362 ) ;
345363
364+ const targetVersion = getTargetVersion ( compatibleVersions , versionRange ) ;
365+
346366 if ( ! targetVersion && this . #refetchOnAllowlistMiss && ! refetch ) {
347367 await this . #triggerUpdate( ) ;
348368 return this . #resolveVersion( snapId , versionRange , true ) ;
0 commit comments