Browse Source

Merge pull request #602 from gre/unify-constants

unify all constants
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
3889a6d129
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/api/Ledger.js
  2. 5
      src/components/CalculateBalance.js
  3. 45
      src/config/constants.js
  4. 3
      src/config/languages.js
  5. 4
      src/helpers/apps/listApps.js
  6. 12
      src/helpers/common.js
  7. 13
      src/helpers/constants.js
  8. 3
      src/helpers/deviceAccess.js
  9. 9
      src/helpers/devices/getFirmwareInfo.js
  10. 3
      src/helpers/devices/getIsGenuine.js
  11. 6
      src/helpers/devices/getLatestFirmwareForDevice.js
  12. 3
      src/helpers/libcore.js
  13. 3
      src/internals/index.js
  14. 21
      src/logger.js
  15. 3
      src/reducers/onboarding.js
  16. 3
      src/renderer/i18n/instanciate.js
  17. 3
      src/renderer/init.js

5
src/api/Ledger.js

@ -1,7 +1,6 @@
// @flow // @flow
import type { Currency } from '@ledgerhq/live-common/lib/types' import type { Currency } from '@ledgerhq/live-common/lib/types'
import { LEDGER_REST_API_BASE } from 'config/constants'
const BASE_URL = process.env.LEDGER_REST_API_BASE || 'https://api.ledgerwallet.com/'
export const blockchainBaseURL = ({ ledgerExplorerId }: Currency): ?string => export const blockchainBaseURL = ({ ledgerExplorerId }: Currency): ?string =>
ledgerExplorerId ? `${BASE_URL}blockchain/v2/${ledgerExplorerId}` : null ledgerExplorerId ? `${LEDGER_REST_API_BASE}blockchain/v2/${ledgerExplorerId}` : null

