1+ /**
2+ * @author github.com/tintinweb
3+ * @license MIT
4+ *
5+ * */
6+ 'use strict'
17const Web3 = require ( 'web3' )
28const Aragon = require ( '@aragon/wrapper' ) . default
39const RxOp = require ( 'rxjs/operators' )
@@ -12,63 +18,104 @@ const ENS_REGISTRIES = {
1218}
1319const IPFS_GATEWAY_URL = 'https://ipfs.eth.aragon.network/ipfs'
1420
15- async function getPermissions ( daoAddress , chainId ) {
16- const dao = new Aragon ( daoAddress , {
17- provider : new Web3 . providers . WebsocketProvider ( ETH_PROVIDERS [ chainId ] ) ,
18- apm : {
19- ipfs : {
20- gateway : IPFS_GATEWAY_URL ,
21+ class RemoteDao {
22+
23+ constructor ( daoAddress , chainId ) {
24+ this . daoAddress = daoAddress
25+ this . chainId = chainId
26+ }
27+
28+ async load ( ) {
29+ const dao = new Aragon ( this . daoAddress , {
30+ provider : new Web3 . providers . WebsocketProvider ( ETH_PROVIDERS [ this . chainId ] ) ,
31+ apm : {
32+ ipfs : {
33+ gateway : IPFS_GATEWAY_URL ,
34+ } ,
35+ ensRegistryAddress : ENS_REGISTRIES [ this . chainId ] ,
2136 } ,
22- ensRegistryAddress : ENS_REGISTRIES [ chainId ] ,
23- } ,
24- } )
25- // Temporarily replace console.info
26- // to hide 'Redefining LocalForage driver: memoryStorageDriver' message
27- const consoleInfo = console . info
28- console . info = ( ) => { }
29- await dao . init ( )
30- console . info = consoleInfo
37+ } )
38+ // Temporarily replace console.info
39+ // to hide 'Redefining LocalForage driver: memoryStorageDriver' message
40+ const consoleInfo = console . info
41+ console . info = ( ) => { }
42+ await dao . init ( )
43+ console . info = consoleInfo
44+
45+ this . apps = await dao . apps
46+ . pipe ( RxOp . takeWhile ( ( apps ) => apps . length <= 1 , true ) )
47+ . toPromise ( )
48+
49+ this . permissions = await dao . permissions
50+ . pipe ( RxOp . takeWhile ( ( permissions ) => permissions . length <= 1 , true ) )
51+ . toPromise ( )
52+
53+ return this
54+ }
3155
32- // Get apps
33- const apps = await dao . apps
34- . pipe ( RxOp . takeWhile ( ( apps ) => apps . length <= 1 , true ) )
35- . toPromise ( )
36- const getAppName = ( address ) => {
37- const app = apps . find ( ( app ) => {
38- return address . toLowerCase ( ) === app . proxyAddress
56+ getApps ( ) {
57+ let apps = this . apps . map ( ( app ) => {
58+ return { ref : `${ app . name } ${ app . proxyAddress . toLowerCase ( ) . substring ( 0 , 6 ) } ` , type : app . name }
3959 } )
40- const name = app ? app . name : 'Unknown'
41- return `${ name } ${ address . toLowerCase ( ) . substring ( 0 , 6 ) } `
60+ let appNamesRefs = apps . map ( app => app . ref )
61+
62+ for ( let p of this . getPermissions ( ) ) {
63+ if ( appNamesRefs . indexOf ( p . app ) < 0 ) {
64+ apps . push ( { ref : p . app , type : '__actor__' } )
65+ appNamesRefs . push ( p . app )
66+ }
67+ if ( appNamesRefs . indexOf ( p . grantee ) < 0 ) {
68+ apps . push ( { ref : p . grantee , type : '__actor__' } )
69+ appNamesRefs . push ( p . grantee )
70+ }
71+ if ( appNamesRefs . indexOf ( p . manager ) < 0 ) {
72+ apps . push ( { ref : p . manager , type : '__actor__' } )
73+ appNamesRefs . push ( p . manager )
74+ }
75+ }
76+ return apps
4277 }
4378
44- // Get roles
45- const allRoles = { }
46- apps . forEach ( ( app ) => {
47- app . roles . forEach ( ( role ) => {
48- allRoles [ role . bytes ] = role . id
79+ getPermissions ( ) {
80+ // Get apps
81+ const apps = this . apps
82+ const getAppName = ( address ) => {
83+ const app = apps . find ( ( app ) => {
84+ return address . toLowerCase ( ) === app . proxyAddress . toLowerCase ( )
85+ } )
86+ const name = app ? app . name : 'Unknown'
87+ return `${ name } ${ address . toLowerCase ( ) . substring ( 0 , 6 ) } `
88+ }
89+
90+ // Get roles
91+ const allRoles = { }
92+ apps . forEach ( ( app ) => {
93+ app . roles . forEach ( ( role ) => {
94+ allRoles [ role . bytes ] = role . id
95+ } )
4996 } )
50- } )
5197
52- // Get permissions
53- const permissions = await dao . permissions
54- . pipe ( RxOp . takeWhile ( ( permissions ) => permissions . length <= 1 , true ) )
55- . toPromise ( )
98+ // Get permissions
99+ const permissions = this . permissions
56100
57- const output = [ ]
58- for ( const [ app , roles ] of Object . entries ( permissions ) ) {
59- for ( const [ roleHash , data ] of Object . entries ( roles ) ) {
60- const roleId = allRoles [ roleHash ] || 'Unknown'
61- data . allowedEntities . forEach ( ( entity ) => {
62- output . push ( {
63- app : getAppName ( app ) ,
64- role : roleId ,
65- grantee : getAppName ( entity ) ,
66- manager : getAppName ( data . manager ) ,
101+ const output = [ ]
102+ for ( const [ app , roles ] of Object . entries ( permissions ) ) {
103+ for ( const [ roleHash , data ] of Object . entries ( roles ) ) {
104+ const roleId = allRoles [ roleHash ] || 'Unknown'
105+ data . allowedEntities . forEach ( ( entity ) => {
106+ output . push ( {
107+ app : getAppName ( app ) ,
108+ role : roleId ,
109+ grantee : getAppName ( entity ) ,
110+ manager : getAppName ( data . manager ) ,
111+ } )
67112 } )
68- } )
113+ }
69114 }
115+ return output
70116 }
71- return output
117+
118+
72119}
73120
74- module . exports . getPermissions = getPermissions
121+ module . exports . RemoteDao = RemoteDao
0 commit comments