Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface EnvironmentVariables {
readonly POAP_API_TOKEN: string
readonly REDIS_URL: string
readonly ENS_API_URL: string
readonly IPFS_GATEWAY_URL: string
readonly ARWEAVE_GATEWAY_URL: string
}

// Cloudflare Workers
Expand Down
27 changes: 22 additions & 5 deletions src/service/ens-metadata/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { database } from '#/database'
import { apiLogger } from '#/logger'
import type { Address, DB } from '#/types'
import type { Environment } from '#/types/index'
import { arrayToChunks, isAddress, raise } from '#/utilities.ts'
import { arrayToChunks, isAddress, raise, resolveDecentralizedURI } from '#/utilities.ts'
import type { ENSProfile } from './types'

export type ENSProfileResponse = ENSProfile & { type: 'error' | 'success' }
Expand All @@ -29,11 +29,13 @@ type Row = {
export class ENSMetadataService implements IENSMetadataService {
readonly #db: Kysely<DB>
readonly #url: string
readonly #gateways: { ipfs: string; arweave: string }

// biome-ignore lint/correctness/noUndeclaredVariables: <explanation>
constructor(env: Env) {
this.#db = database(env)
this.#url = env.ENS_API_URL
this.#gateways = { ipfs: env.IPFS_GATEWAY_URL, arweave: env.ARWEAVE_GATEWAY_URL }
}

async getAddress(ensNameOrAddress: Address | string): Promise<Address> {
Expand Down Expand Up @@ -118,7 +120,7 @@ export class ENSMetadataService implements IENSMetadataService {
})
} catch (_error) {}

return ensProfileData as ENSProfile
return this.#resolveRecordURIs(ensProfileData) as ENSProfile
} catch (error) {
console.log('error', error)
}
Expand Down Expand Up @@ -194,7 +196,7 @@ export class ENSMetadataService implements IENSMetadataService {
} catch (error) {
console.log('cache failed', error)
}
return ensProfileData as ENSProfile
return this.#resolveRecordURIs(ensProfileData) as ENSProfile
} catch (error) {
console.log('error', error)
}
Expand All @@ -206,7 +208,7 @@ export class ENSMetadataService implements IENSMetadataService {
formattedSecondTry.records = formattedSecondTry?.records
? (JSON.parse(formattedSecondTry?.records) as string)
: ''
return secondTry as ENSProfile
return this.#resolveRecordURIs(secondTry as ENSProfile)
}

return {
Expand All @@ -221,7 +223,7 @@ export class ENSMetadataService implements IENSMetadataService {
if (cachedProfile) {
returnedRecord.records = returnedRecord?.records ? (JSON.parse(returnedRecord?.records) as string) : ''
}
return returnedRecord as ENSProfile
return this.#resolveRecordURIs(returnedRecord)
}

/**
Expand Down Expand Up @@ -289,13 +291,15 @@ export class ENSMetadataService implements IENSMetadataService {
if (record.name) {
await this.cacheRecord(record)
record.records = JSON.parse(record?.records || '') as string
this.#resolveRecordURIs(record)
}
// record.records = JSON.parse(record?.records || '') as string;
}

for (const record of filteredCache) {
if (record.name) {
record.records = JSON.parse(record?.records || '') as string
this.#resolveRecordURIs(record)
}
}

Expand Down Expand Up @@ -334,6 +338,19 @@ export class ENSMetadataService implements IENSMetadataService {
}, {})
}

#resolveRecordURIs(profile: ENSProfile): ENSProfile {
if (profile.records && typeof profile.records === 'object') {
const records = profile.records as unknown as Record<string, unknown>
for (const key of Object.keys(records)) {
const value = records[key]
if (typeof value === 'string') {
records[key] = resolveDecentralizedURI(value, this.#gateways)
}
}
}
return profile
}

static #toTableRow(namedata: ENSProfile): {
name: string
address: string
Expand Down
16 changes: 16 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ export function arrayToChunks<T>(array: T[], chunkSize: number): T[][] {
return chunks
}

export function resolveDecentralizedURI(
uri: string | undefined | null,
gateways: { ipfs: string; arweave: string }
): string | undefined | null {
if (!uri) return uri
if (uri.startsWith('ipfs://')) {
const gateway = gateways.ipfs.endsWith('/') ? gateways.ipfs : `${gateways.ipfs}/`
return uri.replace('ipfs://', gateway)
}
if (uri.startsWith('ar://')) {
const gateway = gateways.arweave.endsWith('/') ? gateways.arweave : `${gateways.arweave}/`
return uri.replace('ar://', gateway)
}
return uri
}

// removed properties with undefined values from object
export function removeUndefined<T>(object: T): T {
return JSON.parse(JSON.stringify(object)) as T
Expand Down
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ placement = { mode = "smart" }
compatibility_date = "2023-10-30"
# end of globally inheritable configuration
#
vars = { ENVIRONMENT = "development" }
vars = { ENVIRONMENT = "development", IPFS_GATEWAY_URL = "https://ipfs.io/ipfs/", ARWEAVE_GATEWAY_URL = "https://arweave.net/" }
services = [{ binding = "ens", service = "ens" }]
kv_namespaces = [
{ binding = "EFP_DATA_CACHE", id = "5092581c2d524711a04560d335966a60", preview_id = "608971607bd2469e8972a0811f8de589" },
Expand Down
Loading