import { buildSharedState , SharedState , WorkerState } from './WorkerState' ; import BackgroundController from './controllers/BackgroundController' ; import Wallet = require ( 'ethereumjs-wallet' ) ; export default class SharedStateView { private background : BackgroundController ; constructor ( background ) { this . background = background ; } public async getHubUrl ( ) { const state = await this . getState ( ) ; return state . runtime . currentHubUrl ; } public async getAuthRealm ( ) { const state = await this . getState ( ) ; return state . runtime . currentAuthRealm ; } public async isLocked ( ) { const isUnlocked = await this . isUnlocked ( ) ; return ! isUnlocked ; } public async isUnlocked ( ) { const sharedState = await this . getSharedState ( ) ; return ! sharedState . isLocked && sharedState . didInit ; } public awaitUnlock ( ) { return new Promise ( ( resolve ) => this . background . awaitUnlock ( resolve ) ) ; } public async getSharedState ( ) { const state = await this . getState ( ) ; return buildSharedState ( state ) ; } public async getAccounts ( ) { try { const wallet = await this . getWallet ( ) ; return [ wallet . getAddressString ( ) ] ; } catch ( e ) { return [ ] ; } } public async getWallet ( ) { const state = await this . getState ( ) ; const wallet = state . runtime . wallet ; if ( ! wallet ) { throw new Error ( 'Wallet is unavailable.' ) ; } return wallet ; } public async getState ( ) { return this . background . getState ( ) ; } }