import { AragonArtifact , AragonManifest , IpfsResolver } from '../types' ; import { ErrorInvalid } from '../errors' ; import { getApmInternalAppInfo , getAragonOsInternalAppInfo , hasAppInfo , } from './overrides/index' ; export function parseMetadata ( name , metadata ) { try { return JSON . parse ( metadata ) ; } catch ( error ) { throw new ErrorInvalid ( `Can’t parse ${ name }: invalid JSON.` ) ; } } export async function fetchMetadata ( ipfs , fileName , contentUri ) { const cid = contentUri . match ( /ipfs:(.*)/ ) ?. [ 1 ] ; return cid ? ipfs . json ( cid , fileName ) : { } ; } export async function resolveMetadata ( ipfs , fileName , contentUri ? , metadata ? ) { if ( metadata ) { return parseMetadata ( fileName , metadata ) ; } if ( contentUri ) { return fetchMetadata ( ipfs , fileName , contentUri ) ; } return { } ; } export async function resolveManifest ( ipfs , data ) { return resolveMetadata ( ipfs , 'manifest.json' , data . contentUri , data . manifest ) ; } export async function resolveArtifact ( ipfs , data ) { if ( data . appId && hasAppInfo ( data . appId , 'apm' ) ) { return getApmInternalAppInfo ( data . appId ) ; } if ( data . appId && hasAppInfo ( data . appId , 'aragon' ) ) { return getAragonOsInternalAppInfo ( data . appId ) ; } return resolveMetadata ( ipfs , 'artifact.json' , data . contentUri , data . artifact ) ; }