From 38896c9358c07d722b2a24d458e6c949b802fa37 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Fri, 9 Nov 2018 00:42:50 -0800 Subject: feat(instant): handle privacy mode in wallets --- packages/instant/src/components/zero_ex_instant_provider.tsx | 2 -- packages/instant/src/constants.ts | 3 --- packages/instant/src/redux/actions.ts | 2 -- packages/instant/src/redux/async_data.ts | 9 +++++++-- packages/instant/src/redux/reducer.ts | 4 +--- packages/instant/src/types.ts | 7 +++---- 6 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/instant/src/components/zero_ex_instant_provider.tsx b/packages/instant/src/components/zero_ex_instant_provider.tsx index 11f867abc..411f118cc 100644 --- a/packages/instant/src/components/zero_ex_instant_provider.tsx +++ b/packages/instant/src/components/zero_ex_instant_provider.tsx @@ -98,7 +98,6 @@ export class ZeroExInstantProvider extends React.Component(type: T, data?: P): PlainAction | export enum ActionTypes { SET_ACCOUNT_STATE_LOADING = 'SET_ACCOUNT_STATE_LOADING', SET_ACCOUNT_STATE_LOCKED = 'SET_ACCOUNT_STATE_LOCKED', - SET_ACCOUNT_STATE_ERROR = 'SET_ACCOUNT_STATE_ERROR', SET_ACCOUNT_STATE_READY = 'SET_ACCOUNT_STATE_READY', UPDATE_ACCOUNT_ETH_BALANCE = 'UPDATE_ACCOUNT_ETH_BALANCE', UPDATE_ETH_USD_PRICE = 'UPDATE_ETH_USD_PRICE', @@ -47,7 +46,6 @@ export enum ActionTypes { export const actions = { setAccountStateLoading: () => createAction(ActionTypes.SET_ACCOUNT_STATE_LOADING), setAccountStateLocked: () => createAction(ActionTypes.SET_ACCOUNT_STATE_LOCKED), - setAccountStateError: () => createAction(ActionTypes.SET_ACCOUNT_STATE_ERROR), setAccountStateReady: (address: string) => createAction(ActionTypes.SET_ACCOUNT_STATE_READY, address), updateAccountEthBalance: (addressAndBalance: AddressAndEthBalanceInWei) => createAction(ActionTypes.UPDATE_ACCOUNT_ETH_BALANCE, addressAndBalance), diff --git a/packages/instant/src/redux/async_data.ts b/packages/instant/src/redux/async_data.ts index a47c7a605..b920ac914 100644 --- a/packages/instant/src/redux/async_data.ts +++ b/packages/instant/src/redux/async_data.ts @@ -40,14 +40,19 @@ export const asyncData = { const { store, shouldSetToLoading } = options; const { providerState } = store.getState(); const web3Wrapper = providerState.web3Wrapper; + const provider = providerState.provider; if (shouldSetToLoading && providerState.account.state !== AccountState.Loading) { store.dispatch(actions.setAccountStateLoading()); } let availableAddresses: string[]; try { - availableAddresses = await web3Wrapper.getAvailableAddressesAsync(); + // TODO(bmillman): Add support at the web3Wrapper level for calling `eth_requestAccounts` instead of calling enable here + const isPrivacyModeEnabled = !_.isUndefined((provider as any).enable); + availableAddresses = isPrivacyModeEnabled + ? await (provider as any).enable() + : await web3Wrapper.getAvailableAddressesAsync(); } catch (e) { - store.dispatch(actions.setAccountStateError()); + store.dispatch(actions.setAccountStateLocked()); return; } if (!_.isEmpty(availableAddresses)) { diff --git a/packages/instant/src/redux/reducer.ts b/packages/instant/src/redux/reducer.ts index a5a1b6f7d..ef46fdd9d 100644 --- a/packages/instant/src/redux/reducer.ts +++ b/packages/instant/src/redux/reducer.ts @@ -4,7 +4,7 @@ import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; -import { ERROR_ACCOUNT, LOADING_ACCOUNT, LOCKED_ACCOUNT } from '../constants'; +import { LOADING_ACCOUNT, LOCKED_ACCOUNT } from '../constants'; import { assetMetaDataMap } from '../data/asset_meta_data_map'; import { Account, @@ -65,8 +65,6 @@ export const createReducer = (initialState: State) => { return reduceStateWithAccount(state, LOADING_ACCOUNT); case ActionTypes.SET_ACCOUNT_STATE_LOCKED: return reduceStateWithAccount(state, LOCKED_ACCOUNT); - case ActionTypes.SET_ACCOUNT_STATE_ERROR: - return reduceStateWithAccount(state, ERROR_ACCOUNT); case ActionTypes.SET_ACCOUNT_STATE_READY: { const account: AccountReady = { state: AccountState.Ready, diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts index 62afe652d..b43a82d46 100644 --- a/packages/instant/src/types.ts +++ b/packages/instant/src/types.ts @@ -102,11 +102,10 @@ export interface ProviderState { } export enum AccountState { + None = 'NONE,', Loading = 'LOADING', Ready = 'READY', - Locked = 'LOCKED', // TODO(bmillman): break this up into locked / privacy mode enabled - Error = 'ERROR', - None = 'NONE,', + Locked = 'LOCKED', } export interface AccountReady { @@ -115,7 +114,7 @@ export interface AccountReady { ethBalanceInWei?: BigNumber; } export interface AccountNotReady { - state: AccountState.None | AccountState.Loading | AccountState.Locked | AccountState.Error; + state: AccountState.None | AccountState.Loading | AccountState.Locked; } export type Account = AccountReady | AccountNotReady; -- cgit