import type { Manager } from '@3id/manager' ; import { DIDDataStore } from '@glazed/did-datastore' ; import type { BasicProfile , ImageMetadata , ImageSources } from '@datamodels/identity-profile-basic' ; import { AccountID } from 'caip' ; import { IPFS_PREFIX , IPFS_URL } from '../constants' ; import type { DIDsData } from '../types' ; import { ceramic } from './ceramic' ; export type Dimensions = { height : number ; width : number ; } ; export type SizeMode = 'contain' | 'cover' ; export async function loadProfile ( did , dataStore ) { try { return await dataStore . get ( 'basicProfile' , did ) ; } catch ( err ) { return null ; } } export function formatDID ( did ) { return did . length <= 20 ? did : `${ did . slice ( 0 , 10 ) }…${ did . slice ( - 5 ) }` ; } export function longFormatDID ( did ) { return `${ did . slice ( 0 , 10 ) }…${ did . slice ( - 9 ) }` ; } function selectCover ( options , { height , width } ) { let selected = null ; for ( const option of options ) { if ( option . height >= height && option . width >= width && ( selected === null || ( selected . size != null && option . size != null && option . size < selected . size ) || option . height * option . width < selected . height * selected . width ) ) { selected = option ; } } return selected ; } function selectContain ( options , { height , width } ) { let selected = null ; for ( const option of options ) { if ( option . height <= height && option . width <= width && ( selected === null || ( selected . size != null && option . size != null && option . size < selected . size ) || option . height * option . width > selected . height * selected . width ) ) { selected = option ; } } return selected ; } export function selectImageSource ( sources , dimensions , mode = 'cover' ) { let alternative = null ; if ( Array . isArray ( sources . alternatives ) ) { alternative = mode === 'cover' ? selectCover ( sources . alternatives , dimensions ) : selectContain ( sources . alternatives , dimensions ) ; } return alternative ?? sources . original ; } export function toImageSrc ( image ) { return image . src . replace ( IPFS_PREFIX , IPFS_URL ) ; } export function getImageSrc ( sources , dimensions , mode ? ) { return toImageSrc ( selectImageSource ( sources , dimensions , mode ) ) ; } export async function getDIDsData ( manager ) { const dataStore = manager . dataStore ; const dids = ( await manager . listDIDS ( ) ) ?? [ ] ; const entries = await Promise . all ( dids . map ( async ( did ) => { const accountsObj = await dataStore . get ( 'cryptoAccounts' , did ) ; const accounts = accountsObj ? Object . keys ( accountsObj ) : [ ] ; return { did , accounts : accounts . map ( ( account ) => new AccountID ( account ) ) , profile : await loadProfile ( did , dataStore ) , } ; } ) ) ; return entries . reduce ( ( acc , { did , ... entry } ) => { acc [ did ] = entry ; return acc ; } , { } as DIDsData ) ; }