import { resolveZoneFileToProfile } from '@stacks/profile' ; import { fetchPrivate } from '@stacks/common' ; import { StacksNetwork , StacksMainnet } from '@stacks/network' ; export interface ProfileLookupOptions { username : string ; zoneFileLookupURL ? : string ; network ? : StacksNetwork ; } param { string } username param { string } zoneFileLookupURL returns { Promise } export function lookupProfile ( options ) { if ( ! options . username ) { return Promise . reject ( ) ; } const network = options . network ? options . network : new StacksMainnet ( ) ; let lookupPromise ; if ( options . zoneFileLookupURL ) { const url = `${ options . zoneFileLookupURL . replace ( /\/$/ , '' ) }/${ options . username }` ; lookupPromise = fetchPrivate ( url ) . then ( response => response . json ( ) ) ; } else { lookupPromise = network . getNameInfo ( options . username ) ; } return lookupPromise . then ( ( responseJSON ) => { if ( responseJSON . hasOwnProperty ( 'zonefile' ) && responseJSON . hasOwnProperty ( 'address' ) ) { return resolveZoneFileToProfile ( responseJSON . zonefile , responseJSON . address ) ; } else { throw new Error ( 'Invalid zonefile lookup response: did not contain `address`' + ' or `zonefile` field' ) ; } } ) ; }