5
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<Props> { class CalculateBalance extends Component<Props> {
shouldComponentUpdate(nextProps) {
return hash(nextProps) !== hash(this.props)
}
render() { render() {
const { children } = this.props const { children } = this.props
return children(this.props) return children(this.props)

45
src/config/constants.js

@ -1,10 +1,18 @@
// @flow // @flow
const intFromEnv = (key: string, def: number) => { const intFromEnv = (key: string, def: number): number => {
const v = process.env[key] const v = process.env[key]
if (!isNaN(v)) return parseInt(v, 10) if (!isNaN(v)) return parseInt(v, 10)
return def 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_TIMEOUT = intFromEnv('GET_CALLS_TIMEOUT', 30 * 1000)
export const GET_CALLS_RETRY = intFromEnv('GET_CALLS_RETRY', 2) 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) 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_ADD_ACCOUNTS = 'MODAL_ADD_ACCOUNTS'
export const MODAL_OPERATION_DETAILS = 'MODAL_OPERATION_DETAILS' export const MODAL_OPERATION_DETAILS = 'MODAL_OPERATION_DETAILS'
export const MODAL_RECEIVE = 'MODAL_RECEIVE' export const MODAL_RECEIVE = 'MODAL_RECEIVE'

3
src/config/languages.js

@ -1,6 +1,7 @@
// @flow // @flow
import { LEDGER_DEBUG_ALL_LANGS } from 'config/constants'
const allLanguages = ['en', 'fr'] const allLanguages = ['en', 'fr']
const prodStableLanguages = ['en'] const prodStableLanguages = ['en']
const languages = process.env.LEDGER_DEBUG_ALL_LANGS ? allLanguages : prodStableLanguages const languages = LEDGER_DEBUG_ALL_LANGS ? allLanguages : prodStableLanguages
export default languages export default languages

4
src/helpers/apps/listApps.js

@ -1,12 +1,12 @@
// @flow // @flow
import axios from 'axios' import axios from 'axios'
import { API_BASE_URL } from 'helpers/constants' import { MANAGER_API_BASE } from 'config/constants'
export default async (targetId: string | number) => { export default async (targetId: string | number) => {
try { try {
const { data: deviceData } = await axios.get( 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') const { data } = await axios.get('https://api.ledgerwallet.com/update/applications')

12
src/helpers/common.js

@ -4,9 +4,15 @@
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL, BASE_SOCKET_URL_SECURE } from 'config/constants'
import { createDeviceSocket } from './socket' 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 = { export type LedgerScriptParams = {
firmware?: string, firmware?: string,
@ -35,7 +41,9 @@ export async function createSocketDialog(
managerUrl: boolean = false, managerUrl: boolean = false,
): Promise<string> { ): Promise<string> {
console.warn('DEPRECATED createSocketDialog: use createDeviceSocket') // eslint-disable-line 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() return createDeviceSocket(transport, url).toPromise()
} }

13
src/helpers/constants.js

@ -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],
}

3
src/helpers/deviceAccess.js

@ -2,6 +2,7 @@
import createSemaphore from 'semaphore' import createSemaphore from 'semaphore'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import TransportNodeHid from '@ledgerhq/hw-transport-node-hid' import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'
import { DEBUG_DEVICE } from 'config/constants'
import { retry } from './promise' import { retry } from './promise'
// all open to device must use openDevice so we can prevent race conditions // 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 () => { takeSemaphorePromise(sem, async () => {
const t = await retry(() => TransportNodeHid.open(devicePath), { maxRetry: 1 }) 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 { try {
const res = await job(t) const res = await job(t)
// $FlowFixMe // $FlowFixMe

9
src/helpers/devices/getFirmwareInfo.js

@ -2,7 +2,7 @@
import axios from 'axios' import axios from 'axios'
import isEmpty from 'lodash/isEmpty' import isEmpty from 'lodash/isEmpty'
import { API_BASE_URL } from 'helpers/constants' import { MANAGER_API_BASE } from 'config/constants'
type Input = { type Input = {
version: string, version: string,
@ -12,10 +12,13 @@ type Input = {
let error let error
export default async (data: Input) => { export default async (data: Input) => {
try { try {
const { data: seFirmwareVersion } = await axios.post(`${API_BASE_URL}/firmware_versions_name`, { const { data: seFirmwareVersion } = await axios.post(
`${MANAGER_API_BASE}/firmware_versions_name`,
{
se_firmware_name: data.version, se_firmware_name: data.version,
target_id: data.targetId, target_id: data.targetId,
}) },
)
if (!isEmpty(seFirmwareVersion)) { if (!isEmpty(seFirmwareVersion)) {
return seFirmwareVersion return seFirmwareVersion

3
src/helpers/devices/getIsGenuine.js

@ -1,11 +1,12 @@
// @flow // @flow
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { createSocketDialog } from 'helpers/common' import { createSocketDialog } from 'helpers/common'
import { SKIP_GENUINE } from 'config/constants'
export default async ( export default async (
transport: Transport<*>, transport: Transport<*>,
{ targetId }: { targetId: string | number }, { targetId }: { targetId: string | number },
): Promise<string> => ): Promise<string> =>
process.env.SKIP_GENUINE > 0 SKIP_GENUINE
? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000))
: createSocketDialog(transport, '/genuine', { targetId }, true) : createSocketDialog(transport, '/genuine', { targetId }, true)

6
src/helpers/devices/getLatestFirmwareForDevice.js

@ -1,7 +1,7 @@
// @flow // @flow
import axios from 'axios' import axios from 'axios'
import isEmpty from 'lodash/isEmpty' import isEmpty from 'lodash/isEmpty'
import { API_BASE_URL } from 'helpers/constants' import { MANAGER_API_BASE } from 'config/constants'
import getFirmwareInfo from './getFirmwareInfo' import getFirmwareInfo from './getFirmwareInfo'
@ -17,11 +17,11 @@ export default async (data: Input) => {
// Get device infos from targetId // Get device infos from targetId
const { data: deviceVersion } = await axios.get( 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 // 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, current_se_firmware_version: seFirmwareVersion.id,
device_version: deviceVersion.id, device_version: deviceVersion.id,
providers: [1], providers: [1],

3
src/helpers/libcore.js

@ -4,6 +4,7 @@ import logger from 'logger'
import Btc from '@ledgerhq/hw-app-btc' import Btc from '@ledgerhq/hw-app-btc'
import { withDevice } from 'helpers/deviceAccess' import { withDevice } from 'helpers/deviceAccess'
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' 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 { AccountRaw, OperationRaw, OperationType } from '@ledgerhq/live-common/lib/types'
import type { NJSAccount, NJSOperation } from '@ledgerhq/ledger-core/src/ledgercore_doc' import type { NJSAccount, NJSOperation } from '@ledgerhq/ledger-core/src/ledgercore_doc'
@ -28,8 +29,6 @@ type Props = {
onAccountScanned: AccountRaw => void, onAccountScanned: AccountRaw => void,
} }
const { SHOW_LEGACY_NEW_ACCOUNT } = process.env
export function scanAccountsOnDevice(props: Props): Promise<AccountRaw[]> { export function scanAccountsOnDevice(props: Props): Promise<AccountRaw[]> {
const { devicePath, currencyId, onAccountScanned, core } = props const { devicePath, currencyId, onAccountScanned, core } = props
const currency = getCryptoCurrencyById(currencyId) const currency = getCryptoCurrencyById(currencyId)

3
src/internals/index.js

@ -4,6 +4,7 @@ import logger from 'logger'
import uuid from 'uuid/v4' import uuid from 'uuid/v4'
import { setImplementation } from 'api/network' import { setImplementation } from 'api/network'
import sentry from 'sentry/node' import sentry from 'sentry/node'
import { DEBUG_NETWORK } from 'config/constants'
require('../env') require('../env')
@ -15,7 +16,7 @@ let sentryEnabled = process.env.INITIAL_SENTRY_ENABLED || false
sentry(() => sentryEnabled, process.env.SENTRY_USER_ID) sentry(() => sentryEnabled, process.env.SENTRY_USER_ID)
if (process.env.DEBUG_NETWORK) { if (DEBUG_NETWORK) {
setImplementation(networkArg => { setImplementation(networkArg => {
const id = uuid() const id = uuid()
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

21
src/logger.js

@ -9,6 +9,15 @@
* - for analytics in the future * - 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 logs = []
const MAX_LOG_LENGTH = 500 const MAX_LOG_LENGTH = 500
@ -47,12 +56,12 @@ const makeSerializableLog = (o: mixed) => {
return String(o) return String(o)
} }
const logCmds = !__DEV__ || process.env.DEBUG_COMMANDS const logCmds = !__DEV__ || DEBUG_COMMANDS
const logDb = !__DEV__ || process.env.DEBUG_DB const logDb = !__DEV__ || DEBUG_DB
const logRedux = !__DEV__ || process.env.DEBUG_ACTION const logRedux = !__DEV__ || DEBUG_ACTION
const logTabkey = !__DEV__ || process.env.DEBUG_TAB_KEY const logTabkey = !__DEV__ || DEBUG_TAB_KEY
const logLibcore = !__DEV__ || process.env.DEBUG_LIBCORE const logLibcore = !__DEV__ || DEBUG_LIBCORE
const logWS = !__DEV__ || process.env.DEBUG_WS const logWS = !__DEV__ || DEBUG_WS
export default { export default {
onCmd: (type: string, id: string, spentTime: number, data?: any) => { onCmd: (type: string, id: string, spentTime: number, data?: any) => {

3
src/reducers/onboarding.js

@ -1,5 +1,6 @@
// @flow // @flow
import { SKIP_ONBOARDING } from 'config/constants'
import { handleActions, createAction } from 'redux-actions' import { handleActions, createAction } from 'redux-actions'
type Step = { type Step = {
@ -29,7 +30,7 @@ export type OnboardingState = {
const state: OnboardingState = { const state: OnboardingState = {
stepIndex: 0, stepIndex: 0,
stepName: process.env.SKIP_ONBOARDING ? 'finish' : 'start', stepName: SKIP_ONBOARDING ? 'finish' : 'start',
genuine: { genuine: {
pinStepPass: false, pinStepPass: false,
recoveryStepPass: false, recoveryStepPass: false,

3
src/renderer/i18n/instanciate.js

@ -1,3 +1,4 @@
import { HIGHLIGHT_I18N } from 'config/constants'
import i18n from 'i18next' import i18n from 'i18next'
const commonConfig = { const commonConfig = {
@ -29,7 +30,7 @@ export function createWithBackend(backend, backendOpts) {
...backendOpts, ...backendOpts,
} }
if (process.env.HIGHLIGHT_I18N) { if (HIGHLIGHT_I18N) {
config.postProcess = 'highlight' config.postProcess = 'highlight'
} }

3
src/renderer/init.js

@ -11,6 +11,7 @@ import moment from 'moment'
import createStore from 'renderer/createStore' import createStore from 'renderer/createStore'
import events from 'renderer/events' import events from 'renderer/events'
import { LEDGER_RESET_ALL } from 'config/constants'
import { enableGlobalTab, disableGlobalTab, isGlobalTabEnabled } from 'config/global-tab' import { enableGlobalTab, disableGlobalTab, isGlobalTabEnabled } from 'config/global-tab'
import { fetchAccounts } from 'actions/accounts' import { fetchAccounts } from 'actions/accounts'
@ -34,7 +35,7 @@ const rootNode = document.getElementById('app')
const TAB_KEY = 9 const TAB_KEY = 9
async function init() { async function init() {
if (process.env.LEDGER_RESET_ALL) { if (LEDGER_RESET_ALL) {
await hardReset() await hardReset()
} }

Loading…
Cancel
Save