@1hive/connect-core
Version:
Access and interact with Aragon Organizations and their apps.
88 lines • 3.46 kB
JavaScript
import { isAddress } from '@ethersproject/address';
import ForwardingPathDescription, { decodeForwardingPath, describePath, describeTransaction, } from '../utils/descriptor/index';
import { ErrorInvalidLocation } from '../errors';
import { normalizeFiltersAndCallback, toArrayEntry } from '../utils/misc';
import { subscription } from '../utils/subscriptions';
function normalizeAppFilters(filters) {
var _a;
if (!filters) {
return {};
}
if (typeof filters === 'string') {
return isAddress(filters) ? { address: [filters] } : { name: [filters] };
}
if (Array.isArray(filters)) {
return isAddress((_a = filters[0]) !== null && _a !== void 0 ? _a : '')
? { 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 {
constructor(connection) {
this.connection = connection;
}
get location() {
return this.connection.orgLocation;
}
get address() {
return this.connection.orgAddress;
}
get _connection() {
return this.connection;
}
//////// ACCOUNT /////////
actAss(sender) {
this.connection.actAs = sender;
}
///////// APPS ///////////
async app(filters) {
return this.connection.orgConnector.appForOrg(this, normalizeAppFilters(filters));
}
onApp(filtersOrCallback, callback) {
const [filters, _callback] = normalizeFiltersAndCallback(filtersOrCallback, callback);
return subscription(_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(filtersOrCallback, callback);
return subscription(_callback, (callback) => this.connection.orgConnector.onAppsForOrg(this, normalizeAppFilters(filters), callback));
}
async acl() {
return this.app('acl');
}
async kernel() {
return this.app('kernel');
}
///////// PERMISSIONS ///////////
async permissions() {
return this.connection.orgConnector.permissionsForOrg(this);
}
onPermissions(callback) {
return subscription(callback, (callback) => this.connection.orgConnector.onPermissionsForOrg(this, callback));
}
//////// DESCRIPTIONS /////////
// Return a description of the forwarding path encoded on the evm script
async describeScript(script) {
const installedApps = await this.apps();
const describedSteps = await describePath(decodeForwardingPath(script), installedApps, this.connection.ethersProvider);
return new ForwardingPathDescription(describedSteps, installedApps);
}
// Try to describe a single transaction using Radspec on the context of the organization
async describeTransaction(transaction) {
return describeTransaction(transaction, await this.apps(), this.connection.ethersProvider);
}
}
//# sourceMappingURL=Organization.js.map