From 40724224b1814ab4fedb4307858edb0602ef8db2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Mon, 18 Jun 2018 18:00:27 +0200 Subject: [PATCH] unify all constants --- src/api/Ledger.js | 5 +-- src/components/CalculateBalance.js | 5 +++ src/config/constants.js | 45 ++++++++++++++++++- src/config/languages.js | 3 +- src/helpers/apps/listApps.js | 4 +- src/helpers/common.js | 12 ++++- src/helpers/constants.js | 13 ------ src/helpers/deviceAccess.js | 3 +- src/helpers/devices/getFirmwareInfo.js | 13 +++--- src/helpers/devices/getIsGenuine.js | 3 +- .../devices/getLatestFirmwareForDevice.js | 6 +-- src/helpers/libcore.js | 3 +- src/internals/index.js | 3 +- src/logger.js | 21 ++++++--- src/reducers/onboarding.js | 3 +- src/renderer/i18n/instanciate.js | 3 +- src/renderer/init.js | 3 +- 17 files changed, 104 insertions(+), 44 deletions(-) delete mode 100644 src/helpers/constants.js diff --git a/src/api/Ledger.js b/src/api/Ledger.js index 2c84ab5d..f84d0f7c 100644 --- a/src/api/Ledger.js +++ b/src/api/Ledger.js @@ -1,7 +1,6 @@ // @flow import type { Currency } from '@ledgerhq/live-common/lib/types' - -const BASE_URL = process.env.LEDGER_REST_API_BASE || 'https://api.ledgerwallet.com/' +import { LEDGER_REST_API_BASE } from 'config/constants' export const blockchainBaseURL = ({ ledgerExplorerId }: Currency): ?string => - ledgerExplorerId ? `${BASE_URL}blockchain/v2/${ledgerExplorerId}` : null + ledgerExplorerId ? `${LEDGER_REST_API_BASE}blockchain/v2/${ledgerExplorerId}` : null diff --git a/src/components/CalculateBalance.js b/src/components/CalculateBalance.js index 709f7114..d94f0b6e 100644 --- a/src/components/CalculateBalance.js +++ b/src/components/CalculateBalance.js @@ -79,7 +79,12 @@ const mapStateToProps = (state: State, props: OwnProps) => { } } +const hash = ({ balanceHistory, balanceEnd }) => `${balanceHistory.length}_${balanceEnd}` + class CalculateBalance extends Component { + shouldComponentUpdate(nextProps) { + return hash(nextProps) !== hash(this.props) + } render() { const { children } = this.props return children(this.props) diff --git a/src/config/constants.js b/src/config/constants.js index 343b4ed5..74ca72ef 100644 --- a/src/config/constants.js +++ b/src/config/constants.js @@ -1,10 +1,18 @@ // @flow -const intFromEnv = (key: string, def: number) => { +const intFromEnv = (key: string, def: number): number => { const v = process.env[key] if (!isNaN(v)) return parseInt(v, 10) return def } +const boolFromEnv = (key: string): boolean => { + const v = process.env[key] + return (v && v !== '0' && v !== 'false') || false +} + +const stringFromEnv = (key: string, def: string): string => process.env[key] || def + +// time and delays... export const GET_CALLS_TIMEOUT = intFromEnv('GET_CALLS_TIMEOUT', 30 * 1000) export const GET_CALLS_RETRY = intFromEnv('GET_CALLS_RETRY', 2) @@ -19,6 +27,41 @@ export const CHECK_UPDATE_DELAY = 5e3 export const DEVICE_DISCONNECT_DEBOUNCE = intFromEnv('LEDGER_DEVICE_DISCONNECT_DEBOUNCE', 500) +// Endpoints... + +export const LEDGER_REST_API_BASE = stringFromEnv( + 'LEDGER_REST_API_BASE', + 'https://api.ledgerwallet.com/', +) +export const MANAGER_API_BASE = stringFromEnv( + 'MANAGER_API_BASE', + 'https://beta.manager.live.ledger.fr/api', +) +export const BASE_SOCKET_URL = stringFromEnv('BASE_SOCKET_URL', 'ws://api.ledgerwallet.com/update') +export const BASE_SOCKET_URL_SECURE = stringFromEnv( + 'BASE_SOCKET_URL', + 'wss://api.ledgerwallet.com/update', +) + +// Flags + +export const DEBUG_DEVICE = boolFromEnv('DEBUG_DEVICE') +export const DEBUG_NETWORK = boolFromEnv('DEBUG_NETWORK') +export const DEBUG_COMMANDS = boolFromEnv('DEBUG_COMMANDS') +export const DEBUG_DB = boolFromEnv('DEBUG_DB') +export const DEBUG_ACTION = boolFromEnv('DEBUG_ACTION') +export const DEBUG_TAB_KEY = boolFromEnv('DEBUG_TAB_KEY') +export const DEBUG_LIBCORE = boolFromEnv('DEBUG_LIBCORE') +export const DEBUG_WS = boolFromEnv('DEBUG_WS') +export const LEDGER_RESET_ALL = boolFromEnv('LEDGER_RESET_ALL') +export const LEDGER_DEBUG_ALL_LANGS = boolFromEnv('LEDGER_DEBUG_ALL_LANGS') +export const SKIP_GENUINE = boolFromEnv('SKIP_GENUINE') +export const SKIP_ONBOARDING = boolFromEnv('SKIP_ONBOARDING') +export const SHOW_LEGACY_NEW_ACCOUNT = boolFromEnv('SHOW_LEGACY_NEW_ACCOUNT') +export const HIGHLIGHT_I18N = boolFromEnv('HIGHLIGHT_I18N') + +// Other constants + export const MODAL_ADD_ACCOUNTS = 'MODAL_ADD_ACCOUNTS' export const MODAL_OPERATION_DETAILS = 'MODAL_OPERATION_DETAILS' export const MODAL_RECEIVE = 'MODAL_RECEIVE' diff --git a/src/config/languages.js b/src/config/languages.js index 96e3d3a9..29774a7f 100644 --- a/src/config/languages.js +++ b/src/config/languages.js @@ -1,6 +1,7 @@ // @flow +import { LEDGER_DEBUG_ALL_LANGS } from 'config/constants' const allLanguages = ['en', 'fr'] const prodStableLanguages = ['en'] -const languages = process.env.LEDGER_DEBUG_ALL_LANGS ? allLanguages : prodStableLanguages +const languages = LEDGER_DEBUG_ALL_LANGS ? allLanguages : prodStableLanguages export default languages diff --git a/src/helpers/apps/listApps.js b/src/helpers/apps/listApps.js index edd54366..becd8373 100644 --- a/src/helpers/apps/listApps.js +++ b/src/helpers/apps/listApps.js @@ -1,12 +1,12 @@ // @flow import axios from 'axios' -import { API_BASE_URL } from 'helpers/constants' +import { MANAGER_API_BASE } from 'config/constants' export default async (targetId: string | number) => { try { const { data: deviceData } = await axios.get( - `${API_BASE_URL}/device_versions_target_id/${targetId}`, + `${MANAGER_API_BASE}/device_versions_target_id/${targetId}`, ) const { data } = await axios.get('https://api.ledgerwallet.com/update/applications') diff --git a/src/helpers/common.js b/src/helpers/common.js index a5a28c8c..34b34f09 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -4,9 +4,15 @@ import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' +import { BASE_SOCKET_URL, BASE_SOCKET_URL_SECURE } from 'config/constants' import { createDeviceSocket } from './socket' -import { BASE_SOCKET_URL, APDUS, MANAGER_API_URL } from './constants' +const APDUS = { + GET_FIRMWARE: [0xe0, 0x01, 0x00, 0x00], + // we dont have common call that works inside app & dashboard + // TODO: this should disappear. + GET_FIRMWARE_FALLBACK: [0xe0, 0xc4, 0x00, 0x00], +} export type LedgerScriptParams = { firmware?: string, @@ -35,7 +41,9 @@ export async function createSocketDialog( managerUrl: boolean = false, ): Promise { console.warn('DEPRECATED createSocketDialog: use createDeviceSocket') // eslint-disable-line - const url = `${managerUrl ? MANAGER_API_URL : BASE_SOCKET_URL}${endpoint}?${qs.stringify(params)}` + const url = `${managerUrl ? BASE_SOCKET_URL_SECURE : BASE_SOCKET_URL}${endpoint}?${qs.stringify( + params, + )}` return createDeviceSocket(transport, url).toPromise() } diff --git a/src/helpers/constants.js b/src/helpers/constants.js deleted file mode 100644 index 254ede23..00000000 --- a/src/helpers/constants.js +++ /dev/null @@ -1,13 +0,0 @@ -// Socket endpoint - -export const BASE_SOCKET_URL = 'ws://api.ledgerwallet.com/update' -export const MANAGER_API_URL = 'wss://api.ledgerwallet.com/update' -export const API_BASE_URL = process.env.API_BASE_URL || 'https://beta.manager.live.ledger.fr/api' - -// List of APDUS -export const APDUS = { - GET_FIRMWARE: [0xe0, 0x01, 0x00, 0x00], - // we dont have common call that works inside app & dashboard - // TODO: this should disappear. - GET_FIRMWARE_FALLBACK: [0xe0, 0xc4, 0x00, 0x00], -} diff --git a/src/helpers/deviceAccess.js b/src/helpers/deviceAccess.js index 7d857f4a..34aa3499 100644 --- a/src/helpers/deviceAccess.js +++ b/src/helpers/deviceAccess.js @@ -2,6 +2,7 @@ import createSemaphore from 'semaphore' import type Transport from '@ledgerhq/hw-transport' import TransportNodeHid from '@ledgerhq/hw-transport-node-hid' +import { DEBUG_DEVICE } from 'config/constants' import { retry } from './promise' // all open to device must use openDevice so we can prevent race conditions @@ -20,7 +21,7 @@ export const withDevice: WithDevice = devicePath => { takeSemaphorePromise(sem, async () => { const t = await retry(() => TransportNodeHid.open(devicePath), { maxRetry: 1 }) - if (process.env.DEBUG_DEVICE > 0) t.setDebugMode(true) + if (DEBUG_DEVICE) t.setDebugMode(true) try { const res = await job(t) // $FlowFixMe diff --git a/src/helpers/devices/getFirmwareInfo.js b/src/helpers/devices/getFirmwareInfo.js index f0fd05fa..389cbd1b 100644 --- a/src/helpers/devices/getFirmwareInfo.js +++ b/src/helpers/devices/getFirmwareInfo.js @@ -2,7 +2,7 @@ import axios from 'axios' import isEmpty from 'lodash/isEmpty' -import { API_BASE_URL } from 'helpers/constants' +import { MANAGER_API_BASE } from 'config/constants' type Input = { version: string, @@ -12,10 +12,13 @@ type Input = { let error export default async (data: Input) => { try { - const { data: seFirmwareVersion } = await axios.post(`${API_BASE_URL}/firmware_versions_name`, { - se_firmware_name: data.version, - target_id: data.targetId, - }) + const { data: seFirmwareVersion } = await axios.post( + `${MANAGER_API_BASE}/firmware_versions_name`, + { + se_firmware_name: data.version, + target_id: data.targetId, + }, + ) if (!isEmpty(seFirmwareVersion)) { return seFirmwareVersion diff --git a/src/helpers/devices/getIsGenuine.js b/src/helpers/devices/getIsGenuine.js index 9af44aba..ad82420f 100644 --- a/src/helpers/devices/getIsGenuine.js +++ b/src/helpers/devices/getIsGenuine.js @@ -1,11 +1,12 @@ // @flow import type Transport from '@ledgerhq/hw-transport' import { createSocketDialog } from 'helpers/common' +import { SKIP_GENUINE } from 'config/constants' export default async ( transport: Transport<*>, { targetId }: { targetId: string | number }, ): Promise => - process.env.SKIP_GENUINE > 0 + SKIP_GENUINE ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) : createSocketDialog(transport, '/genuine', { targetId }, true) diff --git a/src/helpers/devices/getLatestFirmwareForDevice.js b/src/helpers/devices/getLatestFirmwareForDevice.js index 9b081681..f3f2ae17 100644 --- a/src/helpers/devices/getLatestFirmwareForDevice.js +++ b/src/helpers/devices/getLatestFirmwareForDevice.js @@ -1,7 +1,7 @@ // @flow import axios from 'axios' import isEmpty from 'lodash/isEmpty' -import { API_BASE_URL } from 'helpers/constants' +import { MANAGER_API_BASE } from 'config/constants' import getFirmwareInfo from './getFirmwareInfo' @@ -17,11 +17,11 @@ export default async (data: Input) => { // Get device infos from targetId const { data: deviceVersion } = await axios.get( - `${API_BASE_URL}/device_versions_target_id/${data.targetId}`, + `${MANAGER_API_BASE}/device_versions_target_id/${data.targetId}`, ) // Fetch next possible firmware - const { data: serverData } = await axios.post(`${API_BASE_URL}/get_latest_firmware`, { + const { data: serverData } = await axios.post(`${MANAGER_API_BASE}/get_latest_firmware`, { current_se_firmware_version: seFirmwareVersion.id, device_version: deviceVersion.id, providers: [1], diff --git a/src/helpers/libcore.js b/src/helpers/libcore.js index b04049c3..3ab6f875 100644 --- a/src/helpers/libcore.js +++ b/src/helpers/libcore.js @@ -4,6 +4,7 @@ import logger from 'logger' import Btc from '@ledgerhq/hw-app-btc' import { withDevice } from 'helpers/deviceAccess' import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' +import { SHOW_LEGACY_NEW_ACCOUNT } from 'config/constants' import type { AccountRaw, OperationRaw, OperationType } from '@ledgerhq/live-common/lib/types' import type { NJSAccount, NJSOperation } from '@ledgerhq/ledger-core/src/ledgercore_doc' @@ -28,8 +29,6 @@ type Props = { onAccountScanned: AccountRaw => void, } -const { SHOW_LEGACY_NEW_ACCOUNT } = process.env - export function scanAccountsOnDevice(props: Props): Promise { const { devicePath, currencyId, onAccountScanned, core } = props const currency = getCryptoCurrencyById(currencyId) diff --git a/src/internals/index.js b/src/internals/index.js index 6c82d86f..c9d156bb 100644 --- a/src/internals/index.js +++ b/src/internals/index.js @@ -4,6 +4,7 @@ import logger from 'logger' import uuid from 'uuid/v4' import { setImplementation } from 'api/network' import sentry from 'sentry/node' +import { DEBUG_NETWORK } from 'config/constants' require('../env') @@ -15,7 +16,7 @@ let sentryEnabled = process.env.INITIAL_SENTRY_ENABLED || false sentry(() => sentryEnabled, process.env.SENTRY_USER_ID) -if (process.env.DEBUG_NETWORK) { +if (DEBUG_NETWORK) { setImplementation(networkArg => { const id = uuid() return new Promise((resolve, reject) => { diff --git a/src/logger.js b/src/logger.js index b8fe6ca8..cc6cfc82 100644 --- a/src/logger.js +++ b/src/logger.js @@ -9,6 +9,15 @@ * - for analytics in the future */ +import { + DEBUG_COMMANDS, + DEBUG_DB, + DEBUG_ACTION, + DEBUG_TAB_KEY, + DEBUG_LIBCORE, + DEBUG_WS, +} from 'config/constants' + const logs = [] const MAX_LOG_LENGTH = 500 @@ -47,12 +56,12 @@ const makeSerializableLog = (o: mixed) => { return String(o) } -const logCmds = !__DEV__ || process.env.DEBUG_COMMANDS -const logDb = !__DEV__ || process.env.DEBUG_DB -const logRedux = !__DEV__ || process.env.DEBUG_ACTION -const logTabkey = !__DEV__ || process.env.DEBUG_TAB_KEY -const logLibcore = !__DEV__ || process.env.DEBUG_LIBCORE -const logWS = !__DEV__ || process.env.DEBUG_WS +const logCmds = !__DEV__ || DEBUG_COMMANDS +const logDb = !__DEV__ || DEBUG_DB +const logRedux = !__DEV__ || DEBUG_ACTION +const logTabkey = !__DEV__ || DEBUG_TAB_KEY +const logLibcore = !__DEV__ || DEBUG_LIBCORE +const logWS = !__DEV__ || DEBUG_WS export default { onCmd: (type: string, id: string, spentTime: number, data?: any) => { diff --git a/src/reducers/onboarding.js b/src/reducers/onboarding.js index 22816556..8fd43f1b 100644 --- a/src/reducers/onboarding.js +++ b/src/reducers/onboarding.js @@ -1,5 +1,6 @@ // @flow +import { SKIP_ONBOARDING } from 'config/constants' import { handleActions, createAction } from 'redux-actions' type Step = { @@ -29,7 +30,7 @@ export type OnboardingState = { const state: OnboardingState = { stepIndex: 0, - stepName: process.env.SKIP_ONBOARDING ? 'finish' : 'start', + stepName: SKIP_ONBOARDING ? 'finish' : 'start', genuine: { pinStepPass: false, recoveryStepPass: false, diff --git a/src/renderer/i18n/instanciate.js b/src/renderer/i18n/instanciate.js index f6e6213a..e4217dfe 100644 --- a/src/renderer/i18n/instanciate.js +++ b/src/renderer/i18n/instanciate.js @@ -1,3 +1,4 @@ +import { HIGHLIGHT_I18N } from 'config/constants' import i18n from 'i18next' const commonConfig = { @@ -29,7 +30,7 @@ export function createWithBackend(backend, backendOpts) { ...backendOpts, } - if (process.env.HIGHLIGHT_I18N) { + if (HIGHLIGHT_I18N) { config.postProcess = 'highlight' } diff --git a/src/renderer/init.js b/src/renderer/init.js index d902122d..0ad9407d 100644 --- a/src/renderer/init.js +++ b/src/renderer/init.js @@ -11,6 +11,7 @@ import moment from 'moment' import createStore from 'renderer/createStore' import events from 'renderer/events' +import { LEDGER_RESET_ALL } from 'config/constants' import { enableGlobalTab, disableGlobalTab, isGlobalTabEnabled } from 'config/global-tab' import { fetchAccounts } from 'actions/accounts' @@ -34,7 +35,7 @@ const rootNode = document.getElementById('app') const TAB_KEY = 9 async function init() { - if (process.env.LEDGER_RESET_ALL) { + if (LEDGER_RESET_ALL) { await hardReset() }