Skip to content

Commit

Permalink
Merge pull request #93 from aragon/accounts
Browse files Browse the repository at this point in the history
wrapper: Allow wrapper to set accounts manually
  • Loading branch information
onbjerg committed Mar 18, 2018
2 parents a6f43e3 + d503244 commit 91274b5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/aragon-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class AppProxy {
this.rpc = new Messenger(provider)
}

/**
* Get an array of the accounts the user currently controls over time.
*
* @return {Observable}
*/
accounts () {
return this.rpc.sendAndObserveResponses(
'accounts'
).pluck('result')
}

/**
* Set the app identifier.
*
Expand Down
39 changes: 35 additions & 4 deletions packages/aragon-wrapper/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Externals
import { Subject, BehaviorSubject, Observable } from 'rxjs/Rx'
import { ReplaySubject, Subject, BehaviorSubject, Observable } from 'rxjs/Rx'
import { isBefore } from 'date-fns'
import uuidv4 from 'uuid/v4'
import Web3 from 'web3'
Expand Down Expand Up @@ -87,16 +87,34 @@ export default class Aragon {
/**
* Initialise the wrapper.
*
* @param {?Array<string>} [accounts=null] An optional array of accounts that the user controls
* @return {Promise<void>}
*/
async init () {
async init (accounts = null) {
await this.initAccounts(accounts)
await this.initAcl()
this.initApps()
this.initForwarders()
this.initNotifications()
this.transactions = new Subject()
}

/**
* Initialise the accounts observable.
*
* @param {?Array<string>} [accounts=null] An optional array of accounts that the user controls
* @return {Promise<void>}
*/
async initAccounts (accounts) {
this.accounts = new ReplaySubject(1)

if (accounts === null) {
accounts = await this.web3.eth.getAccounts()
}

this.setAccounts(accounts)
}

/**
* Initialise the ACL.
*
Expand Down Expand Up @@ -409,7 +427,8 @@ export default class Aragon {
handlers.createRequestHandler(request$, 'notification', handlers.notifications),
handlers.createRequestHandler(request$, 'external_call', handlers.externalCall),
handlers.createRequestHandler(request$, 'external_events', handlers.externalEvents),
handlers.createRequestHandler(request$, 'identify', handlers.identifier)
handlers.createRequestHandler(request$, 'identify', handlers.identifier),
handlers.createRequestHandler(request$, 'accounts', handlers.accounts)
).subscribe(
(response) => messenger.sendResponse(response.id, response.payload)
)
Expand All @@ -425,13 +444,25 @@ export default class Aragon {
}
}

/**
* Set the available accounts for the current user.
*
* @param {Array<string>} accounts
* @return {void}
*/
setAccounts (accounts) {
this.accounts.next(accounts)
}

/**
* Get the available accounts for the current user.
*
* @return {Promise<Array<string>>} An array of addresses
*/
getAccounts () {
return this.web3.eth.getAccounts()
return this.accounts
.take(1)
.toPromise()
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/aragon-wrapper/src/rpc/handlers/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function (request, proxy, wrapper) {
return wrapper.accounts
}
1 change: 1 addition & 0 deletions packages/aragon-wrapper/src/rpc/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ export { default as notifications } from './notifications'
export { call as externalCall } from './external'
export { events as externalEvents } from './external'
export { default as identifier } from './identifier'
export { default as accounts } from './accounts'

0 comments on commit 91274b5

Please sign in to comment.