'use strict' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import { FabricWalletRegistryEntry } from './FabricWalletRegistryEntry' ; import { FileConfigurations } from './FileConfigurations' ; import { FileRegistry } from './FileRegistry' ; import { FabricEnvironmentRegistryEntry , EnvironmentType , EnvironmentFlags } from './FabricEnvironmentRegistryEntry' ; import { FabricEnvironmentRegistry } from './FabricEnvironmentRegistry' ; import { MicrofabEnvironment } from '../environments/MicrofabEnvironment' ; export class FabricWalletRegistry extends FileRegistry < FabricWalletRegistryEntry > { public static instance ( ) { return FabricWalletRegistry . _instance ; } private static _instance : FabricWalletRegistry = new FabricWalletRegistry ( ) ; private constructor ( ) { super ( FileConfigurations . FABRIC_WALLETS ) ; } public async getAll ( showLocalFabric = true ) { const entries = await super . getAll ( ) ; let wallets = [ ] ; const localWallets = [ ] ; for ( const entry of entries ) { const envName = entry . fromEnvironment ; let environmentType ; if ( envName ) { const environment = await FabricEnvironmentRegistry . instance ( ) . get ( envName ) ; environmentType = environment . environmentType ; } if ( environmentType === EnvironmentType . LOCAL_MICROFAB_ENVIRONMENT ) { localWallets . push ( entry ) ; continue ; } wallets . push ( entry ) ; } if ( showLocalFabric && localWallets . length > 0 ) { wallets = [ ... localWallets , ... wallets ] ; } return wallets ; } public async get ( name , fromEnvironment ? ) { const entries = await this . getAll ( ) ; const entry = entries . find ( ( item ) => { if ( item . fromEnvironment && item . fromEnvironment === fromEnvironment ) { return item . name === name && item . fromEnvironment === fromEnvironment ; } else { if ( item . name === name ) { if ( item . environmentGroups ) { if ( item . environmentGroups . length && fromEnvironment ) { return item . environmentGroups . includes ( fromEnvironment ) ; } else { return item ; } } else { return item ; } } } } ) ; if ( ! entry && fromEnvironment ) { throw new Error ( `Entry "${ name }" from environment "${ fromEnvironment }" in registry "${ FileConfigurations . FABRIC_WALLETS }" does not exist` ) ; } else if ( ! entry && ! fromEnvironment ) { throw new Error ( `Entry "${ name }" in registry "${ FileConfigurations . FABRIC_WALLETS }" does not exist` ) ; } else { return entry ; } } public async getEntries ( ) { const normalEntries = await super . getEntries ( ) ; const otherEntries = [ ] ; const microfabEnvironmentEntries = await FabricEnvironmentRegistry . instance ( ) . getAll ( [ EnvironmentFlags . MICROFAB ] ) ; for ( const microfabEnvironmentEntry of microfabEnvironmentEntries ) { const environment = this . newMicrofabEnvironment ( microfabEnvironmentEntry . name , microfabEnvironmentEntry . environmentDirectory , microfabEnvironmentEntry . url ) ; const isAlive = await environment . isAlive ( ) ; if ( ! isAlive ) { continue ; } const walletEntries = await environment . getWalletsAndIdentities ( ) ; otherEntries . push ( ... walletEntries ) ; } return [ ... normalEntries , ... otherEntries ] . sort ( ( a , b ) : number => { const aName = a . displayName ? a . displayName : a . name ; const bName = b . displayName ? b . displayName : b . name ; return aName . localeCompare ( bName ) ; } ) ; } public async update ( entry ) { if ( entry . fromEnvironment ) { await fs . ensureDir ( entry . walletPath ) ; const entryPath = path . join ( entry . walletPath , this . FILE_NAME ) ; await fs . writeJSON ( entryPath , entry ) ; this . emit ( FileRegistry . EVENT_NAME , 'wallets' ) ; } else { await super . update ( entry ) ; } } public newMicrofabEnvironment ( name , directory , url ) { return new MicrofabEnvironment ( name , directory , url ) ; } }