@@ -4,7 +4,7 @@ import { database } from '#/database'
44import { apiLogger } from '#/logger'
55import type { Address , DB } from '#/types'
66import type { Environment } from '#/types/index'
7- import { arrayToChunks , isAddress , raise } from '#/utilities.ts'
7+ import { arrayToChunks , isAddress , raise , resolveDecentralizedURI } from '#/utilities.ts'
88import type { ENSProfile } from './types'
99
1010export type ENSProfileResponse = ENSProfile & { type : 'error' | 'success' }
@@ -29,11 +29,13 @@ type Row = {
2929export class ENSMetadataService implements IENSMetadataService {
3030 readonly #db: Kysely < DB >
3131 readonly #url: string
32+ readonly #gateways: { ipfs : string ; arweave : string }
3233
3334 // biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
3435 constructor ( env : Env ) {
3536 this . #db = database ( env )
3637 this . #url = env . ENS_API_URL
38+ this . #gateways = { ipfs : env . IPFS_GATEWAY_URL , arweave : env . ARWEAVE_GATEWAY_URL }
3739 }
3840
3941 async getAddress ( ensNameOrAddress : Address | string ) : Promise < Address > {
@@ -118,7 +120,7 @@ export class ENSMetadataService implements IENSMetadataService {
118120 } )
119121 } catch ( _error ) { }
120122
121- return ensProfileData as ENSProfile
123+ return this . #resolveRecordURIs ( ensProfileData ) as ENSProfile
122124 } catch ( error ) {
123125 console . log ( 'error' , error )
124126 }
@@ -194,7 +196,7 @@ export class ENSMetadataService implements IENSMetadataService {
194196 } catch ( error ) {
195197 console . log ( 'cache failed' , error )
196198 }
197- return ensProfileData as ENSProfile
199+ return this . #resolveRecordURIs ( ensProfileData ) as ENSProfile
198200 } catch ( error ) {
199201 console . log ( 'error' , error )
200202 }
@@ -206,7 +208,7 @@ export class ENSMetadataService implements IENSMetadataService {
206208 formattedSecondTry . records = formattedSecondTry ?. records
207209 ? ( JSON . parse ( formattedSecondTry ?. records ) as string )
208210 : ''
209- return secondTry as ENSProfile
211+ return this . #resolveRecordURIs ( secondTry as ENSProfile )
210212 }
211213
212214 return {
@@ -221,7 +223,7 @@ export class ENSMetadataService implements IENSMetadataService {
221223 if ( cachedProfile ) {
222224 returnedRecord . records = returnedRecord ?. records ? ( JSON . parse ( returnedRecord ?. records ) as string ) : ''
223225 }
224- return returnedRecord as ENSProfile
226+ return this . #resolveRecordURIs ( returnedRecord )
225227 }
226228
227229 /**
@@ -289,13 +291,15 @@ export class ENSMetadataService implements IENSMetadataService {
289291 if ( record . name ) {
290292 await this . cacheRecord ( record )
291293 record . records = JSON . parse ( record ?. records || '' ) as string
294+ this . #resolveRecordURIs( record )
292295 }
293296 // record.records = JSON.parse(record?.records || '') as string;
294297 }
295298
296299 for ( const record of filteredCache ) {
297300 if ( record . name ) {
298301 record . records = JSON . parse ( record ?. records || '' ) as string
302+ this . #resolveRecordURIs( record )
299303 }
300304 }
301305
@@ -334,6 +338,19 @@ export class ENSMetadataService implements IENSMetadataService {
334338 } , { } )
335339 }
336340
341+ #resolveRecordURIs( profile : ENSProfile ) : ENSProfile {
342+ if ( profile . records && typeof profile . records === 'object' ) {
343+ const records = profile . records as unknown as Record < string , unknown >
344+ for ( const key of Object . keys ( records ) ) {
345+ const value = records [ key ]
346+ if ( typeof value === 'string' ) {
347+ records [ key ] = resolveDecentralizedURI ( value , this . #gateways)
348+ }
349+ }
350+ }
351+ return profile
352+ }
353+
337354 static #toTableRow( namedata : ENSProfile ) : {
338355 name : string
339356 address : string
0 commit comments