@@ -7,46 +7,31 @@ interface ImportValidatorsBundledOptions {
77 shouldStore ?: boolean
88}
99
10- function normalizePath ( value : string ) {
11- return value . replace ( / \\ / g, '/' )
12- }
13-
14- function extractNetworkFromPath ( path : string ) {
15- const normalized = normalizePath ( path )
16- const match = normalized . match ( / (?: ^ | \/ ) p u b l i c \/ v a l i d a t o r s \/ ( [ ^ / ] + ) \/ [ ^ / ] + \. j s o n $ / )
17- return match ?. [ 1 ]
18- }
19-
2010export async function importValidatorsBundled ( nimiqNetwork ?: string , options : ImportValidatorsBundledOptions = { } ) : Result < ValidatorJSON [ ] > {
2111 if ( ! nimiqNetwork )
2212 return [ false , 'Nimiq network is required' , undefined ]
2313
2414 const { shouldStore = true } = options
25- const modules = import . meta. glob ( '../../public/validators/*/*.json' , { eager : true , import : 'default' } )
15+ const storage = useStorage ( 'assets:server:validators' )
16+ const keys = await storage . getKeys ( `${ nimiqNetwork } ` )
2617
2718 const validators : ValidatorJSON [ ] = [ ]
28- for ( const [ path , mod ] of Object . entries ( modules ) ) {
29- if ( path . endsWith ( '.example.json' ) )
19+ for ( const key of keys ) {
20+ if ( ! key . endsWith ( '.json' ) || key . endsWith ( '.example.json' ) )
3021 continue
3122
32- const network = extractNetworkFromPath ( path )
33- if ( ! network || network !== nimiqNetwork )
34- continue
35-
36- const data = ( mod as any ) ?. default ?? mod
23+ const data = await storage . getItem ( key )
3724 const parsed = validatorSchema . safeParse ( data )
38- if ( ! parsed . success ) {
39- return [ false , `Invalid validator data at ${ path } : ${ parsed . error } ` , undefined ]
40- }
25+ if ( ! parsed . success )
26+ return [ false , `Invalid validator data at ${ key } : ${ parsed . error } ` , undefined ]
4127 validators . push ( parsed . data )
4228 }
4329
4430 if ( ! shouldStore )
4531 return [ true , undefined , validators ]
4632
47- if ( validators . length === 0 ) {
33+ if ( validators . length === 0 )
4834 return [ false , `No bundled validators found for network: ${ nimiqNetwork } ` , undefined ]
49- }
5035
5136 const results = await Promise . allSettled ( validators . map ( v => storeValidator ( v . address , v , { upsert : true } ) ) )
5237 const failures = results . filter ( r => r . status === 'rejected' )
0 commit comments