import { Address , AppFilters , AppFiltersParam , SubscriptionCallback , SubscriptionResult , } from '@aragon/connect-types' ; import ForwardingPathDescription , { decodeForwardingPath , describePath , describeTransaction , } from '../utils/descriptor/index' ; import { ConnectionContext , PostProcessDescription } from '../types' ; import { ErrorInvalidLocation } from '../errors' ; import { isAddress } from '../utils/address' ; import { normalizeFiltersAndCallback , toArrayEntry } from '../utils/misc' ; import { subscription } from '../utils/subscriptions' ; import App from './App' ; import Permission from './Permission' ; import Transaction from './Transaction' ; type OnAppCallback = SubscriptionCallback < App | null > ; type OnAppsCallback = SubscriptionCallback < App [ ] > ; function normalizeAppFilters ( filters ? ) { if ( ! filters ) { return { } ; } if ( typeof filters === 'string' ) { return isAddress ( filters ) ? { address : [ filters ] } : { name : [ filters ] } ; } if ( Array . isArray ( filters ) ) { return isAddress ( filters [ 0 ] ?? '' ) ? { address : filters } : { name : filters } ; } if ( filters . address ) { const addresses = toArrayEntry ( filters . address ) ; if ( ! addresses . every ( isAddress ) ) { throw new ErrorInvalidLocation ( ) ; } return { address : addresses } ; } if ( filters . name ) { return { name : toArrayEntry ( filters . name ) } ; } return { } ; } export default class Organization { readonly connection : ConnectionContext ; constructor ( connection ) { this . connection = connection ; } get location ( ) { return this . connection . orgLocation ; } get address ( ) : Address { return this . connection . orgAddress ; } get _connection ( ) : ConnectionContext { return this . connection ; } actAss ( sender ) { this . connection . actAs = sender ; } async app ( filters ? ) { return this . connection . orgConnector . appForOrg ( this , normalizeAppFilters ( filters ) ) ; } onApp ( filtersOrCallback ? , callback ? ) { const [ filters , _callback ] = normalizeFiltersAndCallback < OnAppCallback , AppFiltersParam > ( filtersOrCallback , callback ) ; return subscription < App > ( _callback , ( callback ) => this . connection . orgConnector . onAppForOrg ( this , normalizeAppFilters ( filters ) , callback ) ) ; } async apps ( filters ? ) { return this . connection . orgConnector . appsForOrg ( this , normalizeAppFilters ( filters ) ) ; } onApps ( filtersOrCallback ? , callback ? ) { const [ filters , _callback ] = normalizeFiltersAndCallback < OnAppsCallback , AppFiltersParam > ( filtersOrCallback , callback ) ; return subscription < App [ ] > ( _callback , ( callback ) => this . connection . orgConnector . onAppsForOrg ( this , normalizeAppFilters ( filters ) , callback ) ) ; } async acl ( ) { return this . app ( 'acl' ) ; } async kernel ( ) { return this . app ( 'kernel' ) ; } async permissions ( ) { return this . connection . orgConnector . permissionsForOrg ( this ) ; } onPermissions ( callback ? ) { return subscription < Permission [ ] > ( callback , ( callback ) => this . connection . orgConnector . onPermissionsForOrg ( this , callback ) ) ; } async describeScript ( script ) { const installedApps = await this . apps ( ) ; const describedSteps = await describePath ( decodeForwardingPath ( script ) , installedApps , this . connection . ethersProvider ) ; return new ForwardingPathDescription ( describedSteps , installedApps ) ; } async describeTransaction ( transaction ) { return describeTransaction ( transaction , await this . apps ( ) , this . connection . ethersProvider ) ; } }